use of org.openlmis.stockmanagement.dto.referencedata.LotDto in project openlmis-stockmanagement by OpenLMIS.
the class StockCardSummariesServiceTest method shouldCreateDummyCards.
@Test
public void shouldCreateDummyCards() throws Exception {
// given
UUID orderable1Id = randomUUID();
UUID orderable2Id = randomUUID();
UUID orderable3Id = randomUUID();
UUID orderable4Id = randomUUID();
OrderableDto orderable1 = createOrderableDto(orderable1Id, "1");
OrderableDto orderable2 = createOrderableDto(orderable2Id, "2");
orderable2.getIdentifiers().put("tradeItem", randomUUID().toString());
OrderableDto orderable3 = createOrderableDto(orderable3Id, "3");
OrderableDto orderable4 = createOrderableDto(orderable4Id, "4");
UUID programId = randomUUID();
UUID facilityId = randomUUID();
// 1,2,3,4 all approved
when(orderableReferenceDataService.findAll()).thenReturn(asList(orderable1, orderable2, orderable3, orderable4));
// but only 1, 3 have cards. 2, 4 don't have cards.
when(cardRepository.getIdentitiesBy(programId, facilityId)).thenReturn(asList(new OrderableLotIdentity(orderable1Id, null), new OrderableLotIdentity(orderable3Id, null)));
LotDto lotDto = new LotDto();
lotDto.setId(randomUUID());
// 2 has a lot
when(lotReferenceDataService.getAllLotsOf(fromString(orderable2.getIdentifiers().get("tradeItem")))).thenReturn(singletonList(lotDto));
// when
List<StockCardDto> cardDtos = stockCardSummariesService.createDummyStockCards(programId, facilityId);
// then
assertThat(cardDtos.size(), is(3));
String orderablePropertyName = "orderable";
String lotPropertyName = "lot";
String idPropertyName = "id";
String lineItemsPropertyName = "lineItems";
String stockOnHandPropertyName = "stockOnHand";
String lastUpdatePropertyName = "lastUpdate";
// 2 and lot
assertThat(cardDtos, hasItem(allOf(hasProperty(orderablePropertyName, is(orderable2)), hasProperty(lotPropertyName, is(lotDto)), hasProperty(idPropertyName, nullValue()), hasProperty(stockOnHandPropertyName, nullValue()), hasProperty(lineItemsPropertyName, nullValue()))));
// 2 and no lot
assertThat(cardDtos, hasItem(allOf(hasProperty(orderablePropertyName, is(orderable2)), hasProperty(lotPropertyName, is(nullValue())), hasProperty(idPropertyName, nullValue()), hasProperty(stockOnHandPropertyName, nullValue()), hasProperty(lastUpdatePropertyName, nullValue()), hasProperty(lineItemsPropertyName, nullValue()))));
// 4 and no lot
assertThat(cardDtos, hasItem(allOf(hasProperty(orderablePropertyName, is(orderable4)), hasProperty(lotPropertyName, is(nullValue())), hasProperty(idPropertyName, nullValue()), hasProperty(stockOnHandPropertyName, nullValue()), hasProperty(lastUpdatePropertyName, nullValue()), hasProperty(lineItemsPropertyName, nullValue()))));
}
use of org.openlmis.stockmanagement.dto.referencedata.LotDto in project openlmis-stockmanagement by OpenLMIS.
the class LotValidator method validate.
@Override
public void validate(StockEventDto stockEventDto) {
LOGGER.info("validating lot");
if (!stockEventDto.hasLineItems()) {
return;
}
stockEventDto.getLineItems().forEach(lineItem -> {
if (lineItem.hasLotId()) {
LotDto lotDto = stockEventDto.getContext().findLot(lineItem.getLotId());
checkLotExists(lineItem, lotDto);
checkLotOrderableMatches(stockEventDto, lineItem, lotDto);
}
});
}
use of org.openlmis.stockmanagement.dto.referencedata.LotDto in project openlmis-stockmanagement by OpenLMIS.
the class StockCardServiceIntegrationTest method shouldGetRefdataAndConvertOrganizationsWhenFindStockCard.
@Test
public void shouldGetRefdataAndConvertOrganizationsWhenFindStockCard() throws Exception {
// given
UUID userId = randomUUID();
StockEventDto stockEventDto = createStockEventDto();
stockEventDto.getLineItems().get(0).setLotId(randomUUID());
// 1. mock ref data service
FacilityDto cardFacility = new FacilityDto();
FacilityDto sourceFacility = new FacilityDto();
ProgramDto programDto = new ProgramDto();
OrderableDto orderableDto = new OrderableDto();
LotDto lotDto = new LotDto();
when(facilityReferenceDataService.findOne(stockEventDto.getFacilityId())).thenReturn(cardFacility);
when(facilityReferenceDataService.findOne(fromString("e6799d64-d10d-4011-b8c2-0e4d4a3f65ce"))).thenReturn(sourceFacility);
when(programReferenceDataService.findOne(stockEventDto.getProgramId())).thenReturn(programDto);
when(orderableReferenceDataService.findOne(stockEventDto.getLineItems().get(0).getOrderableId())).thenReturn(orderableDto);
when(lotReferenceDataService.findOne(stockEventDto.getLineItems().get(0).getLotId())).thenReturn(lotDto);
// 2. there is an existing stock card with line items
StockEvent savedEvent = save(stockEventDto, userId);
// when
StockCard savedCard = stockCardRepository.findByOriginEvent(savedEvent);
StockCardDto foundCardDto = stockCardService.findStockCardById(savedCard.getId());
// then
assertThat(foundCardDto.getFacility(), is(cardFacility));
assertThat(foundCardDto.getProgram(), is(programDto));
assertThat(foundCardDto.getOrderable(), is(orderableDto));
assertThat(foundCardDto.getLot(), is(lotDto));
StockCardLineItemDto lineItemDto = foundCardDto.getLineItems().get(0);
assertThat(lineItemDto.getSource(), is(sourceFacility));
assertThat(lineItemDto.getDestination().getName(), is("NGO"));
}
use of org.openlmis.stockmanagement.dto.referencedata.LotDto in project openlmis-stockmanagement by OpenLMIS.
the class StockEventProcessContextBuilder method buildContext.
/**
* Before processing events, put all needed ref data into context so we don't have to do frequent
* network requests.
*
* @param eventDto event dto.
* @return a context object that includes all needed ref data.
*/
public StockEventProcessContext buildContext(StockEventDto eventDto) {
XLOGGER.entry(eventDto);
Profiler profiler = new Profiler("BUILD_CONTEXT");
profiler.setLogger(XLOGGER);
LOGGER.info("build stock event process context");
StockEventProcessContext context = new StockEventProcessContext();
profiler.start("CREATE_LAZY_USER");
OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
Supplier<UUID> userIdSupplier;
if (authentication.isClientOnly()) {
userIdSupplier = eventDto::getUserId;
} else {
userIdSupplier = () -> authenticationHelper.getCurrentUser().getId();
}
LazyResource<UUID> userId = new LazyResource<>(userIdSupplier);
context.setCurrentUserId(userId);
profiler.start("CREATE_LAZY_PROGRAM");
UUID programId = eventDto.getProgramId();
Supplier<ProgramDto> programSupplier = new ReferenceDataSupplier<>(programService, programId);
LazyResource<ProgramDto> program = new LazyResource<>(programSupplier);
context.setProgram(program);
profiler.start("CREATE_LAZY_FACILITY");
UUID facilityId = eventDto.getFacilityId();
Supplier<FacilityDto> facilitySupplier = new ReferenceDataSupplier<>(facilityService, facilityId);
LazyResource<FacilityDto> facility = new LazyResource<>(facilitySupplier);
context.setFacility(facility);
profiler.start("CREATE_LAZY_APPROVED_PRODUCTS");
Supplier<List<OrderableDto>> productsSupplier = () -> orderableReferenceDataService.findAll();
LazyList<OrderableDto> products = new LazyList<>(productsSupplier);
context.setAllApprovedProducts(products);
profiler.start("CREATE_LAZY_LOTS");
Supplier<List<LotDto>> lotsSupplier = () -> getLots(eventDto);
LazyList<LotDto> lots = new LazyList<>(lotsSupplier);
LazyGrouping<UUID, LotDto> lotsGroupedById = new LazyGrouping<>(lots, LotDto::getId);
context.setLots(lotsGroupedById);
profiler.start("CREATE_LAZY_EVENT_REASONS");
Supplier<List<StockCardLineItemReason>> eventReasonsSupplier = () -> reasonRepository.findByIdIn(eventDto.getReasonIds());
LazyList<StockCardLineItemReason> eventReasons = new LazyList<>(eventReasonsSupplier);
LazyGrouping<UUID, StockCardLineItemReason> eventReasonsGroupedById = new LazyGrouping<>(eventReasons, StockCardLineItemReason::getId);
context.setEventReasons(eventReasonsGroupedById);
profiler.start("CREATE_LAZY_NODES");
Supplier<List<Node>> nodesSupplier = () -> nodeRepository.findByIdIn(eventDto.getNodeIds());
LazyList<Node> nodes = new LazyList<>(nodesSupplier);
LazyGrouping<UUID, Node> nodesGroupedById = new LazyGrouping<>(nodes, Node::getId);
context.setNodes(nodesGroupedById);
profiler.start("CREATE_LAZY_STOCK_CARDS");
Supplier<List<StockCard>> cardsSupplier = () -> stockCardRepository.findByProgramIdAndFacilityId(eventDto.getProgramId(), eventDto.getFacilityId());
LazyList<StockCard> cards = new LazyList<>(cardsSupplier);
LazyGrouping<OrderableLotIdentity, StockCard> cardsGroupedByIdentity = new LazyGrouping<>(cards, OrderableLotIdentity::identityOf);
context.setCards(cardsGroupedByIdentity);
profiler.start("CREATE_LAZY_CARD_REASONS");
Supplier<List<StockCardLineItemReason>> cardReasonsSupplier = () -> getCardReasons(eventDto);
LazyList<StockCardLineItemReason> cardReasons = new LazyList<>(cardReasonsSupplier);
LazyGrouping<UUID, StockCardLineItemReason> cardReasonsGroupedById = new LazyGrouping<>(cardReasons, StockCardLineItemReason::getId);
context.setCardReasons(cardReasonsGroupedById);
profiler.start("CREATE_LAZY_SOURCES");
Supplier<List<ValidSourceAssignment>> sourcesSupplier = () -> validSourceAssignmentRepository.findByProgramIdAndFacilityTypeId(eventDto.getProgramId(), context.getFacilityTypeId());
LazyList<ValidSourceAssignment> sources = new LazyList<>(sourcesSupplier);
context.setSources(sources);
profiler.start("CREATE_LAZY_DESTINATIONS");
Supplier<List<ValidDestinationAssignment>> destinationsSupplier = () -> validDestinationAssignmentRepository.findByProgramIdAndFacilityTypeId(eventDto.getProgramId(), context.getFacilityTypeId());
LazyList<ValidDestinationAssignment> destinations = new LazyList<>(destinationsSupplier);
context.setDestinations(destinations);
profiler.stop().log();
XLOGGER.exit(context);
return context;
}
use of org.openlmis.stockmanagement.dto.referencedata.LotDto in project openlmis-stockmanagement by OpenLMIS.
the class LotValidatorTest method shouldFailIfLotDoesNotMatchOrderable.
@Test
public void shouldFailIfLotDoesNotMatchOrderable() throws Exception {
// expect
expectedEx.expectMessage(ERROR_EVENT_LOT_ORDERABLE_NOT_MATCH);
// given
UUID lotId = randomUUID();
stockEventDto.getLineItems().get(0).setLotId(lotId);
LotDto lotDto = new LotDto();
lotDto.setId(lotId);
lotDto.setTradeItemId(UUID.randomUUID());
OrderableDto product = OrderableDto.builder().id(stockEventDto.getLineItems().get(0).getOrderableId()).identifiers(emptyMap()).build();
// when
when(lotReferenceDataService.findOne(lotId)).thenReturn(lotDto);
when(orderableReferenceDataService.findAll()).thenReturn(singletonList(product));
lotValidator.validate(stockEventDto);
}
Aggregations