diff options
author | 2018-02-12 20:55:54 +0100 | |
---|---|---|
committer | 2018-02-23 11:44:45 +0100 | |
commit | b17042b955489d8a023d09abad5436ff9b900dc3 (patch) | |
tree | 1e4392ac04a2fb1ed8d17075d504cf6594acaf16 /nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/restapi | |
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/nfvo/driver/vnfm/svnfm/nokia/restapi')
5 files changed, 453 insertions, 0 deletions
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); + } +} |