2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.appc.data.services.db;
24 import java.io.IOException;
26 import org.apache.commons.io.FileUtils;
27 import org.apache.commons.lang.StringEscapeUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.junit.Test;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import org.openecomp.sdnc.sli.SvcLogicContext;
35 public class TestSQLSaveQuery {
38 private static final Logger LOG = LoggerFactory.getLogger(TestSQLSaveQuery.class);
40 private static String CRYPT_KEY = "";
43 public void testSQLSaveQuery() {
46 String message = FileUtils.readFileToString(new File("src/test/resources/query/sampledata.txt"));
47 System.out.println("TestSQLSaveQuery.testSQLSaveQuery()" + message);
49 SvcLogicContext ctx = new SvcLogicContext();
50 ctx.setAttribute("request-id", "1234");
52 String escapedMessage = StringEscapeUtils.escapeSql(message);
53 ctx.setAttribute("log_message", escapedMessage);
55 //String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , message_type = 'request' , message = '" + escapedMessage + "' ;";
56 String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , message_type = 'request' , message = $log_message ;";
57 System.out.println("Query : " + key);
58 String resolvedContext = resolveCtxVars(key, ctx);
60 System.out.println("Resolved : " + resolvedContext);
62 ctx.setAttribute("log_message", null);
65 } catch (IOException e) {
66 // TODO Auto-generated catch block
74 private String resolveCtxVars(String key,
75 SvcLogicContext ctx) {
80 if (key.startsWith("'") && key.endsWith("'")) {
81 key = key.substring(1, key.length() - 1);
83 LOG.debug("Stripped outer single quotes - key is now [" + key + "]");
86 String[] keyTerms = key.split("\\s+");
88 StringBuffer sqlBuffer = new StringBuffer();
91 for (int i = 0; i < keyTerms.length; i++) {
92 sqlBuffer.append(resolveTerm(keyTerms[i], ctx));
93 sqlBuffer.append(" ");
97 return (sqlBuffer.toString());
101 private String resolveTerm(String term, SvcLogicContext ctx) {
106 LOG.debug("resolveTerm: term is " + term);
108 if (term.startsWith("$") && (ctx != null)) {
109 // Resolve any index variables.
112 String resolved = resolveCtxVariable(term.substring(1), ctx);
113 if(resolved != null){
114 value = "'" +resolved+ "'";
116 System.out.println("Dollar Term : " + term + " Value :"+ value);
119 return ("'" + resolveCtxVariable(term.substring(1), ctx) + "'");
127 private String resolveCtxVariable(String ctxVarName, SvcLogicContext ctx) {
129 if (ctxVarName.indexOf('[') == -1) {
130 // Ctx variable contains no arrays
131 if ("CRYPT_KEY".equals(ctxVarName)) {
133 // Handle crypt key as special case. If it's set as a context variable, use it. Otherwise, use
134 // configured crypt key.
135 String cryptKey = ctx.getAttribute(ctxVarName);
136 if ((cryptKey != null) && (cryptKey.length() > 0)) {
143 return (ctx.getAttribute(ctxVarName));
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
156 + " seems to be missing a ']'");
157 return (ctx.getAttribute(ctxVarName));
160 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
161 String remainder = ctxVarParts[i].substring(endBracketLoc);
164 sbuff.append(ctx.getAttribute(idxVarName));
165 sbuff.append(remainder);
168 // Index is not a variable reference
170 sbuff.append(ctxVarParts[i]);
174 return (ctx.getAttribute(sbuff.toString()));