remove ODL/karaf from common
[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 static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.fail;
29 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.net.URL;
34 import java.sql.Connection;
35 import java.sql.DatabaseMetaData;
36 import java.sql.ResultSet;
37 import java.sql.SQLException;
38 import java.sql.Statement;
39 import java.util.Properties;
40 import org.junit.After;
41 import org.junit.AfterClass;
42 import org.junit.Before;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * @author dt5972
51  *
52  */
53 public class ITCaseSvcLogicParser {
54
55         private static SvcLogicStore store;
56         private static final Logger LOG = LoggerFactory.getLogger(SvcLogicJdbcStore.class);
57
58         @BeforeClass
59         public static void setUpBeforeClass() throws Exception {
60
61                 LOG.info("before class");
62
63                 URL propUrl = ITCaseSvcLogicParser.class.getResource("/svclogic.properties");
64
65                 InputStream propStr = ITCaseSvcLogicParser.class.getResourceAsStream("/svclogic.properties");
66
67                 Properties props = new Properties();
68
69                 props.load(propStr);
70
71         store = SvcLogicStoreFactory.getSvcLogicStore(props);
72
73         assertNotNull(store);
74
75     }
76
77     @AfterClass
78     public static void tearDownAfterClass() throws Exception {
79         LOG.info("after class");
80     }
81
82     @Before
83     public void setUp() throws Exception {
84         LOG.info("before");
85     }
86
87     @After
88     public void tearDown() throws Exception {
89         LOG.info("after");
90     }
91
92     /**
93      * Test method for {@link org.onap.ccsdk.sli.core.sli.SvcLogicParser#parse(java.lang.String)}.
94      */
95     @Test
96     public void testParseValidXml() {
97
98         try {
99             InputStream testStr = getClass().getResourceAsStream("/parser-good.tests");
100             BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
101             String testCaseFile = null;
102             while ((testCaseFile = testsReader.readLine()) != null) {
103
104                 testCaseFile = testCaseFile.trim();
105
106                 if (testCaseFile.length() > 0) {
107                     if (!testCaseFile.startsWith("/")) {
108                         testCaseFile = "/" + testCaseFile;
109                     }
110                     URL testCaseUrl = getClass().getResource(testCaseFile);
111                     if (testCaseUrl == null) {
112                         fail("Could not resolve test case file " + testCaseFile);
113                     }
114                     
115                     // Test parsing and printing
116                     try {
117                         SvcLogicParser parser = new SvcLogicParser();
118                         
119                         for (SvcLogicGraph graph : parser.parse(testCaseUrl.getPath()))  {
120                             System.out.println("XML for graph "+graph.getModule()+":"+graph.getRpc());
121                             graph.printAsXml(System.out);
122                             System.out.println("GV for graph "+graph.getModule()+":"+graph.getRpc());
123                             graph.printAsGv(System.out); 
124                         }
125                     }  catch (Exception e) {
126
127                         fail("Validation failure [" + e.getMessage() + "]");
128                     }
129
130                     try {
131                         SvcLogicParser.load(testCaseUrl.getPath(), store);
132                     } catch (Exception e) {
133
134                         fail("Validation failure [" + e.getMessage() + "]");
135                     }
136                 }
137             }
138         } catch (SvcLogicParserException e) {
139             fail("Parser error : " + e.getMessage());
140         } catch (Exception e) {
141             LOG.error("", e);
142             fail("Caught exception processing test cases");
143         }
144     }
145
146     @Test
147     public void testDblibLoadValidXml() throws IOException, SQLException, ConfigurationException {
148
149         URL propUrl = ITCaseSvcLogicParser.class.getResource("/dblib.properties");
150
151         InputStream propStr = ITCaseSvcLogicParser.class.getResourceAsStream("/dblib.properties");
152
153         Properties props = new Properties();
154
155         props.load(propStr);
156
157         SvcLogicDblibStore dblibStore = new SvcLogicDblibStore(new DBResourceManager(props));
158
159         Connection dbConn = dblibStore.getConnection();
160
161         String dbName = props.getProperty("org.onap.ccsdk.sli.jdbc.database", "sdnctl");
162
163         DatabaseMetaData dbm;
164
165         try {
166             dbm = dbConn.getMetaData();
167         } catch (SQLException e) {
168
169             throw new ConfigurationException("could not get databse metadata", e);
170         }
171
172         // See if table SVC_LOGIC exists. If not, create it.
173         Statement stmt = null;
174         try {
175
176             ResultSet tables = dbm.getTables(null, null, "SVC_LOGIC", null);
177             if (tables.next()) {
178                 LOG.debug("SVC_LOGIC table already exists");
179             } else {
180                 String crTableCmd = "CREATE TABLE " + dbName + ".SVC_LOGIC (" + "module varchar(80) NOT NULL,"
181                         + "rpc varchar(80) NOT NULL," + "version varchar(40) NOT NULL," + "mode varchar(5) NOT NULL,"
182                         + "active varchar(1) NOT NULL,graph BLOB,"
183                         + "modified_timestamp timestamp DEFAULT NULL,"
184                         + "md5sum varchar(128) DEFAULT NULL,"
185                         + "CONSTRAINT P_SVC_LOGIC PRIMARY KEY(module, rpc, version, mode))";
186
187                 stmt = dbConn.createStatement();
188                 stmt.executeUpdate(crTableCmd);
189             }
190         } catch (Exception e) {
191             throw new ConfigurationException("could not create SVC_LOGIC table", e);
192         } finally {
193             if (stmt != null) {
194                 try {
195                     stmt.close();
196                 } catch (SQLException e) {
197                     LOG.error("Statement close error ", e);
198                 }
199             }
200         }
201
202         // See if NODE_TYPES table exists and, if not, create it
203         stmt = null;
204         try {
205
206             ResultSet tables = dbm.getTables(null, null, "NODE_TYPES", null);
207             if (tables.next()) {
208                 LOG.debug("NODE_TYPES table already exists");
209             } else {
210                 String crTableCmd = "CREATE TABLE " + dbName + ".NODE_TYPES (" + "nodetype varchar(80) NOT NULL,"
211                         + "CONSTRAINT P_NODE_TYPES PRIMARY KEY(nodetype))";
212
213                 stmt = dbConn.createStatement();
214
215                 stmt.executeUpdate(crTableCmd);
216             }
217         } catch (Exception e) {
218             throw new ConfigurationException("could not create SVC_LOGIC table", e);
219         } finally {
220             if (stmt != null) {
221                 try {
222                     stmt.close();
223                 } catch (SQLException e) {
224                     LOG.error("Statement close error ", e);
225                 }
226             }
227         }
228
229         try {
230             InputStream testStr = getClass().getResourceAsStream("/parser-good.tests");
231             BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
232             String testCaseFile = null;
233             while ((testCaseFile = testsReader.readLine()) != null) {
234
235                 testCaseFile = testCaseFile.trim();
236
237                 if (testCaseFile.length() > 0) {
238                     if (!testCaseFile.startsWith("/")) {
239                         testCaseFile = "/" + testCaseFile;
240                     }
241                     URL testCaseUrl = getClass().getResource(testCaseFile);
242                     if (testCaseUrl == null) {
243                         fail("Could not resolve test case file " + testCaseFile);
244                     }
245
246                     try {
247                         SvcLogicParser.load(testCaseUrl.getPath(), dblibStore);
248                     } catch (Exception e) {
249
250                         fail("Validation failure [" + e.getMessage() + "]");
251                     }
252                 }
253             }
254         } catch (SvcLogicParserException e) {
255             fail("Parser error : " + e.getMessage());
256         } catch (Exception e) {
257             LOG.error("", e);
258             fail("Caught exception processing test cases");
259         }
260     }
261
262     @Test(expected = SvcLogicException.class)
263     public void testParseInvalidXml() throws SvcLogicException, IOException {
264
265         InputStream testStr = getClass().getResourceAsStream("/parser-bad.tests");
266         BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
267         String testCaseFile;
268         while ((testCaseFile = testsReader.readLine()) != null) {
269
270             testCaseFile = testCaseFile.trim();
271
272             if (testCaseFile.length() > 0) {
273                 if (!testCaseFile.startsWith("/")) {
274                     testCaseFile = "/" + testCaseFile;
275                 }
276                 URL testCaseUrl = getClass().getResource(testCaseFile);
277                 if (testCaseUrl == null) {
278                     fail("Could not resolve test case file " + testCaseFile);
279                 }
280                 SvcLogicParser.validate(testCaseUrl.getPath(), store);
281             }
282         }
283     }
284 }