Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hr.inceptum.fsm.rest;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
- import java.sql.SQLException;
- import java.time.Duration;
- import java.time.Instant;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.UUID;
- import javax.transaction.Transactional;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.DisplayName;
- import org.junit.jupiter.api.Test;
- import org.mockito.MockitoAnnotations;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.boot.test.mock.mockito.MockBean;
- import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
- import org.springframework.test.util.ReflectionTestUtils;
- import org.springframework.test.web.servlet.MockMvc;
- import org.springframework.test.web.servlet.MvcResult;
- import org.springframework.test.web.servlet.setup.MockMvcBuilders;
- import com.fasterxml.jackson.core.type.TypeReference;
- import hr.inceptum.addressbook.AddressBookRestServices;
- import hr.inceptum.addressbook.customer.Customer;
- import hr.inceptum.addressbook.location.Location;
- import hr.inceptum.fsm.dto.workorder.WorkOrderListItemDto;
- import hr.inceptum.fsm.entity.workorder.PreferredTime;
- import hr.inceptum.fsm.entity.workorder.Priority;
- import hr.inceptum.fsm.entity.workorder.WorkOrder;
- import hr.inceptum.fsm.entity.workorder.WorkOrderStatus;
- import hr.inceptum.fsm.entity.workorder.WorkOrderType;
- import hr.inceptum.fsm.service.workorder.WorkOrderService;
- import hr.inceptum.hive.commons.dao.EntityDao;
- import hr.inceptum.hive.commons.entity.AbstractPersistable;
- import hr.inceptum.hive.commons.entity.Tenant;
- import hr.inceptum.hive.commons.rest.RestExceptionsAdvice;
- import hr.inceptum.hive.commons.search.model.CompositeCriterion;
- import hr.inceptum.hive.commons.search.model.CompositionType;
- import hr.inceptum.hive.commons.search.model.Criterion;
- import hr.inceptum.hive.commons.search.model.CriterionType;
- import hr.inceptum.hive.commons.search.model.SearchCriteria;
- import hr.inceptum.hive.commons.search.model.SearchResult;
- import hr.inceptum.hive.commons.search.model.SimpleCriterion;
- import hr.inceptum.hive.commons.search.model.SortType;
- import hr.inceptum.hive.commons.search.model.Window;
- import hr.inceptum.hive.commons.service.EntityService;
- import hr.inceptum.hive.commons.support.WithMockHiveUser;
- import hr.inceptum.hive.commons.tenant.TenantStorage;
- import hr.inceptum.hive.commons.util.JsonParserUtil;
- @SpringBootTest
- @Transactional
- @SuppressWarnings({ "PMD.TooManyFields", "PMD.AvoidDuplicateLiterals", "PMD.UnusedPrivateField" })
- class WorkOrderGetAllControllerTest {
- private static final String BASE_URL = WorkOrderController.URL + "/list";
- // private static final String BASE_URL_SCHEDULER = SchedulerTimelineController.URL + "/";
- private static final String TIME_FIELD = "time";
- private static final String STATUS_FIELD = "status";
- private static final Instant WINDOW_FROM = Instant.parse("2019-05-02T00:00:00.00Z");
- private static final Instant WINDOW_TO = Instant.parse("2019-05-03T00:00:00.00Z");
- private static final Instant BEFORE = Instant.parse("2019-05-01T00:00:00.00Z");
- private static final Instant INSIDE = Instant.parse("2019-05-02T12:00:00.00Z");
- private static final Instant AFTER = Instant.parse("2019-05-04T00:00:00.00Z");
- @MockBean
- private AddressBookRestServices addressBookRestServices;
- @Autowired
- private WorkOrderController workOrderController;
- @Autowired
- private EntityService<Tenant> tenantService;
- @Autowired
- private EntityService<WorkOrderType> workOrderTypeService;
- @Autowired
- private EntityService<Customer> customerService;
- @Autowired
- private EntityService<Location> locationService;
- @Autowired
- private EntityDao<AbstractPersistable> entityDao;
- @Autowired
- private WorkOrderService workOrderService;
- @Autowired
- private MappingJackson2HttpMessageConverter messageConverter;
- @Autowired
- private RestExceptionsAdvice exceptionsAdvice;
- private WorkOrderType createdWorkOrderType;
- private Customer createdCustomer;
- private Location createdLocation;
- private WorkOrder unassignedEmpty;
- private WorkOrder unassignedInside;
- private WorkOrder unassignedInsideMulti;
- private WorkOrder unassignedBefore;
- private WorkOrder unassignedAfter;
- private WorkOrder assignedBefore;
- private WorkOrder assignedBeforePreferred;
- private WorkOrder assignedInsideMulti;
- private WorkOrder assignedInsidePreferred;
- private WorkOrder assignedAfterPreferred;
- private WorkOrder canceledEmpty;
- private MockMvc mockMvc;
- @SuppressWarnings("CPD-START")
- @BeforeEach
- public void setUp() throws SQLException {
- // WorkOrder assignedBefore;
- // WorkOrder assignedBeforePreferred;
- // WorkOrder assignedAfterPreferred;
- MockitoAnnotations.initMocks(this);
- // @formatter:off
- mockMvc = MockMvcBuilders.standaloneSetup(workOrderController)
- .setControllerAdvice(exceptionsAdvice)
- .setMessageConverters(messageConverter).build();
- // @formatter:on
- createTenant();
- createdWorkOrderType = createWorkOrderType();
- createdCustomer = createCutomer();
- createdLocation = createLocation();
- entityDao.flush();
- unassignedEmpty = createWorkOrder("12341243", WorkOrderStatus.UNASSIGNED, null, Set.of());
- unassignedInside = createWorkOrder("12341244", WorkOrderStatus.UNASSIGNED, null, Set.of(new PreferredTime(INSIDE, AFTER)));
- unassignedInsideMulti = createWorkOrder("12341245", WorkOrderStatus.UNASSIGNED, null,
- Set.of(new PreferredTime(BEFORE, INSIDE), new PreferredTime(INSIDE, WINDOW_TO), new PreferredTime(AFTER, AFTER)));
- unassignedBefore = createWorkOrder("12341246", WorkOrderStatus.UNASSIGNED, null, Set.of(new PreferredTime(BEFORE, BEFORE)));
- unassignedAfter = createWorkOrder("12341247", WorkOrderStatus.UNASSIGNED, null, Set.of(new PreferredTime(AFTER, AFTER)));
- assignedBefore = createWorkOrder("12341248", WorkOrderStatus.ASSIGNED, BEFORE, Set.of());
- assignedBeforePreferred = createWorkOrder("12341249", WorkOrderStatus.ASSIGNED, BEFORE, Set.of(new PreferredTime(INSIDE, AFTER)));
- assignedInsideMulti = createWorkOrder("12341250", WorkOrderStatus.ASSIGNED, INSIDE,
- Set.of(new PreferredTime(BEFORE, INSIDE), new PreferredTime(INSIDE, WINDOW_TO), new PreferredTime(AFTER, AFTER)));
- assignedInsidePreferred = createWorkOrder("12341251", WorkOrderStatus.ASSIGNED, INSIDE, Set.of(new PreferredTime(BEFORE, BEFORE)));
- assignedAfterPreferred = createWorkOrder("12341252", WorkOrderStatus.ASSIGNED, AFTER, Set.of(new PreferredTime(INSIDE, AFTER)));
- canceledEmpty = createWorkOrder("12341253", WorkOrderStatus.CANCELED, null, Set.of());
- entityDao.flush();
- }
- private void createTenant() {
- Tenant tenant = new Tenant();
- ReflectionTestUtils.setField(tenant, "id", UUID.randomUUID());
- tenant.setName("Inceptum");
- tenant = tenantService.create(tenant);
- TenantStorage.setTenant(tenant);
- }
- private WorkOrderType createWorkOrderType() {
- final WorkOrderType workOrderType = new WorkOrderType();
- workOrderType.setName("WorkOrderTestType");
- workOrderType.setEstimatedDuration(Duration.ofHours(2L));
- return workOrderTypeService.create(workOrderType);
- }
- private Customer createCutomer() {
- final Customer customer = new Customer();
- customer.setId(UUID.randomUUID());
- return customerService.create(customer);
- }
- private Location createLocation() {
- final Location location = new Location();
- location.setId(UUID.randomUUID());
- return locationService.create(location);
- }
- private WorkOrder createWorkOrder(final String referenceId, final WorkOrderStatus status, final Instant scheduled, final Set<PreferredTime> preferredTimes) {
- final WorkOrder workOrder = new WorkOrder();
- workOrder.setReferenceId(referenceId);
- workOrder.setCardinalNumber(workOrderService.getCount(WorkOrder.class) + 1);
- workOrder.setCustomer(createdCustomer);
- workOrder.setLocation(createdLocation);
- workOrder.setPriority(Priority.NORMAL);
- workOrder.setStatus(WorkOrderStatus.ASSIGNED);
- workOrder.setWorkOrderType(createdWorkOrderType);
- workOrder.setScheduled(scheduled);
- workOrder.setDuration(Duration.ofHours(2L));
- // workOrder.setWorkers(workers);
- workOrder.setPreferredTimes(preferredTimes);
- workOrder.setStatus(status);
- // final WorkOrder created = workOrderService.create(workOrder);
- // workOrderStatusHistoryService.create(new WorkOrderStatusHistory(created, WorkOrderAction.CREATE,
- // null, workers.iterator().next()));
- return workOrderService.create(workOrder);
- // return created;
- }
- @Test
- @WithMockHiveUser(authorities = { "WORK_ORDER_EDIT" })
- @DisplayName("Test for getting full list")
- public void testGetAll() throws Exception {
- final SearchCriteria searchCriteria = new SearchCriteria();
- // final SearchResult<WorkOrder> result = workOrderDao.getList(searchCriteria);
- // @formatter:off
- final MvcResult result = mockMvc.perform(post(BASE_URL)
- .contentType(SearchCriteria.CONTENT_TYPE)
- .content(JsonParserUtil.asJsonString(searchCriteria))
- .accept(WorkOrderListItemDto.CONTENT_TYPE))
- .andExpect(status().isOk())
- .andReturn();
- // @formatter:on
- final SearchResult<WorkOrderListItemDto> received = JsonParserUtil.readFromJson(result.getResponse().getContentAsString(), new TypeReference<SearchResult<WorkOrderListItemDto>>() {
- });
- // final WorkOrder entity = workOrderMapper.newEntity(received);
- // final WorkOrder entity = workOrderListItemMapper.newEntity(received.getResults().get(0));
- assertEquals(11, received.getTotalResults().longValue(), "Should have all 11 created work orders");
- assertEquals(11, received.getResults().size(), "Should contain all 11 results");
- // all unassigned work orders in front, ordered by min preferred time, with null last.
- // same times are ordered by creation order
- assertEquals(unassignedInsideMulti.getId(), received.getResults().get(0).getId(), "1st result should be unassignedInsideMulti work order");
- assertEquals(unassignedBefore.getId(), received.getResults().get(1).getId(), "2nd result should beunassignedBefore work order");
- assertEquals(unassignedInside.getId(), received.getResults().get(2).getId(), "3rd result should beunassignedInside work order");
- assertEquals(unassignedAfter.getId(), received.getResults().get(3).getId(), "4th result should beunassignedAfter work order");
- assertEquals(unassignedEmpty.getId(), received.getResults().get(4).getId(), "5th result should beunassignedEmpty work order");
- // then all other statuses ordered by min preferred time, with null last.
- // same times are ordered by creation order
- assertEquals(assignedBefore.getId(), received.getResults().get(5).getId(), "6th result should be assignedBefore work order");
- assertEquals(assignedBeforePreferred.getId(), received.getResults().get(6).getId(), "7th result should be assignedBeforePreferred work order");
- assertEquals(assignedInsideMulti.getId(), received.getResults().get(7).getId(), "8th result should be assignedInsideMulti work order");
- assertEquals(assignedInsidePreferred.getId(), received.getResults().get(8).getId(), "9th result should be assignedInsidePreferred work order");
- assertEquals(assignedAfterPreferred.getId(), received.getResults().get(9).getId(), "10th result should be assignedAfterPreferred work order");
- assertEquals(canceledEmpty.getId(), received.getResults().get(10).getId(), "11th result should be canceledEmpty work order");
- }
- @Test
- @WithMockHiveUser(authorities = { "WORK_ORDER_EDIT" })
- @DisplayName("Test for getting Windowed list")
- public void testWindowedList() throws Exception {
- final SearchCriteria searchCriteria = new SearchCriteria();
- searchCriteria.setWindow(new Window(0, 5));
- // final SearchResult<WorkOrder> result = workOrderDao.getList(searchCriteria);
- // @formatter:off
- final MvcResult result = mockMvc.perform(post(BASE_URL)
- .contentType(SearchCriteria.CONTENT_TYPE)
- .content(JsonParserUtil.asJsonString(searchCriteria))
- .accept(WorkOrderListItemDto.CONTENT_TYPE))
- .andExpect(status().isOk())
- .andReturn();
- // @formatter:on
- final SearchResult<WorkOrderListItemDto> received = JsonParserUtil.readFromJson(result.getResponse().getContentAsString(), new TypeReference<SearchResult<WorkOrderListItemDto>>() {
- });
- assertEquals(11, received.getTotalResults().longValue(), "Should have all 11 created work orders");
- assertEquals(5, received.getResults().size(), "Should be limited to 5 work orders");
- assertEquals(unassignedInsideMulti.getId(), received.getResults().get(0).getId(), "1st result should be unassignedInsideMulti work order");
- assertEquals(unassignedBefore.getId(), received.getResults().get(1).getId(), "2nd result should be unassignedBefore work order");
- assertEquals(unassignedInside.getId(), received.getResults().get(2).getId(), "3rd result should be unassignedInside work order");
- assertEquals(unassignedAfter.getId(), received.getResults().get(3).getId(), "4th result should be unassignedAfter work order");
- assertEquals(unassignedEmpty.getId(), received.getResults().get(4).getId(), "5th result should be unassignedEmpty work order");
- }
- @Test
- @WithMockHiveUser(authorities = { "WORK_ORDER_EDIT" })
- @DisplayName("Test for getting filtered list")
- public void testFilteredList() throws Exception {
- final SearchCriteria searchCriteria = new SearchCriteria();
- searchCriteria.setCriterion(new SimpleCriterion(STATUS_FIELD, WorkOrderStatus.CANCELED, CriterionType.EQUAL));
- // @formatter:off
- final MvcResult result = mockMvc.perform(post(BASE_URL)
- .contentType(SearchCriteria.CONTENT_TYPE)
- .content(JsonParserUtil.asJsonString(searchCriteria))
- .accept(WorkOrderListItemDto.CONTENT_TYPE))
- .andExpect(status().isOk())
- .andReturn();
- // @formatter:on
- final SearchResult<WorkOrderListItemDto> received = JsonParserUtil.readFromJson(result.getResponse().getContentAsString(), new TypeReference<SearchResult<WorkOrderListItemDto>>() {
- });
- // final SearchResult<WorkOrder> result = workOrderDao.getList(searchCriteria);
- assertEquals(1, received.getTotalResults().longValue(), "Should have only one canceled work order");
- assertEquals(1, received.getResults().size(), "Should have only one canceled work order");
- assertEquals(canceledEmpty.getId(), received.getResults().get(0).getId(), "Only result should be canceled work order");
- }
- @Test
- @WithMockHiveUser(authorities = { "WORK_ORDER_EDIT" })
- @DisplayName("Test for getting full scheduler")
- public void testFullScheduler() throws Exception {
- final SearchCriteria searchCriteria = new SearchCriteria();
- final Criterion timeGreater = new SimpleCriterion(TIME_FIELD, WINDOW_FROM, CriterionType.GREATER_EQUAL);
- final Criterion timeLess = new SimpleCriterion(TIME_FIELD, WINDOW_TO, CriterionType.LESS);
- final Criterion status = new SimpleCriterion(STATUS_FIELD, WorkOrderStatus.CANCELED, CriterionType.NOT_EQUAL);
- searchCriteria.setCriterion(new CompositeCriterion(CompositionType.AND, List.of(timeGreater, timeLess, status)));
- // final SearchResult<WorkOrder> result = workOrderDao.getList(searchCriteria);
- // @formatter:off
- final MvcResult result = mockMvc.perform(post(BASE_URL)
- .contentType(SearchCriteria.CONTENT_TYPE)
- .content(JsonParserUtil.asJsonString(searchCriteria))
- .accept(WorkOrderListItemDto.CONTENT_TYPE))
- .andExpect(status().isOk())
- .andReturn();
- // @formatter:on
- final SearchResult<WorkOrderListItemDto> received = JsonParserUtil.readFromJson(result.getResponse().getContentAsString(), new TypeReference<SearchResult<WorkOrderListItemDto>>() {
- });
- assertEquals(5, received.getTotalResults().longValue(), "Should have 5 of created work orders in window");
- assertEquals(5, received.getResults().size(), "Should have 5 of created work orders in window");
- // all unassigned work orders in front, ordered by min preferred time, with null last.
- // same times are ordered by creation order
- assertEquals(unassignedInsideMulti.getId(), received.getResults().get(0).getId(), "1st result should be unassignedInsideMulti work order");
- assertEquals(unassignedInside.getId(), received.getResults().get(1).getId(), "2nd result should be unassignedInside work order");
- assertEquals(unassignedEmpty.getId(), received.getResults().get(2).getId(), "3rd result should be unassignedEmpty work order");
- // then all other statuses ordered by min preferred time, with null last.
- // same times are ordered by creation order
- assertEquals(assignedInsideMulti.getId(), received.getResults().get(3).getId(), "4th result should be assignedInsideMulti work order");
- assertEquals(assignedInsidePreferred.getId(), received.getResults().get(4).getId(), "5th result should be assignedInsidePreferred work order");
- }
- @Test
- @WithMockHiveUser(authorities = { "WORK_ORDER_EDIT" })
- @DisplayName("Test for getting filtered scheduler")
- public void testFilteredScheduler() throws Exception {
- final SearchCriteria searchCriteria = new SearchCriteria();
- final Criterion timeGreater = new SimpleCriterion(TIME_FIELD, WINDOW_FROM, CriterionType.GREATER_EQUAL);
- final Criterion timeLess = new SimpleCriterion(TIME_FIELD, WINDOW_TO, CriterionType.LESS);
- final Criterion status = new SimpleCriterion(STATUS_FIELD, WorkOrderStatus.CANCELED, CriterionType.NOT_EQUAL);
- final Criterion schedulerCriterion = new CompositeCriterion(CompositionType.AND, List.of(timeGreater, timeLess, status));
- final Criterion custom = new SimpleCriterion(STATUS_FIELD, WorkOrderStatus.ASSIGNED, CriterionType.EQUAL);
- searchCriteria.setCriterion(new CompositeCriterion(CompositionType.AND, List.of(schedulerCriterion, custom)));
- // final SearchResult<WorkOrder> result = workOrderDao.getList(searchCriteria);
- // @formatter:off
- final MvcResult result = mockMvc.perform(post(BASE_URL)
- .contentType(SearchCriteria.CONTENT_TYPE)
- .content(JsonParserUtil.asJsonString(searchCriteria))
- .accept(WorkOrderListItemDto.CONTENT_TYPE))
- .andExpect(status().isOk())
- .andReturn();
- // @formatter:on
- final SearchResult<WorkOrderListItemDto> received = JsonParserUtil.readFromJson(result.getResponse().getContentAsString(), new TypeReference<SearchResult<WorkOrderListItemDto>>() {
- });
- assertEquals(2, received.getTotalResults().longValue(), "Should have 2 of created work orders in window");
- assertEquals(2, received.getResults().size(), "Should have 2 of created work orders in window");
- // then all other statuses ordered by min preferred time, with null last.
- // same times are ordered by creation order
- assertEquals(assignedInsideMulti.getId(), received.getResults().get(0).getId(), "1st result should be assignedInsideMulti work order");
- assertEquals(assignedInsidePreferred.getId(), received.getResults().get(1).getId(), "2nd result should be assignedInsidePreferred work order");
- }
- @Test
- @WithMockHiveUser(authorities = { "WORK_ORDER_EDIT" })
- @SuppressWarnings("CPD-END")
- @DisplayName("Test for getting sorted scheduler")
- public void testSortedScheduler() throws Exception {
- final SearchCriteria searchCriteria = new SearchCriteria();
- final Criterion timeGreater = new SimpleCriterion(TIME_FIELD, WINDOW_FROM, CriterionType.GREATER_EQUAL);
- final Criterion timeLess = new SimpleCriterion(TIME_FIELD, WINDOW_TO, CriterionType.LESS);
- final Criterion status = new SimpleCriterion(STATUS_FIELD, WorkOrderStatus.CANCELED, CriterionType.NOT_EQUAL);
- searchCriteria.setCriterion(new CompositeCriterion(CompositionType.AND, List.of(timeGreater, timeLess, status)));
- searchCriteria.setSortProperties(Map.of(TIME_FIELD, SortType.DESC));
- // final SearchResult<WorkOrder> result = workOrderDao.getList(searchCriteria);
- // @formatter:off
- final MvcResult result = mockMvc.perform(post(BASE_URL )
- .contentType(SearchCriteria.CONTENT_TYPE)
- .content(JsonParserUtil.asJsonString(searchCriteria))
- .accept(WorkOrderListItemDto.CONTENT_TYPE))
- .andExpect(status().isOk())
- .andReturn();
- // @formatter:on
- final SearchResult<WorkOrderListItemDto> received = JsonParserUtil.readFromJson(result.getResponse().getContentAsString(), new TypeReference<SearchResult<WorkOrderListItemDto>>() {
- });
- assertEquals(5, received.getTotalResults().longValue(), "Should have 5 of created work orders in scheduler");
- assertEquals(5, received.getResults().size(), "Should have 5 of created work orders in scheduler");
- // first ordered by time desc and then by other default filters. with null values on end.
- assertEquals(unassignedInside.getId(), received.getResults().get(0).getId(), "1st result should be unassignedInside work order");
- assertEquals(assignedInsideMulti.getId(), received.getResults().get(1).getId(), "2nd result should be assignedInsideMulti work order");
- assertEquals(assignedInsidePreferred.getId(), received.getResults().get(2).getId(), "3rd result should be assignedInsidePreferred work order");
- assertEquals(unassignedInsideMulti.getId(), received.getResults().get(3).getId(), "4th result should be unassignedInsideMulti work order");
- assertEquals(unassignedEmpty.getId(), received.getResults().get(4).getId(), "5th result should be unassignedEmpty work order");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement