Skip to content

Commit

Permalink
OLMIS-7952: Copied and adjusted solutions for 2nd approval for requis…
Browse files Browse the repository at this point in the history
…ition to make application logging performance better, commits: 6da4960 and 87e17c9. (#63)

* OLMIS-7952: Copied and adjusted solutions for 2nd approval for requisition to make application logging performance better, commits: 6da4960 and 87e17c9.

* OLMIS-7952: Removed TODO.

* OLMIS-7952: Style corrections.

* OLMIS-7952: Added integration testing when building.

* OLMIS-7952: Fixed indentations.

* OLMIS-7952: Added test coverage and corrected method description.

* OLMIS-7952: Added another test coverage.

* OLMIS-7952: Added another test coverage.

---------

Co-authored-by: pwargulak <[email protected]>
  • Loading branch information
tsznaj and pwargulak authored Jul 9, 2024
1 parent f00e4aa commit 37f3d27
Show file tree
Hide file tree
Showing 16 changed files with 407 additions and 139 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
--source-file src/main/resources/messages_en.properties

# Run Gradle build
gradle clean build
gradle clean build integrationTest
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

import org.junit.Test;
import org.openlmis.stockmanagement.domain.card.StockCard;
import org.openlmis.stockmanagement.domain.event.CalculatedStockOnHand;
Expand Down Expand Up @@ -61,18 +60,18 @@ CalculatedStockOnHand generateInstance() {
private CalculatedStockOnHand generateInstance(UUID facility, UUID program, UUID product,
UUID lot) {
StockEvent event = new StockEventDataBuilder()
.withoutId()
.withFacility(facility)
.withProgram(program)
.build();
.withoutId()
.withFacility(facility)
.withProgram(program)
.build();

event = stockEventsRepository.save(event);

StockCard stockCard = new StockCardDataBuilder(event)
.withoutId()
.withOrderable(product)
.withLot(lot)
.build();
.withoutId()
.withOrderableId(product)
.withLotId(lot)
.build();

stockCard = stockCardRepository.save(stockCard);

Expand All @@ -97,42 +96,42 @@ public void shouldReturnCalculatedStockOnHandsWhenNoStartDateProvided() {
calculatedStockOnHandRepository.save(calculatedStockOnHand3);

List<CalculatedStockOnHand> resultList1 = calculatedStockOnHandRepository
.findByStockCardIdInAndOccurredDateLessThanEqual(
asList(calculatedStockOnHand1.getStockCard().getId(),
calculatedStockOnHand2.getStockCard().getId(),
calculatedStockOnHand3.getStockCard().getId()),
LocalDate.of(2010, 11, 1));
.findByStockCardIdInAndOccurredDateLessThanEqual(
asList(calculatedStockOnHand1.getStockCard().getId(),
calculatedStockOnHand2.getStockCard().getId(),
calculatedStockOnHand3.getStockCard().getId()),
LocalDate.of(2010, 11, 1));

assertThat(resultList1, hasItems(calculatedStockOnHand1,
calculatedStockOnHand2, calculatedStockOnHand3));
calculatedStockOnHand2, calculatedStockOnHand3));
assertEquals(resultList1.size(), 3);

List<CalculatedStockOnHand> resultList2 = calculatedStockOnHandRepository
.findByStockCardIdInAndOccurredDateLessThanEqual(
asList(calculatedStockOnHand1.getStockCard().getId(),
calculatedStockOnHand2.getStockCard().getId(),
calculatedStockOnHand3.getStockCard().getId()),
LocalDate.of(2010, 9, 15));
.findByStockCardIdInAndOccurredDateLessThanEqual(
asList(calculatedStockOnHand1.getStockCard().getId(),
calculatedStockOnHand2.getStockCard().getId(),
calculatedStockOnHand3.getStockCard().getId()),
LocalDate.of(2010, 9, 15));

assertThat(resultList2, hasItems(calculatedStockOnHand1, calculatedStockOnHand2));
assertEquals(resultList2.size(), 2);

List<CalculatedStockOnHand> resultList3 = calculatedStockOnHandRepository
.findByStockCardIdInAndOccurredDateLessThanEqual(
asList(calculatedStockOnHand1.getStockCard().getId(),
calculatedStockOnHand2.getStockCard().getId(),
calculatedStockOnHand3.getStockCard().getId()),
LocalDate.of(2010, 8, 15));
.findByStockCardIdInAndOccurredDateLessThanEqual(
asList(calculatedStockOnHand1.getStockCard().getId(),
calculatedStockOnHand2.getStockCard().getId(),
calculatedStockOnHand3.getStockCard().getId()),
LocalDate.of(2010, 8, 15));

assertThat(resultList3, hasItems(calculatedStockOnHand1));
assertEquals(resultList3.size(), 1);

List<CalculatedStockOnHand> resultList4 = calculatedStockOnHandRepository
.findByStockCardIdInAndOccurredDateLessThanEqual(
asList(calculatedStockOnHand1.getStockCard().getId(),
calculatedStockOnHand2.getStockCard().getId(),
calculatedStockOnHand3.getStockCard().getId()),
LocalDate.of(2010, 4, 15));
.findByStockCardIdInAndOccurredDateLessThanEqual(
asList(calculatedStockOnHand1.getStockCard().getId(),
calculatedStockOnHand2.getStockCard().getId(),
calculatedStockOnHand3.getStockCard().getId()),
LocalDate.of(2010, 4, 15));

assertTrue(resultList4.isEmpty());
assertEquals(resultList4.size(), 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ private StockCard generateInstance(UUID facility, UUID program, UUID product, UU

StockCard stockCard = new StockCardDataBuilder(event)
.withoutId()
.withOrderable(product)
.withLot(lot)
.withOrderableId(product)
.withLotId(lot)
.withLineItem(lineItem)
.withIsActive(true)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,8 @@ private StockEvent prepareEvent(UUID facility, UUID program) {
private StockCard prepareStockCard(StockEvent event, UUID product, UUID lot) {
StockCard result = new StockCardDataBuilder(event)
.withoutId()
.withOrderable(product)
.withLot(lot)
.withOrderableId(product)
.withLotId(lot)
.build();
return stockCardRepository.save(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.openlmis.stockmanagement.service.referencedata.OrderableReferenceDataService;
import org.openlmis.stockmanagement.service.referencedata.ProgramReferenceDataService;
import org.openlmis.stockmanagement.testutils.StockEventDataBuilder;
import org.slf4j.profiler.Profiler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
Expand Down Expand Up @@ -115,11 +116,9 @@ public void shouldFindExistingStockCards()
OrderableDto orderable3 = createOrderableDto(orderable3Id, "");
OrderableDto orderable4 = createOrderableDto(orderable4Id, "");

when(orderableReferenceDataService
.findAll())
when(orderableReferenceDataService.findByIds(any()))
.thenReturn(asList(orderable1, orderable2, orderable3, orderable4));
when(lotReferenceDataService.getAllLotsOf(any(UUID.class)))
.thenReturn(emptyList());
when(lotReferenceDataService.findByIds(any())).thenReturn(emptyList());
when(facilityReferenceDataService.findOne(any(UUID.class)))
.thenReturn(new FacilityDto());
when(programReferenceDataService.findOne(any(UUID.class)))
Expand Down Expand Up @@ -169,7 +168,7 @@ public void shouldReturnPageOfStockCards() throws Exception {
PageRequest pageRequest = PageRequest.of(0, 1);
//when
Page<StockCardDto> stockCards = stockCardSummariesService
.findStockCards(programId, facilityId, pageRequest);
.findStockCards(programId, facilityId, pageRequest, new Profiler("TEST"));

//then
assertThat(stockCards.getContent().size(), is(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
Expand All @@ -32,7 +33,6 @@

import com.google.common.collect.ImmutableSet;
import java.util.UUID;

import org.junit.Test;
import org.openlmis.stockmanagement.dto.StockCardDto;
import org.openlmis.stockmanagement.exception.PermissionMessageException;
Expand All @@ -42,6 +42,7 @@
import org.openlmis.stockmanagement.service.StockCardSummariesService;
import org.openlmis.stockmanagement.testutils.StockCardDtoDataBuilder;
import org.openlmis.stockmanagement.util.Message;
import org.slf4j.profiler.Profiler;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -146,7 +147,7 @@ public void shouldGetPagedStockCardSummariesWhenPermissionIsGranted() throws Exc

PageRequest pageable = PageRequest.of(0, 20);
when(stockCardSummariesService
.findStockCards(programId, facilityId, pageable))
.findStockCards(eq(programId), eq(facilityId), eq(pageable), any(Profiler.class)))
.thenReturn(new PageImpl<>(singletonList(StockCardDtoDataBuilder.createStockCardDto())));

ResultActions resultActions = mvc.perform(
Expand Down Expand Up @@ -215,4 +216,4 @@ public void shouldReturn404WhenStockCardNotFoundWhileMakingInactive() throws Exc
// then
resultActions.andExpect(status().isNotFound());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.openlmis.stockmanagement.domain.BaseEntity;

@Entity
Expand Down Expand Up @@ -54,6 +55,7 @@ public class StockEvent extends BaseEntity {

private String documentNumber;

@ToString.Exclude
@OneToMany(cascade = ALL, mappedBy = "stockEvent")
private List<StockEventLineItem> lineItems;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

@Service
public class HomeFacilityPermissionService {

static final String WS_TYPE_CODE = "WS";

@Autowired
private AuthenticationHelper authenticationHelper;

Expand All @@ -54,7 +57,28 @@ public void checkProgramSupported(UUID programId) {
}
}

/**
* Returns true if facility is within the same geographic zone as the home facility.
* Returns false otherwise or in case facility id is equal to home facility id.
*
* @param facilityId UUID of facility
* @return boolean flag indicating linkage between facility and home facility
*/
public boolean checkFacilityAndHomeFacilityLinkage(UUID facilityId) {
UUID homeFacilityId = authenticationHelper.getCurrentUser().getHomeFacilityId();
if (facilityId.equals(homeFacilityId)) {
return false;
}
FacilityDto facility = facilityService.findOne(facilityId);
if (facility.getType().getCode().equals(WS_TYPE_CODE)) {
FacilityDto homeFacility = facilityService.findOne(homeFacilityId);
return homeFacility.getGeographicZone().getId().equals(facility.getGeographicZone().getId());
} else {
return false;
}
}

private void throwException(String errorKey, String... params) {
throw new PermissionMessageException(new Message(errorKey, (Object)params));
}
}
}
Loading

0 comments on commit 37f3d27

Please sign in to comment.