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