2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.data.services.db;
29 import java.io.IOException;
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;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
41 public class TestSQLSaveQuery {
43 private static final Logger LOG = LoggerFactory.getLogger(TestSQLSaveQuery.class);
44 private static String CRYPT_KEY = "";
47 public void testSQLSaveQuery() {
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) {
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());
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());
96 private String resolveCtxVars(String key, SvcLogicContext ctx) {
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 + "]");
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(" ");
110 return (sqlBuffer.toString());
113 private String resolveTerm(String term, SvcLogicContext ctx) {
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) + "'");
128 private String resolveCtxVariable(String ctxVarName, SvcLogicContext ctx) {
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)) {
144 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 + " seems to be missing a ']'");
156 return (ctx.getAttribute(ctxVarName));
159 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
160 String remainder = ctxVarParts[i].substring(endBracketLoc);
162 sbuff.append(ctx.getAttribute(idxVarName));
163 sbuff.append(remainder);
166 // Index is not a variable reference
168 sbuff.append(ctxVarParts[i]);
171 return (ctx.getAttribute(sbuff.toString()));