Skip to content

Commit e1d0369

Browse files
konstantinshaplykoimashkanov
authored andcommitted
Integration Tests for existing Controllers
1 parent bcc7373 commit e1d0369

File tree

4 files changed

+421
-7
lines changed

4 files changed

+421
-7
lines changed

src/main/java/com/epam/ta/reportportal/core/tms/controller/TmsTestPlanController.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.epam.ta.reportportal.core.tms.controller;
22

3-
import static com.epam.ta.reportportal.auth.permissions.Permissions.ASSIGNED_TO_PROJECT;
3+
import static com.epam.ta.reportportal.auth.permissions.Permissions.ADMIN_ONLY;
44

55
import com.epam.ta.reportportal.core.tms.dto.TmsTestPlanRQ;
66
import com.epam.ta.reportportal.core.tms.dto.TmsTestPlanRS;
@@ -31,14 +31,14 @@ public class TmsTestPlanController {
3131
private final TmsTestPlanService tmsTestPlanService;
3232

3333
@PostMapping
34-
@PreAuthorize(ASSIGNED_TO_PROJECT)
34+
@PreAuthorize(ADMIN_ONLY)
3535
public TmsTestPlanRS createTestPlan(@PathVariable Long projectId,
3636
@RequestBody TmsTestPlanRQ testPlan) {
3737
return tmsTestPlanService.create(projectId, testPlan);
3838
}
3939

4040
@GetMapping
41-
@PreAuthorize(ASSIGNED_TO_PROJECT)
41+
@PreAuthorize(ADMIN_ONLY)
4242
public Page<TmsTestPlanRS> getTestPlansByCriteria(
4343
@PathVariable Long projectId,
4444
@RequestParam(required = false) List<Long> environmentId,
@@ -50,28 +50,28 @@ public Page<TmsTestPlanRS> getTestPlansByCriteria(
5050
}
5151

5252
@PutMapping("/{id}")
53-
@PreAuthorize(ASSIGNED_TO_PROJECT)
53+
@PreAuthorize(ADMIN_ONLY)
5454
public TmsTestPlanRS updateTestPlan(@PathVariable Long projectId,
5555
@PathVariable("id") Long testPlanId,
5656
@RequestBody TmsTestPlanRQ testPlan) {
5757
return tmsTestPlanService.update(projectId, testPlanId, testPlan);
5858
}
5959

6060
@GetMapping("/{id}")
61-
@PreAuthorize(ASSIGNED_TO_PROJECT)
61+
@PreAuthorize(ADMIN_ONLY)
6262
public TmsTestPlanRS getTestPlanById(@PathVariable Long projectId,
6363
@PathVariable("id") Long testPlanId) {
6464
return tmsTestPlanService.getById(projectId, testPlanId);
6565
}
6666

6767
@DeleteMapping("/{id}")
68-
@PreAuthorize(ASSIGNED_TO_PROJECT)
68+
@PreAuthorize(ADMIN_ONLY)
6969
public void deleteTestPlan(@PathVariable Long projectId, @PathVariable("id") Long testPlanId) {
7070
tmsTestPlanService.delete(projectId, testPlanId);
7171
}
7272

7373
@PatchMapping("/{id}")
74-
@PreAuthorize(ASSIGNED_TO_PROJECT)
74+
@PreAuthorize(ADMIN_ONLY)
7575
public TmsTestPlanRS patchTestPlan(@PathVariable Long projectId,
7676
@PathVariable("id") Long testPlanId,
7777
@RequestBody TmsTestPlanRQ updatedTestPlan) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
package com.epam.ta.reportportal.core.tms.controller.integration;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
7+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
8+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
9+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
10+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
11+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
12+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
13+
14+
import com.epam.ta.reportportal.core.tms.db.entity.TmsMilestone;
15+
import com.epam.ta.reportportal.core.tms.db.entity.TmsTestPlan;
16+
import com.epam.ta.reportportal.core.tms.db.repository.TmsTestPlanRepository;
17+
import com.epam.ta.reportportal.core.tms.dto.TmsTestPlanAttributeRQ;
18+
import com.epam.ta.reportportal.core.tms.dto.TmsTestPlanRQ;
19+
import com.epam.ta.reportportal.ws.BaseMvcTest;
20+
import com.fasterxml.jackson.databind.ObjectMapper;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.ExtendWith;
23+
import org.mockito.junit.jupiter.MockitoExtension;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.http.MediaType;
26+
import java.util.List;
27+
import java.util.Optional;
28+
import org.springframework.test.context.jdbc.Sql;
29+
30+
/**
31+
* @author <a href="mailto:[email protected]">Konstantin Shaplyko</a>
32+
*/
33+
@Sql("/db/tms/tms-product-version/tms-test-plan-fill.sql")
34+
@ExtendWith(MockitoExtension.class)
35+
public class TmsTestPlanIntegrationTest extends BaseMvcTest {
36+
37+
@Autowired
38+
private TmsTestPlanRepository testPlanRepository;
39+
40+
@Test
41+
void createTestPlanIntegrationTest() throws Exception {
42+
TmsTestPlanAttributeRQ attribute = new TmsTestPlanAttributeRQ();
43+
attribute.setValue("value3");
44+
attribute.setAttributeId(3L);
45+
46+
TmsTestPlanRQ tmsTestPlan = new TmsTestPlanRQ();
47+
tmsTestPlan.setName("name3");
48+
tmsTestPlan.setDescription("description3");
49+
tmsTestPlan.setEnvironmentId(3L);
50+
tmsTestPlan.setProductVersionId(3L);
51+
tmsTestPlan.setMilestoneIds(List.of(3L));
52+
tmsTestPlan.setAttributes(List.of(attribute));
53+
54+
ObjectMapper mapper = new ObjectMapper();
55+
String jsonContent = mapper.writeValueAsString(tmsTestPlan);
56+
57+
mockMvc.perform(post("/project/3/tms/test-plan")
58+
.contentType("application/json")
59+
.content(jsonContent)
60+
.with(token(oAuthHelper.getSuperadminToken())))
61+
.andExpect(status().isOk());
62+
63+
Optional<TmsTestPlan> testPlan = testPlanRepository.findById(1L);
64+
65+
assertTrue(testPlan.isPresent());
66+
assertEquals(tmsTestPlan.getName(), testPlan.get().getName());
67+
assertEquals(tmsTestPlan.getDescription(), testPlan.get().getDescription());
68+
assertEquals(tmsTestPlan.getEnvironmentId(), testPlan.get().getEnvironment().getId());
69+
TmsMilestone[] versionArray = testPlan.get().getMilestones()
70+
.toArray(new TmsMilestone[0]);
71+
assertEquals(tmsTestPlan.getMilestoneIds().get(0), versionArray[0].getId());
72+
assertEquals(tmsTestPlan.getProductVersionId(), testPlan.get().getProductVersion().getId());
73+
}
74+
75+
@Test
76+
void getTestPlansByCriteriaIntegrationTest() throws Exception {
77+
Optional<TmsTestPlan> testPlan = testPlanRepository.findById(4L);
78+
79+
mockMvc.perform(get("/project/4/tms/test-plan")
80+
.param("environmentId", "4")
81+
.param("productVersionId", "4")
82+
.param("page", "0")
83+
.param("size", "1")
84+
.with(token(oAuthHelper.getSuperadminToken())))
85+
.andExpect(status().isOk())
86+
.andExpect(jsonPath("$.content[0].id").value(testPlan.get().getId()))
87+
.andExpect(jsonPath("$.content[0].name").value(testPlan.get().getName()))
88+
.andExpect(jsonPath("$.content[0].description").value(testPlan.get().getDescription()))
89+
.andExpect(jsonPath("$.content[0].environment.id").value(testPlan.get()
90+
.getEnvironment().getId()))
91+
.andExpect(jsonPath("$.content[0].productVersion.id").value(testPlan.get()
92+
.getProductVersion().getId()));
93+
}
94+
95+
@Test
96+
void updateTestPlanIntegrationTest() throws Exception {
97+
TmsTestPlanAttributeRQ attribute = new TmsTestPlanAttributeRQ();
98+
attribute.setValue("value5");
99+
attribute.setAttributeId(5L);
100+
101+
TmsTestPlanRQ tmsTestPlan = new TmsTestPlanRQ();
102+
tmsTestPlan.setName("updated_name5");
103+
tmsTestPlan.setDescription("updated_name5");
104+
tmsTestPlan.setEnvironmentId(4L);
105+
tmsTestPlan.setProductVersionId(4L);
106+
tmsTestPlan.setMilestoneIds(List.of(4L));
107+
tmsTestPlan.setAttributes(List.of(attribute));
108+
109+
ObjectMapper mapper = new ObjectMapper();
110+
String jsonContent = mapper.writeValueAsString(tmsTestPlan);
111+
112+
mockMvc.perform(put("/project/5/tms/test-plan/5")
113+
.contentType("application/json")
114+
.content(jsonContent)
115+
.with(token(oAuthHelper.getSuperadminToken())))
116+
.andExpect(status().isOk());
117+
118+
Optional<TmsTestPlan> testPlan = testPlanRepository.findById(5L);
119+
120+
assertTrue(testPlan.isPresent());
121+
assertEquals(tmsTestPlan.getName(), testPlan.get().getName());
122+
assertEquals(tmsTestPlan.getDescription(), testPlan.get().getDescription());
123+
assertEquals(tmsTestPlan.getEnvironmentId(), testPlan.get().getEnvironment().getId());
124+
TmsMilestone[] versionArray = testPlan.get().getMilestones()
125+
.toArray(new TmsMilestone[0]);
126+
assertEquals(tmsTestPlan.getMilestoneIds().get(0), versionArray[0].getId());
127+
assertEquals(tmsTestPlan.getProductVersionId(), testPlan.get().getProductVersion().getId());
128+
}
129+
130+
@Test
131+
void getTestPlanByIdIntegrationTest() throws Exception {
132+
Optional<TmsTestPlan> testPlan = testPlanRepository.findById(5L);
133+
134+
mockMvc.perform(get("/project/5/tms/test-plan/5")
135+
.with(token(oAuthHelper.getSuperadminToken())))
136+
.andExpect(status().isOk())
137+
.andExpect(jsonPath("$.id").value(testPlan.get().getId()))
138+
.andExpect(jsonPath("$.name").value(testPlan.get().getName()))
139+
.andExpect(jsonPath("$.description").value(testPlan.get().getDescription()))
140+
.andExpect(jsonPath("$.environment.id").value(testPlan.get().getEnvironment().getId()))
141+
.andExpect(jsonPath("$.productVersion.id").value(testPlan.get()
142+
.getProductVersion().getId()));
143+
}
144+
145+
@Test
146+
void deleteTestPlanIntegrationTest() throws Exception {
147+
148+
mockMvc.perform(delete("/project/6/tms/test-plan/6")
149+
.contentType(MediaType.APPLICATION_JSON)
150+
.with(token(oAuthHelper.getSuperadminToken())))
151+
.andExpect(status().isOk());
152+
153+
assertFalse(testPlanRepository.findById(6L).isPresent());
154+
}
155+
156+
@Test
157+
void patchTestPlanTest() throws Exception {
158+
TmsTestPlanAttributeRQ attributQ = new TmsTestPlanAttributeRQ();
159+
attributQ.setValue("value5");
160+
attributQ.setAttributeId(5L);
161+
162+
TmsTestPlanRQ tmsTestPlan = new TmsTestPlanRQ();
163+
tmsTestPlan.setName("updated_name5");
164+
tmsTestPlan.setDescription("updated_name5");
165+
tmsTestPlan.setEnvironmentId(4L);
166+
tmsTestPlan.setProductVersionId(4L);
167+
tmsTestPlan.setMilestoneIds(List.of(4L));
168+
tmsTestPlan.setAttributes(List.of(attributQ));
169+
170+
ObjectMapper mapper = new ObjectMapper();
171+
String jsonContent = mapper.writeValueAsString(tmsTestPlan);
172+
173+
mockMvc.perform(patch("/project/5/tms/test-plan/5")
174+
.contentType("application/json")
175+
.content(jsonContent)
176+
.with(token(oAuthHelper.getSuperadminToken())))
177+
.andExpect(status().isOk());
178+
179+
Optional<TmsTestPlan> testPlan = testPlanRepository.findById(5L);
180+
181+
assertTrue(testPlan.isPresent());
182+
assertEquals(tmsTestPlan.getName(), testPlan.get().getName());
183+
assertEquals(tmsTestPlan.getDescription(), testPlan.get().getDescription());
184+
assertEquals(tmsTestPlan.getEnvironmentId(), testPlan.get().getEnvironment().getId());
185+
TmsMilestone[] versionArray = testPlan.get().getMilestones()
186+
.toArray(new TmsMilestone[0]);
187+
assertEquals(tmsTestPlan.getMilestoneIds().get(0), versionArray[0].getId());
188+
assertEquals(tmsTestPlan.getProductVersionId(), testPlan.get().getProductVersion().getId());
189+
}
190+
}

0 commit comments

Comments
 (0)