b482eab0b31a62f31f5e71c95bb4989f4724a4de
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
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.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.data.services.db;
27
28 import java.io.File;
29 import java.io.IOException;
30
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;
37
38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39
40 public class TestSQLSaveQuery {
41
42     private static final Logger LOG = LoggerFactory.getLogger(TestSQLSaveQuery.class);
43     private static String CRYPT_KEY = "";
44
45     @Test
46     public void testSQLSaveQuery() {
47         try {
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) {
59
60         }
61
62     }
63     
64     @Test
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());
76     }
77     
78     @Test
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());
91        
92     }
93
94     private String resolveCtxVars(String key, SvcLogicContext ctx) {
95         if (key == null) {
96             return (null);
97         }
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 + "]");
101         }
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(" ");
107         }
108         return (sqlBuffer.toString());
109     }
110
111     private String resolveTerm(String term, SvcLogicContext ctx) {
112         if (term == null) {
113             return (null);
114         }
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) + "'");
119
120         } else {
121             return (term);
122         }
123
124     }
125
126     private String resolveCtxVariable(String ctxVarName, SvcLogicContext ctx) {
127
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)) {
136                     return (cryptKey);
137                 } else {
138                     return (CRYPT_KEY);
139                 }
140
141             }
142             return (ctx.getAttribute(ctxVarName));
143         }
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));
155                 }
156
157                 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
158                 String remainder = ctxVarParts[i].substring(endBracketLoc);
159                 sbuff.append("[");
160                 sbuff.append(ctx.getAttribute(idxVarName));
161                 sbuff.append(remainder);
162
163             } else {
164                 // Index is not a variable reference
165                 sbuff.append("[");
166                 sbuff.append(ctxVarParts[i]);
167             }
168         }
169         return (ctx.getAttribute(sbuff.toString()));
170     }
171
172 }