From fb17177eb6c9089c0f03b85ac98a82e730995b03 Mon Sep 17 00:00:00 2001 From: --show-origin Date: Fri, 18 Aug 2023 06:50:26 -0700 Subject: [PATCH] refactor: update data injection for demo scenarios --- .../DataInjectionCommandLineRunner.java | 303 +++++++----------- 1 file changed, 108 insertions(+), 195 deletions(-) diff --git a/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java b/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java index eb44226b..66ec01e7 100644 --- a/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java +++ b/backend/src/main/java/org/eclipse/tractusx/puris/backend/DataInjectionCommandLineRunner.java @@ -24,28 +24,21 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; -import org.eclipse.tractusx.puris.backend.common.api.domain.model.ProductStockRequest; +import org.eclipse.tractusx.puris.backend.common.api.domain.model.MessageHeader; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_RequestStateEnum; import org.eclipse.tractusx.puris.backend.common.api.domain.model.datatype.DT_UseCaseEnum; -import org.eclipse.tractusx.puris.backend.common.api.logic.dto.MessageContentErrorDto; -import org.eclipse.tractusx.puris.backend.common.api.logic.dto.MessageHeaderDto; -import org.eclipse.tractusx.puris.backend.common.api.logic.service.RequestService; 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.logic.service.MaterialPartnerRelationService; import org.eclipse.tractusx.puris.backend.masterdata.logic.service.MaterialService; import org.eclipse.tractusx.puris.backend.masterdata.logic.service.PartnerService; -import org.eclipse.tractusx.puris.backend.stock.domain.model.MaterialStock; -import org.eclipse.tractusx.puris.backend.stock.domain.model.PartnerProductStock; -import org.eclipse.tractusx.puris.backend.stock.domain.model.ProductStock; -import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ApiMarshallingService; +import org.eclipse.tractusx.puris.backend.stock.domain.model.*; import org.eclipse.tractusx.puris.backend.stock.logic.adapter.ProductStockSammMapper; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockDto; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockRequestDto; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockRequestForMaterialDto; -import org.eclipse.tractusx.puris.backend.stock.logic.dto.ProductStockResponseDto; import org.eclipse.tractusx.puris.backend.stock.logic.dto.samm.ProductStockSammDto; import org.eclipse.tractusx.puris.backend.stock.logic.service.MaterialStockService; import org.eclipse.tractusx.puris.backend.stock.logic.service.PartnerProductStockService; +import org.eclipse.tractusx.puris.backend.stock.logic.service.ProductStockRequestService; import org.eclipse.tractusx.puris.backend.stock.logic.service.ProductStockService; import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; @@ -53,7 +46,6 @@ import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; -import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; @@ -62,16 +54,15 @@ @Slf4j public class DataInjectionCommandLineRunner implements CommandLineRunner { - @Autowired - private ModelMapper modelMapper; - - @Autowired private MaterialService materialService; @Autowired private PartnerService partnerService; + @Autowired + private MaterialPartnerRelationService mprService; + @Autowired private MaterialStockService materialStockService; @@ -85,17 +76,17 @@ public class DataInjectionCommandLineRunner implements CommandLineRunner { private ProductStockSammMapper productStockSammMapper; @Autowired - private RequestService requestService; + private ProductStockRequestService productStockRequestService; @Value("${puris.demonstrator.role}") private String demoRole; - @Autowired - private ApiMarshallingService apiMarshallingService; - private ObjectMapper objectMapper; + private final String semiconductorMatNbrCustomer = "MNR-7307-AU340474.002"; + private final String semiconductorMatNbrSupplier = "MNR-8101-ID146955.001"; + public DataInjectionCommandLineRunner(ObjectMapper objectMapper) { this.objectMapper = objectMapper; } @@ -121,11 +112,7 @@ public void run(String... args) throws Exception { */ private void setupCustomerRole() throws JsonProcessingException { Partner supplierPartner = createAndGetSupplierPartner(); - Material semiconductorMaterial = getNewSemiconductorMaterial(); - semiconductorMaterial.addPartnerToSuppliedByPartners(supplierPartner); - // adjust flags for customer role - semiconductorMaterial.setMaterialFlag(true); - semiconductorMaterial.setProductFlag(true); + Material semiconductorMaterial = getNewSemiconductorMaterialForCustomer(); semiconductorMaterial = materialService.create(semiconductorMaterial); log.info(String.format("Created material: %s", semiconductorMaterial)); @@ -134,91 +121,88 @@ private void setupCustomerRole() throws JsonProcessingException { log.info(String.format("UUID of supplier partner: %s", supplierPartner.getUuid())); supplierPartner = partnerService.findByUuid(supplierPartner.getUuid()); log.info(String.format("Found supplier partner: %s", supplierPartner)); - log.info(String.format("Relationship to material: %s", supplierPartner.getSuppliesMaterials())); + + + MaterialPartnerRelation semiconductorPartnerRelation = new MaterialPartnerRelation(semiconductorMaterial, + supplierPartner, semiconductorMatNbrSupplier, true, false); + mprService.create(semiconductorPartnerRelation); + semiconductorPartnerRelation = mprService.find(semiconductorMaterial, supplierPartner); + log.info("Found Relation: " + semiconductorPartnerRelation); // customer + material Partner nonScenarioCustomer = createAndGetNonScenarioCustomer(); Material centralControlUnitEntity = getNewCentralControlUnitMaterial(); - centralControlUnitEntity.addPartnerToOrderedByPartners(nonScenarioCustomer); centralControlUnitEntity = materialService.create(centralControlUnitEntity); log.info(String.format("Created Product: %s", centralControlUnitEntity)); - List productsFound = materialService.findAllProducts(); - log.info(String.format("Found Products: %s", productsFound)); - centralControlUnitEntity = - materialService.findProductByMaterialNumberCustomer(centralControlUnitEntity.getMaterialNumberCustomer()); - log.info(String.format("Found product by materialNumber customer: %s", - centralControlUnitEntity)); - nonScenarioCustomer = partnerService.findByUuid(nonScenarioCustomer.getUuid()); - log.info(String.format("Relationship to product: %s", - nonScenarioCustomer.getOrdersProducts())); + MaterialPartnerRelation ccuPartnerRelation = new MaterialPartnerRelation(centralControlUnitEntity, + nonScenarioCustomer, "MNR-4177-C", false, true); + ccuPartnerRelation = mprService.create(ccuPartnerRelation); + log.info("Found Relation: " + ccuPartnerRelation); - centralControlUnitEntity = - materialService.findProductByMaterialNumberCustomer(centralControlUnitEntity.getMaterialNumberCustomer()); - log.info(String.format("Found product by materialNumber customer: %s", - centralControlUnitEntity)); + log.info("All stored Relations: " + mprService.findAll()); - Material existingMaterial = - materialService.findByUuid(semiconductorMaterial.getUuid()); - log.info(String.format("Found existingMaterial by uuid: %s", - existingMaterial)); + List foundPartners = mprService.findAllCustomersForOwnMaterialNumber(centralControlUnitEntity.getOwnMaterialNumber()); - Material existingProduct = - materialService.findProductByMaterialNumberCustomer(centralControlUnitEntity.getMaterialNumberCustomer()); - log.info(String.format("Found existingProduct by customer number: %s", - existingProduct)); + log.info("Customer Partner for CCU: " + foundPartners); - List existingProducts = - materialService.findAllProducts(); - log.info(String.format("Found existingProducts by product flag true: %s", - existingProducts)); + List productsFound = materialService.findAllProducts(); + log.info(String.format("Found Products: %s", productsFound)); - log.info(String.format("Relationship centralControlUnitEntity -> orderedByPartners: %s", - centralControlUnitEntity.getOrderedByPartners().toString())); + productsFound = mprService.findAllProductsThatPartnerBuys(nonScenarioCustomer); + log.info("Products that customer buys: " + productsFound); // Create Material Stock MaterialStock materialStockEntity = new MaterialStock( semiconductorMaterial, - 20, + 5, "BPNS4444444444XX", new Date() ); materialStockEntity = materialStockService.create(materialStockEntity); log.info(String.format("Created materialStock: %s", materialStockEntity)); - List foundMaterialStocks = - materialStockService.findAllByMaterialNumberCustomer(semiconductorMaterial.getMaterialNumberCustomer()); - log.info(String.format("Found materialStock: %s", foundMaterialStocks)); + // Create PartnerProductStock - semiconductorMaterial = materialService.findByUuid(semiconductorMaterial.getUuid()); + semiconductorMaterial = materialService.findByOwnMaterialNumber(semiconductorMaterial.getOwnMaterialNumber()); PartnerProductStock partnerProductStockEntity = new PartnerProductStock( semiconductorMaterial, - 20, + 10, supplierPartner.getSiteBpns(), new Date(), supplierPartner ); - partnerProductStockEntity = partnerProductStockService.create(partnerProductStockEntity); log.info(String.format("Created partnerProductStock: %s", partnerProductStockEntity)); - ProductStockDto productStockDto = modelMapper.map(partnerProductStockEntity, - ProductStockDto.class); - ProductStockSammDto productStockSammDto = productStockSammMapper.toSamm(productStockDto); - log.info(objectMapper.writeValueAsString(productStockSammDto)); + partnerProductStockEntity = partnerProductStockService.create(partnerProductStockEntity); + ProductStockSammDto productStockSammDto = productStockSammMapper.toSamm(partnerProductStockEntity); + log.info("SAMM-DTO:\n" + objectMapper.writeValueAsString(productStockSammDto)); } /** * Generates an initial set of data for a supplier within the demonstration context. */ private void setupSupplierRole() { Partner customerPartner = createAndGetCustomerPartner(); - Material semiconductorMaterial = getNewSemiconductorMaterial(); - semiconductorMaterial.addPartnerToOrderedByPartners(customerPartner); + Material semiconductorMaterial = getNewSemiconductorMaterialForSupplier(); + semiconductorMaterial = materialService.create(semiconductorMaterial); log.info(String.format("Created product: %s", semiconductorMaterial)); + MaterialPartnerRelation semiconductorPartnerRelation = new MaterialPartnerRelation(semiconductorMaterial, + customerPartner, semiconductorMatNbrCustomer, false, true); + semiconductorPartnerRelation = mprService.create(semiconductorPartnerRelation); + + log.info("Created Relation " + semiconductorPartnerRelation); + + semiconductorPartnerRelation = mprService.find(semiconductorMaterial, customerPartner); + + log.info("Found Relation " + semiconductorPartnerRelation); + List materialsFound = materialService.findAllProducts(); log.info(String.format("Found product: %s", materialsFound)); - log.info(String.format("Found customer partner: %s", customerPartner)); - log.info(String.format("Relationship to material: %s", customerPartner.getOrdersProducts())); + + List customerPartners = mprService.findAllCustomersForOwnMaterialNumber(semiconductorMaterial.getOwnMaterialNumber()); + log.info(String.format("Found customer partners for semiconductor: %s", customerPartners)); + ProductStock productStockEntity = new ProductStock( semiconductorMaterial, @@ -229,11 +213,9 @@ private void setupSupplierRole() { ); productStockEntity = productStockService.create(productStockEntity); log.info(String.format("Created productStock: %s", productStockEntity.toString())); - List foundProductStocks = - productStockService - .findAllByMaterialNumberCustomerAndAllocatedToCustomerBpnl( - semiconductorMaterial.getMaterialNumberCustomer(), - customerPartner.getBpnl()); + + List foundProductStocks = productStockService. + findAllByMaterialNumberCustomer(semiconductorMatNbrCustomer, customerPartner); log.info(String.format("Found productStocks by material number and allocated to customer " + "bpnl: %s", foundProductStocks)); } @@ -247,8 +229,6 @@ private void setupSupplierRole() { private Partner createAndGetCustomerPartner() { Partner customerPartnerEntity = new Partner( "Scenario Customer", - true, - false, "http://sokrates-controlplane:8084/api/v1/ids", "BPNL4444444444XX", "BPNS4444444444XX" @@ -268,8 +248,6 @@ private Partner createAndGetCustomerPartner() { private Partner createAndGetSupplierPartner() { Partner supplierPartnerEntity = new Partner( "Scenario Supplier", - false, - true, "http://plato-controlplane:8084/api/v1/ids", "BPNL1234567890ZZ", "BPNS1234567890ZZ" @@ -289,8 +267,6 @@ private Partner createAndGetSupplierPartner() { private Partner createAndGetNonScenarioCustomer() { Partner nonScenarioCustomer = new Partner( "Non-Scenario Customer", - true, - false, "(None Provided!)>", "BPNL2222222222RR", "BPNL2222222222RR" @@ -302,20 +278,20 @@ private Partner createAndGetNonScenarioCustomer() { return nonScenarioCustomer; } - /** - * creates a new semiconductor Material object. - * Note: this object is not yet stored to the database - * @return a reference to the newly created semiconductor material - */ - private Material getNewSemiconductorMaterial() { - return new Material( - false, - true, - "MNR-7307-AU340474.002", - "MNR-8101-ID146955.001", - null, - "semiconductor" - ); + private Material getNewSemiconductorMaterialForSupplier() { + Material material = new Material(); + material.setOwnMaterialNumber(semiconductorMatNbrSupplier); + material.setProductFlag(true); + material.setName("semiconductor"); + return material; + } + + private Material getNewSemiconductorMaterialForCustomer() { + Material material = new Material(); + material.setOwnMaterialNumber(semiconductorMatNbrCustomer); + material.setMaterialFlag(true); + material.setName("semiconductor"); + return material; } /** @@ -324,109 +300,46 @@ private Material getNewSemiconductorMaterial() { * @return a reference to the newly created central control unit material */ private Material getNewCentralControlUnitMaterial() { - return new Material( - false, - true, - "MNR-4177-C", - "MNR-4177-S", - null, - "central control unit" - ); + Material material = new Material(); + material.setOwnMaterialNumber("MNR-4177-S"); + material.setProductFlag(true); + material.setName("central control unit"); + return material; } private void createRequest() throws JsonProcessingException { - MessageHeaderDto messageHeaderDto = new MessageHeaderDto(); - messageHeaderDto.setRequestId(UUID.fromString("4979893e-dd6b-43db-b732-6e48b4ba35b3")); - messageHeaderDto.setRespondAssetId("product-stock-response-api"); - messageHeaderDto.setContractAgreementId("some cid"); - messageHeaderDto.setSender("BPNL1234567890ZZ"); - messageHeaderDto.setSenderEdc("http://plato-controlplane:8084/api/v1/ids"); - messageHeaderDto.setReceiver("BPNL4444444444XX"); - messageHeaderDto.setUseCase(DT_UseCaseEnum.PURIS); - messageHeaderDto.setCreationDate(new Date()); - - log.info(objectMapper.writeValueAsString(messageHeaderDto)); - - List messageContentDtos = new ArrayList<>(); - - ProductStockRequestForMaterialDto messageContentDto = new ProductStockRequestForMaterialDto(); - messageContentDto.setMaterialNumberCustomer("CU-MNR"); - messageContentDto.setMaterialNumberSupplier("SU-MNR"); - messageContentDtos.add(messageContentDto); - messageContentDto = new ProductStockRequestForMaterialDto(); - messageContentDto.setMaterialNumberCustomer("OtherCU-MNR"); - messageContentDto.setMaterialNumberSupplier("OtherSU-MNR"); - messageContentDtos.add(messageContentDto); - - ProductStockRequestDto requestDto = new ProductStockRequestDto( - DT_RequestStateEnum.RECEIPT, - messageHeaderDto, - messageContentDtos - ); - ProductStockRequest createdProductStockRequest = requestService.createRequest(modelMapper.map(requestDto, - ProductStockRequest.class)); - log.info(String.format("Created Request: %s", createdProductStockRequest)); - log.info(createdProductStockRequest.getPayload().get(0).getClass().toString()); - - log.info("Testing ApiMarshallingService:"); - String transformationTest = apiMarshallingService.transformProductStockRequest(requestDto); - log.info("marshalled request to be sent:\n" + transformationTest); - - ProductStockRequestDto productStockRequestDto = apiMarshallingService.transformToProductStockRequestDto(transformationTest); - log.info("unmarshalled the same request as productStockRequestDto: \n" + productStockRequestDto.toString()); - - String sampleResponse = "{\n" + - " \"header\" : {\n" + - " \"requestId\" : \"37be1c8e-e2c3-4fbc-848f-9ee576cecc9f\",\n" + - " \"respondAssetId\" : null,\n" + - " \"creationDate\" : \"2023-07-27T10:56:43.116+00:00\",\n" + - " \"senderEdc\" : \"http://plato-controlplane:8084/api/v1/ids\",\n" + - " \"sender\" : \"BPNL1234567890ZZ\",\n" + - " \"receiver\" : \"BPNL4444444444XX\",\n" + - " \"useCase\" : \"PURIS\",\n" + - " \"contractAgreementId\" : \"product-stock-response-api:c51b78d3-06fa-4539-8d3a-2a0fb19780ba\"\n" + - " },\n" + - " \"content\" : {\n" + - " \"productStock\" : [ {\n" + - " \"positions\" : [ {\n" + - " \"orderPositionReference\" : null,\n" + - " \"lastUpdatedOnDateTime\" : 1690455395379,\n" + - " \"allocatedStocks\" : [ {\n" + - " \"quantityOnAllocatedStock\" : {\n" + - " \"quantityNumber\" : 20.0,\n" + - " \"measurementUnit\" : \"unit:piece\"\n" + - " },\n" + - " \"supplierStockLocationId\" : {\n" + - " \"locationIdType\" : \"BPNS\",\n" + - " \"locationId\" : \"BPNS1234567890ZZ\"\n" + - " }\n" + - " } ]\n" + - " } ],\n" + - " \"materialNumberCustomer\" : \"MNR-7307-AU340474.002\",\n" + - " \"materialNumberCatenaX\" : {\n" + - " \"empty\" : true,\n" + - " \"present\" : false\n" + - " },\n" + - " \"materialNumberSupplier\" : {\n" + - " \"empty\" : false,\n" + - " \"present\" : true\n" + - " }\n" + - " } ]\n" + - " }\n" + - "}\n"; - ProductStockResponseDto productStockResponseDto = apiMarshallingService.transformToProductStockResponseDto(sampleResponse); - // insert a MessageContentErrorDto - MessageContentErrorDto messageContentErrorDto = new MessageContentErrorDto(); - messageContentErrorDto.setMaterialNumberCustomer("Sample MaterialNumber"); - messageContentErrorDto.setError("Sample Error"); - messageContentErrorDto.setMessage("Sample Error Message"); - productStockResponseDto.getPayload().add(messageContentErrorDto); - log.info(productStockResponseDto.toString()); - String productStockResponseString = apiMarshallingService.transformProductStockResponse(productStockResponseDto); - log.info("marshalled sample response: \n" + productStockResponseString); - productStockResponseDto = apiMarshallingService.transformToProductStockResponseDto(productStockResponseString); - log.info("unmarshalled header: \n" + productStockResponseDto.getHeader()); - log.info("unmarshalled content: \n" + productStockResponseDto.getPayload()); + + ProductStockRequest request = new ProductStockRequest(); + MessageHeader messageHeader = new MessageHeader(); + messageHeader.setRequestId(UUID.fromString("4979893e-dd6b-43db-b732-6e48b4ba35b3")); + messageHeader.setRespondAssetId("product-stock-response-api"); + messageHeader.setContractAgreementId("some cid"); + messageHeader.setSender("BPNL1234567890ZZ"); + messageHeader.setSenderEdc("http://plato-controlplane:8084/api/v1/ids"); + messageHeader.setReceiver("BPNL4444444444XX"); + messageHeader.setUseCase(DT_UseCaseEnum.PURIS); + messageHeader.setCreationDate(new Date()); + request.setHeader(messageHeader); + + var productStock = request.getContent().getProductStock(); + ProductStockRequestForMaterial rfm = new ProductStockRequestForMaterial("CU-MNR", + null, "SU-MNR"); + productStock.add(rfm); + + rfm = new ProductStockRequestForMaterial("OtherCU-MNR", + null, "OtherSU-MNR"); + productStock.add(rfm); + request.setState(DT_RequestStateEnum.WORKING); + + request = productStockRequestService.createRequest(request); + + + + String stringOutput = objectMapper.writeValueAsString(request); + log.info("SAMPLE-Request\n" + objectMapper.readTree(stringOutput).toPrettyString()); + + var deserializedRequest = objectMapper.readValue(stringOutput, ProductStockRequest.class); + log.info(deserializedRequest.toString()); }