remove mac validation
[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
13 public class ToscaParserErrorHandlingTest extends SdcToscaParserBasicTest {
14         
15         
16         @Test
17         public void testMissingMetadata(){
18                 String csarPath = "csars/service-missing-meta-file.csar";
19                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
20         File file = new File(fileLocationString);
21                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
22                 testThrowable(captureThrowable, "TP0002");
23         }
24         
25         
26         @Test
27         public void testInvalidYamlContentMeta(){
28                 String csarPath = "csars/service-invalid-yaml-content-meta.csar";
29                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
30         File file = new File(fileLocationString);
31                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
32                 testThrowable(captureThrowable, "TP0002");
33         }
34         
35         @Test
36         public void testEntryDefinitionNotDefined(){
37                 String csarPath = "csars/service-entry-definition-not-defined.csar";
38                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
39         File file = new File(fileLocationString);
40                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
41                 testThrowable(captureThrowable, "TP0002");
42         }
43
44         @Test
45         public void testMissingEntryDefinitionFile(){
46                 String csarPath = "csars/service-missing-entry-definition.csar";
47                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
48         File file = new File(fileLocationString);
49                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
50                 testThrowable(captureThrowable, "TP0002");
51         }
52         
53         //@Test - PA - there are currently no critical erros in JTosca
54         public void tesValidationError(){
55                 String csarPath = "csars/service-invalid-input-args.csar";
56                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
57         File file = new File(fileLocationString);
58                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
59                 testThrowable(captureThrowable, "TP0002");
60         }
61         
62         @Test
63         public void testInValidMinConformanceLevelError(){
64                 String csarPath = "csars/service-invalid-conformence-level.csar";
65                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
66         File file = new File(fileLocationString);
67                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
68                 testThrowable(captureThrowable, "TP0003");
69         }
70
71         @Test
72         public void testIgnoreMaxConformanceLevelNoError(){
73                 String csarPath = "csars/service-max-conformence-level.csar";
74                 //TODO: Currently, the conformentce level of the csar for this test is 99 (hard coded). Consider to add ability to replace the configuration in run time.
75                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
76                 File file = new File(fileLocationString);
77                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
78                 assertNull(captureThrowable);
79         }
80         
81         @Test
82         public void testFileNotFound(){
83                 Throwable captureThrowable = captureThrowable("csars/XXX.csar");
84                 testThrowable(captureThrowable, "TP0001");
85         }
86         
87         @Test
88         public void testInvalidCsarFormat(){
89                 String csarPath = "csars/csar-invalid-zip.zip";
90                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
91         File file = new File(fileLocationString);
92                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
93                 testThrowable(captureThrowable, "TP0002");
94         }
95
96         private static void testThrowable(Throwable captureThrowable, String expectedCode) {
97                 assertNotNull(captureThrowable);
98                 assertTrue(captureThrowable instanceof SdcToscaParserException, "Error thrown is of type "+captureThrowable.getClass().getSimpleName());
99                 assertEquals(((SdcToscaParserException)captureThrowable).getCode(), expectedCode);
100         }
101         
102         public static Throwable captureThrowable(String csarPath) {
103                 Throwable result = null;
104                 try {
105                         factory.getSdcCsarHelper(csarPath);
106                 } catch( Throwable throwable ) {
107                         result = throwable;
108                 }
109                 return result;
110         }
111 }