diff --git a/template-server/src/integration-test/java/de/twomartens/templateservice/TemplateServiceRestTests.java b/template-server/src/integration-test/java/de/twomartens/templateservice/TemplateServiceRestTests.java new file mode 100644 index 0000000..5914dc0 --- /dev/null +++ b/template-server/src/integration-test/java/de/twomartens/templateservice/TemplateServiceRestTests.java @@ -0,0 +1,62 @@ +package de.twomartens.templateservice; + +import de.twomartens.templateservice.entity.Greeting; +import de.twomartens.templateservice.interceptors.RequestTypeInterceptor; +import de.twomartens.templateservice.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 TemplateServiceRestTests { + + @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 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); + } +} diff --git a/template-server/src/test/java/de/twomartens/templateservice/control/GreetingControllerTests.java b/template-server/src/test/java/de/twomartens/templateservice/control/GreetingControllerTests.java new file mode 100644 index 0000000..322cd7e --- /dev/null +++ b/template-server/src/test/java/de/twomartens/templateservice/control/GreetingControllerTests.java @@ -0,0 +1,37 @@ +package de.twomartens.templateservice.control; + +import de.twomartens.templateservice.service.GreetingService; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; +import org.mockito.BDDMockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +@WebMvcTest(GreetingController.class) +class GreetingControllerTests { + + @Autowired + private MockMvc mvc; + + @MockBean + private GreetingService service; + + @Test + void testGreetingRest() throws Exception { + BDDMockito.given(service.createGreeting("Template")) + .willReturn("Hello Template!"); + + mvc.perform(MockMvcRequestBuilders.get("/greeting") + .param("name", "Template") + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.aMapWithSize(1))) + .andExpect(MockMvcResultMatchers.jsonPath("$.message", Matchers.is("Hello Template!"))) + .andReturn(); + } +} diff --git a/template-server/src/test/java/de/twomartens/templateservice/service/GreetingServiceTest.java b/template-server/src/test/java/de/twomartens/templateservice/service/GreetingServiceTest.java new file mode 100644 index 0000000..0c05e66 --- /dev/null +++ b/template-server/src/test/java/de/twomartens/templateservice/service/GreetingServiceTest.java @@ -0,0 +1,37 @@ +package de.twomartens.templateservice.service; + +import de.twomartens.templateservice.configs.TemplateServiceProperties; +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.mockito.BDDMockito.given; + +@ExtendWith(MockitoExtension.class) +class GreetingServiceTest { + + private GreetingService service; + + @Mock + private TemplateServiceProperties properties; + + @Mock + private TemplateServiceProperties.Template template; + + @BeforeEach + void beforeEach() { + given(template.getGreeting()).willReturn("Hello %s"); + given(properties.getTemplate()).willReturn(template); + service = new GreetingService(new SimpleMeterRegistry(), properties); + } + + @Test + void createGreeting() { + String result = service.createGreeting("Test"); + Assertions.assertThat(result).isEqualTo("Hello Test"); + } +}