Skip to content

Commit

Permalink
OLMIS-7953 Copied solution with multiple program ids to make applicat…
Browse files Browse the repository at this point in the history
…ion logging performance better. (#113)

* Cherry-pick (modified) from OAM-218 solution, commit 794d23f.

OAM-218: Many program ids for GET API for orderables. (#109)

* OAM-218: WIP

* OAM-218: Change programId param to repeatable

* OAM-218: Changed getting all orderables to get for many program codes.

* OAM-213: Added fixes.

* OAM-213: Changed to have multiple "program" API parameter.

* OAM-213: Corrected tests.

* OAM-213: Corrected some tests and added some constants.

* OAM-213: Applied some corrections.

* OAM-218: Name corrected.

* OAM-218: Fix for tests.

* OAM-218: Fix for test.

* OAM-218: Fix for repository tests (by pwargulak).

* OAM-218: Fixes after review.

---------

Co-authored-by: Piotr Wargulak <[email protected]>
Co-authored-by: Szymon Radziszewski <[email protected]>

* OLMIS-7953: added test for coverage

* OLMIS-7953: Added coverage test for orderable repository (in progress).

* OLMIS-7953: Added coverage test for orderable repository (finished).

* OLMIS-7953: huge coverage test

* OLMIS-7953: Changed to stream solution after code review.

* OLMIS-7953: Fixed correction.

---------

Co-authored-by: Piotr Wargulak <[email protected]>
Co-authored-by: Szymon Radziszewski <[email protected]>
  • Loading branch information
3 people authored Jul 10, 2024
1 parent 370597d commit b7bc6a9
Show file tree
Hide file tree
Showing 23 changed files with 695 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void setUp() {
orderableDisplayCategoryRepository.save(orderableDisplayCategory);

ProgramOrderable programOrderableFullSupply = new ProgramOrderableDataBuilder()
.withOrderabeDisplayCategory(orderableDisplayCategory)
.withOrderableDisplayCategory(orderableDisplayCategory)
.withProgram(program)
.withoutProduct()
.buildAsNew();
Expand All @@ -177,7 +177,7 @@ public void setUp() {
orderableRepository.saveAndFlush(orderableFullSupply);

ProgramOrderable programOrderable1 = new ProgramOrderableDataBuilder()
.withOrderabeDisplayCategory(orderableDisplayCategory)
.withOrderableDisplayCategory(orderableDisplayCategory)
.withProgram(program2)
.withoutProduct()
.buildAsNew();
Expand All @@ -190,7 +190,7 @@ public void setUp() {
orderableRepository.save(orderable1);

ProgramOrderable programOrderable2 = new ProgramOrderableDataBuilder()
.withOrderabeDisplayCategory(orderableDisplayCategory)
.withOrderableDisplayCategory(orderableDisplayCategory)
.withProgram(program2)
.withoutProduct()
.buildAsNew();
Expand All @@ -203,7 +203,7 @@ public void setUp() {
orderableRepository.save(orderable2);

ProgramOrderable programOrderableNonFullSupply = new ProgramOrderableDataBuilder()
.withOrderabeDisplayCategory(orderableDisplayCategory)
.withOrderableDisplayCategory(orderableDisplayCategory)
.withProgram(program)
.withoutProduct()
.asNonFullSupply()
Expand Down Expand Up @@ -257,10 +257,16 @@ public void shouldGetFullAndNonFullSupply() {

List<UUID> orderableIds = emptyList();

Page<FacilityTypeApprovedProduct> page = ftapRepository
.searchProducts(facility.getId(), program.getId(), null, orderableIds, null,
null, null, pageable
);
Page<FacilityTypeApprovedProduct> page =
ftapRepository.searchProducts(
facility.getId(),
program.getId(),
null,
orderableIds,
null,
null,
null,
pageable);

assertThat(page.getContent(), hasSize(2));
}
Expand All @@ -275,10 +281,16 @@ public void shouldGetFullAndNonFullSupplyFilteredByOrderableIds() {
List<UUID> orderableIds = Lists
.newArrayList(orderableFullSupply.getId(), orderableNonFullSupply.getId());

Page<FacilityTypeApprovedProduct> page = ftapRepository
.searchProducts(facility.getId(), program.getId(), null, orderableIds, null,
null, null, pageable
);
Page<FacilityTypeApprovedProduct> page =
ftapRepository.searchProducts(
facility.getId(),
program.getId(),
null,
orderableIds,
null,
null,
null,
pageable);

assertThat(page.getContent(), hasSize(2));
assertEquals(page.getContent().get(0).getOrderableId(), orderableFullSupply.getId());
Expand All @@ -295,10 +307,16 @@ public void shouldPaginate() {
pageable = PageRequest.of(0, 1);
List<UUID> orderableIds = emptyList();

Page<FacilityTypeApprovedProduct> page = ftapRepository
.searchProducts(facility.getId(), program.getId(), null, orderableIds, null,
null, null, pageable
);
Page<FacilityTypeApprovedProduct> page =
ftapRepository.searchProducts(
facility.getId(),
program.getId(),
null,
orderableIds,
null,
null,
null,
pageable);

assertThat(page.getContent(),
hasSize(1));
Expand All @@ -312,10 +330,16 @@ public void shouldGetFullSupply() {

List<UUID> orderableIds = emptyList();

Page<FacilityTypeApprovedProduct> page = ftapRepository
.searchProducts(facility.getId(), program.getId(), true, orderableIds, null,
null, null, pageable
);
Page<FacilityTypeApprovedProduct> page =
ftapRepository.searchProducts(
facility.getId(),
program.getId(),
true,
orderableIds,
null,
null,
null,
pageable);

assertThat(page.getContent(), hasSize(1));

Expand All @@ -342,21 +366,33 @@ public void shouldGetNonFullSupply() {

List<UUID> orderableIds = emptyList();

Page<FacilityTypeApprovedProduct> page = ftapRepository
.searchProducts(facility.getId(), program.getId(), false, orderableIds, null,
null, null, pageable
);
Page<FacilityTypeApprovedProduct> page =
ftapRepository.searchProducts(
facility.getId(),
program.getId(),
false,
orderableIds,
null,
null,
null,
pageable);

// At this point we have no non-full supply products
assertEquals(0, page.getContent().size());

// Create a non-full supply product
saveAndGetProduct(facilityType1, false);

page = ftapRepository
.searchProducts(facility.getId(), program.getId(), false, orderableIds, null,
null, null, pageable
);
page =
ftapRepository.searchProducts(
facility.getId(),
program.getId(),
false,
orderableIds,
null,
null,
null,
pageable);

// We should be able to find non-full supply product we have created
assertEquals(1, page.getContent().size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,18 @@ public class OrderableRepositoryIntegrationTest {
private static final String EACH = "each";
private static final String ORDERABLE_NAME = "abc";
private static final String SOME_CODE = "some-code";

private final AtomicInteger instanceNumber = new AtomicInteger(0);
private final PageRequest pageable = PageRequest.of(0, Integer.MAX_VALUE, Sort.Direction.ASC,
"fullProductName");
@Autowired
private OrderableRepository repository;

@Autowired
private ProgramRepository programRepository;

@Autowired
private OrderableDisplayCategoryRepository orderableDisplayCategoryRepository;

@Autowired
private EntityManager entityManager;

private AtomicInteger instanceNumber = new AtomicInteger(0);

private PageRequest pageable = PageRequest.of(0, Integer.MAX_VALUE, Sort.Direction.ASC,
"fullProductName");

private int getNextInstanceNumber() {
return this.instanceNumber.incrementAndGet();
}
Expand All @@ -123,13 +117,13 @@ public void shouldNotAllowForDuplicatedActiveProgramOrderables() {
Orderable orderable = new OrderableDataBuilder().buildAsNew();

ProgramOrderable programOrderable = new ProgramOrderableDataBuilder()
.withOrderabeDisplayCategory(orderableDisplayCategory)
.withOrderableDisplayCategory(orderableDisplayCategory)
.withProgram(program)
.withProduct(orderable)
.buildAsNew();

ProgramOrderable programOrderableDuplicated = new ProgramOrderableDataBuilder()
.withOrderabeDisplayCategory(orderableDisplayCategory2)
.withOrderableDisplayCategory(orderableDisplayCategory2)
.withProgram(program)
.withProduct(orderable)
.buildAsNew();
Expand All @@ -154,13 +148,13 @@ public void shouldAllowForDuplicatedProgramOrderablesIfTheyAreInactive() {
Orderable orderable = new OrderableDataBuilder().buildAsNew();

ProgramOrderable programOrderable = new ProgramOrderableDataBuilder()
.withOrderabeDisplayCategory(orderableDisplayCategory)
.withOrderableDisplayCategory(orderableDisplayCategory)
.withProgram(program)
.withProduct(orderable)
.buildAsNew();

ProgramOrderable programOrderableDuplicated = new ProgramOrderableDataBuilder()
.withOrderabeDisplayCategory(orderableDisplayCategory2)
.withOrderableDisplayCategory(orderableDisplayCategory2)
.withProgram(program)
.withProduct(orderable)
.asInactive()
Expand Down Expand Up @@ -344,7 +338,7 @@ public void shouldFindOrderablesByProgram() {

// when
Page<Orderable> foundOrderables = repository.search(
new TestSearchParams(null, null, programCode, null),
new TestSearchParams(null, null, Collections.singleton(programCode), null),
pageable);

// then
Expand Down Expand Up @@ -403,7 +397,7 @@ public void shouldFindOrderablesByAllParams() {
Page<Orderable> foundOrderables = repository.search(
new TestSearchParams(
validOrderable.getProductCode().toString(), NAME,
validProgram.getCode().toString(), null),
Collections.singleton(validProgram.getCode().toString()), null),
pageable);

// then
Expand Down Expand Up @@ -431,8 +425,8 @@ public void shouldFindFirstByVersionNumberAndProductCodeIgnoreCase() {
String lowercaseCode = uppercaseCode.toLowerCase();

Orderable orderable = new OrderableDataBuilder()
.withProductCode(Code.code(uppercaseCode))
.buildAsNew();
.withProductCode(Code.code(uppercaseCode))
.buildAsNew();

assertNull(repository.findFirstByVersionNumberAndProductCodeIgnoreCase(lowercaseCode, 1L));
assertNull(repository.findFirstByVersionNumberAndProductCodeIgnoreCase(uppercaseCode, 1L));
Expand Down Expand Up @@ -471,11 +465,11 @@ public void shouldNotAllowProductCodeDuplicateCaseInsensitive() {
String productCode = "abcdef";

Orderable orderable1 = new OrderableDataBuilder()
.withProductCode(Code.code(productCode))
.buildAsNew();
.withProductCode(Code.code(productCode))
.buildAsNew();
Orderable orderable2 = new OrderableDataBuilder()
.withProductCode(Code.code(productCode.toLowerCase()))
.buildAsNew();
.withProductCode(Code.code(productCode.toLowerCase()))
.buildAsNew();

repository.save(orderable1);
repository.save(orderable2);
Expand Down Expand Up @@ -711,7 +705,7 @@ public void shouldFindLastUpdatedDateFromOrderablesRetrievedByIds() {

Timestamp timestamp = repository.findLatestModifiedDateByIds(ids);
ZonedDateTime lastUpdated = ZonedDateTime.of(timestamp.toLocalDateTime(),
ZoneId.of(ZoneId.systemDefault().toString()));
ZoneId.of(ZoneId.systemDefault().toString()));

//then
assertEquals(lastUpdated, orderable3.getLastUpdated());
Expand All @@ -733,7 +727,7 @@ public void shouldFindLastUpdatedDateFromAllOrderables() {
//when
Timestamp timestamp = repository.findLatestModifiedDateOfAll();
ZonedDateTime lastUpdated = ZonedDateTime.of(timestamp.toLocalDateTime(),
ZoneId.of(ZoneId.systemDefault().toString()));
ZoneId.of(ZoneId.systemDefault().toString()));

//then
assertEquals(lastUpdated, orderable3.getLastUpdated());
Expand All @@ -757,8 +751,8 @@ public void shouldFindLastUpdatedDateFromOrderablesRetrievedByParams() {
new TestSearchParams(orderable3.getProductCode().toString(),
orderable3.getFullProductName(), null,
Sets.newHashSet(Pair.of(orderable1.getId(), orderable1.getVersionNumber()),
Pair.of(orderable2.getId(), orderable2.getVersionNumber()),
Pair.of(orderable3.getId(), orderable3.getVersionNumber()))));
Pair.of(orderable2.getId(), orderable2.getVersionNumber()),
Pair.of(orderable3.getId(), orderable3.getVersionNumber()))));

//then
assertEquals(lastUpdated, orderable3.getLastUpdated().withZoneSameLocal(ZoneId.of("GMT")));
Expand All @@ -783,7 +777,7 @@ public void shouldReturnOrderableWitAllProgramsWhenSearchingByProgramCode() {

// when
Page<Orderable> foundOrderables = repository.search(
new TestSearchParams(null, null, programCode, null),
new TestSearchParams(null, null, Collections.singleton(programCode), null),
pageable);

// then
Expand All @@ -799,10 +793,11 @@ public void shouldReturnOrderableWitAllProgramsWhenSearchingByProgramCode() {
}

private void searchOrderablesAndCheckResults(String code, String name, Program program,
Orderable orderable, int expectedSize) {
Orderable orderable, int expectedSize) {
String programCode = null == program ? null : program.getCode().toString();
Page<Orderable> foundOrderables = repository
.search(new TestSearchParams(code, name, programCode, null), pageable);
.search(new TestSearchParams(code, name, Collections.singleton(programCode), null),
pageable);

assertEquals(expectedSize, foundOrderables.getTotalElements());

Expand All @@ -817,7 +812,7 @@ private ProgramOrderable createProgramOrderable(Program program, Orderable order
"some-code");

return new ProgramOrderableDataBuilder()
.withOrderabeDisplayCategory(orderableDisplayCategory)
.withOrderableDisplayCategory(orderableDisplayCategory)
.withProgram(program)
.withProduct(orderable)
.buildAsNew();
Expand Down Expand Up @@ -895,7 +890,7 @@ private static final class TestSearchParams implements SearchParams {

private String code;
private String name;
private String programCode;
private Set<String> programCodes;
private Set<Pair<UUID, Long>> identityPairs;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ private OrderableDisplayCategory createAndPersistOrderableDisplayCategory(String
private ProgramOrderable createAndPersistProgramOrderable(
Program program, Orderable orderable, OrderableDisplayCategory orderableDisplayCategory) {
ProgramOrderable programOrderable = new ProgramOrderableDataBuilder()
.withProgram(program).withProduct(orderable).withOrderabeDisplayCategory(
.withProgram(program).withProduct(orderable).withOrderableDisplayCategory(
orderableDisplayCategory).buildAsNew();
programOrderableRepository.saveAndFlush(programOrderable);
return programOrderable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class DataImportControllerIntegrationTest extends BaseWebIntegrationTest
private static final MultipartFile file = new MockMultipartFile(
"orderable.csv", "test-data".getBytes());
private final Orderable orderable = new OrderableDataBuilder().build();
private final OrderableDto orderableDto = OrderableDto.newInstance(orderable);
private final OrderableDto orderableDto = OrderableDto.newInstances(orderable);

@Before
@Override
Expand Down
Loading

0 comments on commit b7bc6a9

Please sign in to comment.