From c25e41f54258c1e5fb3e7a83d296b305e36ac1c5 Mon Sep 17 00:00:00 2001 From: Ernst-Christoph Schrewe Date: Mon, 29 Jan 2024 11:24:01 +0100 Subject: [PATCH 1/2] refactor: initial commit --- .../MaterialPartnerRelationRepository.java | 9 + .../MaterialPartnerRelationService.java | 8 + .../MaterialPartnerRelationServiceImpl.java | 20 ++ .../logic/service/MaterialService.java | 65 +++++- .../logic/service/MaterialServiceImpl.java | 132 +++++++++++- .../logic/adapter/ItemStockSammMapper.java | 98 +-------- .../masterdata/logic/MaterialServiceTest.java | 195 ++++++++++++++++++ 7 files changed, 422 insertions(+), 105 deletions(-) diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/MaterialPartnerRelationRepository.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/MaterialPartnerRelationRepository.java index 5cdac5b4..62c6c70b 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/MaterialPartnerRelationRepository.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/domain/repository/MaterialPartnerRelationRepository.java @@ -22,6 +22,7 @@ package org.eclipse.tractusx.puris.backend.masterdata.domain.repository; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.MaterialPartnerRelation; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -44,4 +45,12 @@ public interface MaterialPartnerRelationRepository extends JpaRepository findAllByMaterial_OwnMaterialNumberAndPartnerBuysMaterialIsTrue(String ownMaterialNumber); List findAllByPartnerMaterialNumber(String partnerMaterialNumber); + + List findAllByPartnerMaterialNumberAndPartnerSuppliesMaterialIsTrue(String partnerMaterialNumber); + + List findAllByPartnerMaterialNumberAndPartnerBuysMaterialIsTrue(String partnerMaterialNumber); + + List findAllByPartnerAndPartnerMaterialNumberAndPartnerSuppliesMaterialIsTrue(Partner partner, String partnerMaterialNumber); + + List findAllByPartnerAndPartnerMaterialNumberAndPartnerBuysMaterialIsTrue(Partner partner, String partnerMaterialNumber); } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationService.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationService.java index 80871952..f8a34cd7 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationService.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationService.java @@ -58,4 +58,12 @@ public interface MaterialPartnerRelationService { boolean partnerSuppliesMaterial(Material material, Partner partner); boolean partnerOrdersProduct(Material material, Partner partner); + + List findAllBySupplierPartnerMaterialNumber(String partnerMaterialNumber); + + List findAllByCustomerPartnerMaterialNumber(String partnerMaterialNumber); + + List findAllBySupplierPartnerAndPartnerMaterialNumber(Partner partner, String partnerMaterialNumber); + + List findAllByCustomerPartnerAndPartnerMaterialNumber(Partner partner, String partnerMaterialNumber); } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationServiceImpl.java index c437f165..e68420ca 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationServiceImpl.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialPartnerRelationServiceImpl.java @@ -268,4 +268,24 @@ public boolean partnerOrdersProduct(Material material, Partner partner) { return false; } + @Override + public List findAllBySupplierPartnerMaterialNumber(String partnerMaterialNumber) { + return mprRepository.findAllByPartnerMaterialNumberAndPartnerSuppliesMaterialIsTrue(partnerMaterialNumber); + } + + @Override + public List findAllByCustomerPartnerMaterialNumber(String partnerMaterialNumber) { + return mprRepository.findAllByPartnerMaterialNumberAndPartnerBuysMaterialIsTrue(partnerMaterialNumber); + } + + @Override + public List findAllBySupplierPartnerAndPartnerMaterialNumber(Partner partner, String partnerMaterialNumber) { + return mprRepository.findAllByPartnerAndPartnerMaterialNumberAndPartnerSuppliesMaterialIsTrue(partner, partnerMaterialNumber); + } + + @Override + public List findAllByCustomerPartnerAndPartnerMaterialNumber(Partner partner, String partnerMaterialNumber) { + return mprRepository.findAllByPartnerAndPartnerMaterialNumberAndPartnerBuysMaterialIsTrue(partner, partnerMaterialNumber); + } + } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialService.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialService.java index 96afb531..ddcff5e0 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialService.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialService.java @@ -22,6 +22,7 @@ package org.eclipse.tractusx.puris.backend.masterdata.logic.service; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; import java.util.List; @@ -35,18 +36,68 @@ public interface MaterialService { List findAllProducts(); - - -// Material findByUuid(UUID materialUuid); - Material findByOwnMaterialNumber(String ownMaterialNumber); Material findByMaterialNumberCx(String materialNumberCx); List findAll(); -// Material findMaterialByMaterialNumberCustomer(String materialNumberCustomer); -// -// Material findProductByMaterialNumberCustomer(String materialNumberCustomer); + /** + * This method will do a best effort attempt to return a Material Entity for the given input arguments. + * All arguments are potentially nullable. But the more arguments you provide, the better. + * + * The material will be selected + *
  • by the materialNumberCx, if possible
  • + *
  • otherwise by the customerMaterialNumber, if possible
  • + *
  • otherwise by the supplierMaterialNumber, if possible
  • + * + * If a materialNumberCx is given and there exists a Material Entity that matches, this Material will + * always be chosen. + * + * Otherwise, an attempt will be made to find a matching Material by the other arguments. In this case, + * if a specific partner is given as an argument, a matching supplierMaterialNumber will only be considered, + * if this is actually this partner's material number. + * + * This method will write warnings to the log, if mismatches or ambiguities were found. + * + * Since this method is only meant to be used from a Customer's Perspective, the customerMaterialNumber will + * be treated as ownMaterialNumber. + * + * @param materialNumberCx the CatenaX - MaterialNumber + * @param customerMatNbr the MaterialNumber on the customer's side + * @param supplierMatNbr the MaterialNumber on the supplier's side + * @param partner the Partner + * @return the Material, or null if no matching material was found + */ + Material findFromCustomerPerspective(String materialNumberCx, String customerMatNbr, String supplierMatNbr, Partner partner); + + /** + * This method will do a best effort attempt to return a Material Entity for the given input arguments. + * All arguments are potentially nullable. But the more arguments you provide, the better. + * + * The material will be selected + *
  • by the materialNumberCx, if possible
  • + *
  • otherwise by the customerMaterialNumber, if possible
  • + *
  • otherwise by the supplierMaterialNumber, if possible
  • + * + * If a materialNumberCx is given and there exists a Material Entity that matches, this Material will + * always be chosen. + * + * Otherwise, an attempt will be made to find a matching Material by the other arguments. In this case, + * if a specific partner is given as an argument, a matching customerMaterialNumber will only be considered, + * if this is actually this partner's material number. + * + * This method will write warnings to the log, if mismatches or ambiguities were found. + * + * Since this method is only meant to be used from a Supplier's Perspective, the supplierMaterialNumber will + * be treated as ownMaterialNumber. + * + * @param materialNumberCx the CatenaX - MaterialNumber + * @param customerMatNbr the MaterialNumber on the customer's side + * @param supplierMatNbr the MaterialNumber on the supplier's side + * @param partner the Partner + * @return the Material, or null if no matching material was found + */ + Material findFromSupplierPerspective(String materialNumberCx, String customerMatNbr, String supplierMatNbr, Partner partner); } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialServiceImpl.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialServiceImpl.java index 08d1fb09..071b7a47 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialServiceImpl.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/masterdata/logic/service/MaterialServiceImpl.java @@ -23,6 +23,8 @@ import lombok.extern.slf4j.Slf4j; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.MaterialPartnerRelation; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; import org.eclipse.tractusx.puris.backend.masterdata.domain.repository.MaterialRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -35,7 +37,10 @@ public class MaterialServiceImpl implements MaterialService { @Autowired - MaterialRepository materialRepository; + private MaterialRepository materialRepository; + + @Autowired + private MaterialPartnerRelationService mprService; @Override public Material create(Material material) { @@ -50,7 +55,7 @@ public Material create(Material material) { @Override public Material update(Material material) { Optional existingMaterial = - materialRepository.findById(material.getOwnMaterialNumber()); + materialRepository.findById(material.getOwnMaterialNumber()); if (existingMaterial.isPresent()) { return existingMaterial.get(); } @@ -95,4 +100,127 @@ public List findAll() { return materialRepository.findAll(); } + @Override + public Material findFromCustomerPerspective(String materialNumberCx, String customerMatNbr, String supplierMatNbr, Partner partner) { + Material material = null; + if (materialNumberCx != null) { + // Use Material Number CX + material = findByMaterialNumberCx(materialNumberCx); + if (material != null) { + if (customerMatNbr != null && !material.equals(findByOwnMaterialNumber(customerMatNbr))) { + log.warn("Mismatch between " + material + " and " + customerMatNbr); + } + if (material != null && partner != null && !mprService.partnerSuppliesMaterial(material, partner)) { + log.warn("Partner " + partner + " does not supply material " + material); + } + } + + } + if (material == null && customerMatNbr != null) { + // If previous effort yielded not result, use Customer Material Number + material = findByOwnMaterialNumber(customerMatNbr); + if (material != null && materialNumberCx != null) { + log.warn("Unknown Material Number CX " + materialNumberCx + " for Material " + material); + } + if (material != null && partner != null && !mprService.partnerSuppliesMaterial(material, partner)) { + log.warn("Partner " + partner + " does not supply material " + material); + } + } + if (material == null && supplierMatNbr != null) { + // If previous effort yielded not result, use Supplier Material Number + List materialList; + if (partner != null) { + materialList = mprService.findAllBySupplierPartnerAndPartnerMaterialNumber(partner, supplierMatNbr) + .stream() + .map(MaterialPartnerRelation::getMaterial) + .toList(); + } else { + materialList = mprService.findAllBySupplierPartnerMaterialNumber(supplierMatNbr) + .stream() + .map(MaterialPartnerRelation::getMaterial) + .toList(); + } + + if (!materialList.isEmpty()) { + material = materialList.get(0); + } + if (materialList.size() > 1) { + log.warn("Ambiguous results for supplier partner Material Number " + supplierMatNbr + ", arbitrarily choosing " + material); + } + if (material != null) { + if (materialNumberCx != null) { + log.warn("Unknown Material Number CX " + materialNumberCx + " for Material " + material); + } + if (customerMatNbr != null) { + log.warn("Unknown Customer Material Number " + customerMatNbr + " for Material " + material); + } + } + } + + return material; + } + + @Override + public Material findFromSupplierPerspective(String materialNumberCx, String customerMatNbr, String supplierMatNbr, Partner partner) { + Material material = null; + if (materialNumberCx != null) { + // Use Material Number CX + material = findByMaterialNumberCx(materialNumberCx); + if (material != null) { + if (supplierMatNbr != null && !material.equals(findByOwnMaterialNumber(supplierMatNbr))) { + log.warn("Mismatch between " + material + " and " + supplierMatNbr); + } + if(partner != null && ! mprService.partnerOrdersProduct(material, partner)) { + log.warn("Partner " + partner + " does not order material " + material); + } + } + } + if (material == null && customerMatNbr != null) { + // If previous effort yielded not result, use Customer Material Number + List materialList; + if (partner != null) { + materialList = mprService.findAllByCustomerPartnerAndPartnerMaterialNumber(partner, customerMatNbr) + .stream() + .map(MaterialPartnerRelation::getMaterial) + .toList(); + } else { + materialList = mprService.findAllByCustomerPartnerMaterialNumber(customerMatNbr) + .stream() + .map(MaterialPartnerRelation::getMaterial) + .toList(); + } + + if (!materialList.isEmpty()) { + material = materialList.get(0); + } + if (materialList.size() > 1) { + log.warn("Ambiguous results for customer partner Material Number " + customerMatNbr + ", arbitrarily choosing " + material); + } + if (material != null) { + if (materialNumberCx != null) { + log.warn("Unknown Material Number CX " + materialNumberCx + " for Material " + material); + } + if (supplierMatNbr != null && !material.equals(findByOwnMaterialNumber(supplierMatNbr))) { + log.warn("Mismatch between OwnMaterialNumber " + supplierMatNbr + " and " + material); + } + } + } + if (material == null && supplierMatNbr != null) { + // If previous effort yielded not result, use Supplier Material Number + material = findByOwnMaterialNumber(supplierMatNbr); + if (material != null) { + if (materialNumberCx != null) { + log.warn("Unknown Material Number CX " + materialNumberCx + " for Material " + material); + } + if (customerMatNbr != null) { + log.warn("Unknown customer Material Number " + customerMatNbr + " for Material " + material); + } + if(partner != null && ! mprService.partnerOrdersProduct(material, partner)) { + log.warn("Partner " + partner + " does not order material " + material); + } + } + } + return material; + } + } diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ItemStockSammMapper.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ItemStockSammMapper.java index e1ded015..b71bb1ac 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ItemStockSammMapper.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ItemStockSammMapper.java @@ -163,53 +163,7 @@ public List itemStockSammToReportedProductItemStock(It log.warn("Direction should be INBOUND, aborting"); return outputList; } - Material material = null; - // Use CatenaXNbr - if (matNbrCatenaX != null) { - material = materialService.findByMaterialNumberCx(matNbrCatenaX); - if (material != null) { - if (!material.getOwnMaterialNumber().equals(matNbrSupplier)) { - log.warn("Mismatch between CatenaX Number " + matNbrCatenaX + " and ownMaterialNumber " + matNbrSupplier); - } - var mpr = mprService.find(material, partner); - if (mpr == null) { - log.warn("Missing MaterialPartnerRelation for " + material.getOwnMaterialNumber() + " and Partner " + partner.getBpnl()); - } else { - if (!mpr.getPartnerMaterialNumber().equals(matNbrCustomer)) { - log.warn("Mismatch for MaterialNumberCustomer " + matNbrCustomer + " and " + material.getOwnMaterialNumber()); - } - } - } - } - // Use MatNbrCustomer - if (material == null && matNbrCustomer != null) { - var list = mprService.findAllByPartnerMaterialNumber(matNbrCustomer).stream().filter(m -> { - var mpr = mprService.find(m, partner); - return mpr != null && mpr.isPartnerBuysMaterial(); - }).toList(); - if (!list.isEmpty()) { - material = list.get(0); - if (list.size() > 1) { - log.warn("CustomerMaterialNumber " + matNbrCustomer + " is ambiguous, arbitrarily choosing " + material.getOwnMaterialNumber()); - } - } - } - // Use MatNbrSupplier - if (material == null && matNbrSupplier != null) { - material = materialService.findByOwnMaterialNumber(matNbrSupplier); - - if (material != null) { - if (matNbrCatenaX != null) { - log.warn("Unknown CatenaXNumber for Material " + material.getOwnMaterialNumber()); - } - var mpr = mprService.find(material, partner); - if (mpr != null) { - if (!mpr.getPartnerMaterialNumber().equals(matNbrCustomer)) { - log.warn("Unknown MaterialNumberCustomer " + matNbrCustomer + " for Material " + material.getOwnMaterialNumber()); - } - } - } - } + Material material = materialService.findFromSupplierPerspective(matNbrCatenaX, matNbrCustomer, matNbrSupplier, partner); if (material == null) { log.warn("Could not identify material with CatenaXNbr " + matNbrCatenaX + " ,CustomerMaterialNbr " + matNbrCustomer + " and SupplierMaterialNbr " + matNbrSupplier); return outputList; @@ -252,55 +206,7 @@ public List itemStockSammToReportedMaterialItemStock( log.warn("Direction should be OUTBOUND, aborting"); return outputList; } - Material material = null; - // Use MatNbrCatenaX - if (matNbrCatenaX != null) { - material = materialService.findByMaterialNumberCx(matNbrCatenaX); - if (material != null) { - if (!material.getOwnMaterialNumber().equals(matNbrCustomer)) { - log.warn("Mismatch between CatenaX Number " + matNbrCatenaX + " and ownMaterialNumber " + matNbrCustomer); - } - var mpr = mprService.find(material, partner); - if (mpr == null) { - log.warn("Missing MaterialPartnerRelation for " + material.getOwnMaterialNumber() + " and Partner " + partner.getBpnl()); - } else { - if (!mpr.getPartnerMaterialNumber().equals(matNbrSupplier)) { - log.warn("Mismatch for MaterialNumberSupplier " + matNbrSupplier + " and " + material.getOwnMaterialNumber()); - } - } - } - } - // Use MatNbrCustomer - if (material == null && matNbrCustomer != null) { - material = materialService.findByOwnMaterialNumber(matNbrCustomer); - if (material != null) { - if (matNbrCatenaX != null) { - log.warn("Unknown CatenaXNumber for Material " + material.getOwnMaterialNumber()); - } - var mpr = mprService.find(material, partner); - if (mpr != null) { - if (!mpr.getPartnerMaterialNumber().equals(matNbrSupplier)) { - log.warn("Unknown MaterialNumberSupplier " + matNbrSupplier + " for Material " + material.getOwnMaterialNumber()); - } - } - } - } - //Use MatNbrSupplier - if (material == null && matNbrSupplier != null) { - var list = mprService.findAllByPartnerMaterialNumber(matNbrSupplier) - .stream() - .filter(m -> { - var mpr = mprService.find(m, partner); - return mpr != null && mpr.isPartnerSuppliesMaterial(); - }) - .toList(); - if (!list.isEmpty()) { - material = list.get(0); - if (list.size() > 1) { - log.warn("SupplierMaterialNumber " + matNbrSupplier + " is ambiguous, arbitrarily choosing " + material.getOwnMaterialNumber()); - } - } - } + Material material = materialService.findFromCustomerPerspective(matNbrCatenaX, matNbrCustomer, matNbrSupplier, partner); if (material == null) { log.warn("Could not identify material with CatenaXNbr " + matNbrCatenaX + " ,CustomerMaterialNbr " + matNbrCustomer + " and SupplierMaterialNbr " + matNbrSupplier); return outputList; diff --git a/backend/src/test/java/org/eclipse/tractusx/puris/backend/masterdata/logic/MaterialServiceTest.java b/backend/src/test/java/org/eclipse/tractusx/puris/backend/masterdata/logic/MaterialServiceTest.java index 062669d7..845da196 100644 --- a/backend/src/test/java/org/eclipse/tractusx/puris/backend/masterdata/logic/MaterialServiceTest.java +++ b/backend/src/test/java/org/eclipse/tractusx/puris/backend/masterdata/logic/MaterialServiceTest.java @@ -20,7 +20,11 @@ package org.eclipse.tractusx.puris.backend.masterdata.logic; import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Material; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.MaterialPartnerRelation; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Partner; +import org.eclipse.tractusx.puris.backend.masterdata.domain.model.Site; import org.eclipse.tractusx.puris.backend.masterdata.domain.repository.MaterialRepository; +import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialPartnerRelationService; import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialServiceImpl; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -29,7 +33,9 @@ import org.mockito.MockitoAnnotations; import java.util.HashSet; +import java.util.List; import java.util.Optional; +import java.util.UUID; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; @@ -39,9 +45,17 @@ public class MaterialServiceTest { @Mock private MaterialRepository materialRepository; + @Mock + private MaterialPartnerRelationService mprService; + @InjectMocks private MaterialServiceImpl materialService; + private final static String semiconductorMatNbrCustomer = "MNR-7307-AU340474.002"; + private final static String semiconductorMatNbrSupplier = "MNR-8101-ID146955.001"; + + private final static UUID randomUUID = UUID.randomUUID(); + @BeforeEach void setUp() { MockitoAnnotations.openMocks(this); @@ -80,4 +94,185 @@ void create_WhenMaterialExists_ReturnsNull() { verify(materialRepository, times(1)).findById(material.getOwnMaterialNumber()); verify(materialRepository, never()).save(material); } + + @Test + void identifyMaterial_WithCustomerMaterialNumber_OnSupplierSide() { + // Given + Material material = new Material(); + material.setProductFlag(true); + material.setOwnMaterialNumber(semiconductorMatNbrSupplier); + Partner customerPartner = createAndGetCustomerPartner(); + MaterialPartnerRelation materialPartnerRelation = new MaterialPartnerRelation(material, customerPartner, semiconductorMatNbrCustomer, false, true); + + // When + when(mprService.findAllByCustomerPartnerAndPartnerMaterialNumber(customerPartner, semiconductorMatNbrCustomer)).thenReturn(List.of(materialPartnerRelation)); + when(materialRepository.findById(semiconductorMatNbrSupplier)).thenReturn(Optional.empty()); + + // Then + var result = materialService.findFromSupplierPerspective(null, semiconductorMatNbrCustomer, semiconductorMatNbrSupplier, customerPartner); + assertEquals(result, material); + } + + @Test + void identifyMaterial_WithCustomerMaterialNumber_OnSupplierSide_WithoutPartner() { + // Given + Material material = new Material(); + material.setProductFlag(true); + material.setOwnMaterialNumber(semiconductorMatNbrSupplier); + Partner customerPartner = createAndGetCustomerPartner(); + MaterialPartnerRelation materialPartnerRelation = new MaterialPartnerRelation(material, customerPartner, semiconductorMatNbrCustomer, false, true); + + // When + when(mprService.findAllByCustomerPartnerMaterialNumber(semiconductorMatNbrCustomer)).thenReturn(List.of(materialPartnerRelation)); + + // Then + var result = materialService.findFromSupplierPerspective(null, semiconductorMatNbrCustomer, null, null); + assertEquals(result, material); + } + + @Test + void identifyMaterial_WithWrongId_OnSupplierSide_ShouldFail() { + // Given + Material material = new Material(); + material.setProductFlag(true); + material.setOwnMaterialNumber(semiconductorMatNbrSupplier); + Partner customerPartner = createAndGetCustomerPartner(); + MaterialPartnerRelation materialPartnerRelation = new MaterialPartnerRelation(material, customerPartner, semiconductorMatNbrCustomer, false, true); + + // When + when(mprService.findAllByCustomerPartnerMaterialNumber(semiconductorMatNbrCustomer)).thenReturn(List.of(materialPartnerRelation)); + + // Then + var result = materialService.findFromSupplierPerspective(null, "foo", null, null); + assertNull(result); + } + + @Test + void identifyMaterial_WithCatenaXNumber_OnSupplierSide() { + // Given + Material material = new Material(); + material.setProductFlag(true); + material.setOwnMaterialNumber(semiconductorMatNbrSupplier); + material.setMaterialNumberCx(randomUUID.toString()); + Partner customerPartner = createAndGetCustomerPartner(); + + // When + when(materialRepository.findByMaterialNumberCx(randomUUID.toString())).thenReturn(List.of(material)); + + // Then + var result = materialService.findFromSupplierPerspective(randomUUID.toString(), semiconductorMatNbrCustomer, semiconductorMatNbrSupplier, customerPartner); + assertEquals(result, material); + } + + @Test + void identifyMaterial_WithSupplierMaterialNumber_OnSupplierSide() { + // Given + Material material = new Material(); + material.setProductFlag(true); + material.setOwnMaterialNumber(semiconductorMatNbrSupplier); + Partner customerPartner = createAndGetCustomerPartner(); + MaterialPartnerRelation materialPartnerRelation = new MaterialPartnerRelation(material, customerPartner, semiconductorMatNbrCustomer, false, true); + + // When + when(mprService.findAllByCustomerPartnerAndPartnerMaterialNumber(customerPartner, semiconductorMatNbrCustomer)).thenReturn(List.of(materialPartnerRelation)); + when(materialRepository.findById(semiconductorMatNbrSupplier)).thenReturn(Optional.of(material)); + + // Then + var result = materialService.findFromSupplierPerspective(null, null, semiconductorMatNbrSupplier, customerPartner); + assertEquals(result, material); + } + + + + @Test + void identifyMaterial_WithCustomerMaterialNumber_OnCustomerSide() { + // Given + Material material = new Material(); + material.setMaterialFlag(true); + material.setOwnMaterialNumber(semiconductorMatNbrCustomer); + Partner supplierPartner = createAndGetSupplierPartner(); + MaterialPartnerRelation materialPartnerRelation = new MaterialPartnerRelation(material, supplierPartner, semiconductorMatNbrSupplier, true, false); + + // When + when(materialRepository.findById(semiconductorMatNbrCustomer)).thenReturn(Optional.of(material)); + + // Then + var result = materialService.findFromCustomerPerspective(null, semiconductorMatNbrCustomer, semiconductorMatNbrSupplier, supplierPartner); + assertEquals(material, result); + } + + @Test + void identifyMaterial_WithSupplierMaterialNumber_OnCustomerSide() { + // Given + Material material = new Material(); + material.setMaterialFlag(true); + material.setOwnMaterialNumber(semiconductorMatNbrCustomer); + Partner supplierPartner = createAndGetSupplierPartner(); + MaterialPartnerRelation materialPartnerRelation = new MaterialPartnerRelation(material, supplierPartner, semiconductorMatNbrSupplier, true, false); + + // When + when(mprService.findAllBySupplierPartnerAndPartnerMaterialNumber(supplierPartner, semiconductorMatNbrSupplier)).thenReturn(List.of(materialPartnerRelation)); + + // Then + var result = materialService.findFromCustomerPerspective(null, null, semiconductorMatNbrSupplier, supplierPartner); + assertEquals(material, result); + } + + @Test + void identifyMaterial_WithCatenaXNumber_OnCustomerSide() { + // Given + Material material = new Material(); + material.setMaterialFlag(true); + material.setOwnMaterialNumber(semiconductorMatNbrCustomer); + material.setMaterialNumberCx(randomUUID.toString()); + Partner supplierPartner = createAndGetSupplierPartner(); + + // When + when(materialRepository.findByMaterialNumberCx(randomUUID.toString())).thenReturn(List.of(material)); + + // Then + var result = materialService.findFromSupplierPerspective(randomUUID.toString(), semiconductorMatNbrCustomer, semiconductorMatNbrSupplier, supplierPartner); + assertEquals(result, material); + } + + + + private Partner createAndGetCustomerPartner() { + Partner customerPartnerEntity = new Partner( + "Control Unit Creator Inc.", + "http://customer-control-plane:8184/api/v1/dsp", + "BPNL4444444444XX", + "BPNS4444444444XX", + "Control Unit Creator Production Site", + "BPNA4444444444AA", + "13th Street 47", + "10011 New York", + "USA" + ); + return customerPartnerEntity; + } + + private Partner createAndGetSupplierPartner() { + Partner supplierPartnerEntity = new Partner( + "Semiconductor Supplier Inc.", + "http://supplier-control-plane:9184/api/v1/dsp", + "BPNL1234567890ZZ", + "BPNS1234567890ZZ", + "Semiconductor Supplier Inc. Production Site", + "BPNA1234567890AA", + "Wall Street 101", + "10001 New York", + "USA" + ); + Site secondSite = new Site( + "BPNS2222222222SS", + "Semiconductor Supplier Inc. Secondary Site", + "BPNA2222222222AA", + "Sunset Blvd. 345", + "90001 Los Angeles", + "USA" + ); + supplierPartnerEntity.getSites().add(secondSite); + return supplierPartnerEntity; + } } From 71918471796acf5785c2ac70e6a7b0c98da5a2fa Mon Sep 17 00:00:00 2001 From: Ernst-Christoph Schrewe Date: Tue, 30 Jan 2024 12:50:06 +0100 Subject: [PATCH 2/2] refactor: adapt test mocks --- .../logic/adapter/ItemStockSammMapper.java | 38 ------------------- .../adapter/ItemStockSammMapperTest.java | 21 ++++------ 2 files changed, 8 insertions(+), 51 deletions(-) diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ItemStockSammMapper.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ItemStockSammMapper.java index b71bb1ac..86f1f95a 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ItemStockSammMapper.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ItemStockSammMapper.java @@ -114,45 +114,7 @@ private record PositionsMappingHelper(Date date, String customerOrderId, String String customerOrderPositionId) { } - public ItemStockSamm toItemStockSamm(MaterialItemStock materialItemStock) { - ItemStockSamm samm = new ItemStockSamm(); - samm.setDirection(DirectionCharacteristic.INBOUND); - samm.setPositions(new ArrayList<>()); - samm.setMaterialGlobalAssetId(materialItemStock.getMaterial().getMaterialNumberCx()); - samm.setMaterialNumberCustomer(materialItemStock.getMaterial().getOwnMaterialNumber()); - samm.setMaterialNumberSupplier(mprService.find(materialItemStock.getMaterial(), - materialItemStock.getPartner()).getPartnerMaterialNumber()); - return createPosition(materialItemStock, samm); - } - - private static ItemStockSamm createPosition(ItemStock itemStock, ItemStockSamm samm) { - Position position = new Position(); - samm.setPositions(List.of(position)); - position.setLastUpdatedOnDateTime(itemStock.getLastUpdatedOnDateTime()); - if (itemStock.getCustomerOrderId() != null || itemStock.getCustomerOrderPositionId() != null - || itemStock.getSupplierOrderId() != null) { - OrderPositionReference opr = new OrderPositionReference(itemStock.getSupplierOrderId(), - itemStock.getCustomerOrderId(), itemStock.getCustomerOrderPositionId()); - position.setOrderPositionReference(opr); - } - ItemQuantityEntity itemQuantityEntity = new ItemQuantityEntity(itemStock.getQuantity(), - itemStock.getMeasurementUnit()); - AllocatedStock allocatedStock = new AllocatedStock(itemQuantityEntity, itemStock.getLocationBpns(), - itemStock.isBlocked(), itemStock.getLocationBpna()); - position.setAllocatedStocks(List.of(allocatedStock)); - return samm; - } - public ItemStockSamm toItemStockSamm(ProductItemStock productItemStock) { - ItemStockSamm samm = new ItemStockSamm(); - samm.setDirection(DirectionCharacteristic.OUTBOUND); - samm.setPositions(new ArrayList<>()); - samm.setMaterialGlobalAssetId(productItemStock.getMaterial().getMaterialNumberCx()); - samm.setMaterialNumberSupplier(productItemStock.getMaterial().getOwnMaterialNumber()); - samm.setMaterialNumberCustomer(mprService.find(productItemStock.getMaterial(), - productItemStock.getPartner()).getPartnerMaterialNumber()); - return createPosition(productItemStock, samm); - } public List itemStockSammToReportedProductItemStock(ItemStockSamm samm, Partner partner) { String matNbrCustomer = samm.getMaterialNumberCustomer(); diff --git a/backend/src/test/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ItemStockSammMapperTest.java b/backend/src/test/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ItemStockSammMapperTest.java index b3052e81..7ee493ca 100644 --- a/backend/src/test/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ItemStockSammMapperTest.java +++ b/backend/src/test/java/org/eclipse/tractusx/puris/backend/stock/logic/adapter/ItemStockSammMapperTest.java @@ -65,7 +65,7 @@ public class ItemStockSammMapperTest { final static String SUPPLIER_BPNS2 = "BPNS2222222222SI"; final static String SUPPLIER_BPNA2 = "BPNA2222222222AD"; - final Partner supplierPartner = new Partner( + final static Partner supplierPartner = new Partner( "Scenario Supplier", "http://supplier-control-plane:9184/api/v1/dsp", SUPPLIER_BPNL, @@ -77,7 +77,7 @@ public class ItemStockSammMapperTest { "Germany" ); - final Partner customerPartner = new Partner( + final static Partner customerPartner = new Partner( "Scenario Customer", "http://customer-control-plane:8184/api/v1/dsp", "BPNL4444444444XX", @@ -144,7 +144,7 @@ void map_WhenSingleMaterialItemStock_ReturnsItemStockSamm() { // These should result in two positions // - one WITHOUT orderPositionReference AND two allocatedStocks // - one WITH orderPosition AND one allocatedStocks - ItemStockSamm materialItemStockSamm = itemStockSammMapper.toItemStockSamm(materialItemStock); + ItemStockSamm materialItemStockSamm = itemStockSammMapper.materialItemStocksToItemStockSamm(List.of(materialItemStock)); // Then assertNotNull(materialItemStockSamm); @@ -288,7 +288,7 @@ void map_WhenReportedSammToProductItemStock_ReturnsMultipleReportedProductItemSt // When // Find material based on CX number and mpr - when(materialService.findByMaterialNumberCx(CX_MAT_NUMBER)).thenReturn(semiconductorProduct); + when(materialService.findFromSupplierPerspective(CX_MAT_NUMBER, CUSTOMER_MAT_NUMBER, SUPPLIER_MAT_NUMBER, customerPartner)).thenReturn(semiconductorProduct); when(mprService.find(semiconductorProduct, customerPartner)).thenReturn(mpr); // Then we should build 5 reported product stocks: @@ -298,7 +298,6 @@ void map_WhenReportedSammToProductItemStock_ReturnsMultipleReportedProductItemSt // - OPR, blocked, 10 pieces, BPNS & BPNA // - OPR, not blocked, 20 kilo, BPNS & BPNA List reportedProductItemStocks = itemStockSammMapper.itemStockSammToReportedProductItemStock(inboundProductStockSamm, customerPartner); - // Then assertEquals(5, reportedProductItemStocks.size()); @@ -387,14 +386,12 @@ void test_unmarshalling() { material.setMaterialNumberCx(CX_MAT_NUMBER); MaterialPartnerRelation mpr = new MaterialPartnerRelation(); - mpr.setPartner(supplierPartner); + mpr.setPartner(customerPartner); mpr.setMaterial(material); mpr.setPartnerBuysMaterial(true); mpr.setPartnerMaterialNumber(CUSTOMER_MAT_NUMBER); - when(materialService.findByMaterialNumberCx(CX_MAT_NUMBER)).thenReturn(material); - when(mprService.findAllByPartnerMaterialNumber(CUSTOMER_MAT_NUMBER)).thenReturn(List.of(material)); - when(mprService.find(material, supplierPartner)).thenReturn(mpr); + when(materialService.findFromSupplierPerspective(CX_MAT_NUMBER, CUSTOMER_MAT_NUMBER, SUPPLIER_MAT_NUMBER, customerPartner)).thenReturn(material); var list = itemStockSammMapper.itemStockSammToReportedProductItemStock(SAMM_FROM_CUSTOMER_PARTNER, supplierPartner); assertNotNull(list); @@ -421,14 +418,12 @@ void test_deserializationFromJson() throws Exception { material.setMaterialNumberCx(CX_MAT_NUMBER); MaterialPartnerRelation mpr = new MaterialPartnerRelation(); - mpr.setPartner(supplierPartner); + mpr.setPartner(customerPartner); mpr.setMaterial(material); mpr.setPartnerBuysMaterial(true); mpr.setPartnerMaterialNumber(CUSTOMER_MAT_NUMBER); - when(materialService.findByMaterialNumberCx(CX_MAT_NUMBER)).thenReturn(material); - when(mprService.findAllByPartnerMaterialNumber(CUSTOMER_MAT_NUMBER)).thenReturn(List.of(material)); - when(mprService.find(material, supplierPartner)).thenReturn(mpr); + when(materialService.findFromSupplierPerspective(CX_MAT_NUMBER, CUSTOMER_MAT_NUMBER, SUPPLIER_MAT_NUMBER, customerPartner)).thenReturn(material); ObjectMapper objectMapper = new ObjectMapper();