b49c88779ce29827ff004b8037dbbe325ed2a56d
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APP-C
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.appc.data.services.db;
22
23 import java.io.File;
24 import java.io.IOException;
25
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;
31
32 import org.openecomp.sdnc.sli.SvcLogicContext;
33
34 public class TestSQLSaveQuery {
35
36     private static final Logger LOG = LoggerFactory.getLogger(TestSQLSaveQuery.class);
37     private static String CRYPT_KEY = "";
38
39     @Test
40     public void testSQLSaveQuery() {
41         try {
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) {
53
54         }
55
56     }
57
58     private String resolveCtxVars(String key, SvcLogicContext ctx) {
59         if (key == null) {
60             return (null);
61         }
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 + "]");
65         }
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(" ");
71         }
72         return (sqlBuffer.toString());
73     }
74
75     private String resolveTerm(String term, SvcLogicContext ctx) {
76         if (term == null) {
77             return (null);
78         }
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) + "'");
83
84         } else {
85             return (term);
86         }
87
88     }
89
90     private String resolveCtxVariable(String ctxVarName, SvcLogicContext ctx) {
91
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)) {
100                     return (cryptKey);
101                 } else {
102                     return (CRYPT_KEY);
103                 }
104
105             }
106             return (ctx.getAttribute(ctxVarName));
107         }
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));
119                 }
120
121                 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
122                 String remainder = ctxVarParts[i].substring(endBracketLoc);
123                 sbuff.append("[");
124                 sbuff.append(ctx.getAttribute(idxVarName));
125                 sbuff.append(remainder);
126
127             } else {
128                 // Index is not a variable reference
129                 sbuff.append("[");
130                 sbuff.append(ctxVarParts[i]);
131             }
132         }
133         return (ctx.getAttribute(sbuff.toString()));
134     }
135
136 }