Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OLMIS-7953 Copied solution with multiple program ids to make application logging performance better. #61

Merged
merged 5 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -33,26 +33,25 @@

public class ValidReasonAssignmentRepositoryImpl implements ValidReasonAssignmentRepositoryCustom {

private static final String PROGRAM_ID = "programId";
private static final String FACILITY_TYPE_ID = "facilityTypeId";
static final String PROGRAM_ID = "programId";
static final String FACILITY_TYPE_ID = "facilityTypeId";
private static final String ID = "id";
private static final String REASON_TYPE = "reasonType";
private static final String REASON = "reason";

@PersistenceContext
private EntityManager entityManager;

/**
* 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
* @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,
Collection<ReasonType> reasonTypes, UUID reasonId) {
public List<ValidReasonAssignment> search(Collection<UUID> programIds, UUID facilityTypeId,
Collection<ReasonType> reasonTypes, UUID reasonId) {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();

CriteriaQuery<ValidReasonAssignment> query = builder.createQuery(ValidReasonAssignment.class);
Expand All @@ -61,8 +60,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 All @@ -83,7 +82,6 @@ public List<ValidReasonAssignment> search(UUID programId, UUID facilityTypeId,

query.where(predicate);

return entityManager.createQuery(query)
.getResultList();
return entityManager.createQuery(query).getResultList();
}
}
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
@@ -0,0 +1,141 @@
/*
* This program is part of the OpenLMIS logistics management information system platform software.
* Copyright © 2017 VillageReach
*
* This program is free software: you can redistribute it and/or modify it under the terms
* of the GNU Affero General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details. You should have received a copy of
* the GNU Affero General Public License along with this program. If not, see
* http://www.gnu.org/licenses.  For additional information contact [email protected].
*/

package org.openlmis.stockmanagement.repository.custom.impl;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.openlmis.stockmanagement.repository.custom.impl.ValidReasonAssignmentRepositoryImpl.FACILITY_TYPE_ID;
import static org.openlmis.stockmanagement.repository.custom.impl.ValidReasonAssignmentRepositoryImpl.PROGRAM_ID;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openlmis.stockmanagement.domain.reason.ValidReasonAssignment;

@RunWith(MockitoJUnitRunner.class)
public class ValidReasonAssignmentRepositoryImplTest {

@InjectMocks
private ValidReasonAssignmentRepositoryImpl repository;

@Mock
private EntityManager entityManager;

@Test
public void shouldSearchForProgramIdsOnly() {
//given
UUID programId1 = UUID.randomUUID();
UUID programId2 = UUID.randomUUID();
Collection<UUID> programIds = new ArrayList<>();
programIds.add(programId1);
programIds.add(programId2);

List<ValidReasonAssignment> validReasonAssignmentList = mock(List.class);
TypedQuery typedQuery = mock(TypedQuery.class);
when(typedQuery.getResultList())
.thenReturn(validReasonAssignmentList);

CriteriaQuery query = mock(CriteriaQuery.class);
Predicate conjunctionPredicate = mock(Predicate.class);

Predicate inPredicate = mock(Predicate.class);
Path programIdPath = mock(Path.class);
when(programIdPath.in(programIds)).thenReturn(inPredicate);

Root root = mock(Root.class);
when(root.get(PROGRAM_ID)).thenReturn(programIdPath);
Predicate wherePredicate = mock(Predicate.class);

CriteriaBuilder criteriaBuilder = mock(CriteriaBuilder.class);
when(criteriaBuilder.createQuery(ValidReasonAssignment.class))
.thenReturn(query);
when(criteriaBuilder.conjunction()).thenReturn(conjunctionPredicate);
when(criteriaBuilder.and(conjunctionPredicate, inPredicate)).thenReturn(wherePredicate);

when(query.from(ValidReasonAssignment.class)).thenReturn(root);

when(entityManager.getCriteriaBuilder()).thenReturn(criteriaBuilder);
when(entityManager.createQuery(query))
.thenReturn(typedQuery);

//when
List<ValidReasonAssignment> searchResults =
repository.search(programIds, null, null, null);

//then
verify(entityManager).createQuery(query);
verify(query).where(wherePredicate);
assertThat(searchResults).isEqualTo(validReasonAssignmentList);
}

@Test
public void shouldSearchForFacilityTypeIdOnly() {
//given
UUID facilityTypeId = UUID.randomUUID();

List<ValidReasonAssignment> validReasonAssignmentList = mock(List.class);
TypedQuery typedQuery = mock(TypedQuery.class);
when(typedQuery.getResultList())
.thenReturn(validReasonAssignmentList);

CriteriaQuery query = mock(CriteriaQuery.class);
Predicate conjunctionPredicate = mock(Predicate.class);

Predicate equalPredicate = mock(Predicate.class);
Path facilityTypeIdPath = mock(Path.class);

Root root = mock(Root.class);
when(root.get(FACILITY_TYPE_ID)).thenReturn(facilityTypeIdPath);
Predicate wherePredicate = mock(Predicate.class);

CriteriaBuilder criteriaBuilder = mock(CriteriaBuilder.class);
when(criteriaBuilder.createQuery(ValidReasonAssignment.class))
.thenReturn(query);
when(criteriaBuilder.conjunction()).thenReturn(conjunctionPredicate);
when(criteriaBuilder.equal(facilityTypeIdPath, facilityTypeId)).thenReturn(equalPredicate);
when(criteriaBuilder.and(conjunctionPredicate, equalPredicate)).thenReturn(wherePredicate);

when(query.from(ValidReasonAssignment.class)).thenReturn(root);

when(entityManager.getCriteriaBuilder()).thenReturn(criteriaBuilder);
when(entityManager.createQuery(query))
.thenReturn(typedQuery);

//when
List<ValidReasonAssignment> searchResults =
repository.search(null, facilityTypeId, null, null);

//then
verify(entityManager).createQuery(query);
verify(query).where(wherePredicate);
assertThat(searchResults).isEqualTo(validReasonAssignmentList);
}
}
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
Loading