@Test public void shouldThrowExceptionWhileConvertingRequisitionsToOrderIfUserDoesNotHaveRights() throws Exception { Program program = new Program(); Long userId = 1L; Rnr rnr = new Rnr(); rnr.setId(1L); rnr.setSupervisoryNodeId(1L); rnr.setProgram(program); SupplyLine supplyLine = new SupplyLine(); supplyLine.setSupplyingFacility(new Facility(99L)); supplyLine.setExportOrders(Boolean.FALSE); when(requisitionService.getLWById(1L)).thenReturn(rnr); SupervisoryNode supervisoryNode = new SupervisoryNode(1L); whenNew(SupervisoryNode.class).withArguments(1l).thenReturn(supervisoryNode); when(supplyLineService.getSupplyLineBy(supervisoryNode, program)).thenReturn(supplyLine); List<Rnr> rnrList = new ArrayList<>(); rnrList.add(rnr); when(requisitionService.getFullRequisitionById(1L)).thenReturn(rnr); when(fulfillmentPermissionService.hasPermissionOnWarehouse(userId, 99L, CONVERT_TO_ORDER)).thenReturn(false); expectedException.expect(AccessDeniedException.class); expectedException.expectMessage("user.not.authorized"); orderService.convertToOrder(rnrList, userId); verify(supplyLineService).getSupplyLineBy(supervisoryNode, program); verify(requisitionService).getLWById(rnr.getId()); verify(requisitionService).releaseRequisitionsAsOrder(rnrList, userId); }
@Transactional public void convertToOrder(List<Rnr> rnrList, Long userId) { requisitionService.releaseRequisitionsAsOrder(rnrList, userId); Order order; for (Rnr rnr : rnrList) { Long depotId = rnr.getSupplyingDepotId(); rnr = requisitionService.getLWById(rnr.getId()); rnr.setModifiedBy(userId); order = new Order(rnr); SupplyLine supplyLine = supplyLineService.getSupplyLineBy(new SupervisoryNode(rnr.getSupervisoryNodeId()), rnr.getProgram()); if(depotId != null && supplyLine.getSupplyingFacility().getId() != depotId){ supplyLine = supplyLineService.getByFacilityProgram(depotId, rnr.getProgram().getId()); } order.setSupplyLine(supplyLine); if (!fulfillmentPermissionService.hasPermissionOnWarehouse(userId, supplyLine.getSupplyingFacility().getId(), RightName.CONVERT_TO_ORDER)) { throw new AccessDeniedException("user.not.authorized"); } OrderStatus status; if (order.getSupplyLine() == null) { status = TRANSFER_FAILED; order.setFtpComment(SUPPLY_LINE_MISSING_COMMENT); } else { status = order.getSupplyLine().getExportOrders() ? IN_ROUTE : READY_TO_PACK; } order.setStatus(status); order.setOrderNumber(getOrderNumberConfiguration().getOrderNumberFor(rnr.getId(), programService.getById(rnr.getProgram().getId()), rnr.isEmergency())); orderRepository.save(order); order.setRnr(requisitionService.getFullRequisitionById(order.getRnr().getId())); sendOrderStatusChangeMail(order); orderEventService.notifyForStatusChange(order); } }
OrderService { @Transactional public void convertToOrder(List<Rnr> rnrList, Long userId) { requisitionService.releaseRequisitionsAsOrder(rnrList, userId); Order order; for (Rnr rnr : rnrList) { Long depotId = rnr.getSupplyingDepotId(); rnr = requisitionService.getLWById(rnr.getId()); rnr.setModifiedBy(userId); order = new Order(rnr); SupplyLine supplyLine = supplyLineService.getSupplyLineBy(new SupervisoryNode(rnr.getSupervisoryNodeId()), rnr.getProgram()); if(depotId != null && supplyLine.getSupplyingFacility().getId() != depotId){ supplyLine = supplyLineService.getByFacilityProgram(depotId, rnr.getProgram().getId()); } order.setSupplyLine(supplyLine); if (!fulfillmentPermissionService.hasPermissionOnWarehouse(userId, supplyLine.getSupplyingFacility().getId(), RightName.CONVERT_TO_ORDER)) { throw new AccessDeniedException("user.not.authorized"); } OrderStatus status; if (order.getSupplyLine() == null) { status = TRANSFER_FAILED; order.setFtpComment(SUPPLY_LINE_MISSING_COMMENT); } else { status = order.getSupplyLine().getExportOrders() ? IN_ROUTE : READY_TO_PACK; } order.setStatus(status); order.setOrderNumber(getOrderNumberConfiguration().getOrderNumberFor(rnr.getId(), programService.getById(rnr.getProgram().getId()), rnr.isEmergency())); orderRepository.save(order); order.setRnr(requisitionService.getFullRequisitionById(order.getRnr().getId())); sendOrderStatusChangeMail(order); orderEventService.notifyForStatusChange(order); } } }
OrderService { @Transactional public void convertToOrder(List<Rnr> rnrList, Long userId) { requisitionService.releaseRequisitionsAsOrder(rnrList, userId); Order order; for (Rnr rnr : rnrList) { Long depotId = rnr.getSupplyingDepotId(); rnr = requisitionService.getLWById(rnr.getId()); rnr.setModifiedBy(userId); order = new Order(rnr); SupplyLine supplyLine = supplyLineService.getSupplyLineBy(new SupervisoryNode(rnr.getSupervisoryNodeId()), rnr.getProgram()); if(depotId != null && supplyLine.getSupplyingFacility().getId() != depotId){ supplyLine = supplyLineService.getByFacilityProgram(depotId, rnr.getProgram().getId()); } order.setSupplyLine(supplyLine); if (!fulfillmentPermissionService.hasPermissionOnWarehouse(userId, supplyLine.getSupplyingFacility().getId(), RightName.CONVERT_TO_ORDER)) { throw new AccessDeniedException("user.not.authorized"); } OrderStatus status; if (order.getSupplyLine() == null) { status = TRANSFER_FAILED; order.setFtpComment(SUPPLY_LINE_MISSING_COMMENT); } else { status = order.getSupplyLine().getExportOrders() ? IN_ROUTE : READY_TO_PACK; } order.setStatus(status); order.setOrderNumber(getOrderNumberConfiguration().getOrderNumberFor(rnr.getId(), programService.getById(rnr.getProgram().getId()), rnr.isEmergency())); orderRepository.save(order); order.setRnr(requisitionService.getFullRequisitionById(order.getRnr().getId())); sendOrderStatusChangeMail(order); orderEventService.notifyForStatusChange(order); } }  }
OrderService { @Transactional public void convertToOrder(List<Rnr> rnrList, Long userId) { requisitionService.releaseRequisitionsAsOrder(rnrList, userId); Order order; for (Rnr rnr : rnrList) { Long depotId = rnr.getSupplyingDepotId(); rnr = requisitionService.getLWById(rnr.getId()); rnr.setModifiedBy(userId); order = new Order(rnr); SupplyLine supplyLine = supplyLineService.getSupplyLineBy(new SupervisoryNode(rnr.getSupervisoryNodeId()), rnr.getProgram()); if(depotId != null && supplyLine.getSupplyingFacility().getId() != depotId){ supplyLine = supplyLineService.getByFacilityProgram(depotId, rnr.getProgram().getId()); } order.setSupplyLine(supplyLine); if (!fulfillmentPermissionService.hasPermissionOnWarehouse(userId, supplyLine.getSupplyingFacility().getId(), RightName.CONVERT_TO_ORDER)) { throw new AccessDeniedException("user.not.authorized"); } OrderStatus status; if (order.getSupplyLine() == null) { status = TRANSFER_FAILED; order.setFtpComment(SUPPLY_LINE_MISSING_COMMENT); } else { status = order.getSupplyLine().getExportOrders() ? IN_ROUTE : READY_TO_PACK; } order.setStatus(status); order.setOrderNumber(getOrderNumberConfiguration().getOrderNumberFor(rnr.getId(), programService.getById(rnr.getProgram().getId()), rnr.isEmergency())); orderRepository.save(order); order.setRnr(requisitionService.getFullRequisitionById(order.getRnr().getId())); sendOrderStatusChangeMail(order); orderEventService.notifyForStatusChange(order); } }  @Autowired void setPageSize(@Value("${order.page.size}") String pageSize); @Transactional void convertToOrder(List<Rnr> rnrList, Long userId); List<Order> getOrdersForPage(int page, Long userId, String rightName); List<Order> getOrdersForPage(int page, Long userId, String right, Long supplyDepot, Long program, Long period); Order getOrder(Long id); void updateStatusAndShipmentIdForOrders(Set<String> orderNumbers, ShipmentFileInfo shipmentFileInfo); OrderFileTemplateDTO getOrderFileTemplateDTO(); @Transactional void saveOrderFileTemplate(OrderFileTemplateDTO orderFileTemplateDTO, Long userId); OrderNumberConfiguration getOrderNumberConfiguration(); @Transactional void updateOrderNumberConfiguration(OrderNumberConfiguration orderNumberConfiguration); Set<DateFormat> getAllDateFormats(); void updateOrderStatus(Order order); boolean isShippable(String orderNumber); Integer getNumberOfPages(); Integer getNumberOfPages(Long supplyDepot, Long program, Long period); Integer getPageSize(); List<Order> searchByStatusAndRight(Long userId, String rightName, List<OrderStatus> statuses, Long programId, Long facilityId); boolean hasStatus(String orderNumber, OrderStatus... statuses); Order getByOrderNumber(String orderNumber); }
OrderService { @Transactional public void convertToOrder(List<Rnr> rnrList, Long userId) { requisitionService.releaseRequisitionsAsOrder(rnrList, userId); Order order; for (Rnr rnr : rnrList) { Long depotId = rnr.getSupplyingDepotId(); rnr = requisitionService.getLWById(rnr.getId()); rnr.setModifiedBy(userId); order = new Order(rnr); SupplyLine supplyLine = supplyLineService.getSupplyLineBy(new SupervisoryNode(rnr.getSupervisoryNodeId()), rnr.getProgram()); if(depotId != null && supplyLine.getSupplyingFacility().getId() != depotId){ supplyLine = supplyLineService.getByFacilityProgram(depotId, rnr.getProgram().getId()); } order.setSupplyLine(supplyLine); if (!fulfillmentPermissionService.hasPermissionOnWarehouse(userId, supplyLine.getSupplyingFacility().getId(), RightName.CONVERT_TO_ORDER)) { throw new AccessDeniedException("user.not.authorized"); } OrderStatus status; if (order.getSupplyLine() == null) { status = TRANSFER_FAILED; order.setFtpComment(SUPPLY_LINE_MISSING_COMMENT); } else { status = order.getSupplyLine().getExportOrders() ? IN_ROUTE : READY_TO_PACK; } order.setStatus(status); order.setOrderNumber(getOrderNumberConfiguration().getOrderNumberFor(rnr.getId(), programService.getById(rnr.getProgram().getId()), rnr.isEmergency())); orderRepository.save(order); order.setRnr(requisitionService.getFullRequisitionById(order.getRnr().getId())); sendOrderStatusChangeMail(order); orderEventService.notifyForStatusChange(order); } }  @Autowired void setPageSize(@Value("${order.page.size}") String pageSize); @Transactional void convertToOrder(List<Rnr> rnrList, Long userId); List<Order> getOrdersForPage(int page, Long userId, String rightName); List<Order> getOrdersForPage(int page, Long userId, String right, Long supplyDepot, Long program, Long period); Order getOrder(Long id); void updateStatusAndShipmentIdForOrders(Set<String> orderNumbers, ShipmentFileInfo shipmentFileInfo); OrderFileTemplateDTO getOrderFileTemplateDTO(); @Transactional void saveOrderFileTemplate(OrderFileTemplateDTO orderFileTemplateDTO, Long userId); OrderNumberConfiguration getOrderNumberConfiguration(); @Transactional void updateOrderNumberConfiguration(OrderNumberConfiguration orderNumberConfiguration); Set<DateFormat> getAllDateFormats(); void updateOrderStatus(Order order); boolean isShippable(String orderNumber); Integer getNumberOfPages(); Integer getNumberOfPages(Long supplyDepot, Long program, Long period); Integer getPageSize(); List<Order> searchByStatusAndRight(Long userId, String rightName, List<OrderStatus> statuses, Long programId, Long facilityId); boolean hasStatus(String orderNumber, OrderStatus... statuses); Order getByOrderNumber(String orderNumber); static String SUPPLY_LINE_MISSING_COMMENT; }