First part of onap rename
[appc.git] / appc-config / appc-data-services / provider / src / test / java / org / openecomp / appc / data / services / db / TestSQLSaveQuery.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.data.services.db;
26
27 import java.io.File;
28 import java.io.IOException;
29
30 import org.apache.commons.io.FileUtils;
31 import org.apache.commons.lang.StringEscapeUtils;
32 import org.junit.Test;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
37
38 public class TestSQLSaveQuery {
39
40     private static final Logger LOG = LoggerFactory.getLogger(TestSQLSaveQuery.class);
41     private static String CRYPT_KEY = "";
42
43     @Test
44     public void testSQLSaveQuery() {
45         try {
46             String message = FileUtils.readFileToString(new File("src/test/resources/query/sampledata.txt"));
47             System.out.println("TestSQLSaveQuery.testSQLSaveQuery()" + message);
48             SvcLogicContext ctx = new SvcLogicContext();
49             ctx.setAttribute("request-id", "1234");
50             String escapedMessage = StringEscapeUtils.escapeSql(message);
51             ctx.setAttribute("log_message", escapedMessage);
52             String key = "INSERT INTO CONFIG_TRANSACTION_LOG "
53                     + " SET request_id = $request-id , message_type  =  'request' ,  message        =  $log_message ;";
54             String resolvedContext = resolveCtxVars(key, ctx);
55             ctx.setAttribute("log_message", null);
56         } catch (IOException e) {
57
58         }
59
60     }
61
62     private String resolveCtxVars(String key, SvcLogicContext ctx) {
63         if (key == null) {
64             return (null);
65         }
66         if (key.startsWith("'") && key.endsWith("'")) {
67             key = key.substring(1, key.length() - 1);
68             LOG.debug("Stripped outer single quotes - key is now [" + key + "]");
69         }
70         String[] keyTerms = key.split("\\s+");
71         StringBuffer sqlBuffer = new StringBuffer();
72         for (int i = 0; i < keyTerms.length; i++) {
73             sqlBuffer.append(resolveTerm(keyTerms[i], ctx));
74             sqlBuffer.append(" ");
75         }
76         return (sqlBuffer.toString());
77     }
78
79     private String resolveTerm(String term, SvcLogicContext ctx) {
80         if (term == null) {
81             return (null);
82         }
83         LOG.debug("resolveTerm: term is " + term);
84         if (term.startsWith("$") && (ctx != null)) {
85             // Resolve any index variables.
86             return ("'" + resolveCtxVariable(term.substring(1), ctx) + "'");
87
88         } else {
89             return (term);
90         }
91
92     }
93
94     private String resolveCtxVariable(String ctxVarName, SvcLogicContext ctx) {
95
96         if (ctxVarName.indexOf('[') == -1) {
97             // Ctx variable contains no arrays
98             if ("CRYPT_KEY".equals(ctxVarName)) {
99                 // Handle crypt key as special case. If it's set as a context
100                 // variable, use it. Otherwise, use
101                 // configured crypt key.
102                 String cryptKey = ctx.getAttribute(ctxVarName);
103                 if ((cryptKey != null) && (cryptKey.length() > 0)) {
104                     return (cryptKey);
105                 } else {
106                     return (CRYPT_KEY);
107                 }
108
109             }
110             return (ctx.getAttribute(ctxVarName));
111         }
112         // Resolve any array references
113         StringBuffer sbuff = new StringBuffer();
114         String[] ctxVarParts = ctxVarName.split("\\[");
115         sbuff.append(ctxVarParts[0]);
116         for (int i = 1; i < ctxVarParts.length; i++) {
117             if (ctxVarParts[i].startsWith("$")) {
118                 int endBracketLoc = ctxVarParts[i].indexOf("]");
119                 if (endBracketLoc == -1) {
120                     // Missing end bracket ... give up parsing
121                     LOG.warn("Variable reference " + ctxVarName + " seems to be missing a ']'");
122                     return (ctx.getAttribute(ctxVarName));
123                 }
124
125                 String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
126                 String remainder = ctxVarParts[i].substring(endBracketLoc);
127                 sbuff.append("[");
128                 sbuff.append(ctx.getAttribute(idxVarName));
129                 sbuff.append(remainder);
130
131             } else {
132                 // Index is not a variable reference
133                 sbuff.append("[");
134                 sbuff.append(ctxVarParts[i]);
135             }
136         }
137         return (ctx.getAttribute(sbuff.toString()));
138     }
139
140 }