2863c6c96410cbe189f4b52080693af2b8cbbc07
[ccsdk/sli.git] /
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.onap.ccsdk.sli.adaptors.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 ch.vorburger.mariadb4j.DB;
36 import ch.vorburger.mariadb4j.DBConfigurationBuilder;
37 import junit.framework.TestCase;
38
39 public class ITCaseSqlResource extends TestCase {
40
41         private static final Logger LOG = LoggerFactory
42                         .getLogger(ITCaseSqlResource.class);
43
44
45         public void testExists() throws Exception {
46
47
48                 Properties props = new Properties();
49                 InputStream propStr = getClass().getResourceAsStream("/svclogic.properties");
50                 if (propStr == null) {
51                         fail("src/test/resources/svclogic.properties missing");
52                 }
53
54                 try {
55                         props.load(propStr);
56                         propStr.close();
57                 } catch (Exception e) {
58                         e.printStackTrace();
59                         fail("Could not initialize properties");
60                 }
61
62
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());
67         db.start();
68
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"));
72
73                 // Add properties to global properties
74
75                 Enumeration propNames = props.keys();
76
77                 while (propNames.hasMoreElements()) {
78                         String propName = (String) propNames.nextElement();
79
80                         System.setProperty(propName, props.getProperty(propName));
81                 }
82
83                 SqlResource sqlResource = new SqlResource(new SqlResourcePropertiesProviderImpl());
84
85
86
87                 InputStream testStr = getClass().getResourceAsStream("/save.tests");
88                 BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
89                 SvcLogicContext ctx = new SvcLogicContext();
90
91                 try {
92                         String testExpr = null;
93
94                         int testNum = 0;
95                         while ((testExpr = testsReader.readLine()) != null) {
96                                 testExpr = testExpr.trim();
97
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();
103
104                                         LOG.info("Setting context attribute " + name + " = "
105                                                         + value);
106                                         ctx.setAttribute(name, value);
107
108                                 } else {
109
110                                         testNum++;
111                                         String sqlStmt = testExpr;
112                                         QueryStatus status = sqlResource.save("SQL", true, false, sqlStmt, null, "savetest"+testNum, ctx);
113
114                                         switch (status) {
115                                         case SUCCESS:
116                                                 LOG.info("Found data for query [" + sqlStmt + "]");
117                                                 break;
118                                         case NOT_FOUND:
119                                                 LOG.info("Did not data for query [" + sqlStmt + "]");
120                                                 break;
121                                         default:
122                                                 fail("Failure executing query [" + sqlStmt + "]");
123
124                                         }
125                                 }
126                         }
127
128                 } catch (Exception e) {
129                         e.printStackTrace();
130                         fail("Caught exception running tests");
131                 }
132
133
134                 testStr = getClass().getResourceAsStream("/query.tests");
135                 testsReader = new BufferedReader(new InputStreamReader(testStr));
136
137                 try {
138                         String testExpr = null;
139
140                         int testNum = 0;
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();
148
149                                         LOG.info("Setting context attribute " + name + " = "
150                                                         + value);
151                                         ctx.setAttribute(name, value);
152
153                                 } else {
154
155                                         testNum++;
156
157                                         String sqlStmt = testExpr;
158                                         QueryStatus status = sqlResource.query("SQL", false, null,
159                                                         sqlStmt, "querytest" + testNum, null, ctx);
160
161                                         switch (status) {
162                                         case SUCCESS:
163                                                 LOG.info("Found data for query [" + sqlStmt + "]");
164                                                 break;
165                                         case NOT_FOUND:
166                                                 LOG.info("Did not data for query [" + sqlStmt + "]");
167                                                 break;
168                                         default:
169                                                 fail("Failure executing query [" + sqlStmt + "]");
170
171                                         }
172                                 }
173                         }
174
175                 } catch (Exception e) {
176                         e.printStackTrace();
177                         fail("Caught exception running tests");
178                 }
179
180
181                 testStr = getClass().getResourceAsStream("/delete.tests");
182                 testsReader = new BufferedReader(new InputStreamReader(testStr));
183
184                 try {
185                         String testExpr = null;
186
187                         int testNum = 0;
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();
195
196                                         LOG.info("Setting context attribute " + name + " = "
197                                                         + value);
198                                         ctx.setAttribute(name, value);
199
200                                 } else {
201
202                                         testNum++;
203
204                                         String sqlStmt = testExpr;
205                                         QueryStatus status = sqlResource.delete("SQL", sqlStmt, ctx);
206
207                                         switch (status) {
208                                         case SUCCESS:
209                                                 LOG.info("Found data for query [" + sqlStmt + "]");
210                                                 break;
211                                         case NOT_FOUND:
212                                                 LOG.info("Did not data for query [" + sqlStmt + "]");
213                                                 break;
214                                         default:
215                                                 fail("Failure executing query [" + sqlStmt + "]");
216
217                                         }
218                                 }
219                         }
220
221                 } catch (Exception e) {
222                         e.printStackTrace();
223                         fail("Caught exception running tests");
224                 }
225
226                 for (String attrName : ctx.getAttributeKeySet()) {
227                         LOG.info("ctx.getAttribute("+attrName+") = "+ctx.getAttribute(attrName));
228                 }
229         }
230
231 }