From 98fe4a42e555975fa36ad40020424902cd15be38 Mon Sep 17 00:00:00 2001 From: Fiete Ostkamp Date: Mon, 29 Apr 2024 16:27:03 +0200 Subject: Refactor babel-related code to not update parameter values - return processed Artifact's as List in BabelArtifactService and ArtifactDownloadManager Issue-ID: AAI-3840 Change-Id: Ibc78517e87ebf1bfc9c6336555c96e6d1506cf5c Signed-off-by: Fiete Ostkamp --- .../ArtifactDownloadManagerVnfcTest.java | 21 +++++---------- .../TestArtifactDeploymentManager.java | 27 +++++++++---------- .../notification/TestArtifactDownloadManager.java | 31 ++++++++++++++-------- .../notification/TestBabelArtifactConverter.java | 25 ++++++----------- .../notification/TestEventCallback.java | 4 +-- .../service/TestModelLoaderServiceWithSdc.java | 1 + 6 files changed, 51 insertions(+), 58 deletions(-) (limited to 'src/test/java/org/onap') 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 modelArtifacts = new ArrayList<>(); List 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 createBabelArtifacts() { @@ -191,17 +192,9 @@ public class ArtifactDownloadManagerVnfcTest { private List createModelArtifacts() { List modelArtifacts = new ArrayList<>(); modelArtifacts.add(new ModelArtifact()); - modelArtifacts.add(new ModelArtifact()); return modelArtifacts; } - private List createToscaVnfcArtifacts() { - List vnfcArtifacts = new ArrayList<>(); - vnfcArtifacts.add(new VnfCatalogArtifact("Some VNFC payload")); - vnfcArtifacts.add(new VnfCatalogArtifact("Some VNFC payload")); - return vnfcArtifacts; - } - private List createXmlVnfcArtifacts() { List 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 toscaArtifacts = setupTest(xml, data); - List modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts); + BabelArtifact toscaArtifact = setupTest(xml, data); + List 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 setupTest(byte[] xml, INotificationData data) throws IOException { - List 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()), 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 toscaArtifacts = setupTest(xml, data); - List modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts); + BabelArtifact toscaArtifact = setupTest(xml, data); + List modelArtifacts = new BabelArtifactConverter(new ModelArtifactParser()).convertToModel(toscaArtifact); List 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 toscaArtifacts = setupTest(xml, data); - List modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts); + BabelArtifact toscaArtifact = setupTest(xml, data); + List modelArtifacts = new BabelArtifactConverter(new ModelArtifactParser()).convertToModel(toscaArtifact); List 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 modelArtifacts = new ArrayList<>(); - List catalogFiles = new ArrayList<>(); - assertThat(downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles), - is(true)); - assertThat(catalogFiles.size(), is(0)); + List 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 modelFiles = new ArrayList<>(); List 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 modelFiles = new ArrayList<>(); - List catalogFiles = new ArrayList<>(); - assertThat(downloadManager.downloadArtifacts(data, artifacts, modelFiles, catalogFiles), is(true)); + + List modelArtifacts = new ArrayList<>(); + List 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,17 +46,11 @@ 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, () -> { @@ -67,22 +60,20 @@ public class TestBabelArtifactConverter { INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile(); - List 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 setupTest(byte[] xml, INotificationData data) throws IOException { - List 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 toscaArtifacts = setupTest(xml, data); + BabelArtifact toscaArtifact = setupTest(xml, data); - List modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts); + List 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; -- cgit 1.2.3-korg