Revert "vLAN . Support Policies."
[sdc/sdc-tosca.git] / src / test / java / org / onap / sdc / impl / ToscaParserErrorHandlingTest.java
1 package org.onap.sdc.impl;
2
3 import org.testng.annotations.Test;
4 import static org.testng.Assert.*;
5
6 import java.io.File;
7
8 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
9 import org.onap.sdc.toscaparser.api.utils.JToscaErrorCodes;
10
11
12 /*put(JToscaErrorCodes.GENERAL_ERROR, GENERAL_ERROR);
13
14 put(JToscaErrorCodes.PATH_NOT_VALID, FILE_NOT_FOUND);
15 //CSAR contents problems
16 put(JToscaErrorCodes.MISSING_META_FILE, BAD_FORMAT);
17 put(JToscaErrorCodes.INVALID_META_YAML_CONTENT, BAD_FORMAT);
18 put(JToscaErrorCodes.ENTRY_DEFINITION_NOT_DEFINED, BAD_FORMAT);
19 put(JToscaErrorCodes.MISSING_ENTRY_DEFINITION_FILE, BAD_FORMAT);
20 put(JToscaErrorCodes.CSAR_TOSCA_VALIDATION_ERROR, BAD_FORMAT);
21
22  MISSING_META_FILE("JT1001"),
23 /*     INVALID_META_YAML_CONTENT("JT1002"),
24 /*     ENTRY_DEFINITION_NOT_DEFINED("JT1003"),
25 /*     MISSING_ENTRY_DEFINITION_FILE("JT1004"),
26 /*     GENERAL_ERROR("JT1005"),
27 /*     PATH_NOT_VALID("JT1006"),
28 /*     CSAR_TOSCA_VALIDATION_ERROR("JT1007");
29
30 */
31
32 /*
33  * 
34  * # Errors
35 errors:
36     FILE_NOT_FOUND: {
37         code: TP0001,
38         message: "Error: CSAR file not found."
39     }
40     BAD_FORMAT: {
41         code: TP0002,
42         message: "Error: CSAR file bad format. Check the log for details."
43     }
44     CONFORMANCE_LEVEL_ERROR: {
45         code: TP0003,
46         message: "Error: CSAR version is unsupported. Parser supports versions %s to %s." 
47     }
48     GENERAL_ERROR: {
49         code: TP0004,
50         message: "Error: an unexpected internal error occured."
51     }
52  * 
53  */
54
55 public class ToscaParserErrorHandlingTest extends SdcToscaParserBasicTest {
56         
57         
58         @Test
59         public void testMissingMetadata(){
60                 String csarPath = "csars/service-missing-meta-file.csar";
61                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
62         File file = new File(fileLocationString);
63                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
64                 testThrowable(captureThrowable, "TP0002");
65         }
66         
67         
68         @Test
69         public void testInvalidYamlContentMeta(){
70                 String csarPath = "csars/service-invalid-yaml-content-meta.csar";
71                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
72         File file = new File(fileLocationString);
73                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
74                 testThrowable(captureThrowable, "TP0002");
75         }
76         
77         @Test
78         public void testEntryDefinitionNotDefined(){
79                 String csarPath = "csars/service-entry-definition-not-defined.csar";
80                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
81         File file = new File(fileLocationString);
82                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
83                 testThrowable(captureThrowable, "TP0002");
84         }
85
86         @Test
87         public void testMissingEntryDefinitionFile(){
88                 String csarPath = "csars/service-missing-entry-definition.csar";
89                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
90         File file = new File(fileLocationString);
91                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
92                 testThrowable(captureThrowable, "TP0002");
93         }
94         
95         //@Test - PA - there are currently no critical erros in JTosca
96         public void tesValidationError(){
97                 String csarPath = "csars/service-invalid-input-args.csar";
98                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
99         File file = new File(fileLocationString);
100                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
101                 testThrowable(captureThrowable, "TP0002");
102         }
103         
104         @Test
105         public void testInValidConformanceLevelError(){
106                 String csarPath = "csars/service-invalid-conformence-level.csar";
107                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
108         File file = new File(fileLocationString);
109                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
110                 testThrowable(captureThrowable, "TP0003");
111         }
112         
113         @Test
114         public void testFileNotFound(){
115                 Throwable captureThrowable = captureThrowable("csars/XXX.csar");
116                 testThrowable(captureThrowable, "TP0001");
117         }
118         
119         @Test
120         public void testInvalidCsarFormat(){
121                 String csarPath = "csars/csar-invalid-zip.zip";
122                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
123         File file = new File(fileLocationString);
124                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
125                 testThrowable(captureThrowable, "TP0002");
126         }
127
128         private static void testThrowable(Throwable captureThrowable, String expectedCode) {
129                 assertNotNull(captureThrowable);
130                 assertTrue(captureThrowable instanceof SdcToscaParserException, "Error thrown is of type "+captureThrowable.getClass().getSimpleName());
131                 assertEquals(((SdcToscaParserException)captureThrowable).getCode(), expectedCode);
132         }
133         
134         public static Throwable captureThrowable(String csarPath) {
135                 Throwable result = null;
136                 try {
137                         factory.getSdcCsarHelper(csarPath);
138                 } catch( Throwable throwable ) {
139                         result = throwable;
140                 }
141                 return result;
142         }
143 }