1313import org .junit .jupiter .api .Test ;
1414import org .junit .jupiter .api .Timeout ;
1515import org .springframework .beans .factory .annotation .Autowired ;
16+ import org .springframework .boot .resttestclient .TestRestTemplate ;
1617import org .springframework .boot .test .context .SpringBootTest ;
17- import org .springframework .boot .test .web .client . TestRestTemplate ;
18+ import org .springframework .boot .test .web .server . LocalServerPort ;
1819import org .springframework .http .HttpEntity ;
1920import org .springframework .http .HttpMethod ;
2021import org .springframework .http .HttpStatus ;
@@ -42,8 +43,10 @@ public abstract class AbstractTLRControllerTest {
4243 private final String runPipelineAndWaitEndpoint ;
4344 private final TraceLinkType traceLinkType ;
4445
45- @ Autowired
46- protected TestRestTemplate restTemplate ;
46+ @ LocalServerPort
47+ private int port ;
48+
49+ protected TestRestTemplate restTemplate = new TestRestTemplate ();
4750
4851 @ Autowired
4952 protected RedisAccessor redisAccessor ;
@@ -79,6 +82,10 @@ public AbstractTLRControllerTest(TraceLinkType traceLinkType) {
7982 this .runPipelineAndWaitEndpoint = String .format ("/api/%s/start-and-wait" , endpointName );
8083 }
8184
85+ private String getBaseUrl () {
86+ return "http://localhost:" + port ;
87+ }
88+
8289 // Common test method for starting pipeline and getting results
8390 @ Timeout (value = 6 , unit = TimeUnit .MINUTES )
8491 protected void runPipeline_start_and_getResult (HttpEntity <MultiValueMap <String , Object >> requestEntity ) throws IOException {
@@ -103,30 +110,33 @@ void testRetrievingResultForInvalidId() {
103110 String invalidId = "invalid-project-id" ;
104111
105112 // testGetResult
106- ResponseEntity <ErrorResponse > responseEntity = restTemplate .getForEntity (GET_RESULT_ENDPOINT , ErrorResponse .class , invalidId );
113+ String getUrl = getBaseUrl () + GET_RESULT_ENDPOINT ;
114+ ResponseEntity <ErrorResponse > responseEntity = restTemplate .getForEntity (getUrl , ErrorResponse .class , invalidId );
107115 TestUtils .testInvalidRequestId (responseEntity , invalidId );
108116
109117 // testWaitForResult
110- responseEntity = restTemplate .getForEntity (WAIT_FOR_RESULT_ENDPOINT , ErrorResponse .class , invalidId );
118+ String waitUrl = getBaseUrl () + WAIT_FOR_RESULT_ENDPOINT ;
119+ responseEntity = restTemplate .getForEntity (waitUrl , ErrorResponse .class , invalidId );
111120 TestUtils .testInvalidRequestId (responseEntity , invalidId );
112121 }
113122
114123 @ Test
115124 void testHandleEmptyFile () {
116125 HttpEntity <MultiValueMap <String , Object >> requestEntity = prepareRequestEntityForEmptyFileTest ("emptyFileProject" );
117126
118- ResponseEntity <ErrorResponse > responseEntity = restTemplate .exchange (runPipelineEndpoint , HttpMethod .POST , requestEntity , ErrorResponse .class );
127+ ResponseEntity <ErrorResponse > responseEntity = restTemplate .exchange (getBaseUrl () + runPipelineEndpoint , HttpMethod .POST , requestEntity ,
128+ ErrorResponse .class );
119129 TestUtils .testsForHandelingEmptyFiles (responseEntity );
120130
121- responseEntity = restTemplate .exchange (runPipelineAndWaitEndpoint , HttpMethod .POST , requestEntity , ErrorResponse .class );
131+ responseEntity = restTemplate .exchange (getBaseUrl () + runPipelineAndWaitEndpoint , HttpMethod .POST , requestEntity , ErrorResponse .class );
122132 TestUtils .testsForHandelingEmptyFiles (responseEntity );
123133 }
124134
125135 protected abstract HttpEntity <MultiValueMap <String , Object >> prepareRequestEntityForEmptyFileTest (String projectName );
126136
127137 protected void test_runPipelineAndWaitForResult_helper (HttpEntity <MultiValueMap <String , Object >> requestEntity ) throws IOException {
128138 // test whether runPipeLineAndWait() directly has the result
129- ResponseEntity <String > responseEntity = restTemplate .exchange (runPipelineAndWaitEndpoint , HttpMethod .POST , requestEntity , String .class );
139+ ResponseEntity <String > responseEntity = restTemplate .exchange (getBaseUrl () + runPipelineAndWaitEndpoint , HttpMethod .POST , requestEntity , String .class );
130140 assertNotNull (responseEntity .getBody ());
131141 ArdocoResultResponse response = TestUtils .parseResponseEntityToArdocoResponse (responseEntity );
132142
@@ -155,22 +165,24 @@ protected void test_runPipelineAndWaitForResult_helper(HttpEntity<MultiValueMap<
155165
156166 // Helper methods for common test logic
157167 protected ArdocoResultResponse startNewPipeline_test (HttpEntity <MultiValueMap <String , Object >> requestEntity ) throws IOException {
158- ResponseEntity <String > responseEntity = restTemplate .exchange (runPipelineEndpoint , HttpMethod .POST , requestEntity , String .class );
168+ ResponseEntity <String > responseEntity = restTemplate .exchange (getBaseUrl () + runPipelineEndpoint , HttpMethod .POST , requestEntity , String .class );
159169 assertNotNull (responseEntity .getBody ());
160170 return TestUtils .parseResponseEntityToArdocoResponse (responseEntity );
161171 }
162172
163173 protected void tryGetResultWhenNotReady_test (String projectId ) throws IOException {
164- ResponseEntity <String > responseEntity = restTemplate .getForEntity (GET_RESULT_ENDPOINT , String .class , projectId );
174+ String url = getBaseUrl () + GET_RESULT_ENDPOINT ;
175+ ResponseEntity <String > responseEntity = restTemplate .getForEntity (url , String .class , projectId );
165176 ArdocoResultResponse response = TestUtils .parseResponseEntityToArdocoResponse (responseEntity );
166177 TestUtils .testGetResult_notReady (response , responseEntity , traceLinkType );
167178 resultIsNotInDatabase (response .getRequestId ());
168179 }
169180
170181 protected void waitForResultUntilReady_test (String projectId ) throws IOException {
182+ String url = getBaseUrl () + WAIT_FOR_RESULT_ENDPOINT ;
171183 ResponseEntity <String > waitingEntity ;
172184 do {
173- waitingEntity = restTemplate .getForEntity (WAIT_FOR_RESULT_ENDPOINT , String .class , projectId );
185+ waitingEntity = restTemplate .getForEntity (url , String .class , projectId );
174186 ArdocoResultResponse waitingResponse = TestUtils .parseResponseEntityToArdocoResponse (waitingEntity );
175187
176188 if (waitingEntity .getStatusCode () == HttpStatus .ACCEPTED ) {
@@ -187,22 +199,23 @@ protected void waitForResultUntilReady_test(String projectId) throws IOException
187199 }
188200
189201 protected void getResult_hasResult_test (String projectId ) throws IOException {
190- ResponseEntity <String > responseEntity = restTemplate .getForEntity (GET_RESULT_ENDPOINT , String .class , projectId );
202+ String url = getBaseUrl () + GET_RESULT_ENDPOINT ;
203+ ResponseEntity <String > responseEntity = restTemplate .getForEntity (url , String .class , projectId );
191204 ArdocoResultResponse response = TestUtils .parseResponseEntityToArdocoResponse (responseEntity );
192205 TestUtils .testGetResult_ready (response , responseEntity , traceLinkType );
193206 resultIsInDatabase (response .getRequestId ());
194207 }
195208
196209 protected void runPipeLineDirectlyHasResult_test (HttpEntity <MultiValueMap <String , Object >> requestEntity ) throws IOException {
197- ResponseEntity <String > responseEntity = restTemplate .exchange (runPipelineEndpoint , HttpMethod .POST , requestEntity , String .class );
210+ ResponseEntity <String > responseEntity = restTemplate .exchange (getBaseUrl () + runPipelineEndpoint , HttpMethod .POST , requestEntity , String .class );
198211 assertNotNull (responseEntity .getBody ());
199212 ArdocoResultResponse response = TestUtils .parseResponseEntityToArdocoResponse (responseEntity );
200213 TestUtils .testStartPipeline_resultIsInDatabase (response , responseEntity , traceLinkType );
201214 resultIsInDatabase (response .getRequestId ());
202215 }
203216
204217 protected void runPipeLineAndWaitDirectlyHasResult_test (HttpEntity <MultiValueMap <String , Object >> requestEntity ) throws IOException {
205- ResponseEntity <String > responseEntity = restTemplate .exchange (runPipelineAndWaitEndpoint , HttpMethod .POST , requestEntity , String .class );
218+ ResponseEntity <String > responseEntity = restTemplate .exchange (getBaseUrl () + runPipelineAndWaitEndpoint , HttpMethod .POST , requestEntity , String .class );
206219 assertNotNull (responseEntity .getBody ());
207220 ArdocoResultResponse response = TestUtils .parseResponseEntityToArdocoResponse (responseEntity );
208221 TestUtils .testStartPipeline_resultIsInDatabase (response , responseEntity , traceLinkType );
0 commit comments