Skip to content

Commit db16434

Browse files
authored
DashboardControllerTest + littleLastUpdate (#218)
1 parent 1ba3e85 commit db16434

File tree

3 files changed

+347
-32
lines changed

3 files changed

+347
-32
lines changed

src/main/java/com/ita/if103java/ims/dto/WarehousePremiumStructDto.java

+11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ public class WarehousePremiumStructDto {
99
private Long capacity = 0L;
1010
private List<WarehousePremiumStructDto> childs;
1111

12+
public WarehousePremiumStructDto() {
13+
}
14+
15+
public WarehousePremiumStructDto(Long id, String name, Long charge, Long capacity, List<WarehousePremiumStructDto> childs) {
16+
this.id = id;
17+
this.name = name;
18+
this.charge = charge;
19+
this.capacity = capacity;
20+
this.childs = childs;
21+
}
22+
1223
public Long getId() {
1324
return id;
1425
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
package com.ita.if103java.ims.controller;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
5+
import com.ita.if103java.ims.dto.EndingItemsDto;
6+
import com.ita.if103java.ims.dto.PopularItemsDto;
7+
import com.ita.if103java.ims.dto.PopularItemsRequestDto;
8+
import com.ita.if103java.ims.dto.WarehouseLoadDto;
9+
import com.ita.if103java.ims.dto.WarehousePremiumStructDto;
10+
import com.ita.if103java.ims.entity.AccountType;
11+
import com.ita.if103java.ims.entity.DateType;
12+
import com.ita.if103java.ims.entity.PopType;
13+
import com.ita.if103java.ims.entity.Role;
14+
import com.ita.if103java.ims.entity.User;
15+
import com.ita.if103java.ims.exception.dao.DashboardDataNotFoundException;
16+
import com.ita.if103java.ims.handler.GlobalExceptionHandler;
17+
import com.ita.if103java.ims.security.SecurityInterceptor;
18+
import com.ita.if103java.ims.security.UserDetailsImpl;
19+
import com.ita.if103java.ims.service.DashboardService;
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.Test;
22+
import org.mockito.InjectMocks;
23+
import org.mockito.Mock;
24+
import org.mockito.MockitoAnnotations;
25+
import org.springframework.data.domain.Page;
26+
import org.springframework.data.domain.PageImpl;
27+
import org.springframework.data.domain.PageRequest;
28+
import org.springframework.data.domain.Sort;
29+
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
30+
import org.springframework.http.MediaType;
31+
import org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver;
32+
import org.springframework.test.web.servlet.MockMvc;
33+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
34+
35+
import java.time.ZoneId;
36+
import java.time.ZonedDateTime;
37+
import java.util.ArrayList;
38+
import java.util.Collections;
39+
import java.util.List;
40+
41+
import static com.ita.if103java.ims.security.SecurityInterceptor.init;
42+
43+
import static org.hamcrest.Matchers.hasSize;
44+
import static org.mockito.ArgumentMatchers.any;
45+
import static org.mockito.ArgumentMatchers.anyLong;
46+
import static org.mockito.ArgumentMatchers.eq;
47+
import static org.mockito.Mockito.times;
48+
import static org.mockito.Mockito.verify;
49+
import static org.mockito.Mockito.when;
50+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
51+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
52+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
53+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
54+
55+
class DashboardControllerTest {
56+
57+
private MockMvc mockMvc;
58+
59+
@Mock
60+
DashboardService dashboardService;
61+
62+
@InjectMocks
63+
DashboardController dashboardController;
64+
65+
private User user;
66+
private UserDetailsImpl userDetails;
67+
private AccountType accountType;
68+
private ZonedDateTime currentDateTime;
69+
private DashboardDataNotFoundException dashboardDataNotFoundException;
70+
private Long fakeAccountId = 1L;
71+
private Long fakeWarehouseId = 4L;
72+
private int fakeMinQuantity = 5;
73+
private EndingItemsDto endingItemsDto;
74+
75+
@BeforeEach
76+
void setUp() {
77+
MockitoAnnotations.initMocks(this);
78+
mockMvc = MockMvcBuilders
79+
.standaloneSetup(dashboardController)
80+
.setControllerAdvice(GlobalExceptionHandler.class)
81+
.addInterceptors(new SecurityInterceptor())
82+
.setCustomArgumentResolvers(
83+
new AuthenticationPrincipalArgumentResolver(),
84+
new PageableHandlerMethodArgumentResolver())
85+
.build();
86+
87+
currentDateTime = ZonedDateTime.now(ZoneId.systemDefault());
88+
89+
user = new User(
90+
1L,
91+
"First name",
92+
"Last name",
93+
94+
"qwerty123",
95+
Role.ROLE_ADMIN,
96+
currentDateTime,
97+
currentDateTime,
98+
true,
99+
"klltnpt",
100+
1L);
101+
102+
accountType = new AccountType(
103+
2L,
104+
"Premium",
105+
300.0,
106+
2,
107+
100,
108+
100,
109+
100,
110+
100,
111+
100,
112+
true,
113+
true,
114+
true);
115+
116+
userDetails = new UserDetailsImpl(user, accountType);
117+
endingItemsDto = new EndingItemsDto(1L, "warehouse_a", "apple", 10);
118+
dashboardDataNotFoundException = new DashboardDataNotFoundException("Dashboard not found");
119+
}
120+
121+
@Test
122+
void testGetWarehouseLoad_failFlow() throws Exception {
123+
124+
when(dashboardService.getWarehouseLoad(anyLong())).thenThrow(dashboardDataNotFoundException);
125+
126+
mockMvc.perform(
127+
get("/dashboard/warehouseLoad")
128+
.principal(init(userDetails))
129+
.contentType(MediaType.APPLICATION_JSON))
130+
.andExpect(status().isNotFound())
131+
.andExpect(jsonPath("message").value(dashboardDataNotFoundException.getLocalizedMessage()));
132+
133+
}
134+
135+
@Test
136+
void testGetWarehouseLoad_successFlow() throws Exception{
137+
List<WarehouseLoadDto> list = new ArrayList();
138+
list.add(new WarehouseLoadDto(fakeWarehouseId, "warehouse_a", 100L, 10L));
139+
140+
when(dashboardService.getWarehouseLoad(anyLong())).thenReturn(list);
141+
142+
mockMvc.perform(
143+
get("/dashboard/warehouseLoad")
144+
.principal(init(userDetails))
145+
.contentType(MediaType.APPLICATION_JSON))
146+
.andExpect(status().isOk())
147+
.andExpect(jsonPath("$..id").value(fakeWarehouseId.intValue()))
148+
.andExpect(jsonPath("$..name").value("warehouse_a"))
149+
.andExpect(jsonPath("$..capacity").value(100))
150+
.andExpect(jsonPath("$..charge").value(10));
151+
152+
verify(dashboardService, times(1))
153+
.getWarehouseLoad(userDetails.getUser().getAccountId());
154+
}
155+
156+
@Test
157+
void testGetEndingItems_failFlow() throws Exception {
158+
PageRequest pageable = PageRequest.of(0, 10, Sort.Direction.ASC, "quantity");
159+
160+
when(dashboardService.getEndingItems(any(PageRequest.class), eq(fakeMinQuantity), eq(fakeAccountId)))
161+
.thenThrow(dashboardDataNotFoundException);
162+
163+
mockMvc.perform(
164+
get("/dashboard/endingItems?page=" + pageable.getPageNumber() +
165+
"&size=" + pageable.getPageSize() +
166+
"&sort=" + pageable.getSort().toString() +
167+
"&minQuantity=" + fakeMinQuantity)
168+
.principal(init(userDetails))
169+
.contentType(MediaType.APPLICATION_JSON))
170+
.andExpect(status().isNotFound())
171+
.andExpect(jsonPath("message").value(dashboardDataNotFoundException.getLocalizedMessage()));
172+
}
173+
174+
@Test
175+
void testGetEndingItems_successFlow() throws Exception {
176+
PageRequest pageable = PageRequest.of(0, 10, Sort.Direction.ASC, "quantity");
177+
178+
Page<EndingItemsDto> page = new PageImpl<>(Collections.singletonList(endingItemsDto));
179+
180+
when(dashboardService.getEndingItems(any(PageRequest.class), eq(fakeMinQuantity), eq(fakeAccountId)))
181+
.thenReturn(page);
182+
183+
mockMvc.perform(
184+
get("/dashboard/endingItems?page=" + pageable.getPageNumber() +
185+
"&size=" + pageable.getPageSize() +
186+
"&sort=" + pageable.getSort().toString() +
187+
"&minQuantity=" + fakeMinQuantity)
188+
.principal(init(userDetails))
189+
.contentType(MediaType.APPLICATION_JSON))
190+
.andExpect(status().isOk())
191+
.andExpect(jsonPath("$.content", hasSize(1)))
192+
.andExpect(jsonPath("$.numberOfElements").value(page.getTotalElements()));
193+
194+
verify(dashboardService, times(1))
195+
.getEndingItems(any(PageRequest.class), eq(fakeMinQuantity), eq(fakeAccountId));
196+
}
197+
198+
@Test
199+
void testPopularItems_failFlow() throws Exception {
200+
PopularItemsRequestDto popularItems =
201+
new PopularItemsRequestDto(3, DateType.YEAR, PopType.TOP, "17-02-2020");
202+
203+
ObjectMapper objectMapper = new ObjectMapper();
204+
objectMapper.registerModule(new JavaTimeModule());
205+
String resultJson = objectMapper.writeValueAsString(popularItems);
206+
207+
when(dashboardService.getPopularItems(any(PopularItemsRequestDto.class), anyLong()))
208+
.thenThrow(dashboardDataNotFoundException);
209+
210+
mockMvc.perform(
211+
post("/dashboard/popularityItems/")
212+
.principal(init(userDetails))
213+
.contentType(MediaType.APPLICATION_JSON)
214+
.content(resultJson))
215+
.andExpect(status().isNotFound())
216+
.andExpect(jsonPath("message").value(dashboardDataNotFoundException.getLocalizedMessage()));
217+
}
218+
219+
@Test
220+
void testPopularItems_successFlow() throws Exception {
221+
PopularItemsRequestDto popularItems =
222+
new PopularItemsRequestDto(3, DateType.YEAR, PopType.TOP, "17-02-2020");
223+
224+
List<PopularItemsDto> list = new ArrayList<>();
225+
list.add(new PopularItemsDto("apple", 10L));
226+
227+
ObjectMapper objectMapper = new ObjectMapper();
228+
objectMapper.registerModule(new JavaTimeModule());
229+
String resultJson = objectMapper.writeValueAsString(popularItems);
230+
231+
when(dashboardService.getPopularItems(any(PopularItemsRequestDto.class), anyLong())).thenReturn(list);
232+
233+
mockMvc.perform(
234+
post("/dashboard/popularityItems/")
235+
.principal(init(userDetails))
236+
.contentType(MediaType.APPLICATION_JSON)
237+
.content(resultJson))
238+
.andExpect(status().isOk())
239+
.andExpect(jsonPath("$..name").value(list.get(0).getName()))
240+
.andExpect(jsonPath("$..quantity").value(list.get(0).getQuantity().intValue()));
241+
242+
verify(dashboardService, times(1))
243+
.getPopularItems(any(PopularItemsRequestDto.class), anyLong());
244+
}
245+
246+
@Test
247+
public void testPremiumLoad_failFlow() throws Exception {
248+
249+
when(dashboardService.getPreLoad(anyLong(), anyLong())).thenThrow(dashboardDataNotFoundException);
250+
251+
mockMvc.perform(
252+
get("/dashboard/premiumLoad/" + fakeWarehouseId)
253+
.principal(init(userDetails))
254+
.contentType(MediaType.APPLICATION_JSON))
255+
.andExpect(status().isNotFound())
256+
.andExpect(jsonPath("message").value(dashboardDataNotFoundException.getLocalizedMessage()));
257+
258+
259+
verify(dashboardService, times(1))
260+
.getPreLoad(anyLong(), anyLong());
261+
}
262+
263+
@Test
264+
public void testPremiumLoad_successFlow() throws Exception {
265+
WarehousePremiumStructDto wpsd = new WarehousePremiumStructDto(
266+
fakeWarehouseId,
267+
"warehouse_A",
268+
10L,
269+
100L,
270+
List
271+
.of(new WarehousePremiumStructDto(
272+
fakeWarehouseId + 1,
273+
"warehouse_B",
274+
3L,
275+
60L,
276+
null),
277+
new WarehousePremiumStructDto(
278+
fakeWarehouseId + 2,
279+
"warehouse_C",
280+
7L,
281+
40L,
282+
null)
283+
)
284+
);
285+
when(dashboardService.getPreLoad(anyLong(), anyLong())).thenReturn(wpsd);
286+
287+
mockMvc.perform(
288+
get("/dashboard/premiumLoad/" + fakeWarehouseId)
289+
.principal(init(userDetails))
290+
.contentType(MediaType.APPLICATION_JSON))
291+
.andExpect(status().isOk())
292+
.andExpect(jsonPath("$.id").value(wpsd.getId()))
293+
.andExpect(jsonPath("$.name").value(wpsd.getName()))
294+
.andExpect(jsonPath("$.charge").value(wpsd.getCharge()))
295+
.andExpect(jsonPath("$.capacity").value(wpsd.getCapacity()))
296+
.andExpect(jsonPath("$.childs", hasSize(2)));
297+
298+
verify(dashboardService, times(1))
299+
.getPreLoad(anyLong(), anyLong());
300+
}
301+
}
302+

0 commit comments

Comments
 (0)