Changed to unmaintained
[appc.git] / appc-config / appc-data-services / provider / src / test / java / org / onap / appc / data / services / db / TestSQLSaveQuery.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2018 IBM
10  * ================================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.data.services.db;
27
28 import java.io.File;
29 import java.io.IOException;
30
31 import org.apache.commons.io.FileUtils;
32 import static org.junit.Assert.assertEquals;
33 import static org.junit.Assert.assertNotNull;
34 import org.apache.commons.lang.StringEscapeUtils;
35 import org.junit.Test;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
40
41 public class TestSQLSaveQuery {
42
43     private static final Logger LOG = LoggerFactory.getLogger(TestSQLSaveQuery.class);
44     private static String CRYPT_KEY = "";
45
46     @Test
47     public void testSQLSaveQuery() {
48         try {
49             String message = FileUtils.readFileToString(new File("src/test/resources/query/sampledata.txt"));
50             System.out.println("TestSQLSaveQuery.testSQLSaveQuery()" + message);
51             SvcLogicContext ctx = new SvcLogicContext();
52             ctx.setAttribute("request-id", "1234");
53             String escapedMessage = StringEscapeUtils.escapeSql(message);
54             ctx.setAttribute("log_message", escapedMessage);
55             String key = "INSERT INTO CONFIG_TRANSACTION_LOG "
56                     + " SET request_id = $request-id , message_type  =  'request' ,  message        =  $log_message ;";
57             String resolvedContext = resolveCtxVars(key, ctx);
58             ctx.setAttribute("log_message", null);
59             assertNotNull(message);
60         } catch (IOException e) {
61
62         }
63
64     }
65     
66     @Test
67     public void testSQLSaveQueryForNestedRequestId() throws IOException{
68             String message = FileUtils.readFileToString(new File("src/test/resources/query/sampledata.txt"));
69             SvcLogicContext ctx = new SvcLogicContext();
70             ctx.setAttribute("request-id", "1234");
71             String escapedMessage = StringEscapeUtils.escapeSql(message);
72             ctx.setAttribute("log_message", escapedMessage);
73             String key = "INSERT INTO CONFIG_TRANSACTION_LOG "
74                     + " SET request_id = $[$request-id] , message_type  =  'request' ,  message        =  $log_message ;";
75             String resolvedContext = resolveCtxVars(key, ctx);
76             String expected="INSERT INTO CONFIG_TRANSACTION_LOG SET request_id = 'null' , message_type = 'request' , message = '' ;";
77             assertEquals(expected.trim(),resolvedContext.trim());
78     }
79     
80     @Test
81     public void testSQLSaveQueryForCryptKey() throws IOException{
82             String message = FileUtils.readFileToString(new File("src/test/resources/query/sampledata.txt"));
83             SvcLogicContext ctx = new SvcLogicContext();
84             ctx.setAttribute("request-id", "1234");
85             String escapedMessage = StringEscapeUtils.escapeSql(message);
86             ctx.setAttribute("log_message", escapedMessage);
87             ctx.setAttribute("ctxVarName", "test_crypt_key");
88             String key = "INSERT INTO CONFIG_TRANSACTION_LOG "
89                     + " SET request_id = $request-id , message_type  =  'request' ,  message        =  $CRYPT_KEY ;";
90             String resolvedContext = resolveCtxVars(key, ctx);
91             String expected="INSERT INTO CONFIG_TRANSACTION_LOG SET request_id = '1234' , message_type = 'request' , message = '' ;";
92             assertEquals(expected.trim(),resolvedContext.trim());
93        
94     }
95
96     private String resolveCtxVars(String key, SvcLogicContext ctx) {
97         if (key == null) {
98             return (null);
99         }
100         if (key.startsWith("'") && key.endsWith("'")) {
101             key = key.substring(1, key.length() - 1);
102             LOG.debug("Stripped outer single quotes - key is now [" + key + "]");
103         }
104         String[] keyTerms = key.split("\\s+");
105         StringBuffer sqlBuffer = new StringBuffer();
106         for (int i = 0; i < keyTerms.length; i++) {
107             sqlBuffer.append(resolveTerm(keyTerms[i], ctx));
108             sqlBuffer.append(" ");
109         }
110         return (sqlBuffer.toString());
111     }
112
113     private String resolveTerm(String term, SvcLogicContext ctx) {
114         if (term == null) {
115             return (null);
116         }
117         LOG.debug("resolveTerm: term is " + term);
118         if (term.startsWith("$") && (ctx != null)) {
119             // Resolve any index variables.
120             return ("'" + resolveCtxVariable(term.substring(1), ctx) + "'");
121
122         } else {
123             return (term);
124         }
125
126     }
127
128     private String resolveCtxVariable(String ctxVarName, SvcLogicContext ctx) {
129
130         if (ctxVarName.indexOf('[') == -1) {
131             // Ctx variable contains no arrays
132             if ("CRYPT_KEY".equals(ctxVarName)) {
133                 // Handle crypt key as special case. If it's set as a context
134                 // variable, use it. Otherwise, use
135                 // configured crypt key.
136                 String cryptKey = ctx.getAttribute(ctxVarName);
137                 if ((cryptKey != null) && (cryptKey.length() > 0)) {
138                     return (cryptKey);
139                 } else {
140                     return (CRYPT_KEY);
141                 }
142
143             }
144             return (ctx.getAttribute(ctxVarName));
145         }
146         // Resolve any array references
147         StringBuffer sbuff = new StringBuffer();
148         String[] ctxVarParts = ctxVarName.split("\\[");
149         sbuff.append(ctxVarParts[0]);
150         for (int i = 1; i < ctxVarParts.length; i++) {
151             if (ctxVarParts[i].startsWith("$")) {
152                 int endBracketLoc = ctxVarParts[i].indexOf("]");
153                 if (endBracketLoc == -1) {
154                     // Missing end bracket ... give up parsing
155                     LOG.warn("Variable reference " + ctxVarName + " seems to be missing a ']'");
156                     return (ctx.getAttribute(ctxVarName));
157                 }
158
159                 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
160                 String remainder = ctxVarParts[i].substring(endBracketLoc);
161                 sbuff.append("[");
162                 sbuff.append(ctx.getAttribute(idxVarName));
163                 sbuff.append(remainder);
164
165             } else {
166                 // Index is not a variable reference
167                 sbuff.append("[");
168                 sbuff.append(ctxVarParts[i]);
169             }
170         }
171         return (ctx.getAttribute(sbuff.toString()));
172     }
173
174 }