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 org.apache.commons.lang.StringEscapeUtils;
 
  34 import org.junit.Test;
 
  35 import org.slf4j.Logger;
 
  36 import org.slf4j.LoggerFactory;
 
  38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  40 public class TestSQLSaveQuery {
 
  42     private static final Logger LOG = LoggerFactory.getLogger(TestSQLSaveQuery.class);
 
  43     private static String CRYPT_KEY = "";
 
  46     public void testSQLSaveQuery() {
 
  48             String message = FileUtils.readFileToString(new File("src/test/resources/query/sampledata.txt"));
 
  49             System.out.println("TestSQLSaveQuery.testSQLSaveQuery()" + message);
 
  50             SvcLogicContext ctx = new SvcLogicContext();
 
  51             ctx.setAttribute("request-id", "1234");
 
  52             String escapedMessage = StringEscapeUtils.escapeSql(message);
 
  53             ctx.setAttribute("log_message", escapedMessage);
 
  54             String key = "INSERT INTO CONFIG_TRANSACTION_LOG "
 
  55                     + " SET request_id = $request-id , message_type  =  'request' ,  message        =  $log_message ;";
 
  56             String resolvedContext = resolveCtxVars(key, ctx);
 
  57             ctx.setAttribute("log_message", null);
 
  58         } catch (IOException e) {
 
  65     public void testSQLSaveQueryForNestedRequestId() throws IOException{
 
  66             String message = FileUtils.readFileToString(new File("src/test/resources/query/sampledata.txt"));
 
  67             SvcLogicContext ctx = new SvcLogicContext();
 
  68             ctx.setAttribute("request-id", "1234");
 
  69             String escapedMessage = StringEscapeUtils.escapeSql(message);
 
  70             ctx.setAttribute("log_message", escapedMessage);
 
  71             String key = "INSERT INTO CONFIG_TRANSACTION_LOG "
 
  72                     + " SET request_id = $[$request-id] , message_type  =  'request' ,  message        =  $log_message ;";
 
  73             String resolvedContext = resolveCtxVars(key, ctx);
 
  74             String expected="INSERT INTO CONFIG_TRANSACTION_LOG SET request_id = 'null' , message_type = 'request' , message = '' ;";
 
  75             assertEquals(expected.trim(),resolvedContext.trim());
 
  79     public void testSQLSaveQueryForCryptKey() throws IOException{
 
  80             String message = FileUtils.readFileToString(new File("src/test/resources/query/sampledata.txt"));
 
  81             SvcLogicContext ctx = new SvcLogicContext();
 
  82             ctx.setAttribute("request-id", "1234");
 
  83             String escapedMessage = StringEscapeUtils.escapeSql(message);
 
  84             ctx.setAttribute("log_message", escapedMessage);
 
  85             ctx.setAttribute("ctxVarName", "test_crypt_key");
 
  86             String key = "INSERT INTO CONFIG_TRANSACTION_LOG "
 
  87                     + " SET request_id = $request-id , message_type  =  'request' ,  message        =  $CRYPT_KEY ;";
 
  88             String resolvedContext = resolveCtxVars(key, ctx);
 
  89             String expected="INSERT INTO CONFIG_TRANSACTION_LOG SET request_id = '1234' , message_type = 'request' , message = '' ;";
 
  90             assertEquals(expected.trim(),resolvedContext.trim());
 
  94     private String resolveCtxVars(String key, SvcLogicContext ctx) {
 
  98         if (key.startsWith("'") && key.endsWith("'")) {
 
  99             key = key.substring(1, key.length() - 1);
 
 100             LOG.debug("Stripped outer single quotes - key is now [" + key + "]");
 
 102         String[] keyTerms = key.split("\\s+");
 
 103         StringBuffer sqlBuffer = new StringBuffer();
 
 104         for (int i = 0; i < keyTerms.length; i++) {
 
 105             sqlBuffer.append(resolveTerm(keyTerms[i], ctx));
 
 106             sqlBuffer.append(" ");
 
 108         return (sqlBuffer.toString());
 
 111     private String resolveTerm(String term, SvcLogicContext ctx) {
 
 115         LOG.debug("resolveTerm: term is " + term);
 
 116         if (term.startsWith("$") && (ctx != null)) {
 
 117             // Resolve any index variables.
 
 118             return ("'" + resolveCtxVariable(term.substring(1), ctx) + "'");
 
 126     private String resolveCtxVariable(String ctxVarName, SvcLogicContext ctx) {
 
 128         if (ctxVarName.indexOf('[') == -1) {
 
 129             // Ctx variable contains no arrays
 
 130             if ("CRYPT_KEY".equals(ctxVarName)) {
 
 131                 // Handle crypt key as special case. If it's set as a context
 
 132                 // variable, use it. Otherwise, use
 
 133                 // configured crypt key.
 
 134                 String cryptKey = ctx.getAttribute(ctxVarName);
 
 135                 if ((cryptKey != null) && (cryptKey.length() > 0)) {
 
 142             return (ctx.getAttribute(ctxVarName));
 
 144         // Resolve any array references
 
 145         StringBuffer sbuff = new StringBuffer();
 
 146         String[] ctxVarParts = ctxVarName.split("\\[");
 
 147         sbuff.append(ctxVarParts[0]);
 
 148         for (int i = 1; i < ctxVarParts.length; i++) {
 
 149             if (ctxVarParts[i].startsWith("$")) {
 
 150                 int endBracketLoc = ctxVarParts[i].indexOf("]");
 
 151                 if (endBracketLoc == -1) {
 
 152                     // Missing end bracket ... give up parsing
 
 153                     LOG.warn("Variable reference " + ctxVarName + " seems to be missing a ']'");
 
 154                     return (ctx.getAttribute(ctxVarName));
 
 157                 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
 
 158                 String remainder = ctxVarParts[i].substring(endBracketLoc);
 
 160                 sbuff.append(ctx.getAttribute(idxVarName));
 
 161                 sbuff.append(remainder);
 
 164                 // Index is not a variable reference
 
 166                 sbuff.append(ctxVarParts[i]);
 
 169         return (ctx.getAttribute(sbuff.toString()));