diff --git a/spring-web-modules/spring-mvc-test/build.gradle b/spring-web-modules/spring-mvc-test/build.gradle new file mode 100644 index 000000000000..10f9207555f9 --- /dev/null +++ b/spring-web-modules/spring-mvc-test/build.gradle @@ -0,0 +1,28 @@ +plugins { + id 'java' + id 'org.springframework.boot' version '3.3.2' + id 'io.spring.dependency-management' version '1.1.6' +} + +group = 'com.testing' +version = '0.0.1-SNAPSHOT' + +java { + toolchain { + languageVersion = JavaLanguageVersion.of(17) + } +} + +repositories { + mavenCentral() +} + +dependencies { + implementation 'org.springframework.boot:spring-boot-starter-web' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' +} + +tasks.named('test') { + useJUnitPlatform() +} diff --git a/spring-web-modules/spring-mvc-test/settings.gradle b/spring-web-modules/spring-mvc-test/settings.gradle new file mode 100644 index 000000000000..028442d0f5cc --- /dev/null +++ b/spring-web-modules/spring-mvc-test/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'mvc' diff --git a/spring-web-modules/spring-mvc-test/src/main/java/com/testing/mvc/MvcApplication.java b/spring-web-modules/spring-mvc-test/src/main/java/com/testing/mvc/MvcApplication.java new file mode 100644 index 000000000000..5d97e81de80f --- /dev/null +++ b/spring-web-modules/spring-mvc-test/src/main/java/com/testing/mvc/MvcApplication.java @@ -0,0 +1,13 @@ +package com.testing.mvc; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class MvcApplication { + + public static void main(String[] args) { + SpringApplication.run(MvcApplication.class, args); + } + +} diff --git a/spring-web-modules/spring-mvc-test/src/main/java/com/testing/mvc/SortingController.java b/spring-web-modules/spring-mvc-test/src/main/java/com/testing/mvc/SortingController.java new file mode 100644 index 000000000000..b27de27ebf75 --- /dev/null +++ b/spring-web-modules/spring-mvc-test/src/main/java/com/testing/mvc/SortingController.java @@ -0,0 +1,28 @@ +package com.testing.mvc; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import java.util.List; + +@RestController +public class SortingController { + + private final SortingService sortingService; + + public SortingController(SortingService sortingService){ + this.sortingService=sortingService; + } + + @GetMapping + public ResponseEntity helloWorld(){ + return ResponseEntity.ok("Hello, World!"); + } + + @PostMapping + public ResponseEntity> sort(@RequestBody List arr){ + return ResponseEntity.ok(sortingService.sortArray(arr)); + } +} diff --git a/spring-web-modules/spring-mvc-test/src/main/java/com/testing/mvc/SortingService.java b/spring-web-modules/spring-mvc-test/src/main/java/com/testing/mvc/SortingService.java new file mode 100644 index 000000000000..1629c2e155d8 --- /dev/null +++ b/spring-web-modules/spring-mvc-test/src/main/java/com/testing/mvc/SortingService.java @@ -0,0 +1,13 @@ +package com.testing.mvc; + +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class SortingService { + + public List sortArray(List arr){ + return arr.stream().sorted().toList(); + } +} diff --git a/spring-web-modules/spring-mvc-test/src/main/resources/application.properties b/spring-web-modules/spring-mvc-test/src/main/resources/application.properties new file mode 100644 index 000000000000..6ab31e8aadb1 --- /dev/null +++ b/spring-web-modules/spring-mvc-test/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.application.name=mvc diff --git a/spring-web-modules/spring-mvc-test/src/test/java/com/testing/mvc/springboottest/SortingControllerIntegrationTest.java b/spring-web-modules/spring-mvc-test/src/test/java/com/testing/mvc/springboottest/SortingControllerIntegrationTest.java new file mode 100644 index 000000000000..4a33d74ae722 --- /dev/null +++ b/spring-web-modules/spring-mvc-test/src/test/java/com/testing/mvc/springboottest/SortingControllerIntegrationTest.java @@ -0,0 +1,47 @@ +package com.testing.mvc.springboottest; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import java.util.Arrays; +import java.util.List; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@AutoConfigureMockMvc +@SpringBootTest +class SortingControllerIntegrationTest { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private ObjectMapper objectMapper; + + @Test + void whenHelloWorldMethodIsCalled_thenReturnSuccessString() throws Exception { + mockMvc.perform(get("/")) + .andExpect(status().isOk()) + .andExpect(content().string("Hello, World!")); + } + + @Test + void whenSortMethodIsCalled_thenReturnSortedArray() throws Exception { + List input = Arrays.asList(5, 3, 8, 1, 9, 2); + List sorted = Arrays.asList(1, 2, 3, 5, 8, 9); + + mockMvc.perform(post("/").contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(input))) + .andExpect(status().isOk()) + .andExpect(content().json(objectMapper.writeValueAsString(sorted))); + } + +} \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-test/src/test/java/com/testing/mvc/springboottest/SortingControllerWithWebEnvironmentIntegrationTest.java b/spring-web-modules/spring-mvc-test/src/test/java/com/testing/mvc/springboottest/SortingControllerWithWebEnvironmentIntegrationTest.java new file mode 100644 index 000000000000..12a8049a68aa --- /dev/null +++ b/spring-web-modules/spring-mvc-test/src/test/java/com/testing/mvc/springboottest/SortingControllerWithWebEnvironmentIntegrationTest.java @@ -0,0 +1,52 @@ +package com.testing.mvc.springboottest; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.http.MediaType; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; + +import java.util.Arrays; +import java.util.List; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +class SortingControllerWithWebEnvironmentIntegrationTest { + + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Autowired + private ObjectMapper objectMapper; + + @Test + void whenHelloWorldMethodIsCalled_thenReturnSuccessString() { + ResponseEntity response = restTemplate.getForEntity("http://localhost:" + port + "/", String.class); + Assertions.assertEquals(HttpStatus.OK, response.getStatusCode()); + Assertions.assertEquals("Hello, World!", response.getBody()); + } + + @Test + void whenSortMethodIsCalled_thenReturnSortedArray() throws Exception { + List input = Arrays.asList(5, 3, 8, 1, 9, 2); + List sorted = Arrays.asList(1, 2, 3, 5, 8, 9); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + + ResponseEntity response = restTemplate.postForEntity("http://localhost:" + port + "/", + new HttpEntity<>(objectMapper.writeValueAsString(input), headers), List.class); + + Assertions.assertEquals(HttpStatus.OK, response.getStatusCode()); + Assertions.assertEquals(sorted, response.getBody()); + } +} diff --git a/spring-web-modules/spring-mvc-test/src/test/java/com/testing/mvc/webmvctest/SortingControllerUnitTest.java b/spring-web-modules/spring-mvc-test/src/test/java/com/testing/mvc/webmvctest/SortingControllerUnitTest.java new file mode 100644 index 000000000000..a5ab3450fe42 --- /dev/null +++ b/spring-web-modules/spring-mvc-test/src/test/java/com/testing/mvc/webmvctest/SortingControllerUnitTest.java @@ -0,0 +1,53 @@ +package com.testing.mvc.webmvctest; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.testing.mvc.SortingController; +import com.testing.mvc.SortingService; +import org.junit.jupiter.api.Test; +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 java.util.Arrays; +import java.util.List; + +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@WebMvcTest(SortingController.class) +class SortingControllerUnitTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private SortingService sortingService; + + @Autowired + private ObjectMapper objectMapper; + + @Test + void whenHelloWorldMethodIsCalled_thenReturnSuccessString() throws Exception { + mockMvc.perform(get("/")) + .andExpect(status().isOk()); + } + + @Test + void whenSortMethodIsCalled_thenReturnSortedArray() throws Exception { + List input = Arrays.asList(5, 3, 8, 1, 9, 2); + List sorted = Arrays.asList(1, 2, 3, 5, 8, 9); + + when(sortingService.sortArray(input)).thenReturn(sorted); + + mockMvc.perform(post("/").contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(input))) + .andExpect(status().isOk()) + .andExpect(content().json(objectMapper.writeValueAsString(sorted))); + } + +} \ No newline at end of file