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.onap.ccsdk.sli.adaptors.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 ch.vorburger.mariadb4j.DB;
36 import ch.vorburger.mariadb4j.DBConfigurationBuilder;
37 import junit.framework.TestCase;
39 public class ITCaseSqlResource extends TestCase {
41 private static final Logger LOG = LoggerFactory
42 .getLogger(ITCaseSqlResource.class);
45 public void testExists() throws Exception {
48 Properties props = new Properties();
49 InputStream propStr = getClass().getResourceAsStream("/svclogic.properties");
50 if (propStr == null) {
51 fail("src/test/resources/svclogic.properties missing");
57 } catch (Exception e) {
59 fail("Could not initialize properties");
63 // Start MariaDB4j database
64 DBConfigurationBuilder config = DBConfigurationBuilder.newBuilder();
65 config.setPort(0); // 0 => autom. detect free port
66 DB db = DB.newEmbeddedDB(config.build());
69 // Override jdbc URL and database name
70 props.setProperty("org.onap.ccsdk.sli.jdbc.database", "test");
71 props.setProperty("org.onap.ccsdk.sli.jdbc.url", config.getURL("test"));
73 // Add properties to global properties
75 Enumeration propNames = props.keys();
77 while (propNames.hasMoreElements()) {
78 String propName = (String) propNames.nextElement();
80 System.setProperty(propName, props.getProperty(propName));
83 SqlResource sqlResource = new SqlResource(new SqlResourcePropertiesProviderImpl());
87 InputStream testStr = getClass().getResourceAsStream("/save.tests");
88 BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
89 SvcLogicContext ctx = new SvcLogicContext();
92 String testExpr = null;
95 while ((testExpr = testsReader.readLine()) != null) {
96 testExpr = testExpr.trim();
98 if (testExpr.startsWith("#")) {
99 testExpr = testExpr.substring(1).trim();
100 String[] nameValue = testExpr.split("=");
101 String name = nameValue[0].trim();
102 String value = nameValue[1].trim();
104 LOG.info("Setting context attribute " + name + " = "
106 ctx.setAttribute(name, value);
111 String sqlStmt = testExpr;
112 QueryStatus status = sqlResource.save("SQL", true, false, sqlStmt, null, "savetest"+testNum, ctx);
116 LOG.info("Found data for query [" + sqlStmt + "]");
119 LOG.info("Did not data for query [" + sqlStmt + "]");
122 fail("Failure executing query [" + sqlStmt + "]");
128 } catch (Exception e) {
130 fail("Caught exception running tests");
134 testStr = getClass().getResourceAsStream("/query.tests");
135 testsReader = new BufferedReader(new InputStreamReader(testStr));
138 String testExpr = null;
141 while ((testExpr = testsReader.readLine()) != null) {
142 testExpr = testExpr.trim();
143 if (testExpr.startsWith("#")) {
144 testExpr = testExpr.substring(1).trim();
145 String[] nameValue = testExpr.split("=");
146 String name = nameValue[0].trim();
147 String value = nameValue[1].trim();
149 LOG.info("Setting context attribute " + name + " = "
151 ctx.setAttribute(name, value);
157 String sqlStmt = testExpr;
158 QueryStatus status = sqlResource.query("SQL", false, null,
159 sqlStmt, "querytest" + testNum, null, ctx);
163 LOG.info("Found data for query [" + sqlStmt + "]");
166 LOG.info("Did not data for query [" + sqlStmt + "]");
169 fail("Failure executing query [" + sqlStmt + "]");
175 } catch (Exception e) {
177 fail("Caught exception running tests");
181 testStr = getClass().getResourceAsStream("/delete.tests");
182 testsReader = new BufferedReader(new InputStreamReader(testStr));
185 String testExpr = null;
188 while ((testExpr = testsReader.readLine()) != null) {
189 testExpr = testExpr.trim();
190 if (testExpr.startsWith("#")) {
191 testExpr = testExpr.substring(1).trim();
192 String[] nameValue = testExpr.split("=");
193 String name = nameValue[0].trim();
194 String value = nameValue[1].trim();
196 LOG.info("Setting context attribute " + name + " = "
198 ctx.setAttribute(name, value);
204 String sqlStmt = testExpr;
205 QueryStatus status = sqlResource.delete("SQL", sqlStmt, ctx);
209 LOG.info("Found data for query [" + sqlStmt + "]");
212 LOG.info("Did not data for query [" + sqlStmt + "]");
215 fail("Failure executing query [" + sqlStmt + "]");
221 } catch (Exception e) {
223 fail("Caught exception running tests");
226 for (String attrName : ctx.getAttributeKeySet()) {
227 LOG.info("ctx.getAttribute("+attrName+") = "+ctx.getAttribute(attrName));