generated from 2martens/template-service
63 lines
2.0 KiB
Java
63 lines
2.0 KiB
Java
package de.twomartens.oparlservice;
|
|
|
|
import de.twomartens.oparlservice.entity.Greeting;
|
|
import de.twomartens.oparlservice.interceptors.RequestTypeInterceptor;
|
|
import de.twomartens.oparlservice.interceptors.TraceIdInterceptor;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
import org.springframework.boot.web.server.LocalServerPort;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
|
|
|
@SpringJUnitConfig
|
|
@SpringBootTest(webEnvironment = RANDOM_PORT)
|
|
class OParlServiceRestTests {
|
|
|
|
@Autowired
|
|
private RestTemplate restTemplate;
|
|
|
|
@LocalServerPort
|
|
int randomServerPort;
|
|
|
|
@Autowired
|
|
private RequestTypeInterceptor requestTypeInterceptor;
|
|
|
|
@Autowired
|
|
private TraceIdInterceptor traceIdInterceptor;
|
|
|
|
@BeforeEach
|
|
void setup() {
|
|
traceIdInterceptor.createNewTraceId();
|
|
requestTypeInterceptor.markAsIntegrationTest();
|
|
}
|
|
|
|
@Test
|
|
void testRestService() {
|
|
ResponseEntity<Greeting> result = restTemplate.getForEntity(getEndpoint(), Greeting.class);
|
|
assertThat(result.getStatusCode().is2xxSuccessful()).isTrue();
|
|
assertThat(result.getBody()).isNotNull();
|
|
assertThat(result.getBody().getMessage()).isEqualTo("Hello RestCheck!");
|
|
}
|
|
|
|
/*
|
|
* This method is so complicated because it uses the local-grpc-server configuration for grpc to create
|
|
* a correct link for the rest endpoints in the cloud
|
|
*/
|
|
private String getEndpoint() {
|
|
String hostNameForGrpcServer = "127.0.0.1";
|
|
int port = randomServerPort;
|
|
String protocol = "http";
|
|
|
|
return String.format("%s://%s:%d/greeting?name=RestCheck",
|
|
protocol,
|
|
hostNameForGrpcServer,
|
|
port);
|
|
}
|
|
}
|