Fix license headers
[ccsdk/sli/adaptors.git] / sql-resource / provider / src / test / java / org / openecomp / sdnc / sli / resource / sql / SqlResourceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
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 package org.openecomp.sdnc.sli.resource.sql;
23
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;
29
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;
34
35 import junit.framework.TestCase;
36
37 public class SqlResourceTest extends TestCase {
38
39         private static final Logger LOG = LoggerFactory
40                         .getLogger(SqlResourceTest.class);
41
42
43         public void testExists() {
44
45
46                 Properties props = new Properties();
47                 InputStream propStr = getClass().getResourceAsStream("/svclogic.properties");
48                 if (propStr == null) {
49                         fail("src/test/resources/svclogic.properties missing");
50                 }
51
52                 try {
53                         props.load(propStr);
54                         propStr.close();
55                 } catch (Exception e) {
56                         e.printStackTrace();
57                         fail("Could not initialize properties");
58                 }
59
60                 // Add properties to global properties
61
62                 Enumeration propNames = props.keys();
63
64                 while (propNames.hasMoreElements()) {
65                         String propName = (String) propNames.nextElement();
66
67                         System.setProperty(propName, props.getProperty(propName));
68                 }
69
70                 SqlResource sqlResource = new SqlResource();
71
72
73
74                 InputStream testStr = getClass().getResourceAsStream("/save.tests");
75                 BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
76                 SvcLogicContext ctx = new SvcLogicContext();
77
78                 try {
79                         String testExpr = null;
80
81                         int testNum = 0;
82                         while ((testExpr = testsReader.readLine()) != null) {
83                                 testExpr = testExpr.trim();
84
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();
90
91                                         LOG.info("Setting context attribute " + name + " = "
92                                                         + value);
93                                         ctx.setAttribute(name, value);
94
95                                 } else {
96
97                                         testNum++;
98                                         String sqlStmt = testExpr;
99                                         QueryStatus status = sqlResource.save("SQL", true, false, sqlStmt, null, "savetest"+testNum, ctx);
100
101                                         switch (status) {
102                                         case SUCCESS:
103                                                 LOG.info("Found data for query [" + sqlStmt + "]");
104                                                 break;
105                                         case NOT_FOUND:
106                                                 LOG.info("Did not data for query [" + sqlStmt + "]");
107                                                 break;
108                                         default:
109                                                 fail("Failure executing query [" + sqlStmt + "]");
110
111                                         }
112                                 }
113                         }
114
115                 } catch (Exception e) {
116                         e.printStackTrace();
117                         fail("Caught exception running tests");
118                 }
119
120
121                 testStr = getClass().getResourceAsStream("/query.tests");
122                 testsReader = new BufferedReader(new InputStreamReader(testStr));
123
124                 try {
125                         String testExpr = null;
126
127                         int testNum = 0;
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();
135
136                                         LOG.info("Setting context attribute " + name + " = "
137                                                         + value);
138                                         ctx.setAttribute(name, value);
139
140                                 } else {
141
142                                         testNum++;
143
144                                         String sqlStmt = testExpr;
145                                         QueryStatus status = sqlResource.query("SQL", false, null,
146                                                         sqlStmt, "querytest" + testNum, null, ctx);
147
148                                         switch (status) {
149                                         case SUCCESS:
150                                                 LOG.info("Found data for query [" + sqlStmt + "]");
151                                                 break;
152                                         case NOT_FOUND:
153                                                 LOG.info("Did not data for query [" + sqlStmt + "]");
154                                                 break;
155                                         default:
156                                                 fail("Failure executing query [" + sqlStmt + "]");
157
158                                         }
159                                 }
160                         }
161
162                 } catch (Exception e) {
163                         e.printStackTrace();
164                         fail("Caught exception running tests");
165                 }
166
167
168                 testStr = getClass().getResourceAsStream("/delete.tests");
169                 testsReader = new BufferedReader(new InputStreamReader(testStr));
170
171                 try {
172                         String testExpr = null;
173
174                         int testNum = 0;
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();
182
183                                         LOG.info("Setting context attribute " + name + " = "
184                                                         + value);
185                                         ctx.setAttribute(name, value);
186
187                                 } else {
188
189                                         testNum++;
190
191                                         String sqlStmt = testExpr;
192                                         QueryStatus status = sqlResource.delete("SQL", sqlStmt, ctx);
193
194                                         switch (status) {
195                                         case SUCCESS:
196                                                 LOG.info("Found data for query [" + sqlStmt + "]");
197                                                 break;
198                                         case NOT_FOUND:
199                                                 LOG.info("Did not data for query [" + sqlStmt + "]");
200                                                 break;
201                                         default:
202                                                 fail("Failure executing query [" + sqlStmt + "]");
203
204                                         }
205                                 }
206                         }
207
208                 } catch (Exception e) {
209                         e.printStackTrace();
210                         fail("Caught exception running tests");
211                 }
212
213                 for (String attrName : ctx.getAttributeKeySet()) {
214                         LOG.info("ctx.getAttribute("+attrName+") = "+ctx.getAttribute(attrName));
215                 }
216         }
217
218 }