diff options
Diffstat (limited to 'src/test/java/org/onap')
11 files changed, 304 insertions, 1274 deletions
diff --git a/src/test/java/org/onap/aai/modelloader/entity/catalog/VnfCatalogArtifactHandlerTest.java b/src/test/java/org/onap/aai/modelloader/entity/catalog/VnfCatalogArtifactHandlerTest.java index addea78..aa75cd2 100644 --- a/src/test/java/org/onap/aai/modelloader/entity/catalog/VnfCatalogArtifactHandlerTest.java +++ b/src/test/java/org/onap/aai/modelloader/entity/catalog/VnfCatalogArtifactHandlerTest.java @@ -1,100 +1,100 @@ -/**
- * ============LICENSE_START==========================================
- * org.onap.aai
- * ===================================================================
- * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
- * Copyright © 2017-2018 Amdocs
- * ===================================================================
- * 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.onap.aai.modelloader.entity.catalog;
-
-import static org.junit.Assert.fail;
-
-import com.sun.jersey.api.client.ClientResponse;
-import java.io.IOException;
-import java.util.Properties;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mockito;
-import org.onap.aai.modelloader.config.ModelLoaderConfig;
-import org.onap.aai.modelloader.restclient.AaiRestClient;
-import org.onap.aai.restclient.client.OperationResult;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
-
-@RunWith(PowerMockRunner.class)
-@PrepareForTest({VnfCatalogArtifactHandler.class, ClientResponse.class, AaiRestClient.class})
-public class VnfCatalogArtifactHandlerTest {
-
- protected static String CONFIG_FILE = "model-loader.properties";
-
- @Test
- public void testWithMocks() throws Exception {
-
- Properties configProperties = new Properties();
- try {
- configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));
- } catch (IOException e) {
- fail();
- }
- ModelLoaderConfig config = new ModelLoaderConfig(configProperties, null);
- config.setModelVersion("11");
-
- AaiRestClient mockRestClient = PowerMockito.mock(AaiRestClient.class);
- PowerMockito.whenNew(AaiRestClient.class).withAnyArguments().thenReturn(mockRestClient);
-
- // GET operation
- OperationResult mockGetResp = PowerMockito.mock(OperationResult.class);
-
- // @formatter:off
- PowerMockito.when(mockGetResp.getResultCode())
- .thenReturn(Response.Status.OK.getStatusCode())
- .thenReturn(Response.Status.NOT_FOUND.getStatusCode())
- .thenReturn(Response.Status.NOT_FOUND.getStatusCode())
- .thenReturn(Response.Status.OK.getStatusCode());
- // @formatter:on
- PowerMockito.when(
- mockRestClient.getResource(Mockito.anyString(), Mockito.anyString(), Mockito.any(MediaType.class)))
- .thenReturn(mockGetResp);
-
- // PUT operation
- OperationResult mockPutResp = PowerMockito.mock(OperationResult.class);
-
- PowerMockito.when(mockPutResp.getResultCode()).thenReturn(Response.Status.CREATED.getStatusCode());
- PowerMockito.when(mockRestClient.putResource(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
- Mockito.any(MediaType.class))).thenReturn(mockPutResp);
-
- // Example VNF Catalog with
- VnfCatalogArtifactHandler vnfCAH = new VnfCatalogArtifactHandler(config);
- String examplePath = "src/test/resources/imagedataexample.json";
- /*
- * byte[] encoded = Files.readAllBytes(Paths.get(examplePath)); List<Artifact> artifacts = new
- * ArrayList<Artifact>(); artifacts.add(new VnfCatalogArtifact(new String(encoded, "utf-8")));
- *
- * assertTrue(vnfCAH.pushArtifacts(artifacts, "test", new ArrayList<Artifact>(), mockRestClient));
- *
- * // Only two of the VNF images should be pushed ArgumentCaptor<String> argument =
- * ArgumentCaptor.forClass(String.class); AaiRestClient r = Mockito.verify(mockRestClient, Mockito.times(2));
- * r.putResource(Mockito.anyString(), argument.capture(), Mockito.anyString(), Mockito.any(MediaType.class));
- * assertTrue(argument.getAllValues().get(0).contains("3.16.9"));
- * assertTrue(argument.getAllValues().get(0).contains("VM00"));
- * assertTrue(argument.getAllValues().get(1).contains("3.16.1"));
- * assertTrue(argument.getAllValues().get(1).contains("VM01"));
- */
- }
-}
+/** + * ============LICENSE_START========================================== + * org.onap.aai + * =================================================================== + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs + * =================================================================== + * 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.onap.aai.modelloader.entity.catalog; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; +import org.onap.aai.modelloader.config.ModelLoaderConfig; +import org.onap.aai.modelloader.entity.Artifact; +import org.onap.aai.modelloader.restclient.AaiRestClient; +import org.onap.aai.restclient.client.OperationResult; + +public class VnfCatalogArtifactHandlerTest { + + protected static String CONFIG_FILE = "model-loader.properties"; + + @Test + public void testWithMocks() throws Exception { + + Properties configProperties = new Properties(); + try { + configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)); + } catch (IOException e) { + fail(); + } + ModelLoaderConfig config = new ModelLoaderConfig(configProperties, null); + config.setModelVersion("11"); + + AaiRestClient mockRestClient = mock(AaiRestClient.class); + + // GET operation + OperationResult mockGetResp = mock(OperationResult.class); + + // @formatter:off + when(mockGetResp.getResultCode()) + .thenReturn(Response.Status.OK.getStatusCode()) + .thenReturn(Response.Status.NOT_FOUND.getStatusCode()) + .thenReturn(Response.Status.NOT_FOUND.getStatusCode()) + .thenReturn(Response.Status.OK.getStatusCode()); + // @formatter:on + when(mockRestClient.getResource(Mockito.anyString(), Mockito.anyString(), Mockito.any(MediaType.class))) + .thenReturn(mockGetResp); + + // PUT operation + OperationResult mockPutResp = mock(OperationResult.class); + + when(mockPutResp.getResultCode()).thenReturn(Response.Status.CREATED.getStatusCode()); + when(mockRestClient.putResource(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), + Mockito.any(MediaType.class))).thenReturn(mockPutResp); + + // Example VNF Catalog with + VnfCatalogArtifactHandler vnfCAH = new VnfCatalogArtifactHandler(config); + String examplePath = "src/test/resources/imagedataexample.json"; + byte[] encoded = Files.readAllBytes(Paths.get(examplePath)); + List<Artifact> artifacts = new ArrayList<Artifact>(); + artifacts.add(new VnfCatalogArtifact(new String(encoded, "utf-8"))); + + assertTrue(vnfCAH.pushArtifacts(artifacts, "test", new ArrayList<Artifact>(), mockRestClient)); + + // Only two of the VNF images should be pushed + ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class); + AaiRestClient r = Mockito.verify(mockRestClient, Mockito.times(2)); + r.putResource(Mockito.anyString(), argument.capture(), Mockito.anyString(), Mockito.any(MediaType.class)); + assertTrue(argument.getAllValues().get(0).contains("3.16.9")); + assertTrue(argument.getAllValues().get(0).contains("VM00")); + assertTrue(argument.getAllValues().get(1).contains("3.16.1")); + assertTrue(argument.getAllValues().get(1).contains("VM01")); + } +} diff --git a/src/test/java/org/onap/aai/modelloader/entity/catalog/VnfCatalogArtifactHandler_noMock_Test.java b/src/test/java/org/onap/aai/modelloader/entity/catalog/VnfCatalogArtifactHandler_noMock_Test.java deleted file mode 100644 index a1ca794..0000000 --- a/src/test/java/org/onap/aai/modelloader/entity/catalog/VnfCatalogArtifactHandler_noMock_Test.java +++ /dev/null @@ -1,118 +0,0 @@ -/** - * ============LICENSE_START========================================== - * org.onap.aai - * =================================================================== - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 Amdocs - * =================================================================== - * 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.onap.aai.modelloader.entity.catalog; - -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import com.sun.jersey.api.client.ClientResponse; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import org.junit.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; -import org.onap.aai.modelloader.config.ModelLoaderConfig; -import org.onap.aai.modelloader.entity.Artifact; -import org.onap.aai.modelloader.restclient.AaiRestClient; -import org.onap.aai.restclient.client.OperationResult; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; - - -/** - * No-Mock tests - * - * Because Jacoco (and other coverage tools) can't cope with mocked classes under some circumstances, coverage is/was - * falsely reported as < 50%. Hence these duplicated but non-mock tests to address this, for ONAP reasons. - * - * @author andrewdo - * - */ - -@PrepareForTest({VnfCatalogArtifactHandler.class, ClientResponse.class, AaiRestClient.class}) -public class VnfCatalogArtifactHandler_noMock_Test { - - protected static String CONFIG_FILE = "model-loader.properties"; - - - @Test - public void testWithOutMocks() throws Exception { - - Properties configProperties = new Properties(); - try { - configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)); - } catch (IOException e) { - fail(); - } - - ModelLoaderConfig config = new ModelLoaderConfig(configProperties, null); - config.setModelVersion("11"); - - AaiRestClient mockRestClient = PowerMockito.mock(AaiRestClient.class); - PowerMockito.whenNew(AaiRestClient.class).withAnyArguments().thenReturn(mockRestClient); - - // GET operation - OperationResult mockGetResp = PowerMockito.mock(OperationResult.class); - - // @formatter:off - PowerMockito.when(mockGetResp.getResultCode()) - .thenReturn(Response.Status.OK.getStatusCode()) - .thenReturn(Response.Status.NOT_FOUND.getStatusCode()) - .thenReturn(Response.Status.NOT_FOUND.getStatusCode()) - .thenReturn(Response.Status.OK.getStatusCode()); - // @formatter:on - PowerMockito.when( - mockRestClient.getResource(Mockito.anyString(), Mockito.anyString(), Mockito.any(MediaType.class))) - .thenReturn(mockGetResp); - - // PUT operation - OperationResult mockPutResp = PowerMockito.mock(OperationResult.class); - - PowerMockito.when(mockPutResp.getResultCode()).thenReturn(Response.Status.CREATED.getStatusCode()); - PowerMockito.when(mockRestClient.putResource(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), - Mockito.any(MediaType.class))).thenReturn(mockPutResp); - - // Example VNF Catalog with - VnfCatalogArtifactHandler vnfCAH = new VnfCatalogArtifactHandler(config); - String examplePath = "src/test/resources/imagedataexample.json"; - byte[] encoded = Files.readAllBytes(Paths.get(examplePath)); - List<Artifact> artifacts = new ArrayList<Artifact>(); - artifacts.add(new VnfCatalogArtifact(new String(encoded, "utf-8"))); - - assertTrue(vnfCAH.pushArtifacts(artifacts, "test", new ArrayList<Artifact>(), mockRestClient)); - - // Only two of the VNF images should be pushed - ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class); - AaiRestClient r = Mockito.verify(mockRestClient, Mockito.times(2)); - r.putResource(Mockito.anyString(), argument.capture(), Mockito.anyString(), Mockito.any(MediaType.class)); - assertTrue(argument.getAllValues().get(0).contains("3.16.9")); - assertTrue(argument.getAllValues().get(0).contains("VM00")); - assertTrue(argument.getAllValues().get(1).contains("3.16.1")); - assertTrue(argument.getAllValues().get(1).contains("VM01")); - } - -} diff --git a/src/test/java/org/onap/aai/modelloader/entity/model/AAIRestClientTest.java b/src/test/java/org/onap/aai/modelloader/entity/model/AAIRestClientTest.java deleted file mode 100644 index ecf37d1..0000000 --- a/src/test/java/org/onap/aai/modelloader/entity/model/AAIRestClientTest.java +++ /dev/null @@ -1,137 +0,0 @@ -/** - * ============LICENSE_START========================================== - * org.onap.aai - * =================================================================== - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 Amdocs - * =================================================================== - * 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.onap.aai.modelloader.entity.model; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import com.sun.jersey.api.client.ClientHandlerException; -import java.io.IOException; -import java.security.KeyManagementException; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.UnrecoverableKeyException; -import java.security.cert.CertificateException; -import java.util.Properties; -import javax.ws.rs.core.MediaType; -import org.junit.Before; -import org.junit.Test; -import org.onap.aai.modelloader.config.ModelLoaderConfig; -import org.onap.aai.modelloader.restclient.AaiRestClient; -import org.onap.aai.modelloader.restclient.BabelServiceClient; -import org.onap.aai.modelloader.restclient.BabelServiceClient.BabelServiceException; - -/** - * No-Mock tests - * - * Because Jacoco (and other coverage tools) can't cope with mocked classes under some circumstances, coverage is/was - * falsely reported as < 50%. Hence these duplicated but non-mock tests to address this, for ONAP reasons. - * - * This particular class is to help make up the remaining gaps in test coverage to 50%. - * - * @author andrewdo - * - */ - - -public class AAIRestClientTest { - - private static final String CONFIG_FILE = "model-loader.properties"; - - private ModelLoaderConfig config; - private Properties configProperties; - - @Before - public void setup() throws IOException { - configProperties = new Properties(); - configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)); - config = new ModelLoaderConfig(configProperties, null); - } - - @Test - public void testRestClient() { - - AaiRestClient arc = new AaiRestClient(config); - - arc.deleteResource("testurl", "1", "xxx"); - - arc.getAndDeleteResource("testurl", "xxx"); - - arc.getResource("testurl", "xxx", MediaType.APPLICATION_ATOM_XML_TYPE); - - arc.postResource("testurl", "payload", "xxx", MediaType.APPLICATION_ATOM_XML_TYPE); - - arc.putResource("testurl", "payload", "xxx", MediaType.APPLICATION_ATOM_XML_TYPE); - - try { - arc.wait(1); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - @Test - public void testBabelClient() { - - ModelLoaderConfig mockedConfig = mock(ModelLoaderConfig.class); - - when(mockedConfig.getBabelKeyStorePath()).thenReturn(null); - - try { - - BabelServiceClient bsc = new BabelServiceClient(mockedConfig); - - byte[] artifactPayload = new byte[11]; - - bsc.postArtifact(artifactPayload, "artifactName", "artifactVersion", "transactionId"); - - - } catch (UnrecoverableKeyException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (KeyManagementException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchAlgorithmException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (KeyStoreException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (CertificateException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (BabelServiceException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (ClientHandlerException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - System.out.println("This is expected!"); - } - - - } -} diff --git a/src/test/java/org/onap/aai/modelloader/notification/ArtifactDeploymentManagerMockTest.java b/src/test/java/org/onap/aai/modelloader/notification/ArtifactDeploymentManagerMockTest.java deleted file mode 100644 index 396cc03..0000000 --- a/src/test/java/org/onap/aai/modelloader/notification/ArtifactDeploymentManagerMockTest.java +++ /dev/null @@ -1,274 +0,0 @@ -/** - * ============LICENSE_START========================================== - * org.onap.aai - * =================================================================== - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 Amdocs - * =================================================================== - * 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.onap.aai.modelloader.notification; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithCatalogFile; -import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithOneOfEach; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; -import org.onap.aai.babel.service.data.BabelArtifact; -import org.onap.aai.modelloader.config.ModelLoaderConfig; -import org.onap.aai.modelloader.entity.Artifact; -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.extraction.InvalidArchiveException; -import org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder; -import org.onap.aai.modelloader.util.ArtifactTestUtils; -import org.openecomp.sdc.api.IDistributionClient; -import org.openecomp.sdc.api.notification.INotificationData; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.reflect.Whitebox; - -/** - * Tests {@link ArtifactDeploymentManager } - */ -public class ArtifactDeploymentManagerMockTest { - - private static final String CONFIG_FILE = "model-loader.properties"; - private static final String SHOULD_HAVE_RETURNED_FALSE = "This should have returned false"; - - private Properties configProperties; - private ArtifactDeploymentManager manager; - - private IDistributionClient mockDistributionClient; - private ModelArtifactHandler mockModelArtifactHandler; - private NotificationPublisher mockNotificationPublisher; - private VnfCatalogArtifactHandler mockVnfCatalogArtifactHandler; - - @Before - public void setup() throws IOException { - configProperties = new Properties(); - configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)); - ModelLoaderConfig config = new ModelLoaderConfig(configProperties, null); - - mockDistributionClient = PowerMockito.mock(IDistributionClient.class); - mockModelArtifactHandler = PowerMockito.mock(ModelArtifactHandler.class); - mockNotificationPublisher = PowerMockito.mock(NotificationPublisher.class); - mockVnfCatalogArtifactHandler = PowerMockito.mock(VnfCatalogArtifactHandler.class); - - manager = new ArtifactDeploymentManager(mockDistributionClient, config); - - Whitebox.setInternalState(manager, mockModelArtifactHandler); - Whitebox.setInternalState(manager, mockNotificationPublisher); - Whitebox.setInternalState(manager, mockVnfCatalogArtifactHandler); - } - - @After - public void tearDown() { - configProperties = null; - mockDistributionClient = null; - mockModelArtifactHandler = null; - mockNotificationPublisher = null; - mockVnfCatalogArtifactHandler = null; - manager = null; - } -/* - @Test - public void deploy_csarDeploymentsFailed() throws IOException, BabelArtifactParsingException { - ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils(); - INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile(); - byte[] xml = artifactTestUtils.loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml"); - List<BabelArtifact> toscaArtifacts = setupTest(xml, data); - List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts); - - PowerMockito.when( - mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any())) - .thenReturn(false); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, - data.getServiceArtifacts().get(0)); - - assertFalse(SHOULD_HAVE_RETURNED_FALSE, - manager.deploy(data, data.getServiceArtifacts(), modelArtifacts, new ArrayList<>())); - - Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), - any()); - Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).pushArtifacts(eq(modelArtifacts), - eq(data.getDistributionID()), any(), any()); - Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()), eq(data.getDistributionID()), - any()); - Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(eq(new ArrayList<Artifact>()), - eq(data.getDistributionID()), any()); - Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, - data.getServiceArtifacts().get(0)); - } -*/ - private List<BabelArtifact> setupTest(byte[] xml, INotificationData data) throws IOException { - List<BabelArtifact> toscaArtifacts = new ArrayList<>(); - org.openecomp.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; - } - - @Test - public void deploy_catalogDeploymentsFailed() - throws IOException, BabelArtifactParsingException, InvalidArchiveException { - INotificationData data = getNotificationDataWithCatalogFile(); - - List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>(); - catalogFiles.add(new VnfCatalogArtifact("Some catalog content")); - - PowerMockito.when(mockModelArtifactHandler.pushArtifacts(any(), any(), any(), any())).thenReturn(true); - PowerMockito.when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), - any(), any())).thenReturn(false); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, - data.getServiceArtifacts().get(0)); - - assertFalse(SHOULD_HAVE_RETURNED_FALSE, - manager.deploy(data, data.getServiceArtifacts(), new ArrayList<>(), catalogFiles)); - - Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(new ArrayList<Artifact>()), - eq(data.getDistributionID()), any(), any()); - Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), - any(), any()); - Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()), eq(data.getDistributionID()), - any()); - Mockito.verify(mockVnfCatalogArtifactHandler).rollback(eq(new ArrayList<Artifact>()), - eq(data.getDistributionID()), any()); - Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, - data.getServiceArtifacts().get(0)); - } - -/* - @Test - public void deploy_bothDeploymentsFailed() - throws IOException, BabelArtifactParsingException, InvalidArchiveException { - doFailedCombinedTests(false, false); - } -*/ -/* - @Test - public void deploy_modelsFailedCatalogsOK() - throws IOException, BabelArtifactParsingException, InvalidArchiveException { - doFailedCombinedTests(false, true); - } -*/ -/* - @Test - public void deploy_catalogsFailedModelsOK() - throws IOException, BabelArtifactParsingException, InvalidArchiveException { - doFailedCombinedTests(true, false); - } -*/ - private void doFailedCombinedTests(boolean modelsOK, boolean catalogsOK) - throws IOException, BabelArtifactParsingException, InvalidArchiveException { - INotificationData data = getNotificationDataWithOneOfEach(); - ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils(); - byte[] xml = artifactTestUtils.loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml"); - List<BabelArtifact> toscaArtifacts = setupTest(xml, data); - List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts); - - List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>(); - catalogFiles.add(new VnfCatalogArtifact("Some catalog content")); - - PowerMockito.when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), - any(), any())).thenReturn(catalogsOK); - PowerMockito.when( - mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any())) - .thenReturn(modelsOK); - - PowerMockito.doNothing().when(mockNotificationPublisher).publishDeploySuccess(mockDistributionClient, data, - data.getServiceArtifacts().get(0)); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, - data.getServiceArtifacts().get(0)); - - assertFalse(SHOULD_HAVE_RETURNED_FALSE, - manager.deploy(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles)); - - // Catalog artifacts are only pushed if models are successful. - Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), - any()); - if (modelsOK) { - Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), - any(), any()); - } - - if (modelsOK && catalogsOK) { - Mockito.verify(mockNotificationPublisher).publishDeploySuccess(mockDistributionClient, data, - data.getServiceArtifacts().get(0)); - Mockito.verify(mockModelArtifactHandler, Mockito.never()).rollback(any(), any(), any()); - Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any()); - } else { - Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, - data.getServiceArtifacts().get(0)); - if (modelsOK) { - Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()), - eq(data.getDistributionID()), any()); - Mockito.verify(mockVnfCatalogArtifactHandler).rollback(eq(new ArrayList<Artifact>()), - eq(data.getDistributionID()), any()); - } else { - Mockito.verify(mockModelArtifactHandler).rollback(eq(new ArrayList<Artifact>()), - eq(data.getDistributionID()), any()); - Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any()); - } - } - } - -/* - @Test - public void deploy_bothOK() throws IOException, BabelArtifactParsingException, InvalidArchiveException { - INotificationData data = getNotificationDataWithOneOfEach(); - ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils(); - byte[] xml = artifactTestUtils.loadResource("convertedYmls/AAI-SCP-Test-VSP-resource-1.0.xml"); - List<BabelArtifact> toscaArtifacts = setupTest(xml, data); - List<Artifact> modelArtifacts = new BabelArtifactConverter().convertToModel(toscaArtifacts); - - List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>(); - catalogFiles.add(new VnfCatalogArtifact("Some catalog content")); - - PowerMockito.when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), - any(), any())).thenReturn(true); - PowerMockito.when( - mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any())) - .thenReturn(true); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDeploySuccess(mockDistributionClient, data, - data.getServiceArtifacts().get(0)); - - assertTrue("This should have returned true", - manager.deploy(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles)); - - Mockito.verify(mockVnfCatalogArtifactHandler).pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), - any(), any()); - Mockito.verify(mockModelArtifactHandler).pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), - any()); - Mockito.verify(mockNotificationPublisher).publishDeploySuccess(mockDistributionClient, data, - data.getServiceArtifacts().get(0)); - Mockito.verify(mockModelArtifactHandler, Mockito.never()).rollback(any(), any(), any()); - Mockito.verify(mockVnfCatalogArtifactHandler, Mockito.never()).rollback(any(), any(), any()); - } -*/ -} diff --git a/src/test/java/org/onap/aai/modelloader/notification/ArtifactDeploymentManagerTest.java b/src/test/java/org/onap/aai/modelloader/notification/ArtifactDeploymentManagerTest.java index 9fc0760..0dcff32 100644 --- a/src/test/java/org/onap/aai/modelloader/notification/ArtifactDeploymentManagerTest.java +++ b/src/test/java/org/onap/aai/modelloader/notification/ArtifactDeploymentManagerTest.java @@ -23,6 +23,8 @@ package org.onap.aai.modelloader.notification; import static org.junit.Assert.assertFalse; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithCatalogFile; import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithOneOfEach; @@ -33,8 +35,8 @@ import java.util.Properties; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.Mockito; +import org.mockito.internal.util.reflection.Whitebox; import org.onap.aai.babel.service.data.BabelArtifact; import org.onap.aai.modelloader.config.ModelLoaderConfig; import org.onap.aai.modelloader.entity.Artifact; @@ -45,15 +47,12 @@ import org.onap.aai.modelloader.entity.model.ModelArtifactHandler; import org.onap.aai.modelloader.extraction.InvalidArchiveException; import org.onap.aai.modelloader.util.ArtifactTestUtils; import org.openecomp.sdc.api.IDistributionClient; +import org.openecomp.sdc.api.notification.IArtifactInfo; import org.openecomp.sdc.api.notification.INotificationData; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; /** * Tests {@link ArtifactDeploymentManager } */ -@RunWith(PowerMockRunner.class) public class ArtifactDeploymentManagerTest { private static final String CONFIG_FILE = "model-loader.properties"; @@ -73,16 +72,16 @@ public class ArtifactDeploymentManagerTest { configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)); ModelLoaderConfig config = new ModelLoaderConfig(configProperties, null); - mockDistributionClient = PowerMockito.mock(IDistributionClient.class); - mockModelArtifactHandler = PowerMockito.mock(ModelArtifactHandler.class); - mockNotificationPublisher = PowerMockito.mock(NotificationPublisher.class); - mockVnfCatalogArtifactHandler = PowerMockito.mock(VnfCatalogArtifactHandler.class); + mockDistributionClient = mock(IDistributionClient.class); + mockModelArtifactHandler = mock(ModelArtifactHandler.class); + mockNotificationPublisher = mock(NotificationPublisher.class); + mockVnfCatalogArtifactHandler = mock(VnfCatalogArtifactHandler.class); manager = new ArtifactDeploymentManager(mockDistributionClient, config); - Whitebox.setInternalState(manager, mockModelArtifactHandler); - Whitebox.setInternalState(manager, mockNotificationPublisher); - Whitebox.setInternalState(manager, mockVnfCatalogArtifactHandler); + Whitebox.setInternalState(manager, "modelArtifactHandler", mockModelArtifactHandler); + Whitebox.setInternalState(manager, "notificationPublisher", mockNotificationPublisher); + Whitebox.setInternalState(manager, "vnfCatalogArtifactHandler", mockVnfCatalogArtifactHandler); } @After @@ -98,7 +97,7 @@ public class ArtifactDeploymentManagerTest { private List<BabelArtifact> setupTest(byte[] xml, INotificationData data) throws IOException { List<BabelArtifact> toscaArtifacts = new ArrayList<>(); - org.openecomp.sdc.api.notification.IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0); + IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0); BabelArtifact xmlArtifact = new BabelArtifact(artifactInfo.getArtifactName(), BabelArtifact.ArtifactType.MODEL, new String(xml)); @@ -115,10 +114,10 @@ public class ArtifactDeploymentManagerTest { List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>(); catalogFiles.add(new VnfCatalogArtifact("Some catalog content")); - PowerMockito.when(mockModelArtifactHandler.pushArtifacts(any(), any(), any(), any())).thenReturn(true); - PowerMockito.when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), - any(), any())).thenReturn(false); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, + when(mockModelArtifactHandler.pushArtifacts(any(), any(), any(), any())).thenReturn(true); + when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any())) + .thenReturn(false); + Mockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, data.getServiceArtifacts().get(0)); assertFalse(SHOULD_HAVE_RETURNED_FALSE, @@ -136,7 +135,6 @@ public class ArtifactDeploymentManagerTest { data.getServiceArtifacts().get(0)); } - private void doFailedCombinedTests(boolean modelsOK, boolean catalogsOK) throws IOException, BabelArtifactParsingException, InvalidArchiveException { INotificationData data = getNotificationDataWithOneOfEach(); @@ -148,15 +146,14 @@ public class ArtifactDeploymentManagerTest { List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>(); catalogFiles.add(new VnfCatalogArtifact("Some catalog content")); - PowerMockito.when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), - any(), any())).thenReturn(catalogsOK); - PowerMockito.when( - mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any())) + when(mockVnfCatalogArtifactHandler.pushArtifacts(eq(catalogFiles), eq(data.getDistributionID()), any(), any())) + .thenReturn(catalogsOK); + when(mockModelArtifactHandler.pushArtifacts(eq(modelArtifacts), eq(data.getDistributionID()), any(), any())) .thenReturn(modelsOK); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDeploySuccess(mockDistributionClient, data, + Mockito.doNothing().when(mockNotificationPublisher).publishDeploySuccess(mockDistributionClient, data, data.getServiceArtifacts().get(0)); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, + Mockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, data.getServiceArtifacts().get(0)); assertFalse(SHOULD_HAVE_RETURNED_FALSE, diff --git a/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerNoMockTest.java b/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerNoMockTest.java deleted file mode 100644 index 01f41cf..0000000 --- a/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerNoMockTest.java +++ /dev/null @@ -1,275 +0,0 @@ -/** - * ============LICENSE_START========================================== - * org.onap.aai - * =================================================================== - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 Amdocs - * =================================================================== - * 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.onap.aai.modelloader.notification; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithInvalidType; -import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithModelQuerySpec; -import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithOneService; -import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Matchers; -import org.mockito.Mockito; -import org.onap.aai.babel.service.data.BabelArtifact; -import org.onap.aai.modelloader.config.ModelLoaderConfig; -import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException; -import org.onap.aai.modelloader.restclient.BabelServiceClient; -import org.onap.aai.modelloader.restclient.BabelServiceClient.BabelServiceException; -import org.onap.aai.modelloader.util.ArtifactTestUtils; -import org.openecomp.sdc.api.IDistributionClient; -import org.openecomp.sdc.api.notification.IArtifactInfo; -import org.openecomp.sdc.api.notification.INotificationData; -import org.openecomp.sdc.api.results.IDistributionClientDownloadResult; -import org.openecomp.sdc.impl.DistributionClientDownloadResultImpl; -import org.openecomp.sdc.utils.DistributionActionResultEnum; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.reflect.Whitebox; - -/** - * No-Mock tests - * - * Because Jacoco (and other coverage tools) can't cope with mocked classes under some circumstances, coverage is/was - * falsely reported as < 50%. Hence these duplicated but non-mock tests to address this, for ONAP reasons. - * - * @author andrewdo - * - */ - -/** - * Tests {@link ArtifactDownloadManager} - */ -@PowerMockIgnore({"sun.security.ssl.*", "javax.net.ssl.*"}) -@PrepareForTest({ArtifactDownloadManager.class}) -public class ArtifactDownloadManagerNoMockTest { - - private static final String FALSE_SHOULD_HAVE_BEEN_RETURNED = "A value of 'false' should have been returned"; - private static final String OOPS = "oops"; - private static final String TRUE_SHOULD_HAVE_BEEN_RETURNED = "A value of 'true' should have been returned"; - - private ArtifactDownloadManager downloadManager; - private BabelServiceClient mockBabelClient; - private IDistributionClient mockDistributionClient; - private NotificationPublisher mockNotificationPublisher; - private BabelArtifactConverter mockBabelArtifactConverter; - - @Before - public void setup() throws Exception { - mockBabelClient = PowerMockito.mock(BabelServiceClient.class); - mockDistributionClient = PowerMockito.mock(IDistributionClient.class); - mockNotificationPublisher = PowerMockito.mock(NotificationPublisher.class); - mockBabelArtifactConverter = PowerMockito.mock(BabelArtifactConverter.class); - - Properties configProperties = new Properties(); - configProperties.load(this.getClass().getClassLoader().getResourceAsStream("model-loader.properties")); - downloadManager = - new ArtifactDownloadManager(mockDistributionClient, new ModelLoaderConfig(configProperties, ".")); - - PowerMockito.whenNew(BabelServiceClient.class).withAnyArguments().thenReturn(mockBabelClient); - - Whitebox.setInternalState(downloadManager, mockNotificationPublisher); - Whitebox.setInternalState(downloadManager, mockBabelArtifactConverter); - } - - @After - public void tearDown() { - downloadManager = null; - mockDistributionClient = null; - mockNotificationPublisher = null; - } - - @Test - public void downloadArtifacts_emptyListSupplied() { - List<org.onap.aai.modelloader.entity.Artifact> modelFiles = new ArrayList<>(); - List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>(); - - assertTrue(TRUE_SHOULD_HAVE_BEEN_RETURNED, downloadManager - .downloadArtifacts(getNotificationDataWithOneService(), new ArrayList<>(), modelFiles, catalogFiles)); - - Mockito.verifyZeroInteractions(mockBabelClient, mockDistributionClient, mockNotificationPublisher, - mockBabelArtifactConverter); - } - - @Test - public void downloadArtifacts_artifactDownloadFails() { - INotificationData data = getNotificationDataWithOneService(); - IArtifactInfo artifact = data.getServiceArtifacts().get(0); - PowerMockito.when(mockDistributionClient.download(artifact)) - .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.FAIL, OOPS, null)); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadFailure(mockDistributionClient, data, - artifact, OOPS); - - assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED, - downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null)); - - Mockito.verify(mockDistributionClient).download(artifact); - Mockito.verify(mockNotificationPublisher).publishDownloadFailure(mockDistributionClient, data, artifact, OOPS); - - Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter); - } - - private IDistributionClientDownloadResult createDistributionClientDownloadResult( - DistributionActionResultEnum status, String message, byte[] payload) { - IDistributionClientDownloadResult downloadResult = new DistributionClientDownloadResultImpl(status, message); - - ((DistributionClientDownloadResultImpl) downloadResult).setArtifactPayload(payload); - - return downloadResult; - } - - - /** - * Test disabled as exception handling needs to be reworked - * - * @throws IOException - */ - @SuppressWarnings("unchecked") - @Test - public void downloadArtifacts_invalidToscaCsarFile() throws IOException, BabelServiceException { - INotificationData data = getNotificationDataWithToscaCsarFile(); - IArtifactInfo artifact = data.getServiceArtifacts().get(0); - PowerMockito.when(mockDistributionClient.download(artifact)).thenReturn(createDistributionClientDownloadResult( - DistributionActionResultEnum.SUCCESS, null, "This is not a valid Tosca CSAR File".getBytes())); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, - artifact); - PowerMockito.when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(), - Matchers.anyString())).thenThrow(BabelServiceException.class); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, - artifact); - - assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED, - downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null)); - - Mockito.verify(mockDistributionClient).download(artifact); - Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact); - - Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact); - - Mockito.verifyZeroInteractions(mockBabelArtifactConverter); - - } - - @Test - public void downloadArtifacts_invalidModelQuerySpec() { - INotificationData data = getNotificationDataWithModelQuerySpec(); - IArtifactInfo artifact = data.getServiceArtifacts().get(0); - - List<org.onap.aai.modelloader.entity.Artifact> modelArtifacts = new ArrayList<>(); - - PowerMockito.when(mockDistributionClient.download(artifact)).thenReturn(createDistributionClientDownloadResult( - DistributionActionResultEnum.SUCCESS, null, "This is not a valid Model Query Spec".getBytes())); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, - artifact); - - assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED, - downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, null)); - - Mockito.verify(mockDistributionClient).download(artifact); - Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact); - Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact); - - Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter); - } - - - private void setupValidDownloadCsarMocks(INotificationData data, IArtifactInfo artifactInfo, - ArtifactTestUtils artifactTestUtils) throws IOException, BabelServiceException { - PowerMockito.when(mockDistributionClient.download(artifactInfo)) - .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null, - artifactTestUtils.loadResource("compressedArtifacts/service-VscpaasTest-csar.csar"))); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, - artifactInfo); - PowerMockito.when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(), - Matchers.anyString())).thenReturn(createBabelArtifacts()); - } - - private List<BabelArtifact> createBabelArtifacts() { - List<BabelArtifact> artifactList = new ArrayList<>(); - artifactList.add(new BabelArtifact("ModelArtifact", BabelArtifact.ArtifactType.MODEL, "Some model payload")); - artifactList.add(new BabelArtifact("VNFCArtifact", BabelArtifact.ArtifactType.VNFCATALOG, "Some VNFC payload")); - return artifactList; - } - - @Test - public void downloadArtifacts_validModelQuerySpec() - throws IOException, BabelServiceException, BabelArtifactParsingException { - ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils(); - INotificationData data = getNotificationDataWithModelQuerySpec(); - IArtifactInfo artifact = data.getServiceArtifacts().get(0); - setupValidModelQuerySpecMocks(artifactTestUtils, data, artifact); - - List<org.onap.aai.modelloader.entity.Artifact> modelFiles = new ArrayList<>(); - List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>(); - assertTrue(TRUE_SHOULD_HAVE_BEEN_RETURNED, - downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelFiles, catalogFiles)); - - assertTrue("There should have been some model artifacts", !modelFiles.isEmpty()); - assertTrue("There should not have been any catalog artifacts", catalogFiles.isEmpty()); - - Mockito.verify(mockDistributionClient).download(artifact); - Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact); - - Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter); - } - - private void setupValidModelQuerySpecMocks(ArtifactTestUtils artifactTestUtils, INotificationData data, - IArtifactInfo artifact) throws IOException { - PowerMockito.when(mockDistributionClient.download(artifact)) - .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null, - artifactTestUtils.loadResource("models/named-query-wan-connector.xml"))); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, - artifact); - } - - - - @Test - public void downloadArtifacts_invalidType() - throws IOException, BabelServiceException, BabelArtifactParsingException { - INotificationData data = getNotificationDataWithInvalidType(); - IArtifactInfo artifact = data.getServiceArtifacts().get(0); - - List<org.onap.aai.modelloader.entity.Artifact> catalogArtifacts = new ArrayList<>(); - - PowerMockito.when(mockDistributionClient.download(artifact)).thenReturn(createDistributionClientDownloadResult( - DistributionActionResultEnum.SUCCESS, null, "This content does not matter.".getBytes())); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, - artifact); - - assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED, - downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, catalogArtifacts)); - - Mockito.verify(mockDistributionClient).download(artifact); - Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact); - Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact); - - Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter); - } -} diff --git a/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerTest.java b/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerTest.java index c4ba991..71cd8af 100644 --- a/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerTest.java +++ b/src/test/java/org/onap/aai/modelloader/notification/ArtifactDownloadManagerTest.java @@ -22,26 +22,36 @@ package org.onap.aai.modelloader.notification; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithInvalidType; import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithModelQuerySpec; +import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithOneOfEach; import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithOneService; import static org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile; import java.io.IOException; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; import java.util.ArrayList; import java.util.List; import java.util.Properties; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.Matchers; import org.mockito.Mockito; +import org.mockito.internal.util.reflection.Whitebox; import org.onap.aai.babel.service.data.BabelArtifact; import org.onap.aai.modelloader.config.ModelLoaderConfig; import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException; import org.onap.aai.modelloader.restclient.BabelServiceClient; import org.onap.aai.modelloader.restclient.BabelServiceClient.BabelServiceException; +import org.onap.aai.modelloader.restclient.BabelServiceClientFactory; import org.onap.aai.modelloader.util.ArtifactTestUtils; import org.openecomp.sdc.api.IDistributionClient; import org.openecomp.sdc.api.notification.IArtifactInfo; @@ -49,18 +59,10 @@ import org.openecomp.sdc.api.notification.INotificationData; import org.openecomp.sdc.api.results.IDistributionClientDownloadResult; import org.openecomp.sdc.impl.DistributionClientDownloadResultImpl; import org.openecomp.sdc.utils.DistributionActionResultEnum; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.powermock.reflect.Whitebox; /** * Tests {@link ArtifactDownloadManager} */ -@RunWith(PowerMockRunner.class) -@PowerMockIgnore({"sun.security.ssl.*", "javax.net.ssl.*"}) -@PrepareForTest({ArtifactDownloadManager.class}) public class ArtifactDownloadManagerTest { private static final String FALSE_SHOULD_HAVE_BEEN_RETURNED = "A value of 'false' should have been returned"; @@ -72,23 +74,24 @@ public class ArtifactDownloadManagerTest { private IDistributionClient mockDistributionClient; private NotificationPublisher mockNotificationPublisher; private BabelArtifactConverter mockBabelArtifactConverter; + private BabelServiceClientFactory mockClientFactory; @Before public void setup() throws Exception { - mockBabelClient = PowerMockito.mock(BabelServiceClient.class); - mockDistributionClient = PowerMockito.mock(IDistributionClient.class); - mockNotificationPublisher = PowerMockito.mock(NotificationPublisher.class); - mockBabelArtifactConverter = PowerMockito.mock(BabelArtifactConverter.class); + mockBabelClient = mock(BabelServiceClient.class); + mockDistributionClient = mock(IDistributionClient.class); + mockNotificationPublisher = mock(NotificationPublisher.class); + mockBabelArtifactConverter = mock(BabelArtifactConverter.class); + mockClientFactory = mock(BabelServiceClientFactory.class); + when(mockClientFactory.create(Mockito.any())).thenReturn(mockBabelClient); Properties configProperties = new Properties(); configProperties.load(this.getClass().getClassLoader().getResourceAsStream("model-loader.properties")); - downloadManager = - new ArtifactDownloadManager(mockDistributionClient, new ModelLoaderConfig(configProperties, ".")); + downloadManager = new ArtifactDownloadManager(mockDistributionClient, + new ModelLoaderConfig(configProperties, "."), mockClientFactory); - PowerMockito.whenNew(BabelServiceClient.class).withAnyArguments().thenReturn(mockBabelClient); - - Whitebox.setInternalState(downloadManager, mockNotificationPublisher); - Whitebox.setInternalState(downloadManager, mockBabelArtifactConverter); + Whitebox.setInternalState(downloadManager, "notificationPublisher", mockNotificationPublisher); + Whitebox.setInternalState(downloadManager, "babelArtifactConverter", mockBabelArtifactConverter); } @After @@ -114,10 +117,10 @@ public class ArtifactDownloadManagerTest { public void downloadArtifacts_artifactDownloadFails() { INotificationData data = getNotificationDataWithOneService(); IArtifactInfo artifact = data.getServiceArtifacts().get(0); - PowerMockito.when(mockDistributionClient.download(artifact)) + when(mockDistributionClient.download(artifact)) .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.FAIL, OOPS, null)); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadFailure(mockDistributionClient, data, - artifact, OOPS); + doNothing().when(mockNotificationPublisher).publishDownloadFailure(mockDistributionClient, data, artifact, + OOPS); assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED, downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null)); @@ -137,15 +140,19 @@ public class ArtifactDownloadManagerTest { return downloadResult; } + @Test + public void downloadArtifacts_noSuchAlgorithmExceptionFromCreatingBabelClient() throws Exception { + doCreateBabelClientFailureTest(NoSuchAlgorithmException.class); + } + @SuppressWarnings("unchecked") private void doCreateBabelClientFailureTest(Class<? extends Throwable> exception) throws Exception { - PowerMockito.whenNew(BabelServiceClient.class).withAnyArguments().thenThrow(exception); + when(mockClientFactory.create(Mockito.any())).thenThrow(exception); INotificationData data = getNotificationDataWithToscaCsarFile(); IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0); setupValidDownloadCsarMocks(data, artifactInfo, new ArtifactTestUtils()); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, - artifactInfo); + doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo); assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED, downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null)); @@ -157,9 +164,34 @@ public class ArtifactDownloadManagerTest { Mockito.verifyZeroInteractions(mockBabelArtifactConverter); } + @Test + public void downloadArtifacts_keyStoreExceptionFromCreatingBabelClient() throws Exception { + doCreateBabelClientFailureTest(KeyStoreException.class); + } + + @Test + public void downloadArtifacts_certificateExceptionFromCreatingBabelClient() throws Exception { + doCreateBabelClientFailureTest(CertificateException.class); + } + + @Test + public void downloadArtifacts_iOExceptionFromCreatingBabelClient() throws Exception { + doCreateBabelClientFailureTest(IOException.class); + } + + @Test + public void downloadArtifacts_unrecoverableKeyExceptionFromCreatingBabelClient() throws Exception { + doCreateBabelClientFailureTest(UnrecoverableKeyException.class); + } + + @Test + public void downloadArtifacts_keyManagementExceptionFromCreatingBabelClient() throws Exception { + doCreateBabelClientFailureTest(KeyManagementException.class); + } + /** * Test disabled as exception handling needs to be reworked - * + * * @throws IOException */ @SuppressWarnings("unchecked") @@ -167,14 +199,12 @@ public class ArtifactDownloadManagerTest { public void downloadArtifacts_invalidToscaCsarFile() throws IOException, BabelServiceException { INotificationData data = getNotificationDataWithToscaCsarFile(); IArtifactInfo artifact = data.getServiceArtifacts().get(0); - PowerMockito.when(mockDistributionClient.download(artifact)).thenReturn(createDistributionClientDownloadResult( + when(mockDistributionClient.download(artifact)).thenReturn(createDistributionClientDownloadResult( DistributionActionResultEnum.SUCCESS, null, "This is not a valid Tosca CSAR File".getBytes())); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, - artifact); - PowerMockito.when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(), + doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact); + when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(), Matchers.anyString())).thenThrow(BabelServiceException.class); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, - artifact); + doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifact); assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED, downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, null)); @@ -196,10 +226,9 @@ public class ArtifactDownloadManagerTest { List<org.onap.aai.modelloader.entity.Artifact> modelArtifacts = new ArrayList<>(); - PowerMockito.when(mockDistributionClient.download(artifact)).thenReturn(createDistributionClientDownloadResult( + when(mockDistributionClient.download(artifact)).thenReturn(createDistributionClientDownloadResult( DistributionActionResultEnum.SUCCESS, null, "This is not a valid Model Query Spec".getBytes())); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, - artifact); + doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact); assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED, downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, null)); @@ -211,14 +240,35 @@ public class ArtifactDownloadManagerTest { Mockito.verifyZeroInteractions(mockBabelClient, mockBabelArtifactConverter); } + @Test + public void downloadArtifacts_validToscaCsarFile() + throws IOException, BabelServiceException, BabelArtifactParsingException { + INotificationData data = getNotificationDataWithToscaCsarFile(); + IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0); + + setupValidDownloadCsarMocks(data, artifactInfo, new ArtifactTestUtils()); + + List<org.onap.aai.modelloader.entity.Artifact> modelArtifacts = new ArrayList<>(); + List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>(); + assertTrue(TRUE_SHOULD_HAVE_BEEN_RETURNED, + downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles)); + + assertTrue("There should not have been any catalog files", catalogFiles.size() == 0); + + Mockito.verify(mockDistributionClient).download(artifactInfo); + Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifactInfo); + Mockito.verify(mockBabelClient).postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(), + Matchers.anyString()); + Mockito.verify(mockBabelArtifactConverter).convertToModel(Matchers.any()); + Mockito.verify(mockBabelArtifactConverter).convertToCatalog(Matchers.any()); + } + private void setupValidDownloadCsarMocks(INotificationData data, IArtifactInfo artifactInfo, ArtifactTestUtils artifactTestUtils) throws IOException, BabelServiceException { - PowerMockito.when(mockDistributionClient.download(artifactInfo)) + when(mockDistributionClient.download(artifactInfo)) .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null, artifactTestUtils.loadResource("compressedArtifacts/service-VscpaasTest-csar.csar"))); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, - artifactInfo); - PowerMockito.when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(), + when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(), Matchers.anyString())).thenReturn(createBabelArtifacts()); } @@ -253,14 +303,76 @@ public class ArtifactDownloadManagerTest { private void setupValidModelQuerySpecMocks(ArtifactTestUtils artifactTestUtils, INotificationData data, IArtifactInfo artifact) throws IOException { - PowerMockito.when(mockDistributionClient.download(artifact)) + when(mockDistributionClient.download(artifact)) .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null, artifactTestUtils.loadResource("models/named-query-wan-connector.xml"))); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, - artifact); + doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact); } + @Test + public void downloadArtifacts_validCsarAndModelFiles() + throws IOException, BabelServiceException, BabelArtifactParsingException { + ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils(); + INotificationData data = getNotificationDataWithOneOfEach(); + List<IArtifactInfo> artifacts = new ArrayList<>(); + + IArtifactInfo serviceArtifact = data.getServiceArtifacts().get(0); + IArtifactInfo modelSpecArtifact = data.getResources().get(1).getArtifacts().get(0); + + artifacts.add(serviceArtifact); + artifacts.add(modelSpecArtifact); + + setupValidDownloadCsarMocks(data, serviceArtifact, artifactTestUtils); + setupValidModelQuerySpecMocks(artifactTestUtils, data, modelSpecArtifact); + + List<org.onap.aai.modelloader.entity.Artifact> modelFiles = new ArrayList<>(); + List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>(); + assertTrue(TRUE_SHOULD_HAVE_BEEN_RETURNED, + downloadManager.downloadArtifacts(data, artifacts, modelFiles, catalogFiles)); + + Mockito.verify(mockDistributionClient).download(serviceArtifact); + Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, serviceArtifact); + Mockito.verify(mockBabelClient).postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(), + Matchers.anyString()); + Mockito.verify(mockBabelArtifactConverter).convertToModel(Matchers.any()); + Mockito.verify(mockBabelArtifactConverter).convertToCatalog(Matchers.any()); + + Mockito.verify(mockDistributionClient).download(modelSpecArtifact); + Mockito.verify(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, + modelSpecArtifact); + + Mockito.verifyNoMoreInteractions(mockBabelClient, mockBabelArtifactConverter); + } + + @Test + @SuppressWarnings("unchecked") + public void activateCallback_toscaToModelConverterHasProcessToscaArtifactsException() throws Exception { + ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils(); + INotificationData data = getNotificationDataWithToscaCsarFile(); + IArtifactInfo artifactInfo = data.getServiceArtifacts().get(0); + + when(mockDistributionClient.download(artifactInfo)) + .thenReturn(createDistributionClientDownloadResult(DistributionActionResultEnum.SUCCESS, null, + artifactTestUtils.loadResource("compressedArtifacts/service-VscpaasTest-csar.csar"))); + when(mockBabelArtifactConverter.convertToModel(Mockito.anyList())) + .thenThrow(BabelArtifactParsingException.class); + doNothing().when(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo); + when(mockBabelClient.postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(), + Matchers.anyString())).thenReturn(createBabelArtifacts()); + + List<org.onap.aai.modelloader.entity.Artifact> modelArtifacts = new ArrayList<>(); + List<org.onap.aai.modelloader.entity.Artifact> catalogFiles = new ArrayList<>(); + assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED, + downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), modelArtifacts, catalogFiles)); + assertTrue("There should not have been any catalog files", catalogFiles.size() == 0); + + Mockito.verify(mockDistributionClient).download(artifactInfo); + Mockito.verify(mockNotificationPublisher).publishDeployFailure(mockDistributionClient, data, artifactInfo); + Mockito.verify(mockBabelClient).postArtifact(Matchers.any(), Matchers.anyString(), Matchers.anyString(), + Matchers.anyString()); + Mockito.verify(mockBabelArtifactConverter).convertToModel(Matchers.any()); + } @Test public void downloadArtifacts_invalidType() @@ -270,10 +382,9 @@ public class ArtifactDownloadManagerTest { List<org.onap.aai.modelloader.entity.Artifact> catalogArtifacts = new ArrayList<>(); - PowerMockito.when(mockDistributionClient.download(artifact)).thenReturn(createDistributionClientDownloadResult( + when(mockDistributionClient.download(artifact)).thenReturn(createDistributionClientDownloadResult( DistributionActionResultEnum.SUCCESS, null, "This content does not matter.".getBytes())); - PowerMockito.doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, - artifact); + doNothing().when(mockNotificationPublisher).publishDownloadSuccess(mockDistributionClient, data, artifact); assertFalse(FALSE_SHOULD_HAVE_BEEN_RETURNED, downloadManager.downloadArtifacts(data, data.getServiceArtifacts(), null, catalogArtifacts)); diff --git a/src/test/java/org/onap/aai/modelloader/notification/BabelArtifactConverterTest.java b/src/test/java/org/onap/aai/modelloader/notification/BabelArtifactConverterTest.java index 026c1e3..533a37b 100644 --- a/src/test/java/org/onap/aai/modelloader/notification/BabelArtifactConverterTest.java +++ b/src/test/java/org/onap/aai/modelloader/notification/BabelArtifactConverterTest.java @@ -20,6 +20,7 @@ */ package org.onap.aai.modelloader.notification; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -27,12 +28,13 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.junit.Test; -import org.mockito.Mockito; 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.fixture.NotificationDataFixtureBuilder; +import org.onap.aai.modelloader.util.ArtifactTestUtils; import org.openecomp.sdc.api.notification.INotificationData; -import org.powermock.api.mockito.PowerMockito; /** * Tests {@link BabelArtifactConverter} @@ -49,7 +51,6 @@ public class BabelArtifactConverterTest { public void convert_emptyToscaFiles() throws BabelArtifactParsingException { assertTrue("Nothing should have been returned", new BabelArtifactConverter().convertToModel(new ArrayList<>()).isEmpty()); - PowerMockito.verifyStatic(Mockito.times(1)); } @Test(expected = BabelArtifactParsingException.class) diff --git a/src/test/java/org/onap/aai/modelloader/notification/EventCallbackNoMockTest.java b/src/test/java/org/onap/aai/modelloader/notification/EventCallbackNoMockTest.java deleted file mode 100644 index 19110bf..0000000 --- a/src/test/java/org/onap/aai/modelloader/notification/EventCallbackNoMockTest.java +++ /dev/null @@ -1,123 +0,0 @@ -/** - * ============LICENSE_START========================================== - * org.onap.aai - * =================================================================== - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 Amdocs - * =================================================================== - * 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.onap.aai.modelloader.notification; - -import java.io.IOException; -import java.util.List; -import java.util.Properties; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; -import org.onap.aai.modelloader.config.ModelLoaderConfig; -import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException; -import org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder; -import org.openecomp.sdc.api.IDistributionClient; -import org.openecomp.sdc.api.notification.INotificationData; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.reflect.Whitebox; - -/** - * No-Mock tests - * - * Because Jacoco (and other coverage tools) can't cope with mocked classes under some circumstances, coverage is/was - * falsely reported as < 50%. Hence these duplicated but non-mock tests to address this, for ONAP reasons. - * - * @author andrewdo - * - */ - -/** - * Tests {@link EventCallback} - */ - -public class EventCallbackNoMockTest { - - private static final String CONFIG_FILE = "model-loader.properties"; - - private ModelLoaderConfig config; - private Properties configProperties; - private EventCallback eventCallback; - - private ArtifactDeploymentManager mockArtifactDeploymentManager; - private ArtifactDownloadManager mockArtifactDownloadManager; - private IDistributionClient mockDistributionClient; - - @Before - public void setup() throws IOException { - configProperties = new Properties(); - configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)); - config = new ModelLoaderConfig(configProperties, null); - - mockArtifactDeploymentManager = PowerMockito.mock(ArtifactDeploymentManager.class); - mockArtifactDownloadManager = PowerMockito.mock(ArtifactDownloadManager.class); - mockDistributionClient = PowerMockito.mock(IDistributionClient.class); - - eventCallback = new EventCallback(mockDistributionClient, config); - - Whitebox.setInternalState(eventCallback, mockArtifactDeploymentManager); - Whitebox.setInternalState(eventCallback, mockArtifactDownloadManager); - } - - @After - public void tearDown() { - config = null; - configProperties = null; - eventCallback = null; - mockArtifactDeploymentManager = null; - mockArtifactDownloadManager = null; - mockDistributionClient = null; - } - - @Test - @SuppressWarnings("unchecked") - public void activateCallback_downloadFails() { - INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile(); - - PowerMockito.when(mockArtifactDownloadManager.downloadArtifacts(Mockito.any(INotificationData.class), - Mockito.any(List.class), Mockito.any(List.class), Mockito.any(List.class))).thenReturn(false); - - eventCallback.activateCallback(data); - - Mockito.verify(mockArtifactDownloadManager).downloadArtifacts(Mockito.any(INotificationData.class), - Mockito.any(List.class), Mockito.any(List.class), Mockito.any(List.class)); - Mockito.verifyZeroInteractions(mockArtifactDeploymentManager); - } - - @SuppressWarnings("unchecked") - @Test - public void activateCallback() throws BabelArtifactParsingException { - INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile(); - - PowerMockito.when(mockArtifactDownloadManager.downloadArtifacts(Mockito.any(INotificationData.class), - Mockito.any(List.class), Mockito.any(List.class), Mockito.any(List.class))).thenReturn(true); - - PowerMockito.when(mockArtifactDeploymentManager.deploy(Mockito.any(INotificationData.class), - Mockito.any(List.class), Mockito.any(List.class), Mockito.any(List.class))).thenReturn(true); - - eventCallback.activateCallback(data); - - Mockito.verify(mockArtifactDownloadManager).downloadArtifacts(Mockito.any(INotificationData.class), - Mockito.any(List.class), Mockito.any(List.class), Mockito.any(List.class)); - Mockito.verify(mockArtifactDeploymentManager).deploy(Mockito.any(INotificationData.class), - Mockito.any(List.class), Mockito.any(List.class), Mockito.any(List.class)); - } -} diff --git a/src/test/java/org/onap/aai/modelloader/notification/EventCallbackTest.java b/src/test/java/org/onap/aai/modelloader/notification/EventCallbackTest.java index 0cc183d..bc88c85 100644 --- a/src/test/java/org/onap/aai/modelloader/notification/EventCallbackTest.java +++ b/src/test/java/org/onap/aai/modelloader/notification/EventCallbackTest.java @@ -20,27 +20,28 @@ */
package org.onap.aai.modelloader.notification;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import org.junit.runner.RunWith;
import org.mockito.Mockito;
+import org.mockito.internal.util.reflection.Whitebox;
import org.onap.aai.modelloader.config.ModelLoaderConfig;
import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
import org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder;
import org.openecomp.sdc.api.IDistributionClient;
import org.openecomp.sdc.api.notification.INotificationData;
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.modules.junit4.PowerMockRunner;
-import org.powermock.reflect.Whitebox;
/**
* Tests {@link EventCallback}
*/
-@RunWith(PowerMockRunner.class)
public class EventCallbackTest {
private static final String CONFIG_FILE = "model-loader.properties";
@@ -59,14 +60,14 @@ public class EventCallbackTest { configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));
config = new ModelLoaderConfig(configProperties, null);
- mockArtifactDeploymentManager = PowerMockito.mock(ArtifactDeploymentManager.class);
- mockArtifactDownloadManager = PowerMockito.mock(ArtifactDownloadManager.class);
- mockDistributionClient = PowerMockito.mock(IDistributionClient.class);
+ mockArtifactDeploymentManager = mock(ArtifactDeploymentManager.class);
+ mockArtifactDownloadManager = mock(ArtifactDownloadManager.class);
+ mockDistributionClient = mock(IDistributionClient.class);
eventCallback = new EventCallback(mockDistributionClient, config);
- Whitebox.setInternalState(eventCallback, mockArtifactDeploymentManager);
- Whitebox.setInternalState(eventCallback, mockArtifactDownloadManager);
+ Whitebox.setInternalState(eventCallback, "artifactDeploymentManager", mockArtifactDeploymentManager);
+ Whitebox.setInternalState(eventCallback, "artifactDownloadManager", mockArtifactDownloadManager);
}
@After
@@ -84,14 +85,13 @@ public class EventCallbackTest { public void activateCallback_downloadFails() {
INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile();
- PowerMockito.when(mockArtifactDownloadManager.downloadArtifacts(Mockito.any(INotificationData.class),
- Mockito.any(List.class), Mockito.any(List.class), Mockito.any(List.class)))
- .thenReturn(false);
+ when(mockArtifactDownloadManager.downloadArtifacts(any(INotificationData.class), any(List.class),
+ any(List.class), any(List.class))).thenReturn(false);
eventCallback.activateCallback(data);
- Mockito.verify(mockArtifactDownloadManager).downloadArtifacts(Mockito.any(INotificationData.class),
- Mockito.any(List.class), Mockito.any(List.class), Mockito.any(List.class));
+ verify(mockArtifactDownloadManager).downloadArtifacts(any(INotificationData.class), any(List.class),
+ any(List.class), any(List.class));
Mockito.verifyZeroInteractions(mockArtifactDeploymentManager);
}
@@ -100,20 +100,17 @@ public class EventCallbackTest { public void activateCallback() throws BabelArtifactParsingException {
INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile();
- PowerMockito.when(mockArtifactDownloadManager.downloadArtifacts(Mockito.any(INotificationData.class),
- Mockito.any(List.class), Mockito.any(List.class), Mockito.any(List.class)))
- .thenReturn(true);
+ when(mockArtifactDownloadManager.downloadArtifacts(any(INotificationData.class), any(List.class),
+ any(List.class), any(List.class))).thenReturn(true);
- PowerMockito
- .when(mockArtifactDeploymentManager.deploy(Mockito.any(INotificationData.class),
- Mockito.any(List.class), Mockito.any(List.class), Mockito.any(List.class)))
- .thenReturn(true);
+ when(mockArtifactDeploymentManager.deploy(any(INotificationData.class), any(List.class), any(List.class),
+ any(List.class))).thenReturn(true);
eventCallback.activateCallback(data);
- Mockito.verify(mockArtifactDownloadManager).downloadArtifacts(Mockito.any(INotificationData.class),
- Mockito.any(List.class), Mockito.any(List.class), Mockito.any(List.class));
- Mockito.verify(mockArtifactDeploymentManager).deploy(Mockito.any(INotificationData.class),
- Mockito.any(List.class), Mockito.any(List.class), Mockito.any(List.class));
+ 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),
+ any(List.class));
}
}
diff --git a/src/test/java/org/onap/aai/modelloader/restclient/TestAaiRestClient.java b/src/test/java/org/onap/aai/modelloader/restclient/TestAaiRestClient.java deleted file mode 100644 index d3dab2e..0000000 --- a/src/test/java/org/onap/aai/modelloader/restclient/TestAaiRestClient.java +++ /dev/null @@ -1,149 +0,0 @@ -/** - * ============LICENSE_START========================================== - * org.onap.aai - * =================================================================== - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 Amdocs - * =================================================================== - * 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.onap.aai.modelloader.restclient; - -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Properties; -import java.util.stream.IntStream; -import java.util.stream.Stream; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import org.junit.Ignore; -import org.onap.aai.modelloader.config.ModelLoaderConfig; -import org.onap.aai.modelloader.entity.model.ModelArtifact; -import org.onap.aai.modelloader.entity.model.ModelArtifactParser; -import org.onap.aai.modelloader.restclient.AaiRestClient; -import org.onap.aai.modelloader.entity.ArtifactType; -import org.onap.aai.restclient.client.OperationResult; -import org.w3c.dom.Document; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - -public class TestAaiRestClient { - - // This test requires a running A&AI system. To test locally, annotate with org.junit.Test - @Ignore - public void testRestClient() throws Exception { - final String MODEL_FILE = "src/test/resources/models/l3-network-widget.xml"; - - Properties props = new Properties(); - props.setProperty("ml.distribution.ARTIFACT_TYPES", "MODEL_INVENTORY_PROFILE,MODEL_QUERY_SPEC,VNF_CATALOG"); - props.setProperty("ml.aai.BASE_URL", "https://localhost:8443"); - props.setProperty("ml.aai.MODEL_URL", "/aai/v9/service-design-and-creation/models/model/"); - props.setProperty("ml.aai.KEYSTORE_FILE", "aai-client-cert.p12"); - props.setProperty("ml.aai.KEYSTORE_PASSWORD", "OBF:1i9a1u2a1unz1lr61wn51wn11lss1unz1u301i6o"); - - ModelLoaderConfig config = new ModelLoaderConfig(props, "."); - - File xmlFile = new File(MODEL_FILE); - DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); - dbFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); - DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); - Document doc = dBuilder.parse(xmlFile); - - NodeList nodesList = doc.getDocumentElement().getChildNodes(); - - // Get the model IDs - - // @formatter:off - String modelInvariantId = - getNodesStream(nodesList) - .filter(childNode -> childNode.getNodeName().equals(ModelArtifactParser.MODEL_INVARIANT_ID)) - .findFirst() - .map(Node::getTextContent) - .orElse(null); - - String modelId = getNodesStream(nodesList) - .flatMap(n -> getNodesStream(n.getChildNodes())) - .filter(childNode -> childNode.getNodeName().equals(ModelArtifactParser.MODEL_VER)) - .findFirst() - .map(n -> n.getChildNodes().item(1).getTextContent()) - .orElse(null); - // @formatter:on - - try { - // Build the model artifact - ModelArtifact model = new ModelArtifact(); - model.setModelInvariantId(modelInvariantId); - model.setModelVerId(modelId); - model.setPayload(readFile(MODEL_FILE)); - model.setModelNamespace("http://org.openecomp.aai.inventory/v9"); - - AaiRestClient aaiClient = new AaiRestClient(config); - - // GET model - OperationResult opResult = - aaiClient.getResource(getURL(model, config), "example-trans-id-0", MediaType.APPLICATION_XML_TYPE); - assertTrue(opResult.getResultCode() == Response.Status.NOT_FOUND.getStatusCode()); - - // PUT the model - opResult = aaiClient.putResource(getURL(model, config), model.getPayload(), "example-trans-id-1", - MediaType.APPLICATION_XML_TYPE); - assertTrue(opResult.getResultCode() == Response.Status.CREATED.getStatusCode()); - - // DELETE the model - opResult = aaiClient.getAndDeleteResource(getURL(model, config), "example-trans-id-3"); - assertTrue(opResult.getResultCode() == Response.Status.NO_CONTENT.getStatusCode()); - } catch (Exception e) { - e.printStackTrace(); - } - } - - private Stream<Node> getNodesStream(NodeList nodeList) { - return IntStream.range(0, nodeList.getLength()).mapToObj(nodeList::item); - } - - static String readFile(String path) throws IOException { - byte[] encoded = Files.readAllBytes(Paths.get(path)); - return new String(encoded); - } - - private String getURL(ModelArtifact model, ModelLoaderConfig config) { - String baseURL = config.getAaiBaseUrl().trim(); - String subURL = null; - if (model.getType().equals(ArtifactType.MODEL)) { - subURL = config.getAaiModelUrl(model.getModelNamespaceVersion()).trim(); - } else { - subURL = config.getAaiNamedQueryUrl(model.getModelNamespaceVersion()).trim(); - } - - if ((!baseURL.endsWith("/")) && (!subURL.startsWith("/"))) { - baseURL = baseURL + "/"; - } - - if (baseURL.endsWith("/") && subURL.startsWith("/")) { - baseURL = baseURL.substring(0, baseURL.length() - 1); - } - - if (!subURL.endsWith("/")) { - subURL = subURL + "/"; - } - - return baseURL + subURL + model.getModelInvariantId(); - } -} |