Skip to content

Commit 3666c35

Browse files
committed
integration test for GovernmentController is created
1 parent 919a807 commit 3666c35

File tree

4 files changed

+163
-1
lines changed

4 files changed

+163
-1
lines changed

government-service/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@
103103
<version>1.3.2</version>
104104
<scope>provided</scope>
105105
</dependency>
106+
<dependency>
107+
<groupId>org.glassfish.jersey.core</groupId>
108+
<artifactId>jersey-server</artifactId>
109+
<scope>test</scope>
110+
</dependency>
111+
<dependency>
112+
<groupId>org.springframework</groupId>
113+
<artifactId>spring-test</artifactId>
114+
<version>6.0.7</version>
115+
<scope>test</scope>
116+
</dependency>
106117
</dependencies>
107118

108119
<build>

government-service/src/main/java/com/csaba79coder/bestprotocol/controller/GovernmentController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class GovernmentController implements GovernmentApi {
3232
@return A ResponseEntity containing a list of GovernmentTranslationModel objects.
3333
HTTP status code of 200 (OK) if the request was successful.
3434
*/
35+
// tested
3536
@Override
3637
public ResponseEntity<List<GovernmentTranslationModel>> renderAllGovernments(String languageShortName) {
3738
return ResponseEntity.status(200).body(governmentService.findAllGovernmentsByLangAndGovernmentId(languageShortName));
@@ -44,6 +45,7 @@ public ResponseEntity<List<GovernmentTranslationModel>> renderAllGovernments(Str
4445
@param governmentId The ID of the government to retrieve the translations for.
4546
@return A ResponseEntity containing a list of GovernmentTranslationModel objects and an HTTP status code of 200 (OK) if the request was successful.
4647
*/
48+
// tested
4749
@Override
4850
public ResponseEntity<List<GovernmentTranslationModel>> renderAllGovernmentsById(String languageShortName, Long governmentId) {
4951
return ResponseEntity.status(200).body(governmentService.findAllGovernmentsByLangAndGovernmentId(languageShortName, governmentId));

government-service/src/main/java/com/csaba79coder/bestprotocol/model/government/service/GovernmentService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public List<GovernmentTranslationModel> findAllGovernmentsByLangAndGovernmentId(
5252
.collect(Collectors.toList());
5353
}
5454

55-
// TODO not in usage at the moment, need it for later!
55+
// not in usage at the moment, need it for later!
5656
/**
5757
5858
Returns the {@link GovernmentTranslation} object with the given ID, or throws a {@link NoSuchElementException} if no
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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

Comments
 (0)