e8ff2feeb0af323d4b469deb719950b21c11388c
[sdnc/core.git] / sli / common / src / test / java / org / openecomp / sdnc / sli / SvcLogicParserTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
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.openecomp.sdnc.sli;
26
27 import java.io.BufferedReader;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.net.URL;
31 import java.util.LinkedList;
32
33 import org.openecomp.sdnc.sli.SvcLogicParser;
34 import org.openecomp.sdnc.sli.SvcLogicParserException;
35 import org.openecomp.sdnc.sli.SvcLogicStore;
36 import org.openecomp.sdnc.sli.SvcLogicStoreFactory;
37
38 import junit.framework.TestCase;
39
40 /**
41  * @author dt5972
42  *
43  */
44 public class SvcLogicParserTest extends TestCase {
45
46         /**
47          * Test method for {@link org.openecomp.sdnc.sli.SvcLogicParser#parse(java.lang.String)}.
48          */
49         
50         
51         public void testParse() {
52
53                 
54                 try
55                 {
56
57                         URL propUrl = getClass().getResource("/svclogic.properties");
58                         
59                         InputStream propStr = getClass().getResourceAsStream("/svclogic.properties");
60                         
61                         SvcLogicStore store = SvcLogicStoreFactory.getSvcLogicStore(propStr);
62                         
63                         assertNotNull(store);
64                         
65                         store.registerNodeType("switch");
66                         store.registerNodeType("block");
67                         store.registerNodeType("get-resource");
68                         store.registerNodeType("reserve");
69                         store.registerNodeType("is-available");
70                         store.registerNodeType("exists");
71                         store.registerNodeType("configure");
72                         store.registerNodeType("return");
73                         store.registerNodeType("record");
74                         store.registerNodeType("allocate");
75                         store.registerNodeType("release");
76                         store.registerNodeType("for");
77                         store.registerNodeType("set");
78                         
79                         
80                         InputStream testStr = getClass().getResourceAsStream("/parser-good.tests");
81                         BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
82                         String testCaseFile = null;
83                         while ((testCaseFile = testsReader.readLine()) != null) {
84                                 
85                                 testCaseFile = testCaseFile.trim();
86                                 
87                                 if (testCaseFile.length() > 0)
88                                 {
89                                         if (!testCaseFile.startsWith("/"))
90                                         {
91                                                 testCaseFile = "/"+testCaseFile;
92                                         }
93                                         URL testCaseUrl = getClass().getResource(testCaseFile);
94                                         if (testCaseUrl == null)
95                                         {
96                                                 fail("Could not resolve test case file "+testCaseFile);
97                                         }
98
99                                         try {
100                                                 SvcLogicParser.validate(testCaseUrl.getPath(), store);
101                                         } catch (Exception e) {
102                                                 fail("Validation failure ["+e.getMessage()+"]");
103                                                 
104                                         }
105
106                                         
107                                         
108                                         
109
110                                 }
111                         }
112                         
113                         testStr = getClass().getResourceAsStream("/parser-bad.tests");
114                         testsReader = new BufferedReader(new InputStreamReader(testStr));
115                         testCaseFile = null;
116                         while ((testCaseFile = testsReader.readLine()) != null) {
117                                 
118                                 testCaseFile = testCaseFile.trim();
119                                 
120                                 if (testCaseFile.length() > 0)
121                                 {
122                                         if (!testCaseFile.startsWith("/"))
123                                         {
124                                                 testCaseFile = "/"+testCaseFile;
125                                         }
126                                         URL testCaseUrl = getClass().getResource(testCaseFile);
127                                         if (testCaseUrl == null)
128                                         {
129                                                 fail("Could not resolve test case file "+testCaseFile);
130                                         }
131
132                                         boolean valid = true;
133                                         try {
134                                                 SvcLogicParser.load(testCaseUrl.getPath(), store);
135                                         } catch (Exception e) {
136                                                 System.out.println(e.getMessage());
137                                                 valid = false;
138                                         }
139
140                                         if (valid) {
141                                                 fail("Expected compiler error on "+testCaseFile+", but got success");
142                                         }
143                                         
144
145                                 }
146                         }
147                 }
148                 catch (SvcLogicParserException e)
149                 {
150                         fail("Parser error : "+e.getMessage());
151                 }
152                 catch (Exception e)
153                 {
154                         e.printStackTrace();
155                         fail("Caught exception processing test cases");
156                 }
157                 
158                 
159         }
160         
161         
162
163 }