Search in sources :

Example 1 with ValidSourceAssignment

use of org.openlmis.stockmanagement.domain.sourcedestination.ValidSourceAssignment in project openlmis-stockmanagement by OpenLMIS.

the class SourceDestinationBaseServiceTest method shouldReturnSourceAssignmentWhenSourceIsA_facilityWithoutNode.

@Test
public void shouldReturnSourceAssignmentWhenSourceIsA_facilityWithoutNode() throws Exception {
    // given
    UUID programId = randomUUID();
    UUID facilityTypeId = randomUUID();
    UUID sourceId = randomUUID();
    when(sourceRepository.save(any(ValidSourceAssignment.class))).thenReturn(createSourceAssignment(programId, facilityTypeId, createNode(sourceId, true)));
    FacilityDto facilityDto = new FacilityDto();
    facilityDto.setName(FACILITY_NAME);
    when(facilityReferenceDataService.exists(sourceId)).thenReturn(true);
    when(facilityReferenceDataService.findOne(sourceId)).thenReturn(facilityDto);
    when(nodeRepository.findByReferenceId(sourceId)).thenReturn(null);
    ValidSourceAssignment assignment = createSource(programId, facilityTypeId, sourceId);
    // when
    ValidSourceDestinationDto assignmentDto = validSourceService.assignSource(assignment);
    // then
    assertThat(assignmentDto.getProgramId(), is(programId));
    assertThat(assignmentDto.getFacilityTypeId(), is(facilityTypeId));
    assertThat(assignmentDto.getIsFreeTextAllowed(), is(false));
    assertThat(assignmentDto.getName(), is(FACILITY_NAME));
    assertThat(assignmentDto.getNode().getReferenceId(), is(sourceId));
    assertThat(assignmentDto.getNode().isRefDataFacility(), is(true));
}
Also used : ValidSourceAssignment(org.openlmis.stockmanagement.domain.sourcedestination.ValidSourceAssignment) ValidSourceDestinationDto(org.openlmis.stockmanagement.dto.ValidSourceDestinationDto) FacilityDto(org.openlmis.stockmanagement.dto.referencedata.FacilityDto) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 2 with ValidSourceAssignment

use of org.openlmis.stockmanagement.domain.sourcedestination.ValidSourceAssignment in project openlmis-stockmanagement by OpenLMIS.

the class SourceDestinationBaseServiceTest method shouldReturnSourceAssignmentWhenSourceIsAnOrganization.

@Test
public void shouldReturnSourceAssignmentWhenSourceIsAnOrganization() throws Exception {
    // given
    UUID programId = randomUUID();
    UUID facilityTypeId = randomUUID();
    UUID sourceId = randomUUID();
    when(sourceRepository.save(any(ValidSourceAssignment.class))).thenReturn(createSourceAssignment(programId, facilityTypeId, createNode(sourceId, false)));
    Organization organization = new Organization();
    organization.setName(ORGANIZATION_NAME);
    when(organizationRepository.exists(sourceId)).thenReturn(true);
    when(organizationRepository.findOne(sourceId)).thenReturn(organization);
    when(nodeRepository.findByReferenceId(sourceId)).thenReturn(null);
    ValidSourceAssignment assignment = createSource(programId, facilityTypeId, sourceId);
    // when
    ValidSourceDestinationDto assignmentDto = validSourceService.assignSource(assignment);
    // then
    assertThat(assignmentDto.getProgramId(), is(programId));
    assertThat(assignmentDto.getFacilityTypeId(), is(facilityTypeId));
    assertThat(assignmentDto.getIsFreeTextAllowed(), is(true));
    assertThat(assignmentDto.getName(), is(ORGANIZATION_NAME));
    assertThat(assignmentDto.getNode().getReferenceId(), is(sourceId));
    assertThat(assignmentDto.getNode().isRefDataFacility(), is(false));
}
Also used : ValidSourceAssignment(org.openlmis.stockmanagement.domain.sourcedestination.ValidSourceAssignment) Organization(org.openlmis.stockmanagement.domain.sourcedestination.Organization) ValidSourceDestinationDto(org.openlmis.stockmanagement.dto.ValidSourceDestinationDto) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 3 with ValidSourceAssignment

use of org.openlmis.stockmanagement.domain.sourcedestination.ValidSourceAssignment in project openlmis-stockmanagement by OpenLMIS.

the class SourceDestinationBaseServiceTest method shouldReturnListOfSourceDtosWhenFindValidSourceAssignment.

@Test
public void shouldReturnListOfSourceDtosWhenFindValidSourceAssignment() throws Exception {
    // given
    UUID programId = randomUUID();
    UUID facilityTypeId = randomUUID();
    doNothing().when(programFacilityTypeExistenceService).checkProgramAndFacilityTypeExist(programId, facilityTypeId);
    List<ValidSourceAssignment> validSourceAssignments = asList(createOrganizationSourceAssignment(mockedOrganizationNode("NGO")), createFacilitySourceAssignment(mockedFacilityNode("Health Center")));
    when(sourceRepository.findByProgramIdAndFacilityTypeId(programId, facilityTypeId)).thenReturn(validSourceAssignments);
    // when
    List<ValidSourceDestinationDto> validSources = validSourceService.findSources(programId, facilityTypeId);
    // then
    assertThat(validSources.size(), is(2));
    ValidSourceDestinationDto organization = validSources.get(0);
    assertThat(organization.getName(), is("NGO"));
    assertThat(organization.getIsFreeTextAllowed(), is(true));
    ValidSourceDestinationDto facility = validSources.get(1);
    assertThat(facility.getName(), is("Health Center"));
    assertThat(facility.getIsFreeTextAllowed(), is(false));
}
Also used : ValidSourceAssignment(org.openlmis.stockmanagement.domain.sourcedestination.ValidSourceAssignment) ValidSourceDestinationDto(org.openlmis.stockmanagement.dto.ValidSourceDestinationDto) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 4 with ValidSourceAssignment

use of org.openlmis.stockmanagement.domain.sourcedestination.ValidSourceAssignment 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;
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) LazyList(org.openlmis.stockmanagement.util.LazyList) LazyResource(org.openlmis.stockmanagement.util.LazyResource) Node(org.openlmis.stockmanagement.domain.sourcedestination.Node) FacilityDto(org.openlmis.stockmanagement.dto.referencedata.FacilityDto) ValidSourceAssignment(org.openlmis.stockmanagement.domain.sourcedestination.ValidSourceAssignment) ValidDestinationAssignment(org.openlmis.stockmanagement.domain.sourcedestination.ValidDestinationAssignment) Profiler(org.slf4j.profiler.Profiler) ProgramDto(org.openlmis.stockmanagement.dto.referencedata.ProgramDto) StockCard(org.openlmis.stockmanagement.domain.card.StockCard) LazyList(org.openlmis.stockmanagement.util.LazyList) List(java.util.List) UUID(java.util.UUID) StockEventProcessContext(org.openlmis.stockmanagement.util.StockEventProcessContext) ReferenceDataSupplier(org.openlmis.stockmanagement.util.ReferenceDataSupplier) LotDto(org.openlmis.stockmanagement.dto.referencedata.LotDto) LazyGrouping(org.openlmis.stockmanagement.util.LazyGrouping) StockCardLineItemReason(org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)

Example 5 with ValidSourceAssignment

use of org.openlmis.stockmanagement.domain.sourcedestination.ValidSourceAssignment in project openlmis-stockmanagement by OpenLMIS.

the class ValidSourceAssignmentDataBuilder method createSource.

/**
 * Create a valid source assignment.
 *
 * @param programId      program ID
 * @param facilityTypeId facility type ID
 * @param sourceId       source ID
 * @return valid source assignment
 */
public static ValidSourceAssignment createSource(UUID programId, UUID facilityTypeId, UUID sourceId) {
    Node node = new Node();
    node.setReferenceId(sourceId);
    ValidSourceAssignment assignment = new ValidSourceAssignment();
    assignment.setProgramId(programId);
    assignment.setFacilityTypeId(facilityTypeId);
    assignment.setNode(node);
    return assignment;
}
Also used : ValidSourceAssignment(org.openlmis.stockmanagement.domain.sourcedestination.ValidSourceAssignment) Node(org.openlmis.stockmanagement.domain.sourcedestination.Node)

Aggregations

ValidSourceAssignment (org.openlmis.stockmanagement.domain.sourcedestination.ValidSourceAssignment)11 UUID (java.util.UUID)8 UUID.randomUUID (java.util.UUID.randomUUID)7 Test (org.junit.Test)7 ValidSourceDestinationDto (org.openlmis.stockmanagement.dto.ValidSourceDestinationDto)6 Node (org.openlmis.stockmanagement.domain.sourcedestination.Node)3 FacilityDto (org.openlmis.stockmanagement.dto.referencedata.FacilityDto)3 ResultActions (org.springframework.test.web.servlet.ResultActions)2 List (java.util.List)1 StockCard (org.openlmis.stockmanagement.domain.card.StockCard)1 OrderableLotIdentity (org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)1 StockCardLineItemReason (org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason)1 Organization (org.openlmis.stockmanagement.domain.sourcedestination.Organization)1 ValidDestinationAssignment (org.openlmis.stockmanagement.domain.sourcedestination.ValidDestinationAssignment)1 LotDto (org.openlmis.stockmanagement.dto.referencedata.LotDto)1 OrderableDto (org.openlmis.stockmanagement.dto.referencedata.OrderableDto)1 ProgramDto (org.openlmis.stockmanagement.dto.referencedata.ProgramDto)1 LazyGrouping (org.openlmis.stockmanagement.util.LazyGrouping)1 LazyList (org.openlmis.stockmanagement.util.LazyList)1 LazyResource (org.openlmis.stockmanagement.util.LazyResource)1