Search in sources :

Example 1 with OrderableDto

use of org.openlmis.stockmanagement.dto.referencedata.OrderableDto in project openlmis-stockmanagement by OpenLMIS.

the class StockCardSummariesServiceTest method shouldFindExistingStockCards.

@Test
public void shouldFindExistingStockCards() throws Exception {
    // given
    UUID orderable1Id = randomUUID();
    UUID orderable2Id = randomUUID();
    UUID orderable3Id = randomUUID();
    UUID orderable4Id = randomUUID();
    OrderableDto orderable1 = createOrderableDto(orderable1Id, "");
    OrderableDto orderable2 = createOrderableDto(orderable2Id, "");
    OrderableDto orderable3 = createOrderableDto(orderable3Id, "");
    OrderableDto orderable4 = createOrderableDto(orderable4Id, "");
    UUID programId = randomUUID();
    UUID facilityId = randomUUID();
    when(orderableReferenceDataService.findAll()).thenReturn(asList(orderable1, orderable2, orderable3, orderable4));
    when(cardRepository.findByProgramIdAndFacilityId(programId, facilityId)).thenReturn(asList(createStockCard(orderable1Id, randomUUID()), createStockCard(orderable3Id, randomUUID())));
    when(lotReferenceDataService.getAllLotsOf(any(UUID.class))).thenReturn(emptyList());
    // when
    List<StockCardDto> cardDtos = stockCardSummariesService.findStockCards(programId, facilityId);
    // then
    assertThat(cardDtos.size(), is(2));
    String orderablePropertyName = "orderable";
    String idPropertyName = "id";
    String lineItemsPropertyName = "lineItems";
    String stockOnHandPropertyName = "stockOnHand";
    String lastUpdatePropertyName = "lastUpdate";
    assertThat(cardDtos, hasItem(allOf(hasProperty(orderablePropertyName, is(orderable1)), hasProperty(idPropertyName, notNullValue()), hasProperty(stockOnHandPropertyName, notNullValue()), hasProperty(lastUpdatePropertyName, is(LocalDate.of(2017, 3, 18))), hasProperty(lineItemsPropertyName, nullValue()))));
    assertThat(cardDtos, hasItem(allOf(hasProperty(orderablePropertyName, is(orderable3)), hasProperty(idPropertyName, notNullValue()), hasProperty(stockOnHandPropertyName, notNullValue()), hasProperty(lastUpdatePropertyName, is(LocalDate.of(2017, 3, 18))), hasProperty(lineItemsPropertyName, nullValue()))));
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) StockCardDto(org.openlmis.stockmanagement.dto.StockCardDto) UUID.fromString(java.util.UUID.fromString) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 2 with OrderableDto

use of org.openlmis.stockmanagement.dto.referencedata.OrderableDto 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()))));
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity) StockCardDto(org.openlmis.stockmanagement.dto.StockCardDto) UUID.fromString(java.util.UUID.fromString) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) LotDto(org.openlmis.stockmanagement.dto.referencedata.LotDto) Test(org.junit.Test)

Example 3 with OrderableDto

use of org.openlmis.stockmanagement.dto.referencedata.OrderableDto in project openlmis-stockmanagement by OpenLMIS.

the class VvmValidator method validate.

/**
 * Validates whether the vvm applicables have proper vvm status (if applicable).
 * Throws ValidationMessageException if any of items is in invalid state.
 *  @param vvmApplicables list of items to test
 * @param messageKey error message key for exception
 * @param ignoreMissingOrderable whether should
 */
public void validate(List<? extends VvmApplicable> vvmApplicables, String messageKey, boolean ignoreMissingOrderable) {
    Set<UUID> orderableIds = vvmApplicables.stream().map(VvmApplicable::getOrderableId).collect(Collectors.toSet());
    List<OrderableDto> orderables = orderableReferenceDataService.findByIds(orderableIds);
    Map<UUID, OrderableDto> groupById = orderables.stream().collect(Collectors.toMap(OrderableDto::getId, orderable -> orderable));
    for (VvmApplicable item : vvmApplicables) {
        OrderableDto orderable = groupById.get(item.getOrderableId());
        if (null == orderable) {
            if (ignoreMissingOrderable) {
                continue;
            } else {
                throw new ValidationMessageException(ERROR_EVENT_ORDERABLE_INVALID);
            }
        }
        boolean useVvm = false;
        boolean hasVvmStatus = false;
        if (orderable.getExtraData() != null) {
            useVvm = Boolean.parseBoolean(orderable.getExtraData().get(USE_VVM));
        }
        if (item.getExtraData() != null) {
            hasVvmStatus = item.getExtraData().get(VVM_STATUS) != null;
        }
        if (!useVvm && hasVvmStatus) {
            throw new ValidationMessageException(messageKey);
        }
    }
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) Component(org.springframework.stereotype.Component) List(java.util.List) ERROR_EVENT_ORDERABLE_INVALID(org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_EVENT_ORDERABLE_INVALID) OrderableReferenceDataService(org.openlmis.stockmanagement.service.referencedata.OrderableReferenceDataService) OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) Map(java.util.Map) Autowired(org.springframework.beans.factory.annotation.Autowired) Set(java.util.Set) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) VvmApplicable(org.openlmis.stockmanagement.domain.common.VvmApplicable) VvmApplicable(org.openlmis.stockmanagement.domain.common.VvmApplicable) ValidationMessageException(org.openlmis.stockmanagement.exception.ValidationMessageException) UUID(java.util.UUID)

Example 4 with OrderableDto

use of org.openlmis.stockmanagement.dto.referencedata.OrderableDto 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"));
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) StockEvent(org.openlmis.stockmanagement.domain.event.StockEvent) StockCardLineItemDto(org.openlmis.stockmanagement.dto.StockCardLineItemDto) ProgramDto(org.openlmis.stockmanagement.dto.referencedata.ProgramDto) StockEventDtoDataBuilder.createStockEventDto(org.openlmis.stockmanagement.testutils.StockEventDtoDataBuilder.createStockEventDto) StockEventDto(org.openlmis.stockmanagement.dto.StockEventDto) StockCard(org.openlmis.stockmanagement.domain.card.StockCard) FacilityDto(org.openlmis.stockmanagement.dto.referencedata.FacilityDto) StockCardDto(org.openlmis.stockmanagement.dto.StockCardDto) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) LotDto(org.openlmis.stockmanagement.dto.referencedata.LotDto) BaseIntegrationTest(org.openlmis.stockmanagement.BaseIntegrationTest) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with OrderableDto

use of org.openlmis.stockmanagement.dto.referencedata.OrderableDto in project openlmis-stockmanagement by OpenLMIS.

the class StockCardSummariesService method createOrderableLots.

private Map<OrderableLotIdentity, OrderableLot> createOrderableLots(List<OrderableDto> orderableDtos) {
    Stream<OrderableLot> orderableLots = orderableDtos.stream().flatMap(this::lotsOfOrderable);
    Stream<OrderableLot> orderablesOnly = orderableDtos.stream().map(orderableDto -> new OrderableLot(orderableDto, null));
    return concat(orderableLots, orderablesOnly).collect(toMap(OrderableLotIdentity::identityOf, orderableLot -> orderableLot));
}
Also used : Stream.empty(java.util.stream.Stream.empty) StockCard(org.openlmis.stockmanagement.domain.card.StockCard) OrderableReferenceDataService(org.openlmis.stockmanagement.service.referencedata.OrderableReferenceDataService) Getter(lombok.Getter) OrderableFulfillReferenceDataService(org.openlmis.stockmanagement.service.referencedata.OrderableFulfillReferenceDataService) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ApprovedProductReferenceDataService(org.openlmis.stockmanagement.service.referencedata.ApprovedProductReferenceDataService) OrderableLotIdentity.identityOf(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity.identityOf) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity) Collectors.toMap(java.util.stream.Collectors.toMap) StockCardDto(org.openlmis.stockmanagement.dto.StockCardDto) OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) LotReferenceDataService(org.openlmis.stockmanagement.service.referencedata.LotReferenceDataService) Service(org.springframework.stereotype.Service) Map(java.util.Map) Stream.concat(java.util.stream.Stream.concat) Pageable(org.springframework.data.domain.Pageable) StockCardRepository(org.openlmis.stockmanagement.repository.StockCardRepository) OrderableFulfillDto(org.openlmis.stockmanagement.dto.referencedata.OrderableFulfillDto) Logger(org.slf4j.Logger) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) LotDto(org.openlmis.stockmanagement.dto.referencedata.LotDto) UUID(java.util.UUID) Page(org.springframework.data.domain.Page) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) IdentifiableByOrderableLot(org.openlmis.stockmanagement.domain.identity.IdentifiableByOrderableLot) PageImpl(org.springframework.data.domain.PageImpl) IdentifiableByOrderableLot(org.openlmis.stockmanagement.domain.identity.IdentifiableByOrderableLot)

Aggregations

OrderableDto (org.openlmis.stockmanagement.dto.referencedata.OrderableDto)23 UUID (java.util.UUID)16 Test (org.junit.Test)16 UUID.randomUUID (java.util.UUID.randomUUID)8 LotDto (org.openlmis.stockmanagement.dto.referencedata.LotDto)6 StockCard (org.openlmis.stockmanagement.domain.card.StockCard)5 VvmApplicable (org.openlmis.stockmanagement.domain.common.VvmApplicable)5 StockCardDto (org.openlmis.stockmanagement.dto.StockCardDto)5 URI (java.net.URI)4 StockEventProcessContext (org.openlmis.stockmanagement.util.StockEventProcessContext)4 List (java.util.List)3 OrderableLotIdentity (org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)3 StockEventDto (org.openlmis.stockmanagement.dto.StockEventDto)3 ApprovedProductDto (org.openlmis.stockmanagement.dto.referencedata.ApprovedProductDto)3 FacilityDto (org.openlmis.stockmanagement.dto.referencedata.FacilityDto)3 ProgramDto (org.openlmis.stockmanagement.dto.referencedata.ProgramDto)3 DynamicPageTypeReference (org.openlmis.stockmanagement.util.DynamicPageTypeReference)3 Map (java.util.Map)2 UUID.fromString (java.util.UUID.fromString)2 StockEvent (org.openlmis.stockmanagement.domain.event.StockEvent)2