-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGoalControllerTest.java
More file actions
409 lines (358 loc) · 15.2 KB
/
GoalControllerTest.java
File metadata and controls
409 lines (358 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package com.deloitte.elrr.services.controller;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import com.deloitte.elrr.entity.Goal;
import com.deloitte.elrr.entity.types.GoalType;
import com.deloitte.elrr.services.TestAppConfig;
import com.deloitte.elrr.services.dto.GoalDto;
import com.deloitte.elrr.services.security.MethodSecurityConfig;
import com.deloitte.elrr.services.security.SecurityConfig;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.extern.slf4j.Slf4j;
@WebMvcTest(GoalController.class)
@ContextConfiguration
@AutoConfigureMockMvc(addFilters = true)
@Import({ TestAppConfig.class, SecurityConfig.class,
MethodSecurityConfig.class })
@Slf4j
public class GoalControllerTest extends CommonControllerTest {
@Autowired
private MockMvc mockMvc;
private static final String GOAL_API = "/api/goal";
/**
* Convert object to JSON string
*
* @param obj the object to convert
* @return String JSON representation
* @throws JsonProcessingException
*/
public static String asJsonString(final Object obj)
throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
return mapper.writeValueAsString(obj);
}
private static final UUID GOAL_ID = UUID.randomUUID();
private static final UUID PERSON_ID = UUID.randomUUID();
/**
* Test getting all goals
*
* @throws Exception
*/
@Test
void getAllGoalsTest() throws Exception {
Mockito.doReturn(getGoalList()).when(getGoalSvc()).findGoalsWithFilters(
any(com.deloitte.elrr.entity.Goal.Filter.class));
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
.get(GOAL_API).accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.headers(getHeaders("goal|READ"));
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
assertEquals(200, mvcResult.getResponse().getStatus());
assertNotNull(mvcResult.getResponse().getContentAsString());
List<GoalDto> results = resultsAsObject(
mvcResult.getResponse().getContentAsString(),
new TypeReference<List<GoalDto>>() {
});
assertEquals(2, results.size());
}
/**
* Test getting goals with a specific ID
*
* @throws Exception
*/
@Test
void getAllGoalsByIdTest() throws Exception {
// Controller now uses filter method, return single list element
Mockito.doReturn(List.of(getGoal())).when(getGoalSvc())
.findGoalsWithFilters(
any(com.deloitte.elrr.entity.Goal.Filter.class));
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
.get(GOAL_API + "?id=" + GOAL_ID)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.headers(getHeaders("goal|READ"));
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
assertEquals(200, mvcResult.getResponse().getStatus());
assertNotNull(mvcResult.getResponse().getContentAsString());
List<GoalDto> results = resultsAsObject(
mvcResult.getResponse().getContentAsString(),
new TypeReference<List<GoalDto>>() {
});
assertEquals(1, results.size());
assertEquals(GOAL_ID, results.get(0).getId());
}
/**
* Test getting goals with a specific ID that does not exist
*
* @throws Exception
*/
@Test
void getAllGoalsByIdNotFoundTest() throws Exception {
Mockito.doReturn(new ArrayList<>()).when(getGoalSvc())
.findGoalsWithFilters(
any(com.deloitte.elrr.entity.Goal.Filter.class));
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
.get(GOAL_API + "?id=" + GOAL_ID)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.headers(getHeaders("goal|READ"));
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
assertEquals(200, mvcResult.getResponse().getStatus());
assertNotNull(mvcResult.getResponse().getContentAsString());
}
/**
* Test getting a goal by ID
*
* @throws Exception
*/
@Test
void getGoalByIdTest() throws Exception {
Mockito.doReturn(Optional.of(getGoal())).when(getGoalSvc())
.get(GOAL_ID);
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
.get(GOAL_API + "/" + GOAL_ID)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.headers(getHeaders("goal|READ"));
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
assertEquals(200, mvcResult.getResponse().getStatus());
assertNotNull(mvcResult.getResponse().getContentAsString());
GoalDto result = resultsAsObject(
mvcResult.getResponse().getContentAsString(),
new TypeReference<GoalDto>() {
});
assertEquals(GOAL_ID, result.getId());
assertEquals("Test Goal", result.getName());
}
/**
* Test creating a goal
*
* @throws Exception
*/
@Test
void createGoalTest() throws Exception {
// Arrange
GoalDto goalDto = getGoalDto();
Goal savedGoal = getGoal();
// Mock the setXXXFromIds methods to return the goal unchanged
Mockito.doReturn(savedGoal).when(getGoalSvc())
.setCompetenciesFromIds(any(Goal.class), any());
Mockito.doReturn(savedGoal).when(getGoalSvc())
.setCredentialsFromIds(any(Goal.class), any());
Mockito.doReturn(savedGoal).when(getGoalSvc())
.setLearningResourcesFromIds(any(Goal.class), any());
Mockito.doReturn(savedGoal).when(getGoalSvc()).save(any(Goal.class));
// Act
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
.post(GOAL_API).content(asJsonString(goalDto))
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.headers(getHeaders("goal|CREATE"));
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
// Assert
assertEquals(201, mvcResult.getResponse().getStatus());
assertNotNull(mvcResult.getResponse().getContentAsString());
GoalDto result = resultsAsObject(
mvcResult.getResponse().getContentAsString(),
new TypeReference<GoalDto>() {
});
assertEquals("Test Goal", result.getName());
assertEquals(GoalType.SELF, result.getType());
// Verify that the setXXXFromIds methods were called
verify(getGoalSvc()).setCompetenciesFromIds(any(Goal.class), any());
verify(getGoalSvc()).setCredentialsFromIds(any(Goal.class), any());
verify(getGoalSvc()).setLearningResourcesFromIds(any(Goal.class),
any());
verify(getGoalSvc()).save(any(Goal.class));
// Verify that related entity ids are present on the saved goal
assertNotNull(result.getCompetencyIds());
assertNotNull(result.getCredentialIds());
assertNotNull(result.getLearningResourceIds());
}
/**
* Test updating a goal
*
* @throws Exception
*/
@Test
void updateGoalTest() throws Exception {
GoalDto goalDto = getGoalDto();
goalDto.setName("Updated Goal");
Goal existingGoal = getGoal();
Goal updatedGoal = getGoal();
updatedGoal.setName("Updated Goal");
// Mock the setXXXFromIds methods to return the goal unchanged
Mockito.doReturn(updatedGoal).when(getGoalSvc())
.setCompetenciesFromIds(any(Goal.class), any());
Mockito.doReturn(updatedGoal).when(getGoalSvc())
.setCredentialsFromIds(any(Goal.class), any());
Mockito.doReturn(updatedGoal).when(getGoalSvc())
.setLearningResourcesFromIds(any(Goal.class), any());
Mockito.doReturn(Optional.of(existingGoal)).when(getGoalSvc())
.get(GOAL_ID);
Mockito.doReturn(updatedGoal).when(getGoalSvc()).save(any(Goal.class));
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
.put(GOAL_API + "/" + GOAL_ID).content(asJsonString(goalDto))
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.headers(getHeaders("goal|UPDATE"));
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
assertEquals(200, mvcResult.getResponse().getStatus());
assertNotNull(mvcResult.getResponse().getContentAsString());
GoalDto result = resultsAsObject(
mvcResult.getResponse().getContentAsString(),
new TypeReference<GoalDto>() {
});
assertEquals("Updated Goal", result.getName());
// Verify that the setXXXFromIds methods were called
verify(getGoalSvc()).setCompetenciesFromIds(any(Goal.class), any());
verify(getGoalSvc()).setCredentialsFromIds(any(Goal.class), any());
verify(getGoalSvc()).setLearningResourcesFromIds(any(Goal.class),
any());
verify(getGoalSvc()).get(GOAL_ID);
verify(getGoalSvc()).save(any(Goal.class));
}
/**
* Test deleting a goal
*
* @throws Exception
*/
@Test
void deleteGoalTest() throws Exception {
Mockito.doReturn(Optional.of(getGoal())).when(getGoalSvc())
.get(GOAL_ID);
Mockito.doNothing().when(getGoalSvc()).delete(GOAL_ID);
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
.delete(GOAL_API + "/" + GOAL_ID)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.headers(getHeaders("goal|DELETE"));
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
assertEquals(204, mvcResult.getResponse().getStatus());
}
/**
* Test getting goal by ID - not found
*
* @throws Exception
*/
@Test
void getGoalByIdNotFoundTest() throws Exception {
Mockito.doReturn(Optional.empty()).when(getGoalSvc()).get(GOAL_ID);
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
.get(GOAL_API + "/" + GOAL_ID)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.headers(getHeaders("goal|READ"));
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
assertEquals(404, mvcResult.getResponse().getStatus());
}
/**
* Test getting all goals when no goals exist - should return 200 with empty
* array
*
* @throws Exception
*/
@Test
void getAllGoalsEmptyListTest() throws Exception {
Mockito.doReturn(new ArrayList<>()).when(getGoalSvc())
.findGoalsWithFilters(
any(com.deloitte.elrr.entity.Goal.Filter.class));
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
.get(GOAL_API).accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.headers(getHeaders("goal|READ"));
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
assertEquals(200, mvcResult.getResponse().getStatus());
assertNotNull(mvcResult.getResponse().getContentAsString());
// Verify empty array is returned
List<GoalDto> results = resultsAsObject(
mvcResult.getResponse().getContentAsString(),
new TypeReference<List<GoalDto>>() {
});
assertEquals(0, results.size());
}
/**
* Create a test Goal entity
*
* @return Goal
*/
private Goal getGoal() {
Goal goal = new Goal();
goal.setId(GOAL_ID);
goal.setName("Test Goal");
goal.setDescription("Test Description");
goal.setType(GoalType.SELF);
goal.setStartDate(ZonedDateTime.now());
goal.setAchievedByDate(ZonedDateTime.now().plusMonths(6));
goal.setCompetencies(new HashSet<>());
goal.setCredentials(new HashSet<>());
goal.setLearningResources(new HashSet<>());
return goal;
}
/**
* Create a test GoalDto
*
* @return GoalDto
*/
private GoalDto getGoalDto() {
GoalDto goalDto = new GoalDto();
goalDto.setPersonId(PERSON_ID);
goalDto.setGoalId("test-goal-" + UUID.randomUUID());
goalDto.setName("Test Goal");
goalDto.setDescription("Test Description");
goalDto.setType(GoalType.SELF);
goalDto.setStartDate(ZonedDateTime.now());
goalDto.setAchievedByDate(ZonedDateTime.now().plusMonths(6));
goalDto.setCompetencyIds(new HashSet<>(List.of(UUID.randomUUID())));
goalDto.setCredentialIds(new HashSet<>(List.of(UUID.randomUUID())));
goalDto.setLearningResourceIds(
new HashSet<>(List.of(UUID.randomUUID())));
return goalDto;
}
/**
* Create a list of test Goal entities
*
* @return List<Goal>
*/
private List<Goal> getGoalList() {
List<Goal> goals = new ArrayList<>();
Goal goal1 = getGoal();
goals.add(goal1);
Goal goal2 = new Goal();
goal2.setId(UUID.randomUUID());
goal2.setName("Second Goal");
goal2.setDescription("Second Description");
goal2.setType(GoalType.ASSIGNED);
goal2.setStartDate(ZonedDateTime.now());
goal2.setAchievedByDate(ZonedDateTime.now().plusMonths(3));
goal2.setCompetencies(new HashSet<>());
goal2.setCredentials(new HashSet<>());
goal2.setLearningResources(new HashSet<>());
goals.add(goal2);
return goals;
}
}