diff options
author | mark.j.leonard <mark.j.leonard@gmail.com> | 2018-06-27 18:27:47 +0100 |
---|---|---|
committer | mark.j.leonard <mark.j.leonard@gmail.com> | 2018-06-27 18:28:06 +0100 |
commit | 4036c9aae1965d3fa53956085e9d6f90feb217a8 (patch) | |
tree | d24608e4daf0f06b02ec9c82b40f76d78caed610 /src/test/java | |
parent | dc94e09008ba0eb9dce7541d2c898ddc5c500814 (diff) |
Additional junit tests for artifact generation
New tests to improve code coverage. Remove underscores from existing
Junit test method names.
Issue-ID: AAI-1250
Change-Id: I8d0d9c41d7a124cbedea69d64c4cd78ac6e4bcfa
Signed-off-by: mark.j.leonard <mark.j.leonard@gmail.com>
Diffstat (limited to 'src/test/java')
6 files changed, 398 insertions, 10 deletions
diff --git a/src/test/java/org/onap/aai/babel/csar/vnfcatalog/ConfigurationsToBabelArtifactConverterTest.java b/src/test/java/org/onap/aai/babel/csar/vnfcatalog/ConfigurationsToBabelArtifactConverterTest.java new file mode 100644 index 0000000..b251401 --- /dev/null +++ b/src/test/java/org/onap/aai/babel/csar/vnfcatalog/ConfigurationsToBabelArtifactConverterTest.java @@ -0,0 +1,64 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 European Software Marketing Ltd. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.babel.csar.vnfcatalog; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.junit.Test; +import org.onap.aai.babel.service.data.BabelArtifact; +import org.onap.aai.babel.service.data.BabelArtifact.ArtifactType; +import org.onap.aai.babel.util.ArtifactTestUtils; + +/** + * Tests {@link ConfigurationsToBabelArtifactConverter}. + */ +public class ConfigurationsToBabelArtifactConverterTest { + @Test + public void testNullListSupplied() { + assertThat(ConfigurationsToBabelArtifactConverter.convert(null), is(nullValue())); + } + + @Test + public void testEmptyListSupplied() { + assertThat(ConfigurationsToBabelArtifactConverter.convert(new ArrayList<>()), is(nullValue())); + } + + @Test + public void testValidListSupplied() throws IOException { + String expectedJson = new ArtifactTestUtils().getRequestJson("vnfVendorImageConfigurations.json"); + List<VendorImageConfiguration> configurations = + new Gson().fromJson(expectedJson, new TypeToken<ArrayList<VendorImageConfiguration>>() {}.getType()); + + BabelArtifact artifact = ConfigurationsToBabelArtifactConverter.convert(configurations); + + assertThat(artifact.getName(), is(equalTo("vnfVendorImageConfigurations"))); + assertThat(artifact.getType(), is(equalTo(ArtifactType.VNFCATALOG))); + assertThat(artifact.getPayload(), is(equalTo(expectedJson))); + } +} diff --git a/src/test/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractorTest.java b/src/test/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractorTest.java new file mode 100644 index 0000000..de5ea3f --- /dev/null +++ b/src/test/java/org/onap/aai/babel/csar/vnfcatalog/VnfVendorImageExtractorTest.java @@ -0,0 +1,76 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 European Software Marketing Ltd. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.babel.csar.vnfcatalog; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import org.junit.Test; +import org.onap.aai.babel.service.data.BabelArtifact; +import org.onap.aai.babel.service.data.BabelArtifact.ArtifactType; +import org.onap.aai.babel.util.ArtifactTestUtils; + +/** + * Tests {@link VnfVendorImageExtractor} + */ +public class VnfVendorImageExtractorTest { + + @Test(expected = NullPointerException.class) + public void createVendorImageMappingsNullCsarSupplied() throws ToscaToCatalogException, IOException { + new VnfVendorImageExtractor().extract(null); + } + + @Test(expected = ToscaToCatalogException.class) + public void createVendorImageMappingsEmptyCsarSupplied() throws ToscaToCatalogException, IOException { + new VnfVendorImageExtractor().extract(new byte[0]); + } + + @Test(expected = ToscaToCatalogException.class) + public void createVendorImageMappingsInvalidCsarFile() throws IOException, ToscaToCatalogException { + extractArtifact("noYmlFilesArchive.zip"); + } + + @Test(expected = ToscaToCatalogException.class) + public void createVendorImageMappingsInvalidFile() throws IOException, ToscaToCatalogException { + extractArtifact("Duff.txt"); + } + + @Test + public void createVendorImageMappingsNoVnfConfigurationExists() throws IOException, ToscaToCatalogException { + assertThat(extractArtifact("noVnfConfiguration.csar"), is(nullValue())); + } + + @Test + public void createVendorImageMappingsValidFile() throws IOException, ToscaToCatalogException { + BabelArtifact artifact = extractArtifact("catalog_csar.csar"); + assertThat(artifact.getName(), is(equalTo("vnfVendorImageConfigurations"))); + assertThat(artifact.getType(), is(equalTo(ArtifactType.VNFCATALOG))); + assertThat(artifact.getPayload(), + is(equalTo(new ArtifactTestUtils().getRequestJson("vnfVendorImageConfigurations.json")))); + } + + private BabelArtifact extractArtifact(String artifactName) throws ToscaToCatalogException, IOException { + return new VnfVendorImageExtractor().extract(new ArtifactTestUtils().getCompressedArtifact(artifactName)); + } +} diff --git a/src/test/java/org/onap/aai/babel/service/CsarToXmlConverterTest.java b/src/test/java/org/onap/aai/babel/service/CsarToXmlConverterTest.java index 0d6c7b3..3dd8382 100644 --- a/src/test/java/org/onap/aai/babel/service/CsarToXmlConverterTest.java +++ b/src/test/java/org/onap/aai/babel/service/CsarToXmlConverterTest.java @@ -58,7 +58,12 @@ public class CsarToXmlConverterTest { } private enum CsarTest { - VALID_CSAR_FILE("service-SdWanServiceTest-csar.csar"), NO_YAML_FILES("noYmlFilesArchive.zip"); + VALID_CSAR_FILE( + "service-SdWanServiceTest-csar.csar" + ), + NO_YAML_FILES( + "noYmlFilesArchive.zip" + ); private String filename; private ArtifactTestUtils artifactTestUtils = new ArtifactTestUtils(); @@ -95,27 +100,27 @@ public class CsarToXmlConverterTest { } @Test(expected = NullPointerException.class) - public void generateXmlFromCsar_nullArtifactSupplied() throws CsarConverterException { + public void testNullArtifactSupplied() throws CsarConverterException { converter.generateXmlFromCsar(null, null, null); } @Test(expected = NullPointerException.class) - public void generateXmlFromCsar_missingName() throws CsarConverterException, IOException { + public void testMissingName() throws CsarConverterException, IOException { converter.generateXmlFromCsar(CsarTest.VALID_CSAR_FILE.getContent(), null, null); } @Test(expected = NullPointerException.class) - public void generateXmlFromCsar_missingVersion() throws CsarConverterException, IOException { + public void testMissingVersion() throws CsarConverterException, IOException { converter.generateXmlFromCsar(CsarTest.VALID_CSAR_FILE.getContent(), INCORRECT_CSAR_NAME, null); } @Test(expected = CsarConverterException.class) - public void generateXmlFromCsar_noPayloadExists() throws CsarConverterException { + public void testNoPayloadExists() throws CsarConverterException { converter.generateXmlFromCsar(new byte[0], INCORRECT_CSAR_NAME, SERVICE_VERSION); } @Test(expected = CsarConverterException.class) - public void generateXmlFromCsar_csarFileHasNoYmlFiles() throws CsarConverterException, IOException { + public void testCsarFileHasNoYmlFiles() throws CsarConverterException, IOException { converter.generateXmlFromCsar(CsarTest.NO_YAML_FILES.getContent(), "noYmlFilesArchive.zip", SERVICE_VERSION); } diff --git a/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java b/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java index b5063dd..c770c60 100644 --- a/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java +++ b/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java @@ -48,7 +48,7 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - * Direct invocation of the generate artifacts service implementation + * Direct invocation of the generate artifacts service implementation. * */ @RunWith(SpringJUnit4ClassRunner.class) @@ -63,6 +63,7 @@ public class TestGenerateArtifactsServiceImpl { } private static final String ARTIFACT_GENERATOR_CONFIG = "artifact-generator.properties"; + @Inject private AAIMicroServiceAuth auth; @@ -73,6 +74,13 @@ public class TestGenerateArtifactsServiceImpl { } @Test + public void testGenerateArtifacts() throws Exception { + Response response = processJsonRequest("success_request_vnf_catalog.json"); + assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode())); + assertThat(response.getEntity(), is(getResponseJson("response.json"))); + } + + @Test public void testInvalidCsarFile() throws URISyntaxException, IOException { Response response = processJsonRequest("invalid_csar_request.json"); assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode())); @@ -108,12 +116,12 @@ public class TestGenerateArtifactsServiceImpl { } /** - * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API + * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API. * * @param resource path to the incoming JSON request * @return the Response from the HTTP API - * @throws URISyntaxException - * @throws IOException + * @throws URISyntaxException if the URI cannot be created + * @throws IOException if the resource cannot be loaded */ private Response processJsonRequest(String resource) throws URISyntaxException, IOException { UriInfo mockUriInfo = Mockito.mock(UriInfo.class); @@ -156,6 +164,10 @@ public class TestGenerateArtifactsServiceImpl { return new ArtifactTestUtils().getRequestJson(resource); } + private String getResponseJson(String jsonResponse) throws IOException, URISyntaxException { + return new ArtifactTestUtils().getResponseJson(jsonResponse); + } + private List<String> createSingletonList(String listItem) { return Collections.<String>singletonList(listItem); } diff --git a/src/test/java/org/onap/aai/babel/service/TestRequestHeaders.java b/src/test/java/org/onap/aai/babel/service/TestRequestHeaders.java new file mode 100644 index 0000000..0d81867 --- /dev/null +++ b/src/test/java/org/onap/aai/babel/service/TestRequestHeaders.java @@ -0,0 +1,159 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 European Software Marketing Ltd. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.babel.service; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map.Entry; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MultivaluedHashMap; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.aai.babel.request.RequestHeaders; + +/** + * Tests {@link RequestHeaders}. + * + */ +public class TestRequestHeaders { + + /** + * Tests compatibility with the X-ECOMP-* request headers. + */ + @Test + public void testECOMPHeaders() { + String transactionId = "transaction-id"; + String serviceInstanceId = "service-instance-id"; + + MultivaluedHashMap<String, String> headersMap = new MultivaluedHashMap<>(); + headersMap.put(RequestHeaders.HEADER_REQUEST_ID, createSingletonList(transactionId)); + headersMap.put(RequestHeaders.HEADER_SERVICE_INSTANCE_ID, createSingletonList(serviceInstanceId)); + headersMap.put("X-FromAppId", createSingletonList("app-id")); + headersMap.put("Host", createSingletonList("hostname")); + + HttpHeaders headers = createMockedHeaders(headersMap); + RequestHeaders requestHeaders = new RequestHeaders(headers); + assertThat(requestHeaders.getRequestId(), is(equalTo(transactionId))); + assertThat(requestHeaders.getInstanceId(), is(equalTo(serviceInstanceId))); + } + + @Test + public void testMultipleHeaderValues() { + String transactionId = "transaction-id"; + String serviceInstanceId = "service-instance-id"; + + MultivaluedHashMap<String, String> headersMap = new MultivaluedHashMap<>(); + headersMap.put(RequestHeaders.HEADER_REQUEST_ID, Arrays.asList(transactionId, "fred")); + headersMap.put(RequestHeaders.HEADER_SERVICE_INSTANCE_ID, Arrays.asList(serviceInstanceId, "bob")); + + HttpHeaders headers = createMockedHeaders(headersMap); + RequestHeaders requestHeaders = new RequestHeaders(headers); + assertThat(requestHeaders.getRequestId(), is(equalTo(transactionId))); + assertThat(requestHeaders.getInstanceId(), is(equalTo(serviceInstanceId))); + } + + @Test + public void testStandardHeaders() { + MultivaluedHashMap<String, String> headersMap = new MultivaluedHashMap<>(); + headersMap.put("X-TransactionId", createSingletonList("transaction-id")); + headersMap.put("X-FromAppId", createSingletonList("app-id")); + headersMap.put("Host", createSingletonList("hostname")); + + HttpHeaders headers = createMockedHeaders(headersMap); + RequestHeaders requestHeaders = new RequestHeaders(headers); + assertThat(requestHeaders.getRequestId(), is(nullValue())); + assertThat(requestHeaders.getInstanceId(), is(nullValue())); + } + + @Test + public void testHeadersWithTransactionIdSuffix() { + MultivaluedHashMap<String, String> headersMap = new MultivaluedHashMap<>(); + headersMap.put("X-TransactionId", createSingletonList("transaction-id:123")); + headersMap.put("X-FromAppId", createSingletonList("app-id")); + headersMap.put("Host", createSingletonList("hostname")); + + HttpHeaders headers = createMockedHeaders(headersMap); + RequestHeaders requestHeaders = new RequestHeaders(headers); + assertThat(requestHeaders.getCorrelationId(), is(equalTo("transaction-id"))); + assertThat(requestHeaders.getInstanceId(), is(nullValue())); + } + + @Test + public void testEmptyHeaders() { + MultivaluedHashMap<String, String> headersMap = new MultivaluedHashMap<>(); + headersMap.put(RequestHeaders.HEADER_REQUEST_ID, Collections.emptyList()); + headersMap.put(RequestHeaders.HEADER_SERVICE_INSTANCE_ID, Collections.emptyList()); + + HttpHeaders headers = createMockedHeaders(headersMap); + RequestHeaders requestHeaders = new RequestHeaders(headers); + assertThat(requestHeaders.getRequestId(), is(nullValue())); + assertThat(requestHeaders.getInstanceId(), is(nullValue())); + } + + @Test + public void testNullHeaders() { + MultivaluedHashMap<String, String> headersMap = new MultivaluedHashMap<>(); + headersMap.put(RequestHeaders.HEADER_REQUEST_ID, Collections.emptyList()); + + HttpHeaders headers = createMockedHeaders(headersMap); + Mockito.when(headers.getRequestHeader(RequestHeaders.HEADER_SERVICE_INSTANCE_ID)).thenReturn(null); + + RequestHeaders requestHeaders = new RequestHeaders(headers); + assertThat(requestHeaders.getRequestId(), is(nullValue())); + assertThat(requestHeaders.getInstanceId(), is(nullValue())); + } + + @Test + public void testToString() { + String transactionId = "transaction-id"; + String serviceInstanceId = "service-instance-id"; + + MultivaluedHashMap<String, String> headersMap = new MultivaluedHashMap<>(); + headersMap.put(RequestHeaders.HEADER_REQUEST_ID, createSingletonList(transactionId)); + headersMap.put(RequestHeaders.HEADER_SERVICE_INSTANCE_ID, createSingletonList(serviceInstanceId)); + + HttpHeaders headers = createMockedHeaders(headersMap); + RequestHeaders requestHeaders = new RequestHeaders(headers); + assertThat(requestHeaders.toString(), is(equalTo( + "RequestHeaders [requestId=transaction-id, instanceId=service-instance-id, transactionId=null]"))); + } + + private HttpHeaders createMockedHeaders(MultivaluedHashMap<String, String> headersMap) { + HttpHeaders headers = Mockito.mock(HttpHeaders.class); + for (Entry<String, List<String>> entry : headersMap.entrySet()) { + List<String> valuesList = entry.getValue(); + String value = valuesList == null || valuesList.isEmpty() ? null : valuesList.get(0); + Mockito.when(headers.getHeaderString(entry.getKey())).thenReturn(value); + } + Mockito.when(headers.getRequestHeaders()).thenReturn(headersMap); + return headers; + } + + private List<String> createSingletonList(String listItem) { + return Collections.<String>singletonList(listItem); + } +} diff --git a/src/test/java/org/onap/aai/babel/xml/generator/model/TestModel.java b/src/test/java/org/onap/aai/babel/xml/generator/model/TestModel.java new file mode 100644 index 0000000..77c2a50 --- /dev/null +++ b/src/test/java/org/onap/aai/babel/xml/generator/model/TestModel.java @@ -0,0 +1,72 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 European Software Marketing Ltd. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.babel.xml.generator.model; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; +import java.util.Collection; +import java.util.Properties; +import org.junit.Before; +import org.junit.Test; +import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil; + +/** + * Direct tests of the Model (to improve code coverage). + */ +public class TestModel { + + static { + if (System.getProperty("AJSC_HOME") == null) { + System.setProperty("AJSC_HOME", "."); + } + } + + @Before + public void setup() throws FileNotFoundException, IOException { + InputStream in = TestModel.class.getClassLoader().getResourceAsStream("artifact-generator.properties"); + Properties properties = new Properties(); + properties.load(in); + in.close(); + WidgetConfigurationUtil.setConfig(properties); + } + + @Test + public void testGetModels() { + Collection<String> toscaTypes = Arrays.asList("org.openecomp.resource.vf.allottedResource", + "org.openecomp.resource.cp", "org.openecomp.resource.vfc.nodes.heat.cinder", "any.unknown.type", null); + for (String toscaType : toscaTypes) { + Model.getModelFor(toscaType); + } + } + + @Test + public void testGetCardinality() { + new AllotedResource().getCardinality(); + } + + @Test + public void testGetModelType() { + new OamNetwork().getModelType(); + } + +} |