diff options
Diffstat (limited to 'src/test/java/org/onap')
4 files changed, 259 insertions, 0 deletions
diff --git a/src/test/java/org/onap/sdc/toscaparser/api/GetValidationIssues.java b/src/test/java/org/onap/sdc/toscaparser/api/GetValidationIssues.java new file mode 100644 index 0000000..a5afa6b --- /dev/null +++ b/src/test/java/org/onap/sdc/toscaparser/api/GetValidationIssues.java @@ -0,0 +1,79 @@ +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<String[]> 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/src/test/java/org/onap/sdc/toscaparser/api/JToscaImportTest.java b/src/test/java/org/onap/sdc/toscaparser/api/JToscaImportTest.java new file mode 100644 index 0000000..589e47c --- /dev/null +++ b/src/test/java/org/onap/sdc/toscaparser/api/JToscaImportTest.java @@ -0,0 +1,64 @@ +package org.onap.sdc.toscaparser.api; + +import org.junit.Test; +import org.onap.sdc.toscaparser.api.common.JToscaException; +import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +import static org.junit.Assert.assertEquals; + +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<String> 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<String> 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<String> invalidImportErrors = ThreadLocalsHolder.getCollector() + .getValidationIssueReport() + .stream() + .filter(s -> s.contains("JE195")) + .collect(Collectors.toList()); + assertEquals(0, invalidImportErrors.size()); + } + } + +} diff --git a/src/test/java/org/onap/sdc/toscaparser/api/JToscaMetadataParse.java b/src/test/java/org/onap/sdc/toscaparser/api/JToscaMetadataParse.java new file mode 100644 index 0000000..37c6d18 --- /dev/null +++ b/src/test/java/org/onap/sdc/toscaparser/api/JToscaMetadataParse.java @@ -0,0 +1,61 @@ +package org.onap.sdc.toscaparser.api; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.util.LinkedHashMap; + +import org.junit.Test; +import org.onap.sdc.toscaparser.api.common.JToscaException; +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 { + String fileStr = JToscaMetadataParse.class.getClassLoader().getResource("csars/csar_hello_world.csar").getFile(); + File file = new File(fileStr); + ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null); + LinkedHashMap<String, Object> 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 { + String fileStr = JToscaMetadataParse.class.getClassLoader().getResource("csars/tmpCSAR_Huawei_vSPGW_fixed.csar").getFile(); + File file = new File(fileStr); + ToscaTemplate toscaTemplate = new ToscaTemplate(file.getAbsolutePath(), null, true, null); + int validationIssuesCaught = ThreadLocalsHolder.getCollector().validationIssuesCaught(); + assertTrue(validationIssuesCaught == 0 ); + } + + @Test + public void testEmptyCsar() throws JToscaException { + String fileStr = JToscaMetadataParse.class.getClassLoader().getResource("csars/emptyCsar.csar").getFile(); + File file = new File(fileStr); + 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())); + } + } +} diff --git a/src/test/java/org/onap/sdc/toscaparser/api/elements/EntityTypeTest.java b/src/test/java/org/onap/sdc/toscaparser/api/elements/EntityTypeTest.java new file mode 100644 index 0000000..2a88c2b --- /dev/null +++ b/src/test/java/org/onap/sdc/toscaparser/api/elements/EntityTypeTest.java @@ -0,0 +1,55 @@ +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<String, Object> origMap = EntityType.TOSCA_DEF; + + @Test + public void testUpdateDefinitions() throws Exception { + + Map<String, Object> 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<String, Object> 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<String, Object>) origMap; + } + +}
\ No newline at end of file |