diff options
Diffstat (limited to 'src')
14 files changed, 178 insertions, 163 deletions
diff --git a/src/main/java/org/onap/aai/modelloader/babel/BabelArtifactService.java b/src/main/java/org/onap/aai/modelloader/babel/BabelArtifactService.java index 1221861..029814a 100644 --- a/src/main/java/org/onap/aai/modelloader/babel/BabelArtifactService.java +++ b/src/main/java/org/onap/aai/modelloader/babel/BabelArtifactService.java @@ -19,13 +19,12 @@ */ package org.onap.aai.modelloader.babel; +import java.util.ArrayList; import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; import org.onap.aai.babel.service.data.BabelArtifact; -import org.onap.aai.babel.service.data.BabelRequest; import org.onap.aai.babel.service.data.BabelArtifact.ArtifactType; +import org.onap.aai.babel.service.data.BabelRequest; import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.eelf.LoggerFactory; import org.onap.aai.modelloader.entity.Artifact; @@ -39,62 +38,65 @@ import org.springframework.stereotype.Service; @Service public class BabelArtifactService { - private static Logger logger = LoggerFactory.getInstance().getLogger(BabelArtifactService.class); + private static Logger logger = LoggerFactory.getInstance().getLogger(BabelArtifactService.class); + + private final BabelServiceClient babelServiceClient; + private final BabelArtifactConverter babelArtifactConverter; + + public BabelArtifactService(BabelServiceClient babelServiceClient, BabelArtifactConverter babelArtifactConverter) { + this.babelServiceClient = babelServiceClient; + this.babelArtifactConverter = babelArtifactConverter; + } + + public List<Artifact> invokeBabelService(BabelRequest babelRequest, String distributionId) + throws ProcessToscaArtifactsException { + try { + logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, + "Posting artifact: " + babelRequest.getArtifactName() + ", service version: " + + babelRequest.getArtifactVersion() + + ", artifact version: " + babelRequest.getArtifactVersion()); + + List<BabelArtifact> babelArtifacts = babelServiceClient.postArtifact(babelRequest, distributionId); + + List<Artifact> convertedArtifacts = new ArrayList<>(); + for(BabelArtifact babelArtifact : babelArtifacts) { + if(!isUnknownType(babelArtifact)) { + if(babelArtifact.getType() == ArtifactType.MODEL) { + convertedArtifacts.addAll(babelArtifactConverter.convertToModel(babelArtifact)); + } else { + convertedArtifacts.add(babelArtifactConverter.convertToCatalog(babelArtifact)); + } + } + } + + return convertedArtifacts; - private final BabelServiceClient babelServiceClient; - private final BabelArtifactConverter babelArtifactConverter; + } catch (BabelArtifactParsingException e) { + logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, + "Error for artifact " + babelRequest.getArtifactName() + " " + babelRequest.getArtifactVersion() + + " " + e); + throw new ProcessToscaArtifactsException( + "An error occurred while trying to parse the Babel artifacts: " + e.getLocalizedMessage()); + } catch (Exception e) { + logger.error(ModelLoaderMsgs.BABEL_REST_REQUEST_ERROR, e, "POST", + "Error posting artifact " + babelRequest.getArtifactName() + " " + babelRequest.getArtifactVersion() + + " to Babel: " + + e.getLocalizedMessage()); + throw new ProcessToscaArtifactsException( + "An error occurred while calling the Babel service: " + e.getLocalizedMessage()); + } + } - public BabelArtifactService(BabelServiceClient babelServiceClient, BabelArtifactConverter babelArtifactConverter) { - this.babelServiceClient = babelServiceClient; - this.babelArtifactConverter = babelArtifactConverter; - } + private boolean isUnknownType(BabelArtifact babelArtifact) { + if (babelArtifact.getType() == ArtifactType.MODEL || babelArtifact.getType() == ArtifactType.VNFCATALOG) { + return false; + } else { + logger.warn(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, + babelArtifact.getName() + " " + babelArtifact.getType() + + ". Unexpected artifact types returned by the babel service: " + + babelArtifact.getPayload()); + return true; + } + } - public void invokeBabelService(List<Artifact> modelArtifacts, List<Artifact> catalogArtifacts, BabelRequest babelRequest, String distributionId) - throws ProcessToscaArtifactsException { - try { - logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, - "Posting artifact: " + babelRequest.getArtifactName() + ", service version: " + babelRequest.getArtifactVersion() - + ", artifact version: " + babelRequest.getArtifactVersion()); - - List<BabelArtifact> babelArtifacts = - babelServiceClient.postArtifact(babelRequest, distributionId); - - // Sort Babel artifacts based on type - Map<ArtifactType, List<BabelArtifact>> artifactMap = - babelArtifacts.stream().collect(Collectors.groupingBy(BabelArtifact::getType)); - - if (artifactMap.containsKey(BabelArtifact.ArtifactType.MODEL)) { - modelArtifacts.addAll( - babelArtifactConverter.convertToModel(artifactMap.get(BabelArtifact.ArtifactType.MODEL))); - artifactMap.remove(BabelArtifact.ArtifactType.MODEL); - } - - if (artifactMap.containsKey(BabelArtifact.ArtifactType.VNFCATALOG)) { - catalogArtifacts.addAll(babelArtifactConverter - .convertToCatalog(artifactMap.get(BabelArtifact.ArtifactType.VNFCATALOG))); - artifactMap.remove(BabelArtifact.ArtifactType.VNFCATALOG); - } - - // Log unexpected artifact types - if (!artifactMap.isEmpty()) { - logger.warn(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, - babelRequest.getArtifactName() + " " + babelRequest.getArtifactVersion() - + ". Unexpected artifact types returned by the babel service: " - + artifactMap.keySet().toString()); - } - - } catch (BabelArtifactParsingException e) { - logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, - "Error for artifact " + babelRequest.getArtifactName() + " " + babelRequest.getArtifactVersion() + " " + e); - throw new ProcessToscaArtifactsException( - "An error occurred while trying to parse the Babel artifacts: " + e.getLocalizedMessage()); - } catch (Exception e) { - logger.error(ModelLoaderMsgs.BABEL_REST_REQUEST_ERROR, e, "POST", - "Error posting artifact " + babelRequest.getArtifactName() + " " + babelRequest.getArtifactVersion() + " to Babel: " - + e.getLocalizedMessage()); - throw new ProcessToscaArtifactsException( - "An error occurred while calling the Babel service: " + e.getLocalizedMessage()); - } - } - -} +}
\ No newline at end of file diff --git a/src/main/java/org/onap/aai/modelloader/entity/model/AbstractModelArtifactParser.java b/src/main/java/org/onap/aai/modelloader/entity/model/AbstractModelArtifactParser.java index 35405e6..14f46f3 100644 --- a/src/main/java/org/onap/aai/modelloader/entity/model/AbstractModelArtifactParser.java +++ b/src/main/java/org/onap/aai/modelloader/entity/model/AbstractModelArtifactParser.java @@ -82,7 +82,8 @@ public abstract class AbstractModelArtifactParser implements IModelParser { IModelArtifact model = parseModel(doc.getDocumentElement(), artifactPayload); - if (!processParsedModel(modelList, artifactName, model)) { + boolean success = processParsedModel(modelList, artifactName, model); + if (!success) { modelList = null; } } catch (Exception ex) { diff --git a/src/main/java/org/onap/aai/modelloader/entity/model/ModelArtifactParser.java b/src/main/java/org/onap/aai/modelloader/entity/model/ModelArtifactParser.java index de99880..367b212 100644 --- a/src/main/java/org/onap/aai/modelloader/entity/model/ModelArtifactParser.java +++ b/src/main/java/org/onap/aai/modelloader/entity/model/ModelArtifactParser.java @@ -37,10 +37,12 @@ import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.eelf.LoggerFactory; import org.onap.aai.modelloader.entity.Artifact; import org.onap.aai.modelloader.service.ModelLoaderMsgs; +import org.springframework.stereotype.Component; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; +@Component public class ModelArtifactParser extends AbstractModelArtifactParser { public static final String MODEL_VER = "model-ver"; diff --git a/src/main/java/org/onap/aai/modelloader/notification/ArtifactDownloadManager.java b/src/main/java/org/onap/aai/modelloader/notification/ArtifactDownloadManager.java index f0c96bd..90e20bd 100644 --- a/src/main/java/org/onap/aai/modelloader/notification/ArtifactDownloadManager.java +++ b/src/main/java/org/onap/aai/modelloader/notification/ArtifactDownloadManager.java @@ -24,6 +24,9 @@ import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Base64; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + import org.onap.aai.babel.service.data.BabelRequest; import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.eelf.LoggerFactory; @@ -31,6 +34,8 @@ import org.onap.aai.cl.mdc.MdcContext; import org.onap.aai.cl.mdc.MdcOverride; import org.onap.aai.modelloader.babel.BabelArtifactService; import org.onap.aai.modelloader.entity.Artifact; +import org.onap.aai.modelloader.entity.ArtifactType; +import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifact; import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException; import org.onap.aai.modelloader.entity.model.IModelParser; import org.onap.aai.modelloader.entity.model.NamedQueryArtifactParser; @@ -132,9 +137,11 @@ public class ArtifactDownloadManager { private void processDownloadedArtifacts(List<Artifact> modelArtifacts, List<Artifact> catalogArtifacts, IArtifactInfo artifactInfo, IDistributionClientDownloadResult downloadResult, INotificationData data) throws ProcessToscaArtifactsException, InvalidArchiveException, BabelArtifactParsingException { + List<Artifact> artifacts = null; if ("TOSCA_CSAR".equalsIgnoreCase(artifactInfo.getArtifactType())) { - processToscaArtifacts(modelArtifacts, catalogArtifacts, downloadResult.getArtifactPayload(), artifactInfo, + artifacts = processToscaArtifacts(downloadResult.getArtifactPayload(), artifactInfo, data.getDistributionID(), data.getServiceVersion()); + } else if (ArtifactTypeEnum.MODEL_QUERY_SPEC.toString().equalsIgnoreCase(artifactInfo.getArtifactType())) { processModelQuerySpecArtifact(modelArtifacts, downloadResult); } else { @@ -142,29 +149,46 @@ public class ArtifactDownloadManager { artifactInfo.getArtifactType()); throw new InvalidArchiveException("Unsupported artifact type: " + artifactInfo.getArtifactType()); } + if(artifacts != null) { + for(Artifact artifact : artifacts) { + if(artifact.getType() == ArtifactType.VNF_CATALOG || artifact.getType() == ArtifactType.VNF_CATALOG_XML) { + catalogArtifacts.add(artifact); + } else { + modelArtifacts.add(artifact); + } + } + } } - public void processToscaArtifacts(List<Artifact> modelArtifacts, List<Artifact> catalogArtifacts, byte[] payload, - IArtifactInfo artifactInfo, String distributionId, String serviceVersion) + public List<Artifact> processToscaArtifacts(byte[] payload, IArtifactInfo artifactInfo, String distributionId, String serviceVersion) throws ProcessToscaArtifactsException, InvalidArchiveException { // Get translated artifacts from Babel Service BabelRequest babelRequest = new BabelRequest(); babelRequest.setArtifactName(artifactInfo.getArtifactName()); babelRequest.setCsar(Base64.getEncoder().encodeToString(payload)); babelRequest.setArtifactVersion(serviceVersion); - babelArtifactService.invokeBabelService(modelArtifacts, catalogArtifacts, babelRequest, distributionId); + List<Artifact> artifacts = babelArtifactService.invokeBabelService(babelRequest, distributionId); // Get VNF Catalog artifacts directly from CSAR List<Artifact> csarCatalogArtifacts = vnfCatalogExtractor.extract(payload, artifactInfo.getArtifactName()); // Throw an error if VNF Catalog data is present in the Babel payload and directly in the CSAR - if (!catalogArtifacts.isEmpty() && !csarCatalogArtifacts.isEmpty()) { + if (isDuplicateVnfCatalogData(artifacts, csarCatalogArtifacts)) { logger.error(ModelLoaderMsgs.DUPLICATE_VNFC_DATA_ERROR, artifactInfo.getArtifactName()); throw new InvalidArchiveException("CSAR: " + artifactInfo.getArtifactName() + " contains VNF Catalog data in the format of both TOSCA and XML files. Only one format can be used for each CSAR file."); - } else if (!csarCatalogArtifacts.isEmpty()) { - catalogArtifacts.addAll(csarCatalogArtifacts); } + return Stream + .concat(artifacts.stream(), csarCatalogArtifacts.stream()) + .collect(Collectors.toList()); + + } + + private boolean isDuplicateVnfCatalogData(List<Artifact> babelArtifacts, List<Artifact> csarCatalogArtifacts) { + boolean babelIsEmpty = babelArtifacts.stream() + .filter(VnfCatalogArtifact.class::isInstance) + .findAny().isEmpty(); + return !csarCatalogArtifacts.isEmpty() && !babelIsEmpty; } private void processModelQuerySpecArtifact(List<Artifact> modelArtifacts, diff --git a/src/main/java/org/onap/aai/modelloader/notification/BabelArtifactConverter.java b/src/main/java/org/onap/aai/modelloader/notification/BabelArtifactConverter.java index 5118652..59d3348 100644 --- a/src/main/java/org/onap/aai/modelloader/notification/BabelArtifactConverter.java +++ b/src/main/java/org/onap/aai/modelloader/notification/BabelArtifactConverter.java @@ -20,7 +20,6 @@ */ package org.onap.aai.modelloader.notification; -import java.util.ArrayList; import java.util.List; import java.util.Objects; import org.onap.aai.babel.service.data.BabelArtifact; @@ -38,6 +37,12 @@ import org.springframework.stereotype.Component; @Component public class BabelArtifactConverter { + private final ModelArtifactParser modelArtifactParser; + + public BabelArtifactConverter(ModelArtifactParser modelArtifactParser) { + this.modelArtifactParser = modelArtifactParser; + } + /** * This method converts BabelArtifacts into instances of {@link ModelArtifact}. * @@ -46,24 +51,15 @@ public class BabelArtifactConverter { * @throws BabelArtifactParsingException if an error occurs trying to parse the generated XML files that were * converted from tosca artifacts */ - public List<Artifact> convertToModel(List<BabelArtifact> xmlArtifacts) throws BabelArtifactParsingException { - Objects.requireNonNull(xmlArtifacts); - List<Artifact> modelArtifacts = new ArrayList<>(); - ModelArtifactParser modelArtParser = new ModelArtifactParser(); - - // Parse TOSCA payloads - for (BabelArtifact xmlArtifact : xmlArtifacts) { - - List<Artifact> parsedArtifacts = modelArtParser.parse(xmlArtifact.getPayload(), xmlArtifact.getName()); - - if (parsedArtifacts == null || parsedArtifacts.isEmpty()) { - throw new BabelArtifactParsingException("Could not parse generated XML: " + xmlArtifact.getPayload()); - } - - modelArtifacts.addAll(parsedArtifacts); + public List<Artifact> convertToModel(BabelArtifact babelArtifact) throws BabelArtifactParsingException { + Objects.requireNonNull(babelArtifact); + List<Artifact> parsedArtifacts = modelArtifactParser.parse(babelArtifact.getPayload(), babelArtifact.getName()); + + if (parsedArtifacts == null || parsedArtifacts.isEmpty()) { + throw new BabelArtifactParsingException("Could not parse generated XML: " + babelArtifact.getPayload()); } - return modelArtifacts; + return parsedArtifacts; } /** @@ -72,14 +68,7 @@ public class BabelArtifactConverter { * @param xmlArtifacts xml artifacts to be parsed * @return List<org.openecomp.modelloader.entity.Artifact> list of converted catalog artifacts */ - public List<Artifact> convertToCatalog(List<BabelArtifact> xmlArtifacts) { - Objects.requireNonNull(xmlArtifacts); - List<Artifact> catalogArtifacts = new ArrayList<>(); - - for (BabelArtifact xmlArtifact : xmlArtifacts) { - catalogArtifacts.add(new VnfCatalogArtifact(xmlArtifact.getPayload())); - } - - return catalogArtifacts; + public Artifact convertToCatalog(BabelArtifact babelArtifact) { + return new VnfCatalogArtifact(babelArtifact.getPayload()); } -} +}
\ No newline at end of file diff --git a/src/main/java/org/onap/aai/modelloader/notification/EventCallback.java b/src/main/java/org/onap/aai/modelloader/notification/EventCallback.java index 754eaff..d047bf9 100644 --- a/src/main/java/org/onap/aai/modelloader/notification/EventCallback.java +++ b/src/main/java/org/onap/aai/modelloader/notification/EventCallback.java @@ -67,7 +67,7 @@ public class EventCallback implements INotificationCallback { artifactDownloadManager.downloadArtifacts(data, artifacts, modelArtifacts, catalogArtifacts); if (success) { - success = artifactDeploymentManager.deploy(data, modelArtifacts, catalogArtifacts); + success = artifactDeploymentManager.deploy(data.getDistributionID(), modelArtifacts, catalogArtifacts); } String statusString = success ? "SUCCESS" : "FAILURE"; diff --git a/src/main/java/org/onap/aai/modelloader/service/ArtifactDeploymentManager.java b/src/main/java/org/onap/aai/modelloader/service/ArtifactDeploymentManager.java index 222ae34..e4535be 100644 --- a/src/main/java/org/onap/aai/modelloader/service/ArtifactDeploymentManager.java +++ b/src/main/java/org/onap/aai/modelloader/service/ArtifactDeploymentManager.java @@ -22,11 +22,11 @@ package org.onap.aai.modelloader.service; import java.util.ArrayList; import java.util.List; + import org.onap.aai.modelloader.entity.Artifact; import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifactHandler; import org.onap.aai.modelloader.entity.model.ModelArtifactHandler; import org.onap.aai.modelloader.restclient.AaiRestClient; -import org.onap.sdc.api.notification.INotificationData; import org.springframework.stereotype.Component; /** @@ -48,17 +48,15 @@ public class ArtifactDeploymentManager { /** * Deploys model and catalog artifacts to A&AI. * - * @param data data about the notification that is being processed + * @param distributionId data about the notification that is being processed * @param modelArtifacts collection of artifacts that represent yml files found in a TOSCA_CSAR file that have been * converted to XML and also those for model query specs * @param catalogArtifacts collection of artifacts that represent vnf catalog files * @return boolean <code>true</code> if all deployments were successful otherwise <code>false</code> */ - public boolean deploy(final INotificationData data, final List<Artifact> modelArtifacts, + public boolean deploy(final String distributionId, final List<Artifact> modelArtifacts, final List<Artifact> catalogArtifacts) { - String distributionId = data.getDistributionID(); - List<Artifact> completedArtifacts = new ArrayList<>(); boolean deploySuccess = modelArtifactHandler.pushArtifacts(modelArtifacts, distributionId, completedArtifacts, aaiClient); @@ -77,4 +75,4 @@ public class ArtifactDeploymentManager { return deploySuccess; } -} +}
\ No newline at end of file diff --git a/src/main/java/org/onap/aai/modelloader/service/ModelController.java b/src/main/java/org/onap/aai/modelloader/service/ModelController.java index 4e883aa..233f2ab 100644 --- a/src/main/java/org/onap/aai/modelloader/service/ModelController.java +++ b/src/main/java/org/onap/aai/modelloader/service/ModelController.java @@ -29,6 +29,7 @@ import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.eelf.LoggerFactory; import org.onap.aai.modelloader.config.ModelLoaderConfig; import org.onap.aai.modelloader.entity.Artifact; +import org.onap.aai.modelloader.entity.ArtifactType; import org.onap.aai.modelloader.notification.ArtifactDownloadManager; import org.onap.aai.modelloader.notification.NotificationDataImpl; import org.onap.aai.modelloader.notification.NotificationPublisher; @@ -103,11 +104,16 @@ public class ModelController { logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Generating XML models from test artifact"); + List<Artifact> artifacts = artifactDownloadManager.processToscaArtifacts(csarFile, artifactInfo, "test-transaction-id", modelVersion); List<Artifact> modelArtifacts = new ArrayList<>(); List<Artifact> catalogArtifacts = new ArrayList<>(); - - artifactDownloadManager.processToscaArtifacts(modelArtifacts, - catalogArtifacts, csarFile, artifactInfo, "test-transaction-id", modelVersion); + for(Artifact artifact : artifacts) { + if(artifact.getType().equals(ArtifactType.MODEL)) { + modelArtifacts.add(artifact); + } else { + catalogArtifacts.add(artifact); + } + } logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Loading xml models from test artifacts: " + modelArtifacts.size() + " model(s) and " + catalogArtifacts.size() + " catalog(s)"); @@ -115,7 +121,7 @@ public class ModelController { NotificationDataImpl notificationData = new NotificationDataImpl(); notificationData.setDistributionID("TestDistributionID"); boolean success = - artifactDeploymentManager.deploy(notificationData, modelArtifacts, catalogArtifacts); + artifactDeploymentManager.deploy(notificationData.getDistributionID(), modelArtifacts, catalogArtifacts); logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Deployment success was " + success); response = success ? ResponseEntity.ok().build() : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } catch (Exception e) { diff --git a/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerVnfcTest.java b/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerVnfcTest.java index a64c00c..0e8a733 100644 --- a/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerVnfcTest.java +++ b/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerVnfcTest.java @@ -23,6 +23,7 @@ package org.onap.aai.modelloader.notification; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.when; @@ -99,7 +100,7 @@ public class ArtifactDownloadManagerVnfcTest { assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles), is(true)); - assertEquals(2, catalogFiles.size(), "There should have been some catalog files"); + assertEquals(1, catalogFiles.size(), "There should be a catalog file"); } @Test @@ -114,10 +115,10 @@ public class ArtifactDownloadManagerVnfcTest { List<Artifact> modelArtifacts = new ArrayList<>(); List<Artifact> catalogFiles = new ArrayList<>(); - assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles), - is(true)); + assertTrue(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles)); - assertEquals(3, catalogFiles.size(), "There should have been some catalog files"); + assertEquals(3, catalogFiles.size(), "There should be three catalog artifacts"); + assertEquals(1, modelArtifacts.size(), "There should be a model artifact"); } @Test @@ -171,8 +172,8 @@ public class ArtifactDownloadManagerVnfcTest { when(mockDistributionClient.download(artifactInfo)) .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null, new ArtifactTestUtils().loadResource("compressedArtifacts/service-VscpaasTest-csar.csar"))); - when(mockBabelArtifactConverter.convertToModel(Mockito.anyList())).thenReturn(createModelArtifacts()); - when(mockBabelArtifactConverter.convertToCatalog(Mockito.anyList())).thenReturn(createToscaVnfcArtifacts()); + when(mockBabelArtifactConverter.convertToModel(Mockito.any(BabelArtifact.class))).thenReturn(createModelArtifacts()); + when(mockBabelArtifactConverter.convertToCatalog(Mockito.any(BabelArtifact.class))).thenReturn(new VnfCatalogArtifact("Some VNFC payload")); } private List<BabelArtifact> createBabelArtifacts() { @@ -191,17 +192,9 @@ public class ArtifactDownloadManagerVnfcTest { private List<Artifact> createModelArtifacts() { List<Artifact> modelArtifacts = new ArrayList<>(); modelArtifacts.add(new ModelArtifact()); - modelArtifacts.add(new ModelArtifact()); return modelArtifacts; } - private List<Artifact> createToscaVnfcArtifacts() { - List<Artifact> vnfcArtifacts = new ArrayList<>(); - vnfcArtifacts.add(new VnfCatalogArtifact("Some VNFC payload")); - vnfcArtifacts.add(new VnfCatalogArtifact("Some VNFC payload")); - return vnfcArtifacts; - } - private List<Artifact> createXmlVnfcArtifacts() { List<Artifact> vnfcArtifacts = new ArrayList<>(); vnfcArtifacts.add(new VnfCatalogArtifact(ArtifactType.VNF_CATALOG_XML, "Some VNFC payload")); diff --git a/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDeploymentManager.java b/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDeploymentManager.java index 618a268..2ff4ed5 100644 --- a/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDeploymentManager.java +++ b/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDeploymentManager.java @@ -46,6 +46,7 @@ import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifact; import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifactHandler; import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException; import org.onap.aai.modelloader.entity.model.ModelArtifactHandler; +import org.onap.aai.modelloader.entity.model.ModelArtifactParser; import org.onap.aai.modelloader.extraction.InvalidArchiveException; import org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder; import org.onap.aai.modelloader.restclient.AaiRestClient; @@ -91,13 +92,13 @@ public class TestArtifactDeploymentManager { public void deploy_csarDeploymentsFailed() throws IOException, BabelArtifactParsingException { INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile(); byte[] xml = new ArtifactTestUtils().loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml"); - List<BabelArtifact> toscaArtifacts = setupTest(xml, data); - List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts); + BabelArtifact toscaArtifact = setupTest(xml, data); + List<Artifact> modelArtifacts = new BabelArtifactConverter(new ModelArtifactParser()).convertToModel(toscaArtifact); when(modelArtifactHandlerMock.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any())) .thenReturn(false); - assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data, modelArtifacts, new ArrayList<>()), is(false)); + assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data.getDistributionID(), modelArtifacts, new ArrayList<>()), is(false)); Mockito.verify(modelArtifactHandlerMock).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any()); @@ -109,15 +110,13 @@ public class TestArtifactDeploymentManager { eq(data.getDistributionID()), any()); } - private List<BabelArtifact> setupTest(byte[] xml, INotificationData data) throws IOException { - List<BabelArtifact> toscaArtifacts = new ArrayList<>(); + private BabelArtifact setupTest(byte[] xml, INotificationData data) throws IOException { org.onap.sdc.api.notification.IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0); BabelArtifact xmlArtifact = new BabelArtifact(artifactInfo.getArtifactName(), BabelArtifact.ArtifactType.MODEL, new String(xml)); - toscaArtifacts.add(xmlArtifact); - return toscaArtifacts; + return xmlArtifact; } @Test @@ -132,7 +131,7 @@ public class TestArtifactDeploymentManager { when(vnfCatalogArtifactHandlerMock.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any())) .thenReturn(false); - assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data, new ArrayList<>(), catalogFiles), is(false)); + assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data.getDistributionID(), new ArrayList<>(), catalogFiles), is(false)); Mockito.verify(modelArtifactHandlerMock).pushArtifacts(eq(new ArrayList<Artifact>()), eq(data.getDistributionID()), any(), any()); @@ -163,8 +162,8 @@ public class TestArtifactDeploymentManager { throws IOException, BabelArtifactParsingException, InvalidArchiveException { INotificationData data = getNotificationDataWithOneOfEach(); byte[] xml = new ArtifactTestUtils().loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml"); - List<BabelArtifact> toscaArtifacts = setupTest(xml, data); - List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts); + BabelArtifact toscaArtifact = setupTest(xml, data); + List<Artifact> modelArtifacts = new BabelArtifactConverter(new ModelArtifactParser()).convertToModel(toscaArtifact); List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>(); catalogFiles.add(new VnfCatalogArtifact("Some catalog content")); @@ -174,7 +173,7 @@ public class TestArtifactDeploymentManager { when(modelArtifactHandlerMock.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any())) .thenReturn(modelsDeployed); - assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data, modelArtifacts, catalogFiles), is(false)); + assertThat(SHOULD_HAVE_RETURNED_FALSE, manager.deploy(data.getDistributionID(), modelArtifacts, catalogFiles), is(false)); // Catalog artifacts are only pushed if models are successful. Mockito.verify(modelArtifactHandlerMock).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), @@ -212,8 +211,8 @@ public class TestArtifactDeploymentManager { public void testDeploySuccess() throws IOException, BabelArtifactParsingException, InvalidArchiveException { INotificationData data = getNotificationDataWithOneOfEach(); byte[] xml = new ArtifactTestUtils().loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml"); - List<BabelArtifact> toscaArtifacts = setupTest(xml, data); - List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts); + BabelArtifact toscaArtifact = setupTest(xml, data); + List<Artifact> modelArtifacts = new BabelArtifactConverter(new ModelArtifactParser()).convertToModel(toscaArtifact); List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>(); catalogFiles.add(new VnfCatalogArtifact("Some catalog content")); @@ -223,7 +222,7 @@ public class TestArtifactDeploymentManager { when(modelArtifactHandlerMock.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any())) .thenReturn(true); - assertThat(manager.deploy(data, modelArtifacts, catalogFiles), is(true)); + assertThat(manager.deploy(data.getDistributionID(), modelArtifacts, catalogFiles), is(true)); Mockito.verify(vnfCatalogArtifactHandlerMock).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any()); diff --git a/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDownloadManager.java b/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDownloadManager.java index 27d0aa9..0661583 100644 --- a/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDownloadManager.java +++ b/src/test/java/org/onap/aai/modelloader/notification/TestArtifactDownloadManager.java @@ -24,8 +24,8 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.collection.IsEmptyCollection.empty; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.when; import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithInvalidType; @@ -50,7 +50,9 @@ import org.mockito.MockitoAnnotations; import org.onap.aai.babel.service.data.BabelArtifact; import org.onap.aai.modelloader.babel.BabelArtifactService; import org.onap.aai.modelloader.entity.Artifact; +import org.onap.aai.modelloader.entity.catalog.VnfCatalogArtifact; import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException; +import org.onap.aai.modelloader.entity.model.ModelArtifact; import org.onap.aai.modelloader.extraction.VnfCatalogExtractor; import org.onap.aai.modelloader.restclient.BabelServiceClient; import org.onap.aai.modelloader.restclient.BabelServiceClientException; @@ -196,11 +198,14 @@ public class TestArtifactDownloadManager { setupValidDownloadCsarMocks(data, artifactInfo, new ArtifactTestUtils()); + when(mockBabelArtifactConverter.convertToModel(any(BabelArtifact.class))).thenReturn(List.of(new ModelArtifact())); + when(mockBabelArtifactConverter.convertToCatalog(any(BabelArtifact.class))).thenReturn(new VnfCatalogArtifact("")); + List<org.onap.aai.modelloader.entity.Artifact> modelArtifacts = new ArrayList<>(); - List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>(); - assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles), - is(true)); - assertThat(catalogFiles.size(), is(0)); + List<org.onap.aai.modelloader.entity.Artifact> catalogArtifacts = new ArrayList<>(); + assertTrue(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogArtifacts)); + assertThat(modelArtifacts.size(), is(1)); + assertThat(catalogArtifacts.size(), is(1)); Mockito.verify(mockDistributionClient).download(artifactInfo); Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifactInfo); @@ -233,8 +238,7 @@ public class TestArtifactDownloadManager { List<org.onap.aai.modelloader.entity.Artifact> modelFiles = new ArrayList<>(); List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>(); - assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelFiles, catalogFiles), - is(true)); + assertTrue(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelFiles, catalogFiles)); assertThat(modelFiles, is(not(IsEmptyCollection.empty()))); assertThat(catalogFiles, is(empty())); @@ -268,10 +272,15 @@ public class TestArtifactDownloadManager { setupValidDownloadCsarMocks(data, serviceArtifact, artifactTestUtils); setupValidModelQuerySpecMocks(artifactTestUtils, data, modelSpecArtifact); + when(mockBabelArtifactConverter.convertToModel(any(BabelArtifact.class))).thenReturn(List.of(new ModelArtifact())); + when(mockBabelArtifactConverter.convertToCatalog(any(BabelArtifact.class))).thenReturn(new VnfCatalogArtifact("")); - List<org.onap.aai.modelloader.entity.Artifact> modelFiles = new ArrayList<>(); - List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>(); - assertThat(downloadManager.downloadArtifacts(data, artifacts, modelFiles, catalogFiles), is(true)); + + List<org.onap.aai.modelloader.entity.Artifact> modelArtifacts = new ArrayList<>(); + List<org.onap.aai.modelloader.entity.Artifact> catalogArtifacts = new ArrayList<>(); + assertTrue(downloadManager.downloadArtifacts(data, artifacts, modelArtifacts, catalogArtifacts)); + assertThat(modelArtifacts.size(), is(2)); + assertThat(catalogArtifacts.size(), is(1)); Mockito.verify(mockDistributionClient).download(serviceArtifact); Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, serviceArtifact); @@ -295,7 +304,7 @@ public class TestArtifactDownloadManager { when(mockDistributionClient.download(artifactInfo)) .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null, artifactTestUtils.loadResource("compressedArtifacts/service-VscpaasTest-csar.csar"))); - when(mockBabelArtifactConverter.convertToModel(anyList())) + when(mockBabelArtifactConverter.convertToModel(any())) .thenThrow(BabelArtifactParsingException.class); doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo); when(mockBabelClient.postArtifact(any(), any())).thenReturn(createBabelArtifacts()); diff --git a/src/test/java/org/onap/aai/modelloader/notification/TestBabelArtifactConverter.java b/src/test/java/org/onap/aai/modelloader/notification/TestBabelArtifactConverter.java index 1d81513..aee3dc2 100644 --- a/src/test/java/org/onap/aai/modelloader/notification/TestBabelArtifactConverter.java +++ b/src/test/java/org/onap/aai/modelloader/notification/TestBabelArtifactConverter.java @@ -22,11 +22,9 @@ package org.onap.aai.modelloader.notification; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import java.io.IOException; -import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.Test; @@ -34,6 +32,7 @@ import org.onap.aai.babel.service.data.BabelArtifact; import org.onap.aai.modelloader.entity.Artifact; import org.onap.aai.modelloader.entity.ArtifactType; import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException; +import org.onap.aai.modelloader.entity.model.ModelArtifactParser; import org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder; import org.onap.aai.modelloader.util.ArtifactTestUtils; import org.onap.sdc.api.notification.IArtifactInfo; @@ -47,18 +46,12 @@ public class TestBabelArtifactConverter { @Test public void convert_nullToscaFiles() throws BabelArtifactParsingException { assertThrows(NullPointerException.class, () -> { - new BabelArtifactConverter().convertToModel(null); + new BabelArtifactConverter(new ModelArtifactParser()).convertToModel(null); fail("An instance of ArtifactGenerationException should have been thrown"); }); } @Test - public void testEmptyToscaFiles() throws BabelArtifactParsingException { - assertTrue(new BabelArtifactConverter().convertToModel(new ArrayList<>()).isEmpty(), - "Nothing should have been returned"); - } - - @Test public void testInvalidXml() throws IOException, BabelArtifactParsingException { assertThrows(BabelArtifactParsingException.class, () -> { byte[] problemXml = @@ -67,22 +60,20 @@ public class TestBabelArtifactConverter { INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile(); - List<BabelArtifact> toscaArtifacts = setupTest(problemXml, data); + BabelArtifact toscaArtifact = setupTest(problemXml, data); - new BabelArtifactConverter().convertToModel(toscaArtifacts); + new BabelArtifactConverter(new ModelArtifactParser()).convertToModel(toscaArtifact); fail("An instance of ModelArtifactParsingException should have been thrown"); }); } - private List<BabelArtifact> setupTest(byte[] xml, INotificationData data) throws IOException { - List<BabelArtifact> toscaArtifacts = new ArrayList<>(); + private BabelArtifact setupTest(byte[] xml, INotificationData data) throws IOException { IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0); BabelArtifact xmlArtifact = new BabelArtifact(artifactInfo.getArtifactName(), BabelArtifact.ArtifactType.MODEL, new String(xml)); - toscaArtifacts.add(xmlArtifact); - return toscaArtifacts; + return xmlArtifact; } @Test @@ -90,9 +81,9 @@ public class TestBabelArtifactConverter { INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile(); byte[] xml = new ArtifactTestUtils().loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml"); - List<BabelArtifact> toscaArtifacts = setupTest(xml, data); + BabelArtifact toscaArtifact = setupTest(xml, data); - List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts); + List<Artifact> modelArtifacts = new BabelArtifactConverter(new ModelArtifactParser()).convertToModel(toscaArtifact); assertEquals(1, modelArtifacts.size(), "There should have been 1 artifact"); assertEquals(new String(xml), modelArtifacts.get(0).getPayload()); diff --git a/src/test/java/org/onap/aai/modelloader/notification/TestEventCallback.java b/src/test/java/org/onap/aai/modelloader/notification/TestEventCallback.java index 1073a61..b5d7fd3 100644 --- a/src/test/java/org/onap/aai/modelloader/notification/TestEventCallback.java +++ b/src/test/java/org/onap/aai/modelloader/notification/TestEventCallback.java @@ -96,13 +96,13 @@ public class TestEventCallback { when(mockArtifactDownloadManager.downloadArtifacts(any(INotificationData.class), any(List.class), any(List.class), any(List.class))).thenReturn(true); - when(mockArtifactDeploymentManager.deploy(any(INotificationData.class), any(List.class), any(List.class))) + when(mockArtifactDeploymentManager.deploy(any(String.class), any(List.class), any(List.class))) .thenReturn(true); eventCallback.activateCallback(data); verify(mockArtifactDownloadManager).downloadArtifacts(any(INotificationData.class), any(List.class), any(List.class), any(List.class)); - verify(mockArtifactDeploymentManager).deploy(any(INotificationData.class), any(List.class), any(List.class)); + verify(mockArtifactDeploymentManager).deploy(any(String.class), any(List.class), any(List.class)); } } diff --git a/src/test/java/org/onap/aai/modelloader/service/TestModelLoaderServiceWithSdc.java b/src/test/java/org/onap/aai/modelloader/service/TestModelLoaderServiceWithSdc.java index e9ab7c0..5d64dec 100644 --- a/src/test/java/org/onap/aai/modelloader/service/TestModelLoaderServiceWithSdc.java +++ b/src/test/java/org/onap/aai/modelloader/service/TestModelLoaderServiceWithSdc.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.util.Base64; import org.junit.jupiter.api.Test; +import org.onap.aai.modelloader.config.BeanConfig; import org.onap.aai.modelloader.util.ArtifactTestUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; |