2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
24 package org.onap.appc.data.services.db;
27 import java.io.IOException;
29 import org.apache.commons.io.FileUtils;
30 import org.apache.commons.lang.StringEscapeUtils;
31 import org.junit.Test;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
37 public class TestSQLSaveQuery {
39 private static final Logger LOG = LoggerFactory.getLogger(TestSQLSaveQuery.class);
40 private static String CRYPT_KEY = "";
43 public void testSQLSaveQuery() {
45 String message = FileUtils.readFileToString(new File("src/test/resources/query/sampledata.txt"));
46 System.out.println("TestSQLSaveQuery.testSQLSaveQuery()" + message);
47 SvcLogicContext ctx = new SvcLogicContext();
48 ctx.setAttribute("request-id", "1234");
49 String escapedMessage = StringEscapeUtils.escapeSql(message);
50 ctx.setAttribute("log_message", escapedMessage);
51 String key = "INSERT INTO CONFIG_TRANSACTION_LOG "
52 + " SET request_id = $request-id , message_type = 'request' , message = $log_message ;";
53 String resolvedContext = resolveCtxVars(key, ctx);
54 ctx.setAttribute("log_message", null);
55 } catch (IOException e) {
61 private String resolveCtxVars(String key, SvcLogicContext ctx) {
65 if (key.startsWith("'") && key.endsWith("'")) {
66 key = key.substring(1, key.length() - 1);
67 LOG.debug("Stripped outer single quotes - key is now [" + key + "]");
69 String[] keyTerms = key.split("\\s+");
70 StringBuffer sqlBuffer = new StringBuffer();
71 for (int i = 0; i < keyTerms.length; i++) {
72 sqlBuffer.append(resolveTerm(keyTerms[i], ctx));
73 sqlBuffer.append(" ");
75 return (sqlBuffer.toString());
78 private String resolveTerm(String term, SvcLogicContext ctx) {
82 LOG.debug("resolveTerm: term is " + term);
83 if (term.startsWith("$") && (ctx != null)) {
84 // Resolve any index variables.
85 return ("'" + resolveCtxVariable(term.substring(1), ctx) + "'");
93 private String resolveCtxVariable(String ctxVarName, SvcLogicContext ctx) {
95 if (ctxVarName.indexOf('[') == -1) {
96 // Ctx variable contains no arrays
97 if ("CRYPT_KEY".equals(ctxVarName)) {
98 // Handle crypt key as special case. If it's set as a context
99 // variable, use it. Otherwise, use
100 // configured crypt key.
101 String cryptKey = ctx.getAttribute(ctxVarName);
102 if ((cryptKey != null) && (cryptKey.length() > 0)) {
109 return (ctx.getAttribute(ctxVarName));
111 // Resolve any array references
112 StringBuffer sbuff = new StringBuffer();
113 String[] ctxVarParts = ctxVarName.split("\\[");
114 sbuff.append(ctxVarParts[0]);
115 for (int i = 1; i < ctxVarParts.length; i++) {
116 if (ctxVarParts[i].startsWith("$")) {
117 int endBracketLoc = ctxVarParts[i].indexOf("]");
118 if (endBracketLoc == -1) {
119 // Missing end bracket ... give up parsing
120 LOG.warn("Variable reference " + ctxVarName + " seems to be missing a ']'");
121 return (ctx.getAttribute(ctxVarName));
124 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
125 String remainder = ctxVarParts[i].substring(endBracketLoc);
127 sbuff.append(ctx.getAttribute(idxVarName));
128 sbuff.append(remainder);
131 // Index is not a variable reference
133 sbuff.append(ctxVarParts[i]);
136 return (ctx.getAttribute(sbuff.toString()));