2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
25 package org.onap.ccsdk.sli.core.sli;
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;
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;
53 public class ITCaseSvcLogicParser {
55 private static SvcLogicStore store;
56 private static final Logger LOG = LoggerFactory.getLogger(SvcLogicJdbcStore.class);
59 public static void setUpBeforeClass() throws Exception {
61 LOG.info("before class");
63 URL propUrl = ITCaseSvcLogicParser.class.getResource("/svclogic.properties");
65 InputStream propStr = ITCaseSvcLogicParser.class.getResourceAsStream("/svclogic.properties");
67 Properties props = new Properties();
71 store = SvcLogicStoreFactory.getSvcLogicStore(props);
78 public static void tearDownAfterClass() throws Exception {
79 LOG.info("after class");
83 public void setUp() throws Exception {
88 public void tearDown() throws Exception {
93 * Test method for {@link org.onap.ccsdk.sli.core.sli.SvcLogicParser#parse(java.lang.String)}.
96 public void testParseValidXml() {
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) {
104 testCaseFile = testCaseFile.trim();
106 if (testCaseFile.length() > 0) {
107 if (!testCaseFile.startsWith("/")) {
108 testCaseFile = "/" + testCaseFile;
110 URL testCaseUrl = getClass().getResource(testCaseFile);
111 if (testCaseUrl == null) {
112 fail("Could not resolve test case file " + testCaseFile);
115 // Test parsing and printing
117 SvcLogicParser parser = new SvcLogicParser();
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);
125 } catch (Exception e) {
127 fail("Validation failure [" + e.getMessage() + "]");
131 SvcLogicParser.load(testCaseUrl.getPath(), store);
132 } catch (Exception e) {
134 fail("Validation failure [" + e.getMessage() + "]");
138 } catch (SvcLogicParserException e) {
139 fail("Parser error : " + e.getMessage());
140 } catch (Exception e) {
142 fail("Caught exception processing test cases");
147 public void testDblibLoadValidXml() throws IOException, SQLException, ConfigurationException {
149 URL propUrl = ITCaseSvcLogicParser.class.getResource("/dblib.properties");
151 InputStream propStr = ITCaseSvcLogicParser.class.getResourceAsStream("/dblib.properties");
153 Properties props = new Properties();
157 SvcLogicDblibStore dblibStore = new SvcLogicDblibStore(new DBResourceManager(props));
159 Connection dbConn = dblibStore.getConnection();
161 String dbName = props.getProperty("org.onap.ccsdk.sli.jdbc.database", "sdnctl");
163 DatabaseMetaData dbm;
166 dbm = dbConn.getMetaData();
167 } catch (SQLException e) {
169 throw new ConfigurationException("could not get databse metadata", e);
172 // See if table SVC_LOGIC exists. If not, create it.
173 Statement stmt = null;
176 ResultSet tables = dbm.getTables(null, null, "SVC_LOGIC", null);
178 LOG.debug("SVC_LOGIC table already exists");
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))";
187 stmt = dbConn.createStatement();
188 stmt.executeUpdate(crTableCmd);
190 } catch (Exception e) {
191 throw new ConfigurationException("could not create SVC_LOGIC table", e);
196 } catch (SQLException e) {
197 LOG.error("Statement close error ", e);
202 // See if NODE_TYPES table exists and, if not, create it
206 ResultSet tables = dbm.getTables(null, null, "NODE_TYPES", null);
208 LOG.debug("NODE_TYPES table already exists");
210 String crTableCmd = "CREATE TABLE " + dbName + ".NODE_TYPES (" + "nodetype varchar(80) NOT NULL,"
211 + "CONSTRAINT P_NODE_TYPES PRIMARY KEY(nodetype))";
213 stmt = dbConn.createStatement();
215 stmt.executeUpdate(crTableCmd);
217 } catch (Exception e) {
218 throw new ConfigurationException("could not create SVC_LOGIC table", e);
223 } catch (SQLException e) {
224 LOG.error("Statement close error ", e);
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) {
235 testCaseFile = testCaseFile.trim();
237 if (testCaseFile.length() > 0) {
238 if (!testCaseFile.startsWith("/")) {
239 testCaseFile = "/" + testCaseFile;
241 URL testCaseUrl = getClass().getResource(testCaseFile);
242 if (testCaseUrl == null) {
243 fail("Could not resolve test case file " + testCaseFile);
247 SvcLogicParser.load(testCaseUrl.getPath(), dblibStore);
248 } catch (Exception e) {
250 fail("Validation failure [" + e.getMessage() + "]");
254 } catch (SvcLogicParserException e) {
255 fail("Parser error : " + e.getMessage());
256 } catch (Exception e) {
258 fail("Caught exception processing test cases");
262 @Test(expected = SvcLogicException.class)
263 public void testParseInvalidXml() throws SvcLogicException, IOException {
265 InputStream testStr = getClass().getResourceAsStream("/parser-bad.tests");
266 BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
268 while ((testCaseFile = testsReader.readLine()) != null) {
270 testCaseFile = testCaseFile.trim();
272 if (testCaseFile.length() > 0) {
273 if (!testCaseFile.startsWith("/")) {
274 testCaseFile = "/" + testCaseFile;
276 URL testCaseUrl = getClass().getResource(testCaseFile);
277 if (testCaseUrl == null) {
278 fail("Could not resolve test case file " + testCaseFile);
280 SvcLogicParser.validate(testCaseUrl.getPath(), store);