b8e178ff85671d9e56ccd29735f27cac019e48fe
[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  * 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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.data.services.db;
25
26 import java.io.File;
27 import java.io.IOException;
28
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;
34
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36
37 public class TestSQLSaveQuery {
38
39     private static final Logger LOG = LoggerFactory.getLogger(TestSQLSaveQuery.class);
40     private static String CRYPT_KEY = "";
41
42     @Test
43     public void testSQLSaveQuery() {
44         try {
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) {
56
57         }
58
59     }
60
61     private String resolveCtxVars(String key, SvcLogicContext ctx) {
62         if (key == null) {
63             return (null);
64         }
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 + "]");
68         }
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(" ");
74         }
75         return (sqlBuffer.toString());
76     }
77
78     private String resolveTerm(String term, SvcLogicContext ctx) {
79         if (term == null) {
80             return (null);
81         }
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) + "'");
86
87         } else {
88             return (term);
89         }
90
91     }
92
93     private String resolveCtxVariable(String ctxVarName, SvcLogicContext ctx) {
94
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)) {
103                     return (cryptKey);
104                 } else {
105                     return (CRYPT_KEY);
106                 }
107
108             }
109             return (ctx.getAttribute(ctxVarName));
110         }
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));
122                 }
123
124                 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
125                 String remainder = ctxVarParts[i].substring(endBracketLoc);
126                 sbuff.append("[");
127                 sbuff.append(ctx.getAttribute(idxVarName));
128                 sbuff.append(remainder);
129
130             } else {
131                 // Index is not a variable reference
132                 sbuff.append("[");
133                 sbuff.append(ctxVarParts[i]);
134             }
135         }
136         return (ctx.getAttribute(sbuff.toString()));
137     }
138
139 }