From d4f93c398c370a099b7b5537dbc86672200ebe19 Mon Sep 17 00:00:00 2001 From: Tomasz Golabek Date: Wed, 4 Sep 2019 12:56:12 +0200 Subject: unit tests - NetworkPackageHealer Additional junit tests using mockito Change-Id: I1a48d0dd15a3319dc096816ea36346f4b13322fc Issue-ID: SDC-2326 Signed-off-by: Tomasz Golabek --- .../sdc/healing/healers/NetworkPackageHealer.java | 584 ++++++++++----------- 1 file changed, 281 insertions(+), 303 deletions(-) (limited to 'openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java') diff --git a/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/NetworkPackageHealer.java b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/NetworkPackageHealer.java index e1807bafad..3b716da1ca 100644 --- a/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/NetworkPackageHealer.java +++ b/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/NetworkPackageHealer.java @@ -12,6 +12,9 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. + * ================================================================================ + * Modifications copyright (c) 2019 Nokia + * ================================================================================ */ package org.openecomp.sdc.healing.healers; @@ -59,318 +62,293 @@ import static org.openecomp.core.zusammen.api.ZusammenUtil.*; public class NetworkPackageHealer implements Healer { - private static final byte[] EMPTY_DATA_BYTES = "{}".getBytes(); - private static final String MISSING_ORCHESTRATION_TEMPLATE_CANDIDATE_ERROR = - "Vsp with invalid structure: does not contain element OrchestrationTemplateCandidate"; - private static final String MISSING_ORCHESTRATION_TEMPLATE_CANDIDATE_CONTENT_ERROR = - "Vsp with invalid structure: does not contain element OrchestrationTemplateCandidateContent" - + " under OrchestrationTemplateCandidate"; - - private static final String MISSING_VSP_MODEL_ERROR = - "Vsp with invalid structure: does not contain element VspModel"; - private static final String MISSING_ORCHESTRATION_TEMPLATE_ERROR = - "Vsp with invalid structure: does not contain element OrchestrationTemplate" - + " under VspModel element"; - private static final String MISSING_ORCHESTRATION_TEMPLATE_VALIDATE_DATA_ERROR = - "Vsp with invalid structure: does not contain element OrchestrationTemplateValidationData" - + " under OrchestrationTemplate element"; - - private final VendorSoftwareProductInfoDao vspInfoDao; - private final ZusammenAdaptor zusammenAdaptor; - private final CandidateService candidateService; - - public NetworkPackageHealer() { - this(VendorSoftwareProductInfoDaoFactory.getInstance().createInterface(), - ZusammenAdaptorFactory.getInstance().createInterface(), CandidateServiceFactory - .getInstance().createInterface()); - } - - public NetworkPackageHealer(VendorSoftwareProductInfoDao vspInfoDao, - ZusammenAdaptor zusammenAdaptor, - CandidateService candidateService) { - this.vspInfoDao = vspInfoDao; - this.zusammenAdaptor = zusammenAdaptor; - this.candidateService = candidateService; - } - - @Override - public boolean isHealingNeeded(String itemId, Version version) { - return OnboardingMethod.NetworkPackage.name() - .equals(vspInfoDao.get(new VspDetails(itemId, version)).getOnboardingMethod()) && - isVspMissingAddedElements(itemId, version); - } - - @Override - public void heal(String itemId, Version version) throws Exception { - SessionContext context = createSessionContext(); - ElementContext elementContext = new ElementContext(itemId, version.getId()); - - Element candidateElement = - getElement(context, elementContext, null, ElementType.OrchestrationTemplateCandidate, - MISSING_ORCHESTRATION_TEMPLATE_CANDIDATE_ERROR); - - Collection candidateSubs = - zusammenAdaptor.listElementData(context, elementContext, candidateElement.getElementId()); - - Id candidateValidationElementId = - creatIfAbsentCandidateValidationElementId(candidateSubs, context, - elementContext, candidateElement); - - Element orchestrationTemplateElement = getOrchestrationTemplateElement(context, elementContext); - - Collection orchestrationTemplateSubs = zusammenAdaptor - .listElementData(context, elementContext, orchestrationTemplateElement.getElementId()); - - Id structureElementId = createIfAbsentStructureElementId(orchestrationTemplateSubs, context, - elementContext, orchestrationTemplateElement); - - Element orchestrationTemplateValidationElement = getOrchestrationTemplateValidationElement - (orchestrationTemplateSubs); - - OrchestrationTemplateEntity orchestrationTemplateEntity = - getOrchestrationTemplateEntity(orchestrationTemplateElement, - orchestrationTemplateValidationElement); - - if (StringUtils.isEmpty(orchestrationTemplateEntity.getFileSuffix())) { - return; + private static final byte[] EMPTY_DATA_BYTES = "{}".getBytes(); + private static final String MISSING_ORCHESTRATION_TEMPLATE_CANDIDATE_ERROR = + "Vsp with invalid structure: does not contain element OrchestrationTemplateCandidate"; + private static final String MISSING_ORCHESTRATION_TEMPLATE_CANDIDATE_CONTENT_ERROR = + "Vsp with invalid structure: does not contain element OrchestrationTemplateCandidateContent" + + " under OrchestrationTemplateCandidate"; + + private static final String MISSING_VSP_MODEL_ERROR = + "Vsp with invalid structure: does not contain element VspModel"; + private static final String MISSING_ORCHESTRATION_TEMPLATE_ERROR = + "Vsp with invalid structure: does not contain element OrchestrationTemplate" + " under VspModel element"; + private static final String MISSING_ORCHESTRATION_TEMPLATE_VALIDATE_DATA_ERROR = + "Vsp with invalid structure: does not contain element OrchestrationTemplateValidationData" + + " under OrchestrationTemplate element"; + + private final VendorSoftwareProductInfoDao vspInfoDao; + private final ZusammenAdaptor zusammenAdaptor; + private final CandidateEntityBuilder candidateEntityBuilder; + + public NetworkPackageHealer() { + this(VendorSoftwareProductInfoDaoFactory.getInstance().createInterface(), + ZusammenAdaptorFactory.getInstance().createInterface(), + CandidateServiceFactory.getInstance().createInterface()); } - Element candidateContentElement = getCandidateContentElement(candidateSubs); - - VspDetails vspDetails = vspInfoDao.get(new VspDetails(itemId, version)); - if (isEqual(orchestrationTemplateEntity, - getCandidateData(candidateElement, candidateContentElement))) { - - if (isProcessedEntityValid(orchestrationTemplateEntity)) { - emptyStructureElementAndSub(context, elementContext, candidateElement.getElementId(), - ElementType.OrchestrationTemplateCandidate, candidateContentElement.getElementId(), - ElementType.OrchestrationTemplateCandidateContent); - - populateOrchestrationTemplateStructure(orchestrationTemplateElement.getData(), - structureElementId, vspDetails, context, elementContext); - } else { - emptyStructureElementAndSub(context, elementContext, - orchestrationTemplateElement.getElementId(), ElementType.OrchestrationTemplate, - orchestrationTemplateValidationElement.getElementId(), - ElementType.OrchestrationTemplateValidationData); - - populateCandidateValidationData(context, elementContext, candidateValidationElementId, - orchestrationTemplateEntity.getValidationData()); - } - } else { - populateOrchestrationTemplateStructure(orchestrationTemplateElement.getData(), - structureElementId, vspDetails, context, elementContext); + private NetworkPackageHealer(VendorSoftwareProductInfoDao vspInfoDao, ZusammenAdaptor zusammenAdaptor, + CandidateService candidateService) { + this(vspInfoDao, zusammenAdaptor, new CandidateEntityBuilder(candidateService)); } - } - - private boolean isVspMissingAddedElements(String vspId, Version version) { - SessionContext context = createSessionContext(); - ElementContext elementContext = new ElementContext(vspId, version.getId()); - - return zusammenAdaptor.listElementsByName(context, elementContext, null, - ElementType.OrchestrationTemplateCandidate.name()).stream() - .noneMatch(candidateSub -> ElementType.OrchestrationTemplateCandidateValidationData.name() - .equals(candidateSub.getInfo().getName())); - } - - private boolean isEqual(OrchestrationTemplateEntity orchestrationTemplateEntity, - OrchestrationTemplateCandidateData orchestrationTemplateCandidateData) { - return orchestrationTemplateEntity.getFileName() - .equals(orchestrationTemplateCandidateData.getFileName()) && - orchestrationTemplateEntity.getFileSuffix() - .equals(orchestrationTemplateCandidateData.getFileSuffix()); - } - - private boolean isProcessedEntityValid(OrchestrationTemplateEntity orchestrationTemplateEntity) { - return !orchestrationTemplateEntity.getValidationData().contains(ErrorLevel.ERROR.name()); - } - - private void populateCandidateValidationData(SessionContext context, - ElementContext elementContext, - Id candidateValidationElementId, - String validationData) { - ZusammenElement candidateValidationElement = - buildStructuralElement(ElementType.OrchestrationTemplateCandidateValidationData, - Action.UPDATE); - candidateValidationElement.setElementId(candidateValidationElementId); - candidateValidationElement.setData(new ByteArrayInputStream(validationData.getBytes())); - - zusammenAdaptor.saveElement(context, elementContext, candidateValidationElement, - "Healed Orchestration Template Candidate Validation data"); - } - - private void populateOrchestrationTemplateStructure(InputStream processedFile, - Id structureElementId, VspDetails vspDetails, - SessionContext context, - ElementContext elementContext) - throws Exception { - byte[] byteData = FileUtils.toByteArray(processedFile); - FileContentHandler contentMap = - CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.ZIP, byteData); - OrchestrationTemplateCandidateData orchestrationTemplateEntityData = - new CandidateEntityBuilder(candidateService) - .buildCandidateEntityFromZip(vspDetails, byteData, contentMap, null); - String fileDataStructure = orchestrationTemplateEntityData.getFilesDataStructure(); - - ZusammenElement orchestrationTemplateStructure = - buildStructuralElement(ElementType.OrchestrationTemplateStructure, Action.UPDATE); - orchestrationTemplateStructure.setElementId(structureElementId); - orchestrationTemplateStructure.setData(new ByteArrayInputStream(fileDataStructure.getBytes())); - - zusammenAdaptor.saveElement(context, elementContext, orchestrationTemplateStructure, - "Healed Orchestration Template Structure"); - } - - private Element getOrchestrationTemplateElement(SessionContext context, - ElementContext elementContext) { - ElementInfo vspModelElement = zusammenAdaptor - .getElementInfoByName(context, elementContext, null, ElementType.VspModel.name()) - .orElseThrow(() -> new CoreException(new ErrorCode.ErrorCodeBuilder() - .withMessage(MISSING_VSP_MODEL_ERROR).build())); - - return getElement(context, elementContext, vspModelElement.getId(), - ElementType.OrchestrationTemplate, MISSING_ORCHESTRATION_TEMPLATE_ERROR); - } - - private Element getOrchestrationTemplateValidationElement( - Collection orchestrationTemplateSubs) { - return orchestrationTemplateSubs.stream() - .filter(orchestrationTemplateSub -> ElementType.OrchestrationTemplateValidationData.name() - .equals(orchestrationTemplateSub.getInfo().getName())) - .findFirst() - .orElseThrow(() -> new CoreException(new ErrorCode.ErrorCodeBuilder() - .withMessage(MISSING_ORCHESTRATION_TEMPLATE_VALIDATE_DATA_ERROR).build())); - - } - - private Element getCandidateContentElement(Collection candidateSubs) { - return candidateSubs.stream() - .filter(candidateSub -> ElementType.OrchestrationTemplateCandidateContent.name() - .equals(candidateSub.getInfo().getName())) - .findFirst() - .orElseThrow(() -> new CoreException(new ErrorCode.ErrorCodeBuilder() - .withMessage(MISSING_ORCHESTRATION_TEMPLATE_CANDIDATE_CONTENT_ERROR).build())); - } - - private Id createIfAbsentStructureElementId(Collection orchestrationTemplateSubs, - SessionContext context, ElementContext elementContext, - Element orchestrationTemplateElement) { - return orchestrationTemplateSubs.stream() - .filter(orchestrationTemplateSub -> ElementType.OrchestrationTemplateStructure.name() - .equals(orchestrationTemplateSub.getInfo().getName())) - .findFirst().map(Element::getElementId) - .orElse(addStructureSubElement(context, elementContext, - ElementType.OrchestrationTemplateStructure, - orchestrationTemplateElement.getElementId())); - } - - private Id creatIfAbsentCandidateValidationElementId(Collection candidateSubs, - SessionContext context, ElementContext - elementContext, Element candidateElement) { - return candidateSubs.stream() - .filter(candidateSub -> ElementType.OrchestrationTemplateCandidateValidationData.name() - .equals(candidateSub.getInfo().getName())) - .findFirst().map(Element::getElementId) - .orElse(addStructureSubElement(context, elementContext, - ElementType.OrchestrationTemplateCandidateValidationData, - candidateElement.getElementId())); - } - - private Element getElement(SessionContext context, ElementContext elementContext, - Id parentElementId, ElementType elementType, String errorMessage) { - return zusammenAdaptor - .getElementByName(context, elementContext, parentElementId, elementType.name()) - .orElseThrow(() -> new CoreException( - new ErrorCode.ErrorCodeBuilder().withMessage(errorMessage).build())); - } - - private OrchestrationTemplateEntity getOrchestrationTemplateEntity( - Element orchestrationTemplateElement, Element validationDataElement) { - OrchestrationTemplateEntity orchestrationTemplateEntity = new OrchestrationTemplateEntity(); - - if (isNotEmpty(orchestrationTemplateElement.getData())) { - orchestrationTemplateEntity.setContentData( - ByteBuffer.wrap(FileUtils.toByteArray(orchestrationTemplateElement.getData()))); + + NetworkPackageHealer(VendorSoftwareProductInfoDao vspInfoDao, ZusammenAdaptor zusammenAdaptor, + CandidateEntityBuilder candidateEntityBuilder) { + this.vspInfoDao = vspInfoDao; + this.zusammenAdaptor = zusammenAdaptor; + this.candidateEntityBuilder = candidateEntityBuilder; } - orchestrationTemplateEntity.setFileSuffix( - validationDataElement.getInfo().getProperty(InfoPropertyName.FILE_SUFFIX.getVal())); - orchestrationTemplateEntity.setFileName( - validationDataElement.getInfo().getProperty(InfoPropertyName.FILE_NAME.getVal())); - if (isNotEmpty(validationDataElement.getData())) { - orchestrationTemplateEntity - .setValidationData(new String(FileUtils.toByteArray(validationDataElement.getData()))); + + @Override + public boolean isHealingNeeded(String itemId, Version version) { + return OnboardingMethod.NetworkPackage.name() + .equals(vspInfoDao.get(new VspDetails(itemId, version)).getOnboardingMethod()) + && isVspMissingAddedElements(itemId, version); } - return orchestrationTemplateEntity; - } - - private OrchestrationTemplateCandidateData getCandidateData(Element candidateElement, - Element candidateContentElement) { - OrchestrationTemplateCandidateData candidateData = new OrchestrationTemplateCandidateData(); - candidateData - .setFilesDataStructure(new String(FileUtils.toByteArray(candidateElement.getData()))); - candidateData - .setContentData(ByteBuffer.wrap(FileUtils.toByteArray(candidateContentElement.getData()))); - candidateData.setFileSuffix( - candidateContentElement.getInfo().getProperty(InfoPropertyName.FILE_SUFFIX.getVal())); - candidateData.setFileName( - candidateContentElement.getInfo().getProperty(InfoPropertyName.FILE_NAME.getVal())); - return candidateData; - } - - private Id addStructureSubElement(SessionContext context, ElementContext elementContext, - ElementType elementType, Id parentElementId) { - ZusammenElement newElement = buildStructuralElement(elementType, Action.CREATE); - - ZusammenElement parentElement = buildElement(parentElementId, Action.IGNORE); - parentElement.addSubElement(newElement); - - return zusammenAdaptor.saveElement(context, elementContext, parentElement, - String.format("Add element %s under element id %s", elementType.name(), parentElementId)) - .getSubElements().iterator().next().getElementId(); - } - - private void emptyStructureElementAndSub(SessionContext context, ElementContext elementContext, - Id elementId, ElementType elementType, Id subElementId, - ElementType subElementType) { - ZusammenElement subElement = buildStructuralElement(subElementType, Action.UPDATE); - subElement.setElementId(subElementId); - subElement.setData(new ByteArrayInputStream(EMPTY_DATA_BYTES)); - - ZusammenElement element = buildStructuralElement(elementType, Action.UPDATE); - element.setElementId(elementId); - element.setData(new ByteArrayInputStream(EMPTY_DATA_BYTES)); - element.addSubElement(subElement); - - zusammenAdaptor.saveElement(context, elementContext, element, - String.format("Empty element %s and its sub element %s", elementType.name(), - subElementType.name())); - } - - public enum InfoPropertyName { - FILE_SUFFIX("fileSuffix"), - FILE_NAME("fileName"); - - private String val; - - InfoPropertyName(String val) { - this.val = val; + + @Override + public void heal(String itemId, Version version) throws Exception { + SessionContext context = createSessionContext(); + ElementContext elementContext = new ElementContext(itemId, version.getId()); + + Element candidateElement = getElement(context, elementContext, null, ElementType.OrchestrationTemplateCandidate, + MISSING_ORCHESTRATION_TEMPLATE_CANDIDATE_ERROR); + + Collection candidateSubs = + zusammenAdaptor.listElementData(context, elementContext, candidateElement.getElementId()); + + Id candidateValidationElementId = + creatIfAbsentCandidateValidationElementId(candidateSubs, context, elementContext, candidateElement); + + Element orchestrationTemplateElement = getOrchestrationTemplateElement(context, elementContext); + + Collection orchestrationTemplateSubs = + zusammenAdaptor.listElementData(context, elementContext, orchestrationTemplateElement.getElementId()); + + Id structureElementId = createIfAbsentStructureElementId(orchestrationTemplateSubs, context, elementContext, + orchestrationTemplateElement); + + Element orchestrationTemplateValidationElement = + getOrchestrationTemplateValidationElement(orchestrationTemplateSubs); + + OrchestrationTemplateEntity orchestrationTemplateEntity = + getOrchestrationTemplateEntity(orchestrationTemplateElement, orchestrationTemplateValidationElement); + + if (StringUtils.isEmpty(orchestrationTemplateEntity.getFileSuffix())) { + return; + } + + Element candidateContentElement = getCandidateContentElement(candidateSubs); + + VspDetails vspDetails = vspInfoDao.get(new VspDetails(itemId, version)); + if (isEqual(orchestrationTemplateEntity, getCandidateData(candidateElement, candidateContentElement))) { + + if (isProcessedEntityValid(orchestrationTemplateEntity)) { + emptyStructureElementAndSub(context, elementContext, candidateElement.getElementId(), + ElementType.OrchestrationTemplateCandidate, candidateContentElement.getElementId(), + ElementType.OrchestrationTemplateCandidateContent); + + populateOrchestrationTemplateStructure(orchestrationTemplateElement.getData(), structureElementId, + vspDetails, context, elementContext); + } else { + emptyStructureElementAndSub(context, elementContext, orchestrationTemplateElement.getElementId(), + ElementType.OrchestrationTemplate, orchestrationTemplateValidationElement.getElementId(), + ElementType.OrchestrationTemplateValidationData); + + populateCandidateValidationData(context, elementContext, candidateValidationElementId, + orchestrationTemplateEntity.getValidationData()); + } + } else { + populateOrchestrationTemplateStructure(orchestrationTemplateElement.getData(), structureElementId, + vspDetails, context, elementContext); + } + } + + private boolean isVspMissingAddedElements(String vspId, Version version) { + SessionContext context = createSessionContext(); + ElementContext elementContext = new ElementContext(vspId, version.getId()); + + return zusammenAdaptor.listElementsByName(context, elementContext, null, + ElementType.OrchestrationTemplateCandidate.name()).stream().noneMatch( + candidateSub -> ElementType.OrchestrationTemplateCandidateValidationData.name() + .equals(candidateSub.getInfo().getName())); + } + + private boolean isEqual(OrchestrationTemplateEntity orchestrationTemplateEntity, + OrchestrationTemplateCandidateData orchestrationTemplateCandidateData) { + return orchestrationTemplateEntity.getFileName().equals(orchestrationTemplateCandidateData.getFileName()) + && orchestrationTemplateEntity.getFileSuffix() + .equals(orchestrationTemplateCandidateData.getFileSuffix()); } - private String getVal() { - return val; + private boolean isProcessedEntityValid(OrchestrationTemplateEntity orchestrationTemplateEntity) { + return !orchestrationTemplateEntity.getValidationData().contains(ErrorLevel.ERROR.name()); } - } - private boolean isNotEmpty(InputStream elementData) { - byte[] byteElementData; - if (Objects.isNull(elementData)) { - return false; + private void populateCandidateValidationData(SessionContext context, ElementContext elementContext, + Id candidateValidationElementId, String validationData) { + ZusammenElement candidateValidationElement = + buildStructuralElement(ElementType.OrchestrationTemplateCandidateValidationData, Action.UPDATE); + candidateValidationElement.setElementId(candidateValidationElementId); + candidateValidationElement.setData(new ByteArrayInputStream(validationData.getBytes())); + + zusammenAdaptor.saveElement(context, elementContext, candidateValidationElement, + "Healed Orchestration Template Candidate Validation data"); + } + + private void populateOrchestrationTemplateStructure(InputStream processedFile, Id structureElementId, + VspDetails vspDetails, SessionContext context, ElementContext elementContext) throws Exception { + byte[] byteData = FileUtils.toByteArray(processedFile); + FileContentHandler contentMap = CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.ZIP, byteData); + OrchestrationTemplateCandidateData orchestrationTemplateEntityData = + candidateEntityBuilder + .buildCandidateEntityFromZip(vspDetails, byteData, contentMap, null); + String fileDataStructure = orchestrationTemplateEntityData.getFilesDataStructure(); + + ZusammenElement orchestrationTemplateStructure = + buildStructuralElement(ElementType.OrchestrationTemplateStructure, Action.UPDATE); + orchestrationTemplateStructure.setElementId(structureElementId); + orchestrationTemplateStructure.setData(new ByteArrayInputStream(fileDataStructure.getBytes())); + + zusammenAdaptor.saveElement(context, elementContext, orchestrationTemplateStructure, + "Healed Orchestration Template Structure"); } - try { - byteElementData = IOUtils.toByteArray(elementData); - } catch (IOException e) { - return true; + + private Element getOrchestrationTemplateElement(SessionContext context, ElementContext elementContext) { + ElementInfo vspModelElement = + zusammenAdaptor.getElementInfoByName(context, elementContext, null, ElementType.VspModel.name()) + .orElseThrow(() -> new CoreException( + new ErrorCode.ErrorCodeBuilder().withMessage(MISSING_VSP_MODEL_ERROR).build())); + + return getElement(context, elementContext, vspModelElement.getId(), ElementType.OrchestrationTemplate, + MISSING_ORCHESTRATION_TEMPLATE_ERROR); + } + + private Element getOrchestrationTemplateValidationElement(Collection orchestrationTemplateSubs) { + return orchestrationTemplateSubs.stream() + .filter(orchestrationTemplateSub -> ElementType.OrchestrationTemplateValidationData.name() + .equals(orchestrationTemplateSub.getInfo() + .getName())).findFirst().orElseThrow( + () -> new CoreException(new ErrorCode.ErrorCodeBuilder() + .withMessage(MISSING_ORCHESTRATION_TEMPLATE_VALIDATE_DATA_ERROR) + .build())); + + } + + private Element getCandidateContentElement(Collection candidateSubs) { + return candidateSubs.stream().filter(candidateSub -> ElementType.OrchestrationTemplateCandidateContent.name() + .equals(candidateSub.getInfo().getName())) + .findFirst().orElseThrow(() -> new CoreException(new ErrorCode.ErrorCodeBuilder().withMessage( + MISSING_ORCHESTRATION_TEMPLATE_CANDIDATE_CONTENT_ERROR).build())); + } + + private Id createIfAbsentStructureElementId(Collection orchestrationTemplateSubs, SessionContext context, + ElementContext elementContext, Element orchestrationTemplateElement) { + return orchestrationTemplateSubs.stream() + .filter(orchestrationTemplateSub -> ElementType.OrchestrationTemplateStructure.name() + .equals(orchestrationTemplateSub.getInfo() + .getName())).findFirst() + .map(Element::getElementId).orElse(addStructureSubElement(context, elementContext, + ElementType.OrchestrationTemplateStructure, orchestrationTemplateElement.getElementId())); + } + + private Id creatIfAbsentCandidateValidationElementId(Collection candidateSubs, SessionContext context, + ElementContext elementContext, Element candidateElement) { + return candidateSubs.stream() + .filter(candidateSub -> ElementType.OrchestrationTemplateCandidateValidationData.name() + .equals(candidateSub.getInfo().getName())).findFirst() + .map(Element::getElementId).orElse(addStructureSubElement(context, elementContext, + ElementType.OrchestrationTemplateCandidateValidationData, candidateElement.getElementId())); + } + + private Element getElement(SessionContext context, ElementContext elementContext, Id parentElementId, + ElementType elementType, String errorMessage) { + return zusammenAdaptor.getElementByName(context, elementContext, parentElementId, elementType.name()) + .orElseThrow(() -> new CoreException( + new ErrorCode.ErrorCodeBuilder().withMessage(errorMessage).build())); + } + + private OrchestrationTemplateEntity getOrchestrationTemplateEntity(Element orchestrationTemplateElement, + Element validationDataElement) { + OrchestrationTemplateEntity orchestrationTemplateEntity = new OrchestrationTemplateEntity(); + + if (isNotEmpty(orchestrationTemplateElement.getData())) { + orchestrationTemplateEntity + .setContentData(ByteBuffer.wrap(FileUtils.toByteArray(orchestrationTemplateElement.getData()))); + } + orchestrationTemplateEntity + .setFileSuffix(validationDataElement.getInfo().getProperty(InfoPropertyName.FILE_SUFFIX.getVal())); + orchestrationTemplateEntity + .setFileName(validationDataElement.getInfo().getProperty(InfoPropertyName.FILE_NAME.getVal())); + if (isNotEmpty(validationDataElement.getData())) { + orchestrationTemplateEntity + .setValidationData(new String(FileUtils.toByteArray(validationDataElement.getData()))); + } + return orchestrationTemplateEntity; + } + + private OrchestrationTemplateCandidateData getCandidateData(Element candidateElement, + Element candidateContentElement) { + OrchestrationTemplateCandidateData candidateData = new OrchestrationTemplateCandidateData(); + candidateData.setFilesDataStructure(new String(FileUtils.toByteArray(candidateElement.getData()))); + candidateData.setContentData(ByteBuffer.wrap(FileUtils.toByteArray(candidateContentElement.getData()))); + candidateData + .setFileSuffix(candidateContentElement.getInfo().getProperty(InfoPropertyName.FILE_SUFFIX.getVal())); + candidateData.setFileName(candidateContentElement.getInfo().getProperty(InfoPropertyName.FILE_NAME.getVal())); + return candidateData; + } + + private Id addStructureSubElement(SessionContext context, ElementContext elementContext, ElementType elementType, + Id parentElementId) { + ZusammenElement newElement = buildStructuralElement(elementType, Action.CREATE); + + ZusammenElement parentElement = buildElement(parentElementId, Action.IGNORE); + parentElement.addSubElement(newElement); + + return zusammenAdaptor.saveElement(context, elementContext, parentElement, + String.format("Add element %s under element id %s", elementType.name(), parentElementId)) + .getSubElements().iterator().next().getElementId(); + } + + private void emptyStructureElementAndSub(SessionContext context, ElementContext elementContext, Id elementId, + ElementType elementType, Id subElementId, ElementType subElementType) { + ZusammenElement subElement = buildStructuralElement(subElementType, Action.UPDATE); + subElement.setElementId(subElementId); + subElement.setData(new ByteArrayInputStream(EMPTY_DATA_BYTES)); + + ZusammenElement element = buildStructuralElement(elementType, Action.UPDATE); + element.setElementId(elementId); + element.setData(new ByteArrayInputStream(EMPTY_DATA_BYTES)); + element.addSubElement(subElement); + + zusammenAdaptor.saveElement(context, elementContext, element, + String.format("Empty element %s and its sub element %s", elementType.name(), subElementType.name())); + } + + public enum InfoPropertyName { + FILE_SUFFIX("fileSuffix"), FILE_NAME("fileName"); + + private String val; + + InfoPropertyName(String val) { + this.val = val; + } + + private String getVal() { + return val; + } + } + + private boolean isNotEmpty(InputStream elementData) { + byte[] byteElementData; + if (Objects.isNull(elementData)) { + return false; + } + try { + byteElementData = IOUtils.toByteArray(elementData); + } catch (IOException e) { + return true; + } + return !ArrayUtils.isEmpty(byteElementData); } - return !ArrayUtils.isEmpty(byteElementData); - } } -- cgit 1.2.3-korg