From 8b82790784a31999e4f13f2f794aac43e6c7d3e2 Mon Sep 17 00:00:00 2001 From: Szymon Radziszewski <117299684+sradziszewski@users.noreply.github.com> Date: Wed, 26 Jun 2024 18:22:15 +0200 Subject: [PATCH] OAM-218: Updated programId param to repeatable (#60) * OAM-218: WIP * OAM-218: Updated programId param to repeatable * OAM-218: Removed unused import, provided constants * OAM-218: Removed unnecessary stubbings * OAM-218: Added extra tests * OAM-218: Provided changes after review * OAM-218: Removed 'Ignore' annotation * OAM-218: Added extra condition to test * OAM-218: Fixed params/method names * OAM-218: Added tests for coverage. --------- Co-authored-by: Piotr Wargulak Co-authored-by: tsznaj --- ...onAssignmentRepositoryIntegrationTest.java | 4 +- ...onAssignmentControllerIntegrationTest.java | 2 +- ...eDestinationControllerIntegrationTest.java | 9 ++-- ...dSummariesV2ControllerIntegrationTest.java | 14 +++--- ...SourceDestinationAssignmentRepository.java | 18 +++++-- .../repository/StockCardRepository.java | 23 ++++++--- ...ValidReasonAssignmentRepositoryCustom.java | 2 +- .../ValidReasonAssignmentRepositoryImpl.java | 8 +-- .../service/CalculatedStockOnHandService.java | 12 ++--- .../service/SourceDestinationBaseService.java | 18 ++++--- .../service/StockCardSummariesService.java | 8 +-- .../StockCardSummariesV2SearchParams.java | 6 +-- .../StockEventProcessContextBuilder.java | 12 ++--- .../service/ValidDestinationService.java | 7 +-- .../service/ValidSourceService.java | 7 +-- .../ApprovedProductReferenceDataService.java | 10 ++-- ...eDestinationGeoLevelAffinityValidator.java | 11 ++-- .../web/ValidReasonAssignmentController.java | 2 +- .../ValidReasonAssignmentSearchParams.java | 22 +++++--- .../web/ValidSourceDestinationController.java | 9 ++-- .../ValidSourceDestinationSearchParams.java | 21 ++++++-- src/main/resources/api-definition.yaml | 7 ++- .../SourceDestinationBaseServiceTest.java | 50 ++++++++++--------- .../StockCardSummariesServiceTest.java | 10 ++-- .../StockCardSummariesV2SearchParamsTest.java | 4 +- ...ardSummariesV2SearchParamsDataBuilder.java | 8 +-- ...rceDestinationAssignmentValidatorTest.java | 2 +- ...tinationGeoLevelAffinityValidatorTest.java | 13 ++--- ...ValidReasonAssignmentSearchParamsTest.java | 19 +++++-- ...alidSourceDestinationSearchParamsTest.java | 17 ++++++- 30 files changed, 220 insertions(+), 135 deletions(-) diff --git a/src/integration-test/java/org/openlmis/stockmanagement/repository/ValidReasonAssignmentRepositoryIntegrationTest.java b/src/integration-test/java/org/openlmis/stockmanagement/repository/ValidReasonAssignmentRepositoryIntegrationTest.java index 34f43de5..76bd5f50 100644 --- a/src/integration-test/java/org/openlmis/stockmanagement/repository/ValidReasonAssignmentRepositoryIntegrationTest.java +++ b/src/integration-test/java/org/openlmis/stockmanagement/repository/ValidReasonAssignmentRepositoryIntegrationTest.java @@ -20,6 +20,8 @@ import static org.junit.Assert.assertThat; import com.google.common.collect.Sets; + +import java.util.Collections; import java.util.List; import java.util.UUID; import javax.persistence.EntityManager; @@ -93,7 +95,7 @@ public void shouldReturnValidReasonWithProgramAndFacilityTypeAndReasonAndReasonT repository.save(newAssignment); List validReasonAssignments = repository.search( - PROGRAM_ID, FACILITY_TYPE_ID, Sets.newHashSet( + Collections.singleton(PROGRAM_ID), FACILITY_TYPE_ID, Sets.newHashSet( validReasonAssignment.getReason().getReasonType(), stockCardLineItemReason.getReasonType()), stockCardLineItemReason.getId()); diff --git a/src/integration-test/java/org/openlmis/stockmanagement/web/ValidReasonAssignmentControllerIntegrationTest.java b/src/integration-test/java/org/openlmis/stockmanagement/web/ValidReasonAssignmentControllerIntegrationTest.java index c09ffdc2..04af7ab9 100644 --- a/src/integration-test/java/org/openlmis/stockmanagement/web/ValidReasonAssignmentControllerIntegrationTest.java +++ b/src/integration-test/java/org/openlmis/stockmanagement/web/ValidReasonAssignmentControllerIntegrationTest.java @@ -100,7 +100,7 @@ public void getValidReasonAssignments() { @Test public void getValidReasonAssignmentsByAllParameters() { - when(reasonAssignmentRepository.search(programId, facilityTypeId, + when(reasonAssignmentRepository.search(Collections.singleton(programId), facilityTypeId, Sets.newHashSet(ReasonType.CREDIT, ReasonType.DEBIT), reasonId)).thenReturn( Collections.singletonList(reasonAssignment)); diff --git a/src/integration-test/java/org/openlmis/stockmanagement/web/ValidSourceDestinationControllerIntegrationTest.java b/src/integration-test/java/org/openlmis/stockmanagement/web/ValidSourceDestinationControllerIntegrationTest.java index b7ed0825..702b97d7 100644 --- a/src/integration-test/java/org/openlmis/stockmanagement/web/ValidSourceDestinationControllerIntegrationTest.java +++ b/src/integration-test/java/org/openlmis/stockmanagement/web/ValidSourceDestinationControllerIntegrationTest.java @@ -15,6 +15,7 @@ package org.openlmis.stockmanagement.web; +import static java.util.Collections.singleton; import static java.util.Collections.singletonList; import static java.util.UUID.randomUUID; import static org.hamcrest.Matchers.hasSize; @@ -77,11 +78,11 @@ public void shouldGetValidSourcesOrDestinationsByProgramAndFacility() UUID program = randomUUID(); UUID facility = randomUUID(); - when(validSourceService.findSources(program, facility, false, pageRequest)) + when(validSourceService.findSources(singleton(program), facility, false, pageRequest)) .thenReturn(Pagination.getPage(singletonList(sourceDestination))); - when(validDestinationService.findDestinations(program, facility, false, pageRequest)) - .thenReturn(Pagination.getPage(singletonList(sourceDestination))); + when(validDestinationService.findDestinations(singleton(program), facility, + false, pageRequest)).thenReturn(Pagination.getPage(singletonList(sourceDestination))); verifyZeroInteractions(permissionService); @@ -274,4 +275,4 @@ private void performSourcesOrDestinations( .andExpect(jsonPath("$.content[0].name", is(sourceDestinationDto.getName()))) .andExpect(jsonPath("$.content[0].isFreeTextAllowed", is(true))); } -} \ No newline at end of file +} diff --git a/src/integration-test/java/org/openlmis/stockmanagement/web/stockcardsummariesv2/StockCardSummariesV2ControllerIntegrationTest.java b/src/integration-test/java/org/openlmis/stockmanagement/web/stockcardsummariesv2/StockCardSummariesV2ControllerIntegrationTest.java index eab4bda1..6fb58b76 100644 --- a/src/integration-test/java/org/openlmis/stockmanagement/web/stockcardsummariesv2/StockCardSummariesV2ControllerIntegrationTest.java +++ b/src/integration-test/java/org/openlmis/stockmanagement/web/stockcardsummariesv2/StockCardSummariesV2ControllerIntegrationTest.java @@ -96,7 +96,7 @@ public void shouldGetStockCardSummariesByAllParameters() throws Exception { .param(ACCESS_TOKEN, ACCESS_TOKEN_VALUE) .param(PAGE, String.valueOf(pageable.getPageNumber())) .param(SIZE, String.valueOf(pageable.getPageSize())) - .param(PROGRAM_ID, params.getProgramId().toString()) + .param(PROGRAM_ID, params.getProgramIds().get(0).toString()) .param(FACILITY_ID, params.getFacilityId().toString()) .param(AS_OF_DATE, params.getAsOfDate().toString()) .param(ORDERABLE_ID, params.getOrderableIds().get(0).toString()) @@ -132,7 +132,7 @@ public void shouldSetIntegerMaxValueAsDefaultPageSize() throws Exception { ResultActions resultActions = mvc.perform( get(API_STOCK_CARD_SUMMARIES) .param(ACCESS_TOKEN, ACCESS_TOKEN_VALUE) - .param(PROGRAM_ID, params.getProgramId().toString()) + .param(PROGRAM_ID, params.getProgramIds().get(0).toString()) .param(FACILITY_ID, params.getFacilityId().toString()) .param(AS_OF_DATE, params.getAsOfDate().toString()) .param(ORDERABLE_ID, params.getOrderableIds().get(0).toString()) @@ -157,7 +157,7 @@ public void shouldReturnBadRequestIfFacilityIdIsNotPresent() throws Exception { .param(ACCESS_TOKEN, ACCESS_TOKEN_VALUE) .param(PAGE, String.valueOf(pageable.getPageNumber())) .param(SIZE, String.valueOf(pageable.getPageSize())) - .param(PROGRAM_ID, params.getProgramId().toString()) + .param(PROGRAM_ID, params.getProgramIds().toString()) .param(AS_OF_DATE, params.getAsOfDate().toString()) .param(ORDERABLE_ID, params.getOrderableIds().get(0).toString()) .param(ORDERABLE_ID, params.getOrderableIds().get(1).toString())); @@ -193,7 +193,7 @@ public void shouldReturnForbiddenIfNoPermission() throws Exception { .param(ACCESS_TOKEN, ACCESS_TOKEN_VALUE) .param(PAGE, String.valueOf(pageable.getPageNumber())) .param(SIZE, String.valueOf(pageable.getPageSize())) - .param(PROGRAM_ID, params.getProgramId().toString()) + .param(PROGRAM_ID, params.getProgramIds().get(0).toString()) .param(FACILITY_ID, params.getFacilityId().toString())); resultActions.andExpect(status().isForbidden()); @@ -219,7 +219,7 @@ public void shouldReturnNonEmptySummariesIfFlagIsSet() throws Exception { .param(ACCESS_TOKEN, ACCESS_TOKEN_VALUE) .param(PAGE, String.valueOf(pageable.getPageNumber())) .param(SIZE, String.valueOf(pageable.getPageSize())) - .param(PROGRAM_ID, params.getProgramId().toString()) + .param(PROGRAM_ID, params.getProgramIds().get(0).toString()) .param(FACILITY_ID, params.getFacilityId().toString()) .param(AS_OF_DATE, params.getAsOfDate().toString()) .param(ORDERABLE_ID, params.getOrderableIds().get(0).toString()) @@ -256,7 +256,7 @@ public void shouldRespectSendNonEmptyCardsFlagInSubsequentRequests() throws Exce .param(ACCESS_TOKEN, ACCESS_TOKEN_VALUE) .param(PAGE, String.valueOf(pageable.getPageNumber())) .param(SIZE, String.valueOf(pageable.getPageSize())) - .param(PROGRAM_ID, params.getProgramId().toString()) + .param(PROGRAM_ID, params.getProgramIds().get(0).toString()) .param(FACILITY_ID, params.getFacilityId().toString()) .param(AS_OF_DATE, params.getAsOfDate().toString()) .param(ORDERABLE_ID, params.getOrderableIds().get(0).toString()) @@ -286,7 +286,7 @@ public void shouldRespectSendNonEmptyCardsFlagInSubsequentRequests() throws Exce .param(ACCESS_TOKEN, ACCESS_TOKEN_VALUE) .param(PAGE, String.valueOf(pageable.getPageNumber())) .param(SIZE, String.valueOf(pageable.getPageSize())) - .param(PROGRAM_ID, params.getProgramId().toString()) + .param(PROGRAM_ID, params.getProgramIds().get(0).toString()) .param(FACILITY_ID, params.getFacilityId().toString()) .param(AS_OF_DATE, params.getAsOfDate().toString()) .param(ORDERABLE_ID, params.getOrderableIds().get(0).toString()) diff --git a/src/main/java/org/openlmis/stockmanagement/repository/SourceDestinationAssignmentRepository.java b/src/main/java/org/openlmis/stockmanagement/repository/SourceDestinationAssignmentRepository.java index c13a2465..3af927ad 100644 --- a/src/main/java/org/openlmis/stockmanagement/repository/SourceDestinationAssignmentRepository.java +++ b/src/main/java/org/openlmis/stockmanagement/repository/SourceDestinationAssignmentRepository.java @@ -16,22 +16,30 @@ package org.openlmis.stockmanagement.repository; import java.util.List; +import java.util.Set; import java.util.UUID; import org.openlmis.stockmanagement.domain.sourcedestination.SourceDestinationAssignment; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.repository.NoRepositoryBean; -import org.springframework.data.repository.query.Param; @NoRepositoryBean public interface SourceDestinationAssignmentRepository extends JpaRepository { + List findByProgramIdInAndFacilityTypeId( + Set programIds, UUID facilityTypeId, + Pageable pageable); + List findByProgramIdAndFacilityTypeId( - @Param("programId") UUID programId, @Param("facilityTypeId") UUID facilityTypeId, - Pageable pageable); + UUID programId, UUID facilityTypeId, + Pageable pageable); + + T findByProgramIdInAndFacilityTypeIdAndNodeId( + Set programIds, UUID facilityTypeId, + UUID nodeId); T findByProgramIdAndFacilityTypeIdAndNodeId( - @Param("programId") UUID programId, @Param("facilityTypeId") UUID facilityTypeId, - @Param("nodeId") UUID nodeId); + UUID programId, UUID facilityTypeId, + UUID nodeId); } diff --git a/src/main/java/org/openlmis/stockmanagement/repository/StockCardRepository.java b/src/main/java/org/openlmis/stockmanagement/repository/StockCardRepository.java index c1e38064..253bc6d4 100644 --- a/src/main/java/org/openlmis/stockmanagement/repository/StockCardRepository.java +++ b/src/main/java/org/openlmis/stockmanagement/repository/StockCardRepository.java @@ -29,23 +29,30 @@ public interface StockCardRepository extends JpaRepository { + String PROGRAM_ID = "programId"; + String FACILITY_ID = "facilityId"; + StockCard findByProgramIdAndFacilityIdAndOrderableIdAndLotId( - @Param("programId") UUID programId, - @Param("facilityId") UUID facilityId, + @Param(PROGRAM_ID) UUID programId, + @Param(FACILITY_ID) UUID facilityId, @Param("orderableId") UUID orderableId, @Param("lotId") UUID lotId); Page findByProgramIdAndFacilityId( - @Param("programId") UUID programId, - @Param("facilityId") UUID facilityId, + @Param(PROGRAM_ID) UUID programId, + @Param(FACILITY_ID) UUID facilityId, Pageable pageable); List findByProgramIdAndFacilityId( - @Param("programId") UUID programId, - @Param("facilityId") UUID facilityId); + @Param(PROGRAM_ID) UUID programId, + @Param(FACILITY_ID) UUID facilityId); + + List findByProgramIdInAndFacilityId( + @Param(PROGRAM_ID) Collection programId, + @Param(FACILITY_ID) UUID facilityId); - List findByOrderableIdInAndProgramIdAndFacilityId( - Collection orderableIds, UUID programId, UUID facilityId); + List findByOrderableIdInAndProgramIdInAndFacilityId( + Collection orderableIds, Collection programIds, UUID facilityId); StockCard findByOriginEvent(@Param("originEventId") StockEvent stockEvent); diff --git a/src/main/java/org/openlmis/stockmanagement/repository/custom/ValidReasonAssignmentRepositoryCustom.java b/src/main/java/org/openlmis/stockmanagement/repository/custom/ValidReasonAssignmentRepositoryCustom.java index 5dc2ac92..f96ccbbb 100644 --- a/src/main/java/org/openlmis/stockmanagement/repository/custom/ValidReasonAssignmentRepositoryCustom.java +++ b/src/main/java/org/openlmis/stockmanagement/repository/custom/ValidReasonAssignmentRepositoryCustom.java @@ -23,7 +23,7 @@ public interface ValidReasonAssignmentRepositoryCustom { - List search(UUID programId, UUID facilityTypeId, + List search(Collection programIds, UUID facilityTypeId, Collection reasonTypes, UUID reasonId); } diff --git a/src/main/java/org/openlmis/stockmanagement/repository/custom/impl/ValidReasonAssignmentRepositoryImpl.java b/src/main/java/org/openlmis/stockmanagement/repository/custom/impl/ValidReasonAssignmentRepositoryImpl.java index cc01a8ce..135d2436 100644 --- a/src/main/java/org/openlmis/stockmanagement/repository/custom/impl/ValidReasonAssignmentRepositoryImpl.java +++ b/src/main/java/org/openlmis/stockmanagement/repository/custom/impl/ValidReasonAssignmentRepositoryImpl.java @@ -45,13 +45,13 @@ public class ValidReasonAssignmentRepositoryImpl implements ValidReasonAssignmen /** * This method is supposed to retrieve all Valid Reason Assignments with matched parameters. * - * @param programId Valid Reason Assignment program id + * @param programIds Valid Reason Assignment program ids * @param facilityTypeId Valid Reason Assignment facility type id * @param reasonTypes Valid Reason Assignment stock card line item reason types * @param reasonId Valid Reason Assignment stock card line item reason id * @return List of Valid Reason Assignments matching the parameters. */ - public List search(UUID programId, UUID facilityTypeId, + public List search(Collection programIds, UUID facilityTypeId, Collection reasonTypes, UUID reasonId) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); @@ -61,8 +61,8 @@ public List search(UUID programId, UUID facilityTypeId, Predicate predicate = builder.conjunction(); - if (null != programId) { - predicate = builder.and(predicate, builder.equal(root.get(PROGRAM_ID), programId)); + if (null != programIds) { + predicate = builder.and(predicate, root.get(PROGRAM_ID).in(programIds)); } if (null != facilityTypeId) { diff --git a/src/main/java/org/openlmis/stockmanagement/service/CalculatedStockOnHandService.java b/src/main/java/org/openlmis/stockmanagement/service/CalculatedStockOnHandService.java index 6bd738f6..f66d506f 100644 --- a/src/main/java/org/openlmis/stockmanagement/service/CalculatedStockOnHandService.java +++ b/src/main/java/org/openlmis/stockmanagement/service/CalculatedStockOnHandService.java @@ -60,7 +60,7 @@ public class CalculatedStockOnHandService { /** * Returns list of stock cards with fetched Stock on Hand values. * - * @param programId program id to find stock cards + * @param programIds program ids to find stock cards * @param facilityId facility id to find stock cards * @param asOfDate date used to get latest stock on hand before or equal specific date * @param orderableIds orderable ids to find stock card @@ -68,13 +68,13 @@ public class CalculatedStockOnHandService { * @return List of stock cards with SOH values, empty list if no stock cards were found. */ public List getStockCardsWithStockOnHand( - UUID programId, UUID facilityId, LocalDate asOfDate, List orderableIds, + List programIds, UUID facilityId, LocalDate asOfDate, List orderableIds, Set lotCodeIds) { List stockCards = orderableIds.isEmpty() - ? stockCardRepository.findByProgramIdAndFacilityId(programId, facilityId) - : stockCardRepository.findByOrderableIdInAndProgramIdAndFacilityId( - orderableIds, programId, facilityId); + ? stockCardRepository.findByProgramIdInAndFacilityId(programIds, facilityId) + : stockCardRepository.findByOrderableIdInAndProgramIdInAndFacilityId( + orderableIds, programIds, facilityId); stockCards.forEach(stockCard -> fetchStockOnHand(stockCard, asOfDate != null ? asOfDate : LocalDate.now())); @@ -122,7 +122,7 @@ public List getStockCardsWithStockOnHand( public List getStockCardsWithStockOnHand( UUID programId, UUID facilityId, LocalDate asOfDate, List orderableIds) { - return getStockCardsWithStockOnHand(programId, facilityId, + return getStockCardsWithStockOnHand(Collections.singletonList(programId), facilityId, asOfDate, orderableIds, Collections.emptySet()); } diff --git a/src/main/java/org/openlmis/stockmanagement/service/SourceDestinationBaseService.java b/src/main/java/org/openlmis/stockmanagement/service/SourceDestinationBaseService.java index 5a9e0056..1af43602 100644 --- a/src/main/java/org/openlmis/stockmanagement/service/SourceDestinationBaseService.java +++ b/src/main/java/org/openlmis/stockmanagement/service/SourceDestinationBaseService.java @@ -24,6 +24,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; import org.openlmis.stockmanagement.domain.sourcedestination.Node; @@ -129,20 +130,20 @@ protected ValidSourceDestinationDto find * This method will return only those assignments that match the geo level affinity * or all possible assignments (when filtering params are not provided). * - * @param programId program id + * @param programIds program ids * @param facilityId facility id * @param repository assignment repository * @param assignment type * @return a list of assignment dto or empty list if not found. */ protected Page findAssignments( - UUID programId, UUID facilityId, boolean includeDisabled, + Set programIds, UUID facilityId, boolean includeDisabled, SourceDestinationAssignmentRepository repository, Profiler profiler, Pageable pageable) { profiler.start("FIND_ASSIGNMENTS"); - if (programId != null && facilityId != null) { - return findFilteredAssignments(programId, facilityId, includeDisabled, + if (programIds != null && facilityId != null) { + return findFilteredAssignments(programIds, facilityId, includeDisabled, repository, profiler, pageable); } else if (includeDisabled) { return findAllAssignments(repository, profiler, pageable); @@ -296,7 +297,7 @@ private List createAssignmentDto( } private Page - findFilteredAssignments(UUID programId, UUID facilityId, boolean includeDisabled, + findFilteredAssignments(Set programIds, UUID facilityId, boolean includeDisabled, SourceDestinationAssignmentRepository repository, Profiler profiler, Pageable pageable) { profiler.start("FIND_FACILITY_BY_ID"); FacilityDto facility = facilityRefDataService.findOne(facilityId); @@ -308,11 +309,14 @@ private List createAssignmentDto( profiler.start("CHECK_PROGRAM_AND_FACILITY_TYPE_EXIST"); UUID facilityTypeId = facility.getType().getId(); - programFacilityTypeExistenceService.checkProgramAndFacilityTypeExist(programId, facilityTypeId); + for (UUID programId : programIds) { + programFacilityTypeExistenceService + .checkProgramAndFacilityTypeExist(programId, facilityTypeId); + } profiler.start("FIND_ASSIGNMENTS_BY_PROGRAM_AND_FACILITY_TYPE"); List assignments = repository - .findByProgramIdAndFacilityTypeId(programId, facilityTypeId, Pageable.unpaged()); + .findByProgramIdInAndFacilityTypeId(programIds, facilityTypeId, Pageable.unpaged()); profiler.start("FIND_FACILITY_IDS"); List facilitiesIds = assignments.stream() diff --git a/src/main/java/org/openlmis/stockmanagement/service/StockCardSummariesService.java b/src/main/java/org/openlmis/stockmanagement/service/StockCardSummariesService.java index 8abdff3e..20ba7451 100644 --- a/src/main/java/org/openlmis/stockmanagement/service/StockCardSummariesService.java +++ b/src/main/java/org/openlmis/stockmanagement/service/StockCardSummariesService.java @@ -148,12 +148,14 @@ public StockCardSummaries findStockCards(StockCardSummariesV2SearchParams params if (!homeFacilityPermissionService .checkFacilityAndHomeFacilityLinkage(params.getFacilityId())) { profiler.start("VALIDATE_VIEW_RIGHTS"); - permissionService.canViewStockCard(params.getProgramId(), params.getFacilityId()); + for (UUID id : params.getProgramIds()) { + permissionService.canViewStockCard(id, params.getFacilityId()); + } } profiler.start("GET_APPROVED_PRODUCTS"); OrderablesAggregator approvedProducts = approvedProductReferenceDataService - .getApprovedProducts(params.getFacilityId(), params.getProgramId(), + .getApprovedProducts(params.getFacilityId(), params.getProgramIds(), params.getOrderableIds(), params.getOrderableCode(), params.getOrderableName() ); @@ -194,7 +196,7 @@ public StockCardSummaries findStockCards(StockCardSummariesV2SearchParams params // FIXME: Fix page retrieving/calculation, // page size may be wrong when there are orderables matching not only by lot codes List stockCards = calculatedStockOnHandService.getStockCardsWithStockOnHand( - params.getProgramId(), params.getFacilityId(), params.getAsOfDate(), + params.getProgramIds(), params.getFacilityId(), params.getAsOfDate(), orderableIdsForStockCard, lotCodeIds); Page orderablesPage = approvedProducts.getOrderablesPage(); diff --git a/src/main/java/org/openlmis/stockmanagement/service/StockCardSummariesV2SearchParams.java b/src/main/java/org/openlmis/stockmanagement/service/StockCardSummariesV2SearchParams.java index 05faaecf..a9a3366a 100644 --- a/src/main/java/org/openlmis/stockmanagement/service/StockCardSummariesV2SearchParams.java +++ b/src/main/java/org/openlmis/stockmanagement/service/StockCardSummariesV2SearchParams.java @@ -56,7 +56,7 @@ public final class StockCardSummariesV2SearchParams { static final String ORDERABLE_NAME = "orderableName"; static final String LOT_CODE = "lotCode"; - private UUID programId; + private List programIds; private UUID facilityId; private List orderableIds; private LocalDate asOfDate; @@ -71,14 +71,14 @@ public final class StockCardSummariesV2SearchParams { */ public StockCardSummariesV2SearchParams(MultiValueMap parameters) { if (!MapUtils.isEmpty(parameters)) { - this.programId = getId(PROGRAM_ID, parameters); + this.programIds = getIds(PROGRAM_ID, parameters); this.facilityId = getId(FACILITY_ID, parameters); if (null == facilityId) { throw new ValidationMessageException(ERROR_FACILITY_ID_MISSING); } - if (null == programId) { + if (programIds.isEmpty()) { throw new ValidationMessageException(ERROR_PROGRAM_ID_MISSING); } diff --git a/src/main/java/org/openlmis/stockmanagement/service/StockEventProcessContextBuilder.java b/src/main/java/org/openlmis/stockmanagement/service/StockEventProcessContextBuilder.java index 4373014a..4308fd83 100644 --- a/src/main/java/org/openlmis/stockmanagement/service/StockEventProcessContextBuilder.java +++ b/src/main/java/org/openlmis/stockmanagement/service/StockEventProcessContextBuilder.java @@ -214,16 +214,16 @@ public StockEventProcessContext buildContext(StockEventDto eventDto) { profiler.start("CREATE_LAZY_SOURCES"); Supplier> sourcesSupplier = () -> validSourceAssignmentRepository - .findByProgramIdAndFacilityTypeId( - eventDto.getProgramId(), context.getFacilityTypeId(), Pageable.unpaged()); + .findByProgramIdAndFacilityTypeId(eventDto.getProgramId(), + context.getFacilityTypeId(), Pageable.unpaged()); LazyList sources = new LazyList<>(sourcesSupplier); context.setSources(sources); profiler.start("CREATE_LAZY_DESTINATIONS"); - Supplier> destinationsSupplier = () -> - validDestinationAssignmentRepository - .findByProgramIdAndFacilityTypeId( - eventDto.getProgramId(), context.getFacilityTypeId(), Pageable.unpaged()); + Supplier> destinationsSupplier = + () -> validDestinationAssignmentRepository + .findByProgramIdAndFacilityTypeId(eventDto.getProgramId(), + context.getFacilityTypeId(), Pageable.unpaged()); LazyList destinations = new LazyList<>(destinationsSupplier); context.setDestinations(destinations); diff --git a/src/main/java/org/openlmis/stockmanagement/service/ValidDestinationService.java b/src/main/java/org/openlmis/stockmanagement/service/ValidDestinationService.java index 00618de4..2a2c1b44 100644 --- a/src/main/java/org/openlmis/stockmanagement/service/ValidDestinationService.java +++ b/src/main/java/org/openlmis/stockmanagement/service/ValidDestinationService.java @@ -19,6 +19,7 @@ import static org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_DESTINATION_NOT_FOUND; import static org.slf4j.ext.XLoggerFactory.getXLogger; +import java.util.Set; import java.util.UUID; import org.openlmis.stockmanagement.domain.sourcedestination.ValidDestinationAssignment; import org.openlmis.stockmanagement.dto.ValidSourceDestinationDto; @@ -42,12 +43,12 @@ public class ValidDestinationService extends SourceDestinationBaseService { /** * Find valid sources page by program ID and facility type ID. * - * @param programId program ID + * @param programIds program IDs * @param facilityId facility ID * @param pageable pagination and sorting parameters * @return valid source assignment DTOs */ - public Page findDestinations(UUID programId, + public Page findDestinations(Set programIds, UUID facilityId, boolean includeDisabled, Pageable pageable) { @@ -56,7 +57,7 @@ public Page findDestinations(UUID programId, profiler.setLogger(XLOGGER); Page assignments = - findAssignments(programId, facilityId, includeDisabled, + findAssignments(programIds, facilityId, includeDisabled, validDestinationRepository, profiler, pageable); profiler.stop().log(); XLOGGER.exit(); diff --git a/src/main/java/org/openlmis/stockmanagement/service/ValidSourceService.java b/src/main/java/org/openlmis/stockmanagement/service/ValidSourceService.java index 42848fae..8da4e3a4 100644 --- a/src/main/java/org/openlmis/stockmanagement/service/ValidSourceService.java +++ b/src/main/java/org/openlmis/stockmanagement/service/ValidSourceService.java @@ -19,6 +19,7 @@ import static org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_SOURCE_NOT_FOUND; import static org.slf4j.ext.XLoggerFactory.getXLogger; +import java.util.Set; import java.util.UUID; import org.openlmis.stockmanagement.domain.sourcedestination.ValidSourceAssignment; import org.openlmis.stockmanagement.dto.ValidSourceDestinationDto; @@ -42,12 +43,12 @@ public class ValidSourceService extends SourceDestinationBaseService { /** * Find valid destinations page by program ID and facility ID. * - * @param programId program ID + * @param programIds program IDs * @param facilityId facility ID * @param pageable pagination and sorting parameters * @return page of valid destination assignment DTOs */ - public Page findSources(UUID programId, + public Page findSources(Set programIds, UUID facilityId, boolean includeDisabled, Pageable pageable) { @@ -56,7 +57,7 @@ public Page findSources(UUID programId, profiler.setLogger(XLOGGER); Page sourceAssignments = - findAssignments(programId, facilityId, includeDisabled, + findAssignments(programIds, facilityId, includeDisabled, validSourceRepository, profiler, pageable); profiler.stop().log(); XLOGGER.exit(); diff --git a/src/main/java/org/openlmis/stockmanagement/service/referencedata/ApprovedProductReferenceDataService.java b/src/main/java/org/openlmis/stockmanagement/service/referencedata/ApprovedProductReferenceDataService.java index 7b4bc4d8..6d1c2b98 100644 --- a/src/main/java/org/openlmis/stockmanagement/service/referencedata/ApprovedProductReferenceDataService.java +++ b/src/main/java/org/openlmis/stockmanagement/service/referencedata/ApprovedProductReferenceDataService.java @@ -19,6 +19,7 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.UUID; import org.openlmis.stockmanagement.dto.referencedata.ApprovedProductDto; @@ -56,7 +57,8 @@ protected Class getArrayResultClass() { */ public OrderablesAggregator getApprovedProducts(UUID facilityId, UUID programId, Collection orderableIds) { - return this.getApprovedProducts(facilityId, programId, orderableIds, null, null); + return this.getApprovedProducts(facilityId, Collections.singleton(programId), orderableIds, + null, null); } /** @@ -65,7 +67,7 @@ public OrderablesAggregator getApprovedProducts(UUID facilityId, UUID programId, * The result is wrapped to a separate class to improve the performance * * @param facilityId id of the facility - * @param programId id of the program + * @param programIds ids of the programs * @param orderableIds Id of orderables * @param orderableCode Code of the orderables * @param orderableName Name of the orderables @@ -74,14 +76,14 @@ public OrderablesAggregator getApprovedProducts(UUID facilityId, UUID programId, */ public OrderablesAggregator getApprovedProducts( UUID facilityId, - UUID programId, + Collection programIds, Collection orderableIds, String orderableCode, String orderableName ) { RequestParameters params = RequestParameters.init(); - params.set("programId", programId); + programIds.forEach(id -> params.set("programId", id)); if (!isEmpty(orderableIds)) { params.set("orderableId", orderableIds); diff --git a/src/main/java/org/openlmis/stockmanagement/validators/SourceDestinationGeoLevelAffinityValidator.java b/src/main/java/org/openlmis/stockmanagement/validators/SourceDestinationGeoLevelAffinityValidator.java index 98977eb6..87c4f120 100644 --- a/src/main/java/org/openlmis/stockmanagement/validators/SourceDestinationGeoLevelAffinityValidator.java +++ b/src/main/java/org/openlmis/stockmanagement/validators/SourceDestinationGeoLevelAffinityValidator.java @@ -15,6 +15,7 @@ package org.openlmis.stockmanagement.validators; +import static java.util.Collections.singleton; import static org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_DESTINATION_ASSIGNMENT_NO_MATCH_GEO_LEVEL_AFFINITY; import static org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_SOURCE_ASSIGNMENT_NO_MATCH_GEO_LEVEL_AFFINITY; @@ -80,8 +81,8 @@ public void validate(StockEventDto stockEventDto) { private void validateDestinations(StockEventDto stockEventDto, Profiler profiler) { profiler.start("FIND_DESTINATIONS"); Page validDestinationDtos = validDestinationService - .findDestinations(stockEventDto.getProgramId(), stockEventDto.getFacilityId(), false, - Pageable.unpaged()); + .findDestinations(singleton(stockEventDto.getProgramId()), stockEventDto.getFacilityId(), + false, Pageable.unpaged()); profiler.start("GET_DESTINATION_IDS"); List validDestinationDtoIds = getValidNodeIds(validDestinationDtos.getContent()); @@ -105,9 +106,9 @@ private void validateDestinations(StockEventDto stockEventDto, Profiler profile private void validateSources(StockEventDto stockEventDto, Profiler profiler) { profiler.start("FIND_SOURCES"); - Page validSourceDtos = - validSourceService.findSources(stockEventDto.getProgramId(), - stockEventDto.getFacilityId(), false, Pageable.unpaged()); + Page validSourceDtos = validSourceService + .findSources(singleton(stockEventDto.getProgramId()), stockEventDto.getFacilityId(), false, + Pageable.unpaged()); profiler.start("GET_SOURCE_IDS"); List validSourceDtoIds = getValidNodeIds(validSourceDtos.getContent()); diff --git a/src/main/java/org/openlmis/stockmanagement/web/ValidReasonAssignmentController.java b/src/main/java/org/openlmis/stockmanagement/web/ValidReasonAssignmentController.java index 49309ae1..4d66836e 100644 --- a/src/main/java/org/openlmis/stockmanagement/web/ValidReasonAssignmentController.java +++ b/src/main/java/org/openlmis/stockmanagement/web/ValidReasonAssignmentController.java @@ -86,7 +86,7 @@ public List getValidReasons( ValidReasonAssignmentSearchParams params = new ValidReasonAssignmentSearchParams(queryParams); profiler.start("SEARCH_VALID_REASONS_IN_SERVICE"); - List reasons = reasonAssignmentRepository.search(params.getProgram(), + List reasons = reasonAssignmentRepository.search(params.getProgramIds(), params.getFacilityType(), params.getReasonType(), params.getReason()); diff --git a/src/main/java/org/openlmis/stockmanagement/web/ValidReasonAssignmentSearchParams.java b/src/main/java/org/openlmis/stockmanagement/web/ValidReasonAssignmentSearchParams.java index 6efb38d7..0fca2779 100644 --- a/src/main/java/org/openlmis/stockmanagement/web/ValidReasonAssignmentSearchParams.java +++ b/src/main/java/org/openlmis/stockmanagement/web/ValidReasonAssignmentSearchParams.java @@ -51,17 +51,25 @@ public ValidReasonAssignmentSearchParams(MultiValueMap queryMap) } /** - * Gets program. - * - * @return String value of program id or null if params doesn't contain "program" param. - * Empty string for null request param value. + * Gets collection of {@link UUID} for "program" key from params. */ - public UUID getProgram() { + public Collection getProgramIds() { if (!queryParams.containsKey(PROGRAM)) { return null; } - String program = queryParams.getFirst(PROGRAM); - return UuidUtil.fromString(program).orElse(null); + + Set programs = new HashSet<>(); + queryParams.asMultiValueMap().forEach((key, value) -> { + if (Objects.equals(key, PROGRAM)) { + value.forEach(id -> { + if (id != null && !id.isEmpty()) { + programs.add(UuidUtil.fromString(id).get()); + } + }); + } + }); + + return programs; } /** diff --git a/src/main/java/org/openlmis/stockmanagement/web/ValidSourceDestinationController.java b/src/main/java/org/openlmis/stockmanagement/web/ValidSourceDestinationController.java index af0fb5ae..9d0b545d 100644 --- a/src/main/java/org/openlmis/stockmanagement/web/ValidSourceDestinationController.java +++ b/src/main/java/org/openlmis/stockmanagement/web/ValidSourceDestinationController.java @@ -73,9 +73,10 @@ public Page getValidDestinations( ValidSourceDestinationSearchParams params = new ValidSourceDestinationSearchParams(parameters); LOGGER.info(format("Try to find valid destinations with program %s and facility %s", - params.getProgramId(), params.getFacilityId())); + params.getFacilityId(), + params.getProgramIds())); return validDestinationService.findDestinations( - params.getProgramId(), params.getFacilityId(), params.getIncludeDisabled(), pageable); + params.getProgramIds(), params.getFacilityId(), params.getIncludeDisabled(), pageable); } /** @@ -113,9 +114,9 @@ public Page getValidSources( ValidSourceDestinationSearchParams params = new ValidSourceDestinationSearchParams(parameters); LOGGER.debug(format("Try to find valid sources with program %s and facility %s", - params.getProgramId(), params.getFacilityId())); + params.getProgramIds(), params.getFacilityId())); return validSourceService.findSources( - params.getProgramId(), params.getFacilityId(), params.getIncludeDisabled(), pageable); + params.getProgramIds(), params.getFacilityId(), params.getIncludeDisabled(), pageable); } /** diff --git a/src/main/java/org/openlmis/stockmanagement/web/ValidSourceDestinationSearchParams.java b/src/main/java/org/openlmis/stockmanagement/web/ValidSourceDestinationSearchParams.java index cb286600..6651bc01 100644 --- a/src/main/java/org/openlmis/stockmanagement/web/ValidSourceDestinationSearchParams.java +++ b/src/main/java/org/openlmis/stockmanagement/web/ValidSourceDestinationSearchParams.java @@ -21,7 +21,11 @@ import static org.openlmis.stockmanagement.i18n.MessageKeys.ERROR_PROVIDED_PROGRAM_ID_WITHOUT_FACILITY_ID; import java.util.Collections; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; import java.util.UUID; + import org.openlmis.stockmanagement.exception.ValidationMessageException; import org.openlmis.stockmanagement.util.Message; import org.openlmis.stockmanagement.util.UuidUtil; @@ -48,12 +52,23 @@ public ValidSourceDestinationSearchParams(MultiValueMap queryMap * * @return UUID value of program id or null if params doesn't contain this param. */ - public UUID getProgramId() { + public Set getProgramIds() { if (!queryParams.containsKey(PROGRAM_ID)) { return null; } - String program = queryParams.getFirst(PROGRAM_ID); - return UuidUtil.fromString(program).orElse(null); + + Set programs = new HashSet<>(); + queryParams.asMultiValueMap().forEach((key, value) -> { + if (Objects.equals(key, PROGRAM_ID)) { + value.forEach(id -> { + if (id != null && !id.isEmpty()) { + programs.add(UuidUtil.fromString(id).get()); + } + }); + } + }); + + return programs.isEmpty() ? null : programs; } /** diff --git a/src/main/resources/api-definition.yaml b/src/main/resources/api-definition.yaml index b637efa9..783d9a8f 100644 --- a/src/main/resources/api-definition.yaml +++ b/src/main/resources/api-definition.yaml @@ -338,8 +338,8 @@ traits: type: string programId: required: true - description: Program id, indicates the program that the queries stock card summaries is under. - repeat: false + description: Program id, indicates the program that the queries stock card summaries is under, can specify multiple program ids. + repeat: true type: string orderableId: required: false @@ -543,6 +543,7 @@ traits: programId: required: false description: Program ID, indicates the program that the valid destinations are under. + repeat: true facilityId: required: false description: Facility ID, indicates the facility that the valid destinations are under. @@ -622,6 +623,7 @@ traits: programId: required: false description: Program ID, indicates the program that the valid sources are under. + repeat: true facilityId: required: false description: Facility ID, indicates the facility that the valid sources are under. @@ -736,6 +738,7 @@ traits: description: Program id type: string required: false + repeat: true facilityType: description: Facility type id type: string diff --git a/src/test/java/org/openlmis/stockmanagement/service/SourceDestinationBaseServiceTest.java b/src/test/java/org/openlmis/stockmanagement/service/SourceDestinationBaseServiceTest.java index f38a152a..068a7ae2 100644 --- a/src/test/java/org/openlmis/stockmanagement/service/SourceDestinationBaseServiceTest.java +++ b/src/test/java/org/openlmis/stockmanagement/service/SourceDestinationBaseServiceTest.java @@ -16,6 +16,7 @@ package org.openlmis.stockmanagement.service; import static java.util.Arrays.asList; +import static java.util.Collections.singleton; import static java.util.UUID.randomUUID; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; @@ -44,7 +45,7 @@ import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.junit.MockitoJUnitRunner; import org.openlmis.stockmanagement.domain.sourcedestination.Node; import org.openlmis.stockmanagement.domain.sourcedestination.Organization; import org.openlmis.stockmanagement.domain.sourcedestination.ValidDestinationAssignment; @@ -117,7 +118,7 @@ public void shouldThrowValidationExceptionWhenProgramAndFacilityTypeNotFound() .checkProgramAndFacilityTypeExist(programId, facilityTypeId); //when - validSourceService.findSources(programId, facilityId, false, pageRequest); + validSourceService.findSources(singleton(programId), facilityId, false, pageRequest); } @Test @@ -337,7 +338,7 @@ public void shouldReturnListOfAllDestinationDtosWhenIncludeDisabledTrue() //when Page validDestinations = - validDestinationService.findDestinations(null, null, true, pageRequest); + validDestinationService.findDestinations(null, null, true, pageRequest); //then assertThat(validDestinations.getContent().size(), is(2)); @@ -374,16 +375,16 @@ public void shouldReturnListOfDestinationDtosWhenFindValidDestinationAssignment( createFacilityDestination(mockedFacilityNode(facilityId, FACILITY_NODE_NAME)) ); - when(destinationRepository.findByProgramIdAndFacilityTypeId( - programId, facilityTypeId, Pageable.unpaged())) + when(destinationRepository.findByProgramIdInAndFacilityTypeId( + singleton(programId), facilityTypeId, Pageable.unpaged())) .thenReturn(validDestinationAssignments); when(facilityReferenceDataService.findByIds(anyListOf(UUID.class))).thenReturn( Collections.singletonMap(facilityId, facilityDto)); //when - Page validDestinations = - validDestinationService.findDestinations(programId, facilityId, false, pageRequest); + Page validDestinations = validDestinationService + .findDestinations(singleton(programId), facilityId, false, pageRequest); //then assertThat(validDestinations.getContent().size(), is(2)); @@ -422,16 +423,16 @@ public void shouldReturnListOfDestinationDtosWhenFindValidDestinationAssignment( createFacilityDestination(mockedFacilityNode(facilityId, FACILITY_NODE_NAME)) ); - when(destinationRepository.findByProgramIdAndFacilityTypeId( - programId, facilityTypeId, Pageable.unpaged())) + when(destinationRepository.findByProgramIdInAndFacilityTypeId( + singleton(programId), facilityTypeId, Pageable.unpaged())) .thenReturn(validDestinationAssignments); when(facilityReferenceDataService.findByIds(anyListOf(UUID.class))).thenReturn( Collections.singletonMap(facilityId, facilityDto)); //when - Page validDestinations = - validDestinationService.findDestinations(programId, facilityId, true, pageRequest); + Page validDestinations = validDestinationService + .findDestinations(singleton(programId), facilityId, true, pageRequest); //then assertThat(validDestinations.getContent().size(), is(4)); @@ -505,8 +506,8 @@ public void shouldReturnListOfSourceDtosWhenFindingValidSourceAssignment() createOrganizationSourceAssignment(mockedOrganizationNode(ORGANIZATION_NODE_NAME, true)), createFacilitySourceAssignment(mockedFacilityNode(facilityId, FACILITY_NODE_NAME))); - when(sourceRepository.findByProgramIdAndFacilityTypeId( - programId, facilityTypeId, Pageable.unpaged())) + when(sourceRepository.findByProgramIdInAndFacilityTypeId( + singleton(programId), facilityTypeId, Pageable.unpaged())) .thenReturn(validSourceAssignments); when(facilityReferenceDataService.findByIds(anyListOf(UUID.class))).thenReturn( @@ -514,7 +515,7 @@ public void shouldReturnListOfSourceDtosWhenFindingValidSourceAssignment() //when Page validSources = - validSourceService.findSources(programId, facilityId, false, pageRequest); + validSourceService.findSources(singleton(programId), facilityId, false, pageRequest); //then assertThat(validSources.getContent().size(), is(2)); @@ -579,8 +580,8 @@ public void shouldReturnListOfDestinationDtosWhenGeoLevelAffinityMatch() regionGeoLevelId, regionGeoZoneId, geoLevelAffinity); //when - Page validDestinations = - validDestinationService.findDestinations(programId, facilityId, false, pageRequest); + Page validDestinations = validDestinationService + .findDestinations(singleton(programId), facilityId, false, pageRequest); //then assertThat(validDestinations.getContent().size(), is(2)); @@ -608,8 +609,8 @@ public void shouldReturnListOfDestinationDtosWhenGeoLevelAffinitIsNull() regionGeoLevelId, regionGeoZoneId, geoLevelAffinity); //when - Page validDestinations = - validDestinationService.findDestinations(programId, facilityId, false, pageRequest); + Page validDestinations = validDestinationService + .findDestinations(singleton(programId), facilityId, false, pageRequest); //then assertThat(validDestinations.getContent().size(), is(2)); @@ -637,8 +638,8 @@ public void shouldReturnListOfDestinationDtosWithOnlyOrganizationWhenGeoLevelAff regionGeoLevelId, regionGeoZoneId, geoLevelAffinity); //when - Page validDestinations = - validDestinationService.findDestinations(programId, facilityId, false, pageRequest); + Page validDestinations = validDestinationService + .findDestinations(singleton(programId), facilityId, false, pageRequest); //then assertThat(validDestinations.getContent().size(), is(1)); @@ -664,8 +665,8 @@ private void mockValidDestinationsAndFacilitiesWithGeoZonesAndLevel(UUID program createFacilityDestinationWithGeoLevelAffinity(mockedFacilityNode(refDataFacilityId, FACILITY_NODE_NAME), geoLevelAffinity)); - when(destinationRepository.findByProgramIdAndFacilityTypeId( - programId, facilityTypeId, Pageable.unpaged())) + when(destinationRepository.findByProgramIdInAndFacilityTypeId( + singleton(programId), facilityTypeId, Pageable.unpaged())) .thenReturn(validDestinationAssignments); FacilityDto refDataFacilityDto = createFacilityDtoWithFacilityType(refDataFacilityId, @@ -699,7 +700,8 @@ public void shouldThrowExceptionWhenDeleteDestinationAssignmentNotExists() public void shouldThrowExceptionWhenFacilityNotExists() throws Exception { when(facilityReferenceDataService.findOne(any(UUID.class))).thenReturn(null); - validDestinationService.findDestinations(randomUUID(), randomUUID(), false, pageRequest); + validDestinationService + .findDestinations(singleton(randomUUID()), randomUUID(), false, pageRequest); } @Test @@ -818,4 +820,4 @@ private Node mockedOrganizationNode(String name, boolean disabled) { return node; } -} \ No newline at end of file +} diff --git a/src/test/java/org/openlmis/stockmanagement/service/StockCardSummariesServiceTest.java b/src/test/java/org/openlmis/stockmanagement/service/StockCardSummariesServiceTest.java index e45d32a0..18e39e97 100644 --- a/src/test/java/org/openlmis/stockmanagement/service/StockCardSummariesServiceTest.java +++ b/src/test/java/org/openlmis/stockmanagement/service/StockCardSummariesServiceTest.java @@ -48,7 +48,7 @@ import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; +import org.mockito.junit.MockitoJUnitRunner; import org.openlmis.stockmanagement.domain.card.StockCard; import org.openlmis.stockmanagement.domain.event.CalculatedStockOnHand; import org.openlmis.stockmanagement.domain.event.StockEvent; @@ -216,7 +216,7 @@ public void shouldFindStockCards() { when(approvedProductReferenceDataService .getApprovedProducts( eq(params.getFacilityId()), - eq(params.getProgramId()), + eq(params.getProgramIds()), eq(params.getOrderableIds()), eq(params.getOrderableCode()), eq(params.getOrderableName()) @@ -244,7 +244,7 @@ public void shouldFindStockCards() { StockEvent event = new StockEventDataBuilder() .withFacility(params.getFacilityId()) - .withProgram(params.getProgramId()) + .withProgram(params.getProgramIds().get(0)) .build(); StockCard stockCard = new StockCardDataBuilder(event) @@ -260,7 +260,7 @@ public void shouldFindStockCards() { List stockCards = asList(stockCard, stockCard1); when(calculatedStockOnHandService - .getStockCardsWithStockOnHand(params.getProgramId(), params.getFacilityId(), + .getStockCardsWithStockOnHand(params.getProgramIds(), params.getFacilityId(), params.getAsOfDate(), Collections.emptyList(), Collections.emptySet())) .thenReturn(stockCards); @@ -280,7 +280,7 @@ public void shouldThrowExceptionIfNoPermission() { doThrow(new PermissionMessageException(new Message("no permission"))) .when(permissionService) - .canViewStockCard(params.getProgramId(), params.getFacilityId()); + .canViewStockCard(params.getProgramIds().get(0), params.getFacilityId()); stockCardSummariesService.findStockCards(params); } diff --git a/src/test/java/org/openlmis/stockmanagement/service/StockCardSummariesV2SearchParamsTest.java b/src/test/java/org/openlmis/stockmanagement/service/StockCardSummariesV2SearchParamsTest.java index 8696746e..753894d9 100644 --- a/src/test/java/org/openlmis/stockmanagement/service/StockCardSummariesV2SearchParamsTest.java +++ b/src/test/java/org/openlmis/stockmanagement/service/StockCardSummariesV2SearchParamsTest.java @@ -113,7 +113,7 @@ public void shouldCreateParamsFromMap() { StockCardSummariesV2SearchParams params = new StockCardSummariesV2SearchParams(parameters); - assertEquals(programId, params.getProgramId()); + assertEquals(programId, params.getProgramIds().get(0)); assertEquals(facilityId, params.getFacilityId()); assertEquals(asOfDate, params.getAsOfDate()); assertEquals(asList(orderableId1, orderableId2), params.getOrderableIds()); @@ -126,7 +126,7 @@ public void shouldCreateEmptyParams() { StockCardSummariesV2SearchParams params = new StockCardSummariesV2SearchParams(parameters); - assertEquals(params.getProgramId(), null); + assertEquals(params.getProgramIds(), null); assertEquals(params.getFacilityId(), null); assertEquals(params.getAsOfDate(), null); assertTrue(isEmpty(params.getOrderableIds())); diff --git a/src/test/java/org/openlmis/stockmanagement/testutils/StockCardSummariesV2SearchParamsDataBuilder.java b/src/test/java/org/openlmis/stockmanagement/testutils/StockCardSummariesV2SearchParamsDataBuilder.java index 6a14f8b7..da20cafd 100644 --- a/src/test/java/org/openlmis/stockmanagement/testutils/StockCardSummariesV2SearchParamsDataBuilder.java +++ b/src/test/java/org/openlmis/stockmanagement/testutils/StockCardSummariesV2SearchParamsDataBuilder.java @@ -24,7 +24,7 @@ public class StockCardSummariesV2SearchParamsDataBuilder { - private UUID programId; + private List programIds; private UUID facilityId; private List orderableId; private LocalDate asOfDate; @@ -37,7 +37,7 @@ public class StockCardSummariesV2SearchParamsDataBuilder { * Creates builder for creating new instance of {@link StockCardSummariesV2SearchParams}. */ public StockCardSummariesV2SearchParamsDataBuilder() { - programId = UUID.randomUUID(); + programIds = asList(UUID.randomUUID(), UUID.randomUUID()); facilityId = UUID.randomUUID(); orderableId = asList(UUID.randomUUID(), UUID.randomUUID()); asOfDate = LocalDate.now(); @@ -52,7 +52,7 @@ public StockCardSummariesV2SearchParamsDataBuilder() { * @return created stock card line item reason. */ public StockCardSummariesV2SearchParams build() { - return new StockCardSummariesV2SearchParams(programId, facilityId, + return new StockCardSummariesV2SearchParams(programIds, facilityId, orderableId, asOfDate, nonEmptyOnly, orderableCode, orderabletName, lotCode); } @@ -62,7 +62,7 @@ public StockCardSummariesV2SearchParamsDataBuilder withoutFacilityId() { } public StockCardSummariesV2SearchParamsDataBuilder withoutProgramId() { - this.programId = null; + this.programIds = null; return this; } diff --git a/src/test/java/org/openlmis/stockmanagement/validators/SourceDestinationAssignmentValidatorTest.java b/src/test/java/org/openlmis/stockmanagement/validators/SourceDestinationAssignmentValidatorTest.java index 6c03dca6..4373e95e 100644 --- a/src/test/java/org/openlmis/stockmanagement/validators/SourceDestinationAssignmentValidatorTest.java +++ b/src/test/java/org/openlmis/stockmanagement/validators/SourceDestinationAssignmentValidatorTest.java @@ -181,4 +181,4 @@ private void createContextWithFacility(StockEventDto eventDto) { setContext(eventDto); } -} \ No newline at end of file +} diff --git a/src/test/java/org/openlmis/stockmanagement/validators/SourceDestinationGeoLevelAffinityValidatorTest.java b/src/test/java/org/openlmis/stockmanagement/validators/SourceDestinationGeoLevelAffinityValidatorTest.java index 83d5de0b..4c0de117 100644 --- a/src/test/java/org/openlmis/stockmanagement/validators/SourceDestinationGeoLevelAffinityValidatorTest.java +++ b/src/test/java/org/openlmis/stockmanagement/validators/SourceDestinationGeoLevelAffinityValidatorTest.java @@ -16,6 +16,7 @@ package org.openlmis.stockmanagement.validators; import static java.util.Arrays.asList; +import static java.util.Collections.singleton; import static java.util.UUID.randomUUID; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.Matchers.any; @@ -81,7 +82,7 @@ public void shouldNotRejectSourceWhenGeoAffinityMatch() { createValidSourceDestinationDto(randomUUID(), ORGANIZATION_NODE_NAME)); when( - validSourceService.findSources(stockEventDto.getProgramId(), + validSourceService.findSources(singleton(stockEventDto.getProgramId()), stockEventDto.getFacilityId(), false, Pageable.unpaged())) .thenReturn(Pagination.getPage(validDestinationAssignments)); @@ -99,7 +100,7 @@ public void shouldNotRejectDestinationWhenGeoAffinityMatch() { createValidSourceDestinationDto(randomUUID(), ORGANIZATION_NODE_NAME)); when( - validDestinationService.findDestinations(stockEventDto.getProgramId(), + validDestinationService.findDestinations(singleton(stockEventDto.getProgramId()), stockEventDto.getFacilityId(), false, Pageable.unpaged())) .thenReturn(Pagination.getPage(validDestinationAssignments)); @@ -121,7 +122,7 @@ public void shouldRejectSourceWhenGeoAffinityNotMatch() { createValidSourceDestinationDto(randomUUID(), ORGANIZATION_NODE_NAME)); when( - validSourceService.findSources(stockEventDto.getProgramId(), + validSourceService.findSources(singleton(stockEventDto.getProgramId()), stockEventDto.getFacilityId(), false, Pageable.unpaged())) .thenReturn(Pagination.getPage(validDestinationAssignments)); @@ -143,7 +144,7 @@ public void shouldRejectSourceWhenValidDestinationAssignmentsIsEmpty() { setContext(stockEventDto); when( - validSourceService.findSources(stockEventDto.getProgramId(), + validSourceService.findSources(singleton(stockEventDto.getProgramId()), stockEventDto.getFacilityId(), false, Pageable.unpaged())) .thenReturn(Page.empty()); @@ -166,7 +167,7 @@ public void shouldRejectSourceWithMessageWhenGeoAffinityNotMatch() { createValidSourceDestinationDto(randomUUID(), FACILITY_NODE_NAME), createValidSourceDestinationDto(randomUUID(), ORGANIZATION_NODE_NAME)); - when(validSourceService.findSources(stockEventDto.getProgramId(), + when(validSourceService.findSources(singleton(stockEventDto.getProgramId()), stockEventDto.getFacilityId(), false, Pageable.unpaged())) .thenReturn(Pagination.getPage(validDestinationAssignments)); @@ -190,7 +191,7 @@ public void shouldRejectDestinationWhenGeoAffinityNotMatch() { createValidSourceDestinationDto(randomUUID(), FACILITY_NODE_NAME), createValidSourceDestinationDto(randomUUID(), ORGANIZATION_NODE_NAME)); - when(validDestinationService.findDestinations(stockEventDto.getProgramId(), + when(validDestinationService.findDestinations(singleton(stockEventDto.getProgramId()), stockEventDto.getFacilityId(), false, Pageable.unpaged())) .thenReturn(Pagination.getPage(validDestinationAssignments)); diff --git a/src/test/java/org/openlmis/stockmanagement/web/ValidReasonAssignmentSearchParamsTest.java b/src/test/java/org/openlmis/stockmanagement/web/ValidReasonAssignmentSearchParamsTest.java index b52c3089..22fad7c5 100644 --- a/src/test/java/org/openlmis/stockmanagement/web/ValidReasonAssignmentSearchParamsTest.java +++ b/src/test/java/org/openlmis/stockmanagement/web/ValidReasonAssignmentSearchParamsTest.java @@ -16,13 +16,16 @@ package org.openlmis.stockmanagement.web; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import static org.openlmis.stockmanagement.web.ValidReasonAssignmentSearchParams.FACILITY_TYPE; import static org.openlmis.stockmanagement.web.ValidReasonAssignmentSearchParams.PROGRAM; import static org.openlmis.stockmanagement.web.ValidReasonAssignmentSearchParams.REASON; import static org.openlmis.stockmanagement.web.ValidReasonAssignmentSearchParams.REASON_TYPE; import com.google.common.collect.Sets; +import java.util.Collection; import java.util.UUID; import org.junit.Test; import org.openlmis.stockmanagement.domain.reason.ReasonType; @@ -38,10 +41,17 @@ public class ValidReasonAssignmentSearchParamsTest { @Test public void shouldGetProgramIdValueFromParameters() { LinkedMultiValueMap queryMap = new LinkedMultiValueMap<>(); + final UUID typeId = UUID.randomUUID(); queryMap.add(PROGRAM, VALUE.toString()); + queryMap.add(PROGRAM, ""); + queryMap.add(FACILITY_TYPE, typeId.toString()); ValidReasonAssignmentSearchParams params = new ValidReasonAssignmentSearchParams(queryMap); - assertEquals(VALUE, params.getProgram()); + Collection programIds = params.getProgramIds(); + + assertTrue(programIds.contains(VALUE)); + assertFalse(programIds.contains(typeId)); + assertFalse(programIds.contains("")); } @Test @@ -49,7 +59,7 @@ public void shouldAssignNullIfProgramIdIsAbsentInParameters() { ValidReasonAssignmentSearchParams params = new ValidReasonAssignmentSearchParams(new LinkedMultiValueMap<>()); - assertNull(params.getProgram()); + assertNull(params.getProgramIds()); } @Test @@ -98,10 +108,13 @@ public void shouldGetReasonTypesFromParameters() { LinkedMultiValueMap queryMap = new LinkedMultiValueMap<>(); queryMap.add(REASON_TYPE, DEBIT); queryMap.add(REASON_TYPE, CREDIT); + queryMap.add(REASON, VALUE.toString()); ValidReasonAssignmentSearchParams params = new ValidReasonAssignmentSearchParams(queryMap); + Collection reasonTypes = params.getReasonType(); + assertEquals(Sets.newHashSet(ReasonType.fromString(CREDIT), ReasonType.fromString(DEBIT)), - params.getReasonType()); + reasonTypes); } @Test diff --git a/src/test/java/org/openlmis/stockmanagement/web/ValidSourceDestinationSearchParamsTest.java b/src/test/java/org/openlmis/stockmanagement/web/ValidSourceDestinationSearchParamsTest.java index 8142a8f7..17d3bad2 100644 --- a/src/test/java/org/openlmis/stockmanagement/web/ValidSourceDestinationSearchParamsTest.java +++ b/src/test/java/org/openlmis/stockmanagement/web/ValidSourceDestinationSearchParamsTest.java @@ -18,11 +18,13 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import static org.openlmis.stockmanagement.web.ValidSourceDestinationSearchParams.FACILITY_ID; import static org.openlmis.stockmanagement.web.ValidSourceDestinationSearchParams.INCLUDE_DISABLED; import static org.openlmis.stockmanagement.web.ValidSourceDestinationSearchParams.PROGRAM_ID; import java.util.UUID; + import org.junit.Test; import org.openlmis.stockmanagement.exception.ValidationMessageException; import org.springframework.util.LinkedMultiValueMap; @@ -41,7 +43,7 @@ public void shouldGetProgramIdValueFromParameters() { queryMap.add(FACILITY_ID, FACILITY_ID_VALUE.toString()); ValidSourceDestinationSearchParams params = new ValidSourceDestinationSearchParams(queryMap); - assertEquals(PROGRAM_ID_VALUE, params.getProgramId()); + assertTrue(params.getProgramIds().contains(PROGRAM_ID_VALUE)); } @Test @@ -49,7 +51,18 @@ public void shouldAssignNullIfProgramIdIsAbsentInParameters() { ValidSourceDestinationSearchParams params = new ValidSourceDestinationSearchParams(new LinkedMultiValueMap<>()); - assertNull(params.getProgramId()); + assertNull(params.getProgramIds()); + } + + @Test + public void shouldAssignNullIfProgramIdIsNullAndEmptyStringInParameters() { + LinkedMultiValueMap queryMap = new LinkedMultiValueMap<>(); + queryMap.add(PROGRAM_ID, null); + queryMap.add(PROGRAM_ID, ""); + queryMap.add(FACILITY_ID, FACILITY_ID_VALUE.toString()); + ValidSourceDestinationSearchParams params = new ValidSourceDestinationSearchParams(queryMap); + + assertNull(params.getProgramIds()); } @Test