0e6000fb24514d046d2342b2f123fcd1f37cd32a
[ccsdk/sli/core.git] / sli / common / src / test / java / org / onap / ccsdk / sli / core / sli / ITCaseSvcLogicParser.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 /**
23  * 
24  */
25 package org.onap.ccsdk.sli.core.sli;
26
27 import java.io.BufferedReader;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.net.URL;
31
32 import junit.framework.TestCase;
33
34 /**
35  * @author dt5972
36  *
37  */
38 public class ITCaseSvcLogicParser extends TestCase {
39
40         /**
41          * Test method for {@link org.onap.ccsdk.sli.core.sli.SvcLogicParser#parse(java.lang.String)}.
42          */
43         
44         
45         public void testParse() {
46
47                 
48                 try
49                 {
50
51                         URL propUrl = getClass().getResource("/svclogic.properties");
52                         
53                         InputStream propStr = getClass().getResourceAsStream("/svclogic.properties");
54                         
55                         SvcLogicStore store = SvcLogicStoreFactory.getSvcLogicStore(propStr);
56                         
57                         assertNotNull(store);
58                         
59                         store.registerNodeType("switch");
60                         store.registerNodeType("block");
61                         store.registerNodeType("get-resource");
62                         store.registerNodeType("reserve");
63                         store.registerNodeType("is-available");
64                         store.registerNodeType("exists");
65                         store.registerNodeType("configure");
66                         store.registerNodeType("return");
67                         store.registerNodeType("record");
68                         store.registerNodeType("allocate");
69                         store.registerNodeType("release");
70                         store.registerNodeType("for");
71                         store.registerNodeType("set");
72                         
73                         
74                         InputStream testStr = getClass().getResourceAsStream("/parser-good.tests");
75                         BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
76                         String testCaseFile = null;
77                         while ((testCaseFile = testsReader.readLine()) != null) {
78                                 
79                                 testCaseFile = testCaseFile.trim();
80                                 
81                                 if (testCaseFile.length() > 0)
82                                 {
83                                         if (!testCaseFile.startsWith("/"))
84                                         {
85                                                 testCaseFile = "/"+testCaseFile;
86                                         }
87                                         URL testCaseUrl = getClass().getResource(testCaseFile);
88                                         if (testCaseUrl == null)
89                                         {
90                                                 fail("Could not resolve test case file "+testCaseFile);
91                                         }
92
93                                         try {
94                                                 SvcLogicParser.validate(testCaseUrl.getPath(), store);
95                                         } catch (Exception e) {
96                                                 fail("Validation failure ["+e.getMessage()+"]");
97                                                 
98                                         }
99
100                                         
101                                         
102                                         
103
104                                 }
105                         }
106                         
107                         testStr = getClass().getResourceAsStream("/parser-bad.tests");
108                         testsReader = new BufferedReader(new InputStreamReader(testStr));
109                         testCaseFile = null;
110                         while ((testCaseFile = testsReader.readLine()) != null) {
111                                 
112                                 testCaseFile = testCaseFile.trim();
113                                 
114                                 if (testCaseFile.length() > 0)
115                                 {
116                                         if (!testCaseFile.startsWith("/"))
117                                         {
118                                                 testCaseFile = "/"+testCaseFile;
119                                         }
120                                         URL testCaseUrl = getClass().getResource(testCaseFile);
121                                         if (testCaseUrl == null)
122                                         {
123                                                 fail("Could not resolve test case file "+testCaseFile);
124                                         }
125
126                                         boolean valid = true;
127                                         try {
128                                                 SvcLogicParser.load(testCaseUrl.getPath(), store);
129                                         } catch (Exception e) {
130                                                 System.out.println(e.getMessage());
131                                                 valid = false;
132                                         }
133
134                                         if (valid) {
135                                                 fail("Expected compiler error on "+testCaseFile+", but got success");
136                                         }
137                                         
138
139                                 }
140                         }
141                 }
142                 catch (SvcLogicParserException e)
143                 {
144                         fail("Parser error : "+e.getMessage());
145                 }
146                 catch (Exception e)
147                 {
148                         e.printStackTrace();
149                         fail("Caught exception processing test cases");
150                 }
151                 
152                 
153         }
154         
155         
156
157 }