Skip to content

Commit

Permalink
Cherry-pick (modified) from OAM-218 solution, commit 8b82790.
Browse files Browse the repository at this point in the history
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 <[email protected]>
Co-authored-by: tsznaj <[email protected]>
# Conflicts:
#	src/integration-test/java/org/openlmis/stockmanagement/web/ValidSourceDestinationControllerIntegrationTest.java
#	src/main/java/org/openlmis/stockmanagement/repository/SourceDestinationAssignmentRepository.java
#	src/main/java/org/openlmis/stockmanagement/service/SourceDestinationBaseService.java
#	src/main/java/org/openlmis/stockmanagement/service/StockCardSummariesService.java
#	src/main/java/org/openlmis/stockmanagement/service/ValidDestinationService.java
#	src/main/java/org/openlmis/stockmanagement/service/ValidSourceService.java
#	src/main/java/org/openlmis/stockmanagement/validators/SourceDestinationGeoLevelAffinityValidator.java
#	src/main/java/org/openlmis/stockmanagement/web/ValidSourceDestinationController.java
#	src/test/java/org/openlmis/stockmanagement/service/SourceDestinationBaseServiceTest.java
#	src/test/java/org/openlmis/stockmanagement/validators/SourceDestinationGeoLevelAffinityValidatorTest.java
  • Loading branch information
sradziszewski authored and tsznaj committed Jul 2, 2024
1 parent 5556e29 commit a740cc4
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand Down Expand Up @@ -93,7 +94,7 @@ public void shouldReturnValidReasonWithProgramAndFacilityTypeAndReasonAndReasonT
repository.save(newAssignment);

List<ValidReasonAssignment> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@

public interface StockCardRepository extends JpaRepository<StockCard, UUID> {

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<StockCard> findByProgramIdAndFacilityId(
@Param("programId") UUID programId,
@Param("facilityId") UUID facilityId,
@Param(PROGRAM_ID) UUID programId,
@Param(FACILITY_ID) UUID facilityId,
Pageable pageable);

List<StockCard> findByProgramIdAndFacilityId(
@Param("programId") UUID programId,
@Param("facilityId") UUID facilityId);
@Param(PROGRAM_ID) UUID programId,
@Param(FACILITY_ID) UUID facilityId);

List<StockCard> findByOrderableIdInAndProgramIdAndFacilityId(
Collection<UUID> orderableIds, UUID programId, UUID facilityId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public interface ValidReasonAssignmentRepositoryCustom {

List<ValidReasonAssignment> search(UUID programId, UUID facilityTypeId,
List<ValidReasonAssignment> search(Collection<UUID> programIds, UUID facilityTypeId,
Collection<ReasonType> reasonTypes, UUID reasonId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValidReasonAssignment> search(UUID programId, UUID facilityTypeId,
public List<ValidReasonAssignment> search(Collection<UUID> programIds, UUID facilityTypeId,
Collection<ReasonType> reasonTypes, UUID reasonId) {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();

Expand All @@ -61,8 +61,8 @@ public List<ValidReasonAssignment> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public List<ValidReasonAssignmentDto> getValidReasons(
ValidReasonAssignmentSearchParams params = new ValidReasonAssignmentSearchParams(queryParams);

profiler.start("SEARCH_VALID_REASONS_IN_SERVICE");
List<ValidReasonAssignment> reasons = reasonAssignmentRepository.search(params.getProgram(),
List<ValidReasonAssignment> reasons = reasonAssignmentRepository.search(params.getProgramIds(),
params.getFacilityType(),
params.getReasonType(), params.getReason());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,25 @@ public ValidReasonAssignmentSearchParams(MultiValueMap<String, String> 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<UUID> getProgramIds() {
if (!queryParams.containsKey(PROGRAM)) {
return null;
}
String program = queryParams.getFirst(PROGRAM);
return UuidUtil.fromString(program).orElse(null);

Set<UUID> 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;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/api-definition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ traits:
description: Program id
type: string
required: false
repeat: true
facilityType:
description: Facility type id
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,18 +41,25 @@ public class ValidReasonAssignmentSearchParamsTest {
@Test
public void shouldGetProgramIdValueFromParameters() {
LinkedMultiValueMap<String, String> 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<UUID> programIds = params.getProgramIds();

assertTrue(programIds.contains(VALUE));
assertFalse(programIds.contains(typeId));
assertFalse(programIds.contains(""));
}

@Test
public void shouldAssignNullIfProgramIdIsAbsentInParameters() {
ValidReasonAssignmentSearchParams params =
new ValidReasonAssignmentSearchParams(new LinkedMultiValueMap<>());

assertNull(params.getProgram());
assertNull(params.getProgramIds());
}

@Test
Expand Down Expand Up @@ -98,10 +108,13 @@ public void shouldGetReasonTypesFromParameters() {
LinkedMultiValueMap<String, String> queryMap = new LinkedMultiValueMap<>();
queryMap.add(REASON_TYPE, DEBIT);
queryMap.add(REASON_TYPE, CREDIT);
queryMap.add(REASON, VALUE.toString());
ValidReasonAssignmentSearchParams params = new ValidReasonAssignmentSearchParams(queryMap);

Collection<ReasonType> reasonTypes = params.getReasonType();

assertEquals(Sets.newHashSet(ReasonType.fromString(CREDIT), ReasonType.fromString(DEBIT)),
params.getReasonType());
reasonTypes);
}

@Test
Expand Down

0 comments on commit a740cc4

Please sign in to comment.