Skip to content

Commit

Permalink
OAM-191: adding unit of orderable id to identifiers (#54)
Browse files Browse the repository at this point in the history
* OAM-191: Changed orderable and lot identifying to contain also unit of orderable uuid.

* OAM-91: Merged two interfaces.

* OAM-91: Corrections made.

* OAM-191: Corrections made.

* OAM-191: Corrections made.

* OAM-191: Added index migrations for StockCard identity change

* OAM-191: Changed from OrderableLotUnit to OrderableLot.

* OAM-191: Fixed OrderableLotIdentity

* OAM-191: Corrected name.

* OAM-191: Fixed issues.

* OAM-191: Added running integration tests on build.

* OAM-191: Reverted lockjson changes.

* OAM-191: Added new integration test.

* Revert "OAM-191: Added new integration test."

This reverts commit 7990c1f.

* OAM-191: Added fix.

* OAM-191: Added coverage tests.

* OAM-191: Added coverage tests by moving test class form integration tests to unit tests.

* OAM-191: Optimized imports.

* OAM-191: Fixed test.

* OAM-191: Addded coverage tests and renamed test class.

---------

Co-authored-by: Piotr Wargulak <[email protected]>
  • Loading branch information
tsznaj and pwargulak committed Jun 13, 2024
1 parent e497730 commit 6bba589
Show file tree
Hide file tree
Showing 30 changed files with 647 additions and 224 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 @@ -78,6 +78,11 @@ StockCard generateInstance() throws Exception {
}

private StockCard generateInstance(UUID facility, UUID program, UUID product, UUID lot) {
return generateInstance(facility, program, product, lot, null);
}

private StockCard generateInstance(UUID facility, UUID program, UUID product, UUID lot,
UUID unitId) {
StockEvent event = new StockEventDataBuilder()
.withoutId()
.withFacility(facility)
Expand All @@ -96,6 +101,7 @@ private StockCard generateInstance(UUID facility, UUID program, UUID product, UU
.withoutId()
.withOrderableId(product)
.withLotId(lot)
.withUnitOfOrderableId(unitId)
.withLineItem(lineItem)
.withIsActive(true)
.build();
Expand All @@ -104,6 +110,19 @@ private StockCard generateInstance(UUID facility, UUID program, UUID product, UU
return stockCard;
}

@Test(expected = PersistenceException.class)
public void shouldNotAllowCreatingCardForTheSameFacilityProgramProductAndLotAndUnit()
throws Exception {
stockCard1 = generateInstance();
stockCard2 = generateInstance(stockCard1.getFacilityId(), stockCard1.getProgramId(),
stockCard1.getOrderableId(), stockCard1.getLotId(), stockCard1.getUnitOfOrderableId());

stockCardRepository.save(stockCard1);
stockCardRepository.save(stockCard2);

entityManager.flush();
}

@Test(expected = PersistenceException.class)
public void shouldNotAllowCreatingCardForTheSameFacilityProgramProductAndLot() throws Exception {
stockCard1 = generateInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import org.hibernate.annotations.LazyCollection;
import org.openlmis.stockmanagement.domain.BaseEntity;
import org.openlmis.stockmanagement.domain.event.StockEvent;
import org.openlmis.stockmanagement.domain.identity.IdentifiableByOrderableLot;
import org.openlmis.stockmanagement.domain.identity.IdentifiableByOrderableLotUnit;
import org.openlmis.stockmanagement.dto.StockEventDto;
import org.openlmis.stockmanagement.dto.StockEventLineItemDto;
import org.openlmis.stockmanagement.exception.ValidationMessageException;
Expand All @@ -56,8 +56,8 @@
import org.slf4j.LoggerFactory;

@NamedQuery(name = StockCard.QUERY_FIND_LOT_IDENT_BY_PROG_FACILITY,
query = "SELECT new org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity("
+ "s.orderableId, s.lotId)"
query = "SELECT new org.openlmis.stockmanagement.domain.identity.OrderableLotUnitIdentity("
+ "s.orderableId, s.lotId, s.unitOfOrderableId)"
+ " FROM StockCard s"
+ " WHERE s.programId = :" + StockCard.PARAM_PROGRAM_ID
+ " AND s.facilityId = :" + StockCard.PARAM_FACILITY_ID)
Expand All @@ -70,7 +70,7 @@
indexes = @Index(columnList = "facilityId,programId,orderableId"))
//the above line creates an index, it'll make select statements faster
//especially for getStockCardIdBy method of StockCardRepository
public class StockCard extends BaseEntity implements IdentifiableByOrderableLot {
public class StockCard extends BaseEntity implements IdentifiableByOrderableLotUnit {
public static final String QUERY_FIND_LOT_IDENT_BY_PROG_FACILITY = "StockCard"
+ ".findLotIdentByProgFacility";
public static final String PARAM_PROGRAM_ID = "programId";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.openlmis.stockmanagement.domain.BaseEntity;
import org.openlmis.stockmanagement.domain.ExtraDataConverter;
import org.openlmis.stockmanagement.domain.common.VvmApplicable;
import org.openlmis.stockmanagement.domain.identity.IdentifiableByOrderableLot;
import org.openlmis.stockmanagement.domain.identity.IdentifiableByOrderableLotUnit;
import org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventoryLineItemAdjustment;

@Data
Expand All @@ -47,7 +47,7 @@
@Entity
@Table(name = "stock_event_line_items", schema = "stockmanagement")
public class StockEventLineItem extends BaseEntity
implements IdentifiableByOrderableLot, VvmApplicable {
implements IdentifiableByOrderableLotUnit, VvmApplicable {

@Column(nullable = false)
private UUID orderableId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

import java.util.UUID;

public interface IdentifiableByOrderableLot {
public interface IdentifiableByOrderableLotUnit {

UUID getOrderableId();

UUID getLotId();

UUID getUnitOfOrderableId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,32 @@
import lombok.Getter;
import lombok.Setter;

/**
* Class acting as identity where triple (orderableId, lotId, unitOfOrderableId) is unique.
*/
@Getter
@Setter
public class OrderableLotIdentity {
public class OrderableLotUnitIdentity {
private UUID orderableId;
private UUID lotId;
private UUID unitOfOrderableId;

public OrderableLotIdentity(UUID orderableId, UUID lotId) {
/**
* Constructor for OrderableLotUnitIdentity.
*
* @param orderableId orderable id
* @param lotId lot id
* @param unitOfOrderableId unit of orderable id
*/
public OrderableLotUnitIdentity(UUID orderableId, UUID lotId, UUID unitOfOrderableId) {
this.orderableId = orderableId;
this.lotId = lotId;
this.unitOfOrderableId = unitOfOrderableId;
}

public static OrderableLotUnitIdentity identityOf(IdentifiableByOrderableLotUnit identifiable) {
return new OrderableLotUnitIdentity(identifiable.getOrderableId(), identifiable.getLotId(),
identifiable.getUnitOfOrderableId());
}

@Override
Expand All @@ -39,22 +56,23 @@ public boolean equals(Object object) {
return false;
}

OrderableLotIdentity that = (OrderableLotIdentity) object;
OrderableLotUnitIdentity that = (OrderableLotUnitIdentity) object;

if (orderableId != null ? !orderableId.equals(that.orderableId) : that.orderableId != null) {
return false;
}
return lotId != null ? lotId.equals(that.lotId) : that.lotId == null;
if (lotId != null ? !lotId.equals(that.lotId) : that.lotId != null) {
return false;
}
return unitOfOrderableId == null ? that.unitOfOrderableId == null :
unitOfOrderableId.equals(that.unitOfOrderableId);
}

@Override
public int hashCode() {
int result = orderableId != null ? orderableId.hashCode() : 0;
result = 31 * result + (lotId != null ? lotId.hashCode() : 0);
result = 31 * result + (unitOfOrderableId != null ? unitOfOrderableId.hashCode() : 0);
return result;
}

public static OrderableLotIdentity identityOf(IdentifiableByOrderableLot identifiable) {
return new OrderableLotIdentity(identifiable.getOrderableId(), identifiable.getLotId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.openlmis.stockmanagement.domain.BaseEntity;
import org.openlmis.stockmanagement.domain.ExtraDataConverter;
import org.openlmis.stockmanagement.domain.common.VvmApplicable;
import org.openlmis.stockmanagement.domain.identity.IdentifiableByOrderableLot;
import org.openlmis.stockmanagement.domain.identity.IdentifiableByOrderableLotUnit;

@Entity
@Data
Expand All @@ -45,7 +45,7 @@
@Table(name = "physical_inventory_line_items", schema = "stockmanagement")
public class PhysicalInventoryLineItem
extends BaseEntity
implements VvmApplicable, IdentifiableByOrderableLot {
implements VvmApplicable, IdentifiableByOrderableLotUnit {
@Column(nullable = false)
private UUID orderableId;
@Column
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;
import org.openlmis.stockmanagement.domain.common.VvmApplicable;
import org.openlmis.stockmanagement.domain.identity.IdentifiableByOrderableLot;
import org.openlmis.stockmanagement.domain.identity.IdentifiableByOrderableLotUnit;
import org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory;
import org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventoryLineItem;
import org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventoryLineItemAdjustment;
Expand All @@ -33,7 +33,7 @@
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class PhysicalInventoryLineItemDto implements IdentifiableByOrderableLot, VvmApplicable {
public class PhysicalInventoryLineItemDto implements IdentifiableByOrderableLotUnit, VvmApplicable {
private UUID orderableId;
private UUID lotId;
private UUID unitOfOrderableId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import lombok.Setter;
import lombok.ToString;
import org.openlmis.stockmanagement.domain.card.StockCard;
import org.openlmis.stockmanagement.domain.identity.IdentifiableByOrderableLot;
import org.openlmis.stockmanagement.domain.identity.IdentifiableByOrderableLotUnit;
import org.openlmis.stockmanagement.dto.referencedata.FacilityDto;
import org.openlmis.stockmanagement.dto.referencedata.LotDto;
import org.openlmis.stockmanagement.dto.referencedata.OrderableDto;
Expand All @@ -47,7 +47,7 @@
@Setter
@ToString
@EqualsAndHashCode
public final class StockCardDto implements IdentifiableByOrderableLot {
public final class StockCardDto implements IdentifiableByOrderableLotUnit {

@JsonInclude(NON_NULL)
private UUID id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
import lombok.Setter;
import org.openlmis.stockmanagement.domain.common.VvmApplicable;
import org.openlmis.stockmanagement.domain.event.StockEventLineItem;
import org.openlmis.stockmanagement.domain.identity.IdentifiableByOrderableLot;
import org.openlmis.stockmanagement.domain.identity.IdentifiableByOrderableLotUnit;
import org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventoryLineItemAdjustment;

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class StockEventLineItemDto implements IdentifiableByOrderableLot, VvmApplicable {
public class StockEventLineItemDto implements IdentifiableByOrderableLotUnit, VvmApplicable {
private UUID orderableId;
private UUID lotId;
private Integer quantity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.UUID;
import org.openlmis.stockmanagement.domain.card.StockCard;
import org.openlmis.stockmanagement.domain.event.StockEvent;
import org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity;
import org.openlmis.stockmanagement.domain.identity.OrderableLotUnitIdentity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
Expand Down Expand Up @@ -50,8 +50,9 @@ List<StockCard> findByOrderableIdInAndProgramIdAndFacilityId(
StockCard findByOriginEvent(@Param("originEventId") StockEvent stockEvent);

@Query(name = StockCard.QUERY_FIND_LOT_IDENT_BY_PROG_FACILITY)
List<OrderableLotIdentity> getIdentitiesBy(@Param(StockCard.PARAM_PROGRAM_ID) UUID programId,
@Param(StockCard.PARAM_FACILITY_ID) UUID facilityId);
List<OrderableLotUnitIdentity> getIdentitiesBy(@Param(StockCard.PARAM_PROGRAM_ID) UUID programId,
@Param(StockCard.PARAM_FACILITY_ID)
UUID facilityId);

Page<StockCard> findByFacilityIdInAndProgramIdInAndIdIn(Collection<UUID> facilityIds,
Collection<UUID> programIds, Collection<UUID> ids, Pageable pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.util.UUID;
import org.openlmis.stockmanagement.domain.card.StockCard;
import org.openlmis.stockmanagement.domain.event.StockEvent;
import org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity;
import org.openlmis.stockmanagement.domain.identity.OrderableLotUnitIdentity;
import org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventory;
import org.openlmis.stockmanagement.domain.physicalinventory.PhysicalInventoryLineItem;
import org.openlmis.stockmanagement.dto.PhysicalInventoryDto;
Expand Down Expand Up @@ -167,13 +167,13 @@ void submitPhysicalInventory(PhysicalInventoryDto inventoryDto, UUID eventId) {
inventory.setStockEvent(event);
}

Map<OrderableLotIdentity, StockCard> cards = calculatedStockOnHandService
Map<OrderableLotUnitIdentity, StockCard> cards = calculatedStockOnHandService
.getStockCardsWithStockOnHand(inventory.getProgramId(), inventory.getFacilityId())
.stream()
.collect(toMap(OrderableLotIdentity::identityOf, card -> card));
.collect(toMap(OrderableLotUnitIdentity::identityOf, card -> card));

for (PhysicalInventoryLineItem line : inventory.getLineItems()) {
StockCard stockCard = cards.get(OrderableLotIdentity.identityOf(line));
StockCard stockCard = cards.get(OrderableLotUnitIdentity.identityOf(line));
if (stockCard != null) {
line.setPreviousStockOnHandWhenSubmitted(stockCard.getStockOnHand());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ private StockCardDto cardToDto(FacilityDto facility, ProgramDto program,
if (card.getLotId() != null) {
cardDto.setLot(LotDto.builder().id(card.getLotId()).build());
}
cardDto.setUnitOfOrderableId(card.getUnitOfOrderableId());
List<StockCardLineItemDto> lineItems = cardDto.getLineItems();
if (!isEmpty(lineItems)) {
cardDto.setLastUpdate(card.getOccurredDate());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import static org.apache.commons.collections4.CollectionUtils.isEmpty;
import static org.openlmis.stockmanagement.domain.card.StockCard.createStockCardFrom;
import static org.openlmis.stockmanagement.domain.card.StockCardLineItem.createLineItemFrom;
import static org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity.identityOf;
import static org.openlmis.stockmanagement.domain.identity.OrderableLotUnitIdentity.identityOf;
import static org.openlmis.stockmanagement.domain.reason.ReasonCategory.PHYSICAL_INVENTORY;
import static org.openlmis.stockmanagement.service.PermissionService.STOCK_CARDS_VIEW;

Expand All @@ -36,7 +36,7 @@
import javax.validation.constraints.NotNull;
import org.openlmis.stockmanagement.domain.card.StockCard;
import org.openlmis.stockmanagement.domain.card.StockCardLineItem;
import org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity;
import org.openlmis.stockmanagement.domain.identity.OrderableLotUnitIdentity;
import org.openlmis.stockmanagement.domain.sourcedestination.Node;
import org.openlmis.stockmanagement.domain.sourcedestination.Organization;
import org.openlmis.stockmanagement.dto.StockCardDto;
Expand Down Expand Up @@ -238,7 +238,7 @@ private List<StockCardLineItem> getSavedButNewLineItems(List<StockCard> cardsToU

private StockCard findOrCreateCard(StockEventDto eventDto, StockEventLineItemDto eventLineItem,
UUID savedEventId, List<StockCard> cardsToUpdate) {
OrderableLotIdentity identity = identityOf(eventLineItem);
OrderableLotUnitIdentity identity = identityOf(eventLineItem);
StockCard card = eventDto.getContext().findCard(identity);

if (null == card) {
Expand Down
Loading

0 comments on commit 6bba589

Please sign in to comment.