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=========================================================
22 package org.openecomp.sdnc.sli.resource.sql;
24 import java.io.BufferedReader;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.util.Enumeration;
28 import java.util.Properties;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import junit.framework.TestCase;
37 public class SqlResourceTest extends TestCase {
39 private static final Logger LOG = LoggerFactory
40 .getLogger(SqlResourceTest.class);
43 public void testExists() {
46 Properties props = new Properties();
47 InputStream propStr = getClass().getResourceAsStream("/svclogic.properties");
48 if (propStr == null) {
49 fail("src/test/resources/svclogic.properties missing");
55 } catch (Exception e) {
57 fail("Could not initialize properties");
60 // Add properties to global properties
62 Enumeration propNames = props.keys();
64 while (propNames.hasMoreElements()) {
65 String propName = (String) propNames.nextElement();
67 System.setProperty(propName, props.getProperty(propName));
70 SqlResource sqlResource = new SqlResource();
74 InputStream testStr = getClass().getResourceAsStream("/save.tests");
75 BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
76 SvcLogicContext ctx = new SvcLogicContext();
79 String testExpr = null;
82 while ((testExpr = testsReader.readLine()) != null) {
83 testExpr = testExpr.trim();
85 if (testExpr.startsWith("#")) {
86 testExpr = testExpr.substring(1).trim();
87 String[] nameValue = testExpr.split("=");
88 String name = nameValue[0].trim();
89 String value = nameValue[1].trim();
91 LOG.info("Setting context attribute " + name + " = "
93 ctx.setAttribute(name, value);
98 String sqlStmt = testExpr;
99 QueryStatus status = sqlResource.save("SQL", true, false, sqlStmt, null, "savetest"+testNum, ctx);
103 LOG.info("Found data for query [" + sqlStmt + "]");
106 LOG.info("Did not data for query [" + sqlStmt + "]");
109 fail("Failure executing query [" + sqlStmt + "]");
115 } catch (Exception e) {
117 fail("Caught exception running tests");
121 testStr = getClass().getResourceAsStream("/query.tests");
122 testsReader = new BufferedReader(new InputStreamReader(testStr));
125 String testExpr = null;
128 while ((testExpr = testsReader.readLine()) != null) {
129 testExpr = testExpr.trim();
130 if (testExpr.startsWith("#")) {
131 testExpr = testExpr.substring(1).trim();
132 String[] nameValue = testExpr.split("=");
133 String name = nameValue[0].trim();
134 String value = nameValue[1].trim();
136 LOG.info("Setting context attribute " + name + " = "
138 ctx.setAttribute(name, value);
144 String sqlStmt = testExpr;
145 QueryStatus status = sqlResource.query("SQL", false, null,
146 sqlStmt, "querytest" + testNum, null, ctx);
150 LOG.info("Found data for query [" + sqlStmt + "]");
153 LOG.info("Did not data for query [" + sqlStmt + "]");
156 fail("Failure executing query [" + sqlStmt + "]");
162 } catch (Exception e) {
164 fail("Caught exception running tests");
168 testStr = getClass().getResourceAsStream("/delete.tests");
169 testsReader = new BufferedReader(new InputStreamReader(testStr));
172 String testExpr = null;
175 while ((testExpr = testsReader.readLine()) != null) {
176 testExpr = testExpr.trim();
177 if (testExpr.startsWith("#")) {
178 testExpr = testExpr.substring(1).trim();
179 String[] nameValue = testExpr.split("=");
180 String name = nameValue[0].trim();
181 String value = nameValue[1].trim();
183 LOG.info("Setting context attribute " + name + " = "
185 ctx.setAttribute(name, value);
191 String sqlStmt = testExpr;
192 QueryStatus status = sqlResource.delete("SQL", sqlStmt, ctx);
196 LOG.info("Found data for query [" + sqlStmt + "]");
199 LOG.info("Did not data for query [" + sqlStmt + "]");
202 fail("Failure executing query [" + sqlStmt + "]");
208 } catch (Exception e) {
210 fail("Caught exception running tests");
213 for (String attrName : ctx.getAttributeKeySet()) {
214 LOG.info("ctx.getAttribute("+attrName+") = "+ctx.getAttribute(attrName));