diff options
author | Denes Nemeth <denes.nemeth@nokia.com> | 2018-02-12 20:55:54 +0100 |
---|---|---|
committer | Denes Nemeth <denes.nemeth@nokia.com> | 2018-02-23 11:44:45 +0100 |
commit | b17042b955489d8a023d09abad5436ff9b900dc3 (patch) | |
tree | 1e4392ac04a2fb1ed8d17075d504cf6594acaf16 /nokiav2/driver/src/test/java/org/onap/vfc | |
parent | d4982f7b1777e9cdae9a4cc7d0d104263889ac69 (diff) |
Updating Nokia driver
Change-Id: I950afe6acbdb359cd67a448024f006a45e8fc293
Signed-off-by: Denes Nemeth <denes.nemeth@nokia.com>
Issue-ID: VFC-728
Diffstat (limited to 'nokiav2/driver/src/test/java/org/onap/vfc')
40 files changed, 7489 insertions, 0 deletions
diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/FullUnitTestSuite.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/FullUnitTestSuite.java new file mode 100644 index 00000000..f5f60fb1 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/FullUnitTestSuite.java @@ -0,0 +1,99 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.TestCbamRestApiProvider; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.TestGenericExternalSystemInfoProvider; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.TestIpMappingProvider; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.TestMsbApiProvider; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc.*; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.TestCbamVnfPackageBuilder; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.TestCbamVnfdBuilder; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.TestOnapVnfPackageBuilder; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.TestOnapVnfdBuilder; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.restapi.*; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.TestConditions; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.TestRealConfig; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.TestServletInitializer; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestCbamUtils; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestStoreLoader; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestSystemFunctions; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.*; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.TestLifecycleChangeNotificationManager; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + + TestCbamRestApiProvider.class, + TestGenericExternalSystemInfoProvider.class, + TestIpMappingProvider.class, + TestMsbApiProvider.class, + + TestVfcExternalSystemInfoProvider.class, + TestVfcGrantManager.class, + TestVfcPackageProvider.class, + TestVfcRestApiProvider.class, + TestVfcNotificationSender.class, + + TestCbamVnfdBuilder.class, + TestOnapVnfdBuilder.class, + TestCbamVnfPackageBuilder.class, + TestOnapVnfPackageBuilder.class, + + TestConverterApi.class, + TestLcmApi.class, + TestLcnApi.class, + TestSwaggerApi.class, + TestSwaggerDefinitionConsistency.class, + + TestConditions.class, + TestServletInitializer.class, + TestRealConfig.class, + + TestCbamUtils.class, + TestStoreLoader.class, + TestSystemFunctions.class, + + TestAdditionalParams.class, + TestCbamCatalogManager.class, + TestCbamTokenProvider.class, + TestDriverProperties.class, + TestJobManager.class, + TestVfcGrantManager.class, + TestLifecycleChangeNotificationManager.class, + TestLifecycleManager.class, + TestSelfRegistrationManager.class, + + TestNokiaSvnfmApplication.class, + +}) +public class FullUnitTestSuite { +} + + + + + + + + + + + + + diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/TestNokiaSvnfmApplication.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/TestNokiaSvnfmApplication.java new file mode 100644 index 00000000..47e441ff --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/TestNokiaSvnfmApplication.java @@ -0,0 +1,151 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia; + +import junit.framework.TestCase; +import org.apache.log4j.Logger; +import org.junit.Before; +import org.junit.Test; +import org.mockito.*; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.context.event.ContextClosedEvent; +import org.springframework.test.util.ReflectionTestUtils; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertTrue; +import static org.mockito.Mockito.verify; + + +public class TestNokiaSvnfmApplication { + @Mock + private SelfRegistrationManager selfRegistrationManager; + @Mock + private JobManager jobManager; + @Mock + private Logger logger; + @InjectMocks + private NokiaSvnfmApplication.SelfRegistrationTrigger selfRegistrationTriggerer; + @InjectMocks + private NokiaSvnfmApplication.SelfDeRegistrationTrigger selfUnregistrationTriggerer; + + + @Before + public void initMocks() throws Exception { + MockitoAnnotations.initMocks(this); + ReflectionTestUtils.setField(NokiaSvnfmApplication.class, "logger", logger); + } + + /** + * Assert that the entry point of the application does not change + */ + @Test + public void doNotRename() { + //verify + //1. if the entry point is renamed the main class of spring boot in the driverwar must also be changed + //2. all classes that use @Autowrire must be in a subpackage relative to this class + assertEquals("org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.NokiaSvnfmApplication", NokiaSvnfmApplication.class.getCanonicalName()); + } + + /** + * Assert that the self registration process is started after the servlet is up and is able to answer REST requests. + */ + @Test + public void testRegistrationIsCalledAfterComponentIsUp() throws Exception { + //given + ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class); + //when + selfRegistrationTriggerer.onApplicationEvent(event); + //verify + verify(selfRegistrationManager).register(); + verify(logger).info("Self registration started"); + verify(logger).info("Self registration finished"); + // this forces the event to be fired after the servlet is up (prevents refactor) + assertTrue(ApplicationReadyEvent.class.isAssignableFrom(event.getClass())); + } + + /** + * Assert that the self de-registration process is started after the servlet has been ramped down + */ + @Test + public void testUnRegistrationIsCalledAfterComponentIsUp() throws Exception { + //given + ContextClosedEvent event = Mockito.mock(ContextClosedEvent.class); + //when + selfUnregistrationTriggerer.onApplicationEvent(event); + //verify + InOrder inOrder = Mockito.inOrder(jobManager, selfRegistrationManager); + inOrder.verify(jobManager).prepareForShutdown(); + inOrder.verify(selfRegistrationManager).deRegister(); + verify(logger).info("Self de-registration started"); + verify(logger).info("Self de-registration finished"); + // this forces the event to be fired after the servlet is down (prevents refactor) + assertTrue(ContextClosedEvent.class.isAssignableFrom(event.getClass())); + } + + /** + * Failures in registration is logged and propagated + */ + @Test + public void failedRegistration() { + //given + RuntimeException expectedException = new RuntimeException(); + Mockito.doThrow(expectedException).when(selfRegistrationManager).register(); + ApplicationReadyEvent event = Mockito.mock(ApplicationReadyEvent.class); + //when + try { + selfRegistrationTriggerer.onApplicationEvent(event); + //verify + TestCase.fail(); + } catch (RuntimeException e) { + assertEquals(e, expectedException); + } + verify(logger).error("Self registration failed", expectedException); + } + + /** + * Failures in de-registration is logged and propagated + */ + @Test + public void failedDeRegistration() { + //given + RuntimeException expectedException = new RuntimeException(); + Mockito.doThrow(expectedException).when(selfRegistrationManager).deRegister(); + ContextClosedEvent event = Mockito.mock(ContextClosedEvent.class); + //when + try { + selfUnregistrationTriggerer.onApplicationEvent(event); + //verify + TestCase.fail(); + } catch (RuntimeException e) { + assertEquals(e, expectedException); + } + verify(logger).error("Self de-registration failed", expectedException); + } + + /** + * static entry point calling an other static entry point can not be tested + */ + @Test + public void useless() throws Exception { + try { + NokiaSvnfmApplication.main(null); + } catch (Exception e) { + + } + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/ct/CTReal.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/ct/CTReal.java new file mode 100644 index 00000000..e013142a --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/ct/CTReal.java @@ -0,0 +1,42 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.ct; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.NokiaSvnfmApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(value = SpringRunner.class) +@SpringBootTest(classes = NokiaSvnfmApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +@ActiveProfiles("real") +public class CTReal { + + /** + * The following is not a real test, but only start the driver locally. + * It takes parameters from application-real.properties + */ + @Test + public void testBasicWorkflow() throws Exception { + + + Thread.sleep(10000000 * 1000L); + } + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/core/TestCbamRestApiProvider.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/core/TestCbamRestApiProvider.java new file mode 100644 index 00000000..45a3216a --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/core/TestCbamRestApiProvider.java @@ -0,0 +1,236 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core; + +import com.nokia.cbam.catalog.v1.api.DefaultApi; +import com.nokia.cbam.lcm.v32.ApiClient; +import com.nokia.cbam.lcm.v32.api.OperationExecutionsApi; +import com.nokia.cbam.lcm.v32.api.VnfsApi; +import com.nokia.cbam.lcn.v32.api.SubscriptionsApi; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo; +import org.onap.msb.sdk.discovery.entity.NodeInfo; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VnfmInfoProvider; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamTokenProvider; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; +import org.onap.vnfmdriver.model.VnfmInfo; +import org.springframework.core.env.Environment; + +import java.util.Base64; +import java.util.HashSet; +import java.util.Set; + +import static junit.framework.TestCase.*; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; +import static org.springframework.test.util.ReflectionTestUtils.setField; + +public class TestCbamRestApiProvider extends TestBase { + @Mock + private Environment environment; + @Mock + private CbamTokenProvider cbamTokenProvider; + private MicroServiceFullInfo microServiceInfo = new MicroServiceFullInfo(); + private Set<NodeInfo> nodes = new HashSet<>(); + + private CbamRestApiProvider cbamRestApiProvider; + + @Before + public void init() { + microServiceInfo.setNodes(nodes); + CbamRestApiProvider real = new CbamRestApiProvider(driverProperties, cbamTokenProvider, vnfmInfoProvider); + setField(real, "trustedCertificates", "mytrustedCertificates"); + setField(real, "skipCertificateVerification", true); + cbamRestApiProvider = spy(real); + when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn(""); + when(environment.getProperty(VnfmInfoProvider.VNFM_INFO_CACHE_EVICTION_IN_MS, Long.class, Long.valueOf(10 * 60 * 1000))).thenReturn(10 * 60 * 1000L); + } + + /** + * test CBAM LCM API retrieval without SSL verification + */ + @Test + public void testCbamLcmApi() throws Exception { + VnfmInfo expectedVnfmInfo = new VnfmInfo(); + when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo); + expectedVnfmInfo.setUrl("https://cbamUrl:123/d"); + when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken"); + //when + VnfsApi cbamLcmApi = cbamRestApiProvider.getCbamLcmApi(VNFM_ID); + //verify + ApiClient apiClient = cbamLcmApi.getApiClient(); + assertEquals("https://cbamUrl:123/d", apiClient.getBasePath()); + assertNull(apiClient.getSslCaCert()); + assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken()); + assertEquals(2, cbamLcmApi.getApiClient().getAuthentications().size()); + assertTrue(!cbamLcmApi.getApiClient().isVerifyingSsl()); + } + + /** + * test CBAM LCM API retrieval with SSL verification + */ + @Test + public void testCbamLcmApiWithSslVerfy() throws Exception { + VnfmInfo expectedVnfmInfo = new VnfmInfo(); + when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo); + expectedVnfmInfo.setUrl("https://cbamUrl:123/d"); + when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken"); + setField(cbamRestApiProvider, "skipCertificateVerification", false); + setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem"))); + //when + VnfsApi cbamLcmApi = cbamRestApiProvider.getCbamLcmApi(VNFM_ID); + //verify + ApiClient apiClient = cbamLcmApi.getApiClient(); + assertEquals("https://cbamUrl:123/d", apiClient.getBasePath()); + assertNotNull(apiClient.getSslCaCert()); + assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken()); + assertEquals(2, cbamLcmApi.getApiClient().getAuthentications().size()); + assertTrue(cbamLcmApi.getApiClient().isVerifyingSsl()); + } + + /** + * test CBAM Catalog API retrieval without SSL verification + */ + @Test + public void testCbamCatalogApi() throws Exception { + VnfmInfo expectedVnfmInfo = new VnfmInfo(); + when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo); + when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken"); + when(driverProperties.getCbamCatalogUrl()).thenReturn("https://1.2.3.4/path"); + //when + DefaultApi cbamCatalogApi = cbamRestApiProvider.getCbamCatalogApi(VNFM_ID); + //verify + com.nokia.cbam.catalog.v1.ApiClient apiClient = cbamCatalogApi.getApiClient(); + assertEquals("https://1.2.3.4/path", apiClient.getBasePath()); + assertNull(apiClient.getSslCaCert()); + assertEquals("myToken", ((com.nokia.cbam.catalog.v1.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken()); + assertEquals(2, cbamCatalogApi.getApiClient().getAuthentications().size()); + assertTrue(!cbamCatalogApi.getApiClient().isVerifyingSsl()); + } + + /** + * test CBAM Catalog API retrieval with SSL verification + */ + @Test + public void testCbamCatalogApiWithSslVerfy() throws Exception { + VnfmInfo expectedVnfmInfo = new VnfmInfo(); + when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo); + when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken"); + when(driverProperties.getCbamCatalogUrl()).thenReturn("https://1.2.3.4/path"); + setField(cbamRestApiProvider, "skipCertificateVerification", false); + setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem"))); + //when + DefaultApi cbamLcmApi = cbamRestApiProvider.getCbamCatalogApi(VNFM_ID); + //verify + com.nokia.cbam.catalog.v1.ApiClient apiClient = cbamLcmApi.getApiClient(); + assertEquals("https://1.2.3.4/path", apiClient.getBasePath()); + assertNotNull(apiClient.getSslCaCert()); + assertEquals("myToken", ((com.nokia.cbam.catalog.v1.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken()); + assertEquals(2, cbamLcmApi.getApiClient().getAuthentications().size()); + assertTrue(cbamLcmApi.getApiClient().isVerifyingSsl()); + } + + /** + * test CBAM Lcn API retrieval without SSL verification + */ + @Test + public void testCbamLcnApi() throws Exception { + VnfmInfo expectedVnfmInfo = new VnfmInfo(); + when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo); + when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken"); + when(driverProperties.getCbamLcnUrl()).thenReturn("https://1.2.3.4/path"); + //when + SubscriptionsApi cbamLcnApi = cbamRestApiProvider.getCbamLcnApi(VNFM_ID); + //verify + com.nokia.cbam.lcn.v32.ApiClient apiClient = cbamLcnApi.getApiClient(); + assertEquals("https://1.2.3.4/path", apiClient.getBasePath()); + assertNull(apiClient.getSslCaCert()); + assertEquals("myToken", ((com.nokia.cbam.lcn.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken()); + assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size()); + assertTrue(!cbamLcnApi.getApiClient().isVerifyingSsl()); + } + + /** + * test CBAM Lcn API retrieval with SSL verification + */ + @Test + public void testCbamLcnApiWithSslVerfy() throws Exception { + VnfmInfo expectedVnfmInfo = new VnfmInfo(); + when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo); + when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken"); + when(driverProperties.getCbamLcnUrl()).thenReturn("https://1.2.3.4/path"); + setField(cbamRestApiProvider, "skipCertificateVerification", false); + setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem"))); + //when + SubscriptionsApi cbamLcnApi = cbamRestApiProvider.getCbamLcnApi(VNFM_ID); + //verify + com.nokia.cbam.lcn.v32.ApiClient apiClient = cbamLcnApi.getApiClient(); + assertEquals("https://1.2.3.4/path", apiClient.getBasePath()); + assertNotNull(apiClient.getSslCaCert()); + assertEquals("myToken", ((com.nokia.cbam.lcn.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken()); + assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size()); + assertTrue(cbamLcnApi.getApiClient().isVerifyingSsl()); + } + + /** + * test CBAM operation exeution API retrieval without SSL verification + */ + @Test + public void testCbamOpexApi() throws Exception { + VnfmInfo expectedVnfmInfo = new VnfmInfo(); + when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo); + when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken"); + when(nsLcmApi.queryVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo); + expectedVnfmInfo.setUrl("https://cbamUrl:123/d"); + //when + OperationExecutionsApi cbamLcnApi = cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID); + //verify + ApiClient apiClient = cbamLcnApi.getApiClient(); + assertEquals("https://cbamUrl:123/d", apiClient.getBasePath()); + assertNull(apiClient.getSslCaCert()); + assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken()); + assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size()); + assertTrue(!cbamLcnApi.getApiClient().isVerifyingSsl()); + } + + /** + * test CBAM operation execution API retrieval with SSL verification + */ + @Test + public void testCbamOpexApiWithSslVerfy() throws Exception { + when(cbamTokenProvider.getToken(VNFM_ID)).thenReturn("myToken"); + setField(cbamRestApiProvider, "skipCertificateVerification", false); + setField(cbamRestApiProvider, "trustedCertificates", Base64.getEncoder().encodeToString(TestUtil.loadFile("unittests/sample.cert.pem"))); + VnfmInfo expectedVnfmInfo = new VnfmInfo(); + when(nsLcmApi.queryVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo); + expectedVnfmInfo.setUrl("https://cbamUrl:123/d"); + when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(expectedVnfmInfo); + //when + OperationExecutionsApi cbamLcnApi = cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID); + //verify + ApiClient apiClient = cbamLcnApi.getApiClient(); + assertEquals("https://cbamUrl:123/d", apiClient.getBasePath()); + assertNotNull(apiClient.getSslCaCert()); + assertEquals("myToken", ((com.nokia.cbam.lcm.v32.auth.OAuth) apiClient.getAuthentication("OauthClient")).getAccessToken()); + assertEquals(2, cbamLcnApi.getApiClient().getAuthentications().size()); + assertTrue(cbamLcnApi.getApiClient().isVerifyingSsl()); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/core/TestGenericExternalSystemInfoProvider.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/core/TestGenericExternalSystemInfoProvider.java new file mode 100644 index 00000000..44263835 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/core/TestGenericExternalSystemInfoProvider.java @@ -0,0 +1,104 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; +import org.onap.vnfmdriver.model.VimInfo; +import org.onap.vnfmdriver.model.VnfmInfo; +import org.springframework.core.env.Environment; +import org.springframework.test.util.ReflectionTestUtils; + +import static java.lang.Long.valueOf; +import static junit.framework.TestCase.assertEquals; +import static org.mockito.Mockito.*; + +public class TestGenericExternalSystemInfoProvider extends TestBase { + + private GenericExternalSystemInfoProvider genericExternalSystemInfoProvider; + + @Before + public void init() { + when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn(""); + ReflectionTestUtils.setField(GenericExternalSystemInfoProvider.class, "logger", logger); + genericExternalSystemInfoProvider = Mockito.spy(new TestClass(environment)); + } + + /** + * the VNFM info is not retrieved within the cache eviction period + */ + @Test + public void testQueryVnfmInfoWithin() throws Exception { + VnfmInfo expectedVnfmInfo = Mockito.mock(VnfmInfo.class); + when(genericExternalSystemInfoProvider.queryVnfmInfoFromSource(VNFM_ID)).thenReturn(expectedVnfmInfo); + when(environment.getProperty(GenericExternalSystemInfoProvider.VNFM_INFO_CACHE_EVICTION_IN_MS, Long.class, valueOf(GenericExternalSystemInfoProvider.DEFAULT_CACHE_EVICTION_TIMEOUT_IN_MS))).thenReturn(Long.valueOf(1234)); + genericExternalSystemInfoProvider.afterPropertiesSet(); + //when + VnfmInfo vnfmInfo = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID); + //verify + verify(logger).info("Quering VNFM info from source with " + VNFM_ID + " identifier"); + assertEquals(expectedVnfmInfo, vnfmInfo); + //when + VnfmInfo vnfmInfo2 = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID); + //verify source system not called again + verify(logger).info("Quering VNFM info from source with " + VNFM_ID + " identifier"); + verify(genericExternalSystemInfoProvider, Mockito.times(1)).queryVnfmInfoFromSource(VNFM_ID); + } + + /** + * the VNFM info is retrieved without the cache eviction period + */ + @Test + public void testQueryVnfmInfoOutside() throws Exception { + VnfmInfo expectedVnfmInfo = Mockito.mock(VnfmInfo.class); + when(genericExternalSystemInfoProvider.queryVnfmInfoFromSource(VNFM_ID)).thenReturn(expectedVnfmInfo); + when(environment.getProperty(GenericExternalSystemInfoProvider.VNFM_INFO_CACHE_EVICTION_IN_MS, Long.class, valueOf(GenericExternalSystemInfoProvider.DEFAULT_CACHE_EVICTION_TIMEOUT_IN_MS))).thenReturn(Long.valueOf(1)); + genericExternalSystemInfoProvider.afterPropertiesSet(); + + //when + VnfmInfo vnfmInfo = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID); + //verify + assertEquals(expectedVnfmInfo, vnfmInfo); + //when + //sleeping is required to make time pass (for cache to notice the change) + Thread.sleep(10); + VnfmInfo vnfmInfo2 = genericExternalSystemInfoProvider.getVnfmInfo(VNFM_ID); + //verify source system called again + verify(logger, times(2)).info("Quering VNFM info from source with " + VNFM_ID + " identifier"); + verify(genericExternalSystemInfoProvider, Mockito.times(2)).queryVnfmInfoFromSource(VNFM_ID); + } + + class TestClass extends GenericExternalSystemInfoProvider { + + TestClass(Environment environment) { + super(environment); + } + + @Override + public VnfmInfo queryVnfmInfoFromSource(String vnfmId) { + return null; + } + + @Override + public VimInfo getVimInfo(String vimId) { + return null; + } + } + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/core/TestIpMappingProvider.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/core/TestIpMappingProvider.java new file mode 100644 index 00000000..a1f91855 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/core/TestIpMappingProvider.java @@ -0,0 +1,61 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core; + +import org.junit.Before; +import org.junit.Test; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; +import org.springframework.core.env.Environment; + +import static junit.framework.TestCase.assertEquals; +import static org.mockito.Mockito.when; + +public class TestIpMappingProvider extends TestBase { + + private IpMappingProvider ipMappingProvider; + + @Before + public void init() { + ipMappingProvider = new TestClass(environment); + } + + /** + * the IP addresses are correctly mapped + */ + @Test + public void testIpMapping() throws Exception { + when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn(" 1.2.3.4 -> 2.3.4.5 , 1.2.3.5 -> 3.4.5.6"); + //when + ipMappingProvider.afterPropertiesSet(); + //verify + assertEquals("2.3.4.5", ipMappingProvider.mapPrivateIpToPublicIp("1.2.3.4")); + assertEquals(".......", ipMappingProvider.mapPrivateIpToPublicIp(".......")); + assertEquals("3.4.5.6", ipMappingProvider.mapPrivateIpToPublicIp("1.2.3.5")); + assertEquals("1.2.3.5.", ipMappingProvider.mapPrivateIpToPublicIp("1.2.3.5.")); + + } + + class TestClass extends IpMappingProvider { + + TestClass(Environment environment) { + super(environment); + } + + } + + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/core/TestMsbApiProvider.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/core/TestMsbApiProvider.java new file mode 100644 index 00000000..e6d8ebb4 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/core/TestMsbApiProvider.java @@ -0,0 +1,146 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.onap.msb.sdk.discovery.common.RouteException; +import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo; +import org.onap.msb.sdk.discovery.entity.NodeInfo; +import org.onap.msb.sdk.httpclient.msb.MSBServiceClient; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamTokenProvider; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; +import org.springframework.core.env.Environment; + +import java.util.HashSet; +import java.util.Set; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.fail; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.springframework.test.util.ReflectionTestUtils.setField; + +public class TestMsbApiProvider extends TestBase { + @Mock + private Environment environment; + @Mock + private CbamTokenProvider cbamTokenProvider; + private MicroServiceFullInfo microServiceInfo = new MicroServiceFullInfo(); + private Set<NodeInfo> nodes = new HashSet<>(); + private MsbApiProvider msbApiProvider; + + @Before + public void init() { + setField(MsbApiProvider.class, "logger", logger); + msbApiProvider = new MsbApiProvider(environment); + microServiceInfo.setNodes(nodes); + } + + /** + * test MSB client is created based on driver properties + */ + @Test + public void testMsbClient() { + setFieldWithPropertyAnnotation(msbApiProvider, "${messageBusIp}", "mymessageBusIp"); + setFieldWithPropertyAnnotation(msbApiProvider, "${messageBusPort}", "123"); + //when + MSBServiceClient msbClient = msbApiProvider.getMsbClient(); + //verify + assertEquals("mymessageBusIp:123", msbClient.getMsbSvrAddress()); + } + + /** + * error is propagated if no suitable micro service endpoint is found + */ + @Test + public void testNoSuitableMicroService() throws Exception { + NodeInfo dockerAccessPoint = new NodeInfo(); + dockerAccessPoint.setIp("172.1.2.3"); + microServiceInfo.setServiceName("serviceName"); + microServiceInfo.setVersion("v1"); + microServiceInfo.setUrl("/lead/nslcm/v1"); + when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn(""); + nodes.add(dockerAccessPoint); + msbApiProvider = new MsbApiProvider(environment) { + @Override + public MSBServiceClient getMsbClient() { + return msbClient; + } + }; + when(msbClient.queryMicroServiceInfo("serviceName", "v1")).thenReturn(microServiceInfo); + //when + try { + msbApiProvider.getMicroServiceUrl("serviceName", "v1"); + fail(); + } catch (Exception e) { + assertEquals("The serviceName service with v1 does not have any valid nodes[172.1.2.3:null ttl:]", e.getMessage()); + verify(logger).error("The serviceName service with v1 does not have any valid nodes[172.1.2.3:null ttl:]"); + } + } + + /** + * non Docker endpoint is selected + */ + @Test + public void testExistingValidEndpoint() throws Exception { + NodeInfo nonDocker = new NodeInfo(); + nonDocker.setIp("173.1.2.3"); + nonDocker.setPort("234"); + microServiceInfo.setServiceName("serviceName"); + microServiceInfo.setVersion("v1"); + microServiceInfo.setUrl("/lead/nslcm/v1"); + when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("173.1.2.3->1.2.3.4"); + nodes.add(nonDocker); + msbApiProvider = new MsbApiProvider(environment) { + @Override + public MSBServiceClient getMsbClient() { + return msbClient; + } + }; + when(msbClient.queryMicroServiceInfo("serviceName", "v1")).thenReturn(microServiceInfo); + msbApiProvider.afterPropertiesSet(); + //when + assertEquals("http://1.2.3.4:234/lead/nslcm/v1", msbApiProvider.getMicroServiceUrl("serviceName", "v1")); + } + + /** + * if unable to get micro service info the error is propagated + */ + @Test + public void testUnableQueryMicroserviInfo() throws Exception { + msbApiProvider = new MsbApiProvider(environment) { + @Override + public MSBServiceClient getMsbClient() { + return msbClient; + } + }; + RouteException expectedException = new RouteException(); + when(msbClient.queryMicroServiceInfo("serviceName", "v1")).thenThrow(expectedException); + + //when + try { + msbApiProvider.getMicroServiceUrl("serviceName", "v1"); + fail(); + } catch (Exception e) { + assertEquals("Unable to get micro service URL for serviceName with version v1", e.getMessage()); + verify(logger).error("Unable to get micro service URL for serviceName with version v1", expectedException); + } + } + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/vfc/TestVfcExternalSystemInfoProvider.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/vfc/TestVfcExternalSystemInfoProvider.java new file mode 100644 index 00000000..1081b93c --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/vfc/TestVfcExternalSystemInfoProvider.java @@ -0,0 +1,100 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc; + +import org.junit.Before; +import org.junit.Test; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; +import org.onap.vnfmdriver.ApiException; +import org.onap.vnfmdriver.model.VimInfo; +import org.onap.vnfmdriver.model.VnfmInfo; +import org.springframework.test.util.ReflectionTestUtils; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.fail; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class TestVfcExternalSystemInfoProvider extends TestBase { + private VfcExternalSystemInfoProvider vfcExternalSystemInfoProvider; + + @Before + public void init() { + vfcExternalSystemInfoProvider = new VfcExternalSystemInfoProvider(environment, vfcRestApiProvider); + ReflectionTestUtils.setField(VfcExternalSystemInfoProvider.class, "logger", logger); + } + + /** + * VIM is queried using VF-C APIs + */ + @Test + public void testVimRetrieval() throws Exception { + VimInfo expectedVimInfo = new VimInfo(); + when(nsLcmApi.queryVIMInfo(VIM_ID)).thenReturn(expectedVimInfo); + //when + VimInfo vimInfo = vfcExternalSystemInfoProvider.getVimInfo(VIM_ID); + //verify + assertEquals(expectedVimInfo, vimInfo); + } + + /** + * failure to retrieve VIM from VF-C is propagated + */ + @Test + public void testUnableToQueryVim() throws Exception { + ApiException expectedException = new ApiException(); + when(nsLcmApi.queryVIMInfo(VIM_ID)).thenThrow(expectedException); + //when + try { + vfcExternalSystemInfoProvider.getVimInfo(VIM_ID); + fail(); + } catch (Exception e) { + assertEquals("Unable to query VIM from VF-C with myVimId identifier", e.getMessage()); + verify(logger).error("Unable to query VIM from VF-C with myVimId identifier", expectedException); + } + } + + /** + * VNFM is queried using VF-C APIs + */ + @Test + public void testVnfmRetrieval() throws Exception { + VnfmInfo expectedVimInfo = new VnfmInfo(); + when(nsLcmApi.queryVnfmInfo(VNFM_ID)).thenReturn(expectedVimInfo); + //when + VnfmInfo vimInfo = vfcExternalSystemInfoProvider.queryVnfmInfoFromSource(VNFM_ID); + //verify + assertEquals(expectedVimInfo, vimInfo); + } + + /** + * failure to retrieve VNFM from VF-C is propagated + */ + @Test + public void testUnableToQueryVnfm() throws Exception { + ApiException expectedException = new ApiException(); + when(nsLcmApi.queryVnfmInfo(VNFM_ID)).thenThrow(expectedException); + //when + try { + vfcExternalSystemInfoProvider.queryVnfmInfoFromSource(VNFM_ID); + fail(); + } catch (Exception e) { + assertEquals("Unable to query VNFM from VF-C with myVnfmId identifier", e.getMessage()); + verify(logger).error("Unable to query VNFM from VF-C with myVnfmId identifier", expectedException); + } + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/vfc/TestVfcGrantManager.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/vfc/TestVfcGrantManager.java new file mode 100644 index 00000000..7ca19d63 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/vfc/TestVfcGrantManager.java @@ -0,0 +1,337 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc; + +import com.google.common.collect.Lists; +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.nokia.cbam.lcm.v32.model.*; +import com.nokia.cbam.lcm.v32.model.VnfInfo; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; +import org.onap.vnfmdriver.ApiException; +import org.onap.vnfmdriver.model.*; +import org.onap.vnfmdriver.model.ScaleDirection; + +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import static java.nio.file.Files.readAllBytes; +import static junit.framework.TestCase.*; +import static org.mockito.Mockito.*; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCM_API_VERSION; +import static org.springframework.test.util.ReflectionTestUtils.setField; + +public class TestVfcGrantManager extends TestBase { + + private ArgumentCaptor<GrantVNFRequest> grantRequest = ArgumentCaptor.forClass(GrantVNFRequest.class); + private GrantVNFResponseVim vim = new GrantVNFResponseVim(); + private GrantVNFResponse grantResponse = new GrantVNFResponse(); + @Mock + private CatalogManager cbamCatalogManager; + @InjectMocks + private VfcGrantManager vfcGrantManager; + + @Before + public void initMocks() throws Exception { + setField(VfcGrantManager.class, "logger", logger); + when(nsLcmApi.grantvnf(grantRequest.capture())).thenReturn(grantResponse); + grantResponse.setVim(vim); + } + + /** + * test grant request for instantiation + */ + @Test + public void testGrantDuringInstantiation() throws Exception { + String cbamVnfdContent = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/vnfd.instantiation.yaml").toURI()))); + //when + vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, "level1", cbamVnfdContent, JOB_ID); + //verify + assertEquals(1, grantRequest.getAllValues().size()); + GrantVNFRequest request = grantRequest.getValue(); + assertVduInGrant(request.getAddResource(), "vdu1", 1); + assertVduInGrant(request.getAddResource(), "vdu2", 2); + assertEquals(0, request.getRemoveResource().size()); + assertBasicGrantAttributes(request, org.onap.vnfmdriver.model.OperationType.INSTANTIATE); + } + + /** + * test failure logging & propagation during grant request for instantiation + */ + @Test + public void testFailureDuringGrantPreparation() throws Exception { + String cbamVnfdContent = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/vnfd.instantiation.yaml").toURI()))); + //when + try { + vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, "missingLevel", cbamVnfdContent, JOB_ID); + //verify + fail(); + } catch (RuntimeException e) { + verify(logger).error(Mockito.eq("Unable to prepare grant request for instantiation"), Mockito.any(RuntimeException.class)); + verifyNoMoreInteractions(nsLcmApi); + } + } + + /** + * test grant request for instantiation + */ + @Test + public void testFailureDuringGrantRequest() throws Exception { + String cbamVnfdContent = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/vnfd.instantiation.yaml").toURI()))); + ApiException expectedException = new ApiException("a"); + when(nsLcmApi.grantvnf(Mockito.any())).thenThrow(expectedException); + //when + try { + vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, "level1", cbamVnfdContent, JOB_ID); + //verify + fail(); + } catch (RuntimeException e) { + verify(logger).error("Unable to request grant", expectedException); + } + } + + /** + * No grant is requested for termination if the the VNF is not instantiated + */ + @Test + public void testNoGrantIsRequestedIfNotInstantiated() { + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnf.setInstantiationState(InstantiationState.NOT_INSTANTIATED); + //when + vfcGrantManager.requestGrantForTerminate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, vnf, JOB_ID); + //verify + verifyNoMoreInteractions(nsLcmApi); + } + + /** + * grant is requested for termination if the the VNF is instantiated + */ + @Test + public void testGrantIsRequestedIfInstantiated() { + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnf.setInstantiationState(InstantiationState.INSTANTIATED); + InstantiatedVnfInfo instantiatedVnfInfo = new InstantiatedVnfInfo(); + VnfcResourceInfo vnfc = new VnfcResourceInfo(); + vnfc.setId("vnfcId1"); + vnfc.setVduId("vdu1"); + instantiatedVnfInfo.setVnfcResourceInfo(new ArrayList<>()); + instantiatedVnfInfo.getVnfcResourceInfo().add(vnfc); + vnf.setInstantiatedVnfInfo(instantiatedVnfInfo); + VnfProperty prop = new VnfProperty(); + prop.setName(LifecycleManager.ONAP_CSAR_ID); + prop.setValue(ONAP_CSAR_ID); + vnf.setVnfConfigurableProperties(new ArrayList<>()); + vnf.getVnfConfigurableProperties().add(prop); + //when + vfcGrantManager.requestGrantForTerminate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, vnf, JOB_ID); + //verify + assertEquals(1, grantRequest.getAllValues().size()); + GrantVNFRequest request = grantRequest.getValue(); + assertVduInGrant(request.getRemoveResource(), "vdu1", 1); + assertVduInGrant(request.getRemoveResource(), "vdu2", 0); + assertEquals(0, request.getAddResource().size()); + assertBasicGrantAttributes(request, org.onap.vnfmdriver.model.OperationType.TERMINAL); + } + + /** + * test failure logging & propagation during grant request for instantiation + */ + @Test + public void testFailureDuringTerminationGrantPreparation() throws Exception { + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnf.setInstantiatedVnfInfo(null); + vnf.setInstantiationState(InstantiationState.INSTANTIATED); + //when + try { + vfcGrantManager.requestGrantForTerminate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, vnf, JOB_ID); + //verify + fail(); + } catch (RuntimeException e) { + verify(logger).error(Mockito.eq("Unable to prepare grant request for termination"), Mockito.any(RuntimeException.class)); + verifyNoMoreInteractions(nsLcmApi); + } + } + + /** + * failuire is to request grant is logged + */ + @Test + public void testFailureToRequestGrantIsLogged() throws Exception { + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnf.setInstantiationState(InstantiationState.INSTANTIATED); + InstantiatedVnfInfo instantiatedVnfInfo = new InstantiatedVnfInfo(); + VnfcResourceInfo vnfc = new VnfcResourceInfo(); + vnfc.setId("vnfcId1"); + vnfc.setVduId("vdu1"); + instantiatedVnfInfo.setVnfcResourceInfo(new ArrayList<>()); + instantiatedVnfInfo.getVnfcResourceInfo().add(vnfc); + vnf.setInstantiatedVnfInfo(instantiatedVnfInfo); + VnfProperty prop = new VnfProperty(); + prop.setName(LifecycleManager.ONAP_CSAR_ID); + prop.setValue(ONAP_CSAR_ID); + vnf.setVnfConfigurableProperties(new ArrayList<>()); + vnf.getVnfConfigurableProperties().add(prop); + ApiException expectedException = new ApiException(); + when(nsLcmApi.grantvnf(Mockito.any())).thenThrow(expectedException); + //when + try { + vfcGrantManager.requestGrantForTerminate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, vnf, JOB_ID); + //verify + fail(); + } catch (RuntimeException e) { + verify(logger).error(Mockito.eq("Unable to request grant"), Mockito.eq(expectedException)); + } + } + + /** + * failuire is to request grant is logged + */ + @Test + public void testFailureToRequestGrantForScaleIsLogged() throws Exception { + String cbamVnfdContent = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/vnfd.scale.yaml").toURI()))); + VnfScaleRequest scaleRequest = new VnfScaleRequest(); + scaleRequest.setType(ScaleDirection.OUT); + scaleRequest.setAspectId("aspect1"); + scaleRequest.setNumberOfSteps("2"); + com.nokia.cbam.lcm.v32.ApiException expectedException = new com.nokia.cbam.lcm.v32.ApiException(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + //when + try { + vfcGrantManager.requestGrantForScale(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, scaleRequest, JOB_ID); + //verify + fail(); + } catch (RuntimeException e) { + verify(logger).error(Mockito.eq("Unable to query VNF myVnfId"), Mockito.eq(expectedException)); + assertEquals(e.getCause(), expectedException); + } + } + + /** + * test grant request for scale out + */ + @Test + public void testGrantDuringScaleOut() throws Exception { + String cbamVnfdContent = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/vnfd.scale.yaml").toURI()))); + VnfScaleRequest scaleRequest = new VnfScaleRequest(); + scaleRequest.setType(ScaleDirection.OUT); + scaleRequest.setAspectId("aspect1"); + scaleRequest.setNumberOfSteps("2"); + VnfInfo vnf = new VnfInfo(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnf); + vnf.setVnfdId(CBAM_VNFD_ID); + when(cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID)).thenReturn(cbamVnfdContent); + //when + vfcGrantManager.requestGrantForScale(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, scaleRequest, JOB_ID); + //verify + assertEquals(1, grantRequest.getAllValues().size()); + GrantVNFRequest request = grantRequest.getValue(); + assertVduInGrant(request.getAddResource(), "vdu1", 4); + assertVduInGrant(request.getAddResource(), "vdu2", 2); + assertEquals(0, request.getRemoveResource().size()); + assertBasicGrantAttributes(request, org.onap.vnfmdriver.model.OperationType.SCALEOUT); + } + + /** + * test grant request for scale in + */ + @Test + public void testGrantDuringScaleIn() throws Exception { + String cbamVnfdContent = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/vnfd.scale.yaml").toURI()))); + VnfScaleRequest scaleRequest = new VnfScaleRequest(); + scaleRequest.setType(ScaleDirection.IN); + scaleRequest.setAspectId("aspect1"); + scaleRequest.setNumberOfSteps("2"); + VnfInfo vnf = new VnfInfo(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnf); + vnf.setVnfdId(CBAM_VNFD_ID); + when(cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID)).thenReturn(cbamVnfdContent); + //when + vfcGrantManager.requestGrantForScale(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, scaleRequest, JOB_ID); + //verify + assertEquals(1, grantRequest.getAllValues().size()); + GrantVNFRequest request = grantRequest.getValue(); + assertVduInGrant(request.getRemoveResource(), "vdu1", 4); + assertVduInGrant(request.getRemoveResource(), "vdu2", 2); + assertEquals(0, request.getAddResource().size()); + assertBasicGrantAttributes(request, org.onap.vnfmdriver.model.OperationType.SCALEIN); + } + + + /** + * test grant request for healing + */ + @Test + public void testGrantDuringHealing() throws Exception { + //when + VnfHealRequest healRequest = new VnfHealRequest(); + VnfHealRequestAffectedvm affectedVm = new VnfHealRequestAffectedvm(); + affectedVm.setVduid("vdu1"); + healRequest.setAffectedvm(affectedVm); + vfcGrantManager.requestGrantForHeal(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, healRequest, JOB_ID); + //verify + assertEquals(1, grantRequest.getAllValues().size()); + GrantVNFRequest request = grantRequest.getValue(); + assertVduInGrant(request.getAddResource(), "vdu1", 1); + assertVduInGrant(request.getRemoveResource(), "vdu1", 1); + assertBasicGrantAttributes(request, org.onap.vnfmdriver.model.OperationType.HEAL); + } + + private void assertBasicGrantAttributes(GrantVNFRequest request, org.onap.vnfmdriver.model.OperationType type) { + assertEquals(JOB_ID, request.getJobId()); + assertEquals(type, request.getLifecycleOperation()); + assertEquals(ONAP_CSAR_ID, request.getVnfDescriptorId()); + assertEquals(VNF_ID, request.getVnfInstanceId()); + JsonObject additionalParams = new Gson().toJsonTree(request.getAdditionalParam()).getAsJsonObject(); + assertEquals(VIM_ID, additionalParams.get("vimId").getAsString()); + assertEquals(VNFM_ID, additionalParams.get("vnfmId").getAsString()); + } + + private void assertVduInGrant(List<ResourceChange> changes, String vduName, int count) { + ArrayList<ResourceChange> clonedChanges = Lists.newArrayList(changes); + for (int i = 0; i < count + 1; i++) { + Iterator<ResourceChange> iter = clonedChanges.iterator(); + boolean found = false; + while (iter.hasNext()) { + ResourceChange change = iter.next(); + if (change.getVdu().equals(vduName)) { + iter.remove(); + found = true; + break; + } + } + if (i >= count) { + assertFalse(found); + } else { + assertTrue(found); + } + } + } + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/vfc/TestVfcNotificationSender.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/vfc/TestVfcNotificationSender.java new file mode 100644 index 00000000..42906c7b --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/vfc/TestVfcNotificationSender.java @@ -0,0 +1,760 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc; + +import com.google.common.collect.Lists; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import com.nokia.cbam.lcm.v32.model.AffectedVirtualLink; +import com.nokia.cbam.lcm.v32.model.AffectedVirtualStorage; +import com.nokia.cbam.lcm.v32.model.AffectedVnfc; +import com.nokia.cbam.lcm.v32.model.ChangeType; +import com.nokia.cbam.lcm.v32.model.*; +import com.nokia.cbam.lcm.v32.model.OperationType; +import com.nokia.cbam.lcm.v32.model.ScaleDirection; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.ReportedAffectedConnectionPoints; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.ReportedAffectedCp; +import org.onap.vnfmdriver.ApiException; +import org.onap.vnfmdriver.model.*; +import org.threeten.bp.OffsetDateTime; + +import java.util.ArrayList; +import java.util.List; + +import static junit.framework.TestCase.*; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.*; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCM_API_VERSION; +import static org.springframework.test.util.ReflectionTestUtils.setField; + +public class TestVfcNotificationSender extends TestBase { + private VfcNotificationSender vfcNotificationSender; + private ArgumentCaptor<VNFLCMNotification> sentLcnToVfc = ArgumentCaptor.forClass(VNFLCMNotification.class); + private VnfLifecycleChangeNotification recievedLcn = new VnfLifecycleChangeNotification(); + private List<OperationExecution> operationExecutions = new ArrayList<>(); + private OperationExecution instantiationOperation = new OperationExecution(); + private OperationExecution scaleOperation = new OperationExecution(); + private OperationExecution healOperation = new OperationExecution(); + private OperationExecution terminationOperation = new OperationExecution(); + private ReportedAffectedConnectionPoints affectedCp; + + + @Before + public void init() throws Exception { + vfcNotificationSender = new VfcNotificationSender(driverProperties, vfcRestApiProvider); + setField(VfcNotificationSender.class, "logger", logger); + Mockito.doNothing().when(nsLcmApi).vNFLCMNotification(eq(VNFM_ID), eq(VNF_ID), sentLcnToVfc.capture()); + instantiationOperation.setId("instantiationOperationExecutionId"); + instantiationOperation.setStartTime(OffsetDateTime.now()); + instantiationOperation.setOperationType(OperationType.INSTANTIATE); + scaleOperation.setId("scaleOperationExecutionId"); + scaleOperation.setStartTime(OffsetDateTime.now().plusDays(1)); + scaleOperation.setOperationType(OperationType.SCALE); + terminationOperation.setId("terminationExecutionId"); + terminationOperation.setStartTime(OffsetDateTime.now().plusDays(1)); + terminationOperation.setOperationType(OperationType.TERMINATE); + healOperation.setId("healOperaitonExecutionId"); + healOperation.setOperationType(OperationType.HEAL); + healOperation.setStartTime(OffsetDateTime.now().plusDays(1)); + when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(operationExecutions); + prepOperation(instantiationOperation); + prepOperation(scaleOperation); + prepOperation(healOperation); + prepOperation(terminationOperation); + recievedLcn.setVnfInstanceId(VNF_ID); + } + + private void prepOperation(OperationExecution operationExecution) throws com.nokia.cbam.lcm.v32.ApiException { + addEmptyModifiedConnectionPoints(operationExecution); + JsonElement root = new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + JOB_ID + "\"}}"); + operationExecution.setOperationParams(root); + switch (operationExecution.getOperationType()) { + case TERMINATE: + root.getAsJsonObject().addProperty("terminationType", "GRACEFULL"); + } + when(operationExecutionApi.operationExecutionsOperationExecutionIdGet(operationExecution.getId(), NOKIA_LCM_API_VERSION)).thenReturn(operationExecution); + operationExecutions.add(operationExecution); + } + + private void addEmptyModifiedConnectionPoints(OperationExecution operationExecution) { + OperationResult operationResult = new OperationResult(); + operationResult.operationResult = new ReportedAffectedConnectionPoints(); + JsonElement additionalData = new Gson().toJsonTree(operationResult); + operationExecution.setAdditionalData(additionalData); + } + + /** + * test start notification success scenario + * - the affected resources are not processed even if present + * - LCN is sent to VF-C + */ + @Test + public void testStartLcn() { + recievedLcn.setStatus(OperationStatus.STARTED); + recievedLcn.setOperation(OperationType.INSTANTIATE); + //when + vfcNotificationSender.processNotification(recievedLcn, instantiationOperation, affectedCp, VIM_ID); + //verify + assertEquals(1, sentLcnToVfc.getAllValues().size()); + assertNull(sentLcnToVfc.getValue().getAffectedVl()); + assertNull(sentLcnToVfc.getValue().getAffectedVnfc()); + assertNull(sentLcnToVfc.getValue().getAffectedCp()); + assertNull(sentLcnToVfc.getValue().getAffectedVirtualStorage()); + + assertEquals(JOB_ID, sentLcnToVfc.getValue().getJobId()); + assertEquals(org.onap.vnfmdriver.model.OperationType.INSTANTIATE, sentLcnToVfc.getValue().getOperation()); + assertEquals(VnfLcmNotificationStatus.START, sentLcnToVfc.getValue().getStatus()); + assertEquals(VNF_ID, sentLcnToVfc.getValue().getVnfInstanceId()); + } + + /** + * test end notification success scenario + * - LCN is sent to VF-C + */ + @Test + public void testFinishLcn() { + //given + recievedLcn.setOperation(OperationType.INSTANTIATE); + recievedLcn.setStatus(OperationStatus.FINISHED); + AffectedVnfc affectedVnfc = new AffectedVnfc(); + affectedVnfc.setChangeType(ChangeType.ADDED); + affectedVnfc.setId("myVnfcId"); + affectedVnfc.setVduId("myVduId"); + affectedVnfc.setStorageResourceIds(Lists.newArrayList("storageId1")); + affectedVnfc.setComputeResource(new ResourceHandle()); + affectedVnfc.getComputeResource().setResourceId("serverProviderId"); + affectedVnfc.getComputeResource().setVimId(VIM_ID); + affectedVnfc.getComputeResource().setAdditionalData(new JsonParser().parse("{ \"name\" : \"myVmName\" } ")); + recievedLcn.setAffectedVnfcs(new ArrayList<>()); + recievedLcn.getAffectedVnfcs().add(affectedVnfc); + + AffectedVirtualLink affectedVirtualLink = new AffectedVirtualLink(); + affectedVirtualLink.setChangeType(ChangeType.ADDED); + affectedVirtualLink.setId("vlId"); + affectedVirtualLink.setVirtualLinkDescId("vlVnfdId"); + affectedVirtualLink.setResource(new ResourceHandle()); + affectedVirtualLink.getResource().setVimId(VIM_ID); + affectedVirtualLink.getResource().setResourceId("networkProviderId"); + recievedLcn.setAffectedVirtualLinks(new ArrayList<>()); + recievedLcn.getAffectedVirtualLinks().add(affectedVirtualLink); + + AffectedVirtualStorage affectedStorage = new AffectedVirtualStorage(); + affectedStorage.setChangeType(ChangeType.ADDED); + affectedStorage.setId("storageId"); + affectedStorage.setVirtualStorageDescId("storageVnfdId"); + affectedStorage.setResource(new ResourceHandle()); + affectedStorage.getResource().setVimId(VIM_ID); + affectedStorage.getResource().setResourceId("storageProviderId"); + recievedLcn.setAffectedVirtualStorages(new ArrayList<>()); + recievedLcn.getAffectedVirtualStorages().add(affectedStorage); + + ReportedAffectedConnectionPoints affectedConnectionPoints = new ReportedAffectedConnectionPoints(); + ReportedAffectedCp affectedCp = new ReportedAffectedCp(); + affectedCp.setChangeType(ChangeType.ADDED); + affectedCp.setCpdId("cpVnfdId"); + affectedCp.setIpAddress("1.2.3.4"); + affectedCp.setMacAddress("myMac"); + affectedCp.setName("myPortName"); + affectedCp.setCpId("cpId"); + + affectedCp.setNetworkProviderId("networkProviderId"); + affectedCp.setProviderId("portProviderId"); + affectedCp.setServerProviderId("serverProviderId"); + affectedCp.setTenantId("tenantId"); + affectedConnectionPoints.getPost().add(affectedCp); + + OperationResult operationResult = new OperationResult(); + operationResult.operationResult = affectedConnectionPoints; + JsonElement additionalData = new Gson().toJsonTree(operationResult); + instantiationOperation.setAdditionalData(additionalData); + //when + vfcNotificationSender.processNotification(recievedLcn, instantiationOperation, affectedConnectionPoints, VIM_ID); + //verify + assertEquals(1, sentLcnToVfc.getAllValues().size()); + + assertEquals(1, sentLcnToVfc.getValue().getAffectedVl().size()); + org.onap.vnfmdriver.model.AffectedVirtualLink actualAffectedVl = sentLcnToVfc.getValue().getAffectedVl().get(0); + assertEquals(org.onap.vnfmdriver.model.VnfNotificationType.ADDED, actualAffectedVl.getChangeType()); + assertEquals("vlVnfdId", actualAffectedVl.getVldid()); + assertEquals("myVnfId_vlId", actualAffectedVl.getVlInstanceId()); + assertEquals("networkProviderId", actualAffectedVl.getNetworkResource().getResourceId()); + assertEquals(AffectedVirtualLinkType.NETWORK, actualAffectedVl.getNetworkResource().getResourceType()); + + assertEquals(1, sentLcnToVfc.getValue().getAffectedVnfc().size()); + org.onap.vnfmdriver.model.AffectedVnfc actualAffectdVnfc = sentLcnToVfc.getValue().getAffectedVnfc().get(0); + assertEquals("myVduId", actualAffectdVnfc.getVduId()); + assertEquals(VIM_ID, actualAffectdVnfc.getVimid()); + assertEquals("myVmName", actualAffectdVnfc.getVmname()); + assertEquals("serverProviderId", actualAffectdVnfc.getVmid()); + assertEquals(org.onap.vnfmdriver.model.VnfNotificationType.ADDED, actualAffectdVnfc.getChangeType()); + assertEquals("myVnfId_myVnfcId", actualAffectdVnfc.getVnfcInstanceId()); + + assertEquals(1, sentLcnToVfc.getValue().getAffectedCp().size()); + AffectedCp actualAffectedCp = sentLcnToVfc.getValue().getAffectedCp().get(0); + assertEquals("cpVnfdId", actualAffectedCp.getCpdid()); + assertEquals("myVnfId_cpId", actualAffectedCp.getCpinstanceid()); + assertEquals(null, actualAffectedCp.getOwnerId()); + assertEquals(null, actualAffectedCp.getOwnerType()); + assertEquals("networkProviderId", actualAffectedCp.getVirtualLinkInstanceId()); + assertEquals("1.2.3.4", actualAffectedCp.getPortResource().getIpAddress()); + assertEquals("myMac", actualAffectedCp.getPortResource().getMacAddress()); + assertEquals("tenantId", actualAffectedCp.getPortResource().getTenant()); + assertEquals(VIM_ID, actualAffectedCp.getPortResource().getVimid()); + assertEquals("serverProviderId", actualAffectedCp.getPortResource().getInstId()); + assertEquals("portProviderId", actualAffectedCp.getPortResource().getResourceid()); + assertEquals("myPortName", actualAffectedCp.getPortResource().getResourceName()); + assertEquals(VnfCpNotificationType.ADDED, actualAffectedCp.getChangeType()); + + assertNull(sentLcnToVfc.getValue().getAffectedVirtualStorage()); + assertEquals(JOB_ID, sentLcnToVfc.getValue().getJobId()); + assertEquals(org.onap.vnfmdriver.model.OperationType.INSTANTIATE, sentLcnToVfc.getValue().getOperation()); + assertEquals(VnfLcmNotificationStatus.RESULT, sentLcnToVfc.getValue().getStatus()); + assertEquals(VNF_ID, sentLcnToVfc.getValue().getVnfInstanceId()); + } + + /** + * test end notification success scenario for ECP + */ + @Test + public void testFinishLcnForEcp() { + //given + recievedLcn.setOperation(OperationType.INSTANTIATE); + recievedLcn.setStatus(OperationStatus.FINISHED); + AffectedVnfc affectedVnfc = new AffectedVnfc(); + affectedVnfc.setChangeType(ChangeType.ADDED); + affectedVnfc.setId("myVnfcId"); + affectedVnfc.setVduId("myVduId"); + affectedVnfc.setStorageResourceIds(Lists.newArrayList("storageId1")); + affectedVnfc.setComputeResource(new ResourceHandle()); + affectedVnfc.getComputeResource().setResourceId("serverProviderId"); + affectedVnfc.getComputeResource().setVimId(VIM_ID); + affectedVnfc.getComputeResource().setAdditionalData(new JsonParser().parse("{ \"name\" : \"myVmName\" } ")); + recievedLcn.setAffectedVnfcs(new ArrayList<>()); + recievedLcn.getAffectedVnfcs().add(affectedVnfc); + + ReportedAffectedConnectionPoints affectedConnectionPoints = new ReportedAffectedConnectionPoints(); + ReportedAffectedCp affectedCp = new ReportedAffectedCp(); + affectedCp.setChangeType(ChangeType.ADDED); + //affectedCp.setCpdId("cpVnfdId"); + affectedCp.setIpAddress("1.2.3.4"); + affectedCp.setMacAddress("myMac"); + affectedCp.setName("myPortName"); + affectedCp.setCpId("cpId"); + affectedCp.setEcpdId("ecpdId"); + affectedCp.setNetworkProviderId("networkProviderId"); + affectedCp.setProviderId("portProviderId"); + affectedCp.setServerProviderId("serverProviderId"); + affectedCp.setTenantId("tenantId"); + affectedConnectionPoints.getPost().add(affectedCp); + + OperationResult operationResult = new OperationResult(); + operationResult.operationResult = affectedConnectionPoints; + JsonElement additionalData = new Gson().toJsonTree(operationResult); + instantiationOperation.setAdditionalData(additionalData); + //when + vfcNotificationSender.processNotification(recievedLcn, instantiationOperation, affectedConnectionPoints, VIM_ID); + //verify + assertEquals(1, sentLcnToVfc.getAllValues().size()); + + assertEquals(1, sentLcnToVfc.getValue().getAffectedCp().size()); + AffectedCp actualAffectedCp = sentLcnToVfc.getValue().getAffectedCp().get(0); + assertEquals("ecpdId", actualAffectedCp.getCpdid()); + assertEquals("myVnfId_cpId", actualAffectedCp.getCpinstanceid()); + assertEquals(null, actualAffectedCp.getOwnerId()); + assertEquals(null, actualAffectedCp.getOwnerType()); + assertEquals("networkProviderId", actualAffectedCp.getVirtualLinkInstanceId()); + assertEquals("1.2.3.4", actualAffectedCp.getPortResource().getIpAddress()); + assertEquals("myMac", actualAffectedCp.getPortResource().getMacAddress()); + assertEquals("tenantId", actualAffectedCp.getPortResource().getTenant()); + assertEquals(VIM_ID, actualAffectedCp.getPortResource().getVimid()); + assertEquals("serverProviderId", actualAffectedCp.getPortResource().getInstId()); + assertEquals("portProviderId", actualAffectedCp.getPortResource().getResourceid()); + assertEquals("myPortName", actualAffectedCp.getPortResource().getResourceName()); + assertEquals(VnfCpNotificationType.ADDED, actualAffectedCp.getChangeType()); + } + + /** + * test end notification success scenario with termination + */ + @Test + public void testFinishLcnWithTerminate() { + //given + recievedLcn.setOperation(OperationType.TERMINATE); + recievedLcn.setStatus(OperationStatus.FINISHED); + AffectedVnfc affectedVnfc = new AffectedVnfc(); + affectedVnfc.setChangeType(ChangeType.REMOVED); + affectedVnfc.setId("myVnfcId"); + affectedVnfc.setVduId("myVduId"); + affectedVnfc.setStorageResourceIds(Lists.newArrayList("storageId1")); + affectedVnfc.setComputeResource(new ResourceHandle()); + affectedVnfc.getComputeResource().setResourceId("serverProviderId"); + affectedVnfc.getComputeResource().setVimId(VIM_ID); + affectedVnfc.getComputeResource().setAdditionalData(new JsonParser().parse("{ \"name\" : \"myVmName\" } ")); + recievedLcn.setAffectedVnfcs(new ArrayList<>()); + recievedLcn.getAffectedVnfcs().add(affectedVnfc); + + AffectedVirtualLink affectedVirtualLink = new AffectedVirtualLink(); + affectedVirtualLink.setChangeType(ChangeType.REMOVED); + affectedVirtualLink.setId("vlId"); + affectedVirtualLink.setVirtualLinkDescId("vlVnfdId"); + affectedVirtualLink.setResource(new ResourceHandle()); + affectedVirtualLink.getResource().setVimId(VIM_ID); + affectedVirtualLink.getResource().setResourceId("networkProviderId"); + recievedLcn.setAffectedVirtualLinks(new ArrayList<>()); + recievedLcn.getAffectedVirtualLinks().add(affectedVirtualLink); + + AffectedVirtualStorage affectedStorage = new AffectedVirtualStorage(); + affectedStorage.setChangeType(ChangeType.REMOVED); + affectedStorage.setId("storageId"); + affectedStorage.setVirtualStorageDescId("storageVnfdId"); + affectedStorage.setResource(new ResourceHandle()); + affectedStorage.getResource().setVimId(VIM_ID); + affectedStorage.getResource().setResourceId("storageProviderId"); + recievedLcn.setAffectedVirtualStorages(new ArrayList<>()); + recievedLcn.getAffectedVirtualStorages().add(affectedStorage); + + ReportedAffectedConnectionPoints affectedConnectionPoints = new ReportedAffectedConnectionPoints(); + ReportedAffectedCp affectedCp = new ReportedAffectedCp(); + affectedCp.setChangeType(ChangeType.REMOVED); + affectedCp.setCpdId("cpVnfdId"); + affectedCp.setIpAddress("1.2.3.4"); + affectedCp.setMacAddress("myMac"); + affectedCp.setName("myPortName"); + affectedCp.setCpId("cpId"); + + // affectedCp.setEcpdId("ecpdId"); + affectedCp.setNetworkProviderId("networkProviderId"); + affectedCp.setProviderId("portProviderId"); + affectedCp.setServerProviderId("serverProviderId"); + affectedCp.setTenantId("tenantId"); + affectedConnectionPoints.getPost().add(affectedCp); + + OperationResult operationResult = new OperationResult(); + operationResult.operationResult = affectedConnectionPoints; + JsonElement additionalData = new Gson().toJsonTree(operationResult); + instantiationOperation.setAdditionalData(additionalData); + //when + vfcNotificationSender.processNotification(recievedLcn, terminationOperation, affectedConnectionPoints, VIM_ID); + //verify + assertEquals(1, sentLcnToVfc.getAllValues().size()); + + assertEquals(1, sentLcnToVfc.getValue().getAffectedVl().size()); + org.onap.vnfmdriver.model.AffectedVirtualLink actualAffectedVl = sentLcnToVfc.getValue().getAffectedVl().get(0); + assertEquals(org.onap.vnfmdriver.model.VnfNotificationType.REMOVED, actualAffectedVl.getChangeType()); + assertEquals("vlVnfdId", actualAffectedVl.getVldid()); + assertEquals("myVnfId_vlId", actualAffectedVl.getVlInstanceId()); + assertEquals("networkProviderId", actualAffectedVl.getNetworkResource().getResourceId()); + assertEquals(AffectedVirtualLinkType.NETWORK, actualAffectedVl.getNetworkResource().getResourceType()); + + assertEquals(1, sentLcnToVfc.getValue().getAffectedVnfc().size()); + org.onap.vnfmdriver.model.AffectedVnfc actualAffectdVnfc = sentLcnToVfc.getValue().getAffectedVnfc().get(0); + assertEquals("myVduId", actualAffectdVnfc.getVduId()); + assertEquals(VIM_ID, actualAffectdVnfc.getVimid()); + assertEquals("myVmName", actualAffectdVnfc.getVmname()); + assertEquals("serverProviderId", actualAffectdVnfc.getVmid()); + assertEquals(org.onap.vnfmdriver.model.VnfNotificationType.REMOVED, actualAffectdVnfc.getChangeType()); + assertEquals("myVnfId_myVnfcId", actualAffectdVnfc.getVnfcInstanceId()); + + assertEquals(1, sentLcnToVfc.getValue().getAffectedCp().size()); + AffectedCp actualAffectedCp = sentLcnToVfc.getValue().getAffectedCp().get(0); + assertEquals("cpVnfdId", actualAffectedCp.getCpdid()); + assertEquals("myVnfId_cpId", actualAffectedCp.getCpinstanceid()); + assertEquals(null, actualAffectedCp.getOwnerId()); + assertEquals(null, actualAffectedCp.getOwnerType()); + assertEquals("networkProviderId", actualAffectedCp.getVirtualLinkInstanceId()); + assertEquals("1.2.3.4", actualAffectedCp.getPortResource().getIpAddress()); + assertEquals("myMac", actualAffectedCp.getPortResource().getMacAddress()); + assertEquals("tenantId", actualAffectedCp.getPortResource().getTenant()); + assertEquals(VnfCpNotificationType.REMOVED, actualAffectedCp.getChangeType()); + assertEquals(VIM_ID, actualAffectedCp.getPortResource().getVimid()); + assertEquals("serverProviderId", actualAffectedCp.getPortResource().getInstId()); + assertEquals("portProviderId", actualAffectedCp.getPortResource().getResourceid()); + assertEquals("myPortName", actualAffectedCp.getPortResource().getResourceName()); + + assertNull(sentLcnToVfc.getValue().getAffectedVirtualStorage()); + assertEquals(JOB_ID, sentLcnToVfc.getValue().getJobId()); + assertEquals(org.onap.vnfmdriver.model.OperationType.TERMINAL, sentLcnToVfc.getValue().getOperation()); + assertEquals(VnfLcmNotificationStatus.RESULT, sentLcnToVfc.getValue().getStatus()); + assertEquals(VNF_ID, sentLcnToVfc.getValue().getVnfInstanceId()); + } + + /** + * test end notification success scenario for modifiction (heal) + * - LCN is sent to VF-C + */ + @Test + public void testFinishLcnForModification() { + //given + recievedLcn.setOperation(OperationType.HEAL); + recievedLcn.setStatus(OperationStatus.FINISHED); + AffectedVnfc affectedVnfc = new AffectedVnfc(); + affectedVnfc.setChangeType(ChangeType.MODIFIED); + affectedVnfc.setId("myVnfcId"); + affectedVnfc.setVduId("myVduId"); + affectedVnfc.setStorageResourceIds(Lists.newArrayList("storageId1")); + affectedVnfc.setComputeResource(new ResourceHandle()); + affectedVnfc.getComputeResource().setResourceId("serverProviderId"); + affectedVnfc.getComputeResource().setVimId(VIM_ID); + affectedVnfc.getComputeResource().setAdditionalData(new JsonParser().parse("{ \"name\" : \"myVmName\" } ")); + recievedLcn.setAffectedVnfcs(new ArrayList<>()); + recievedLcn.getAffectedVnfcs().add(affectedVnfc); + + AffectedVirtualLink affectedVirtualLink = new AffectedVirtualLink(); + affectedVirtualLink.setChangeType(ChangeType.MODIFIED); + affectedVirtualLink.setId("vlId"); + affectedVirtualLink.setVirtualLinkDescId("vlVnfdId"); + affectedVirtualLink.setResource(new ResourceHandle()); + affectedVirtualLink.getResource().setVimId(VIM_ID); + affectedVirtualLink.getResource().setResourceId("networkProviderId"); + recievedLcn.setAffectedVirtualLinks(new ArrayList<>()); + recievedLcn.getAffectedVirtualLinks().add(affectedVirtualLink); + + + AffectedVirtualStorage affectedStorage = new AffectedVirtualStorage(); + affectedStorage.setChangeType(ChangeType.MODIFIED); + affectedStorage.setId("storageId"); + affectedStorage.setVirtualStorageDescId("storageVnfdId"); + affectedStorage.setResource(new ResourceHandle()); + affectedStorage.getResource().setVimId(VIM_ID); + affectedStorage.getResource().setResourceId("storageProviderId"); + recievedLcn.setAffectedVirtualStorages(new ArrayList<>()); + recievedLcn.getAffectedVirtualStorages().add(affectedStorage); + + ReportedAffectedConnectionPoints affectedConnectionPoints = new ReportedAffectedConnectionPoints(); + ReportedAffectedCp affectedCp = new ReportedAffectedCp(); + affectedCp.setChangeType(ChangeType.MODIFIED); + affectedCp.setCpdId("cpVnfdId"); + affectedCp.setIpAddress("1.2.3.4"); + affectedCp.setMacAddress("myMac"); + affectedCp.setName("myPortName"); + affectedCp.setCpId("cpId"); + + // affectedCp.setEcpdId("ecpdId"); + affectedCp.setNetworkProviderId("networkProviderId"); + affectedCp.setProviderId("portProviderId"); + affectedCp.setServerProviderId("serverProviderId"); + affectedCp.setTenantId("tenantId"); + affectedConnectionPoints.getPost().add(affectedCp); + + + OperationResult operationResult = new OperationResult(); + operationResult.operationResult = affectedConnectionPoints; + JsonElement additionalData = new Gson().toJsonTree(operationResult); + instantiationOperation.setAdditionalData(additionalData); + //when + vfcNotificationSender.processNotification(recievedLcn, healOperation, affectedConnectionPoints, VIM_ID); + //verify + assertEquals(1, sentLcnToVfc.getAllValues().size()); + + assertEquals(1, sentLcnToVfc.getValue().getAffectedVl().size()); + org.onap.vnfmdriver.model.AffectedVirtualLink actualAffectedVl = sentLcnToVfc.getValue().getAffectedVl().get(0); + assertEquals(org.onap.vnfmdriver.model.VnfNotificationType.MODIFIED, actualAffectedVl.getChangeType()); + assertEquals("vlVnfdId", actualAffectedVl.getVldid()); + assertEquals("myVnfId_vlId", actualAffectedVl.getVlInstanceId()); + assertEquals("networkProviderId", actualAffectedVl.getNetworkResource().getResourceId()); + assertEquals(AffectedVirtualLinkType.NETWORK, actualAffectedVl.getNetworkResource().getResourceType()); + + assertEquals(1, sentLcnToVfc.getValue().getAffectedVnfc().size()); + org.onap.vnfmdriver.model.AffectedVnfc actualAffectdVnfc = sentLcnToVfc.getValue().getAffectedVnfc().get(0); + assertEquals("myVduId", actualAffectdVnfc.getVduId()); + assertEquals(VIM_ID, actualAffectdVnfc.getVimid()); + assertEquals("myVmName", actualAffectdVnfc.getVmname()); + assertEquals("serverProviderId", actualAffectdVnfc.getVmid()); + assertEquals(org.onap.vnfmdriver.model.VnfNotificationType.MODIFIED, actualAffectdVnfc.getChangeType()); + assertEquals("myVnfId_myVnfcId", actualAffectdVnfc.getVnfcInstanceId()); + + assertEquals(1, sentLcnToVfc.getValue().getAffectedCp().size()); + AffectedCp actualAffectedCp = sentLcnToVfc.getValue().getAffectedCp().get(0); + assertEquals("cpVnfdId", actualAffectedCp.getCpdid()); + assertEquals("myVnfId_cpId", actualAffectedCp.getCpinstanceid()); + assertEquals(null, actualAffectedCp.getOwnerId()); + assertEquals(null, actualAffectedCp.getOwnerType()); + assertEquals("networkProviderId", actualAffectedCp.getVirtualLinkInstanceId()); + assertEquals("1.2.3.4", actualAffectedCp.getPortResource().getIpAddress()); + assertEquals("myMac", actualAffectedCp.getPortResource().getMacAddress()); + assertEquals("tenantId", actualAffectedCp.getPortResource().getTenant()); + assertEquals(VIM_ID, actualAffectedCp.getPortResource().getVimid()); + assertEquals("serverProviderId", actualAffectedCp.getPortResource().getInstId()); + assertEquals("portProviderId", actualAffectedCp.getPortResource().getResourceid()); + assertEquals("myPortName", actualAffectedCp.getPortResource().getResourceName()); + assertEquals(VnfCpNotificationType.CHANGED, actualAffectedCp.getChangeType()); + + assertNull(sentLcnToVfc.getValue().getAffectedVirtualStorage()); + assertEquals(JOB_ID, sentLcnToVfc.getValue().getJobId()); + assertEquals(org.onap.vnfmdriver.model.OperationType.HEAL, sentLcnToVfc.getValue().getOperation()); + assertEquals(VnfLcmNotificationStatus.RESULT, sentLcnToVfc.getValue().getStatus()); + assertEquals(VNF_ID, sentLcnToVfc.getValue().getVnfInstanceId()); + } + + /** + * test end notification success scenario for scale-out + * - LCN is sent to VF-C + */ + @Test + public void testFinishLcnForScaleout() { + //given + recievedLcn.setOperation(OperationType.SCALE); + recievedLcn.setStatus(OperationStatus.FINISHED); + recievedLcn.setLifecycleOperationOccurrenceId(scaleOperation.getId()); + ScaleVnfRequest request = new ScaleVnfRequest(); + request.setAdditionalParams(new JsonParser().parse("{ \"jobId\" : \"" + JOB_ID + "\" }")); + request.setType(ScaleDirection.OUT); + scaleOperation.setOperationParams(request); + scaleOperation.setOperationType(OperationType.SCALE); + AffectedVnfc affectedVnfc = new AffectedVnfc(); + affectedVnfc.setChangeType(ChangeType.ADDED); + affectedVnfc.setId("myVnfcId"); + affectedVnfc.setVduId("myVduId"); + affectedVnfc.setStorageResourceIds(Lists.newArrayList("storageId1")); + affectedVnfc.setComputeResource(new ResourceHandle()); + affectedVnfc.getComputeResource().setResourceId("serverProviderId"); + affectedVnfc.getComputeResource().setVimId(VIM_ID); + affectedVnfc.getComputeResource().setAdditionalData(new JsonParser().parse("{ \"name\" : \"myVmName\" } ")); + recievedLcn.setAffectedVnfcs(new ArrayList<>()); + recievedLcn.getAffectedVnfcs().add(affectedVnfc); + + AffectedVirtualLink affectedVirtualLink = new AffectedVirtualLink(); + affectedVirtualLink.setChangeType(ChangeType.ADDED); + affectedVirtualLink.setId("vlId"); + affectedVirtualLink.setVirtualLinkDescId("vlVnfdId"); + affectedVirtualLink.setResource(new ResourceHandle()); + affectedVirtualLink.getResource().setVimId(VIM_ID); + affectedVirtualLink.getResource().setResourceId("networkProviderId"); + recievedLcn.setAffectedVirtualLinks(new ArrayList<>()); + recievedLcn.getAffectedVirtualLinks().add(affectedVirtualLink); + + + AffectedVirtualStorage affectedStorage = new AffectedVirtualStorage(); + affectedStorage.setChangeType(ChangeType.ADDED); + affectedStorage.setId("storageId"); + affectedStorage.setVirtualStorageDescId("storageVnfdId"); + affectedStorage.setResource(new ResourceHandle()); + affectedStorage.getResource().setVimId(VIM_ID); + affectedStorage.getResource().setResourceId("storageProviderId"); + recievedLcn.setAffectedVirtualStorages(new ArrayList<>()); + recievedLcn.getAffectedVirtualStorages().add(affectedStorage); + + ReportedAffectedConnectionPoints affectedConnectionPoints = new ReportedAffectedConnectionPoints(); + ReportedAffectedCp affectedCp = new ReportedAffectedCp(); + affectedCp.setChangeType(ChangeType.ADDED); + affectedCp.setCpdId("cpVnfdId"); + affectedCp.setIpAddress("1.2.3.4"); + affectedCp.setMacAddress("myMac"); + affectedCp.setName("myPortName"); + affectedCp.setCpId("cpId"); + + // affectedCp.setEcpdId("ecpdId"); + affectedCp.setNetworkProviderId("networkProviderId"); + affectedCp.setProviderId("portProviderId"); + affectedCp.setServerProviderId("serverProviderId"); + affectedCp.setTenantId("tenantId"); + affectedConnectionPoints.getPost().add(affectedCp); + + OperationResult operationResult = new OperationResult(); + operationResult.operationResult = affectedConnectionPoints; + JsonElement additionalData = new Gson().toJsonTree(operationResult); + scaleOperation.setAdditionalData(additionalData); + //when + vfcNotificationSender.processNotification(recievedLcn, scaleOperation, affectedConnectionPoints, VIM_ID); + //verify + assertEquals(1, sentLcnToVfc.getAllValues().size()); + + assertEquals(1, sentLcnToVfc.getValue().getAffectedVl().size()); + org.onap.vnfmdriver.model.AffectedVirtualLink actualAffectedVl = sentLcnToVfc.getValue().getAffectedVl().get(0); + assertEquals(org.onap.vnfmdriver.model.VnfNotificationType.ADDED, actualAffectedVl.getChangeType()); + assertEquals("vlVnfdId", actualAffectedVl.getVldid()); + assertEquals("myVnfId_vlId", actualAffectedVl.getVlInstanceId()); + assertEquals("networkProviderId", actualAffectedVl.getNetworkResource().getResourceId()); + assertEquals(AffectedVirtualLinkType.NETWORK, actualAffectedVl.getNetworkResource().getResourceType()); + + assertEquals(1, sentLcnToVfc.getValue().getAffectedVnfc().size()); + org.onap.vnfmdriver.model.AffectedVnfc actualAffectdVnfc = sentLcnToVfc.getValue().getAffectedVnfc().get(0); + assertEquals("myVduId", actualAffectdVnfc.getVduId()); + assertEquals(VIM_ID, actualAffectdVnfc.getVimid()); + assertEquals("myVmName", actualAffectdVnfc.getVmname()); + assertEquals("serverProviderId", actualAffectdVnfc.getVmid()); + assertEquals(org.onap.vnfmdriver.model.VnfNotificationType.ADDED, actualAffectdVnfc.getChangeType()); + assertEquals("myVnfId_myVnfcId", actualAffectdVnfc.getVnfcInstanceId()); + + assertEquals(1, sentLcnToVfc.getValue().getAffectedCp().size()); + AffectedCp actualAffectedCp = sentLcnToVfc.getValue().getAffectedCp().get(0); + assertEquals("cpVnfdId", actualAffectedCp.getCpdid()); + assertEquals("myVnfId_cpId", actualAffectedCp.getCpinstanceid()); + assertEquals(null, actualAffectedCp.getOwnerId()); + assertEquals(null, actualAffectedCp.getOwnerType()); + assertEquals("networkProviderId", actualAffectedCp.getVirtualLinkInstanceId()); + assertEquals("1.2.3.4", actualAffectedCp.getPortResource().getIpAddress()); + assertEquals("myMac", actualAffectedCp.getPortResource().getMacAddress()); + assertEquals("tenantId", actualAffectedCp.getPortResource().getTenant()); + assertEquals(VIM_ID, actualAffectedCp.getPortResource().getVimid()); + assertEquals("serverProviderId", actualAffectedCp.getPortResource().getInstId()); + assertEquals("portProviderId", actualAffectedCp.getPortResource().getResourceid()); + assertEquals("myPortName", actualAffectedCp.getPortResource().getResourceName()); + assertEquals(VnfCpNotificationType.ADDED, actualAffectedCp.getChangeType()); + + assertNull(sentLcnToVfc.getValue().getAffectedVirtualStorage()); + assertEquals(JOB_ID, sentLcnToVfc.getValue().getJobId()); + assertEquals(org.onap.vnfmdriver.model.OperationType.SCALEOUT, sentLcnToVfc.getValue().getOperation()); + assertEquals(VnfLcmNotificationStatus.RESULT, sentLcnToVfc.getValue().getStatus()); + assertEquals(VNF_ID, sentLcnToVfc.getValue().getVnfInstanceId()); + } + + /** + * test end notification success scenario for scale-out + * - LCN is sent to VF-C + */ + @Test + public void testFinishLcnForScaleIn() { + //given + recievedLcn.setOperation(OperationType.SCALE); + recievedLcn.setStatus(OperationStatus.FINISHED); + recievedLcn.setLifecycleOperationOccurrenceId(scaleOperation.getId()); + ScaleVnfRequest request = new ScaleVnfRequest(); + request.setAdditionalParams(new JsonParser().parse("{ \"jobId\" : \"" + JOB_ID + "\" }")); + request.setType(ScaleDirection.IN); + scaleOperation.setOperationParams(request); + AffectedVnfc affectedVnfc = new AffectedVnfc(); + affectedVnfc.setChangeType(ChangeType.REMOVED); + affectedVnfc.setId("myVnfcId"); + affectedVnfc.setVduId("myVduId"); + affectedVnfc.setStorageResourceIds(Lists.newArrayList("storageId1")); + affectedVnfc.setComputeResource(new ResourceHandle()); + affectedVnfc.getComputeResource().setResourceId("serverProviderId"); + affectedVnfc.getComputeResource().setVimId(VIM_ID); + affectedVnfc.getComputeResource().setAdditionalData(new JsonParser().parse("{ \"name\" : \"myVmName\" } ")); + recievedLcn.setAffectedVnfcs(new ArrayList<>()); + recievedLcn.getAffectedVnfcs().add(affectedVnfc); + + AffectedVirtualLink affectedVirtualLink = new AffectedVirtualLink(); + affectedVirtualLink.setChangeType(ChangeType.REMOVED); + affectedVirtualLink.setId("vlId"); + affectedVirtualLink.setVirtualLinkDescId("vlVnfdId"); + affectedVirtualLink.setResource(new ResourceHandle()); + affectedVirtualLink.getResource().setVimId(VIM_ID); + affectedVirtualLink.getResource().setResourceId("networkProviderId"); + recievedLcn.setAffectedVirtualLinks(new ArrayList<>()); + recievedLcn.getAffectedVirtualLinks().add(affectedVirtualLink); + + + AffectedVirtualStorage affectedStorage = new AffectedVirtualStorage(); + affectedStorage.setChangeType(ChangeType.REMOVED); + affectedStorage.setId("storageId"); + affectedStorage.setVirtualStorageDescId("storageVnfdId"); + affectedStorage.setResource(new ResourceHandle()); + affectedStorage.getResource().setVimId(VIM_ID); + affectedStorage.getResource().setResourceId("storageProviderId"); + recievedLcn.setAffectedVirtualStorages(new ArrayList<>()); + recievedLcn.getAffectedVirtualStorages().add(affectedStorage); + + ReportedAffectedConnectionPoints affectedConnectionPoints = new ReportedAffectedConnectionPoints(); + ReportedAffectedCp affectedCp = new ReportedAffectedCp(); + affectedCp.setChangeType(ChangeType.REMOVED); + affectedCp.setCpdId("cpVnfdId"); + affectedCp.setIpAddress("1.2.3.4"); + affectedCp.setMacAddress("myMac"); + affectedCp.setName("myPortName"); + affectedCp.setCpId("cpId"); + + // affectedCp.setEcpdId("ecpdId"); + affectedCp.setNetworkProviderId("networkProviderId"); + affectedCp.setProviderId("portProviderId"); + affectedCp.setServerProviderId("serverProviderId"); + affectedCp.setTenantId("tenantId"); + affectedConnectionPoints.getPost().add(affectedCp); + + + OperationResult operationResult = new OperationResult(); + operationResult.operationResult = affectedConnectionPoints; + JsonElement additionalData = new Gson().toJsonTree(operationResult); + scaleOperation.setAdditionalData(additionalData); + scaleOperation.setOperationType(OperationType.SCALE); + //when + vfcNotificationSender.processNotification(recievedLcn, scaleOperation, affectedConnectionPoints, VIM_ID); + //verify + assertEquals(1, sentLcnToVfc.getAllValues().size()); + + assertEquals(1, sentLcnToVfc.getValue().getAffectedVl().size()); + org.onap.vnfmdriver.model.AffectedVirtualLink actualAffectedVl = sentLcnToVfc.getValue().getAffectedVl().get(0); + assertEquals(org.onap.vnfmdriver.model.VnfNotificationType.REMOVED, actualAffectedVl.getChangeType()); + assertEquals("vlVnfdId", actualAffectedVl.getVldid()); + assertEquals("myVnfId_vlId", actualAffectedVl.getVlInstanceId()); + assertEquals("networkProviderId", actualAffectedVl.getNetworkResource().getResourceId()); + assertEquals(AffectedVirtualLinkType.NETWORK, actualAffectedVl.getNetworkResource().getResourceType()); + + assertEquals(1, sentLcnToVfc.getValue().getAffectedVnfc().size()); + org.onap.vnfmdriver.model.AffectedVnfc actualAffectdVnfc = sentLcnToVfc.getValue().getAffectedVnfc().get(0); + assertEquals("myVduId", actualAffectdVnfc.getVduId()); + assertEquals(VIM_ID, actualAffectdVnfc.getVimid()); + assertEquals("myVmName", actualAffectdVnfc.getVmname()); + assertEquals("serverProviderId", actualAffectdVnfc.getVmid()); + assertEquals(org.onap.vnfmdriver.model.VnfNotificationType.REMOVED, actualAffectdVnfc.getChangeType()); + assertEquals("myVnfId_myVnfcId", actualAffectdVnfc.getVnfcInstanceId()); + + assertEquals(1, sentLcnToVfc.getValue().getAffectedCp().size()); + AffectedCp actualAffectedCp = sentLcnToVfc.getValue().getAffectedCp().get(0); + assertEquals("cpVnfdId", actualAffectedCp.getCpdid()); + assertEquals("myVnfId_cpId", actualAffectedCp.getCpinstanceid()); + assertEquals(null, actualAffectedCp.getOwnerId()); + assertEquals(null, actualAffectedCp.getOwnerType()); + assertEquals("networkProviderId", actualAffectedCp.getVirtualLinkInstanceId()); + assertEquals("1.2.3.4", actualAffectedCp.getPortResource().getIpAddress()); + assertEquals("myMac", actualAffectedCp.getPortResource().getMacAddress()); + assertEquals("tenantId", actualAffectedCp.getPortResource().getTenant()); + assertEquals(VIM_ID, actualAffectedCp.getPortResource().getVimid()); + assertEquals("serverProviderId", actualAffectedCp.getPortResource().getInstId()); + assertEquals("portProviderId", actualAffectedCp.getPortResource().getResourceid()); + assertEquals("myPortName", actualAffectedCp.getPortResource().getResourceName()); + assertEquals(VnfCpNotificationType.REMOVED, actualAffectedCp.getChangeType()); + + assertNull(sentLcnToVfc.getValue().getAffectedVirtualStorage()); + assertEquals(JOB_ID, sentLcnToVfc.getValue().getJobId()); + assertEquals(org.onap.vnfmdriver.model.OperationType.SCALEIN, sentLcnToVfc.getValue().getOperation()); + assertEquals(VnfLcmNotificationStatus.RESULT, sentLcnToVfc.getValue().getStatus()); + assertEquals(VNF_ID, sentLcnToVfc.getValue().getVnfInstanceId()); + } + + /** + * Unable to send notification to VF-C results in error + */ + @Test + public void testUnableToSendNotificationToVfc() throws Exception { + ApiException expectedException = new ApiException(); + doThrow(expectedException).when(nsLcmApi).vNFLCMNotification(any(), any(), any()); + recievedLcn.setStatus(OperationStatus.STARTED); + recievedLcn.setOperation(OperationType.INSTANTIATE); + //when + try { + vfcNotificationSender.processNotification(recievedLcn, instantiationOperation, affectedCp, VIM_ID); + //verify + fail(); + } catch (Exception e) { + verify(logger).error("Unable to send LCN to VF-C", expectedException); + assertEquals(expectedException, e.getCause()); + } + } + + class OperationResult { + ReportedAffectedConnectionPoints operationResult; + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/vfc/TestVfcPackageProvider.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/vfc/TestVfcPackageProvider.java new file mode 100644 index 00000000..62ffdb4f --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/vfc/TestVfcPackageProvider.java @@ -0,0 +1,156 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc; + +import org.apache.http.HttpHeaders; +import org.apache.http.client.methods.HttpGet; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.IpMappingProvider; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; +import org.onap.vfccatalog.ApiException; +import org.onap.vfccatalog.model.VnfPkgDetailInfo; +import org.onap.vfccatalog.model.VnfPkgInfo; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.fail; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.springframework.test.util.ReflectionTestUtils.setField; + +public class TestVfcPackageProvider extends TestBase { + + private static final String CSAR_ID = "csarId"; + private static final String CBAM_VNFD_ID = "CBAM_VNFD_ID"; + @Mock + private IpMappingProvider ipMappingProvider; + + @Mock + private VfcPackageProvider vfcPackageProvider; + + + @Before + public void initMocks() throws Exception { + setField(VfcPackageProvider.class, "logger", logger); + vfcPackageProvider = new VfcPackageProvider(vfcRestApiProvider, ipMappingProvider); + } + + /** + * query CBAM VNFD identifier from VF-C catalog + */ + @Test + public void testGetCbamVnfd() throws Exception { + VnfPkgDetailInfo vnfPackageDetails = new VnfPkgDetailInfo(); + vnfPackageDetails.setCsarId(CSAR_ID); + vnfPackageDetails.setPackageInfo(new VnfPkgInfo()); + vnfPackageDetails.getPackageInfo().setVnfdModel("{ \"metadata\" : { \"resourceVendorModelNumber\" : \"" + CBAM_VNFD_ID + "\" }}"); + vnfPackageDetails.getPackageInfo().setDownloadUrl("http://127.0.0.1/a.csar"); + when(vfcCatalogApi.queryVnfPackage(CSAR_ID)).thenReturn(vnfPackageDetails); + //when + String cbamVnfdId = vfcPackageProvider.getCbamVnfdId(CSAR_ID); + //verify + assertEquals(CBAM_VNFD_ID, cbamVnfdId); + } + + /** + * download ONAP VNFD from VF-C catalog + */ + @Test + public void testDownload() throws Exception { + VnfPkgDetailInfo vnfPackageDetails = new VnfPkgDetailInfo(); + vnfPackageDetails.setCsarId(CSAR_ID); + vnfPackageDetails.setPackageInfo(new VnfPkgInfo()); + vnfPackageDetails.getPackageInfo().setVnfdModel("{ \"metadata\" : { \"resourceVendorModelNumber\" : \"" + CBAM_VNFD_ID + "\" }}"); + vnfPackageDetails.getPackageInfo().setDownloadUrl("http://127.0.0.1/a.csar"); + when(vfcCatalogApi.queryVnfPackage(CSAR_ID)).thenReturn(vnfPackageDetails); + byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar"); + when(ipMappingProvider.mapPrivateIpToPublicIp("127.0.0.1")).thenReturn("1.2.3.4"); + when(entity.getContent()).thenReturn(new ByteArrayInputStream(onapPackageContent)); + //when + byte[] actualContent = vfcPackageProvider.getPackage(CSAR_ID); + //verify + Assert.assertArrayEquals(onapPackageContent, actualContent); + assertEquals(HttpGet.class, request.getValue().getClass()); + assertEquals("http://1.2.3.4/a.csar", request.getValue().getURI().toString()); + assertEquals("application/octet-stream", request.getValue().getFirstHeader(HttpHeaders.ACCEPT).getValue()); + } + + /** + * failure to query package from VF-C is propagated + */ + @Test + public void unableToGetCbamVnfdFromCatalog() throws Exception { + ApiException expectedException = new ApiException(); + when(vfcCatalogApi.queryVnfPackage(CSAR_ID)).thenThrow(expectedException); + //when + try { + vfcPackageProvider.getCbamVnfdId(CSAR_ID); + fail(); + } catch (Exception e) { + verify(logger).error("Unable to query VNF package with csarId from VF-C", expectedException); + assertEquals(expectedException, e.getCause()); + } + } + + /** + * failure to download package from VF-C is propagated + */ + @Test + public void unableToDownloadFromCatalog() throws Exception { + VnfPkgDetailInfo vnfPackageDetails = new VnfPkgDetailInfo(); + vnfPackageDetails.setCsarId(CSAR_ID); + vnfPackageDetails.setPackageInfo(new VnfPkgInfo()); + vnfPackageDetails.getPackageInfo().setVnfdModel("{ \"metadata\" : { \"resourceVendorModelNumber\" : \"" + CBAM_VNFD_ID + "\" }}"); + vnfPackageDetails.getPackageInfo().setDownloadUrl("http://127.0.0.1/a.csar"); + when(vfcCatalogApi.queryVnfPackage(CSAR_ID)).thenReturn(vnfPackageDetails); + byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar"); + when(ipMappingProvider.mapPrivateIpToPublicIp("127.0.0.1")).thenReturn("1.2.3.4"); + IOException expectedException = new IOException(); + when(httpClient.execute(Mockito.any())).thenThrow(expectedException); + //when + try { + vfcPackageProvider.getPackage(CSAR_ID); + fail(); + } catch (Exception e) { + verify(logger).error("Unable to download package from http://1.2.3.4/a.csar from VF-C", expectedException); + assertEquals(expectedException, e.getCause()); + } + } + + /** + * failure to query package for download package from VF-C is propagated + */ + @Test + public void unableToQueryPackageForDownloadFromCatalog() throws Exception { + ApiException expectedException = new ApiException(); + when(vfcCatalogApi.queryVnfPackage(CSAR_ID)).thenThrow(expectedException); + //when + try { + vfcPackageProvider.getPackage(CSAR_ID); + fail(); + } catch (Exception e) { + verify(logger).error("Unable to query VNF package with csarId from VF-C", expectedException); + assertEquals(expectedException, e.getCause()); + } + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/vfc/TestVfcRestApiProvider.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/vfc/TestVfcRestApiProvider.java new file mode 100644 index 00000000..a527b568 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/vfc/TestVfcRestApiProvider.java @@ -0,0 +1,64 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc; + +import org.junit.Before; +import org.junit.Test; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; +import org.onap.vfccatalog.api.VnfpackageApi; +import org.onap.vnfmdriver.api.NslcmApi; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertNull; +import static org.mockito.Mockito.when; + +public class TestVfcRestApiProvider extends TestBase { + private VfcRestApiProvider vfcRestApiProvider; + + @Before + public void init() { + vfcRestApiProvider = new VfcRestApiProvider(msbApiProvider); + } + + /** + * test VF-C NSLCM API retrieval + */ + @Test + public void testNsLcmApi() throws Exception { + when(msbApiProvider.getMicroServiceUrl(VfcRestApiProvider.NSLCM_API_SERVICE_NAME, VfcRestApiProvider.NSLCM_API_VERION)).thenReturn("http://1.2.3.4:1234/nslcm/v1/lead"); + //when + NslcmApi nsLcmApi = vfcRestApiProvider.getNsLcmApi(); + //verify + assertEquals("http://1.2.3.4:1234/lead", nsLcmApi.getApiClient().getBasePath()); + assertNull(nsLcmApi.getApiClient().getSslCaCert()); + assertEquals(0, nsLcmApi.getApiClient().getAuthentications().size()); + } + + /** + * test VF-C catalog API retrieval + */ + @Test + public void testNsCatalogApi() throws Exception { + when(msbApiProvider.getMicroServiceUrl(VfcRestApiProvider.NSCATALOG_SERVICE_NAME, VfcRestApiProvider.NSCATALOG_API_VERSION)).thenReturn("http://1.2.3.4:1234/lead"); + //when + VnfpackageApi nsCatalogApi = vfcRestApiProvider.getOnapCatalogApi(); + //verify + assertEquals("http://1.2.3.4:1234/lead", nsCatalogApi.getApiClient().getBasePath()); + assertNull(nsCatalogApi.getApiClient().getSslCaCert()); + assertEquals(0, nsCatalogApi.getApiClient().getAuthentications().size()); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestCbamVnfPackageBuilder.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestCbamVnfPackageBuilder.java new file mode 100644 index 00000000..3b80eca0 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestCbamVnfPackageBuilder.java @@ -0,0 +1,54 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer; + +import org.junit.Test; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; + +import static org.mockito.Mockito.when; + + +public class TestCbamVnfPackageBuilder extends TestBase { + private CbamVnfPackageBuilder cbamVnfPackageBuilder = new CbamVnfPackageBuilder(); + + /** + * + */ + @Test + public void testEmpty() throws Exception { + byte[] zipContent = TestUtil.loadFile("unittests/packageconverter/cbam.package.zip"); + when(systemFunctions.loadFile("cbam.pre.collectConnectionPoints.js")).thenCallRealMethod(); + when(systemFunctions.loadFile("cbam.post.collectConnectionPoints.js")).thenCallRealMethod(); + when(systemFunctions.loadFile("cbam.collectConnectionPoints.js")).thenCallRealMethod(); + + //when + byte[] modifiedContent = cbamVnfPackageBuilder.toModifiedCbamVnfPackage(zipContent, "vnfdloc/a.yaml", "modifiedContent"); + //verify + assertFileInZip(modifiedContent, "keep/me", "kuku\n".getBytes()); + assertFileInZip(modifiedContent, "TOSCA-Metadata/TOSCA.meta", ("TOSCA-Meta-File-Version: 1.0\n" + + "CSAR-Version: 1.1\n" + + "Created-By: Nokia\n" + + "Entry-Definitions: vnfdloc/a.yaml\n").getBytes()); + assertFileInZip(modifiedContent, "javascript/cbam.pre.collectConnectionPoints.js", TestUtil.loadFile("cbam.pre.collectConnectionPoints.js")); + assertFileInZip(modifiedContent, "javascript/cbam.post.collectConnectionPoints.js", TestUtil.loadFile("cbam.post.collectConnectionPoints.js")); + assertFileInZip(modifiedContent, "javascript/cbam.collectConnectionPoints.js", TestUtil.loadFile("cbam.collectConnectionPoints.js")); + + + } + + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestCbamVnfdBuilder.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestCbamVnfdBuilder.java new file mode 100644 index 00000000..5840ef4e --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestCbamVnfdBuilder.java @@ -0,0 +1,47 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer; + +import org.junit.Test; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; + +import static junit.framework.TestCase.assertEquals; + + +public class TestCbamVnfdBuilder extends TestBase { + private CbamVnfdBuilder packageTransformer = new CbamVnfdBuilder(); + + /** + * test package conversion on the most mininal VNFD possible + */ + @Test + public void testEmpty() throws Exception { + String out = packageTransformer.build(new String(TestUtil.loadFile("unittests/packageconverter/cbam.minimal.original.vnfd.yaml"))); + String expected = new String(TestUtil.loadFile("unittests/packageconverter/cbam.minimal.modified.vnfd.yaml")); + assertEquals(expected, out); + } + + /** + * test package conversion on the most full VNFD possible + */ + @Test + public void testFull() throws Exception { + String out = packageTransformer.build(new String(TestUtil.loadFile("unittests/packageconverter/cbam.full.original.vnfd.yaml"))); + String expected = new String(TestUtil.loadFile("unittests/packageconverter/cbam.full.modified.vnfd.yaml")); + assertEquals(expected, out); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapVnfPackageBuilder.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapVnfPackageBuilder.java new file mode 100644 index 00000000..9c9f7958 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapVnfPackageBuilder.java @@ -0,0 +1,74 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer; + +import org.junit.Test; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static junit.framework.TestCase.assertEquals; +import static org.mockito.Mockito.when; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip; + + +public class TestOnapVnfPackageBuilder extends TestBase { + + /** + * The the main reads from standard in and writes to standard out + */ + @Test + public void testInputStreams() throws Exception { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + PrintStream actualOut = new PrintStream(bos, true); + when(systemFunctions.out()).thenReturn(actualOut); + when(systemFunctions.in()).thenReturn(new ByteArrayInputStream(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip"))); + when(systemFunctions.loadFile("cbam.pre.collectConnectionPoints.js")).thenCallRealMethod(); + when(systemFunctions.loadFile("cbam.collectConnectionPoints.js")).thenCallRealMethod(); + when(systemFunctions.loadFile("cbam.post.collectConnectionPoints.js")).thenCallRealMethod(); + when(systemFunctions.loadFile("TOSCA.meta")).thenCallRealMethod(); + when(systemFunctions.loadFile("MainServiceTemplate.meta")).thenCallRealMethod(); + + + String cbamVnfd = new String(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip.vnfd")); + String expectedOnapVnfd = new OnapVnfdBuilder().toOnapVnfd(cbamVnfd); + + //when + OnapVnfPackageBuilder.main(null); + //verify + assertFileInZip(bos.toByteArray(), "TOSCA-Metadata/TOSCA.meta", TestUtil.loadFile("TOSCA.meta")); + assertFileInZip(bos.toByteArray(), "Definitions/MainServiceTemplate.yaml", expectedOnapVnfd.getBytes()); + assertFileInZip(bos.toByteArray(), "MainServiceTemplate.yaml", expectedOnapVnfd.getBytes()); + assertFileInZip(bos.toByteArray(), "MainServiceTemplate.meta", TestUtil.loadFile("MainServiceTemplate.meta")); + ByteArrayOutputStream actualModifiedCbamVnfPackage = getFileInZip(new ByteArrayInputStream(bos.toByteArray()), "Artifacts/Deployment/OTHER/cbam.package.zip"); + byte[] expectedModifiedCbamPackage = new CbamVnfPackageBuilder().toModifiedCbamVnfPackage(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip"), "vnfdloc/a.yaml", new CbamVnfdBuilder().build(cbamVnfd)); + assertItenticalZips(expectedModifiedCbamPackage, actualModifiedCbamVnfPackage.toByteArray()); + } + + + /** + * Prevents moving the class (customer documentation) must be updated + */ + @Test + public void testPreventMove() { + assertEquals("org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.OnapVnfPackageBuilder", OnapVnfPackageBuilder.class.getCanonicalName()); + } + + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapVnfdBuilder.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapVnfdBuilder.java new file mode 100644 index 00000000..1edf5eca --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/packagetransformer/TestOnapVnfdBuilder.java @@ -0,0 +1,60 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer; + +import org.junit.Test; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; + +import java.util.NoSuchElementException; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.fail; + + +public class TestOnapVnfdBuilder extends TestBase { + private OnapVnfdBuilder packageTransformer = new OnapVnfdBuilder(); + + /** + * Test empty VNFD conversion + */ + @Test + public void testEmpty() { + assertEquals(new String(TestUtil.loadFile("unittests/packageconverter/empty.vnfd.onap.yaml")), packageTransformer.toOnapVnfd(new String(TestUtil.loadFile("unittests/packageconverter/empty.vnfd.cbam.yaml")))); + } + + /** + * Test all Tosca nodes conversions for successful scenario + */ + @Test + public void testNodes() { + assertEquals(new String(TestUtil.loadFile("unittests/packageconverter/nodes.vnfd.onap.yaml")), packageTransformer.toOnapVnfd(new String(TestUtil.loadFile("unittests/packageconverter/nodes.vnfd.cbam.yaml")))); + } + + /** + * if a node refers to a non existing node it results in a failure + */ + @Test + public void testInconsitentVnfd() { + try { + packageTransformer.toOnapVnfd(new String(TestUtil.loadFile("unittests/packageconverter/nodes.vnfd.inconsistent.cbam.yaml"))); + fail(); + } catch (NoSuchElementException e) { + assertEquals("The VNFD does not have a node called myComputeMissing but required by an other node", e.getMessage()); + } + } + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/TestConverterApi.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/TestConverterApi.java new file mode 100644 index 00000000..9ac38914 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/TestConverterApi.java @@ -0,0 +1,114 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.restapi; + +import com.google.common.collect.Lists; +import junit.framework.TestCase; +import org.apache.http.entity.ContentType; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.CbamVnfPackageBuilder; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.CbamVnfdBuilder; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.OnapVnfdBuilder; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.mock.web.DelegatingServletOutputStream; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.Part; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.Arrays; + +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip; +import static org.springframework.test.util.ReflectionTestUtils.setField; + + +public class TestConverterApi extends TestBase { + + + @InjectMocks + private ConverterApi converterApi; + @Mock + private HttpServletRequest httpRequest; + + @Before + public void initMocks() throws Exception { + setField(ConverterApi.class, "logger", logger); + } + + /** + */ + @Test + public void test() throws Exception { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + PrintStream actualOut = new PrintStream(bos, true); + when(systemFunctions.out()).thenReturn(actualOut); + when(systemFunctions.loadFile("cbam.pre.collectConnectionPoints.js")).thenCallRealMethod(); + when(systemFunctions.loadFile("cbam.collectConnectionPoints.js")).thenCallRealMethod(); + when(systemFunctions.loadFile("cbam.post.collectConnectionPoints.js")).thenCallRealMethod(); + when(systemFunctions.loadFile("TOSCA.meta")).thenCallRealMethod(); + when(systemFunctions.loadFile("MainServiceTemplate.meta")).thenCallRealMethod(); + when(httpResponse.getOutputStream()).thenReturn(new DelegatingServletOutputStream(actualOut)); + Part part = Mockito.mock(Part.class); + when(part.getInputStream()).thenReturn(new ByteArrayInputStream(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip"))); + when(httpRequest.getParts()).thenReturn(Lists.newArrayList(part)); + //when + converterApi.convert(httpResponse, httpRequest); + //verify + verifyVnfPackageWritterToOutputStream(bos); + verify(httpResponse).addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_OCTET_STREAM.getMimeType()); + verify(httpResponse).setStatus(HttpStatus.OK.value()); + verify(httpResponse).addHeader(HttpHeaders.CONTENT_LENGTH, Integer.toString(bos.toByteArray().length)); + verify(httpResponse).addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + "core.csar" + "\""); + } + + private void verifyVnfPackageWritterToOutputStream(ByteArrayOutputStream bos) throws Exception { + String cbamVnfd = new String(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip.vnfd")); + String expectedOnapVnfd = new OnapVnfdBuilder().toOnapVnfd(cbamVnfd); + assertFileInZip(bos.toByteArray(), "TOSCA-Metadata/TOSCA.meta", TestUtil.loadFile("TOSCA.meta")); + assertFileInZip(bos.toByteArray(), "Definitions/MainServiceTemplate.yaml", expectedOnapVnfd.getBytes()); + assertFileInZip(bos.toByteArray(), "MainServiceTemplate.yaml", expectedOnapVnfd.getBytes()); + assertFileInZip(bos.toByteArray(), "MainServiceTemplate.meta", TestUtil.loadFile("MainServiceTemplate.meta")); + ByteArrayOutputStream actualModifiedCbamVnfPackage = getFileInZip(new ByteArrayInputStream(bos.toByteArray()), "Artifacts/Deployment/OTHER/cbam.package.zip"); + byte[] expectedModifiedCbamPackage = new CbamVnfPackageBuilder().toModifiedCbamVnfPackage(TestUtil.loadFile("unittests/packageconverter/cbam.package.zip"), "vnfdloc/a.yaml", new CbamVnfdBuilder().build(cbamVnfd)); + assertItenticalZips(expectedModifiedCbamPackage, actualModifiedCbamVnfPackage.toByteArray()); + } + + @Test + public void testDownloaderPage() throws Exception { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + PrintStream actualOut = new PrintStream(bos, true); + when(httpResponse.getOutputStream()).thenReturn(new DelegatingServletOutputStream(actualOut)); + when(systemFunctions.loadFile("upload.html")).thenCallRealMethod(); + //when + converterApi.getUploadPageForConvertingVnfd(httpResponse); + //verify + TestCase.assertTrue(Arrays.equals(TestUtil.loadFile("upload.html"), bos.toByteArray())); + verify(httpResponse).addHeader(HttpHeaders.CONTENT_LENGTH, Integer.toString(bos.toByteArray().length)); + + } + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/TestLcmApi.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/TestLcmApi.java new file mode 100644 index 00000000..1fdeb323 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/TestLcmApi.java @@ -0,0 +1,130 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.restapi; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; +import org.onap.vnfmdriver.model.VnfHealRequest; +import org.onap.vnfmdriver.model.VnfInstantiateRequest; +import org.onap.vnfmdriver.model.VnfScaleRequest; +import org.onap.vnfmdriver.model.VnfTerminateRequest; + +import static org.apache.http.HttpStatus.SC_CREATED; +import static org.mockito.Mockito.verify; +import static org.springframework.test.util.ReflectionTestUtils.setField; + + +public class TestLcmApi extends TestBase { + + @Mock + private LifecycleManager lifecycleManager; + @Mock + private JobManager jobManager; + @InjectMocks + private LcmApi lcmApi; + + @Before + public void initMocks() throws Exception { + setField(LcmApi.class, "logger", logger); + } + + /** + * test instantiation handled by LCM + */ + @Test + public void testInstantiation() { + VnfInstantiateRequest req = new VnfInstantiateRequest(); + //when + lcmApi.instantiateVnf(req, VNFM_ID, httpResponse); + //verify + verify(lifecycleManager).instantiate(VNFM_ID, req, httpResponse); + verify(httpResponse).setStatus(SC_CREATED); + verify(logger).info("REST: Instantiate VNF"); + } + + /** + * test heal handled by LCM + */ + @Test + public void testHeal() { + VnfHealRequest req = new VnfHealRequest(); + //when + lcmApi.healVnf(req, VNFM_ID, VNF_ID, httpResponse); + //verify + verify(lifecycleManager).healVnf(VNFM_ID, VNF_ID, req, httpResponse); + verify(logger).info("REST: Heal VNF"); + } + + /** + * test query handled by LCM + */ + @Test + public void testQuery() { + //when + lcmApi.queryVnf(VNFM_ID, VNF_ID, httpResponse); + //verify + verify(lifecycleManager).queryVnf(VNFM_ID, VNF_ID); + verify(logger).info("REST: Query VNF"); + + } + + /** + * test scale handled by LCM + */ + @Test + public void testScale() { + VnfScaleRequest req = new VnfScaleRequest(); + //when + lcmApi.scaleVnf(req, VNFM_ID, VNF_ID, httpResponse); + //verify + verify(lifecycleManager).scaleVnf(VNFM_ID, VNF_ID, req, httpResponse); + verify(logger).info("REST: Scale VNF"); + + } + + /** + * test terminate handled by LCM + */ + @Test + public void testTerminate() { + VnfTerminateRequest req = new VnfTerminateRequest(); + //when + lcmApi.terminateVnf(req, VNFM_ID, VNF_ID, httpResponse); + //verify + verify(lifecycleManager).terminateVnf(VNFM_ID, VNF_ID, req, httpResponse); + verify(logger).info("REST: Terminate VNF"); + + } + + /** + * test job query handled by job manager + */ + @Test + public void testJob() { + //when + lcmApi.getJob(VNFM_ID, JOB_ID, httpResponse); + //verify + verify(jobManager).getJob(VNFM_ID, JOB_ID); + verify(logger).debug("REST: Query job"); + + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/TestLcnApi.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/TestLcnApi.java new file mode 100644 index 00000000..fc6b97b4 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/TestLcnApi.java @@ -0,0 +1,65 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.restapi; + +import com.nokia.cbam.lcm.v32.model.VnfLifecycleChangeNotification; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.LifecycleChangeNotificationManager; + +import static org.mockito.Mockito.verify; +import static org.springframework.test.util.ReflectionTestUtils.setField; + + +public class TestLcnApi extends TestBase { + + @Mock + private VnfLifecycleChangeNotification lcn; + @Mock + private LifecycleChangeNotificationManager lcnManager; + @InjectMocks + private LcnApi lcnApi; + + @Before + public void initMocks() throws Exception { + setField(LcnApi.class, "logger", logger); + } + + /** + * test REST "ping" from CBAM to driver + */ + @Test + public void testPing() { + lcnApi.testLcnConnectivity(null); + //verify no exception is thrown + } + + /** + * test LCN is handled by LCN manager + */ + @Test + public void testHandleLcn() { + //when + lcnApi.handleLcn(lcn, httpResponse); + //verify + verify(lcnManager).handleLcn(lcn); + verify(logger).info("REST: handle LCN"); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/TestSwaggerApi.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/TestSwaggerApi.java new file mode 100644 index 00000000..638f7237 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/TestSwaggerApi.java @@ -0,0 +1,62 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.restapi; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mockito; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; + +import javax.servlet.ServletOutputStream; + +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.springframework.test.util.ReflectionTestUtils.setField; + + +public class TestSwaggerApi extends TestBase { + + @InjectMocks + private SwaggerApi swaggerApi; + + + @Before + public void initMocks() throws Exception { + setField(SwaggerApi.class, "logger", logger); + } + + /** + * test swagger definition get + */ + @Test + public void testSwaggerRetrieval() throws Exception { + byte[] bytes = new byte[]{1, 2}; + when(selfRegistrationManager.getSwaggerApiDefinition()).thenReturn(bytes); + ServletOutputStream os = Mockito.mock(ServletOutputStream.class); + when(httpResponse.getOutputStream()).thenReturn(os); + //when + swaggerApi.getSwaggerApiDefinition(httpResponse); + //verify + verify(httpResponse).addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); + verify(httpResponse).addHeader(HttpHeaders.CONTENT_LENGTH, Integer.toString(bytes.length)); + verify(os).write(bytes); + verify(logger).info("REST: get swagger definition"); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/TestSwaggerDefinitionConsistency.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/TestSwaggerDefinitionConsistency.java new file mode 100644 index 00000000..eeb79be8 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi/TestSwaggerDefinitionConsistency.java @@ -0,0 +1,82 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.restapi; + +import com.google.common.collect.HashMultimap; +import com.google.common.collect.Sets; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import junit.framework.TestCase; +import org.junit.Test; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import java.lang.reflect.Method; +import java.util.HashSet; +import java.util.NoSuchElementException; +import java.util.Set; + +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil.loadFile; + +public class TestSwaggerDefinitionConsistency extends TestBase { + + public static final HashSet<Class<?>> CLASSES = Sets.newHashSet(LcmApi.class, LcnApi.class, SwaggerApi.class, ConverterApi.class); + + @Test + public void test() throws Exception { + JsonObject root = new JsonParser().parse(new String(loadFile("self.swagger.json"))).getAsJsonObject(); + String basePath = root.get("basePath").getAsString(); + HashMultimap<String, RequestMethod> expectedPaths = HashMultimap.create(); + for (String pathName : child(root, "paths").keySet()) { + JsonObject path = child(child(root, "paths"), pathName); + for (String method : path.keySet()) { + locate(basePath + pathName); + expectedPaths.put(basePath + pathName, RequestMethod.valueOf(method.toUpperCase())); + } + } + + for (Class<?> clazz : CLASSES) { + RequestMapping currentBasePath = clazz.getAnnotation(RequestMapping.class); + for (Method method : clazz.getMethods()) { + RequestMapping methodMapping = method.getAnnotation(RequestMapping.class); + if (methodMapping != null) { + String fPath = currentBasePath.value()[0] + methodMapping.value()[0]; + RequestMethod restMethod = methodMapping.method()[0]; + Set<RequestMethod> currentMethods = expectedPaths.get(fPath); + if (!currentMethods.contains(restMethod)) { + TestCase.fail("Not documented REST API" + fPath + " " + restMethod + " current " + currentMethods); + } + } + } + } + } + + private void locate(String path) { + for (Class<?> clazz : CLASSES) { + RequestMapping basePath = clazz.getAnnotation(RequestMapping.class); + for (Method method : clazz.getMethods()) { + RequestMapping methodMapping = method.getAnnotation(RequestMapping.class); + if (methodMapping != null && path.equals(basePath.value()[0] + methodMapping.value()[0])) { + return; + } + } + } + throw new NoSuchElementException(path); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/spring/TestConditions.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/spring/TestConditions.java new file mode 100644 index 00000000..f716cad6 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/spring/TestConditions.java @@ -0,0 +1,66 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring; + +import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.env.Environment; + +import static org.mockito.Mockito.when; + +public class TestConditions { + + @Mock + private ConditionContext conditionContext; + @Mock + private Environment environment; + + private String[] activeProfiles = new String[]{"a", "b"}; + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + when(conditionContext.getEnvironment()).thenReturn(environment); + when(environment.getActiveProfiles()).thenReturn(activeProfiles); + } + + /** + * if direct integration is not specified VF-C based integration is used + */ + @Test + public void testVfcBased() throws Exception { + //verify + TestCase.assertTrue(new Conditions.UseForVfc().matches(conditionContext, null)); + TestCase.assertFalse(new Conditions.UseForDirect().matches(conditionContext, null)); + } + + /** + * if direct integration is not specified VF-C based integration is used + */ + @Test + public void testDirectBased() throws Exception { + activeProfiles[1] = "direct"; + //verify + TestCase.assertFalse(new Conditions.UseForVfc().matches(conditionContext, null)); + TestCase.assertTrue(new Conditions.UseForDirect().matches(conditionContext, null)); + } + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/spring/TestRealConfig.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/spring/TestRealConfig.java new file mode 100644 index 00000000..1c92a75a --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/spring/TestRealConfig.java @@ -0,0 +1,43 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring; + +import com.nokia.cbam.lcm.v32.model.VnfIdentifierCreationNotification; +import com.nokia.cbam.lcm.v32.model.VnfIdentifierDeletionNotification; +import com.nokia.cbam.lcm.v32.model.VnfInfoAttributeValueChangeNotification; +import com.nokia.cbam.lcm.v32.model.VnfLifecycleChangeNotification; +import org.junit.Test; +import org.springframework.boot.autoconfigure.web.HttpMessageConverters; +import org.springframework.http.MediaType; + +public class TestRealConfig { + + /** + * test that the converter can transform the inherited classes + */ + @Test + public void test() throws Exception { + HttpMessageConverters converters = new RealConfig().customConverters(); + //verify + converters.getConverters().get(0).canRead(VnfIdentifierCreationNotification.class, MediaType.APPLICATION_JSON); + converters.getConverters().get(0).canRead(VnfIdentifierDeletionNotification.class, MediaType.APPLICATION_JSON); + converters.getConverters().get(0).canRead(VnfInfoAttributeValueChangeNotification.class, MediaType.APPLICATION_JSON); + converters.getConverters().get(0).canRead(VnfLifecycleChangeNotification.class, MediaType.APPLICATION_JSON); + converters.getConverters().get(0).canRead(String.class, MediaType.APPLICATION_JSON); + } + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/spring/TestServletInitializer.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/spring/TestServletInitializer.java new file mode 100644 index 00000000..75c68959 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/spring/TestServletInitializer.java @@ -0,0 +1,40 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring; + +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.NokiaSvnfmApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; + +import static org.mockito.Mockito.verify; + +public class TestServletInitializer { + + /** + * test that the Nokia application is added to the Spring context + */ + @Test + public void testSpringBootApplicationInit() throws Exception { + SpringApplicationBuilder springApplicationBuilder = Mockito.mock(SpringApplicationBuilder.class); + //when + new ServletInitializer().configure(springApplicationBuilder); + //verify + verify(springApplicationBuilder).sources(NokiaSvnfmApplication.class); + } + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/TestCbamUtils.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/TestCbamUtils.java new file mode 100644 index 00000000..7f544616 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/TestCbamUtils.java @@ -0,0 +1,136 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util; + +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; +import org.junit.Test; +import org.mockito.Mockito; +import org.slf4j.Logger; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.fail; +import static org.mockito.Mockito.verify; + +public class TestCbamUtils { + + /** + * test child of json object + */ + @Test + public void testChild() throws Exception { + JsonObject parent = new JsonObject(); + JsonObject child = new JsonObject(); + parent.add("x", child); + assertEquals(child, CbamUtils.child(parent, "x")); + } + + /** + * if child is not a json object error is propagated + */ + @Test + public void testNonJsonObjectChild() throws Exception { + JsonObject parent = new JsonObject(); + JsonPrimitive child = new JsonPrimitive("y"); + parent.add("x", child); + try { + CbamUtils.child(parent, "x"); + fail(); + } catch (RuntimeException e) { + assertEquals("Not a JSON Object: \"y\"", e.getMessage()); + } + } + + /** + * if no child is present error is propagated + */ + @Test + public void testMissingChild() throws Exception { + JsonObject parent = new JsonObject(); + try { + CbamUtils.child(parent, "z"); + fail(); + } catch (RuntimeException e) { + assertEquals("Missing child z", e.getMessage()); + } + } + + /** + * test child of json object + */ + @Test + public void testChildElement() throws Exception { + JsonObject parent = new JsonObject(); + JsonPrimitive child = new JsonPrimitive("y"); + parent.add("x", child); + assertEquals(child, CbamUtils.childElement(parent, "x")); + } + + /** + * if no child is present error is propagated + */ + @Test + public void testMissingChildElement() throws Exception { + JsonObject parent = new JsonObject(); + try { + CbamUtils.childElement(parent, "z"); + fail(); + } catch (RuntimeException e) { + assertEquals("Missing child z", e.getMessage()); + } + } + + + /** + * test fatal failure handling + */ + @Test + public void testFatalFailure() throws Exception { + Exception expectedException = new Exception(); + Logger logger = Mockito.mock(Logger.class); + try { + CbamUtils.fatalFailure(logger, "msg", expectedException); + fail(); + } catch (RuntimeException e) { + assertEquals("msg", e.getMessage()); + assertEquals(expectedException, e.getCause()); + verify(logger).error("msg", expectedException); + } + } + + /** + * test fatal failure handling with no wrapped exception + */ + @Test + public void testFatalFailureWithNoException() throws Exception { + Logger logger = Mockito.mock(Logger.class); + try { + CbamUtils.fatalFailure(logger, "msg"); + fail(); + } catch (RuntimeException e) { + assertEquals("msg", e.getMessage()); + verify(logger).error("msg"); + } + } + + @Test + public void useStaticWay() { + TestUtil.coveragePrivateConstructorForClassesWithStaticMethodsOnly(CbamUtils.class); + } + + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/TestStoreLoader.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/TestStoreLoader.java new file mode 100644 index 00000000..2dc33f3b --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/TestStoreLoader.java @@ -0,0 +1,346 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util; + +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.net.URL; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.cert.CertificateException; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import static com.google.common.collect.Sets.newHashSet; +import static org.junit.Assert.*; + +public class TestStoreLoader { + + private static final String PASSWORD = "password"; + private static final String SUN = "SUN"; + private static final String JKS = "JKS"; + private static final String TEST_JKS = "test.jks"; + private static final String PEM_WITHOUT_BEGIN = "" + + "INVALID\n" + + "-----END RSA PRIVATE KEY-----"; + private static final String PEM_WITHOUT_END = "-----BEGIN RSA PRIVATE KEY-----\n" + + "INVALID\n" + + ""; + private static final String PEMS_WITHOUT_PRIVATE_KEY = "-----BEGIN CERTIFICATE-----\n" + + "MIICUTCCAfugAwIBAgIBADANBgkqhkiG9w0BAQQFADBXMQswCQYDVQQGEwJDTjEL\n" + + "MAkGA1UECBMCUE4xCzAJBgNVBAcTAkNOMQswCQYDVQQKEwJPTjELMAkGA1UECxMC\n" + + "VU4xFDASBgNVBAMTC0hlcm9uZyBZYW5nMB4XDTA1MDcxNTIxMTk0N1oXDTA1MDgx\n" + + "NDIxMTk0N1owVzELMAkGA1UEBhMCQ04xCzAJBgNVBAgTAlBOMQswCQYDVQQHEwJD\n" + + "TjELMAkGA1UEChMCT04xCzAJBgNVBAsTAlVOMRQwEgYDVQQDEwtIZXJvbmcgWWFu\n" + + "ZzBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQCp5hnG7ogBhtlynpOS21cBewKE/B7j\n" + + "V14qeyslnr26xZUsSVko36ZnhiaO/zbMOoRcKK9vEcgMtcLFuQTWDl3RAgMBAAGj\n" + + "gbEwga4wHQYDVR0OBBYEFFXI70krXeQDxZgbaCQoR4jUDncEMH8GA1UdIwR4MHaA\n" + + "FFXI70krXeQDxZgbaCQoR4jUDncEoVukWTBXMQswCQYDVQQGEwJDTjELMAkGA1UE\n" + + "CBMCUE4xCzAJBgNVBAcTAkNOMQswCQYDVQQKEwJPTjELMAkGA1UECxMCVU4xFDAS\n" + + "BgNVBAMTC0hlcm9uZyBZYW5nggEAMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEE\n" + + "BQADQQA/ugzBrjjK9jcWnDVfGHlk3icNRq0oV7Ri32z/+HQX67aRfgZu7KWdI+Ju\n" + + "Wm7DCfrPNGVwFWUQOmsPue9rZBgO\n" + + "-----END CERTIFICATE-----\n" + + "-----BEGIN CERTIFICATE-----\n" + + "MIICUTCCAfugAwIBAgIBADANBgkqhkiG9w0BAQQFADBXMQswCQYDVQQGEwJDTjEL\n" + + "MAkGA1UECBMCUE4xCzAJBgNVBAcTAkNOMQswCQYDVQQKEwJPTjELMAkGA1UECxMC\n" + + "VU4xFDASBgNVBAMTC0hlcm9uZyBZYW5nMB4XDTA1MDcxNTIxMTk0N1oXDTA1MDgx\n" + + "NDIxMTk0N1owVzELMAkGA1UEBhMCQ04xCzAJBgNVBAgTAlBOMQswCQYDVQQHEwJD\n" + + "TjELMAkGA1UEChMCT04xCzAJBgNVBAsTAlVOMRQwEgYDVQQDEwtIZXJvbmcgWWFu\n" + + "ZzBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQCp5hnG7ogBhtlynpOS21cBewKE/B7j\n" + + "V14qeyslnr26xZUsSVko36ZnhiaO/zbMOoRcKK9vEcgMtcLFuQTWDl3RAgMBAAGj\n" + + "gbEwga4wHQYDVR0OBBYEFFXI70krXeQDxZgbaCQoR4jUDncEMH8GA1UdIwR4MHaA\n" + + "FFXI70krXeQDxZgbaCQoR4tUDncEoVukWTBXMQswCQYDVQQGEwJDTjELMAkGA1UE\n" + + "CBMCUE4xCzAJBgNVBAcTAkNOMQswCQYDVQQKEwJPTjELMAkGA1UECxMCVU4xFDAS\n" + + "BgNVBAMTC0hlcm9uZyBZYW5nggEAMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEE\n" + + "BQADQQA/ugzBrjjK9jcWnDVfGHlk3icNRq0oV7Ri32z/+HQX67aRfgZu7KWdI+Ju\n" + + "Wm7DCfrPNGVwFWUQOmsPue9rZBgO\n" + + "-----END CERTIFICATE-----"; + private static final String PEMS = "-----BEGIN CERTIFICATE-----\n" + + "MIICUTCCAfugAwIBAgIBADANBgkqhkiG9w0BAQQFADBXMQswCQYDVQQGEwJDTjEL\n" + + "MAkGA1UECBMCUE4xCzAJBgNVBAcTAkNOMQswCQYDVQQKEwJPTjELMAkGA1UECxMC\n" + + "VU4xFDASBgNVBAMTC0hlcm9uZyBZYW5nMB4XDTA1MDcxNTIxMTk0N1oXDTA1MDgx\n" + + "NDIxMTk0N1owVzELMAkGA1UEBhMCQ04xCzAJBgNVBAgTAlBOMQswCQYDVQQHEwJD\n" + + "TjELMAkGA1UEChMCT04xCzAJBgNVBAsTAlVOMRQwEgYDVQQDEwtIZXJvbmcgWWFu\n" + + "ZzBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQCp5hnG7ogBhtlynpOS21cBewKE/B7j\n" + + "V14qeyslnr26xZUsSVko36ZnhiaO/zbMOoRcKK9vEcgMtcLFuQTWDl3RAgMBAAGj\n" + + "gbEwga4wHQYDVR0OBBYEFFXI70krXeQDxZgbaCQoR4jUDncEMH8GA1UdIwR4MHaA\n" + + "FFXI70krXeQDxZgbaCQoR4jUDncEoVukWTBXMQswCQYDVQQGEwJDTjELMAkGA1UE\n" + + "CBMCUE4xCzAJBgNVBAcTAkNOMQswCQYDVQQKEwJPTjELMAkGA1UECxMCVU4xFDAS\n" + + "BgNVBAMTC0hlcm9uZyBZYW5nggEAMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEE\n" + + "BQADQQA/ugzBrjjK9jcWnDVfGHlk3icNRq0oV7Ri32z/+HQX67aRfgZu7KWdI+Ju\n" + + "Wm7DCfrPNGVwFWUQOmsPue9rZBgO\n" + + "-----END CERTIFICATE-----\n" + + "-----BEGIN CERTIFICATE-----\n" + + "MIICUTCCAfugAwIBAgIBADANBgkqhkiG9w0BAQQFADBXMQswCQYDVQQGEwJDTjEL\n" + + "MAkGA1UECBMCUE4xCzAJBgNVBAcTAkNOMQswCQYDVQQKEwJPTjELMAkGA1UECxMC\n" + + "VU4xFDASBgNVBAMTC0hlcm9uZyBZYW5nMB4XDTA1MDcxNTIxMTk0N1oXDTA1MDgx\n" + + "NDIxMTk0N1owVzELMAkGA1UEBhMCQ04xCzAJBgNVBAgTAlBOMQswCQYDVQQHEwJD\n" + + "TjELMAkGA1UEChMCT04xCzAJBgNVBAsTAlVOMRQwEgYDVQQDEwtIZXJvbmcgWWFu\n" + + "ZzBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQCp5hnG7ogBhtlynpOS21cBewKE/B7j\n" + + "V14qeyslnr26xZUsSVko36ZnhiaO/zbMOoRcKK9vEcgMtcLFuQTWDl3RAgMBAAGj\n" + + "gbEwga4wHQYDVR0OBBYEFFXI70krXeQDxZgbaCQoR4jUDncEMH8GA1UdIwR4MHaA\n" + + "FFXI70krXeQDxZgbaCQoR4tUDncEoVukWTBXMQswCQYDVQQGEwJDTjELMAkGA1UE\n" + + "CBMCUE4xCzAJBgNVBAcTAkNOMQswCQYDVQQKEwJPTjELMAkGA1UECxMCVU4xFDAS\n" + + "BgNVBAMTC0hlcm9uZyBZYW5nggEAMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEE\n" + + "BQADQQA/ugzBrjjK9jcWnDVfGHlk3icNRq0oV7Ri32z/+HQX67aRfgZu7KWdI+Ju\n" + + "Wm7DCfrPNGVwFWUQOmsPue9rZBgO\n" + + "-----END CERTIFICATE-----\n" + + "-----BEGIN RSA PRIVATE KEY-----\n" + + "MIIEuwIBADANBgkqhkiG9w0BAQEFAASCBKUwggShAgEAAoIBAF53wUbKmDHtvfOb8u1HPqEBFNNF\n" + + "csnOMjIcSEhAwIQMbgrOuQ+vH/YgXuuDJaURS85H8P4UTt6lYOJn+SFnXvS82E7LHJpVrWwQzbh2\n" + + "QKh13/akPe90DlNTUGEYO7rHaPLqTlld0jkLFSytwqfwqn9yrYpM1ncUOpCciK5j8t8MzO71LJoJ\n" + + "g24CFxpjIS0tBrJvKzrRNcxWSRDLmu2kNmtsh7yyJouE6XoizVmBmNVltHhFaDMmqjugMQA2CZfL\n" + + "rxiR1ep8TH8IBvPqysqZI1RIpB/e0engP4/1KLrOt+6gGS0JEDh1kG2fJObl+N4n3sCOtgaz5Uz8\n" + + "8jpwbmZ3Se8CAwEAAQKCAQAdOsSs2MbavAsIM3qo/GBehO0iqdxooMpbQvECmjZ3JTlvUqNkPPWQ\n" + + "vFdiW8PsHTvtackhdLsqnNUreKxXL5rr8vqi9qm0/0mXpGNi7gP3m/FeaVdYnfpIwgCe6lag5k6M\n" + + "yv7PG/6N8+XrWyBdwlOe96bGohvB4Jp2YFjSTM67QONQ8CdmfqokqJ8/3RyrpDvGN3iX3yzBqXGO\n" + + "jPkoJQv3I4lsYdR0nl4obHHnMSeWCQCYvJoZ7ZOliu/Dd0ksItlodG6s8r/ujkSa8VIhe0fnXTf0\n" + + "i7lqa55CAByGN4MOR0bAkJwIB7nZzQKurBPcTAYJFFvAc5hgMnWT0XW83TehAoGBALVPGnznScUw\n" + + "O50OXKI5yhxGf/XDT8g28L8Oc4bctRzI+8YfIFfLJ57uDGuojO/BpqtYmXmgORru0jYR8idEkZrx\n" + + "gf62czOiJrCWTkBCEMtrNfFHQJQCQrjfbHofp7ODnEHbHFm7zdlbfNnEBBaKXxd2rVv4UTEhgftv\n" + + "wsHcimbXAoGBAIViWrHWElMeQT0datqlThE/u51mcK4VlV7iRWXVa1/gAP85ZAu44VvvDlkpYVkF\n" + + "zSRR+lHSOzsubDMN45OBQW6UA3RPg4TCvrTOmhQUeF5XPuSdcD0R2At6pdaLwAKnOtILg13Ha6ym\n" + + "Igjv8glodvem3hWLmpHIhNBiaXtf8wqpAoGADH5a8OhvKOtd8EChGXyp9LDW+HRw9vbyN/gi9dQX\n" + + "ltgyoUBb1jDllgoJSRHgRFUvyvbb/ImR5c03JwqtiQ8siWTC9G5WGeS+jcSNt9fVmG7W1L14MbrG\n" + + "Jj8fFns/7xrOlasnlPdgA+5N+CONtI/sZY2D/KZr0drhPhZBcWJlFxkCgYAn+4SOPEo/6hjKNhA6\n" + + "vER7fSxDEVsDg+rDh3YgAWpvUdlaqBxqOyAqi600YugQZGHK2lv7vNYOdmrunuIx7BPuDqY+bjtR\n" + + "R4Mc9bVQAZbXSLXMl7j2RWwKfNhLSJbk9LX4EoVtTgLjvOUE4tAdq9fFgpqdwLwzqPTO9kECP4++\n" + + "CQKBgH6tO/xcNxG/uXUideluAn3H2KeyyznZMJ7oCvzf26/XpTAMI243OoeftiKVMgxuZ7hjwqfn\n" + + "/VHXABc4i5gchr9RzSb1hZ/IqFzq2YGmbppg5Ok2cgwalDoDBi21bRf8aDRweL62mO+7aPnCQZ58\n" + + "j5W72PB8BAr6xg0Oro25O4os\n" + + "-----END RSA PRIVATE KEY-----"; + private static final String PEM = "-----BEGIN CERTIFICATE-----\n" + + "MIICUTCCAfugAwIBAgIBADANBgkqhkiG9w0BAQQFADBXMQswCQYDVQQGEwJDTjEL\n" + + "MAkGA1UECBMCUE4xCzAJBgNVBAcTAkNOMQswCQYDVQQKEwJPTjELMAkGA1UECxMC\n" + + "VU4xFDASBgNVBAMTC0hlcm9uZyBZYW5nMB4XDTA1MDcxNTIxMTk0N1oXDTA1MDgx\n" + + "NDIxMTk0N1owVzELMAkGA1UEBhMCQ04xCzAJBgNVBAgTAlBOMQswCQYDVQQHEwJD\n" + + "TjELMAkGA1UEChMCT04xCzAJBgNVBAsTAlVOMRQwEgYDVQQDEwtIZXJvbmcgWWFu\n" + + "ZzBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQCp5hnG7ogBhtlynpOS21cBewKE/B7j\n" + + "V14qeyslnr26xZUsSVko36ZnhiaO/zbMOoRcKK9vEcgMtcLFuQTWDl3RAgMBAAGj\n" + + "gbEwga4wHQYDVR0OBBYEFFXI70krXeQDxZgbaCQoR4jUDncEMH8GA1UdIwR4MHaA\n" + + "FFXI70krXeQDxZgbaCQoR4jUDncEoVukWTBXMQswCQYDVQQGEwJDTjELMAkGA1UE\n" + + "CBMCUE4xCzAJBgNVBAcTAkNOMQswCQYDVQQKEwJPTjELMAkGA1UECxMCVU4xFDAS\n" + + "BgNVBAMTC0hlcm9uZyBZYW5nggEAMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEE\n" + + "BQADQQA/ugzBrjjK9jcWnDVfGHlk3icNRq0oV7Ri32z/+HQX67aRfgZu7KWdI+Ju\n" + + "Wm7DCfrPNGVwFWUQOmsPue9rZBgO\n" + + "-----END CERTIFICATE-----\n" + + "-----BEGIN RSA PRIVATE KEY-----\n" + + "MIIEuwIBADANBgkqhkiG9w0BAQEFAASCBKUwggShAgEAAoIBAF53wUbKmDHtvfOb8u1HPqEBFNNF\n" + + "csnOMjIcSEhAwIQMbgrOuQ+vH/YgXuuDJaURS85H8P4UTt6lYOJn+SFnXvS82E7LHJpVrWwQzbh2\n" + + "QKh13/akPe90DlNTUGEYO7rHaPLqTlld0jkLFSytwqfwqn9yrYpM1ncUOpCciK5j8t8MzO71LJoJ\n" + + "g24CFxpjIS0tBrJvKzrRNcxWSRDLmu2kNmtsh7yyJouE6XoizVmBmNVltHhFaDMmqjugMQA2CZfL\n" + + "rxiR1ep8TH8IBvPqysqZI1RIpB/e0engP4/1KLrOt+6gGS0JEDh1kG2fJObl+N4n3sCOtgaz5Uz8\n" + + "8jpwbmZ3Se8CAwEAAQKCAQAdOsSs2MbavAsIM3qo/GBehO0iqdxooMpbQvECmjZ3JTlvUqNkPPWQ\n" + + "vFdiW8PsHTvtackhdLsqnNUreKxXL5rr8vqi9qm0/0mXpGNi7gP3m/FeaVdYnfpIwgCe6lag5k6M\n" + + "yv7PG/6N8+XrWyBdwlOe96bGohvB4Jp2YFjSTM67QONQ8CdmfqokqJ8/3RyrpDvGN3iX3yzBqXGO\n" + + "jPkoJQv3I4lsYdR0nl4obHHnMSeWCQCYvJoZ7ZOliu/Dd0ksItlodG6s8r/ujkSa8VIhe0fnXTf0\n" + + "i7lqa55CAByGN4MOR0bAkJwIB7nZzQKurBPcTAYJFFvAc5hgMnWT0XW83TehAoGBALVPGnznScUw\n" + + "O50OXKI5yhxGf/XDT8g28L8Oc4bctRzI+8YfIFfLJ57uDGuojO/BpqtYmXmgORru0jYR8idEkZrx\n" + + "gf62czOiJrCWTkBCEMtrNfFHQJQCQrjfbHofp7ODnEHbHFm7zdlbfNnEBBaKXxd2rVv4UTEhgftv\n" + + "wsHcimbXAoGBAIViWrHWElMeQT0datqlThE/u51mcK4VlV7iRWXVa1/gAP85ZAu44VvvDlkpYVkF\n" + + "zSRR+lHSOzsubDMN45OBQW6UA3RPg4TCvrTOmhQUeF5XPuSdcD0R2At6pdaLwAKnOtILg13Ha6ym\n" + + "Igjv8glodvem3hWLmpHIhNBiaXtf8wqpAoGADH5a8OhvKOtd8EChGXyp9LDW+HRw9vbyN/gi9dQX\n" + + "ltgyoUBb1jDllgoJSRHgRFUvyvbb/ImR5c03JwqtiQ8siWTC9G5WGeS+jcSNt9fVmG7W1L14MbrG\n" + + "Jj8fFns/7xrOlasnlPdgA+5N+CONtI/sZY2D/KZr0drhPhZBcWJlFxkCgYAn+4SOPEo/6hjKNhA6\n" + + "vER7fSxDEVsDg+rDh3YgAWpvUdlaqBxqOyAqi600YugQZGHK2lv7vNYOdmrunuIx7BPuDqY+bjtR\n" + + "R4Mc9bVQAZbXSLXMl7j2RWwKfNhLSJbk9LX4EoVtTgLjvOUE4tAdq9fFgpqdwLwzqPTO9kECP4++\n" + + "CQKBgH6tO/xcNxG/uXUideluAn3H2KeyyznZMJ7oCvzf26/XpTAMI243OoeftiKVMgxuZ7hjwqfn\n" + + "/VHXABc4i5gchr9RzSb1hZ/IqFzq2YGmbppg5Ok2cgwalDoDBi21bRf8aDRweL62mO+7aPnCQZ58\n" + + "j5W72PB8BAr6xg0Oro25O4os\n" + + "-----END RSA PRIVATE KEY-----"; + private static final String PEM_WITHOUT_PRIVATE_KEY = + "-----BEGIN CERTIFICATE-----\n" + + "MIICUTCCAfugAwIBAgIBADANBgkqhkiG9w0BAQQFADBXMQswCQYDVQQGEwJDTjEL\n" + + "MAkGA1UECBMCUE4xCzAJBgNVBAcTAkNOMQswCQYDVQQKEwJPTjELMAkGA1UECxMC\n" + + "VU4xFDASBgNVBAMTC0hlcm9uZyBZYW5nMB4XDTA1MDcxNTIxMTk0N1oXDTA1MDgx\n" + + "NDIxMTk0N1owVzELMAkGA1UEBhMCQ04xCzAJBgNVBAgTAlBOMQswCQYDVQQHEwJD\n" + + "TjELMAkGA1UEChMCT04xCzAJBgNVBAsTAlVOMRQwEgYDVQQDEwtIZXJvbmcgWWFu\n" + + "ZzBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQCp5hnG7ogBhtlynpOS21cBewKE/B7j\n" + + "V14qeyslnr26xZUsSVko36ZnhiaO/zbMOoRcKK9vEcgMtcLFuQTWDl3RAgMBAAGj\n" + + "gbEwga4wHQYDVR0OBBYEFFXI70krXeQDxZgbaCQoR4jUDncEMH8GA1UdIwR4MHaA\n" + + "FFXI70krXeQDxZgbaCQoR4jUDncEoVukWTBXMQswCQYDVQQGEwJDTjELMAkGA1UE\n" + + "CBMCUE4xCzAJBgNVBAcTAkNOMQswCQYDVQQKEwJPTjELMAkGA1UECxMCVU4xFDAS\n" + + "BgNVBAMTC0hlcm9uZyBZYW5nggEAMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEE\n" + + "BQADQQA/ugzBrjjK9jcWnDVfGHlk3icNRq0oV7Ri32z/+HQX67aRfgZu7KWdI+Ju\n" + + "Wm7DCfrPNGVwFWUQOmsPue9rZBgO\n" + + "-----END CERTIFICATE-----"; + private static final String INVALID_CERT = + "-----BEGIN CERTIFICATE-----\n" + + "MIIC" + + "-----END CERTIFICATE-----"; + private static final String PEM_WITH_INVALID_PRIVATE_KEY = "-----BEGIN RSA PRIVATE KEY-----\n" + + "INVALID\n" + + "-----END RSA PRIVATE KEY-----"; + private static final String EMPTY_PEM = ""; + private static KeyStore testStore; + + @BeforeClass + public static void setUpBefore() throws NoSuchProviderException, KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException { + testStore = KeyStore.getInstance(JKS, SUN); + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + URL url = classLoader.getResource(TEST_JKS); + String jksFileName = url.getFile(); + File jks = new File(jksFileName); + FileInputStream fos = new FileInputStream(jks); + testStore.load(fos, PASSWORD.toCharArray()); + } + + @Test + public void useInStaticWay() { + TestUtil.coveragePrivateConstructorForClassesWithStaticMethodsOnly(StoreLoader.class); + } + + /** + * test certificate extraction from PEM + */ + @Test + public void testCertifate() { + assertEquals(new HashSet<>(), StoreLoader.getCertifacates("")); + String pem = "-----BEGIN CERTIFICATE-----\ncontent1\n-----END CERTIFICATE-----\n-----BEGIN CERTIFICATE-----\ncontent2\n-----END CERTIFICATE-----"; + Set<String> certifacates = StoreLoader.getCertifacates(pem); + assertEquals(newHashSet("-----BEGIN CERTIFICATE-----\ncontent1\n-----END CERTIFICATE-----", "-----BEGIN CERTIFICATE-----\ncontent2\n-----END CERTIFICATE-----"), certifacates); + } + + /** + * test keystore loading from PEM with single certificate & key + */ + @Test + public void loadStrore() throws Exception { + //when + KeyStore ks = StoreLoader.loadStore(PEM, PASSWORD, PASSWORD); + + //then + assertNotNull(ks.getCertificate(PASSWORD)); + assertTrue(Arrays.equals(testStore.getCertificate(PASSWORD).getPublicKey().getEncoded(), ks.getCertificate(PASSWORD).getPublicKey().getEncoded())); + } + + /** + * test keystore loading from PEM with multiple certifcates + */ + @Test + public void loadStroreWithCerts() throws Exception { + //when + KeyStore ks = StoreLoader.loadStore(PEMS, PASSWORD, PASSWORD); + + //then + assertNotNull(ks.getCertificate(PASSWORD)); + assertTrue(Arrays.equals(testStore.getCertificate(PASSWORD).getPublicKey().getEncoded(), ks.getCertificate(PASSWORD).getPublicKey().getEncoded())); + } + + /** + * test keystore loading from PEM without a private key + */ + @Test + public void loadStroreWithCertsWithoutPrivateKey() throws Exception { + //when + KeyStore ks = StoreLoader.loadStore(PEMS_WITHOUT_PRIVATE_KEY, PASSWORD, PASSWORD); + + //then + assertNotNull(ks); + String alias = PASSWORD + "0"; + assertNotNull(ks.getCertificate(alias)); + assertTrue(Arrays.equals(testStore.getCertificate(PASSWORD).getPublicKey().getEncoded(), ks.getCertificate(alias).getPublicKey().getEncoded())); + } + + /** + * non closed BEGIN CERTIFICATE pattern results in empty keystore + */ + @Test + public void loadStroreWithPemWithoutBegin() throws Exception { + //when + KeyStore ks = StoreLoader.loadStore(PEM_WITHOUT_BEGIN, PASSWORD, PASSWORD); + + //then + assertNull(ks.getCertificate(PASSWORD)); + } + + /** + * non closed END CERTIFICATE pattern results in empty keystore + */ + @Test + public void loadStroreWithPemWithoutEnd() throws Exception { + //when + KeyStore ks = StoreLoader.loadStore(PEM_WITHOUT_END, PASSWORD, PASSWORD); + + //then + assertNull(ks.getCertificate(PASSWORD)); + } + + /** + * empty PEM results in empty keystore + */ + @Test + public void loadStroreWithPemWithoutBeginAndEnd() throws Exception { + //when + KeyStore ks = StoreLoader.loadStore(EMPTY_PEM, PASSWORD, PASSWORD); + + //then + assertNull(ks.getCertificate(PASSWORD)); + } + + /** + * invalid private key results in an error + */ + @Test + public void loadStroreWithPemWithInvalidKey() throws Exception { + //when + try { + StoreLoader.loadStore(PEM_WITH_INVALID_PRIVATE_KEY, PASSWORD, PASSWORD); + fail(); + } catch (Exception e) { + assertEquals("Unable to load key", e.getMessage()); + + } + } + + /** + * invalid password results in error + */ + @Test + public void testInvlaidPassword() throws Exception { + //when + try { + StoreLoader.loadStore(PEM, null, null); + fail(); + } catch (Exception e) { + assertEquals("Unable to create keystore", e.getMessage()); + } + } + + /** + * invalid certificate results in error + */ + @Test + public void testInvlaidCertificate() throws Exception { + //when + try { + StoreLoader.loadStore(INVALID_CERT, null, null); + fail(); + } catch (Exception e) { + assertEquals("Unable to load certificates", e.getMessage()); + + } + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/TestSystemFunctions.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/TestSystemFunctions.java new file mode 100644 index 00000000..ec1ebd78 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/TestSystemFunctions.java @@ -0,0 +1,118 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util; + +import org.junit.Test; + +import java.util.Base64; +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.*; + +public class TestSystemFunctions { + + /** + * test sleep + */ + @Test + public void testSleep() throws Exception { + long start = System.currentTimeMillis(); + SystemFunctions.systemFunctions().sleep(123); + long end = System.currentTimeMillis(); + assertTrue(end - start >= 123); + } + + /** + * test interrupted sleep + */ + @Test + public void testInterruptedSleep() throws Exception { + long start = System.currentTimeMillis(); + Set<RuntimeException> exceptions = new HashSet<>(); + class Inter extends Thread { + @Override + public void run() { + try { + SystemFunctions.systemFunctions().sleep(10000); + } catch (RuntimeException e) { + exceptions.add(e); + } + } + } + Inter inter = new Inter(); + inter.start(); + //when + inter.interrupt(); + + //verify + while (exceptions.size() != 1) { + + } + } + + /** + * test current time + */ + @Test + public void testCurrentTime() { + long now = System.currentTimeMillis(); + long now2 = SystemFunctions.systemFunctions().currentTimeMillis(); + assertTrue(Math.abs(now2 - now) < 1000); + } + + /** + * test file load + */ + @Test + public void testFileLoad() { + byte[] bytes = SystemFunctions.systemFunctions().loadFile("unittests/empty.zip"); + assertEquals("UEsFBgAAAAAAAAAAAAAAAAAAAAAAAA==", Base64.getEncoder().encodeToString(bytes)); + } + + /** + * missing file results in error + */ + @Test + public void testMissingFileLoad() { + try { + SystemFunctions.systemFunctions().loadFile("unittests/missing"); + fail(); + } catch (Exception e) { + assertEquals("Unable to load unittests/missing", e.getMessage()); + } + } + + /** + * Test standard stream wrapping + */ + @Test + public void testStandardStreams() { + assertEquals(System.err, SystemFunctions.systemFunctions().err()); + assertEquals(System.out, SystemFunctions.systemFunctions().out()); + assertEquals(System.in, SystemFunctions.systemFunctions().in()); + } + + /** + * Test HTTP client wrapping + * (semi useless test) + */ + @Test + public void testHttp() { + assertNotNull(SystemFunctions.systemFunctions().getHttpClient()); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/TestUtil.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/TestUtil.java new file mode 100644 index 00000000..f5d9cc41 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/util/TestUtil.java @@ -0,0 +1,57 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util; + +import com.google.common.io.ByteStreams; + +import java.io.InputStream; +import java.lang.reflect.Constructor; + +public class TestUtil { + /** + * Due to sonar issue you have to create a private constructor for classes + * that only have static methods to prevent the creation of instances. This + * constructor can not be coveraged / tested since it is private. + * + * @param clazz + */ + public static void coveragePrivateConstructorForClassesWithStaticMethodsOnly(Class<?> clazz) { + for (Constructor<?> constructor : clazz.getDeclaredConstructors()) { + if (constructor.getParameterTypes().length == 0) { + constructor.setAccessible(true); + try { + constructor.newInstance(); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } else { + throw new IllegalStateException("This should only be used if the class has a singe private constructor"); + } + } + } + + public static byte[] loadFile(String url) { + try { + InputStream stream = SystemFunctions.class.getClassLoader().getResourceAsStream(url); + byte[] bytes = ByteStreams.toByteArray(stream); + return bytes; + } catch (Exception e) { + throw new RuntimeException("Unable to load " + url, e); + } + } + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestAdditionalParams.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestAdditionalParams.java new file mode 100644 index 00000000..e0e90000 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestAdditionalParams.java @@ -0,0 +1,61 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm; + +import com.google.common.collect.Maps; +import com.nokia.cbam.lcm.v32.model.NetworkAddress; +import org.junit.Test; +import pl.pojo.tester.internal.field.AbstractFieldValueChanger; +import pl.pojo.tester.internal.field.DefaultFieldValueChanger; +import pl.pojo.tester.internal.field.collections.map.AbstractMapFieldValueChanger; + +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static pl.pojo.tester.api.assertion.Assertions.assertPojoMethodsFor; + + +public class TestAdditionalParams { + + /** + * Test basic POJO behaviour + */ + @Test + public void test() { + + class MapValueChanger extends AbstractMapFieldValueChanger<Map<String, List<NetworkAddress>>> { + @Override + protected Map<String, List<NetworkAddress>> increaseValue(Map<String, List<NetworkAddress>> stringListMap, Class<?> aClass) { + if (stringListMap == null) { + return Maps.newHashMap(); + } else { + stringListMap.put(UUID.randomUUID().toString(), null); + return stringListMap; + } + } + + @Override + protected boolean canChange(Class<?> type) { + return type.getCanonicalName().contains("Map"); + } + } + + final AbstractFieldValueChanger valueChanger = new MapValueChanger().attachNext(DefaultFieldValueChanger.INSTANCE); + assertPojoMethodsFor(AdditionalParams.class).using(valueChanger).areWellImplemented(); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestBase.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestBase.java new file mode 100644 index 00000000..be3ea0eb --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestBase.java @@ -0,0 +1,174 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm; + +import com.google.common.io.ByteStreams; +import com.nokia.cbam.catalog.v1.api.DefaultApi; +import com.nokia.cbam.lcm.v32.api.OperationExecutionsApi; +import com.nokia.cbam.lcm.v32.api.VnfsApi; +import com.nokia.cbam.lcn.v32.api.SubscriptionsApi; +import org.apache.commons.lang3.ArrayUtils; +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.impl.client.CloseableHttpClient; +import org.assertj.core.util.Lists; +import org.junit.After; +import org.junit.Before; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.msb.sdk.httpclient.msb.MSBServiceClient; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.INotificationSender; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VnfmInfoProvider; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.MsbApiProvider; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc.VfcRestApiProvider; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions; +import org.onap.vfccatalog.api.VnfpackageApi; +import org.onap.vnfmdriver.api.NslcmApi; +import org.slf4j.Logger; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.env.Environment; +import org.springframework.test.util.ReflectionTestUtils; + +import javax.servlet.http.HttpServletResponse; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertTrue; +import static org.mockito.Mockito.when; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip; + +public class TestBase { + public static final String VNF_ID = "myVnfId"; + public static final String VNFM_ID = "myVnfmId"; + public static final String ONAP_CSAR_ID = "myOnapCsarId"; + public static final String VIM_ID = "myVimId"; + public static final String JOB_ID = "myJobId"; + public static final String CBAM_VNFD_ID = "cbamVnfdId"; + @Mock + protected CbamRestApiProvider cbamRestApiProvider; + @Mock + protected VfcRestApiProvider vfcRestApiProvider; + @Mock + protected MsbApiProvider msbApiProvider; + @Mock + protected VnfmInfoProvider vnfmInfoProvider; + @Mock + protected VnfsApi vnfApi; + @Mock + protected OperationExecutionsApi operationExecutionApi; + @Mock + protected SelfRegistrationManager selfRegistrationManager; + @Mock + protected Logger logger; + @Mock + protected SubscriptionsApi lcnApi; + @Mock + protected MSBServiceClient msbClient; + @Mock + protected DriverProperties driverProperties; + @Mock + protected NslcmApi nsLcmApi; + @Mock + protected INotificationSender notificationSender; + @Mock + protected SystemFunctions systemFunctions; + @Mock + protected VnfpackageApi vfcCatalogApi; + @Mock + protected DefaultApi cbamCatalogApi; + @Mock + protected CloseableHttpClient httpClient; + @Mock + protected CloseableHttpResponse response; + protected ArgumentCaptor<HttpUriRequest> request = ArgumentCaptor.forClass(HttpUriRequest.class); + @Mock + protected HttpEntity entity; + @Mock + protected HttpServletResponse httpResponse; + @Mock + protected Environment environment; + + @Before + public void genericSetup() throws Exception { + MockitoAnnotations.initMocks(this); + ReflectionTestUtils.setField(SystemFunctions.class, "INSTANCE", systemFunctions); + when(cbamRestApiProvider.getCbamLcmApi(VNFM_ID)).thenReturn(vnfApi); + when(cbamRestApiProvider.getCbamOperationExecutionApi(VNFM_ID)).thenReturn(operationExecutionApi); + when(cbamRestApiProvider.getCbamLcnApi(VNFM_ID)).thenReturn(lcnApi); + when(cbamRestApiProvider.getCbamCatalogApi(VNFM_ID)).thenReturn(cbamCatalogApi); + when(msbApiProvider.getMsbClient()).thenReturn(msbClient); + when(vfcRestApiProvider.getNsLcmApi()).thenReturn(nsLcmApi); + when(vfcRestApiProvider.getOnapCatalogApi()).thenReturn(vfcCatalogApi); + when(systemFunctions.getHttpClient()).thenReturn(httpClient); + when(httpClient.execute(request.capture())).thenReturn(response); + when(response.getEntity()).thenReturn(entity); + when(driverProperties.getVnfmId()).thenReturn(VNFM_ID); + when(systemFunctions.getHttpClient()).thenReturn(httpClient); + } + + @After + public void tearGeneric() { + ReflectionTestUtils.setField(SystemFunctions.class, "INSTANCE", null); + } + + protected void assertFileInZip(byte[] zip, String path, byte[] expectedContent) throws Exception { + assertTrue(Arrays.equals(expectedContent, getFileInZip(new ByteArrayInputStream(zip), path).toByteArray())); + } + + protected void assertItenticalZips(byte[] expected, byte[] actual) throws Exception { + assertEquals(build(expected), build(actual)); + } + + private Map<String, List<Byte>> build(byte[] zip) throws Exception { + Map<String, List<Byte>> files = new HashMap<>(); + ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zip)); + ZipEntry zipEntry; + while ((zipEntry = zipInputStream.getNextEntry()) != null) { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + ByteStreams.copy(zipInputStream, byteArrayOutputStream); + files.put(zipEntry.getName(), Lists.newArrayList(ArrayUtils.toObject(byteArrayOutputStream.toByteArray()))); + } + zipInputStream.close(); + return files; + } + + protected void setFieldWithPropertyAnnotation(Object obj, String key, String value) { + for (Field field : obj.getClass().getDeclaredFields()) { + for (Value fieldValue : field.getAnnotationsByType(Value.class)) { + if (fieldValue.value().equals(key)) { + try { + field.setAccessible(true); + field.set(obj, value); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + } + } + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestCbamCatalogManager.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestCbamCatalogManager.java new file mode 100644 index 00000000..f38758f0 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestCbamCatalogManager.java @@ -0,0 +1,261 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm; + +import com.nokia.cbam.catalog.v1.model.CatalogAdapterVnfpackage; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.IPackageProvider; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; + +import static junit.framework.TestCase.*; +import static org.junit.Assert.assertArrayEquals; +import static org.mockito.Mockito.*; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CatalogManager.getFileInZip; +import static org.springframework.test.util.ReflectionTestUtils.setField; + +public class TestCbamCatalogManager extends TestBase { + + private static final String CSAR_ID = "csarId"; + private static final String CBAM_VNFD_ID = "CBAM_VNFD_ID"; + private CatalogManager cbamCatalogManager; + @Mock + private IPackageProvider packageProvider; + + private List<CatalogAdapterVnfpackage> existingVnfPackages = new ArrayList<>(); + private ArgumentCaptor<File> uploadedFile = ArgumentCaptor.forClass(File.class); + + @Before + public void initMocks() throws Exception { + setField(CatalogManager.class, "logger", logger); + when(cbamCatalogApi.list()).thenReturn(existingVnfPackages); + cbamCatalogManager = new CatalogManager(cbamRestApiProvider, packageProvider); + } + + /** + * the package is transferred from source to CBAM catalog + */ + @Test + public void testPackageTransfer() throws Exception { + CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage(); + existingPackage.setVnfdId("unknownId"); + existingVnfPackages.add(existingPackage); + CatalogAdapterVnfpackage createdPackage = new CatalogAdapterVnfpackage(); + createdPackage.setVnfdId(CBAM_VNFD_ID); + when(cbamCatalogApi.create(uploadedFile.capture())).thenAnswer(new Answer<CatalogAdapterVnfpackage>() { + @Override + public CatalogAdapterVnfpackage answer(InvocationOnMock invocationOnMock) throws Throwable { + return createdPackage; + } + }); + byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar"); + when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent); + when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID); + //when + CatalogAdapterVnfpackage cbamPackage = cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID); + //verify + byte[] a2 = Files.readAllBytes(uploadedFile.getValue().toPath()); + assertArrayEquals(getFileInZip(new ByteArrayInputStream(onapPackageContent), "Artifacts/Deployment/OTHER/cbam.package.zip").toByteArray(), a2); + assertEquals(createdPackage, cbamPackage); + } + + /** + * the package is transfer fails, but the package has been uploaded (possibly by other thread / work flow) + * the transfer succeeds + */ + @Test + public void testPackageTransferConcurrency() throws Exception { + CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage(); + existingPackage.setVnfdId("unknownId"); + existingVnfPackages.add(existingPackage); + CatalogAdapterVnfpackage createdPackage = new CatalogAdapterVnfpackage(); + createdPackage.setVnfdId(CBAM_VNFD_ID); + byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar"); + when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent); + when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID); + RuntimeException can_not_upload_package = new RuntimeException("Can not upload package"); + when(cbamCatalogApi.create(uploadedFile.capture())).thenAnswer(new Answer<CatalogAdapterVnfpackage>() { + @Override + public CatalogAdapterVnfpackage answer(InvocationOnMock invocationOnMock) throws Throwable { + //this is done by an other thread + existingVnfPackages.add(createdPackage); + when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenReturn(createdPackage); + throw can_not_upload_package; + } + }); + //when + CatalogAdapterVnfpackage cbamPackage = cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID); + //verify + //the correct portion of the package is extracted and uploaded to CBAM + byte[] expectedContentToUpload = getFileInZip(new ByteArrayInputStream(onapPackageContent), "Artifacts/Deployment/OTHER/cbam.package.zip").toByteArray(); + assertTrue(Arrays.equals(expectedContentToUpload, Files.readAllBytes(uploadedFile.getValue().toPath()))); + assertEquals(createdPackage, cbamPackage); + verify(logger).debug("Probably concurrent package uploads", can_not_upload_package); + } + + /** + * If the package already exists in CBAM catalog it is not re-uploaded + */ + @Test + public void testIdempotentPackageUpload() throws Exception { + CatalogAdapterVnfpackage createdPackage = new CatalogAdapterVnfpackage(); + createdPackage.setVnfdId(CBAM_VNFD_ID); + when(cbamCatalogApi.create(uploadedFile.capture())).thenAnswer(new Answer<CatalogAdapterVnfpackage>() { + @Override + public CatalogAdapterVnfpackage answer(InvocationOnMock invocationOnMock) throws Throwable { + return createdPackage; + } + }); + when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID); + CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage(); + existingPackage.setVnfdId(CBAM_VNFD_ID); + existingVnfPackages.add(existingPackage); + when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenReturn(existingPackage); + //when + CatalogAdapterVnfpackage cbamPackage = cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID); + //verify + verify(cbamCatalogApi, never()).create(Mockito.any()); + assertEquals(existingPackage, cbamPackage); + verify(packageProvider, never()).getPackage(CSAR_ID); + } + + /** + * failure to list package in CBAM results in error + */ + @Test + public void testFailureToListVnfPackagesInCbam() throws Exception { + when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID); + com.nokia.cbam.catalog.v1.ApiException expectedException = new com.nokia.cbam.catalog.v1.ApiException(); + when(cbamCatalogApi.list()).thenThrow(expectedException); + //when + try { + cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID); + fail(); + } catch (Exception e) { + verify(logger).error("Unable to determine if the VNF package has been replicated in CBAM", expectedException); + assertEquals(expectedException, e.getCause()); + } + } + + /** + * failure to query package from CBAM results in error + */ + @Test + public void testFailureToQueryVnfPackagesFromCbam() throws Exception { + when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID); + CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage(); + existingPackage.setVnfdId(CBAM_VNFD_ID); + existingVnfPackages.add(existingPackage); + com.nokia.cbam.catalog.v1.ApiException expectedException = new com.nokia.cbam.catalog.v1.ApiException(); + when(cbamCatalogApi.getById(CBAM_VNFD_ID)).thenThrow(expectedException); + //when + try { + cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID); + fail(); + } catch (Exception e) { + verify(logger).error("Unable to query VNF package with CBAM_VNFD_ID from CBAM", expectedException); + assertEquals(expectedException, e.getCause()); + } + } + + /** + * failure to create package in CBAM results in error + */ + @Test + public void testFailureToCreatePackageInCbam() throws Exception { + CatalogAdapterVnfpackage existingPackage = new CatalogAdapterVnfpackage(); + existingPackage.setVnfdId("unknownId"); + existingVnfPackages.add(existingPackage); + when(packageProvider.getCbamVnfdId(CSAR_ID)).thenReturn(CBAM_VNFD_ID); + byte[] onapPackageContent = TestUtil.loadFile("unittests/TestCbamCatalogManager.sample.csar"); + when(packageProvider.getPackage(CSAR_ID)).thenReturn(onapPackageContent); + com.nokia.cbam.catalog.v1.ApiException expectedException = new com.nokia.cbam.catalog.v1.ApiException(); + when(cbamCatalogApi.create(Mockito.any())).thenThrow(expectedException); + try { + cbamCatalogManager.preparePackageInCbam(VNFM_ID, CSAR_ID); + fail(); + } catch (Exception e) { + verify(logger).error("Unable to create VNF with csarId CSAR identifier in package in CBAM", expectedException); + assertEquals(expectedException, e.getCause()); + } + } + + /** + * the VNFD is extracted from zip + */ + @Test + public void testExtractVnfdFromPackage() throws Exception { + Path csar = Files.createTempFile(UUID.randomUUID().toString(), "csar"); + Files.write(csar, TestUtil.loadFile("unittests/cbam.package.zip")); + when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(csar.toFile()); + //when + String content = cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID); + //verify + assertEquals("dummy vnfd\n", content); + } + + /** + * if VNFD the Tosca meta can not be extracted sensible error is returned + */ + @Test + public void testEmptyCbamPackage() throws Exception { + Path csar = Files.createTempFile(UUID.randomUUID().toString(), "csar"); + Files.write(csar, TestUtil.loadFile("unittests/empty.zip")); + when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(csar.toFile()); + //when + try { + cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID); + fail(); + } catch (RuntimeException e) { + verify(logger).error("Unable to get package with (CBAM_VNFD_ID)", e.getCause()); + assertEquals("Unable to find the TOSCA-Metadata/TOSCA.meta in archive found: []", e.getCause().getMessage()); + } + } + + /** + * if VNFD can not be extracted sensible error is returned + */ + @Test + public void testMissingVnfdCbamPackage() throws Exception { + Path csar = Files.createTempFile(UUID.randomUUID().toString(), "csar"); + Files.write(csar, TestUtil.loadFile("unittests/missing.vnfd.zip")); + when(cbamCatalogApi.content(CBAM_VNFD_ID)).thenReturn(csar.toFile()); + //when + try { + cbamCatalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID); + fail(); + } catch (RuntimeException e) { + verify(logger).error("Unable to get package with (" + CBAM_VNFD_ID + ")", e.getCause()); + assertTrue("Unable to find the vnfdloc/a.yaml in archive found: [TOSCA-Metadata/, TOSCA-Metadata/TOSCA.meta]".equals(e.getCause().getMessage()) + || "Unable to find the vnfdloc/a.yaml in archive found: [TOSCA-Metadata/TOSCA.meta, TOSCA-Metadata/]".equals(e.getCause().getMessage()) + ); + } + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestCbamTokenProvider.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestCbamTokenProvider.java new file mode 100644 index 00000000..670aa77f --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestCbamTokenProvider.java @@ -0,0 +1,438 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm; + +import com.google.common.io.ByteStreams; +import org.eclipse.jetty.server.NetworkTrafficServerConnector; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.AbstractHandler; +import org.eclipse.jetty.util.ssl.SslContextFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mockito; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VnfmInfoProvider; +import org.onap.vnfmdriver.model.VnfmInfo; +import org.springframework.http.HttpStatus; + +import javax.net.ssl.*; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.GeneralSecurityException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.cert.CertificateException; +import java.security.spec.InvalidKeySpecException; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; + +import static junit.framework.TestCase.*; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.springframework.test.util.ReflectionTestUtils.setField; + +class HttpTestServer { + Server _server; + List<String> requests = new ArrayList<>(); + List<Integer> codes = new ArrayList<>(); + List<String> respones = new ArrayList<>(); + + public void start() throws Exception { + configureServer(); + startServer(); + } + + private void startServer() throws Exception { + requests.clear(); + codes.clear(); + _server.start(); + } + + protected void configureServer() throws Exception { + Path jksPath = Paths.get(TestCbamTokenProvider.class.getResource("/unittests/localhost.jks").toURI()); + String path = jksPath.normalize().toAbsolutePath().toUri().toString(); + _server = new Server(); + SslContextFactory factory = new SslContextFactory(path); + factory.setKeyStorePassword("changeit"); + NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(_server, factory); + connector.setHost("127.0.0.1"); + _server.addConnector(connector); + _server.setHandler(new AbstractHandler() { + @Override + public void handle(String target, org.eclipse.jetty.server.Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException { + requests.add(new String(ByteStreams.toByteArray(request.getInputStream()))); + httpServletResponse.getWriter().write(respones.remove(0)); + httpServletResponse.setStatus(codes.remove(0)); + request.setHandled(true); + } + }); + } + + public void stop() throws Exception { + _server.stop(); + } +} + +public class TestCbamTokenProvider extends TestBase { + + private static String GOOD_RESPONSE = "{ \"access_token\" : \"myToken\", \"expires_in\" : 1000 }"; + @InjectMocks + private CbamTokenProvider cbamTokenProvider; + private VnfmInfo vnfmInfo = new VnfmInfo(); + private ArgumentCaptor<SSLSocketFactory> sslSocketFactory = ArgumentCaptor.forClass(SSLSocketFactory.class); + private ArgumentCaptor<HostnameVerifier> hostnameVerifier = ArgumentCaptor.forClass(HostnameVerifier.class); + private HttpTestServer testServer; + + @Before + public void initMocks() throws Exception { + setField(CbamTokenProvider.class, "logger", logger); + setField(cbamTokenProvider, "username", "myUserName"); + setField(cbamTokenProvider, "password", "myPassword"); + setField(cbamTokenProvider, "skipCertificateVerification", true); + setField(cbamTokenProvider, "skipHostnameVerification", true); + when(vnfmInfoProvider.getVnfmInfo(VNFM_ID)).thenReturn(vnfmInfo); + vnfmInfo.setPassword("vnfmPassword"); + vnfmInfo.setUserName("vnfmUserName"); + vnfmInfo.setUrl("http://127.0.0.3:12345"); + testServer = new HttpTestServer(); + testServer.start(); + URI uri = testServer._server.getURI(); + setField(cbamTokenProvider, "cbamKeyCloakBaseUrl", uri.toString()); + } + + private void addGoodTokenResponse() { + testServer.respones.add(GOOD_RESPONSE); + testServer.codes.add(HttpStatus.OK.value()); + } + + @After + public void testServer() throws Exception { + testServer.stop(); + } + + /** + * a new token is requested no token has been requested before + */ + @Test + public void testBasicTokenRequest() throws Exception { + //given + addGoodTokenResponse(); + //when + String token = cbamTokenProvider.getToken(VNFM_ID); + //verify + assertEquals(1, testServer.requests.size()); + assertTokenRequest(testServer.requests.get(0)); + assertEquals("myToken", token); + + } + + /** + * a new token is requested if the previous token has expired + */ + @Test + public void testTokenIsRequestedIfPreviousExpired() throws Exception { + //given + addGoodTokenResponse(); + String firstToken = cbamTokenProvider.getToken(VNFM_ID); + testServer.respones.add("{ \"access_token\" : \"myToken2\", \"expires_in\" : 2000 }"); + testServer.codes.add(HttpStatus.OK.value()); + when(systemFunctions.currentTimeMillis()).thenReturn(500L * 1000 + 1L); + //when + String token = cbamTokenProvider.getToken(VNFM_ID); + //verify + assertEquals(2, testServer.requests.size()); + assertTokenRequest(testServer.requests.get(0)); + assertTokenRequest(testServer.requests.get(1)); + assertEquals("myToken2", token); + } + + /** + * a new token is not requested if the previous token has not expired + */ + @Test + public void testTokenIsNotRequestedIfPreviousHasNotExpired() throws Exception { + //given + addGoodTokenResponse(); + String firstToken = cbamTokenProvider.getToken(VNFM_ID); + testServer.respones.add("{ \"access_token\" : \"myToken2\", \"expires_in\" : 2000 }"); + testServer.codes.add(HttpStatus.OK.value()); + when(systemFunctions.currentTimeMillis()).thenReturn(500L * 1000); + //when + String token = cbamTokenProvider.getToken(VNFM_ID); + //verify + assertEquals(1, testServer.requests.size()); + assertTokenRequest(testServer.requests.get(0)); + assertEquals("myToken", token); + } + + /** + * failed token requests are retried for a fixed number amount of times + */ + @Test + public void testRetry() throws Exception { + //given + addFailedResponse(); + addFailedResponse(); + addFailedResponse(); + addFailedResponse(); + addGoodTokenResponse(); + //cbamTokenProvider.failOnRequestNumber = 5; + //when + String token = cbamTokenProvider.getToken(VNFM_ID); + //verify + assertEquals(5, testServer.requests.size()); + assertTokenRequest(testServer.requests.get(0)); + assertTokenRequest(testServer.requests.get(1)); + assertTokenRequest(testServer.requests.get(2)); + assertTokenRequest(testServer.requests.get(3)); + assertTokenRequest(testServer.requests.get(4)); + verify(logger).warn(eq("Unable to get token to access CBAM API (1/5)"), Mockito.<RuntimeException>any()); + verify(logger).warn(eq("Unable to get token to access CBAM API (2/5)"), Mockito.<RuntimeException>any()); + verify(logger).warn(eq("Unable to get token to access CBAM API (3/5)"), Mockito.<RuntimeException>any()); + verify(logger).warn(eq("Unable to get token to access CBAM API (4/5)"), Mockito.<RuntimeException>any()); + assertEquals("myToken", token); + } + + /** + * failed token requests are retried for a fixed number amount of times (reacing maximal number or retries) + */ + @Test + public void testNoMoreRetry() throws Exception { + //given + addFailedResponse(); + addFailedResponse(); + addFailedResponse(); + addFailedResponse(); + addFailedResponse(); + //when + try { + cbamTokenProvider.getToken(VNFM_ID); + fail(); + } catch (RuntimeException e) { + assertNotNull(e.getCause()); + } + //verify + assertEquals(5, testServer.requests.size()); + assertTokenRequest(testServer.requests.get(0)); + assertTokenRequest(testServer.requests.get(1)); + assertTokenRequest(testServer.requests.get(2)); + assertTokenRequest(testServer.requests.get(3)); + assertTokenRequest(testServer.requests.get(4)); + verify(logger).warn(eq("Unable to get token to access CBAM API (1/5)"), Mockito.<RuntimeException>any()); + verify(logger).warn(eq("Unable to get token to access CBAM API (2/5)"), Mockito.<RuntimeException>any()); + verify(logger).warn(eq("Unable to get token to access CBAM API (3/5)"), Mockito.<RuntimeException>any()); + verify(logger).warn(eq("Unable to get token to access CBAM API (4/5)"), Mockito.<RuntimeException>any()); + verify(logger).error(eq("Unable to get token to access CBAM API (giving up retries)"), Mockito.<RuntimeException>any()); + } + + private void addFailedResponse() { + testServer.codes.add(HttpStatus.UNAUTHORIZED.value()); + testServer.respones.add(new String()); + } + + /** + * the SSL connection is established without certificate & hostname verification + */ + @Test + public void noSslVerification() throws Exception { + //given + //the default settings is no SSL & hostname check + addGoodTokenResponse(); + //when + cbamTokenProvider.getToken(VNFM_ID); + //verify + //no exception is thrown + } + + /** + * if SSL is verified the certificates must be defined + */ + @Test + public void testInvalidCombinationOfSettings() throws Exception { + //given + setField(cbamTokenProvider, "skipCertificateVerification", false); + //when + try { + cbamTokenProvider.getToken(VNFM_ID); + //verify + fail(); + } catch (RuntimeException e) { + assertEquals("If the skipCertificateVerification is set to false (default) the trustedCertificates can not be empty", e.getMessage()); + } + } + + /** + * if SSL is verified the certificates must be defined + */ + @Test + public void testInvalidCombinationOfSettings2() throws Exception { + //given + setField(cbamTokenProvider, "skipCertificateVerification", false); + setField(cbamTokenProvider, "trustedCertificates", "xx\nxx"); + //when + try { + cbamTokenProvider.getToken(VNFM_ID); + //verify + fail(); + } catch (RuntimeException e) { + assertEquals("The trustedCertificates must be a base64 encoded collection of PEM certificates", e.getMessage()); + assertNotNull(e.getCause()); + } + } + + /** + * the SSL connection is established without certificate & hostname verification + */ + @Test + public void testNotTrustedSslConnection() throws Exception { + //given + setField(cbamTokenProvider, "skipCertificateVerification", false); + Path caPem = Paths.get(TestCbamTokenProvider.class.getResource("/unittests/sample.cert.pem").toURI()); + setField(cbamTokenProvider, "trustedCertificates", Base64.getEncoder().encodeToString(Files.readAllBytes(caPem))); + addGoodTokenResponse(); + //when + try { + cbamTokenProvider.getToken(VNFM_ID); + //verify + fail(); + } catch (RuntimeException e) { + assertTrue(e.getMessage().contains("unable to find valid certification path")); + assertTrue(e.getCause() instanceof SSLHandshakeException); + } + } + + /** + * the SSL connection is established with certificate & hostname verification + */ + @Test + public void testHostnameVerificationSucceeds() throws Exception { + //given + setField(cbamTokenProvider, "skipCertificateVerification", false); + Path caPem = Paths.get(TestCbamTokenProvider.class.getResource("/unittests/localhost.cert.pem").toURI()); + setField(cbamTokenProvider, "trustedCertificates", Base64.getEncoder().encodeToString(Files.readAllBytes(caPem))); + setField(cbamTokenProvider, "cbamKeyCloakBaseUrl", testServer._server.getURI().toString().replace("127.0.0.1", "localhost")); + setField(cbamTokenProvider, "skipHostnameVerification", false); + addGoodTokenResponse(); + //when + cbamTokenProvider.getToken(VNFM_ID); + //verify + //no seception is thrown + } + + /** + * the SSL connection is dropped with certificate & hostname verification due to invalid hostname + */ + @Test + public void testHostnameverifcationfail() throws Exception { + //given + setField(cbamTokenProvider, "skipCertificateVerification", false); + Path caPem = Paths.get(TestCbamTokenProvider.class.getResource("/unittests/localhost.cert.pem").toURI()); + setField(cbamTokenProvider, "trustedCertificates", Base64.getEncoder().encodeToString(Files.readAllBytes(caPem))); + setField(cbamTokenProvider, "skipHostnameVerification", false); + addGoodTokenResponse(); + //when + try { + cbamTokenProvider.getToken(VNFM_ID); + //verify + fail(); + } catch (RuntimeException e) { + assertTrue(e.getMessage().contains("Hostname 127.0.0.1 not verified")); + assertTrue(e.getCause() instanceof SSLPeerUnverifiedException); + } + } + + /** + * invalid certificate content + */ + @Test + public void testInvalidCerificateContent() throws Exception { + //given + setField(cbamTokenProvider, "skipCertificateVerification", false); + setField(cbamTokenProvider, "trustedCertificates", Base64.getEncoder().encodeToString("-----BEGIN CERTIFICATE-----\nkuku\n-----END CERTIFICATE-----\n".getBytes())); + setField(cbamTokenProvider, "skipHostnameVerification", false); + addGoodTokenResponse(); + //when + try { + cbamTokenProvider.getToken(VNFM_ID); + //verify + fail(); + } catch (RuntimeException e) { + assertTrue(e.getMessage().contains("Unable to load certificates")); + assertTrue(e.getCause() instanceof GeneralSecurityException); + } + } + + /** + * Verify client certificates are not verified + * \ + */ + @Test + public void testClientCertificates() throws Exception { + //when + new CbamTokenProvider.AllTrustedTrustManager().checkClientTrusted(null, null); + //verify + //no security exception is thrown + } + + /** + * Exception during keystore creation is logged (semi-useless) + */ + @Test + public void testKeystoreCreationFailure() { + KeyStoreException expectedException = new KeyStoreException(); + class X extends CbamTokenProvider { + X(VnfmInfoProvider vnfmInfoProvider) { + super(vnfmInfoProvider); + } + + @Override + TrustManager[] buildTrustManager() throws KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException, CertificateException { + throw expectedException; + } + } + try { + new X(null).buildSSLSocketFactory(); + fail(); + } catch (RuntimeException e) { + assertEquals(expectedException, e.getCause()); + verify(logger).error("Unable to create SSL socket factory", expectedException); + } + } + + private void assertTokenRequest(String body) { + assertContains(body, "grant_type", "password"); + assertContains(body, "client_id", "vnfmUserName"); + assertContains(body, "client_secret", "vnfmPassword"); + assertContains(body, "username", "myUserName"); + assertContains(body, "password", "myPassword"); + } + + private void assertContains(String content, String key, String value) { + assertTrue(content.contains(key + "=" + value)); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestDriverProperties.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestDriverProperties.java new file mode 100644 index 00000000..22db05b2 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestDriverProperties.java @@ -0,0 +1,35 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ + +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm; + +import org.junit.Test; + +import static pl.pojo.tester.api.assertion.Assertions.assertPojoMethodsFor; + + +public class TestDriverProperties { + + /** + * Test basic POJO behaviour + */ + @Test + public void test() { + + + assertPojoMethodsFor(DriverProperties.class).areWellImplemented(); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestJobManager.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestJobManager.java new file mode 100644 index 00000000..1ba58abe --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestJobManager.java @@ -0,0 +1,544 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm; + +import com.google.common.collect.Lists; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import com.nokia.cbam.lcm.v32.ApiException; +import com.nokia.cbam.lcm.v32.model.*; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.onap.vnfmdriver.model.JobDetailInfo; +import org.onap.vnfmdriver.model.JobResponseInfo; +import org.onap.vnfmdriver.model.JobStatus; +import org.springframework.test.util.ReflectionTestUtils; +import org.threeten.bp.OffsetDateTime; + +import javax.servlet.http.HttpServletResponse; +import java.util.ArrayList; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.UUID; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import static junit.framework.TestCase.*; +import static org.mockito.Mockito.*; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCM_API_VERSION; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.ILifecycleChangeNotificationManager.SEPARATOR; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager.extractOnapJobId; +import static org.onap.vnfmdriver.model.JobStatus.*; + +public class TestJobManager extends TestBase { + + @Mock + private HttpServletResponse httpResponse; + + @InjectMocks + private JobManager jobManager; + private List<VnfInfo> vnfs = new ArrayList<>(); + + @Before + public void initMocks() throws Exception { + ReflectionTestUtils.setField(JobManager.class, "logger", logger); + when(vnfApi.vnfsGet(NOKIA_LCM_API_VERSION)).thenReturn(vnfs); + when(selfRegistrationManager.isReady()).thenReturn(true); + } + + /** + * Only the _ can be used as separator + * . / % & handled specially in URLs + * - used in CBAM for separation + */ + @Test + public void testSeparator() { + assertEquals("_", SEPARATOR); + } + + /** + * The operation result must contain the ONAP job identifier under the jobId field + */ + @Test + public void testJobIdExtractionFromOperationResult() { + assertEquals("1234", extractOnapJobId(new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"1234\"}}"))); + try { + extractOnapJobId(new JsonParser().parse("{ }")); + fail(); + } catch (NoSuchElementException e) { + assertEquals("The operation result {} does not contain the mandatory additionalParams structure", e.getMessage()); + } + try { + extractOnapJobId(new JsonParser().parse("{ \"additionalParams\" : { } }")); + fail(); + } catch (NoSuchElementException e) { + assertEquals("The operation result {\"additionalParams\":{}} does not contain the mandatory jobId in the additionalParams structure", e.getMessage()); + } + } + + /** + * If the VNF does not exists but the job manager still runs the VNF manipulation process the job is reported to be running + */ + @Test + public void testJobForNonExistingVnfReportedRunningIfJobIsOngoing() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + //when + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, STARTED, "50", "Operation started"); + } + + /** + * If the VNF does not exists and the internal job is not running the job is reported to be finished + */ + @Test + public void testJobForExistingVnfReportedRunningIfJobIsFinished() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + jobManager.jobFinished(jobId); + //when + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, JobStatus.FINISHED, "100", "Operation finished"); + } + + /** + * Spawning jobs after preparing for shutdown results in error + */ + @Test + public void testNoMoreJobsAreAllowedAfterPrepareForShutdown() throws Exception { + jobManager.prepareForShutdown(); + //when + try { + jobManager.spawnJob(JOB_ID, httpResponse); + fail(); + } catch (Exception e) { + verify(logger).error("The service is preparing to shut down"); + } + } + + /** + * Verify if the jobId has valid format + */ + @Test + public void testJobIdValidation() throws Exception { + try { + //when + jobManager.getJob(VNFM_ID, "bad"); + //verify + fail(); + } catch (IllegalArgumentException e) { + assertEquals("The jobId should be in the <vnfId>_<UUID> format, but was bad", e.getMessage()); + } + try { + //when + jobManager.getJob(VNFM_ID, "vnfId_"); + //verify + fail(); + } catch (IllegalArgumentException e) { + assertEquals("The UUID in the jobId (vnfId_) can not be empty", e.getMessage()); + } + try { + //when + jobManager.getJob(VNFM_ID, "_UUID"); + //verify + fail(); + } catch (IllegalArgumentException e) { + assertEquals("The vnfId in the jobId (_UUID) can not be empty", e.getMessage()); + } + } + + /** + * If the VNF exists but no operation execution is present with given internalJobId, than the state of the + * job is ongoing if the internal job is ongoing + */ + @Test + public void testExistingVnfWithNotYetStartedOperation() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + VnfInfo detailedVnf = new VnfInfo(); + detailedVnf.setId(VNF_ID); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(detailedVnf); + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, STARTED, "50", "Operation started"); + assertTrue(jobManager.hasOngoingJobs()); + } + + /** + * If the VNF exists but no operation execution is present with given internalJobId, than the state of the + * job is failed if the internal job is finished (the operation on CBAM was not able to start) + */ + @Test + public void testExistingVnfWithNotUnableToStartOperation() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + VnfInfo detailedVnf = new VnfInfo(); + detailedVnf.setId(VNF_ID); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(detailedVnf); + jobManager.jobFinished(jobId); + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, ERROR, "100", "Operation failed due to The requested operation was not able to start on CBAM"); + assertFalse(jobManager.hasOngoingJobs()); + } + + /** + * If the VNF exists but and the operation execution is present with given internalJobId, than the state of the + * job is ongoing if the operation is ongoing + */ + @Test + public void testExistingVnfWithStartedOperation() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + VnfInfo detailedVnf = new VnfInfo(); + detailedVnf.setId(VNF_ID); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(detailedVnf); + OperationExecution operation = new OperationExecution(); + operation.setId(UUID.randomUUID().toString()); + operation.setStartTime(OffsetDateTime.now()); + operation.setStatus(OperationStatus.STARTED); + detailedVnf.setOperationExecutions(new ArrayList<>()); + detailedVnf.getOperationExecutions().add(operation); + JsonElement operationParams = new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + jobId + "\"}}"); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(operation.getId(), NOKIA_LCM_API_VERSION)).thenReturn(operationParams); + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, STARTED, "50", "Operation started"); + assertTrue(jobManager.hasOngoingJobs()); + } + + /** + * If the VNF does not exists till the time the job queries the status of the operation + */ + @Test + public void testTerminatedVnf() throws Exception { + //ddd + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + VnfInfo detailedVnf = new VnfInfo(); + detailedVnf.setId(VNF_ID); + List<Integer> vnfQueryCallCounter = new ArrayList<>(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenAnswer(new Answer<VnfInfo>() { + @Override + public VnfInfo answer(InvocationOnMock invocation) throws Throwable { + vnfs.clear(); + return detailedVnf; + } + }); + + jobManager.jobFinished(jobId); + + OperationExecution operation = new OperationExecution(); + operation.setId(UUID.randomUUID().toString()); + operation.setStartTime(OffsetDateTime.now()); + operation.setStatus(OperationStatus.FINISHED); + operation.setOperationType(OperationType.TERMINATE); + detailedVnf.setOperationExecutions(new ArrayList<>()); + detailedVnf.getOperationExecutions().add(operation); + + JsonElement operationParams = new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + jobId + "\"}}"); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(operation.getId(), NOKIA_LCM_API_VERSION)).thenReturn(operationParams); + //when + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, FINISHED, "100", "Operation finished"); + } + + /** + * If the VNF exists but and the operation execution is present with given internalJobId, than the state of the + * job is error if the operation is failed + */ + @Test + public void testExistingVnfWithFailedOperation() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + VnfInfo detailedVnf = new VnfInfo(); + detailedVnf.setId(VNF_ID); + List<Integer> vnfCounter = new ArrayList<>(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(detailedVnf); + OperationExecution operation = new OperationExecution(); + operation.setId(UUID.randomUUID().toString()); + operation.setStartTime(OffsetDateTime.now()); + operation.setStatus(OperationStatus.FAILED); + ProblemDetails errorDetails = new ProblemDetails(); + errorDetails.setTitle("Title"); + errorDetails.setDetail("detail"); + operation.setError(errorDetails); + detailedVnf.setOperationExecutions(new ArrayList<>()); + detailedVnf.getOperationExecutions().add(operation); + JsonElement operationParams = new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + jobId + "\"}}"); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(operation.getId(), NOKIA_LCM_API_VERSION)).thenReturn(operationParams); + //when + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, ERROR, "100", "Operation failed due to Title: detail"); + assertTrue(jobManager.hasOngoingJobs()); + } + + private void assertResult(String jobId, JobDetailInfo job, JobStatus status, String progress, String descriptor) { + assertEquals(jobId, job.getJobId()); + assertEquals(status, job.getResponseDescriptor().getStatus()); + assertEquals(progress, job.getResponseDescriptor().getProgress()); + assertNull(job.getResponseDescriptor().getErrorCode()); + boolean finalState = JobStatus.ERROR.equals(status) || JobStatus.FINISHED.equals(status); + if (finalState) { + assertEquals(2, job.getResponseDescriptor().getResponseHistoryList().size()); + JobResponseInfo startEvent = job.getResponseDescriptor().getResponseHistoryList().get(0); + JobResponseInfo endEvent = job.getResponseDescriptor().getResponseHistoryList().get(1); + assertNull(startEvent.getErrorCode()); + assertEquals("50", startEvent.getProgress()); + assertEquals(JobStatus.STARTED.name(), startEvent.getStatus()); + assertEquals("1", startEvent.getResponseId()); + assertEquals("Operation started", startEvent.getStatusDescription()); + + assertNull(endEvent.getErrorCode()); + assertEquals("100", endEvent.getProgress()); + assertEquals(job.getResponseDescriptor().getStatus().name(), endEvent.getStatus()); + assertEquals("2", endEvent.getResponseId()); + assertEquals(descriptor, endEvent.getStatusDescription()); + } else { + assertEquals(1, job.getResponseDescriptor().getResponseHistoryList().size()); + assertNull(job.getResponseDescriptor().getResponseHistoryList().get(0).getErrorCode()); + assertEquals(progress, job.getResponseDescriptor().getResponseHistoryList().get(0).getProgress()); + assertEquals(job.getResponseDescriptor().getStatus().name(), job.getResponseDescriptor().getResponseHistoryList().get(0).getStatus()); + assertEquals("1", job.getResponseDescriptor().getResponseHistoryList().get(0).getResponseId()); + assertEquals(descriptor, job.getResponseDescriptor().getResponseHistoryList().get(0).getStatusDescription()); + } + assertEquals(Integer.toString(job.getResponseDescriptor().getResponseHistoryList().size()), job.getResponseDescriptor().getResponseId()); + assertEquals(descriptor, job.getResponseDescriptor().getStatusDescription()); + } + + /** + * If the VNF exists but and the operation execution is present with given internalJobId, than the state of the + * job is finished if the operation is finished, but is not a termination + */ + @Test + public void testExistingVnfWithFinishedOperation() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + VnfInfo detailedVnf = new VnfInfo(); + detailedVnf.setId(VNF_ID); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(detailedVnf); + OperationExecution operation = new OperationExecution(); + operation.setId(UUID.randomUUID().toString()); + operation.setStartTime(OffsetDateTime.now()); + operation.setStatus(OperationStatus.FINISHED); + operation.setOperationType(OperationType.SCALE); + detailedVnf.setOperationExecutions(new ArrayList<>()); + detailedVnf.getOperationExecutions().add(operation); + JsonElement operationParams = new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + jobId + "\"}}"); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(operation.getId(), NOKIA_LCM_API_VERSION)).thenReturn(operationParams); + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, JobStatus.FINISHED, "100", "Operation finished"); + assertTrue(jobManager.hasOngoingJobs()); + } + + /** + * If the VNF exists but and the operation execution is present with given internalJobId, than the state of the + * job is ongoing if the termination operation is finished. In ONAP terminology the termination includes + * delete, so the ONAP operation is ongoing since the VNF is not yet deleted + */ + @Test + public void testExistingVnfWithFinishedTerminationOperation() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + VnfInfo detailedVnf = new VnfInfo(); + detailedVnf.setId(VNF_ID); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(detailedVnf); + OperationExecution operation = new OperationExecution(); + operation.setId(UUID.randomUUID().toString()); + operation.setStartTime(OffsetDateTime.now()); + operation.setStatus(OperationStatus.FINISHED); + operation.setOperationType(OperationType.TERMINATE); + detailedVnf.setOperationExecutions(new ArrayList<>()); + detailedVnf.getOperationExecutions().add(operation); + JsonElement operationParams = new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + jobId + "\"}}"); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(operation.getId(), NOKIA_LCM_API_VERSION)).thenReturn(operationParams); + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, STARTED, "50", "Operation started"); + //when + jobManager.jobFinished(jobId); + job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertResult(jobId, job, ERROR, "100", "Operation failed due to unable to delete VNF"); + assertFalse(jobManager.hasOngoingJobs()); + + } + + /** + * Failuire to retrieve operation parameters (CBAM REST API fail) is logged and propagated + */ + @Test + public void failuresDuringOperationExecutionRetrievalIsLoggedAndPropagated() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + VnfInfo detailedVnf = new VnfInfo(); + detailedVnf.setId(VNF_ID); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(detailedVnf); + OperationExecution operation = new OperationExecution(); + operation.setId(UUID.randomUUID().toString()); + detailedVnf.setOperationExecutions(new ArrayList<>()); + detailedVnf.getOperationExecutions().add(operation); + ApiException expectedException = new ApiException(); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(operation.getId(), NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + //verify + try { + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + fail(); + } catch (RuntimeException e) { + assertEquals(expectedException, e.getCause()); + verify(logger).error("Unable to retrieve operation parameters", expectedException); + } + assertTrue(jobManager.hasOngoingJobs()); + } + + /** + * Failure to retrieve VNF (CBAM REST API fail) is logged and propagated + */ + @Test + public void failuresDuringVnfRetrievalIsLoggedAndPropagated() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + ApiException expectedException = new ApiException(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + //verify + try { + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + fail(); + } catch (RuntimeException e) { + assertEquals(expectedException, e.getCause()); + verify(logger).error("Unable to retrieve VNF", expectedException); + } + assertTrue(jobManager.hasOngoingJobs()); + } + + /** + * When searching for the ONAP job by iterating the operation executions. The newest jobs + * are inspected first (performance optimalization) + */ + @Test + public void testNewestOperationAreInspectedFirst() throws Exception { + String jobId = jobManager.spawnJob(VNF_ID, httpResponse); + VnfInfo vnf = new VnfInfo(); + vnf.setId(VNF_ID); + vnfs.add(vnf); + VnfInfo detailedVnf = new VnfInfo(); + detailedVnf.setId(VNF_ID); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(detailedVnf); + OperationExecution olderOperation = new OperationExecution(); + olderOperation.setId(UUID.randomUUID().toString()); + olderOperation.setStartTime(OffsetDateTime.now()); + olderOperation.setStatus(OperationStatus.FINISHED); + olderOperation.setOperationType(OperationType.TERMINATE); + OperationExecution newerOperation = new OperationExecution(); + newerOperation.setId(UUID.randomUUID().toString()); + newerOperation.setStartTime(OffsetDateTime.now().plusDays(1)); + newerOperation.setStatus(OperationStatus.FINISHED); + newerOperation.setOperationType(OperationType.TERMINATE); + detailedVnf.setOperationExecutions(new ArrayList<>()); + detailedVnf.getOperationExecutions().add(olderOperation); + detailedVnf.getOperationExecutions().add(newerOperation); + JsonElement operationParams = new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + jobId + "\"}}"); + List<String> queriedOperaionsInOrder = new ArrayList<>(); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(Mockito.anyString(), Mockito.eq(NOKIA_LCM_API_VERSION))) + .then(new Answer<Object>() { + @Override + public Object answer(InvocationOnMock invocationOnMock) throws Throwable { + queriedOperaionsInOrder.add(invocationOnMock.getArguments()[0].toString()); + if (invocationOnMock.getArguments()[0].equals(olderOperation.getId())) { + return new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + jobId + "\"}}"); + } else { + return new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + "nonMatching" + "\"}}"); + } + } + }); + JobDetailInfo job = jobManager.getJob(VNFM_ID, jobId); + //verify + assertEquals(Lists.newArrayList(newerOperation.getId(), olderOperation.getId()), queriedOperaionsInOrder); + assertTrue(jobManager.hasOngoingJobs()); + } + + /** + * if the registration process has not finished it is prevented to spawn jobs + */ + @Test + public void noJobCanBeStartedIfRegistrationNotFinished() throws Exception { + //given + when(selfRegistrationManager.isReady()).thenReturn(false); + //when + try { + jobManager.spawnJob(VNF_ID, httpResponse); + fail(); + } catch (RuntimeException e) { + assertEquals("The service is not yet ready", e.getMessage()); + } + } + + /** + * Ongoing job are out waited during the the preparation for shutdown + */ + @Test + public void onGoingJobsAreOutwaitedDuringShutdown() throws Exception { + String firstJobId = jobManager.spawnJob(VNF_ID, httpResponse); + ExecutorService executorService = Executors.newCachedThreadPool(); + ArgumentCaptor<Integer> sleeps = ArgumentCaptor.forClass(Integer.class); + doNothing().when(systemFunctions).sleep(sleeps.capture()); + Future<?> shutDown = executorService.submit(new Runnable() { + @Override + public void run() { + jobManager.prepareForShutdown(); + } + }); + while (sleeps.getAllValues().size() == 0) { + try { + Thread.sleep(1); + } catch (InterruptedException e) { + } + } + assertFalse(shutDown.isDone()); + jobManager.jobFinished(firstJobId); + //verify + shutDown.get(); + verify(systemFunctions, times(sleeps.getAllValues().size())).sleep(500L); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestLifecycleManager.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestLifecycleManager.java new file mode 100644 index 00000000..64adaab1 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestLifecycleManager.java @@ -0,0 +1,1336 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.nokia.cbam.catalog.v1.model.CatalogAdapterVnfpackage; +import com.nokia.cbam.lcm.v32.ApiException; +import com.nokia.cbam.lcm.v32.model.*; +import com.nokia.cbam.lcm.v32.model.OperationType; +import com.nokia.cbam.lcm.v32.model.VimInfo; +import com.nokia.cbam.lcm.v32.model.VnfInfo; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.InOrder; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VimInfoProvider; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc.TestVfcGrantManager; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc.VfcGrantManager; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.StoreLoader; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.LifecycleChangeNotificationManager; +import org.onap.vnfmdriver.model.ExtVirtualLinkInfo; +import org.onap.vnfmdriver.model.*; +import org.onap.vnfmdriver.model.ScaleDirection; +import org.threeten.bp.OffsetDateTime; + +import javax.servlet.http.HttpServletResponse; +import java.nio.file.Paths; +import java.util.*; + +import static java.lang.Boolean.parseBoolean; +import static java.nio.file.Files.readAllBytes; +import static junit.framework.TestCase.*; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.*; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCM_API_VERSION; +import static org.springframework.test.util.ReflectionTestUtils.setField; + +public class TestLifecycleManager extends TestBase { + public static final String JOB_ID = "myJobId"; + public static final String CBAM_VNFD_ID = "cbamVnfdId"; + public static final String TENANT = "myTenant"; + public static final String OPERATION_EXECUTION_ID = "operationExecutionId"; + private static final String ONAP_CSAR_ID = "myOnapCsarId"; + private static final String VIM_ID = "ownerId_regionId"; + private GrantVNFResponseVim vim = new GrantVNFResponseVim(); + @Mock + private CatalogManager catalogManager; + @Mock + private VfcGrantManager vfcGrantManager; + @Mock + private CbamTokenProvider tokenProvider; + @Mock + private JobManager jobManager; + @Mock + private LifecycleChangeNotificationManager notificationManager; + @Mock + private HttpServletResponse restResponse; + @Mock + private VimInfoProvider vimInfoProvider; + + private ArgumentCaptor<CreateVnfRequest> createRequest = ArgumentCaptor.forClass(CreateVnfRequest.class); + private AdditionalParams additionalParam = new AdditionalParams(); + private String INSTANTIATION_LEVEL = "level1"; + private GrantVNFResponseVim grantResponse = new GrantVNFResponseVim(); + private String cbamVnfdContent; + private OperationExecution instantiationOperationExecution = new OperationExecution(); + private OperationExecution modifyPropertyoperationExecution = new OperationExecution(); + private OperationExecution scaleOperationExecution = new OperationExecution(); + private OperationExecution healOperationExecution = new OperationExecution(); + + private VnfInfo vnfInfo = new VnfInfo(); + private List<OperationExecution> operationExecutions = new ArrayList<>(); + private org.onap.vnfmdriver.model.VimInfo vimInfo = new org.onap.vnfmdriver.model.VimInfo(); + private ExtVirtualLinkInfo externalVirtualLink = new ExtVirtualLinkInfo(); + private ExtManagedVirtualLinkData extManVl = new ExtManagedVirtualLinkData(); + private ArgumentCaptor<ModifyVnfInfoRequest> actualVnfModifyRequest = ArgumentCaptor.forClass(ModifyVnfInfoRequest.class); + private Set<Boolean> finished = new HashSet<>(); + private ArgumentCaptor<ScaleVnfRequest> actualScaleRequest = ArgumentCaptor.forClass(ScaleVnfRequest.class); + private ArgumentCaptor<HealVnfRequest> actualHealRequest = ArgumentCaptor.forClass(HealVnfRequest.class); + + private LifecycleManager lifecycleManager; + + @Before + public void initMocks() throws Exception { + vnfInfo.setExtensions(new ArrayList<>()); + vnfInfo.setOperationExecutions(new ArrayList<>()); + lifecycleManager = new LifecycleManager(catalogManager, vfcGrantManager, cbamRestApiProvider, vimInfoProvider, jobManager, notificationManager); + cbamVnfdContent = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/vnfd.full.yaml").toURI()))); + setField(LifecycleManager.class, "logger", logger); + CatalogAdapterVnfpackage cbamPackage = new CatalogAdapterVnfpackage(); + when(catalogManager.preparePackageInCbam(VNFM_ID, ONAP_CSAR_ID)).thenReturn(cbamPackage); + cbamPackage.setVnfdId(CBAM_VNFD_ID); + vnfInfo.setVnfdId(CBAM_VNFD_ID); + vnfInfo.setId(VNF_ID); + when(jobManager.spawnJob(VNF_ID, restResponse)).thenReturn(JOB_ID); + when(catalogManager.getCbamVnfdContent(VNFM_ID, CBAM_VNFD_ID)).thenReturn(cbamVnfdContent); + cbamPackage.setId(CBAM_VNFD_ID); + vimInfo.setUrl("cloudUrl"); + vimInfo.setPassword("vimPassword"); + vimInfo.setUserName("vimUsername"); + vimInfo.setSslInsecure("true"); + vimInfo.setVimId(VIM_ID); + vimInfo.setName("vimName"); + when(vimInfoProvider.getVimInfo((VIM_ID))).thenReturn(vimInfo); + instantiationOperationExecution.setId(OPERATION_EXECUTION_ID); + instantiationOperationExecution.setOperationType(OperationType.INSTANTIATE); + instantiationOperationExecution.setStartTime(OffsetDateTime.now()); + when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(operationExecutions); + operationExecutions.add(modifyPropertyoperationExecution); + modifyPropertyoperationExecution.setStartTime(OffsetDateTime.now()); + modifyPropertyoperationExecution.setOperationType(OperationType.MODIFY_INFO); + operationExecutions.add(instantiationOperationExecution); + instantiationOperationExecution.setStatus(OperationStatus.FINISHED); + modifyPropertyoperationExecution.setStatus(OperationStatus.FINISHED); + modifyPropertyoperationExecution.setId(UUID.randomUUID().toString()); + scaleOperationExecution.setId(UUID.randomUUID().toString()); + healOperationExecution.setId(UUID.randomUUID().toString()); + when(vnfApi.vnfsVnfInstanceIdPatch(eq(VNF_ID), actualVnfModifyRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(modifyPropertyoperationExecution); + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + finished.add(Boolean.TRUE); + return null; + } + }).when(jobManager).jobFinished(JOB_ID); + when(vnfApi.vnfsVnfInstanceIdScalePost(eq(VNF_ID), actualScaleRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenAnswer(new Answer<OperationExecution>() { + @Override + public OperationExecution answer(InvocationOnMock invocation) throws Throwable { + operationExecutions.add(scaleOperationExecution); + return scaleOperationExecution; + } + }); + when(vnfApi.vnfsVnfInstanceIdHealPost(eq(VNF_ID), actualHealRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenAnswer(new Answer<OperationExecution>() { + @Override + public OperationExecution answer(InvocationOnMock invocation) throws Throwable { + operationExecutions.add(healOperationExecution); + return healOperationExecution; + } + }); + } + + /** + * test instantiation + */ + @Test + public void testInstantiation() throws Exception { + //given + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + additionalParam.setInstantiationLevel(INSTANTIATION_LEVEL); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + grantResponse.setAccessInfo(accessInfo); + ArgumentCaptor<InstantiateVnfRequest> actualInstantiationRequest = ArgumentCaptor.forClass(InstantiateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdInstantiatePost(eq(VNF_ID), actualInstantiationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(instantiationOperationExecution); + //when + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + waitForJobToFinishInJobManager(finished); + //verify + assertEquals(VNF_ID, response.getVnfInstanceId()); + assertEquals(JOB_ID, response.getJobId()); + assertEquals(createRequest.getAllValues().size(), 1); + assertEquals("myDescription", createRequest.getValue().getDescription()); + assertEquals("vnfName", createRequest.getValue().getName()); + assertEquals(CBAM_VNFD_ID, createRequest.getValue().getVnfdId()); + assertEquals(1, actualInstantiationRequest.getAllValues().size()); + assertEquals(1, actualInstantiationRequest.getValue().getVims().size()); + OPENSTACKV2INFO actualVim = (OPENSTACKV2INFO) actualInstantiationRequest.getValue().getVims().get(0); + assertEquals(VIM_ID, actualVim.getId()); + assertEquals(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO, actualVim.getVimInfoType()); + assertEquals(Boolean.valueOf(parseBoolean(vimInfo.getSslInsecure())), actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertEquals("cloudUrl", actualVim.getInterfaceInfo().getEndpoint()); + //FIXME assertEquals();actualVim.getInterfaceInfo().getTrustedCertificates()); + assertEquals("vimPassword", actualVim.getAccessInfo().getPassword()); + assertEquals("regionId", actualVim.getAccessInfo().getRegion()); + assertEquals("myTenant", actualVim.getAccessInfo().getTenant()); + assertEquals("vimUsername", actualVim.getAccessInfo().getUsername()); + assertEquals(1, actualInstantiationRequest.getValue().getComputeResourceFlavours().size()); + assertEquals("flavourProviderId", actualInstantiationRequest.getValue().getComputeResourceFlavours().get(0).getResourceId()); + assertEquals(VIM_ID, actualInstantiationRequest.getValue().getComputeResourceFlavours().get(0).getVimId()); + assertEquals("virtualComputeDescId", actualInstantiationRequest.getValue().getComputeResourceFlavours().get(0).getVnfdVirtualComputeDescId()); + assertEquals(1, actualInstantiationRequest.getValue().getExtManagedVirtualLinks().size()); + assertEquals(extManVl, actualInstantiationRequest.getValue().getExtManagedVirtualLinks().get(0)); + assertEquals(2, actualInstantiationRequest.getValue().getExtVirtualLinks().size()); + + assertEquals("myNetworkProviderId", actualInstantiationRequest.getValue().getExtVirtualLinks().get(0).getResourceId()); + assertEquals("myEVlId", actualInstantiationRequest.getValue().getExtVirtualLinks().get(0).getExtVirtualLinkId()); + assertEquals(1, actualInstantiationRequest.getValue().getExtVirtualLinks().get(0).getExtCps().size()); + assertEquals("myCpdId", actualInstantiationRequest.getValue().getExtVirtualLinks().get(0).getExtCps().get(0).getCpdId()); + + assertEquals(VIM_ID, actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getVimId()); + assertEquals("evlId1", actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getExtVirtualLinkId()); + assertEquals("networkProviderId1", actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getResourceId()); + assertEquals(1, actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getExtCps().size()); + + + assertEquals(Integer.valueOf(2), actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getExtCps().get(0).getNumDynamicAddresses()); + assertEquals("cpdId3", actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getExtCps().get(0).getCpdId()); + assertEquals(1, actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getExtCps().get(0).getAddresses().size()); + assertEquals("1.2.3.4", actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getExtCps().get(0).getAddresses().get(0).getIp()); + assertEquals("mac", actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getExtCps().get(0).getAddresses().get(0).getMac()); + assertEquals("subnetId", actualInstantiationRequest.getValue().getExtVirtualLinks().get(1).getExtCps().get(0).getAddresses().get(0).getSubnetId()); + + assertEquals("myFlavorId", actualInstantiationRequest.getValue().getFlavourId()); + assertEquals(Boolean.TRUE, actualInstantiationRequest.getValue().isGrantlessMode()); + assertEquals("level1", actualInstantiationRequest.getValue().getInstantiationLevelId()); + assertEquals(1, actualInstantiationRequest.getValue().getZones().size()); + assertEquals(VIM_ID, actualInstantiationRequest.getValue().getZones().get(0).getVimId()); + assertEquals("zoneProviderId", actualInstantiationRequest.getValue().getZones().get(0).getResourceId()); + assertEquals("zoneId", actualInstantiationRequest.getValue().getZones().get(0).getId()); + assertEquals(1, actualInstantiationRequest.getValue().getSoftwareImages().size()); + assertEquals(VIM_ID, actualInstantiationRequest.getValue().getSoftwareImages().get(0).getVimId()); + assertEquals("imageProviderId", actualInstantiationRequest.getValue().getSoftwareImages().get(0).getResourceId()); + assertEquals("imageId", actualInstantiationRequest.getValue().getSoftwareImages().get(0).getVnfdSoftwareImageId()); + String actualEmbeddedAdditionParams = new Gson().toJson(actualInstantiationRequest.getValue().getAdditionalParams()); + assertTrue("{\"jobId\":\"myJobId\",\"a\":\"b\"}".equals(actualEmbeddedAdditionParams) || "{\"a\":\"b\",\"jobId\":\"myJobId\"}".equals(actualEmbeddedAdditionParams)); + assertTrue(actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertTrue(actualVim.getInterfaceInfo().isSkipCertificateHostnameCheck()); + + assertEquals(1, actualVnfModifyRequest.getAllValues().size()); + assertEquals(2, actualVnfModifyRequest.getValue().getExtensions().size()); + assertEquals(LifecycleManager.ONAP_CSAR_ID, actualVnfModifyRequest.getValue().getExtensions().get(0).getName()); + assertEquals(ONAP_CSAR_ID, actualVnfModifyRequest.getValue().getExtensions().get(0).getValue()); + assertEquals(ILifecycleChangeNotificationManager.EXTERNAL_VNFM_ID, actualVnfModifyRequest.getValue().getExtensions().get(1).getName()); + assertEquals(VNFM_ID, actualVnfModifyRequest.getValue().getExtensions().get(1).getValue()); + + //the 3.2 API does not accept empty array + assertNull(actualVnfModifyRequest.getValue().getVnfConfigurableProperties()); + verify(jobManager).spawnJob(VNF_ID, restResponse); + } + + /** + * invalid VIM type results in failure + */ + @Test + public void testInstantiationWithInvalidVimType() throws Exception { + //given + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OTHER_VIM_INFO); + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + //when + try { + lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + //verify + fail(); + } catch (Exception e) { + assertEquals("Only OPENSTACK_V2_INFO, OPENSTACK_V3_INFO and VMWARE_VCLOUD_INFO is the supported VIM types", e.getMessage()); + } + verify(vnfApi, never()).vnfsPost(Mockito.any(), Mockito.any()); + verify(logger).error("Only OPENSTACK_V2_INFO, OPENSTACK_V3_INFO and VMWARE_VCLOUD_INFO is the supported VIM types"); + } + + /** + * test instantiation with KeyStone V2 based with SSL + */ + @Test + public void testInstantiationV2WithSsl() throws Exception { + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + additionalParam.setInstantiationLevel(INSTANTIATION_LEVEL); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + String caCert = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/localhost.cert.pem").toURI()))); + vimInfo.setSslInsecure("false"); + vimInfo.setSslCacert(caCert); + grantResponse.setAccessInfo(accessInfo); + ArgumentCaptor<InstantiateVnfRequest> actualInstantiationRequest = ArgumentCaptor.forClass(InstantiateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdInstantiatePost(eq(VNF_ID), actualInstantiationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(instantiationOperationExecution); + //when + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualInstantiationRequest.getValue().getVims().size()); + //verify + OPENSTACKV2INFO actualVim = (OPENSTACKV2INFO) actualInstantiationRequest.getValue().getVims().get(0); + assertEquals(StoreLoader.getCertifacates(caCert).iterator().next(), new String(actualVim.getInterfaceInfo().getTrustedCertificates().get(0))); + assertTrue(!actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertTrue(!actualVim.getInterfaceInfo().isSkipCertificateHostnameCheck()); + } + + /** + * test instantiation with KeyStone V3 based + */ + @Test + public void testInstantiationV3() throws Exception { + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V3_INFO); + + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + additionalParam.setInstantiationLevel(INSTANTIATION_LEVEL); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + grantResponse.setAccessInfo(accessInfo); + ArgumentCaptor<InstantiateVnfRequest> actualInstantiationRequest = ArgumentCaptor.forClass(InstantiateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdInstantiatePost(eq(VNF_ID), actualInstantiationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(instantiationOperationExecution); + //when + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualInstantiationRequest.getValue().getVims().size()); + //verify + OPENSTACKV3INFO actualVim = (OPENSTACKV3INFO) actualInstantiationRequest.getValue().getVims().get(0); + assertEquals(VIM_ID, actualVim.getId()); + assertEquals(VimInfo.VimInfoTypeEnum.OPENSTACK_V3_INFO, actualVim.getVimInfoType()); + assertEquals(Boolean.valueOf(parseBoolean(vimInfo.getSslInsecure())), actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertEquals("cloudUrl", actualVim.getInterfaceInfo().getEndpoint()); + //FIXME assertEquals();actualVim.getInterfaceInfo().getTrustedCertificates()); + assertEquals("vimPassword", actualVim.getAccessInfo().getPassword()); + assertEquals("regionId", actualVim.getAccessInfo().getRegion()); + assertEquals("myTenant", actualVim.getAccessInfo().getProject()); + assertEquals("myDomain", actualVim.getAccessInfo().getDomain()); + assertEquals("vimUsername", actualVim.getAccessInfo().getUsername()); + assertTrue(actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertTrue(actualVim.getInterfaceInfo().isSkipCertificateHostnameCheck()); + } + + /** + * test instantiation with KeyStone V3 based with SSL + */ + @Test + public void testInstantiationV3WithSsl() throws Exception { + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V3_INFO); + + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + additionalParam.setInstantiationLevel(INSTANTIATION_LEVEL); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + String caCert = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/localhost.cert.pem").toURI()))); + vimInfo.setSslInsecure("false"); + vimInfo.setSslCacert(caCert); + grantResponse.setAccessInfo(accessInfo); + ArgumentCaptor<InstantiateVnfRequest> actualInstantiationRequest = ArgumentCaptor.forClass(InstantiateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdInstantiatePost(eq(VNF_ID), actualInstantiationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(instantiationOperationExecution); + //when + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualInstantiationRequest.getValue().getVims().size()); + //verify + OPENSTACKV3INFO actualVim = (OPENSTACKV3INFO) actualInstantiationRequest.getValue().getVims().get(0); + assertEquals(VIM_ID, actualVim.getId()); + assertEquals(VimInfo.VimInfoTypeEnum.OPENSTACK_V3_INFO, actualVim.getVimInfoType()); + assertEquals(Boolean.valueOf(parseBoolean(vimInfo.getSslInsecure())), actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertEquals("cloudUrl", actualVim.getInterfaceInfo().getEndpoint()); + //FIXME assertEquals();actualVim.getInterfaceInfo().getTrustedCertificates()); + assertEquals("vimPassword", actualVim.getAccessInfo().getPassword()); + assertEquals("regionId", actualVim.getAccessInfo().getRegion()); + assertEquals("myTenant", actualVim.getAccessInfo().getProject()); + assertEquals("myDomain", actualVim.getAccessInfo().getDomain()); + assertEquals("vimUsername", actualVim.getAccessInfo().getUsername()); + assertEquals(StoreLoader.getCertifacates(caCert).iterator().next(), new String(actualVim.getInterfaceInfo().getTrustedCertificates().get(0))); + assertTrue(!actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertTrue(!actualVim.getInterfaceInfo().isSkipCertificateHostnameCheck()); + } + + /** + * test instantiation with vcloud + */ + @Test + public void testInstantiationVcloud() throws Exception { + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.VMWARE_VCLOUD_INFO); + + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + additionalParam.setInstantiationLevel(INSTANTIATION_LEVEL); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + grantResponse.setAccessInfo(accessInfo); + ArgumentCaptor<InstantiateVnfRequest> actualInstantiationRequest = ArgumentCaptor.forClass(InstantiateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdInstantiatePost(eq(VNF_ID), actualInstantiationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(instantiationOperationExecution); + //when + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualInstantiationRequest.getValue().getVims().size()); + //verify + VMWAREVCLOUDINFO actualVim = (VMWAREVCLOUDINFO) actualInstantiationRequest.getValue().getVims().get(0); + assertEquals(VIM_ID, actualVim.getId()); + assertEquals(VimInfo.VimInfoTypeEnum.VMWARE_VCLOUD_INFO, actualVim.getVimInfoType()); + assertEquals(Boolean.valueOf(parseBoolean(vimInfo.getSslInsecure())), actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertEquals("cloudUrl", actualVim.getInterfaceInfo().getEndpoint()); + //FIXME assertEquals();actualVim.getInterfaceInfo().getTrustedCertificates()); + assertEquals("vimPassword", actualVim.getAccessInfo().getPassword()); + assertEquals("regionId", actualVim.getAccessInfo().getOrganization()); + assertEquals("vimUsername", actualVim.getAccessInfo().getUsername()); + assertTrue(actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertTrue(actualVim.getInterfaceInfo().isSkipCertificateHostnameCheck()); + } + + /** + * test instantiation with vCloud with SSL + */ + @Test + public void testInstantiationVcloudWithSsl() throws Exception { + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.VMWARE_VCLOUD_INFO); + + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + additionalParam.setInstantiationLevel(INSTANTIATION_LEVEL); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + String caCert = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/localhost.cert.pem").toURI()))); + vimInfo.setSslInsecure("false"); + vimInfo.setSslCacert(caCert); + grantResponse.setAccessInfo(accessInfo); + ArgumentCaptor<InstantiateVnfRequest> actualInstantiationRequest = ArgumentCaptor.forClass(InstantiateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdInstantiatePost(eq(VNF_ID), actualInstantiationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(instantiationOperationExecution); + //when + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualInstantiationRequest.getValue().getVims().size()); + //verify + VMWAREVCLOUDINFO actualVim = (VMWAREVCLOUDINFO) actualInstantiationRequest.getValue().getVims().get(0); + assertEquals(VIM_ID, actualVim.getId()); + assertEquals(VimInfo.VimInfoTypeEnum.VMWARE_VCLOUD_INFO, actualVim.getVimInfoType()); + assertEquals(Boolean.valueOf(parseBoolean(vimInfo.getSslInsecure())), actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertEquals("cloudUrl", actualVim.getInterfaceInfo().getEndpoint()); + //FIXME assertEquals();actualVim.getInterfaceInfo().getTrustedCertificates()); + assertEquals("vimPassword", actualVim.getAccessInfo().getPassword()); + assertEquals("regionId", actualVim.getAccessInfo().getOrganization()); + assertEquals("vimUsername", actualVim.getAccessInfo().getUsername()); + assertEquals(StoreLoader.getCertifacates(caCert).iterator().next(), new String(actualVim.getInterfaceInfo().getTrustedCertificates().get(0))); + assertTrue(!actualVim.getInterfaceInfo().isSkipCertificateVerification()); + assertTrue(!actualVim.getInterfaceInfo().isSkipCertificateHostnameCheck()); + } + + /** + * test failure in the instantiation request marks the job to be finished in job manager + */ + @Test + public void testFailureInTheInstantiationRequest() throws Exception { + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + additionalParam.setInstantiationLevel(INSTANTIATION_LEVEL); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + grantResponse.setAccessInfo(accessInfo); + ArgumentCaptor<InstantiateVnfRequest> actualInstantiationRequest = ArgumentCaptor.forClass(InstantiateVnfRequest.class); + ApiException expectedException = new ApiException(); + when(vnfApi.vnfsVnfInstanceIdInstantiatePost(eq(VNF_ID), actualInstantiationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenThrow(expectedException); + + //when + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + //verfiy + waitForJobToFinishInJobManager(finished); + assertEquals(VNF_ID, response.getVnfInstanceId()); + assertEquals(JOB_ID, response.getJobId()); + verify(logger).error("Unable to instantiate VNF with myVnfId identifier", expectedException); + } + + /** + * instantiation fails if VF-C does not send vim identifier in grant response + */ + @Test + public void testVfcFailsToSendVimId() throws Exception { + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + additionalParam.setInstantiationLevel(INSTANTIATION_LEVEL); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + //grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + String caCert = new String(readAllBytes(Paths.get(TestVfcGrantManager.class.getResource("/unittests/localhost.cert.pem").toURI()))); + vimInfo.setSslInsecure("false"); + vimInfo.setSslCacert(caCert); + grantResponse.setAccessInfo(accessInfo); + ArgumentCaptor<InstantiateVnfRequest> actualInstantiationRequest = ArgumentCaptor.forClass(InstantiateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdInstantiatePost(eq(VNF_ID), actualInstantiationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(instantiationOperationExecution); + //when + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + waitForJobToFinishInJobManager(finished); + assertEquals(0, actualInstantiationRequest.getAllValues().size()); + //verify + verify(logger).error("VF-C did not send VIM identifier in grant response"); + + } + + /** + * test operation execution polling is retried in case of failures + */ + @Test + public void testFailureInTheOperationExecutionPollingDuringInstantiationRequest() throws Exception { + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + additionalParam.setInstantiationLevel(INSTANTIATION_LEVEL); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + grantResponse.setAccessInfo(accessInfo); + ArgumentCaptor<InstantiateVnfRequest> actualInstantiationRequest = ArgumentCaptor.forClass(InstantiateVnfRequest.class); + ApiException expectedException = new ApiException(); + List<ApiException> polling = new ArrayList<>(); + when(vnfApi.vnfsVnfInstanceIdInstantiatePost(eq(VNF_ID), actualInstantiationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(instantiationOperationExecution); + when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).then(new Answer<List<OperationExecution>>() { + @Override + public List<OperationExecution> answer(InvocationOnMock invocation) throws Throwable { + if (polling.size() > 2) { + return operationExecutions; + } + ApiException apiException = new ApiException(); + polling.add(apiException); + throw apiException; + } + }); + //when + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + //verfiy + waitForJobToFinishInJobManager(finished); + assertEquals(VNF_ID, response.getVnfInstanceId()); + assertEquals(JOB_ID, response.getJobId()); + assertEquals(3, polling.size()); + for (ApiException e : polling) { + verify(logger).warn("Unable to retrieve operations details", e); + } + verify(systemFunctions, Mockito.times(3)).sleep(5000); + } + + /** + * failure in VNF creation is logged an proagated + */ + @Test + public void failureInVnfCreationIsPropagated() throws Exception { + //given + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + + ApiException expectedException = new ApiException(); + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenThrow(expectedException); + //when + try { + lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + //verify + fail(); + } catch (RuntimeException e) { + assertEquals(expectedException, e.getCause()); + verify(logger).error("Unable to create the VNF", expectedException); + } + } + + /** + * failure in updating the modifyable attributes of the VNF is logged an proagated + */ + @Test + public void failureInVnfModificationIsPropagated() throws Exception { + //given + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + + ApiException expectedException = new ApiException(); + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + when(vnfApi.vnfsVnfInstanceIdPatch(eq(VNF_ID), actualVnfModifyRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenThrow(expectedException); + + //when + try { + lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + //verify + fail(); + } catch (RuntimeException e) { + assertEquals(expectedException, e.getCause().getCause()); + verify(logger).error("Unable to set the onapCsarId property on the VNF", expectedException); + } + } + + /** + * if the VIM info can not be queried the VNF is not instantiated and + * error propagated through job + */ + @Test + public void testFailureInQueryVimInfo() throws Exception { + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + when(vnfApi.vnfsPost(createRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenReturn(vnfInfo); + when(vfcGrantManager.requestGrantForInstantiate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, INSTANTIATION_LEVEL, cbamVnfdContent, JOB_ID)).thenReturn(grantResponse); + grantResponse.setVimId(VIM_ID); + GrantVNFResponseVimAccessInfo accessInfo = new GrantVNFResponseVimAccessInfo(); + accessInfo.setTenant(TENANT); + grantResponse.setAccessInfo(accessInfo); + + when(vimInfoProvider.getVimInfo(VIM_ID)).thenThrow(new RuntimeException()); + //when + lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + verify(vnfApi, never()).vnfsVnfInstanceIdInstantiatePost(Mockito.any(), Mockito.any(), Mockito.any()); + } + + /** + * test termination basic success scenario + * - the VNF is not deleted before the notifications are processed + */ + @Test + public void testTermination() throws Exception { + //given + VnfTerminateRequest terminationRequest = new VnfTerminateRequest(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + vnfInfo.setInstantiationState(InstantiationState.INSTANTIATED); + vnfInfo.setOperationExecutions(operationExecutions); + VnfProperty vnfdId = new VnfProperty(); + vnfdId.setName(LifecycleManager.ONAP_CSAR_ID); + vnfdId.setValue(ONAP_CSAR_ID); + vnfInfo.getExtensions().add(vnfdId); + ArgumentCaptor<TerminateVnfRequest> actualTerminationRequest = ArgumentCaptor.forClass(TerminateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdTerminatePost(eq(VNF_ID), actualTerminationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenAnswer(new Answer<OperationExecution>() { + @Override + public OperationExecution answer(InvocationOnMock invocation) throws Throwable { + OperationExecution terminationOperation = new OperationExecution(); + terminationOperation.setId("terminationId"); + operationExecutions.add(terminationOperation); + terminationOperation.setStatus(OperationStatus.FINISHED); + return terminationOperation; + } + }); + JsonElement instantiationParameters = new JsonParser().parse("{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] } "); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet("operationExecutionId", NOKIA_LCM_API_VERSION)).thenReturn(instantiationParameters); + //when + JobInfo jobInfo = lifecycleManager.terminateVnf(VNFM_ID, VNF_ID, terminationRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualTerminationRequest.getAllValues().size()); + assertEquals(TerminationType.FORCEFUL, actualTerminationRequest.getValue().getTerminationType()); + InOrder notificationIsProcessedBeforeDeletingTheVnf = Mockito.inOrder(vfcGrantManager, notificationManager, vnfApi); + notificationIsProcessedBeforeDeletingTheVnf.verify(vfcGrantManager).requestGrantForTerminate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, vnfInfo, JOB_ID); + notificationIsProcessedBeforeDeletingTheVnf.verify(notificationManager).waitForTerminationToBeProcessed("terminationId"); + notificationIsProcessedBeforeDeletingTheVnf.verify(vnfApi).vnfsVnfInstanceIdDelete(VNF_ID, NOKIA_LCM_API_VERSION); + verify(jobManager).spawnJob(VNF_ID, restResponse); + } + + /** + * test termination of a non instantiated VNF + * - the VNF is not terminated (only deleted) + */ + @Test + public void testTerminationOfNonInstantiated() throws Exception { + //given + VnfTerminateRequest terminationRequest = new VnfTerminateRequest(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + vnfInfo.setInstantiationState(InstantiationState.NOT_INSTANTIATED); + vnfInfo.setOperationExecutions(operationExecutions); + VnfProperty vnfdId = new VnfProperty(); + vnfdId.setName(LifecycleManager.ONAP_CSAR_ID); + vnfdId.setValue(ONAP_CSAR_ID); + vnfInfo.getExtensions().add(vnfdId); + JsonElement instantiationParameters = new JsonParser().parse("{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] } "); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet("operationExecutionId", NOKIA_LCM_API_VERSION)).thenReturn(instantiationParameters); + //when + JobInfo jobInfo = lifecycleManager.terminateVnf(VNFM_ID, VNF_ID, terminationRequest, restResponse); + //verify + boolean deleted = false; + while (!deleted) { + try { + Mockito. + verify(vnfApi).vnfsVnfInstanceIdDelete(VNF_ID, NOKIA_LCM_API_VERSION); + deleted = true; + } catch (Error e) { + } + } + verify(vfcGrantManager, never()).requestGrantForTerminate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, vnfInfo, JOB_ID); + verify(notificationManager, never()).waitForTerminationToBeProcessed("terminationId"); + } + + /** + * test that the VNF deletion is not started before the termination finishes + */ + @Test + public void testTerminationOperationIsOutwaited() throws Exception { + //given + VnfTerminateRequest terminationRequest = new VnfTerminateRequest(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + vnfInfo.setInstantiationState(InstantiationState.INSTANTIATED); + vnfInfo.setOperationExecutions(operationExecutions); + VnfProperty vnfdId = new VnfProperty(); + vnfdId.setName(LifecycleManager.ONAP_CSAR_ID); + vnfdId.setValue(ONAP_CSAR_ID); + vnfInfo.getExtensions().add(vnfdId); + ArgumentCaptor<TerminateVnfRequest> actualTerminationRequest = ArgumentCaptor.forClass(TerminateVnfRequest.class); + OperationExecution terminationOperation = new OperationExecution(); + when(vnfApi.vnfsVnfInstanceIdTerminatePost(eq(VNF_ID), actualTerminationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenAnswer(new Answer<OperationExecution>() { + @Override + public OperationExecution answer(InvocationOnMock invocation) throws Throwable { + terminationOperation.setId("terminationId"); + operationExecutions.add(terminationOperation); + terminationOperation.setStatus(OperationStatus.STARTED); + return terminationOperation; + } + }); + JsonElement instantiationParameters = new JsonParser().parse("{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] } "); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet("operationExecutionId", NOKIA_LCM_API_VERSION)).thenReturn(instantiationParameters); + Set<Integer> calls = new HashSet<>(); + when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenAnswer(new Answer<List<OperationExecution>>() { + @Override + public List<OperationExecution> answer(InvocationOnMock invocation) throws Throwable { + if (calls.size() == 1000) { + terminationOperation.setStatus(OperationStatus.FINISHED); + } + calls.add(calls.size()); + return operationExecutions; + } + }); + //when + JobInfo jobInfo = lifecycleManager.terminateVnf(VNFM_ID, VNF_ID, terminationRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + verify(vnfApi, times(1001)).vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION); + verify(systemFunctions, times(1000)).sleep(5000); + } + + + /** + * test that failured during waiting for the operation to finish is tolerated (idefineiatelly) + */ + @Test + public void testTerminationOperationIsOutwaitedWithErrors() throws Exception { + //given + VnfTerminateRequest terminationRequest = new VnfTerminateRequest(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + vnfInfo.setInstantiationState(InstantiationState.INSTANTIATED); + vnfInfo.setOperationExecutions(operationExecutions); + VnfProperty vnfdId = new VnfProperty(); + vnfdId.setName(LifecycleManager.ONAP_CSAR_ID); + vnfdId.setValue(ONAP_CSAR_ID); + vnfInfo.getExtensions().add(vnfdId); + ArgumentCaptor<TerminateVnfRequest> actualTerminationRequest = ArgumentCaptor.forClass(TerminateVnfRequest.class); + OperationExecution terminationOperation = new OperationExecution(); + when(vnfApi.vnfsVnfInstanceIdTerminatePost(eq(VNF_ID), actualTerminationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenAnswer(new Answer<OperationExecution>() { + @Override + public OperationExecution answer(InvocationOnMock invocation) throws Throwable { + terminationOperation.setId("terminationId"); + operationExecutions.add(terminationOperation); + terminationOperation.setStatus(OperationStatus.STARTED); + return terminationOperation; + } + }); + JsonElement instantiationParameters = new JsonParser().parse("{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] } "); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet("operationExecutionId", NOKIA_LCM_API_VERSION)).thenReturn(instantiationParameters); + Set<Integer> calls = new HashSet<>(); + List<ApiException> expectedExceptions = new ArrayList<>(); + when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenAnswer(new Answer<List<OperationExecution>>() { + @Override + public List<OperationExecution> answer(InvocationOnMock invocation) throws Throwable { + if (calls.size() >= 100) { + terminationOperation.setStatus(OperationStatus.FINISHED); + return operationExecutions; + } + calls.add(calls.size()); + ApiException apiException = new ApiException(); + expectedExceptions.add(apiException); + throw apiException; + } + }); + //when + JobInfo jobInfo = lifecycleManager.terminateVnf(VNFM_ID, VNF_ID, terminationRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + verify(vnfApi, times(101)).vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION); + verify(systemFunctions, times(100)).sleep(5000); + for (ApiException expectedException : expectedExceptions) { + verify(logger).warn("Unable to retrieve operations details", expectedException); + } + } + + /** + * test gracefull termination + */ + @Test + public void testGracefullTermination() throws Exception { + //given + VnfTerminateRequest terminationRequest = new VnfTerminateRequest(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + terminationRequest.setTerminationType(VnfTerminationType.GRACEFUL); + terminationRequest.setGracefulTerminationTimeout("1234"); + vnfInfo.setInstantiationState(InstantiationState.INSTANTIATED); + vnfInfo.setOperationExecutions(operationExecutions); + VnfProperty vnfdId = new VnfProperty(); + vnfdId.setName(LifecycleManager.ONAP_CSAR_ID); + vnfdId.setValue(ONAP_CSAR_ID); + vnfInfo.getExtensions().add(vnfdId); + ArgumentCaptor<TerminateVnfRequest> actualTerminationRequest = ArgumentCaptor.forClass(TerminateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdTerminatePost(eq(VNF_ID), actualTerminationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenAnswer(new Answer<OperationExecution>() { + @Override + public OperationExecution answer(InvocationOnMock invocation) throws Throwable { + OperationExecution terminationOperation = new OperationExecution(); + terminationOperation.setId("terminationId"); + operationExecutions.add(terminationOperation); + terminationOperation.setStatus(OperationStatus.FINISHED); + return terminationOperation; + } + }); + doAnswer(invocation -> { + verify(jobManager, Mockito.never()).jobFinished(JOB_ID); + return null; + }).when(vnfApi).vnfsVnfInstanceIdDelete(VNF_ID, NOKIA_LCM_API_VERSION); + JsonElement instantiationParameters = new JsonParser().parse("{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] } "); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet("operationExecutionId", NOKIA_LCM_API_VERSION)).thenReturn(instantiationParameters); + //when + JobInfo jobInfo = lifecycleManager.terminateVnf(VNFM_ID, VNF_ID, terminationRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualTerminationRequest.getAllValues().size()); + assertEquals(TerminationType.GRACEFUL, actualTerminationRequest.getValue().getTerminationType()); + assertEquals(Integer.valueOf(1234), actualTerminationRequest.getValue().getGracefulTerminationTimeout()); + InOrder notificationIsProcessedBeforeDeletingTheVnf = Mockito.inOrder(vfcGrantManager, notificationManager, vnfApi); + notificationIsProcessedBeforeDeletingTheVnf.verify(vfcGrantManager).requestGrantForTerminate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, vnfInfo, JOB_ID); + notificationIsProcessedBeforeDeletingTheVnf.verify(notificationManager).waitForTerminationToBeProcessed("terminationId"); + notificationIsProcessedBeforeDeletingTheVnf.verify(vnfApi).vnfsVnfInstanceIdDelete(VNF_ID, NOKIA_LCM_API_VERSION); + } + + /** + * instantiation with missing ONAP csarId to instantiation extra param result in failure + */ + @Test + public void testMissingVnfParameters() throws Exception { + //given + VnfInstantiateRequest instantiationRequest = prepareInstantiationRequest(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + String src = "{ \"inputs\" : { \"vnfs\" : { \"" + ONAP_CSAR_ID + "invalid" + "\" : {}}}, \"vimId\" : \"" + VIM_ID + "\"}"; + instantiationRequest.setAdditionalParam(new JsonParser().parse(src)); + //when + try { + VnfInstantiateResponse response = lifecycleManager.instantiate(VNFM_ID, instantiationRequest, restResponse); + fail(); + } catch (Exception e) { + assertEquals("The additional parameter section does not contain setting for VNF with myOnapCsarId CSAR id", e.getMessage()); + verify(logger).error("The additional parameter section does not contain setting for VNF with myOnapCsarId CSAR id"); + } + } + + /** + * test explicit forceful termination + */ + @Test + public void testExplicitForcefulTermination() throws Exception { + //given + VnfTerminateRequest terminationRequest = new VnfTerminateRequest(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + terminationRequest.setTerminationType(VnfTerminationType.FORCEFUL); + terminationRequest.setGracefulTerminationTimeout("1234"); + vnfInfo.setInstantiationState(InstantiationState.INSTANTIATED); + vnfInfo.setOperationExecutions(operationExecutions); + VnfProperty vnfdId = new VnfProperty(); + vnfdId.setName(LifecycleManager.ONAP_CSAR_ID); + vnfdId.setValue(ONAP_CSAR_ID); + vnfInfo.getExtensions().add(vnfdId); + ArgumentCaptor<TerminateVnfRequest> actualTerminationRequest = ArgumentCaptor.forClass(TerminateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdTerminatePost(eq(VNF_ID), actualTerminationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenAnswer(new Answer<OperationExecution>() { + @Override + public OperationExecution answer(InvocationOnMock invocation) throws Throwable { + OperationExecution terminationOperation = new OperationExecution(); + terminationOperation.setId("terminationId"); + operationExecutions.add(terminationOperation); + terminationOperation.setStatus(OperationStatus.FINISHED); + return terminationOperation; + } + }); + doAnswer(invocation -> { + verify(jobManager, Mockito.never()).jobFinished(JOB_ID); + return null; + }).when(vnfApi).vnfsVnfInstanceIdDelete(VNF_ID, NOKIA_LCM_API_VERSION); + JsonElement instantiationParameters = new JsonParser().parse("{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] } "); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet("operationExecutionId", NOKIA_LCM_API_VERSION)).thenReturn(instantiationParameters); + //when + JobInfo jobInfo = lifecycleManager.terminateVnf(VNFM_ID, VNF_ID, terminationRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualTerminationRequest.getAllValues().size()); + assertEquals(TerminationType.FORCEFUL, actualTerminationRequest.getValue().getTerminationType()); + assertNull(actualTerminationRequest.getValue().getGracefulTerminationTimeout()); + InOrder notificationIsProcessedBeforeDeletingTheVnf = Mockito.inOrder(vfcGrantManager, notificationManager, vnfApi); + notificationIsProcessedBeforeDeletingTheVnf.verify(vfcGrantManager).requestGrantForTerminate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, vnfInfo, JOB_ID); + notificationIsProcessedBeforeDeletingTheVnf.verify(notificationManager).waitForTerminationToBeProcessed("terminationId"); + notificationIsProcessedBeforeDeletingTheVnf.verify(vnfApi).vnfsVnfInstanceIdDelete(VNF_ID, NOKIA_LCM_API_VERSION); + } + + /** + * test failure in the termination workflow finishes the job + */ + @Test + public void testFailureInTheTerminationFinishesTheManagedJob() throws Exception { + //given + VnfTerminateRequest terminationRequest = new VnfTerminateRequest(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + terminationRequest.setTerminationType(VnfTerminationType.FORCEFUL); + terminationRequest.setGracefulTerminationTimeout("1234"); + vnfInfo.setInstantiationState(InstantiationState.INSTANTIATED); + vnfInfo.setOperationExecutions(operationExecutions); + VnfProperty vnfdId = new VnfProperty(); + vnfdId.setName(LifecycleManager.ONAP_CSAR_ID); + vnfdId.setValue(ONAP_CSAR_ID); + vnfInfo.getExtensions().add(vnfdId); + ArgumentCaptor<TerminateVnfRequest> actualTerminationRequest = ArgumentCaptor.forClass(TerminateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdTerminatePost(eq(VNF_ID), actualTerminationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenAnswer(new Answer<OperationExecution>() { + @Override + public OperationExecution answer(InvocationOnMock invocation) throws Throwable { + OperationExecution terminationOperation = new OperationExecution(); + terminationOperation.setId("terminationId"); + operationExecutions.add(terminationOperation); + terminationOperation.setStatus(OperationStatus.FINISHED); + return terminationOperation; + } + }); + ApiException expectedException = new ApiException(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + JsonElement instantiationParameters = new JsonParser().parse("{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] } "); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet("operationExecutionId", NOKIA_LCM_API_VERSION)).thenReturn(instantiationParameters); + //when + JobInfo jobInfo = lifecycleManager.terminateVnf(VNFM_ID, VNF_ID, terminationRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(0, actualTerminationRequest.getAllValues().size()); + Mockito.verifyZeroInteractions(vfcGrantManager); + } + + /** + * if termination fails the VNF is not deleted + */ + @Test + public void testFailedTerminationAbortsTerminationWorkflow() throws Exception { + //given + VnfTerminateRequest terminationRequest = new VnfTerminateRequest(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + vnfInfo.setInstantiationState(InstantiationState.INSTANTIATED); + vnfInfo.setOperationExecutions(operationExecutions); + VnfProperty vnfdId = new VnfProperty(); + vnfdId.setName(LifecycleManager.ONAP_CSAR_ID); + vnfdId.setValue(ONAP_CSAR_ID); + vnfInfo.getExtensions().add(vnfdId); + ArgumentCaptor<TerminateVnfRequest> actualTerminationRequest = ArgumentCaptor.forClass(TerminateVnfRequest.class); + when(vnfApi.vnfsVnfInstanceIdTerminatePost(eq(VNF_ID), actualTerminationRequest.capture(), eq(NOKIA_LCM_API_VERSION))).thenAnswer(new Answer<OperationExecution>() { + @Override + public OperationExecution answer(InvocationOnMock invocation) throws Throwable { + OperationExecution terminationOperation = new OperationExecution(); + terminationOperation.setId("terminationId"); + operationExecutions.add(terminationOperation); + terminationOperation.setStatus(OperationStatus.FAILED); + return terminationOperation; + } + }); + JsonElement instantiationParameters = new JsonParser().parse("{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] } "); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet("operationExecutionId", NOKIA_LCM_API_VERSION)).thenReturn(instantiationParameters); + //when + JobInfo jobInfo = lifecycleManager.terminateVnf(VNFM_ID, VNF_ID, terminationRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualTerminationRequest.getAllValues().size()); + assertEquals(TerminationType.FORCEFUL, actualTerminationRequest.getValue().getTerminationType()); + verify(vfcGrantManager).requestGrantForTerminate(VNFM_ID, VNF_ID, VIM_ID, ONAP_CSAR_ID, vnfInfo, JOB_ID); + verify(vnfApi, never()).vnfsVnfInstanceIdDelete(VNF_ID, NOKIA_LCM_API_VERSION); + verify(logger).error("Unable to terminate VNF the operation did not finish with success"); + } + + /** + * test VNF query basic success scenario + */ + @Test + public void testQuery() throws Exception { + vnfInfo.setDescription("myDescription"); + vnfInfo.setName("myName"); + vnfInfo.setVnfSoftwareVersion("vnfSoftwareVersion"); + vnfInfo.setVnfProvider("myProvider"); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + VnfProperty prop = new VnfProperty(); + prop.setName(LifecycleManager.ONAP_CSAR_ID); + prop.setValue(ONAP_CSAR_ID); + vnfInfo.getExtensions().add(prop); + //when + org.onap.vnfmdriver.model.VnfInfo vnf = lifecycleManager.queryVnf(VNFM_ID, VNF_ID); + //verify + assertEquals(VNF_ID, vnf.getVnfInstanceId()); + //FIXME ? (do not know what exactly the vnf version mean in core terminology) + assertEquals("vnfSoftwareVersion", vnf.getVersion()); + assertEquals(ONAP_CSAR_ID, vnf.getVnfdId()); + assertEquals("myDescription", vnf.getVnfInstanceDescription()); + assertEquals("myName", vnf.getVnfInstanceName()); + assertEquals(ONAP_CSAR_ID, vnf.getVnfPackageId()); + assertEquals("myProvider", vnf.getVnfProvider()); + //FIXME (in swagger schema ) + assertEquals("ACTIVE", vnf.getVnfStatus()); + assertEquals("Kuku", vnf.getVnfType()); + } + + /** + * error is propagated and logged if the queried VNF does not exist + */ + @Test + public void testQueryForNonExistingVnf() throws Exception { + + ApiException expectedException = new ApiException(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + //when + try { + lifecycleManager.queryVnf(VNFM_ID, VNF_ID); + //verify + fail(); + } catch (Exception e) { + verify(logger).error("Unable to query VNF (myVnfId)", expectedException); + assertEquals(expectedException, e.getCause()); + } + } + + /** + * test scale basic scenario + */ + @Test + public void testScale() throws Exception { + VnfScaleRequest scaleRequest = new VnfScaleRequest(); + scaleRequest.setNumberOfSteps("2"); + scaleRequest.setAspectId("myAspect"); + scaleRequest.setType(ScaleDirection.IN); + scaleRequest.setAdditionalParam(new JsonParser().parse("{ \"a\" : \"b\", \"c\" : \"d\" }")); + scaleOperationExecution.setStatus(OperationStatus.FINISHED); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + VnfProperty prop = new VnfProperty(); + prop.setValue(ONAP_CSAR_ID); + prop.setName(LifecycleManager.ONAP_CSAR_ID); + vnfInfo.getExtensions().add(prop); + vnfInfo.getOperationExecutions().add(instantiationOperationExecution); + String instantiationParams = "{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] }"; + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(instantiationOperationExecution.getId(), NOKIA_LCM_API_VERSION)).thenReturn(new JsonParser().parse(instantiationParams)); + //when + JobInfo job = lifecycleManager.scaleVnf(VNFM_ID, VNF_ID, scaleRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualScaleRequest.getAllValues().size()); + ScaleVnfRequest sRequest = actualScaleRequest.getValue(); + InOrder workflowOrder = Mockito.inOrder(vfcGrantManager, vnfApi); + workflowOrder.verify(vfcGrantManager).requestGrantForScale(eq(VNFM_ID), eq(VNF_ID), eq(VIM_ID), eq(ONAP_CSAR_ID), eq(scaleRequest), eq(JOB_ID)); + workflowOrder.verify(vnfApi).vnfsVnfInstanceIdScalePost(VNF_ID, sRequest, NOKIA_LCM_API_VERSION); + assertEquals("myAspect", sRequest.getAspectId()); + assertEquals(com.nokia.cbam.lcm.v32.model.ScaleDirection.IN, sRequest.getType()); + assertEquals(Integer.valueOf(2), sRequest.getNumberOfSteps()); + assertTrue("{\"jobId\":\"myJobId\",\"a\":\"b\"}".equals(new Gson().toJson(sRequest.getAdditionalParams())) || "{\"a\":\"b\",\"jobId\":\"myJobId\"}".equals(new Gson().toJson(sRequest.getAdditionalParams()))); + verify(jobManager).spawnJob(VNF_ID, restResponse); + } + + /** + * test scale out basic scenario + */ + @Test + public void testScaleOut() throws Exception { + VnfScaleRequest scaleRequest = new VnfScaleRequest(); + scaleRequest.setNumberOfSteps("2"); + scaleRequest.setAspectId("myAspect"); + scaleRequest.setType(ScaleDirection.OUT); + scaleRequest.setAdditionalParam(new JsonParser().parse("{ \"a\" : \"b\" }")); + scaleOperationExecution.setStatus(OperationStatus.FINISHED); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + VnfProperty prop = new VnfProperty(); + prop.setValue(ONAP_CSAR_ID); + prop.setName(LifecycleManager.ONAP_CSAR_ID); + vnfInfo.getExtensions().add(prop); + vnfInfo.getOperationExecutions().add(instantiationOperationExecution); + String instantiationParams = "{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] }"; + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(instantiationOperationExecution.getId(), NOKIA_LCM_API_VERSION)).thenReturn(new JsonParser().parse(instantiationParams)); + //when + JobInfo job = lifecycleManager.scaleVnf(VNFM_ID, VNF_ID, scaleRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualScaleRequest.getAllValues().size()); + ScaleVnfRequest sRequest = actualScaleRequest.getValue(); + InOrder workflowOrder = Mockito.inOrder(vfcGrantManager, vnfApi); + workflowOrder.verify(vfcGrantManager).requestGrantForScale(eq(VNFM_ID), eq(VNF_ID), eq(VIM_ID), eq(ONAP_CSAR_ID), eq(scaleRequest), eq(JOB_ID)); + workflowOrder.verify(vnfApi).vnfsVnfInstanceIdScalePost(VNF_ID, sRequest, NOKIA_LCM_API_VERSION); + assertEquals("myAspect", sRequest.getAspectId()); + assertEquals(com.nokia.cbam.lcm.v32.model.ScaleDirection.OUT, sRequest.getType()); + assertEquals(Integer.valueOf(2), sRequest.getNumberOfSteps()); + assertTrue("{\"jobId\":\"myJobId\",\"a\":\"b\"}".equals(new Gson().toJson(sRequest.getAdditionalParams())) || "{\"a\":\"b\",\"jobId\":\"myJobId\"}".equals(new Gson().toJson(sRequest.getAdditionalParams()))); + } + + /** + * test scale operation is out waited + */ + @Test + public void testScaleOutwait() throws Exception { + VnfScaleRequest scaleRequest = new VnfScaleRequest(); + scaleRequest.setNumberOfSteps("2"); + scaleRequest.setAspectId("myAspect"); + scaleRequest.setType(ScaleDirection.IN); + scaleRequest.setAdditionalParam(new JsonParser().parse("{ \"a\" : \"b\" }")); + scaleOperationExecution.setStatus(OperationStatus.STARTED); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + VnfProperty prop = new VnfProperty(); + prop.setValue(ONAP_CSAR_ID); + prop.setName(LifecycleManager.ONAP_CSAR_ID); + vnfInfo.getExtensions().add(prop); + vnfInfo.getOperationExecutions().add(instantiationOperationExecution); + String instantiationParams = "{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] }"; + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(instantiationOperationExecution.getId(), NOKIA_LCM_API_VERSION)).thenReturn(new JsonParser().parse(instantiationParams)); + List<ApiException> expectedExceptions = new ArrayList<>(); + when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenAnswer((Answer<List<OperationExecution>>) invocation -> { + if (expectedExceptions.size() >= 100) { + scaleOperationExecution.setStatus(OperationStatus.FINISHED); + return operationExecutions; + } + ApiException apiException = new ApiException(); + expectedExceptions.add(apiException); + // when(operationExecutionApi.operationExecutionsOperationExecutionIdGet(scaleOperationExecution.getId(), NOKIA_LCM_API_VERSION)).thenReturn(scaleOperationExecution); + throw apiException; + }); + + //when + JobInfo job = lifecycleManager.scaleVnf(VNFM_ID, VNF_ID, scaleRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(100, expectedExceptions.size()); + for (ApiException expectedException : expectedExceptions) { + verify(logger).warn("Unable to retrieve operations details", expectedException); + } + verify(systemFunctions, times(100)).sleep(5000); + } + + /** + * test scale failure propagation + */ + @Test + public void testScaleFailurePropagation() throws Exception { + ApiException expectedException = new ApiException(); + VnfScaleRequest scaleRequest = new VnfScaleRequest(); + scaleRequest.setNumberOfSteps("2"); + scaleRequest.setAspectId("myAspect"); + scaleRequest.setType(ScaleDirection.IN); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + //when + lifecycleManager.scaleVnf(VNFM_ID, VNF_ID, scaleRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + verify(logger).error("Unable to scale VNF with myVnfId identifier", expectedException); + } + + /** + * test heal basic scenario + */ + @Test + public void testHeal() throws Exception { + VnfHealRequest healRequest = new VnfHealRequest(); + healRequest.setAction("myAction"); + VnfHealRequestAffectedvm affectedVm = new VnfHealRequestAffectedvm(); + affectedVm.setVmname("vmName"); + healRequest.setAffectedvm(affectedVm); + healOperationExecution.setStatus(OperationStatus.FINISHED); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + VnfProperty prop = new VnfProperty(); + prop.setValue(ONAP_CSAR_ID); + prop.setName(LifecycleManager.ONAP_CSAR_ID); + vnfInfo.getExtensions().add(prop); + vnfInfo.getOperationExecutions().add(instantiationOperationExecution); + String instantiationParams = "{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] }"; + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(instantiationOperationExecution.getId(), NOKIA_LCM_API_VERSION)).thenReturn(new JsonParser().parse(instantiationParams)); + //when + JobInfo job = lifecycleManager.healVnf(VNFM_ID, VNF_ID, healRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(1, actualHealRequest.getAllValues().size()); + HealVnfRequest sRequest = actualHealRequest.getValue(); + InOrder workflowOrder = Mockito.inOrder(vfcGrantManager, vnfApi); + workflowOrder.verify(vfcGrantManager).requestGrantForHeal(eq(VNFM_ID), eq(VNF_ID), eq(VIM_ID), eq(ONAP_CSAR_ID), eq(healRequest), eq(JOB_ID)); + workflowOrder.verify(vnfApi).vnfsVnfInstanceIdHealPost(VNF_ID, sRequest, NOKIA_LCM_API_VERSION); + JsonObject root = new Gson().toJsonTree(sRequest.getAdditionalParams()).getAsJsonObject(); + assertEquals("myAction", root.get("action").getAsString()); + assertEquals("vmName", root.get("vmName").getAsString()); + assertEquals(JOB_ID, root.get("jobId").getAsString()); + verify(jobManager).spawnJob(VNF_ID, restResponse); + } + + /** + * test heal operation is out waited + */ + @Test + public void testHealOutwait() throws Exception { + VnfHealRequest healRequest = new VnfHealRequest(); + healRequest.setAction("myAction"); + VnfHealRequestAffectedvm affectedVm = new VnfHealRequestAffectedvm(); + affectedVm.setVmname("vmName"); + healRequest.setAffectedvm(affectedVm); + healOperationExecution.setStatus(OperationStatus.FINISHED); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnfInfo); + VnfProperty prop = new VnfProperty(); + prop.setValue(ONAP_CSAR_ID); + prop.setName(LifecycleManager.ONAP_CSAR_ID); + vnfInfo.getExtensions().add(prop); + vnfInfo.getOperationExecutions().add(instantiationOperationExecution); + String instantiationParams = "{ \"vims\" : [ { \"id\" : \"" + VIM_ID + "\" } ] }"; + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(instantiationOperationExecution.getId(), NOKIA_LCM_API_VERSION)).thenReturn(new JsonParser().parse(instantiationParams)); + List<ApiException> expectedExceptions = new ArrayList<>(); + when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenAnswer(new Answer<List<OperationExecution>>() { + @Override + public List<OperationExecution> answer(InvocationOnMock invocation) throws Throwable { + if (expectedExceptions.size() >= 100) { + scaleOperationExecution.setStatus(OperationStatus.FINISHED); + return operationExecutions; + } + ApiException apiException = new ApiException(); + expectedExceptions.add(apiException); + throw apiException; + } + }); + //when + JobInfo job = lifecycleManager.healVnf(VNFM_ID, VNF_ID, healRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + assertEquals(100, expectedExceptions.size()); + for (ApiException expectedException : expectedExceptions) { + verify(logger).warn("Unable to retrieve operations details", expectedException); + } + verify(systemFunctions, times(100)).sleep(5000); + } + + /** + * failure in heal propagates in error + */ + @Test + public void testHealFailurePropagation() throws Exception { + ApiException expectedException = new ApiException(); + VnfHealRequest healRequest = new VnfHealRequest(); + healRequest.setAction("myAction"); + VnfHealRequestAffectedvm affectedVm = new VnfHealRequestAffectedvm(); + affectedVm.setVmname("vmName"); + healRequest.setAffectedvm(affectedVm); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + //when + JobInfo job = lifecycleManager.healVnf(VNFM_ID, VNF_ID, healRequest, restResponse); + //verify + waitForJobToFinishInJobManager(finished); + verify(logger).error("Unable to heal VNF with myVnfId identifier", expectedException); + } + + private void waitForJobToFinishInJobManager(Set<Boolean> finished) throws InterruptedException { + while (finished.size() == 0) { + systemFunctions().sleep(100); + } + } + + private VnfInstantiateRequest prepareInstantiationRequest(VimInfo.VimInfoTypeEnum cloudType) { + VnfInstantiateRequest instantiationRequest = new VnfInstantiateRequest(); + instantiationRequest.setVnfPackageId(ONAP_CSAR_ID); + instantiationRequest.setVnfDescriptorId(ONAP_CSAR_ID); + instantiationRequest.setVnfInstanceDescription("myDescription"); + instantiationRequest.setVnfInstanceName("vnfName"); + additionalParam.setInstantiationLevel("level1"); + switch (cloudType) { + case OPENSTACK_V2_INFO: + additionalParam.setVimType(VimInfo.VimInfoTypeEnum.OPENSTACK_V2_INFO); + break; + case OPENSTACK_V3_INFO: + additionalParam.setVimType(VimInfo.VimInfoTypeEnum.OPENSTACK_V3_INFO); + additionalParam.setDomain("myDomain"); + break; + case VMWARE_VCLOUD_INFO: + additionalParam.setVimType(VimInfo.VimInfoTypeEnum.VMWARE_VCLOUD_INFO); + break; + default: + additionalParam.setVimType(VimInfo.VimInfoTypeEnum.OTHER_VIM_INFO); + } + + Map<String, List<NetworkAddress>> exteranalConnectionPointAddresses = new HashMap<>(); + exteranalConnectionPointAddresses.put("ecp1", new ArrayList<>()); + NetworkAddress networkAddress = new NetworkAddress(); + networkAddress.setIp("1.2.3.4"); + networkAddress.setMac("mac"); + networkAddress.setSubnetId("subnetId"); + exteranalConnectionPointAddresses.get("ecp1").add(networkAddress); + additionalParam.setExternalConnectionPointAddresses(exteranalConnectionPointAddresses); + VimComputeResourceFlavour flavor = new VimComputeResourceFlavour(); + flavor.setResourceId("flavourProviderId"); + flavor.setVimId(VIM_ID); + flavor.setVnfdVirtualComputeDescId("virtualComputeDescId"); + additionalParam.getComputeResourceFlavours().add(flavor); + ExtVirtualLinkData evl = new ExtVirtualLinkData(); + evl.setResourceId("networkProviderId1"); + evl.setVimId(VIM_ID); + evl.setExtVirtualLinkId("evlId1"); + VnfExtCpData ecp2 = new VnfExtCpData(); + ecp2.setCpdId("cpdId3"); + ecp2.setAddresses(new ArrayList<>()); + ecp2.getAddresses().add(networkAddress); + ecp2.setNumDynamicAddresses(2); + evl.getExtCps().add(ecp2); + additionalParam.getExtVirtualLinks().add(evl); + externalVirtualLink.setCpdId("myCpdId"); + externalVirtualLink.setResourceId("myNetworkProviderId"); + externalVirtualLink.setVlInstanceId("myEVlId"); + externalVirtualLink.setResourceSubnetId("notUsedSubnetId"); + instantiationRequest.setExtVirtualLink(new ArrayList<>()); + instantiationRequest.getExtVirtualLink().add(externalVirtualLink); + additionalParam.getExtManagedVirtualLinks().add(extManVl); + ZoneInfo zone = new ZoneInfo(); + zone.setId("zoneId"); + zone.setResourceId("zoneProviderId"); + zone.setVimId(VIM_ID); + additionalParam.getZones().add(zone); + VimSoftwareImage image = new VimSoftwareImage(); + image.setResourceId("imageProviderId"); + image.setVimId(VIM_ID); + image.setVnfdSoftwareImageId("imageId"); + additionalParam.getSoftwareImages().add(image); + additionalParam.setAdditionalParams(new JsonParser().parse("{ \"a\" : \"b\" }")); + String params = new Gson().toJson(additionalParam); + String src = "{ \"inputs\" : { \"vnfs\" : { \"" + ONAP_CSAR_ID + "\" : " + params + "}}, \"vimId\" : \"" + VIM_ID + "\"}"; + instantiationRequest.setAdditionalParam(new JsonParser().parse(src)); + return instantiationRequest; + } + + /** + * Test vimId decomposition + */ + @Test + public void testVimIdSplitting() { + assertEquals("regionId", LifecycleManager.getRegionName("cloudOwner_regionId")); + assertEquals("cloudOwner", LifecycleManager.getCloudOwner("cloudOwner_regionId")); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestSelfRegistrationManager.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestSelfRegistrationManager.java new file mode 100644 index 00000000..79ec1a83 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/TestSelfRegistrationManager.java @@ -0,0 +1,345 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm; + +import com.google.common.collect.Lists; +import com.nokia.cbam.lcn.v32.ApiException; +import com.nokia.cbam.lcn.v32.model.CreateSubscriptionRequest; +import com.nokia.cbam.lcn.v32.model.Subscription; +import com.nokia.cbam.lcn.v32.model.SubscriptionAuthentication; +import com.nokia.cbam.lcn.v32.model.VnfNotificationType; +import org.junit.Before; +import org.junit.Test; +import org.mockito.*; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.onap.msb.sdk.discovery.common.RouteException; +import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo; +import org.onap.msb.sdk.discovery.entity.MicroServiceInfo; +import org.onap.msb.sdk.discovery.entity.Node; +import org.onap.msb.sdk.discovery.entity.RouteResult; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core.SelfRegistrationManager; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; + +import static com.nokia.cbam.lcn.v32.model.OperationType.*; +import static junit.framework.TestCase.*; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCN_API_VERSION; +import static org.springframework.test.util.ReflectionTestUtils.setField; + +public class TestSelfRegistrationManager extends TestBase { + @Mock + private JobManager jobManager; + private List<Subscription> subscriptions = new ArrayList<>(); + private ArgumentCaptor<MicroServiceInfo> registeredMicroservice = ArgumentCaptor.forClass(MicroServiceInfo.class); + private ArgumentCaptor<CreateSubscriptionRequest> subscriptionToCreate = ArgumentCaptor.forClass(CreateSubscriptionRequest.class); + @InjectMocks + private SelfRegistrationManager selfRegistrationManager; + + @Before + public void initMocks() throws Exception { + setField(SelfRegistrationManager.class, "logger", logger); + when(lcnApi.subscriptionsGet(NOKIA_LCN_API_VERSION)).thenReturn(subscriptions); + when(driverProperties.getVnfmId()).thenReturn(VNFM_ID); + setField(selfRegistrationManager, "driverMsbExternalIp", "1.2.3.4"); + setField(selfRegistrationManager, "driverVnfmExternalIp", "5.6.7.8"); + setField(selfRegistrationManager, "driverPort", "12345"); + Subscription unknownSubscription = new Subscription(); + unknownSubscription.setId(UUID.randomUUID().toString()); + unknownSubscription.setCallbackUrl("unknown"); + subscriptions.add(unknownSubscription); + } + + /** + * test the basic registration process + * - first subscribe to CBAM LCNs + * - second publish it's existence on MSB + */ + @Test + public void testRegistration() throws Exception { + //given + Subscription subscription = new Subscription(); + when(lcnApi.subscriptionsPost(subscriptionToCreate.capture(), Mockito.eq(NOKIA_LCN_API_VERSION))).thenReturn(subscription); + MicroServiceFullInfo returnedMicroService = new MicroServiceFullInfo(); + when(msbClient.registerMicroServiceInfo(registeredMicroservice.capture())).thenReturn(returnedMicroService); + //when + selfRegistrationManager.register(); + //verify + InOrder registrationOrder = Mockito.inOrder(lcnApi, msbClient); + registrationOrder.verify(lcnApi).subscriptionsPost(any(), any()); + registrationOrder.verify(msbClient).registerMicroServiceInfo(any()); + + assertMicroserviceRegistered(); + assertNewLcnSubscription(); + assertServiceUp(); + } + + private void assertNewLcnSubscription() { + CreateSubscriptionRequest subscriptionCreation = subscriptionToCreate.getValue(); + assertEquals("http://5.6.7.8:12345/api/NokiaSVNFM/v1/lcn", subscriptionCreation.getCallbackUrl()); + assertEquals(SubscriptionAuthentication.TypeEnum.NONE, subscriptionCreation.getAuthentication().getType()); + assertNull(subscriptionCreation.getAuthentication().getUserName()); + assertNull(subscriptionCreation.getAuthentication().getClientName()); + assertNull(subscriptionCreation.getAuthentication().getClientPassword()); + assertNull(subscriptionCreation.getAuthentication().getPassword()); + assertNull(subscriptionCreation.getAuthentication().getTokenUrl()); + assertNull(subscriptionCreation.getFilter().getVnfdId()); + assertNull(subscriptionCreation.getFilter().getVnfInstanceId()); + assertNull(subscriptionCreation.getFilter().getVnfProductName()); + assertNull(subscriptionCreation.getFilter().getVnfSoftwareVersion()); + assertEquals(Lists.newArrayList(VnfNotificationType.VNFLIFECYCLECHANGENOTIFICATION), subscriptionCreation.getFilter().getNotificationTypes()); + assertTrue(subscriptionCreation.getFilter().getOperationTypes().contains(HEAL)); + assertTrue(subscriptionCreation.getFilter().getOperationTypes().contains(SCALE)); + assertTrue(subscriptionCreation.getFilter().getOperationTypes().contains(TERMINATE)); + assertTrue(subscriptionCreation.getFilter().getOperationTypes().contains(INSTANTIATE)); + assertEquals(4, subscriptionCreation.getFilter().getOperationTypes().size()); + } + + private void assertMicroserviceRegistered() { + MicroServiceInfo microserviceRequest = registeredMicroservice.getValue(); + assertEquals(1, microserviceRequest.getNodes().size()); + Node node = microserviceRequest.getNodes().iterator().next(); + assertEquals("0", node.getTtl()); + assertEquals("1.2.3.4", node.getIp()); + assertEquals("12345", node.getPort()); + assertEquals("REST", microserviceRequest.getProtocol()); + assertNull(microserviceRequest.getMetadata()); + //very strange, but it should be null for ONAP to work + assertEquals("", microserviceRequest.getPath()); + assertEquals(SelfRegistrationManager.SERVICE_NAME, microserviceRequest.getServiceName()); + assertEquals("/api/NokiaSVNFM/v1", microserviceRequest.getUrl()); + assertEquals("v1", microserviceRequest.getVersion()); + //1 means internal service to ONAP + assertEquals("1", microserviceRequest.getVisualRange()); + } + + /** + * If the subscription already exists the subscription is not recreated + */ + @Test + public void testResubscription() throws Exception { + //given + MicroServiceFullInfo returnedMicroService = new MicroServiceFullInfo(); + when(msbClient.registerMicroServiceInfo(registeredMicroservice.capture())).thenReturn(returnedMicroService); + Subscription existingSubscription = new Subscription(); + existingSubscription.setId(UUID.randomUUID().toString()); + existingSubscription.setCallbackUrl("http://5.6.7.8:12345/api/NokiaSVNFM/v1/lcn"); + subscriptions.add(existingSubscription); + //when + selfRegistrationManager.register(); + //verify + assertMicroserviceRegistered(); + verify(lcnApi, never()).subscriptionsPost(any(), any()); + assertServiceUp(); + } + + /** + * If the LCN subscription fails the microservice is not registered + */ + @Test + public void testFailedLcnSubscription() throws Exception { + //given + ApiException expectedException = new ApiException(); + when(lcnApi.subscriptionsPost(any(), any())).thenThrow(expectedException); + //when + try { + selfRegistrationManager.register(); + fail(); + } catch (RuntimeException e) { + assertEquals(expectedException, e.getCause()); + } + //verify + verify(msbClient, never()).registerMicroServiceInfo(any()); + verify(logger).error("Unable to subscribe to CBAM LCN", expectedException); + assertServiceDown(); + } + + /** + * If the registration to MSB fails the subscription is deleted + */ + @Test + public void testFailedMsbPublish() throws Exception { + //given + Subscription subscription = new Subscription(); + when(lcnApi.subscriptionsPost(subscriptionToCreate.capture(), Mockito.eq(NOKIA_LCN_API_VERSION))).thenAnswer(new Answer<Subscription>() { + @Override + public Subscription answer(InvocationOnMock invocationOnMock) throws Throwable { + subscription.setCallbackUrl("http://5.6.7.8:12345/api/NokiaSVNFM/v1/lcn"); + subscription.setId(UUID.randomUUID().toString()); + subscriptions.add(subscription); + return subscription; + } + }); + MicroServiceFullInfo returnedMicroService = new MicroServiceFullInfo(); + RouteException expectedException = new RouteException(); + when(msbClient.registerMicroServiceInfo(registeredMicroservice.capture())).thenThrow(expectedException); + //when + try { + selfRegistrationManager.register(); + //verify + fail(); + } catch (RuntimeException e) { + assertEquals(expectedException, e.getCause()); + } + assertNewLcnSubscription(); + verify(lcnApi).subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION); + assertServiceDown(); + } + + /** + * basic service unregistration + * - ongoing jobs are outwaited + * - first the service is removed from MSB + * - second unregistration + */ + @Test + public void testUnregistration() throws Exception { + //given + Subscription subscription = new Subscription(); + subscription.setCallbackUrl("http://5.6.7.8:12345/api/NokiaSVNFM/v1/lcn"); + subscription.setId(UUID.randomUUID().toString()); + subscriptions.add(subscription); + when(jobManager.hasOngoingJobs()).thenReturn(false); + MicroServiceFullInfo returnedMicroService = new MicroServiceFullInfo(); + //when + selfRegistrationManager.deRegister(); + //verify + InOrder inOrder = Mockito.inOrder(jobManager, msbClient, lcnApi); + inOrder.verify(msbClient).cancelMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION); + inOrder.verify(lcnApi).subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION); + assertServiceDown(); + } + + /** + * if the MSB reports that it could not cancel the service, but the service has + * disappeared from MSB the cancellation is considered to be successful + */ + @Test + public void testPartiallyFailedMsbCancel() throws Exception { + //given + Subscription subscription = new Subscription(); + subscription.setCallbackUrl("http://5.6.7.8:12345/api/NokiaSVNFM/v1/lcn"); + subscription.setId(UUID.randomUUID().toString()); + subscriptions.add(subscription); + when(jobManager.hasOngoingJobs()).thenReturn(false); + when(msbClient.cancelMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION)).then(new Answer<RouteResult>() { + @Override + public RouteResult answer(InvocationOnMock invocationOnMock) throws Throwable { + when(msbClient.queryMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION)).thenThrow(new RouteException()); + throw new RouteException(); + } + }); + MicroServiceFullInfo returnedMicroService = new MicroServiceFullInfo(); + //when + selfRegistrationManager.deRegister(); + //verify + InOrder inOrder = Mockito.inOrder(jobManager, msbClient, lcnApi); + inOrder.verify(msbClient).cancelMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION); + inOrder.verify(msbClient).queryMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION); + inOrder.verify(lcnApi).subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION); + assertServiceDown(); + } + + /** + * failure of unregistration from MSB should be propagated + */ + @Test + public void testUnregistrationFailure() throws Exception { + //given + Subscription subscription = new Subscription(); + subscription.setCallbackUrl("http://5.6.7.8:12345/api/NokiaSVNFM/v1/lcn"); + subscription.setId(UUID.randomUUID().toString()); + subscriptions.add(subscription); + when(msbClient.cancelMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION)).then(new Answer<RouteResult>() { + @Override + public RouteResult answer(InvocationOnMock invocationOnMock) throws Throwable { + throw new RouteException(); + } + }); + MicroServiceFullInfo returnedMicroService = new MicroServiceFullInfo(); + //when + try { + selfRegistrationManager.deRegister(); + fail(); + } catch (RuntimeException e) { + + } + //verify + InOrder inOrder = Mockito.inOrder(jobManager, msbClient, lcnApi); + inOrder.verify(msbClient).cancelMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION); + inOrder.verify(msbClient).queryMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION); + verify(lcnApi, Mockito.never()).subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION); + assertServiceDown(); + } + + /** + * failure of subscription deletion from MSB should be propagated + */ + @Test + public void testSubscriptionFailure() throws Exception { + //given + Subscription subscription = new Subscription(); + subscription.setCallbackUrl("http://5.6.7.8:12345/api/NokiaSVNFM/v1/lcn"); + subscription.setId(UUID.randomUUID().toString()); + subscriptions.add(subscription); + when(jobManager.hasOngoingJobs()).thenReturn(false); + ApiException expectedException = new ApiException(); + doThrow(expectedException).when(lcnApi).subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION); + //when + try { + selfRegistrationManager.deRegister(); + fail(); + } catch (RuntimeException e) { + + } + //verify + InOrder inOrder = Mockito.inOrder(jobManager, msbClient, lcnApi); + inOrder.verify(msbClient).cancelMicroServiceInfo(SelfRegistrationManager.SERVICE_NAME, SelfRegistrationManager.DRIVER_VERSION); + inOrder.verify(lcnApi).subscriptionsSubscriptionIdDelete(subscription.getId(), NOKIA_LCN_API_VERSION); + assertServiceDown(); + } + + /** + * the swagger API definitions embedded in the code + */ + @Test + public void testSwaggerApi() throws Exception { + //no idea how to test this except repeat implementation + byte[] a = TestUtil.loadFile(SelfRegistrationManager.SWAGGER_API_DEFINITION); + tearGeneric(); + //when + assertTrue(Arrays.equals(a, selfRegistrationManager.getSwaggerApiDefinition())); + } + + public void assertServiceUp() throws Exception { + assertTrue(selfRegistrationManager.isReady()); + } + + /** + * if there are ongoing jobs then the guard thros exception + */ + public void assertServiceDown() { + assertFalse(selfRegistrationManager.isReady()); + + } + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestLifecycleChangeNotificationManager.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestLifecycleChangeNotificationManager.java new file mode 100644 index 00000000..ed009b97 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestLifecycleChangeNotificationManager.java @@ -0,0 +1,457 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification; + +import com.google.gson.*; +import com.nokia.cbam.lcm.v32.ApiException; +import com.nokia.cbam.lcm.v32.model.*; +import org.joda.time.DateTime; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mockito; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.ILifecycleChangeNotificationManager; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; +import org.threeten.bp.OffsetDateTime; + +import java.util.ArrayList; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import static com.nokia.cbam.lcm.v32.model.OperationType.*; +import static junit.framework.TestCase.*; +import static org.mockito.Mockito.*; +import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCM_API_VERSION; +import static org.springframework.test.util.ReflectionTestUtils.setField; + +public class TestLifecycleChangeNotificationManager extends TestBase { + + public static final String OPERATION_EXECUTION_ID = "myOperationExecutionId"; + + @InjectMocks + private LifecycleChangeNotificationManager lifecycleChangeNotificationManager; + private VnfLifecycleChangeNotification recievedLcn = new VnfLifecycleChangeNotification(); + private List<OperationExecution> operationExecutions = new ArrayList<>(); + private OperationExecution instantiationOperation = new OperationExecution(); + private OperationExecution scaleOperation = new OperationExecution(); + private OperationExecution healOperation = new OperationExecution(); + private OperationExecution terminationOperation = new OperationExecution(); + + private ArgumentCaptor<OperationExecution> currentOperationExecution = ArgumentCaptor.forClass(OperationExecution.class); + private ArgumentCaptor<ReportedAffectedConnectionPoints> affectedConnectionPoints = ArgumentCaptor.forClass(ReportedAffectedConnectionPoints.class); + + private List<VnfInfo> vnfs = new ArrayList<>(); + private VnfInfo vnf = new VnfInfo(); + + @Before + public void initMocks() throws Exception { + setField(LifecycleChangeNotificationManager.class, "logger", logger); + instantiationOperation.setId("instantiationOperationExecutionId"); + instantiationOperation.setStartTime(OffsetDateTime.now()); + instantiationOperation.setOperationType(OperationType.INSTANTIATE); + scaleOperation.setId("scaleOperationExecutionId"); + scaleOperation.setStartTime(OffsetDateTime.now().plusDays(1)); + scaleOperation.setOperationType(OperationType.SCALE); + terminationOperation.setId("terminationExecutionId"); + terminationOperation.setStartTime(OffsetDateTime.now().plusDays(1)); + terminationOperation.setOperationType(OperationType.TERMINATE); + healOperation.setId("healOperaitonExecutionId"); + healOperation.setOperationType(OperationType.HEAL); + recievedLcn.setLifecycleOperationOccurrenceId("instantiationOperationExecutionId"); + healOperation.setStartTime(OffsetDateTime.now().plusDays(1)); + recievedLcn.setVnfInstanceId(VNF_ID); + when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(operationExecutions); + prepOperation(instantiationOperation); + prepOperation(scaleOperation); + prepOperation(healOperation); + prepOperation(terminationOperation); + doNothing().when(notificationSender).processNotification(eq(recievedLcn), currentOperationExecution.capture(), affectedConnectionPoints.capture(), eq(VIM_ID)); + InstantiateVnfRequest instantiateVnfRequest = new InstantiateVnfRequest(); + VimInfo vimInfo = new VimInfo(); + vimInfo.setId(VIM_ID); + instantiateVnfRequest.getVims().add(vimInfo); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(instantiationOperation.getId(), NOKIA_LCM_API_VERSION)).thenReturn(new Gson().toJsonTree(instantiateVnfRequest)); + when(vnfApi.vnfsGet(NOKIA_LCM_API_VERSION)).thenReturn(vnfs); + vnfs.add(vnf); + vnf.setId(VNF_ID); + VnfProperty prop = new VnfProperty(); + prop.setName(ILifecycleChangeNotificationManager.EXTERNAL_VNFM_ID); + prop.setValue(VNFM_ID); + vnf.setExtensions(new ArrayList<>()); + vnf.getExtensions().add(prop); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenReturn(vnf); + } + + private void prepOperation(OperationExecution operationExecution) throws ApiException { + addEmptyModifiedConnectionPoints(operationExecution); + JsonElement root = new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + JOB_ID + "\"}}"); + operationExecution.setOperationParams(root); + switch (operationExecution.getOperationType()) { + case TERMINATE: + root.getAsJsonObject().addProperty("terminationType", "GRACEFULL"); + } + when(operationExecutionApi.operationExecutionsOperationExecutionIdGet(operationExecution.getId(), NOKIA_LCM_API_VERSION)).thenReturn(operationExecution); + operationExecutions.add(operationExecution); + } + + private void addEmptyModifiedConnectionPoints(OperationExecution operationExecution) { + OperationResult operationResult = new OperationResult(); + operationResult.operationResult = new ReportedAffectedConnectionPoints(); + JsonElement additionalData = new Gson().toJsonTree(operationResult); + operationExecution.setAdditionalData(additionalData); + } + + /** + * The first instantiation before the current operation is selected + */ + @Test + public void testLastInstantiationSelection() { + List<OperationExecution> operations = new ArrayList<>(); + + OperationExecution operation = buildOperation(OffsetDateTime.now(), TERMINATE); + OperationExecution operationScale = buildOperation(OffsetDateTime.now().minusDays(1), SCALE); + OperationExecution operationClosestInstantiate = buildOperation(OffsetDateTime.now().minusDays(2), INSTANTIATE); + OperationExecution operationFurthers = buildOperation(OffsetDateTime.now().minusDays(3), INSTANTIATE); + + operations.add(operation); + operations.add(operationScale); + operations.add(operationClosestInstantiate); + operations.add(operationFurthers); + assertEquals(operationClosestInstantiate, LifecycleChangeNotificationManager.findLastInstantiationBefore(operations, operation)); + } + + /** + * The instantiation operation itself is valid as the last instantiation operation + */ + @Test + public void testInstantiationSufficesTheLastInstantiation() { + DateTime baseTime = DateTime.now(); + List<OperationExecution> operations = new ArrayList<>(); + + OperationExecution operation = buildOperation(OffsetDateTime.now(), INSTANTIATE); + OperationExecution operationScale = buildOperation(OffsetDateTime.now().minusDays(1), SCALE); + OperationExecution operationFurthers = buildOperation(OffsetDateTime.now().minusDays(2), INSTANTIATE); + + operations.add(operation); + operations.add(operationScale); + operations.add(operationFurthers); + assertEquals(operation, LifecycleChangeNotificationManager.findLastInstantiationBefore(operations, operation)); + } + + /** + * If no instantiation operation is found for before the selected operation + */ + @Test + public void testNoInstantiation() { + DateTime baseTime = DateTime.now(); + List<OperationExecution> operations = new ArrayList<>(); + + OperationExecution operation = buildOperation(OffsetDateTime.now(), TERMINATE); + OperationExecution operationScale = buildOperation(OffsetDateTime.now().minusDays(1), SCALE); + + operations.add(operation); + operations.add(operationScale); + try { + LifecycleChangeNotificationManager.findLastInstantiationBefore(operations, operation); + fail(); + } catch (NoSuchElementException e) { + + } + } + + /** + * the operations are ordered from newest (first) to oldest (last) + */ + @Test + public void testOperationOrdering() { + List<OperationExecution> operationExecutions = new ArrayList<>(); + OperationExecution before = buildOperation(OffsetDateTime.now(), OperationType.INSTANTIATE); + operationExecutions.add(before); + OperationExecution after = buildOperation(OffsetDateTime.now().plusDays(1), OperationType.SCALE); + operationExecutions.add(after); + List<OperationExecution> sorted1 = LifecycleChangeNotificationManager.NEWEST_OPERATIONS_FIRST.sortedCopy(operationExecutions); + assertEquals(after, sorted1.get(0)); + assertEquals(before, sorted1.get(1)); + } + + /** + * if VNF listing fails the processing of the notifications is aborted + */ + @Test + public void testUnableToListVnfs() throws Exception { + ApiException expectedException = new ApiException(); + when(vnfApi.vnfsGet(NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + //when + try { + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + fail(); + } catch (Exception e) { + assertEquals(expectedException, e.getCause()); + verify(logger).error("Unable to list VNFs / query VNF", expectedException); + } + } + + /** + * if VNF query fails the processing of the notifications is aborted + */ + @Test + public void testUnableToQueryVnf() throws Exception { + ApiException expectedException = new ApiException(); + when(vnfApi.vnfsVnfInstanceIdGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + //when + try { + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + fail(); + } catch (Exception e) { + assertEquals(expectedException, e.getCause()); + verify(logger).error("Unable to list VNFs / query VNF", expectedException); + } + } + + /** + * if the VNF is not managed by this VNFM the LCN is dropped + */ + @Test + public void testNonManagedVnf() throws Exception { + vnf.getExtensions().clear(); + //when + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + //verify + Mockito.verifyZeroInteractions(operationExecutionApi); + verify(logger).warn("The VNF with " + VNF_ID + " identifier is not a managed VNF"); + } + + /** + * if the VNF is not managed by this VNFM the LCN is dropped + */ + @Test + public void testManagedByOtherVnf() throws Exception { + vnf.getExtensions().get(0).setValue("unknownVnfmId"); + //when + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + //verify + Mockito.verifyZeroInteractions(operationExecutionApi); + verify(logger).warn("The VNF with " + VNF_ID + " identifier is not a managed by the VNFM with id unknownVnfmId"); + } + + /** + * if the VNF disappeared before processing the LCN + */ + @Test + public void testDisappearedVnf() throws Exception { + vnfs.clear(); + //when + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + //verify + Mockito.verifyZeroInteractions(operationExecutionApi); + verify(logger).warn("The VNF with " + VNF_ID + " disappeared before being able to process the LCN"); + } + + /** + * if the operation parameters of the last instantiation is non querieable error is propagated + */ + @Test + public void testUnableToQueryOperationParams() throws Exception { + recievedLcn.setOperation(OperationType.TERMINATE); + recievedLcn.setStatus(OperationStatus.FINISHED); + RuntimeException expectedException = new RuntimeException(); + when(operationExecutionApi.operationExecutionsOperationExecutionIdOperationParamsGet(instantiationOperation.getId(), NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + //when + try { + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + fail(); + } catch (Exception e) { + //verify + Mockito.verifyZeroInteractions(nsLcmApi); + assertEquals(expectedException, e.getCause()); + verify(logger).error("Unable to detect last instantiation operation", e.getCause()); + } + } + + /** + * if unable to send LCN to VF-C the error is propagated + */ + @Test + public void testUnableToQueryCurrentOperation() throws Exception { + recievedLcn.setOperation(OperationType.TERMINATE); + recievedLcn.setStatus(OperationStatus.FINISHED); + ApiException expectedException = new ApiException(); + when(vnfApi.vnfsVnfInstanceIdOperationExecutionsGet(VNF_ID, NOKIA_LCM_API_VERSION)).thenThrow(expectedException); + //when + try { + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + fail(); + } catch (Exception e) { + //verify + assertEquals(expectedException, e.getCause()); + verify(logger).error("Unable to retrieve the current VNF myVnfId", e.getCause()); + } + } + + /** + * test that waitForTerminationToBeProcessed outwaits the successfull processing of the termination notification + */ + @Test + public void testWaitForTermination() throws Exception { + //given + //add an non processed notification + VnfLifecycleChangeNotification nonProcessedEvent = new VnfLifecycleChangeNotification(); + nonProcessedEvent.setStatus(OperationStatus.FINISHED); + nonProcessedEvent.setOperation(OperationType.TERMINATE); + OperationExecution secondTerminationOperationExecution = new OperationExecution(); + secondTerminationOperationExecution.setOperationType(OperationType.TERMINATE); + secondTerminationOperationExecution.setId("secondId"); + secondTerminationOperationExecution.setOperationParams(buildTerminationParams()); + nonProcessedEvent.setLifecycleOperationOccurrenceId(secondTerminationOperationExecution.getId()); + lifecycleChangeNotificationManager.handleLcn(nonProcessedEvent); + //add second termination + recievedLcn.setOperation(OperationType.TERMINATE); + recievedLcn.setStatus(OperationStatus.FINISHED); + recievedLcn.setLifecycleOperationOccurrenceId(terminationOperation.getId()); + ExecutorService executorService = Executors.newCachedThreadPool(); + Future<Boolean> waitExitedWithSuccess = executorService.submit(new Callable<Boolean>() { + @Override + public Boolean call() throws Exception { + try { + lifecycleChangeNotificationManager.waitForTerminationToBeProcessed(terminationOperation.getId()); + return true; + } catch (Exception e) { + return false; + } + } + }); + //when + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + //verify + assertTrue(waitExitedWithSuccess.get()); + } + + /** + * Forceful termination results in an empty affected connection points + */ + @Test + public void testMissingPreResultForForcefullTermination() { + //given + recievedLcn.setOperation(OperationType.INSTANTIATE); + recievedLcn.setStatus(OperationStatus.FINISHED); + recievedLcn.setLifecycleOperationOccurrenceId(terminationOperation.getId()); + JsonObject additionalData = new JsonObject(); + additionalData.add("operationResult", new JsonObject()); + ((JsonObject) terminationOperation.getOperationParams()).addProperty("terminationType", "FORCEFUL"); + terminationOperation.setAdditionalData(additionalData); + terminationOperation.setStatus(OperationStatus.FINISHED); + terminationOperation.setOperationType(OperationType.TERMINATE); + //when + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + assertNull(affectedConnectionPoints.getValue()); + verify(logger).warn("Unable to send information related to affected connection points during forceful termination"); + } + + /** + * test end notification scenario for failed scale-out + * - LCN is sent to VF-C, but the + */ + @Test + public void testMissingPreResultForFailedOperation() { + //given + recievedLcn.setOperation(OperationType.SCALE); + recievedLcn.setStatus(OperationStatus.FAILED); + recievedLcn.setLifecycleOperationOccurrenceId(scaleOperation.getId()); + ScaleVnfRequest request = new ScaleVnfRequest(); + request.setAdditionalParams(new JsonParser().parse("{ \"type\" : \"IN\", \"jobId\" : \"" + JOB_ID + "\" }")); + request.setType(ScaleDirection.OUT); + scaleOperation.setOperationParams(request); + scaleOperation.setAdditionalData(null); + scaleOperation.setStatus(OperationStatus.FAILED); + scaleOperation.setOperationType(OperationType.SCALE); + //when + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + assertEquals(0, affectedConnectionPoints.getValue().getPost().size()); + assertEquals(0, affectedConnectionPoints.getValue().getPre().size()); + verify(logger).warn("The operation failed and the affected connection points were not reported"); + } + + /** + * test end notification success scenario for scale-out + * - LCN is sent to VF-C + */ + @Test + public void testMissingPreResult() { + //given + recievedLcn.setOperation(OperationType.SCALE); + recievedLcn.setStatus(OperationStatus.FINISHED); + recievedLcn.setLifecycleOperationOccurrenceId(scaleOperation.getId()); + ScaleVnfRequest request = new ScaleVnfRequest(); + request.setAdditionalParams(new JsonParser().parse("{ \"type\" : \"IN\", \"jobId\" : \"" + JOB_ID + "\" }")); + request.setType(ScaleDirection.OUT); + scaleOperation.setOperationParams(request); + JsonObject additionalData = new JsonObject(); + additionalData.add("operationResult", new JsonObject()); + scaleOperation.setAdditionalData(additionalData); + scaleOperation.setStatus(OperationStatus.FINISHED); + scaleOperation.setOperationType(OperationType.SCALE); + JsonElement root = new JsonParser().parse("{ \"additionalParams\" : { \"jobId\" : \"" + JOB_ID + "\"}}"); + JsonObject operationParams = new JsonObject(); + //when + try { + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + fail(); + } catch (Exception e) { + assertEquals("All operations must return the { \"operationResult\" : { \"cbam_pre\" : [<fillMeOut>], \"cbam_post\" : [<fillMeOut>] } } structure", e.getMessage()); + } + } + + /** + * missing connection points are tolerated in case of failed operations + */ + @Test + public void testMissingConnectionPoints() { + //given + recievedLcn.setOperation(OperationType.INSTANTIATE); + recievedLcn.setStatus(OperationStatus.FAILED); + recievedLcn.setLifecycleOperationOccurrenceId(instantiationOperation.getId()); + instantiationOperation.setAdditionalData(null); + instantiationOperation.setStatus(OperationStatus.FAILED); + //when + lifecycleChangeNotificationManager.handleLcn(recievedLcn); + assertEquals(0, affectedConnectionPoints.getValue().getPost().size()); + assertEquals(0, affectedConnectionPoints.getValue().getPre().size()); + verify(logger).warn("The operation failed and the affected connection points were not reported"); + } + + private JsonObject buildTerminationParams() { + JsonObject root = new JsonObject(); + root.add("terminationType", new JsonPrimitive("GRACEFULL")); + return root; + } + + private OperationExecution buildOperation(OffsetDateTime baseTime, OperationType operationType) { + OperationExecution operation = new OperationExecution(); + operation.setStartTime(baseTime); + operation.setOperationType(operationType); + return operation; + } + + class OperationResult { + ReportedAffectedConnectionPoints operationResult; + } + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestProcessedNotification.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestProcessedNotification.java new file mode 100644 index 00000000..bbe660a0 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestProcessedNotification.java @@ -0,0 +1,28 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification; + +import org.junit.Test; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; + +import static pl.pojo.tester.api.assertion.Assertions.assertPojoMethodsFor; + +public class TestProcessedNotification extends TestBase { + @Test + public void testPojo() { + assertPojoMethodsFor(ProcessedNotification.class).areWellImplemented(); + } +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestReportedAffectedConnectionPoints.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestReportedAffectedConnectionPoints.java new file mode 100644 index 00000000..83699723 --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestReportedAffectedConnectionPoints.java @@ -0,0 +1,30 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification; + +import org.junit.Test; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; + +import static pl.pojo.tester.api.assertion.Assertions.assertPojoMethodsFor; + +public class TestReportedAffectedConnectionPoints extends TestBase { + @Test + public void testPojo() { + assertPojoMethodsFor(ReportedAffectedConnectionPoints.class).areWellImplemented(); + + } + +} diff --git a/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestReportedAffectedCp.java b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestReportedAffectedCp.java new file mode 100644 index 00000000..cb4d7e1c --- /dev/null +++ b/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/vnfm/notification/TestReportedAffectedCp.java @@ -0,0 +1,30 @@ +/* + * Copyright 2016-2017, Nokia Corporation + * + * 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. + */ +package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification; + +import org.junit.Test; +import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; + +import static pl.pojo.tester.api.assertion.Assertions.assertPojoMethodsFor; + +public class TestReportedAffectedCp extends TestBase { + @Test + public void testPojo() { + assertPojoMethodsFor(ReportedAffectedCp.class).areWellImplemented(); + + } + +} |