diff options
Diffstat (limited to 'catalog-be')
8 files changed, 112 insertions, 212 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java index d8f85617c0..cac36cbe0e 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java @@ -396,7 +396,7 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { .exportComponent(parent) .left().map(toscaRepresentation -> { log.debug("Tosca yaml exported for component {} ", parent.getUniqueId()); - return toscaRepresentation.getMainYaml().getBytes(); + return toscaRepresentation.getMainYaml(); }).right().map(toscaError -> { log.debug("Failed export tosca yaml for component {} error {}", parent.getUniqueId(), toscaError); return new ByActionStatusComponentException(componentsUtils.convertFromToscaError(toscaError)); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java index 291d2bb508..241148b531 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CsarUtils.java @@ -253,7 +253,7 @@ public class CsarUtils { .getToscaArtifacts() .get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE); - Either<MainYamlWithDependencies, ResponseFormat> toscaRepresentation = + Either<ToscaRepresentation, ResponseFormat> toscaRepresentation = fetchToscaRepresentation(component, getFromCS, artifactDef); // This should not be done but in order to keep the refactoring small enough we stop here. @@ -261,8 +261,8 @@ public class CsarUtils { byte[] mainYaml; List<Triple<String, String, Component>> dependencies; if(toscaRepresentation.isLeft()) { - mainYaml = toscaRepresentation.left().value().mainYaml; - dependencies = toscaRepresentation.left().value().dependencies.orElse(null); + mainYaml = toscaRepresentation.left().value().getMainYaml(); + dependencies = toscaRepresentation.left().value().getDependencies().getOrElse(new ArrayList<>()); } else { return Either.right(toscaRepresentation.right().value()); } @@ -315,7 +315,7 @@ public class CsarUtils { return writeAllFilesToCsar(component, collectedComponentCsarDefinition.left().value(), zip, isInCertificationRequest); } - private Either<MainYamlWithDependencies, ResponseFormat> fetchToscaRepresentation( + private Either<ToscaRepresentation, ResponseFormat> fetchToscaRepresentation( Component component, boolean getFromCS, ArtifactDefinition artifactDef @@ -326,14 +326,14 @@ public class CsarUtils { !(lifecycleState == LifecycleStateEnum.NOT_CERTIFIED_CHECKIN || lifecycleState == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); - Either<MainYamlWithDependencies, ResponseFormat> toscaRepresentation = + Either<ToscaRepresentation, ResponseFormat> toscaRepresentation = shouldBeFetchedFromCassandra ? fetchToscaRepresentation(artifactDef) : generateToscaRepresentation(component); return toscaRepresentation.left().bind(iff( - myd -> !myd.dependencies.isPresent(), - myd -> fetchToscaTemplateDependencies(myd.mainYaml, component) + myd -> !myd.getDependencies().isDefined(), + myd -> fetchToscaTemplateDependencies(myd.getMainYaml(), component) )); } @@ -341,7 +341,7 @@ public class CsarUtils { return l -> p.test(l) ? ifTrue.apply(l) : Either.left(l); } - private Either<MainYamlWithDependencies, ResponseFormat> fetchToscaTemplateDependencies( + private Either<ToscaRepresentation, ResponseFormat> fetchToscaTemplateDependencies( byte[] mainYml, Component component ) { @@ -349,47 +349,21 @@ public class CsarUtils { log.debug("Failed to retrieve dependencies for component {}, error {}", component.getUniqueId(), toscaError); return componentsUtils.getResponseFormat(componentsUtils.convertFromToscaError(toscaError)); - }).left().map(tt -> MainYamlWithDependencies.make(mainYml, tt)); + }).left().map(tt -> ToscaRepresentation.make(mainYml, tt)); } - private Either<MainYamlWithDependencies, ResponseFormat> generateToscaRepresentation(Component component) { + private Either<ToscaRepresentation, ResponseFormat> generateToscaRepresentation(Component component) { return toscaExportUtils.exportComponent(component).right().map(toscaError -> { - log.debug("exportComponent failed", toscaError); + log.debug("exportComponent failed {}", toscaError); return componentsUtils.getResponseFormat(componentsUtils.convertFromToscaError(toscaError)); - }).left().map(MainYamlWithDependencies::make); + }); } - private Either<MainYamlWithDependencies, ResponseFormat> fetchToscaRepresentation(ArtifactDefinition artifactDef) { + private Either<ToscaRepresentation, ResponseFormat> fetchToscaRepresentation(ArtifactDefinition artifactDef) { return getFromCassandra(artifactDef.getEsId()).right().map(as -> { log.debug(ARTIFACT_NAME_UNIQUE_ID, artifactDef.getArtifactName(), artifactDef.getUniqueId()); return componentsUtils.getResponseFormat(as); - }).left().map(MainYamlWithDependencies::make); - } - - // TODO: Refactor the ToscaRepresentation class in order to remove the following one - // This will be done in a separate change - private static class MainYamlWithDependencies { - - private final byte[] mainYaml; - private final Optional<List<Triple<String, String, Component>>> dependencies; - - private MainYamlWithDependencies(byte[] mainYaml, - Optional<List<Triple<String, String, Component>>> dependencies) { - this.mainYaml = mainYaml; - this.dependencies = dependencies; - } - - public static MainYamlWithDependencies make(byte[] mainYaml) { - return new MainYamlWithDependencies(mainYaml, Optional.empty()); - } - - public static MainYamlWithDependencies make(ToscaRepresentation tr) { - return new MainYamlWithDependencies(tr.getMainYaml().getBytes(), Optional.ofNullable(tr.getDependencies())); - } - - public static MainYamlWithDependencies make(byte[] mainYaml, ToscaTemplate tt) { - return new MainYamlWithDependencies(mainYaml, Optional.ofNullable(tt.getDependencies())); - } + }).left().map(ToscaRepresentation::make); } /** @@ -686,7 +660,7 @@ public class CsarUtils { boolean isAssociatedComponent, ZipWriter zw ) { - Either<String, ToscaError> yml = toscaExportUtils + Either<byte[], ToscaError> yml = toscaExportUtils .exportComponentInterface(component, isAssociatedComponent) .left().map(ToscaRepresentation::getMainYaml); @@ -705,7 +679,7 @@ public class CsarUtils { if (cassandraId == null || cassandraId.isEmpty()) { return toscaExportUtils.exportComponent(childComponent) .right().map(toscaErrorToActionStatus(childComponent)) - .left().map(toscaRepresentation -> toscaRepresentation.getMainYaml().getBytes()); + .left().map(ToscaRepresentation::getMainYaml); } else { return getFromCassandra(cassandraId); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java index a492f01953..aa9cd8ce49 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java @@ -170,15 +170,7 @@ public class ToscaExportHandler { public ToscaExportHandler(){} public Either<ToscaRepresentation, ToscaError> exportComponent(Component component) { - - Either<ToscaTemplate, ToscaError> toscaTemplateRes = convertToToscaTemplate(component); - if (toscaTemplateRes.isRight()) { - return Either.right(toscaTemplateRes.right().value()); - } - - ToscaTemplate toscaTemplate = toscaTemplateRes.left().value(); - ToscaRepresentation toscaRepresentation = this.createToscaRepresentation(toscaTemplate); - return Either.left(toscaRepresentation); + return convertToToscaTemplate(component).left().map(this::createToscaRepresentation); } public Either<ToscaRepresentation, ToscaError> exportComponentInterface(final Component component, @@ -227,11 +219,7 @@ public class ToscaExportHandler { sb.append(yamlAsString); sb.append(ConfigurationManager.getConfigurationManager().getConfiguration().getHeatEnvArtifactFooter()); - ToscaRepresentation toscaRepresentation = new ToscaRepresentation(); - toscaRepresentation.setMainYaml(sb.toString()); - toscaRepresentation.setDependencies(toscaTemplate.getDependencies()); - - return toscaRepresentation; + return ToscaRepresentation.make(sb.toString().getBytes(), toscaTemplate); } public Either<ToscaTemplate, ToscaError> getDependencies(Component component) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaRepresentation.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaRepresentation.java index 49fd91cfed..1ca95576a8 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaRepresentation.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaRepresentation.java @@ -20,30 +20,36 @@ package org.openecomp.sdc.be.tosca; +import io.vavr.control.Option; +import lombok.Getter; import org.apache.commons.lang3.tuple.Triple; import org.openecomp.sdc.be.model.Component; import java.util.List; +import org.openecomp.sdc.be.tosca.model.ToscaTemplate; public class ToscaRepresentation { - private String mainYaml; - private List<Triple<String, String, Component>> dependencies; + @Getter + private final byte[] mainYaml; - public String getMainYaml() { - return mainYaml; - } + @Getter + private final Option<List<Triple<String, String, Component>>> dependencies; - public void setMainYaml(String mainYaml) { + private ToscaRepresentation(byte[] mainYaml, Option<List<Triple<String, String, Component>>> dependencies) { this.mainYaml = mainYaml; + this.dependencies = dependencies; } - public List<Triple<String, String, Component>> getDependencies() { - return dependencies; + public static ToscaRepresentation make(byte[] mainYaml) { + return new ToscaRepresentation(mainYaml, Option.none()); } - public void setDependencies(List<Triple<String, String, Component>> dependancies) { - this.dependencies = dependancies; + public static ToscaRepresentation make(byte[] mainYaml, List<Triple<String, String, Component>> dependencies) { + return new ToscaRepresentation(mainYaml, Option.of(dependencies)); } + public static ToscaRepresentation make(byte[] mainYaml, ToscaTemplate tt) { + return new ToscaRepresentation(mainYaml, Option.of(tt.getDependencies())); + } } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ZipWriter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ZipWriter.java index 4740fb2042..916895fff4 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ZipWriter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ZipWriter.java @@ -54,10 +54,14 @@ public interface ZipWriter { * * @param entryName The entry's name to use in the zip file */ - default Function<String, Try<Void>> write(String entryName) { + default Function<String, Try<Void>> writeString(String entryName) { return payload -> write(entryName, payload.getBytes()); } + default Function<byte[], Try<Void>> write(String entryName) { + return payload -> write(entryName, payload); + } + /** * Builds a ZipWriter that outputs the data on a {@link java.util.zip.ZipOutputStream} * @param zos the target {@link java.util.zip.ZipOutputStream} diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/CsarUtilsTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/CsarUtilsTest.java index 07cf727a9e..4aa967be47 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/CsarUtilsTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/CsarUtilsTest.java @@ -235,8 +235,7 @@ public class CsarUtilsTest extends BeConfDependentTest { ByteBuffer bufferData = ByteBuffer.wrap(data); artifactData.setData(bufferData); - ToscaRepresentation tosca = new ToscaRepresentation(); - tosca.setMainYaml("value"); + ToscaRepresentation tosca = ToscaRepresentation.make("value".getBytes()); Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData)); @@ -322,8 +321,7 @@ public class CsarUtilsTest extends BeConfDependentTest { dependencies.add(triple); toscaTemplate.setDependencies(dependencies); - ToscaRepresentation tosca = new ToscaRepresentation(); - tosca.setMainYaml("value"); + ToscaRepresentation tosca = ToscaRepresentation.make("value".getBytes()); Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData)); @@ -438,8 +436,7 @@ public class CsarUtilsTest extends BeConfDependentTest { dependencies.add(triple); toscaTemplate.setDependencies(dependencies); - ToscaRepresentation tosca = new ToscaRepresentation(); - tosca.setMainYaml(new String(contentData, StandardCharsets.UTF_8)); + ToscaRepresentation tosca = ToscaRepresentation.make(contentData); Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData)); @@ -489,8 +486,7 @@ public class CsarUtilsTest extends BeConfDependentTest { dependencies.add(triple); toscaTemplate.setDependencies(dependencies); - ToscaRepresentation tosca = new ToscaRepresentation(); - tosca.setMainYaml(new String(contentData, StandardCharsets.UTF_8)); + ToscaRepresentation tosca = ToscaRepresentation.make(contentData); Mockito.when(artifactCassandraDao.getArtifact(Mockito.any(String.class))).thenReturn(Either.left(artifactData)); @@ -579,8 +575,7 @@ public class CsarUtilsTest extends BeConfDependentTest { @Test public void testWriteComponentInterface() throws IOException { String fileName = "name.hello"; - ToscaRepresentation tosca = new ToscaRepresentation(); - tosca.setMainYaml("value"); + ToscaRepresentation tosca = ToscaRepresentation.make("value".getBytes()); Mockito.when(toscaExportUtils.exportComponentInterface(Mockito.any(Component.class), Mockito.any(Boolean.class))) .thenReturn(Either.left(tosca)); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaRepresentationTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaRepresentationTest.java deleted file mode 100644 index 3da170883b..0000000000 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaRepresentationTest.java +++ /dev/null @@ -1,79 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * 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. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.sdc.be.tosca; - -import org.apache.commons.lang3.tuple.Triple; -import org.junit.Test; -import org.openecomp.sdc.be.model.Component; - -import java.util.List; - - -public class ToscaRepresentationTest { - - private ToscaRepresentation createTestSubject() { - return new ToscaRepresentation(); - } - - - @Test - public void testGetMainYaml() throws Exception { - ToscaRepresentation testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getMainYaml(); - } - - - @Test - public void testSetMainYaml() throws Exception { - ToscaRepresentation testSubject; - String mainYaml = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setMainYaml(mainYaml); - } - - - @Test - public void testGetDependencies() throws Exception { - ToscaRepresentation testSubject; - List<Triple<String, String, Component>> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getDependencies(); - } - - - @Test - public void testSetDependencies() throws Exception { - ToscaRepresentation testSubject; - List<Triple<String, String, Component>> dependencies = null; - - // default test - testSubject = createTestSubject(); - testSubject.setDependencies(dependencies); - } -} diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsConverterTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsConverterTest.java index 07ad8d5db9..93a45ad20f 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsConverterTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsConverterTest.java @@ -16,9 +16,20 @@ package org.openecomp.sdc.be.tosca.utils; +import static org.mockito.Mockito.mock; +import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.SELF; +import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.addInterfaceTypeElement; + import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.function.Function; import org.apache.commons.collections4.MapUtils; import org.junit.Assert; import org.junit.Before; @@ -41,7 +52,6 @@ import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.Service; import org.openecomp.sdc.be.model.ServiceMetadataDefinition; -import org.openecomp.sdc.tosca.datatypes.ToscaFunctions; import org.openecomp.sdc.be.tosca.InterfacesOperationsConverter; import org.openecomp.sdc.be.tosca.PropertyConvertor; import org.openecomp.sdc.be.tosca.ToscaExportHandler; @@ -49,17 +59,8 @@ import org.openecomp.sdc.be.tosca.ToscaRepresentation; import org.openecomp.sdc.be.tosca.model.ToscaNodeType; import org.openecomp.sdc.be.tosca.model.ToscaTemplate; import org.openecomp.sdc.common.util.YamlToObjectConverter; +import org.openecomp.sdc.tosca.datatypes.ToscaFunctions; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; - -import static org.mockito.Mockito.mock; -import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.SELF; -import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.addInterfaceTypeElement; @RunWith(MockitoJUnitRunner.class) public class InterfacesOperationsConverterTest { @@ -109,8 +110,10 @@ public class InterfacesOperationsConverterTest { template.setInterface_types(interfaceTypeElement); final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); - Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations")); - Assert.assertTrue(toscaRepresentation.getMainYaml().contains("NodeTypeName")); + Assert.assertTrue(all( + containsAll("NodeTypeName"), + containsNone("operations") + ).apply(new String(toscaRepresentation.getMainYaml()))); } @Test @@ -135,8 +138,10 @@ public class InterfacesOperationsConverterTest { template.setInterface_types(interfaceTypeElement); final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); - Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations")); - Assert.assertTrue(toscaRepresentation.getMainYaml().contains("NodeTypeName")); + Assert.assertTrue(all( + containsAll("NodeTypeName"), + containsNone("operations") + ).apply(new String(toscaRepresentation.getMainYaml()))); } @Test @@ -161,15 +166,13 @@ public class InterfacesOperationsConverterTest { template.setNode_types(nodeTypes); final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); - String mainYaml = toscaRepresentation.getMainYaml(); - Assert.assertFalse(mainYaml.contains("operations")); - Assert.assertTrue(mainYaml.contains("resourceName:")); - Assert.assertTrue(mainYaml.contains("inputs:")); + String mainYaml = new String(toscaRepresentation.getMainYaml()); + Assert.assertTrue(all( + containsAll("resourceName:", "inputs:", "has description", MAPPED_PROPERTY_NAME, "com.some.resource.or.other.resourceName"), + containsNone("operations", "defaultp") + ).apply(mainYaml)); + validateOperationInputs(mainYaml, 2, null); - Assert.assertFalse(mainYaml.contains("defaultp")); - Assert.assertTrue(mainYaml.contains("has description")); - Assert.assertTrue(mainYaml.contains(MAPPED_PROPERTY_NAME)); - Assert.assertTrue(mainYaml.contains("com.some.resource.or.other.resourceName")); } @Test @@ -192,15 +195,12 @@ public class InterfacesOperationsConverterTest { nodeTypes.put(NODE_TYPE_NAME, nodeType); template.setNode_types(nodeTypes); final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); - String mainYaml = toscaRepresentation.getMainYaml(); - Assert.assertFalse(mainYaml.contains("operations")); - Assert.assertTrue(mainYaml.contains("serviceName:")); - Assert.assertTrue(mainYaml.contains("inputs:")); + String mainYaml = new String(toscaRepresentation.getMainYaml()); + Assert.assertTrue(all( + containsAll("serviceName", "inputs:", "has description", MAPPED_PROPERTY_NAME, "com.some.service.or.other.serviceName"), + containsNone("operations", "defaultp") + ).apply(mainYaml)); validateOperationInputs(mainYaml, 2, null); - Assert.assertFalse(mainYaml.contains("defaultp")); - Assert.assertTrue(mainYaml.contains("has description")); - Assert.assertTrue(mainYaml.contains(MAPPED_PROPERTY_NAME)); - Assert.assertTrue(mainYaml.contains("com.some.service.or.other.serviceName")); } @@ -226,15 +226,12 @@ public class InterfacesOperationsConverterTest { template.setNode_types(nodeTypes); final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); - String mainYaml = toscaRepresentation.getMainYaml(); - Assert.assertFalse(mainYaml.contains("operations")); - Assert.assertTrue(mainYaml.contains("resourceName:")); - Assert.assertTrue(mainYaml.contains("inputs:")); + String mainYaml = new String(toscaRepresentation.getMainYaml()); + Assert.assertTrue(all( + containsAll("resourceName:", "inputs:", "has description", MAPPED_PROPERTY_NAME, "com.some.resource.or.other.resourceName"), + containsNone("operations", "defaultp") + ).apply(mainYaml)); validateServiceProxyOperationInputs(mainYaml); - Assert.assertFalse(mainYaml.contains("defaultp")); - Assert.assertTrue(mainYaml.contains("has description")); - Assert.assertTrue(mainYaml.contains(MAPPED_PROPERTY_NAME)); - Assert.assertTrue(mainYaml.contains("com.some.resource.or.other.resourceName")); } @Test @@ -258,12 +255,10 @@ public class InterfacesOperationsConverterTest { template.setNode_types(nodeTypes); final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); - Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations")); - Assert.assertFalse(toscaRepresentation.getMainYaml().contains(INPUT_NAME_PREFIX)); - Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp")); - Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceNameNoInputs:")); - Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description")); - Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName")); + Assert.assertTrue(all( + containsAll("resourceNameNoInputs", "has description", "com.some.resource.or.other.resourceName"), + containsNone("operations", INPUT_NAME_PREFIX, "defaultp") + ).apply(new String(toscaRepresentation.getMainYaml()))); } @Test @@ -293,10 +288,11 @@ public class InterfacesOperationsConverterTest { nodeTypes.put("test", nodeType); template.setNode_types(nodeTypes); final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); - String mainYaml = toscaRepresentation.getMainYaml(); - Assert.assertFalse(mainYaml.contains("operations")); - Assert.assertTrue(mainYaml.contains("resourceNameInputMappedToOutput:")); - Assert.assertTrue(mainYaml.contains("inputs:")); + String mainYaml = new String(toscaRepresentation.getMainYaml()); + Assert.assertTrue(all( + containsAll("resourceNameInputMappedToOutput:", "inputs:"), + containsNone("operations") + ).apply(mainYaml)); validateOperationInputs(mainYaml, 2, "name_for_op_1"); } @@ -342,13 +338,29 @@ public class InterfacesOperationsConverterTest { template.setNode_types(nodeTypes); final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); - String mainYaml = toscaRepresentation.getMainYaml(); - Assert.assertFalse(mainYaml.contains("operations")); - Assert.assertTrue(mainYaml.contains("resourceNameInputMappedToOutput:")); - Assert.assertTrue(mainYaml.contains("inputs:")); + String mainYaml = new String(toscaRepresentation.getMainYaml()); + Assert.assertTrue(all( + containsAll("resourceNameInputMappedToOutput:", "inputs:"), + containsNone("operations") + ).apply(mainYaml)); validateOperationInputs(mainYaml, 2, "name_for_op_1"); } + @FunctionalInterface + interface MainYamlAssertion extends Function<String, Boolean> {} + + private static Function<String, Boolean> all(MainYamlAssertion...fs) { + return s -> io.vavr.collection.List.of(fs).map(f -> f.apply(s)).fold(true, (l, r) -> l && r); + } + + private static MainYamlAssertion containsNone(String...expected) { + return s -> io.vavr.collection.List.of(expected).map(e -> !s.contains(e)).fold(true, (l, r) -> l && r); + } + + private static MainYamlAssertion containsAll(String...expected) { + return s -> io.vavr.collection.List.of(expected).map(s::contains).fold(true, (l, r) -> l && r); + } + private void addOperationsToInterface(Component component, InterfaceDefinition addedInterface, int numOfOps, int numOfInputsPerOp, boolean hasInputs, boolean hasOutputs) { @@ -426,17 +438,17 @@ public class InterfacesOperationsConverterTest { operationInputDefinition.setName(outputName); operationInputDefinition.setType(inputTypes[index]); operationInputDefinition.setRequired(index % 2 == 0); - Map<String, List<String>> toscaDefaultValueMap = new HashMap<>(); List<String> toscaDefaultValues = new ArrayList<>(); toscaDefaultValues.add(SELF); toscaDefaultValues.add(interfaceName); toscaDefaultValues.add(operationName); toscaDefaultValues.add(outputName); + Map<String, List<String>> toscaDefaultValueMap = new HashMap<>(); toscaDefaultValueMap.put(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName(), toscaDefaultValues); return operationInputDefinition; } - private void validateOperationInputs(String mainYaml, int numOfInputsPerOp, String mappedOperationName) { + private void validateOperationInputs(final String mainYaml, int numOfInputsPerOp, String mappedOperationName) { String nodeTypeKey = NODE_TYPE_NAME + ":"; String nodeTypesRepresentation = mainYaml.substring(mainYaml.indexOf(nodeTypeKey) + nodeTypeKey.length(), mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length() |