|
| 1 | +package com.csaba79coder.bestprotocol; |
| 2 | + |
| 3 | +import com.csaba79coder.bestprotocol.controller.GovernmentController; |
| 4 | +import com.csaba79coder.bestprotocol.model.GovernmentTranslationModel; |
| 5 | +import com.csaba79coder.bestprotocol.model.government.service.GovernmentService; |
| 6 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.DisplayName; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 12 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 15 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 16 | +import org.springframework.http.MediaType; |
| 17 | +import org.springframework.test.context.ContextConfiguration; |
| 18 | +import org.springframework.test.web.servlet.MockMvc; |
| 19 | +import org.springframework.test.web.servlet.MvcResult; |
| 20 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 21 | +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import static org.assertj.core.api.BDDAssertions.then; |
| 27 | +import static org.mockito.ArgumentMatchers.anyString; |
| 28 | +import static org.mockito.Mockito.when; |
| 29 | +import static org.mockito.MockitoAnnotations.openMocks; |
| 30 | + |
| 31 | +/** |
| 32 | + * Integration test class for the {@link GovernmentController}. |
| 33 | + * This class tests the behavior of the GovernmentController by sending mock HTTP requests |
| 34 | + * and asserting the responses returned by the controller methods. |
| 35 | + */ |
| 36 | +@WebMvcTest(GovernmentController.class) |
| 37 | +@ContextConfiguration(classes = {GovernmentController.class}) |
| 38 | +@ExtendWith(MockitoExtension.class) |
| 39 | +@SuppressWarnings("resource") |
| 40 | +public class GovernmentControllerIT { |
| 41 | + |
| 42 | + @Autowired |
| 43 | + private MockMvc mockMvc; |
| 44 | + |
| 45 | + @Autowired |
| 46 | + private ObjectMapper objectMapper; |
| 47 | + |
| 48 | + @MockBean |
| 49 | + private GovernmentService governmentService; |
| 50 | + |
| 51 | + private List<GovernmentTranslationModel> governmentTranslationModels; |
| 52 | + |
| 53 | + /** |
| 54 | +
|
| 55 | + This method sets up the test environment before each test case. It initializes the mock objects using the openMocks() method from Mockito. |
| 56 | + It also creates an empty ArrayList of GovernmentTranslationModel and adds one model with languageShortName as "en" and name as "Ministry of Finance". |
| 57 | + */ |
| 58 | + @BeforeEach |
| 59 | + void setUp() { |
| 60 | + openMocks(this); |
| 61 | + governmentTranslationModels = new ArrayList<>(); |
| 62 | + governmentTranslationModels.add(new GovernmentTranslationModel() |
| 63 | + .languageShortName("en") |
| 64 | + .name("Ministry of Finance")); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Tests the {@link GovernmentController#renderAllGovernments(String)} method by sending a GET request |
| 69 | + * to the "/en/api/admin/governments" endpoint with the "en" language parameter and verifying that |
| 70 | + * the response contains a list of government models with the expected properties. |
| 71 | + * |
| 72 | + * <p>This test uses the {@link MockMvc} API to send the HTTP request and to perform assertions on the |
| 73 | + * response. It also uses the {@link ObjectMapper} to serialize and deserialize JSON objects.</p> |
| 74 | + * |
| 75 | + * @throws Exception if an error occurs while sending the HTTP request or processing the response |
| 76 | + */ |
| 77 | + @Test |
| 78 | + @DisplayName("should return all governments") |
| 79 | + void shouldReturnAllGovernments() throws Exception { |
| 80 | + // given |
| 81 | + when(governmentService.findAllGovernmentsByLangAndGovernmentId(anyString())).thenReturn(governmentTranslationModels); |
| 82 | + |
| 83 | + // when |
| 84 | + mockMvc.perform(MockMvcRequestBuilders.get("/en/api/admin/governments") |
| 85 | + .contentType(MediaType.APPLICATION_JSON) |
| 86 | + .accept(MediaType.APPLICATION_JSON)) |
| 87 | + .andExpect(MockMvcResultMatchers.status().isOk()) |
| 88 | + .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON)) |
| 89 | + .andExpect(MockMvcResultMatchers.jsonPath("$").isNotEmpty()) |
| 90 | + .andExpect(MockMvcResultMatchers.jsonPath("$[0].name").value("Ministry of Finance")); |
| 91 | + |
| 92 | + // then |
| 93 | + String responseBody = mockMvc.perform(MockMvcRequestBuilders.get("/en/api/admin/governments") |
| 94 | + .contentType(MediaType.APPLICATION_JSON) |
| 95 | + .accept(MediaType.APPLICATION_JSON)) |
| 96 | + .andReturn() |
| 97 | + .getResponse() |
| 98 | + .getContentAsString(); |
| 99 | + |
| 100 | + List<GovernmentTranslationModel> result = objectMapper.readValue(responseBody, new TypeReference<>(){}); |
| 101 | + then(result.size()).isEqualTo(1); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | +
|
| 106 | + Tests the {@link GovernmentController#renderAllGovernmentsById(String, Long)} method by sending a GET request |
| 107 | + to the "/en/api/admin/governments/{id}" endpoint with the "en" language parameter and a government ID, |
| 108 | + and verifying that the response contains a list of government models with the expected properties. |
| 109 | + <p>This test uses the {@link MockMvc} API to send the HTTP request and to perform assertions on the |
| 110 | + response. It also uses the {@link ObjectMapper} to serialize and deserialize JSON objects.</p> |
| 111 | + @throws Exception if an error occurs while sending the HTTP request or processing the response |
| 112 | + */ |
| 113 | + @Test |
| 114 | + @DisplayName("should return all governments by id") |
| 115 | + void shouldReturnAllGovernmentsById() throws Exception { |
| 116 | + // given |
| 117 | + String languageShortName = "en"; |
| 118 | + Long governmentId = 1L; |
| 119 | + List<GovernmentTranslationModel> governmentTranslationModels = new ArrayList<>(); |
| 120 | + governmentTranslationModels.add(new GovernmentTranslationModel() |
| 121 | + .languageShortName(languageShortName) |
| 122 | + .name("Ministry of Finance")); |
| 123 | + when(governmentService.findAllGovernmentsByLangAndGovernmentId(languageShortName, governmentId)) |
| 124 | + .thenReturn(governmentTranslationModels); |
| 125 | + |
| 126 | + // when |
| 127 | + mockMvc.perform(MockMvcRequestBuilders.get("/en/api/admin/governments/1") |
| 128 | + .contentType(MediaType.APPLICATION_JSON) |
| 129 | + .accept(MediaType.APPLICATION_JSON)) |
| 130 | + .andExpect(MockMvcResultMatchers.status().isOk()) |
| 131 | + .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON)) |
| 132 | + .andExpect(MockMvcResultMatchers.jsonPath("$").isNotEmpty()) |
| 133 | + .andExpect(MockMvcResultMatchers.jsonPath("$[0].name").value("Ministry of Finance")); |
| 134 | + |
| 135 | + // then |
| 136 | + MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/en/api/admin/governments/1") |
| 137 | + .contentType(MediaType.APPLICATION_JSON) |
| 138 | + .accept(MediaType.APPLICATION_JSON)) |
| 139 | + .andReturn(); |
| 140 | + String responseBody = result.getResponse().getContentAsString(); |
| 141 | + |
| 142 | + List<GovernmentTranslationModel> actualModels = objectMapper.readValue(responseBody, new TypeReference<>(){}); |
| 143 | + List<GovernmentTranslationModel> expectedModels = governmentTranslationModels; |
| 144 | + |
| 145 | + then(actualModels) |
| 146 | + .usingRecursiveComparison() |
| 147 | + .isEqualTo(expectedModels); |
| 148 | + } |
| 149 | +} |
0 commit comments