2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.sdc.impl;
23 import org.testng.annotations.Test;
24 import static org.testng.Assert.*;
28 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
29 import org.onap.sdc.toscaparser.api.utils.JToscaErrorCodes;
33 public class ToscaParserErrorHandlingTest extends SdcToscaParserBasicTest {
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");
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");
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");
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");
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");
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");
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);
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);
111 public void testFileNotFound(){
112 Throwable captureThrowable = captureThrowable("csars/XXX.csar");
113 testThrowable(captureThrowable, "TP0001");
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");
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);
131 public static Throwable captureThrowable(String csarPath) {
132 Throwable result = null;
134 factory.getSdcCsarHelper(csarPath);
135 } catch( Throwable throwable ) {