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.junit.Test;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import org.openecomp.sdnc.sli.SvcLogicContext;
34 public class TestSQLSaveQuery {
36 private static final Logger LOG = LoggerFactory.getLogger(TestSQLSaveQuery.class);
37 private static String CRYPT_KEY = "";
40 public void testSQLSaveQuery() {
42 String message = FileUtils.readFileToString(new File("src/test/resources/query/sampledata.txt"));
43 System.out.println("TestSQLSaveQuery.testSQLSaveQuery()" + message);
44 SvcLogicContext ctx = new SvcLogicContext();
45 ctx.setAttribute("request-id", "1234");
46 String escapedMessage = StringEscapeUtils.escapeSql(message);
47 ctx.setAttribute("log_message", escapedMessage);
48 String key = "INSERT INTO CONFIG_TRANSACTION_LOG "
49 + " SET request_id = $request-id , message_type = 'request' , message = $log_message ;";
50 String resolvedContext = resolveCtxVars(key, ctx);
51 ctx.setAttribute("log_message", null);
52 } catch (IOException e) {
58 private String resolveCtxVars(String key, SvcLogicContext ctx) {
62 if (key.startsWith("'") && key.endsWith("'")) {
63 key = key.substring(1, key.length() - 1);
64 LOG.debug("Stripped outer single quotes - key is now [" + key + "]");
66 String[] keyTerms = key.split("\\s+");
67 StringBuffer sqlBuffer = new StringBuffer();
68 for (int i = 0; i < keyTerms.length; i++) {
69 sqlBuffer.append(resolveTerm(keyTerms[i], ctx));
70 sqlBuffer.append(" ");
72 return (sqlBuffer.toString());
75 private String resolveTerm(String term, SvcLogicContext ctx) {
79 LOG.debug("resolveTerm: term is " + term);
80 if (term.startsWith("$") && (ctx != null)) {
81 // Resolve any index variables.
82 return ("'" + resolveCtxVariable(term.substring(1), ctx) + "'");
90 private String resolveCtxVariable(String ctxVarName, SvcLogicContext ctx) {
92 if (ctxVarName.indexOf('[') == -1) {
93 // Ctx variable contains no arrays
94 if ("CRYPT_KEY".equals(ctxVarName)) {
95 // Handle crypt key as special case. If it's set as a context
96 // variable, use it. Otherwise, use
97 // configured crypt key.
98 String cryptKey = ctx.getAttribute(ctxVarName);
99 if ((cryptKey != null) && (cryptKey.length() > 0)) {
106 return (ctx.getAttribute(ctxVarName));
108 // Resolve any array references
109 StringBuffer sbuff = new StringBuffer();
110 String[] ctxVarParts = ctxVarName.split("\\[");
111 sbuff.append(ctxVarParts[0]);
112 for (int i = 1; i < ctxVarParts.length; i++) {
113 if (ctxVarParts[i].startsWith("$")) {
114 int endBracketLoc = ctxVarParts[i].indexOf("]");
115 if (endBracketLoc == -1) {
116 // Missing end bracket ... give up parsing
117 LOG.warn("Variable reference " + ctxVarName + " seems to be missing a ']'");
118 return (ctx.getAttribute(ctxVarName));
121 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
122 String remainder = ctxVarParts[i].substring(endBracketLoc);
124 sbuff.append(ctx.getAttribute(idxVarName));
125 sbuff.append(remainder);
128 // Index is not a variable reference
130 sbuff.append(ctxVarParts[i]);
133 return (ctx.getAttribute(sbuff.toString()));