From 2a3357829783d850d230d8b85b9e1c15effe1a38 Mon Sep 17 00:00:00 2001 From: Jessica Wagantall Date: Thu, 7 Nov 2019 11:33:01 -0800 Subject: Migrate jtosca contents Issue-ID: CIMAN-33 Signed-off-by: Jessica Wagantall --- .../sdc/toscaparser/api/GetValidationIssues.java | 100 +++++++ .../onap/sdc/toscaparser/api/JToscaImportTest.java | 309 +++++++++++++++++++++ .../sdc/toscaparser/api/JToscaMetadataParse.java | 127 +++++++++ .../api/elements/CalculatePropertyByPathTest.java | 167 +++++++++++ .../toscaparser/api/elements/EntityTypeTest.java | 75 +++++ .../toscaparser/api/functions/GetInputTest.java | 96 +++++++ 6 files changed, 874 insertions(+) create mode 100644 jtosca/src/test/java/org/onap/sdc/toscaparser/api/GetValidationIssues.java create mode 100644 jtosca/src/test/java/org/onap/sdc/toscaparser/api/JToscaImportTest.java create mode 100644 jtosca/src/test/java/org/onap/sdc/toscaparser/api/JToscaMetadataParse.java create mode 100644 jtosca/src/test/java/org/onap/sdc/toscaparser/api/elements/CalculatePropertyByPathTest.java create mode 100644 jtosca/src/test/java/org/onap/sdc/toscaparser/api/elements/EntityTypeTest.java create mode 100644 jtosca/src/test/java/org/onap/sdc/toscaparser/api/functions/GetInputTest.java (limited to 'jtosca/src/test/java/org') diff --git a/jtosca/src/test/java/org/onap/sdc/toscaparser/api/GetValidationIssues.java b/jtosca/src/test/java/org/onap/sdc/toscaparser/api/GetValidationIssues.java new file mode 100644 index 0000000..140a6e9 --- /dev/null +++ b/jtosca/src/test/java/org/onap/sdc/toscaparser/api/GetValidationIssues.java @@ -0,0 +1,100 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.sdc.toscaparser.api; + +import com.opencsv.CSVWriter; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + +//Generate excel file, include all validation issues errors in jtosca +//the error java code, the line number and file name for each error. +public class GetValidationIssues { + + public static CSVWriter fileWriter = null; + public static List data = new ArrayList<>(); + + public static void main(String[] args) { + System.out.println("GetAllValidationIssues - path to project files Directory is " + Arrays.toString(args)); + File jtoscaFiles = new File(args[0] + "\\jtosca\\src\\main\\java\\org\\onap\\sdc\\toscaparser\\api"); + + try { + printFiles(jtoscaFiles); + fileWriter = new CSVWriter(new FileWriter(args[1] + "\\JToscaValidationIssues_" + System.currentTimeMillis() + ".csv"), '\t'); + fileWriter.writeNext(new String[]{"Error Message", "Class Name", "Line No."}, false); + fileWriter.writeAll(data, false); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + fileWriter.flush(); + fileWriter.close(); + } catch (IOException e) { + System.out.println("Error while flushing/closing fileWriter !!!"); + e.printStackTrace(); + } + } + } + + private static void printFiles(File dir) { + if (dir != null && dir.exists()) { + for (File file : dir.listFiles()) { + if (file.isDirectory()) + printFiles(file); + else { + Scanner scanner = null; + try { + scanner = new Scanner(file); + + int lineNum = 0; + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + lineNum++; + if (line.startsWith("/*python")) + break; + + if (!line.trim().startsWith("//") && !line.trim().startsWith("#") && line.contains("ThreadLocalsHolder.getCollector().appendValidationIssue")) { + String errMsg = line.trim(); + if (!errMsg.contains(";")) { + String nextLine = null; + while (scanner.hasNextLine() && (nextLine == null || !nextLine.contains(";"))) { + nextLine = scanner.nextLine(); + errMsg += nextLine.trim(); + } + } + + data.add(new String[]{errMsg, file.getName(), String.valueOf(lineNum)}); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + } +} + diff --git a/jtosca/src/test/java/org/onap/sdc/toscaparser/api/JToscaImportTest.java b/jtosca/src/test/java/org/onap/sdc/toscaparser/api/JToscaImportTest.java new file mode 100644 index 0000000..5876ac7 --- /dev/null +++ b/jtosca/src/test/java/org/onap/sdc/toscaparser/api/JToscaImportTest.java @@ -0,0 +1,309 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (c) 2017 AT&T Intellectual Property. + * ================================================================================ + * 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========================================================= + * Modifications copyright (c) 2019 Fujitsu Limited. + * ================================================================================ + */ +package org.onap.sdc.toscaparser.api; + +import org.junit.Test; +import org.onap.sdc.toscaparser.api.common.JToscaException; +import org.onap.sdc.toscaparser.api.elements.DataType; +import org.onap.sdc.toscaparser.api.elements.PropertyDef; +import org.onap.sdc.toscaparser.api.elements.constraints.Schema; +import org.onap.sdc.toscaparser.api.parameters.Annotation; +import org.onap.sdc.toscaparser.api.parameters.Input; +import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder; + +import java.io.File; +import java.util.*; +import java.util.stream.Collectors; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.core.IsNull.notNullValue; +import static org.junit.Assert.*; + +public class JToscaImportTest { + + @Test + public void testNoMissingTypeValidationError() throws JToscaException { + String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/sdc-onboarding_csar.csar") + .getFile(); + File file = new File(fileStr); + new ToscaTemplate(file.getAbsolutePath(), null, true, null); + List missingTypeErrors = ThreadLocalsHolder.getCollector().getValidationIssueReport().stream() + .filter(s -> s.contains("JE136")).collect(Collectors.toList()); + assertEquals(0, missingTypeErrors.size()); + } + + @Test + public void testNoStackOverFlowError() { + Exception jte = null; + try { + String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/sdc-onboarding_csar.csar") + .getFile(); + File file = new File(fileStr); + new ToscaTemplate(file.getAbsolutePath(), null, true, null); + } catch (Exception e) { + jte = e; + } + assertEquals(null, jte); + } + + @Test + public void testNoInvalidImports() throws JToscaException { + List fileNames = new ArrayList<>(); + fileNames.add("csars/tmpCSAR_Huawei_vSPGW_fixed.csar"); + fileNames.add("csars/sdc-onboarding_csar.csar"); + fileNames.add("csars/resource-Spgw-csar-ZTE.csar"); + + for (String fileName : fileNames) { + String fileStr = JToscaImportTest.class.getClassLoader().getResource(fileName).getFile(); + File file = new File(fileStr); + new ToscaTemplate(file.getAbsolutePath(), null, true, null); + List invalidImportErrors = ThreadLocalsHolder.getCollector().getValidationIssueReport().stream() + .filter(s -> s.contains("JE195")).collect(Collectors.toList()); + assertEquals(0, invalidImportErrors.size()); + } + } + + @Test + public void testParseAnnotations() throws JToscaException { + + String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-AdiodVmxVpeBvService-csar.csar").getFile(); + File file = new File(fileStr); + ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null); + + List inputs = toscaTemplate.getInputs(); + assertNotNull(inputs); + assertTrue(inputs.stream().filter(i -> i.getAnnotations() != null).collect(Collectors.toList()).isEmpty()); + + inputs.forEach(Input::parseAnnotations); + assertTrue(!inputs.stream().filter(i -> i.getAnnotations() != null).collect(Collectors.toList()).isEmpty()); + } + + @Test + public void testGetInputsWithAndWithoutAnnotations() throws JToscaException { + + String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-AdiodVmxVpeBvService-csar.csar").getFile(); + File file = new File(fileStr); + ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null); + List inputs = toscaTemplate.getInputs(); + assertNotNull(inputs); + assertTrue(inputs.stream().filter(i -> i.getAnnotations() != null).collect(Collectors.toList()).isEmpty()); + + inputs = toscaTemplate.getInputs(true); + assertNotNull(inputs); + validateInputsAnnotations(inputs); + + inputs = toscaTemplate.getInputs(false); + assertNotNull(inputs); + assertTrue(inputs.stream().filter(i -> i.getAnnotations() != null).collect(Collectors.toList()).isEmpty()); + } + + @Test + public void testGetPropertyNameTest() throws JToscaException { + + String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-AdiodVmxVpeBvService-csar.csar").getFile(); + File file = new File(fileStr); + ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null); + NodeTemplate nodeTemplate = toscaTemplate.getNodeTemplates().get(0); + + ArrayList valueList = (ArrayList) nodeTemplate.getPropertyValueFromTemplatesByName("vmxvpfe_sriov41_0_port_vlanfilter"); + assertEquals(4, valueList.size()); + + assertEquals("vPE", (String) nodeTemplate.getPropertyValueFromTemplatesByName("nf_role")); + + assertNull(nodeTemplate.getPropertyValueFromTemplatesByName("test")); + } + + @Test + public void testGetParentNodeTemplateTest() throws JToscaException { + + String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-AdiodVmxVpeBvService-csar.csar").getFile(); + File file = new File(fileStr); + ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null); + NodeTemplate nodeTemplate = toscaTemplate.getNodeTemplates().get(0); + //parent of this VF is service (null) + assertNull(nodeTemplate.getParentNodeTemplate()); + List children = nodeTemplate.getSubMappingToscaTemplate().getNodeTemplates(); + assertFalse(children.isEmpty()); + NodeTemplate cVFC = children.get(4); + //parent is the VF above + assertEquals("2017-488_ADIOD-vPE 0", cVFC.getParentNodeTemplate().getName()); + List children1 = cVFC.getSubMappingToscaTemplate().getNodeTemplates(); + assertFalse(children1.isEmpty()); + //parent is the CVFC above + assertEquals(cVFC, children1.get(0).getParentNodeTemplate()); + +/* + + TopologyTemplate tt = nodeTemplate.getOriginComponentTemplate(); + List groups = tt.getGroups(); + List policies = tt.getPolicies(); + + TopologyTemplate tt1 = cVFC.getOriginComponentTemplate(); + groups = tt.getGroups(); + policies = tt.getPolicies(); +*/ + + } + + @Test + public void testNullValueHasNoNullPointerException() throws JToscaException { + + String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-JennyVtsbcKarunaSvc-csar.csar").getFile(); + File file = new File(fileStr); + ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null); + List inputs = toscaTemplate.getInputs(); + assertNotNull(inputs); + } + + @Test + public void testGetPolicyMetadata() throws JToscaException { + String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-NetworkCloudVnfServiceMock-csar.csar").getFile(); + File file = new File(fileStr); + ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null); + ArrayList policies = toscaTemplate.getPolicies(); + assertNotNull(policies); + assertEquals(1, policies.size()); + assertEquals("org.openecomp.policies.External", policies.get(0).getType()); + assertEquals("adf03496-bf87-43cf-b20a-450e47cb44bd", policies.get(0).getMetaData().getOrDefault("UUID", "").toString()); + assertTrue(policies.get(0).getMetaData().getOrDefault("UUID_test", "").toString().isEmpty()); + } + + @Test + public void testGetPolicyMetadataObj() throws JToscaException { + String fileStr = JToscaImportTest.class.getClassLoader().getResource("csars/service-NetworkCloudVnfServiceMock-csar.csar").getFile(); + File file = new File(fileStr); + ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null); + ArrayList policies = toscaTemplate.getPolicies(); + assertNotNull(policies); + assertEquals(1, policies.size()); + assertEquals("adf03496-bf87-43cf-b20a-450e47cb44bd", policies.get(0).getMetaDataObj().getAllProperties().getOrDefault("UUID", "").toString()); + assertTrue(policies.get(0).getMetaDataObj().getAllProperties().getOrDefault("name_test", "").toString().isEmpty()); + } + + private void validateInputsAnnotations(List inputs) { + List inputsWithAnnotations = inputs.stream().filter(i -> i.getAnnotations() != null) + .collect(Collectors.toList()); + assertTrue(!inputs.isEmpty()); + inputsWithAnnotations.stream().forEach(i -> validateAnnotations(i)); + } + + private void validateAnnotations(Input input) { + assertNotNull(input.getAnnotations()); + assertEquals(input.getAnnotations().size(), 1); + Annotation annotation = input.getAnnotations().get("source"); + assertEquals(annotation.getName(), "source"); + assertEquals(annotation.getType().toLowerCase(), "org.openecomp.annotations.source"); + assertNotNull(annotation.getProperties()); + Optional source_type = annotation.getProperties().stream() + .filter(p -> p.getName().equals("source_type")).findFirst(); + assertTrue(source_type.isPresent()); + assertEquals(source_type.get().getValue(), "HEAT"); + } + + private static final String TEST_DATATYPE_FILENAME = "csars/dataTypes-test-service.csar"; + private static final String TEST_DATATYPE_TEST1 = "TestType1"; + private static final String TEST_DATATYPE_TEST2 = "TestType2"; + private static final String TEST_DATATYPE_PROPERTY_STR = "strdata"; + private static final String TEST_DATATYPE_PROPERTY_INT = "intdata"; + private static final String TEST_DATATYPE_PROPERTY_LIST = "listdata"; + private static final String TEST_DATATYPE_PROPERTY_TYPE = "type"; + private static final String TEST_DATATYPE_PROPERTY_ENTRY_SCHEMA = "entry_schema"; + private static final String TEST_DATATYPE_TOSTRING = "data_types="; + + @Test + public void testGetDataType() throws JToscaException { + String fileStr = JToscaImportTest.class.getClassLoader().getResource(TEST_DATATYPE_FILENAME).getFile(); + File file = new File(fileStr); + ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null); + HashSet dataTypes = toscaTemplate.getDataTypes(); + assertThat(dataTypes, notNullValue()); + assertThat(dataTypes.size(), is(2)); + + for (DataType dataType : dataTypes) { + LinkedHashMap properties; + PropertyDef property; + if (dataType.getType().equals(TEST_DATATYPE_TEST1)) { + properties = dataType.getAllProperties(); + property = properties.get(TEST_DATATYPE_PROPERTY_STR); + assertThat(property, notNullValue()); + assertThat(property.getName(), is(TEST_DATATYPE_PROPERTY_STR)); + assertThat(property.getSchema().get(TEST_DATATYPE_PROPERTY_TYPE), is(Schema.STRING)); + } + if (dataType.getType().equals(TEST_DATATYPE_TEST2)) { + properties = dataType.getAllProperties(); + property = properties.get(TEST_DATATYPE_PROPERTY_INT); + assertThat(property, notNullValue()); + assertThat(property.getName(), is(TEST_DATATYPE_PROPERTY_INT)); + assertThat(property.getSchema().get(TEST_DATATYPE_PROPERTY_TYPE), is(Schema.INTEGER)); + + property = properties.get(TEST_DATATYPE_PROPERTY_LIST); + assertThat(property, notNullValue()); + assertThat(property.getName(), is(TEST_DATATYPE_PROPERTY_LIST)); + assertThat(property.getSchema().get(TEST_DATATYPE_PROPERTY_TYPE), is(Schema.LIST)); + assertThat(property.getSchema().get(TEST_DATATYPE_PROPERTY_ENTRY_SCHEMA), is(TEST_DATATYPE_TEST1)); + + assertThat((LinkedHashMap) toscaTemplate.getTopologyTemplate().getCustomDefs().get(TEST_DATATYPE_TEST1), notNullValue()); + assertThat((LinkedHashMap) toscaTemplate.getTopologyTemplate().getCustomDefs().get(TEST_DATATYPE_TEST2), notNullValue()); + assertThat(toscaTemplate.toString(), containsString(TEST_DATATYPE_TOSTRING)); + } + } + + } + + @Test + public void testGetInputValidate() throws JToscaException { + String fileStr = JToscaImportTest.class.getClassLoader().getResource(TEST_DATATYPE_FILENAME).getFile(); + File file = new File(fileStr); + ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null); + HashSet dataTypes = toscaTemplate.getDataTypes(); + assertThat(dataTypes, notNullValue()); + assertThat(dataTypes.size(), is(2)); + + for (DataType dataType : dataTypes) { + LinkedHashMap properties; + PropertyDef property; + if (dataType.getType().equals(TEST_DATATYPE_TEST1)) { + properties = dataType.getAllProperties(); + property = properties.get(TEST_DATATYPE_PROPERTY_STR); + assertThat(property, notNullValue()); + assertThat(property.getName(), is(TEST_DATATYPE_PROPERTY_STR)); + assertThat(property.getSchema().get(TEST_DATATYPE_PROPERTY_TYPE), is(Schema.STRING)); + } + if (dataType.getType().equals(TEST_DATATYPE_TEST2)) { + properties = dataType.getAllProperties(); + property = properties.get(TEST_DATATYPE_PROPERTY_INT); + assertThat(property, notNullValue()); + assertThat(property.getName(), is(TEST_DATATYPE_PROPERTY_INT)); + assertThat(property.getSchema().get(TEST_DATATYPE_PROPERTY_TYPE), is(Schema.INTEGER)); + + property = properties.get(TEST_DATATYPE_PROPERTY_LIST); + assertThat(property, notNullValue()); + assertThat(property.getName(), is(TEST_DATATYPE_PROPERTY_LIST)); + assertThat(property.getSchema().get(TEST_DATATYPE_PROPERTY_TYPE), is(Schema.LIST)); + assertThat(property.getSchema().get(TEST_DATATYPE_PROPERTY_ENTRY_SCHEMA), is(TEST_DATATYPE_TEST1)); + + assertThat((LinkedHashMap) toscaTemplate.getTopologyTemplate().getCustomDefs().get(TEST_DATATYPE_TEST1), notNullValue()); + assertThat((LinkedHashMap) toscaTemplate.getTopologyTemplate().getCustomDefs().get(TEST_DATATYPE_TEST2), notNullValue()); + assertThat(toscaTemplate.toString(), containsString(TEST_DATATYPE_TOSTRING)); + } + } + } +} diff --git a/jtosca/src/test/java/org/onap/sdc/toscaparser/api/JToscaMetadataParse.java b/jtosca/src/test/java/org/onap/sdc/toscaparser/api/JToscaMetadataParse.java new file mode 100644 index 0000000..2ec41b2 --- /dev/null +++ b/jtosca/src/test/java/org/onap/sdc/toscaparser/api/JToscaMetadataParse.java @@ -0,0 +1,127 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.sdc.toscaparser.api; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.hasSize; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashMap; + +import java.util.Map; +import org.junit.Test; +import org.onap.sdc.toscaparser.api.common.JToscaException; +import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue; +import org.onap.sdc.toscaparser.api.utils.JToscaErrorCodes; +import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder; + +public class JToscaMetadataParse { + + @Test + public void testMetadataParsedCorrectly() throws JToscaException { + final File file = loadCsar("csars/csar_hello_world.csar"); + ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null); + LinkedHashMap metadataProperties = toscaTemplate.getMetaProperties("TOSCA.meta"); + assertNotNull(metadataProperties); + Object entryDefinition = metadataProperties.get("Entry-Definitions"); + assertNotNull(entryDefinition); + assertEquals("tosca_helloworld.yaml", entryDefinition); + } + + @Test + public void noWarningsAfterParse() throws JToscaException { + final File file = loadCsar("csars/tmpCSAR_Huawei_vSPGW_fixed.csar"); + ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null); + int validationIssuesCaught = ThreadLocalsHolder.getCollector().validationIssuesCaught(); + assertTrue(validationIssuesCaught == 0); + } + + @Test + public void requiredInputErrorsAfterParse() throws JToscaException { + final File file = loadCsar("csars/tmpCSAR_Huawei_vSPGW_without_required_inputs.csar"); + new ToscaTemplate(file.getAbsolutePath(), null, true, null); + + final Map validationIssues = ThreadLocalsHolder.getCollector() + .getValidationIssues(); + final Collection actualValidationIssueList = validationIssues.values(); + + final Collection expectedValidationIssueList = new ArrayList<>(); + final String errorCode = "JE003"; + final String errorFormat = "MissingRequiredFieldError: The required input \"%s\" was not provided"; + expectedValidationIssueList.add(new JToscaValidationIssue(errorCode + , String.format(errorFormat, "nf_naming_code"))); + expectedValidationIssueList.add(new JToscaValidationIssue(errorCode + , String.format(errorFormat, "nf_type"))); + expectedValidationIssueList.add(new JToscaValidationIssue(errorCode + , String.format(errorFormat, "nf_role"))); + expectedValidationIssueList.add(new JToscaValidationIssue(errorCode + , String.format(errorFormat, "min_instances"))); + expectedValidationIssueList.add(new JToscaValidationIssue(errorCode + , String.format(errorFormat, "max_instances"))); + expectedValidationIssueList.add(new JToscaValidationIssue(errorCode + , String.format(errorFormat, "nf_function"))); + + assertThat("The actual and the expected validation issue lists should have the same size" + , actualValidationIssueList, hasSize(expectedValidationIssueList.size()) + ); + + assertThat("The actual and the expected validation issue lists should be the same" + , actualValidationIssueList, containsInAnyOrder(expectedValidationIssueList.toArray(new JToscaValidationIssue[0])) + ); + } + + @Test + public void testEmptyCsar() throws JToscaException { + final File file = loadCsar("csars/emptyCsar.csar"); + try { + ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null); + } catch (JToscaException e) { + assertTrue(e.getCode().equals(JToscaErrorCodes.INVALID_CSAR_FORMAT.getValue())); + } + int validationIssuesCaught = ThreadLocalsHolder.getCollector().validationIssuesCaught(); + assertTrue(validationIssuesCaught == 0); + } + + @Test + public void testEmptyPath() throws JToscaException { + String fileStr = JToscaMetadataParse.class.getClassLoader().getResource("").getFile(); + File file = new File(fileStr); + try { + ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null); + } catch (JToscaException e) { + assertTrue(e.getCode().equals(JToscaErrorCodes.PATH_NOT_VALID.getValue())); + } + } + + private File loadCsar(final String csarFilePath) { + final URL resourceUrl = JToscaMetadataParse.class.getClassLoader().getResource(csarFilePath); + assertNotNull(String.format("Could not load CSAR file '%s'", csarFilePath), resourceUrl); + + return new File(resourceUrl.getFile()); + } +} diff --git a/jtosca/src/test/java/org/onap/sdc/toscaparser/api/elements/CalculatePropertyByPathTest.java b/jtosca/src/test/java/org/onap/sdc/toscaparser/api/elements/CalculatePropertyByPathTest.java new file mode 100644 index 0000000..fd84d6e --- /dev/null +++ b/jtosca/src/test/java/org/onap/sdc/toscaparser/api/elements/CalculatePropertyByPathTest.java @@ -0,0 +1,167 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.sdc.toscaparser.api.elements; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.sdc.toscaparser.api.JToscaImportTest; +import org.onap.sdc.toscaparser.api.NodeTemplate; +import org.onap.sdc.toscaparser.api.Property; +import org.onap.sdc.toscaparser.api.ToscaTemplate; +import org.onap.sdc.toscaparser.api.common.JToscaException; + +import java.io.File; +import java.net.URL; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class CalculatePropertyByPathTest { + private static ToscaTemplate toscaTemplate; + + @BeforeClass + public static void setUpClass() throws JToscaException { + URL scarUrl = JToscaImportTest.class.getClassLoader().getResource("csars/service-NetworkCloudVnfServiceMock-csar.csar"); + if (scarUrl != null) { + File file = new File(scarUrl.getFile()); + toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null); + } + + } + + @Test + public void testGetPropertyWhenPropertyHasListOfDataTypesAndPathIsNotEmpty() throws JToscaException { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(0); //testVM_testVM_SRIOVtrunk1_port + + Property property = cp.getProperties().get("related_networks"); + List propertyValueList = property.getLeafPropertyValue("related_network_role"); + assertEquals(3, propertyValueList.size()); + assertTrue(propertyValueList.contains("cor_direct_2")); + assertTrue(propertyValueList.contains("sgi_direct_2")); + assertTrue(propertyValueList.contains("int_imbl_2")); + } + + @Test + public void testGetPropertyWhenPropertyHasDataTypeAndPathIsEmpty() { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(1); //testVM_testVM_SRIOVNonTrunk0_port + + Property property = cp.getProperties().get("exCP_naming"); + List propertyValueList = property.getLeafPropertyValue(""); + assertTrue(propertyValueList.isEmpty()); + } + + @Test + public void testGetPropertyWhenPropertyHasSimpleTypeAndValueAsGetInputIsNotResolvedCorrectlyAndPathIsEmpty() { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(1); //testVM_testVM_SRIOVNonTrunk0_port + + Property property = cp.getProperties().get("network"); + List propertyValueList = property.getLeafPropertyValue(""); + assertTrue(propertyValueList.isEmpty()); + } + + @Test + public void testGetPropertyWhenPropertyHasSimpleTypeAndPathIsEmpty() { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(1); //testVM_testVM_SRIOVNonTrunk0_port + + Property property = cp.getProperties().get("subinterface_indicator"); + List propertyValueList = property.getLeafPropertyValue(""); + assertEquals(1, propertyValueList.size()); + assertEquals("false", propertyValueList.get(0)); + } + + + @Test + public void testGetPropertyWhenPropertyHasDataTypeAndPathIsNotEmpty() { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(2); //testVM_testVM_OVS_port + + Property property = cp.getProperties().get("ip_requirements"); + List propertyValueList = property.getLeafPropertyValue("ip_version"); + assertEquals(1, propertyValueList.size()); + assertEquals("4", propertyValueList.get(0)); + } + + @Test + public void testGetPropertyWhenPropertyHasListOfDataTypesAndPathIsNull() { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(2); //testVM_testVM_OVS_port + + Property property = cp.getProperties().get("ip_requirements"); + assertTrue(property.getLeafPropertyValue(null).isEmpty()); + } + + @Test + public void testGetPropertyWhenPropertyHasListOfDataTypesAndPathIsComplex() { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(0); //testVM_testVM_SRIOVtrunk1_port + + Property property = cp.getProperties().get("ip_requirements"); + List propertyValueList = property.getLeafPropertyValue("ip_count_required#is_required"); + assertEquals(1, propertyValueList.size()); + assertEquals("false", propertyValueList.get(0)); + } + + @Test + public void testGetPropertyWhenPropertyHasListOfDataTypesAndPathIsWrong() { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(0); //testVM_testVM_SRIOVtrunk1_port + + Property property = cp.getProperties().get("ip_requirements"); + List propertyValueList = property.getLeafPropertyValue("ip_count_required#is_required_1"); + assertEquals(0, propertyValueList.size()); + } + + @Test + public void testGetPropertyWhenPropertyHasDataTypeWithoutSchemaAndComplexPath() { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(0); //testVM_testVM_SRIOVtrunk1_port + + Property property = cp.getProperties().get("mac_requirements"); + List propertyValueList = property.getLeafPropertyValue("mac_count_required#is_required"); + assertEquals(1, propertyValueList.size()); + assertEquals("false", propertyValueList.get(0)); + } + + @Test + public void testGetPropertyWhenPropertyHasDataTypeWithoutSchemaAndSimplePath() { + NodeTemplate cp = toscaTemplate.getNodeTemplates().get(0) //Network Cloud VNF MOCK 0 + .getSubMappingToscaTemplate().getNodeTemplates().get(0) //abstract_testVM + .getSubMappingToscaTemplate().getNodeTemplates().get(0); //testVM_testVM_SRIOVtrunk1_port + + Property property = cp.getProperties().get("mac_requirements"); + List propertyValueList = property.getLeafPropertyValue("mac_count_required"); + assertEquals(0, propertyValueList.size()); + } +} diff --git a/jtosca/src/test/java/org/onap/sdc/toscaparser/api/elements/EntityTypeTest.java b/jtosca/src/test/java/org/onap/sdc/toscaparser/api/elements/EntityTypeTest.java new file mode 100644 index 0000000..d65de28 --- /dev/null +++ b/jtosca/src/test/java/org/onap/sdc/toscaparser/api/elements/EntityTypeTest.java @@ -0,0 +1,75 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.sdc.toscaparser.api.elements; + +import org.junit.After; +import org.junit.Test; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +public class EntityTypeTest { + + private static final Map origMap = EntityType.TOSCA_DEF; + + @Test + public void testUpdateDefinitions() throws Exception { + + Map testData = new HashMap<>(); + testData.put("tosca.nodes.nfv.VNF", "{derived_from=tosca.nodes.Root, properties={id={type=string, description=ID of this VNF}, vendor={type=string, description=name of the vendor who generate this VNF}, version={type=version, description=version of the software for this VNF}}, requirements=[{virtualLink={capability=tosca.capabilities.nfv.VirtualLinkable, relationship=tosca.relationships.nfv.VirtualLinksTo, node=tosca.nodes.nfv.VL}}]}"); + testData.put("tosca.nodes.nfv.VDU", "{derived_from=tosca.nodes.Compute, capabilities={high_availability={type=tosca.capabilities.nfv.HA}, virtualbinding={type=tosca.capabilities.nfv.VirtualBindable}, monitoring_parameter={type=tosca.capabilities.nfv.Metric}}, requirements=[{high_availability={capability=tosca.capabilities.nfv.HA, relationship=tosca.relationships.nfv.HA, node=tosca.nodes.nfv.VDU, occurrences=[0, 1]}}]}"); + testData.put("tosca.nodes.nfv.CP", "{derived_from=tosca.nodes.network.Port, properties={type={type=string, required=false}}, requirements=[{virtualLink={capability=tosca.capabilities.nfv.VirtualLinkable, relationship=tosca.relationships.nfv.VirtualLinksTo, node=tosca.nodes.nfv.VL}}, {virtualBinding={capability=tosca.capabilities.nfv.VirtualBindable, relationship=tosca.relationships.nfv.VirtualBindsTo, node=tosca.nodes.nfv.VDU}}], attributes={address={type=string}}}"); + testData.put("tosca.nodes.nfv.VL", "{derived_from=tosca.nodes.network.Network, properties={vendor={type=string, required=true, description=name of the vendor who generate this VL}}, capabilities={virtual_linkable={type=tosca.capabilities.nfv.VirtualLinkable}}}"); + testData.put("tosca.nodes.nfv.VL.ELine", "{derived_from=tosca.nodes.nfv.VL, capabilities={virtual_linkable={occurrences=2}}}"); + testData.put("tosca.nodes.nfv.VL.ELAN", "{derived_from=tosca.nodes.nfv.VL}"); + testData.put("tosca.nodes.nfv.VL.ETree", "{derived_from=tosca.nodes.nfv.VL}"); + testData.put("tosca.nodes.nfv.FP", "{derived_from=tosca.nodes.Root, properties={policy={type=string, required=false, description=name of the vendor who generate this VL}}, requirements=[{forwarder={capability=tosca.capabilities.nfv.Forwarder, relationship=tosca.relationships.nfv.ForwardsTo}}]}"); + testData.put("tosca.groups.nfv.VNFFG", "{derived_from=tosca.groups.Root, properties={vendor={type=string, required=true, description=name of the vendor who generate this VNFFG}, version={type=string, required=true, description=version of this VNFFG}, number_of_endpoints={type=integer, required=true, description=count of the external endpoints included in this VNFFG}, dependent_virtual_link={type=list, entry_schema={type=string}, required=true, description=Reference to a VLD used in this Forwarding Graph}, connection_point={type=list, entry_schema={type=string}, required=true, description=Reference to Connection Points forming the VNFFG}, constituent_vnfs={type=list, entry_schema={type=string}, required=true, description=Reference to a list of VNFD used in this VNF Forwarding Graph}}}"); + testData.put("tosca.relationships.nfv.VirtualLinksTo", "{derived_from=tosca.relationships.network.LinksTo, valid_target_types=[tosca.capabilities.nfv.VirtualLinkable]}"); + testData.put("tosca.relationships.nfv.VirtualBindsTo", "{derived_from=tosca.relationships.network.BindsTo, valid_target_types=[tosca.capabilities.nfv.VirtualBindable]}"); + testData.put("tosca.relationships.nfv.HA", "{derived_from=tosca.relationships.Root, valid_target_types=[tosca.capabilities.nfv.HA]}"); + testData.put("tosca.relationships.nfv.Monitor", "{derived_from=tosca.relationships.ConnectsTo, valid_target_types=[tosca.capabilities.nfv.Metric]}"); + testData.put("tosca.relationships.nfv.ForwardsTo", "{derived_from=tosca.relationships.root, valid_target_types=[tosca.capabilities.nfv.Forwarder]}"); + testData.put("tosca.capabilities.nfv.VirtualLinkable", "{derived_from=tosca.capabilities.network.Linkable}"); + testData.put("tosca.capabilities.nfv.VirtualBindable", "{derived_from=tosca.capabilities.network.Bindable}"); + testData.put("tosca.capabilities.nfv.HA", "{derived_from=tosca.capabilities.Root, valid_source_types=[tosca.nodes.nfv.VDU]}"); + testData.put("tosca.capabilities.nfv.HA.ActiveActive", "{derived_from=tosca.capabilities.nfv.HA}"); + testData.put("tosca.capabilities.nfv.HA.ActivePassive", "{derived_from=tosca.capabilities.nfv.HA}"); + testData.put("tosca.capabilities.nfv.Metric", "{derived_from=tosca.capabilities.Root}"); + testData.put("tosca.capabilities.nfv.Forwarder", "{derived_from=tosca.capabilities.Root}"); + + Map expectedDefMap = origMap; + expectedDefMap.putAll(testData); + EntityType.updateDefinitions("tosca_simple_profile_for_nfv_1_0_0"); + + assertEquals(expectedDefMap, EntityType.TOSCA_DEF); + + } + + @After + public void tearDown() throws Exception { + EntityType.TOSCA_DEF = (LinkedHashMap) origMap; + } + +} diff --git a/jtosca/src/test/java/org/onap/sdc/toscaparser/api/functions/GetInputTest.java b/jtosca/src/test/java/org/onap/sdc/toscaparser/api/functions/GetInputTest.java new file mode 100644 index 0000000..98e5102 --- /dev/null +++ b/jtosca/src/test/java/org/onap/sdc/toscaparser/api/functions/GetInputTest.java @@ -0,0 +1,96 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (c) 2019 Fujitsu Limited. + * ================================================================================ + * 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.sdc.toscaparser.api.functions; + +import org.junit.Test; +import org.onap.sdc.toscaparser.api.*; +import org.onap.sdc.toscaparser.api.common.JToscaException; +import org.onap.sdc.toscaparser.api.elements.constraints.Schema; +import org.onap.sdc.toscaparser.api.parameters.Input; +import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder; + +import java.io.File; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.*; + +public class GetInputTest { + + private static final String TEST_FILENAME = "csars/listed_input.csar"; + private static final String TEST_FILENAME_NG = "csars/listed_input_ng.csar"; + private static final String TEST_PROPERTY_ROLE = "role"; + private static final String TEST_PROPERTY_LONGITUDE = "longitude"; + private static final String TEST_DEFAULT_VALUE = "dsvpn-hub"; + private static final String TEST_DESCRIPTION_VALUE = "This is used for SDWAN only"; + private static final String TEST_INPUT_TYPE = "type"; + private static final String TEST_INPUT_SCHEMA_TYPE = "tosca.datatypes.siteresource.site"; + private static final String TEST_TOSTRING = "get_input:[sites, 1, longitude]"; + private static final String TEST_INPUT_SITES = "sites"; + + @Test + public void validate() throws JToscaException { + String fileStr = JToscaImportTest.class.getClassLoader().getResource(TEST_FILENAME).getFile(); + File file = new File(fileStr); + ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null, false); + NodeTemplate nodeTemplate = toscaTemplate.getNodeTemplates().get(1).getSubMappingToscaTemplate().getNodeTemplates().get(0); + ArrayList inputs = toscaTemplate.getNodeTemplates().get(1).getSubMappingToscaTemplate().getInputs(); + LinkedHashMap properties = nodeTemplate.getProperties(); + assertThat(properties, notNullValue()); + assertThat(properties.size(), is(14)); + + Property property = properties.get(TEST_PROPERTY_ROLE); + assertThat(properties, notNullValue()); + assertThat(property.getName(), is(TEST_PROPERTY_ROLE)); + assertThat(property.getType(), is(Schema.STRING)); + assertThat(property.getDefault(), is(TEST_DEFAULT_VALUE)); + assertThat(property.getDescription(), is(TEST_DESCRIPTION_VALUE)); + GetInput getInput = (GetInput) property.getValue(); + assertThat(getInput.getEntrySchema().get(TEST_INPUT_TYPE).toString(), is(TEST_INPUT_SCHEMA_TYPE)); + + property = properties.get(TEST_PROPERTY_LONGITUDE); + assertThat(properties, notNullValue()); + assertThat(property.getName(), is(TEST_PROPERTY_LONGITUDE)); + assertThat(property.getValue().toString(), is(TEST_TOSTRING)); + getInput = (GetInput) property.getValue(); + ArrayList getInputArguments = getInput.getArguments(); + assertThat(getInputArguments.size(), is(3)); + assertThat(getInputArguments.get(0).toString(), is(TEST_INPUT_SITES)); + assertThat(getInputArguments.get(1).toString(), is("1")); + assertThat(getInputArguments.get(2).toString(), is(TEST_PROPERTY_LONGITUDE)); + + Input in = inputs.get(10); + assertThat(in.getEntrySchema().get(TEST_INPUT_TYPE), is(TEST_INPUT_SCHEMA_TYPE)); + assertThat(in.getName(), is(TEST_INPUT_SITES)); + assertThat(in.getType(), is(Input.LIST)); + } + + @Test + public void validate_ng() throws JToscaException { + //invalid file + String fileStr = JToscaImportTest.class.getClassLoader().getResource(TEST_FILENAME_NG).getFile(); + File file = new File(fileStr); + ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null, false); + + List issues = ThreadLocalsHolder.getCollector().getValidationIssueReport(); + assertTrue(issues.stream().anyMatch(x -> x.contains("JE282"))); + } +} -- cgit 1.2.3-korg