2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.data.services.db;
28 import java.io.IOException;
30 import org.apache.commons.io.FileUtils;
31 import org.apache.commons.lang.StringEscapeUtils;
32 import org.junit.Test;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
38 public class TestSQLSaveQuery {
40 private static final Logger LOG = LoggerFactory.getLogger(TestSQLSaveQuery.class);
41 private static String CRYPT_KEY = "";
44 public void testSQLSaveQuery() {
46 String message = FileUtils.readFileToString(new File("src/test/resources/query/sampledata.txt"));
47 System.out.println("TestSQLSaveQuery.testSQLSaveQuery()" + message);
48 SvcLogicContext ctx = new SvcLogicContext();
49 ctx.setAttribute("request-id", "1234");
50 String escapedMessage = StringEscapeUtils.escapeSql(message);
51 ctx.setAttribute("log_message", escapedMessage);
52 String key = "INSERT INTO CONFIG_TRANSACTION_LOG "
53 + " SET request_id = $request-id , message_type = 'request' , message = $log_message ;";
54 String resolvedContext = resolveCtxVars(key, ctx);
55 ctx.setAttribute("log_message", null);
56 } catch (IOException e) {
62 private String resolveCtxVars(String key, SvcLogicContext ctx) {
66 if (key.startsWith("'") && key.endsWith("'")) {
67 key = key.substring(1, key.length() - 1);
68 LOG.debug("Stripped outer single quotes - key is now [" + key + "]");
70 String[] keyTerms = key.split("\\s+");
71 StringBuffer sqlBuffer = new StringBuffer();
72 for (int i = 0; i < keyTerms.length; i++) {
73 sqlBuffer.append(resolveTerm(keyTerms[i], ctx));
74 sqlBuffer.append(" ");
76 return (sqlBuffer.toString());
79 private String resolveTerm(String term, SvcLogicContext ctx) {
83 LOG.debug("resolveTerm: term is " + term);
84 if (term.startsWith("$") && (ctx != null)) {
85 // Resolve any index variables.
86 return ("'" + resolveCtxVariable(term.substring(1), ctx) + "'");
94 private String resolveCtxVariable(String ctxVarName, SvcLogicContext ctx) {
96 if (ctxVarName.indexOf('[') == -1) {
97 // Ctx variable contains no arrays
98 if ("CRYPT_KEY".equals(ctxVarName)) {
99 // Handle crypt key as special case. If it's set as a context
100 // variable, use it. Otherwise, use
101 // configured crypt key.
102 String cryptKey = ctx.getAttribute(ctxVarName);
103 if ((cryptKey != null) && (cryptKey.length() > 0)) {
110 return (ctx.getAttribute(ctxVarName));
112 // Resolve any array references
113 StringBuffer sbuff = new StringBuffer();
114 String[] ctxVarParts = ctxVarName.split("\\[");
115 sbuff.append(ctxVarParts[0]);
116 for (int i = 1; i < ctxVarParts.length; i++) {
117 if (ctxVarParts[i].startsWith("$")) {
118 int endBracketLoc = ctxVarParts[i].indexOf("]");
119 if (endBracketLoc == -1) {
120 // Missing end bracket ... give up parsing
121 LOG.warn("Variable reference " + ctxVarName + " seems to be missing a ']'");
122 return (ctx.getAttribute(ctxVarName));
125 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
126 String remainder = ctxVarParts[i].substring(endBracketLoc);
128 sbuff.append(ctx.getAttribute(idxVarName));
129 sbuff.append(remainder);
132 // Index is not a variable reference
134 sbuff.append(ctxVarParts[i]);
137 return (ctx.getAttribute(sbuff.toString()));