6c88494dc20bb9bb24822d145b110ba6c5fe3589
[sdc/sdc-tosca.git] / sdc-tosca / src / test / java / org / onap / sdc / impl / ToscaParserErrorHandlingTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * sdc-tosca
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.sdc.impl;
22
23 import org.testng.annotations.Test;
24 import static org.testng.Assert.*;
25
26 import java.io.File;
27
28 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
29 import org.onap.sdc.toscaparser.api.utils.JToscaErrorCodes;
30
31
32
33 public class ToscaParserErrorHandlingTest extends SdcToscaParserBasicTest {
34         
35         
36         @Test
37         public void testMissingMetadata(){
38                 String csarPath = "csars/service-missing-meta-file.csar";
39                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
40         File file = new File(fileLocationString);
41                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
42                 testThrowable(captureThrowable, "TP0002");
43         }
44         
45         
46         @Test
47         public void testInvalidYamlContentMeta(){
48                 String csarPath = "csars/service-invalid-yaml-content-meta.csar";
49                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
50         File file = new File(fileLocationString);
51                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
52                 testThrowable(captureThrowable, "TP0002");
53         }
54         
55         @Test
56         public void testEntryDefinitionNotDefined(){
57                 String csarPath = "csars/service-entry-definition-not-defined.csar";
58                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
59         File file = new File(fileLocationString);
60                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
61                 testThrowable(captureThrowable, "TP0002");
62         }
63
64         @Test
65         public void testMissingEntryDefinitionFile(){
66                 String csarPath = "csars/service-missing-entry-definition.csar";
67                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
68         File file = new File(fileLocationString);
69                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
70                 testThrowable(captureThrowable, "TP0002");
71         }
72         
73         //@Test - PA - there are currently no critical erros in JTosca
74         public void tesValidationError(){
75                 String csarPath = "csars/service-invalid-input-args.csar";
76                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
77         File file = new File(fileLocationString);
78                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
79                 testThrowable(captureThrowable, "TP0002");
80         }
81         
82         @Test
83         public void testInValidMinConformanceLevelError(){
84                 String csarPath = "csars/service-invalid-conformence-level.csar";
85                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
86         File file = new File(fileLocationString);
87                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
88                 testThrowable(captureThrowable, "TP0003");
89         }
90
91         @Test
92         public void testIgnoreMaxConformanceLevelNoError(){
93                 String csarPath = "csars/service-max-conformence-level.csar";
94                 //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.
95                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
96                 File file = new File(fileLocationString);
97                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
98                 assertNull(captureThrowable);
99         }
100
101         @Test
102         public void testVerifyConformanceLevelVersion9(){
103                 String csarPath = "csars/service-Servicetosca9-csar.csar";
104                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
105                 File file = new File(fileLocationString);
106                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
107                 assertNull(captureThrowable);
108         }
109         
110         @Test
111         public void testFileNotFound(){
112                 Throwable captureThrowable = captureThrowable("csars/XXX.csar");
113                 testThrowable(captureThrowable, "TP0001");
114         }
115         
116         @Test
117         public void testInvalidCsarFormat(){
118                 String csarPath = "csars/csar-invalid-zip.zip";
119                 String fileLocationString = ToscaParserErrorHandlingTest.class.getClassLoader().getResource(csarPath).getFile();
120         File file = new File(fileLocationString);
121                 Throwable captureThrowable = captureThrowable(file.getAbsolutePath());
122                 testThrowable(captureThrowable, "TP0002");
123         }
124
125         private static void testThrowable(Throwable captureThrowable, String expectedCode) {
126                 assertNotNull(captureThrowable);
127                 assertTrue(captureThrowable instanceof SdcToscaParserException, "Error thrown is of type "+captureThrowable.getClass().getSimpleName());
128                 assertEquals(((SdcToscaParserException)captureThrowable).getCode(), expectedCode);
129         }
130         
131         public static Throwable captureThrowable(String csarPath) {
132                 Throwable result = null;
133                 try {
134                         factory.getSdcCsarHelper(csarPath);
135                 } catch( Throwable throwable ) {
136                         result = throwable;
137                 }
138                 return result;
139         }
140 }