diff options
author | Anand Chaturvedi <ac204h@att.com> | 2017-08-25 16:15:07 -0400 |
---|---|---|
committer | Skip Wonnell <skip@att.com> | 2017-08-25 21:57:17 +0000 |
commit | 25b7ed894e566f954b51fc5487e9e678557eb2e5 (patch) | |
tree | 8e1aa9e835802bf4d60ecf8ce591a44c11569e2d /appc-config/appc-data-services/provider/src | |
parent | 257a39e7daf14a1c760e597a699f6545d6b74eac (diff) |
Junit Cases for APPC dataServices
Issue-ID: APPC-168
Change-Id: Ia5ae825f1c7919d93afb460cde7ae0fa2bcd0c6c
Author: Anand Chaturvedi <ac204h@att.com>
Date: Fri Aug 25 15:41:42 2017 -0400
Signed-off-by: Anand Chaturvedi <ac204h@att.com>
Diffstat (limited to 'appc-config/appc-data-services/provider/src')
3 files changed, 429 insertions, 258 deletions
diff --git a/appc-config/appc-data-services/provider/src/test/java/org/openecomp/appc/data/services/db/TestConfigResourceNode.java b/appc-config/appc-data-services/provider/src/test/java/org/openecomp/appc/data/services/db/TestConfigResourceNode.java index b134de0b1..781c43f43 100644 --- a/appc-config/appc-data-services/provider/src/test/java/org/openecomp/appc/data/services/db/TestConfigResourceNode.java +++ b/appc-config/appc-data-services/provider/src/test/java/org/openecomp/appc/data/services/db/TestConfigResourceNode.java @@ -1,115 +1,151 @@ -package org.openecomp.appc.data.services.db;
-
-import java.io.InputStream;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Properties;
-
-import org.junit.Test;
-import org.openecomp.appc.data.services.AppcDataServiceConstant;
-import org.openecomp.appc.data.services.db.DGGeneralDBService;
-import org.openecomp.appc.data.services.node.ConfigResourceNode;
-import org.apache.commons.lang.StringEscapeUtils;
-import org.junit.After;
-import org.junit.Before;
-import org.openecomp.sdnc.sli.SvcLogicContext;
-import org.openecomp.sdnc.sli.SvcLogicException;
-import org.openecomp.sdnc.sli.SvcLogicResource;
-import org.openecomp.sdnc.sli.SvcLogicResource.QueryStatus;
-import org.openecomp.sdnc.sli.resource.sql.SqlResource;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
-
-import java.io.File;
-
-public class TestConfigResourceNode {
-
- @Test(expected = Exception.class)
- public void testGetUploadConfig1() throws SvcLogicException{
- SvcLogicContext ctx = new SvcLogicContext();
- ctx.setAttribute("test","test");
- ConfigResourceNode dbService = new ConfigResourceNode() ;
- Map<String,String> map = new HashMap<String,String>();
- dbService.getConfigFileReference(map,ctx);
- //System.out.println(status);
-
-
- }
-
-@Test(expected = Exception.class)
-
-public void testGetUploadConfig2() throws SvcLogicException{
-SvcLogicContext ctx = new SvcLogicContext();
-ctx.setAttribute("test","test");
- ConfigResourceNode dbService = new ConfigResourceNode() ;
- Map<String,String> map = new HashMap<String,String>();
- dbService.getTemplate(map,ctx);
- //System.out.println(status);
-}
-
-@Test(expected = Exception.class)
-
-public void testGetUploadConfig3() throws SvcLogicException{
-SvcLogicContext ctx = new SvcLogicContext();
-ctx.setAttribute("test","test");
- ConfigResourceNode dbService = new ConfigResourceNode() ;
- Map<String,String> map = new HashMap<String,String>();
- dbService.getVnfcReference(map, ctx);
- //System.out.println(status);
-
-
-}
-
-@Test(expected = Exception.class)
-
-public void testGetUploadConfig4() throws SvcLogicException{
-SvcLogicContext ctx = new SvcLogicContext();
-ctx.setAttribute("test","test");
- ConfigResourceNode dbService = new ConfigResourceNode() ;
- Map<String,String> map = new HashMap<String,String>();
- dbService.getSmmChainKeyFiles(map, ctx);
- //System.out.println(status);
-
-
-}
-
-@Test(expected = Exception.class)
-
-public void testGetUploadConfig5() throws SvcLogicException{
-SvcLogicContext ctx = new SvcLogicContext();
-ctx.setAttribute("test","test");
- ConfigResourceNode dbService = new ConfigResourceNode() ;
- Map<String,String> map = new HashMap<String,String>();
- dbService.getDownloadConfigTemplateByVnf(map, ctx);
- //System.out.println(status);
-
-
-}
-
-@Test(expected = Exception.class)
-public void testGetUploadConfig6() throws SvcLogicException{
-SvcLogicContext ctx = new SvcLogicContext();
-ctx.setAttribute("test","test");
- ConfigResourceNode dbService = new ConfigResourceNode() ;
- Map<String,String> map = new HashMap<String,String>();
- dbService.getCommonConfigInfo(map, ctx);
- //System.out.println(status);
-
-
-}
-
-@Test(expected = Exception.class)
-public void testGetUploadConfig7() throws SvcLogicException{
-SvcLogicContext ctx = new SvcLogicContext();
-ctx.setAttribute("test","test");
- ConfigResourceNode dbService = new ConfigResourceNode() ;
- Map<String,String> map = new HashMap<String,String>();
- dbService.updateUploadConfig(map, ctx);
- //System.out.println(status);
-
-
-}
-
-
-}
+/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.data.services.db; + +import java.util.HashMap; +import java.util.Map; +import org.junit.Test; +import org.openecomp.appc.data.services.AppcDataServiceConstant; +import org.openecomp.appc.data.services.node.ConfigResourceNode; +import org.openecomp.sdnc.sli.SvcLogicContext; +import org.openecomp.sdnc.sli.SvcLogicException; + +public class TestConfigResourceNode { + + @Test(expected = Exception.class) + public void testgetConfigFileReferenc() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("test", "test"); + ConfigResourceNode dbService = new ConfigResourceNode(); + Map<String, String> map = new HashMap<String, String>(); + dbService.getConfigFileReference(map, ctx); + } + + @Test(expected = Exception.class) + + public void testgetTemplate() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("test", "test"); + ctx.setAttribute(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY, "test"); + ctx.setAttribute(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "tmp.test"); + ctx.setAttribute("template-name", "test.json"); + + ConfigResourceNode dbService = new ConfigResourceNode(); + Map<String, String> map = new HashMap<String, String>(); + dbService.getTemplate(map, ctx); + + } + + @Test(expected = Exception.class) + public void testgetVnfcReference() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("test", "test"); + ConfigResourceNode dbService = new ConfigResourceNode(); + Map<String, String> map = new HashMap<String, String>(); + map.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "tmp.test"); + dbService.getVnfcReference(map, ctx); + } + + @Test(expected = Exception.class) + public void testgetSmmChainKeyFiles() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("test", "test"); + ctx.setAttribute("site-location","test/location"); + ConfigResourceNode dbService = new ConfigResourceNode(); + Map<String, String> map = new HashMap<String, String>(); + map.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "tmp.test"); + dbService.getSmmChainKeyFiles(map, ctx); + } + + @Test(expected = Exception.class) + + public void testgetDownloadConfigTemplateByVnf() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("test", "test"); + ConfigResourceNode dbService = new ConfigResourceNode(); + Map<String, String> map = new HashMap<String, String>(); + map.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "tmp.test"); + dbService.getDownloadConfigTemplateByVnf(map, ctx); + + } + + @Test(expected = Exception.class) + public void testgetCommonConfigInfo() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("test", "test"); + ConfigResourceNode dbService = new ConfigResourceNode(); + Map<String, String> map = new HashMap<String, String>(); + dbService.getCommonConfigInfo(map, ctx); + + } + + @Test(expected = Exception.class) + public void testupdateUploadConfigss() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("test", "test"); + ConfigResourceNode dbService = new ConfigResourceNode(); + Map<String, String> map = new HashMap<String, String>(); + dbService.updateUploadConfig(map, ctx); + + } + + @Test(expected = Exception.class) + public void testgetConfigFilesByVnfVmNCategory() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + ConfigResourceNode node = new ConfigResourceNode(); + Map<String, String> inParams = new HashMap<String, String>(); + inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "response-prefix"); + inParams.put(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY, "config_template"); + inParams.put((String) AppcDataServiceConstant.INPUT_PARAM_VNF_ID, "test"); + inParams.put((String) AppcDataServiceConstant.INPUT_PARAM_VM_NAME, "test"); + node.getConfigFilesByVnfVmNCategory(inParams, ctx); + } + + @Test(expected = Exception.class) + public void testsaveConfigTransactionLog() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + ConfigResourceNode node = new ConfigResourceNode(); + Map<String, String> inParams = new HashMap<String, String>(); + inParams.put(AppcDataServiceConstant.INPUT_PARAM_MESSAGE, "testMessage"); + inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "response-prefix"); + inParams.put(AppcDataServiceConstant.INPUT_PARAM_MESSAGE_TYPE, "testmessage"); + ctx.setAttribute("request-id", "tets-id"); + node.saveConfigTransactionLog(inParams, ctx); + + } + + @Test(expected = Exception.class) + public void testsaveConfigBlock() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + ConfigResourceNode node = new ConfigResourceNode(); + Map<String, String> inParams = new HashMap<String, String>(); + inParams.put(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX, "tmp"); + ctx.setAttribute("configuration", "test"); + ctx.setAttribute("tmp.convertconfig.escapeData", "test"); + ctx.setAttribute("tmp.merge.mergedData", "test"); + node.saveConfigBlock(inParams, ctx); + } + +} diff --git a/appc-config/appc-data-services/provider/src/test/java/org/openecomp/appc/data/services/db/TestDGGeneralDBService.java b/appc-config/appc-data-services/provider/src/test/java/org/openecomp/appc/data/services/db/TestDGGeneralDBService.java index 386b9f926..ec173146a 100644 --- a/appc-config/appc-data-services/provider/src/test/java/org/openecomp/appc/data/services/db/TestDGGeneralDBService.java +++ b/appc-config/appc-data-services/provider/src/test/java/org/openecomp/appc/data/services/db/TestDGGeneralDBService.java @@ -17,13 +17,190 @@ * limitations under the License. * ============LICENSE_END========================================================= */ +package org.openecomp.appc.data.services.db; - +import static org.junit.Assert.assertEquals; +import java.io.IOException; +import java.io.InputStream; +import java.util.Enumeration; +import java.util.Properties; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.StringEscapeUtils; import org.junit.Test; -//Model JUNIT Class +import org.openecomp.sdnc.sli.SvcLogicContext; +import org.openecomp.sdnc.sli.SvcLogicException; +import org.openecomp.sdnc.sli.SvcLogicResource.QueryStatus; +import org.openecomp.appc.data.services.db.DGGeneralDBService; + public class TestDGGeneralDBService { + DGGeneralDBService dbService; + private static String STRING_ENCODING = "utf-8"; + + // @Before + public void setUp() { + Properties props = new Properties(); + InputStream propStr = getClass().getResourceAsStream("/svclogic.properties"); + if (propStr == null) { + System.err.println("src/test/resources/svclogic.properties missing"); + } + try { + props.load(propStr); + propStr.close(); + } catch (Exception e) { + e.printStackTrace(); + System.err.println("Could not initialize properties"); + } + // Add properties to global properties + Enumeration propNames = props.keys(); + while (propNames.hasMoreElements()) { + String propName = (String) propNames.nextElement(); + System.setProperty(propName, props.getProperty(propName)); + } + dbService = DGGeneralDBService.initialise(); + } + + @Test(expected = Exception.class) + public void testGetUploadConfig() throws SvcLogicException { + + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("vnf-name", "test"); + ctx.setAttribute("vnf-id", "test"); + QueryStatus status = dbService.getUploadConfigInfo(ctx, "test"); + assertEquals(status, "SUCCESS"); + + } + + @Test(expected = Exception.class) + public void testGetDeviceProtocolByVnfType() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("vnf-type", "test"); + QueryStatus status = dbService.getDeviceProtocolByVnfType(ctx, "test"); + assertEquals(status, "SUCCESS"); + } + + @Test(expected = Exception.class) + public void testGettConfigFileReferenceByFileTypeNVnfType() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("vnf-type", "test"); + QueryStatus status = dbService.getConfigFileReferenceByFileTypeNVnfType(ctx, "test", "device_configuration"); + assertEquals(status, "SUCCESS"); + + } + + @Test(expected = Exception.class) + public void testGetDeviceAuthenticationByVnfType() throws Exception { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("vnf-type", "test"); + QueryStatus status = dbService.getDeviceAuthenticationByVnfType(ctx, "test"); + assertEquals(status, "SUCCESS"); + + } + + @Test(expected = Exception.class) + public void testGetTemplate() throws Exception { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("vnfc-type", "test"); + ctx.setAttribute("request-action", "Configure"); + QueryStatus status = dbService.getTemplate(ctx, "test", "config_template"); + assertEquals(status, "SUCCESS"); + + } + + @Test(expected = Exception.class) + public void testGetTemplateByVnfTypeNAction() throws Exception { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("vnf-type", "test"); + ctx.setAttribute("request-action", "Configure"); + QueryStatus status = dbService.getTemplateByVnfTypeNAction(ctx, "test", "config_template"); + assertEquals(status, "SUCCESS"); + + } + + @Test(expected = Exception.class) + public void testGetTemplateByTemplateName() throws Exception { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("request-action", "Configure"); + ctx.setAttribute("vnf-type", "test"); + QueryStatus status = dbService.getTemplateByTemplateName(ctx, "test", "template.json"); + assertEquals(status, "SUCCESS"); + + } + + @Test(expected = Exception.class) + public void testGetTemplateByVnfType() throws SvcLogicException { + + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("vnf-type", "test"); + QueryStatus status = dbService.getTemplateByVnfType(ctx, "test", "config_template"); + assertEquals(status, "SUCCESS"); + + } + + @Test(expected = Exception.class) + public void testGetConfigureActionDGByVnfTypeNAction() throws SvcLogicException { + + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("vnf-type", "test"); + ctx.setAttribute("request-action", "ConfigModify"); + QueryStatus status = dbService.getConfigureActionDGByVnfTypeNAction(ctx, "test"); + assertEquals(status, "SUCCESS"); + + } + + @Test(expected = Exception.class) + public void testGetConfigureActionDGByVnfType() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("vnf-type", "test"); + QueryStatus status = dbService.getConfigureActionDGByVnfType(ctx, "test"); + assertEquals(status, "SUCCESS"); + + } + + @Test(expected = Exception.class) + public void testGetMaxConfigFileId() throws SvcLogicException { + + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("vnf-id", "test"); + ctx.setAttribute("vm-name", "test"); + QueryStatus status = dbService.getMaxConfigFileId(ctx, "test", "device_configuration"); + assertEquals(status, "SUCCESS"); + + } + + @Test(expected = Exception.class) + public void testGetConfigFilesByVnfVmNCategory() throws SvcLogicException { + + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("vnf-id", "test"); + ctx.setAttribute("vm-name", "test"); + QueryStatus status = dbService.getConfigFilesByVnfVmNCategory(ctx, "test", "device_configuration", "test", + "ibcx0001vm001"); + assertEquals(status, "SUCCESS"); + + } + + @Test(expected = Exception.class) + public void testGetDownloadConfigTemplateByVnf() throws SvcLogicException { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("vnf-type", "test"); + QueryStatus status = dbService.getDownloadConfigTemplateByVnf(ctx, "test"); + assertEquals(status, "SUCCESS"); + } + @Test(expected = Exception.class) + public void testSaveConfigTxLog() throws SvcLogicException, IOException { + SvcLogicContext ctx = new SvcLogicContext(); + String message = IOUtils.toString( + TestDGGeneralDBService.class.getClassLoader().getResourceAsStream("query/message3.txt"), + STRING_ENCODING); + ctx.setAttribute("request-id", "1234"); + String escapedMessage = StringEscapeUtils.escapeSql(message); + ctx.setAttribute("log-message", escapedMessage); + ctx.setAttribute("log-message-type", "request"); + QueryStatus status = dbService.saveConfigTransactionLog(ctx, "test"); + assertEquals(status, "SUCCESS"); + } } diff --git a/appc-config/appc-data-services/provider/src/test/java/org/openecomp/appc/data/services/db/TestSQLSaveQuery.java b/appc-config/appc-data-services/provider/src/test/java/org/openecomp/appc/data/services/db/TestSQLSaveQuery.java index 995934933..b49c88779 100644 --- a/appc-config/appc-data-services/provider/src/test/java/org/openecomp/appc/data/services/db/TestSQLSaveQuery.java +++ b/appc-config/appc-data-services/provider/src/test/java/org/openecomp/appc/data/services/db/TestSQLSaveQuery.java @@ -25,7 +25,6 @@ import java.io.IOException; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringEscapeUtils; -import org.apache.commons.lang.StringUtils; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,145 +33,104 @@ import org.openecomp.sdnc.sli.SvcLogicContext; public class TestSQLSaveQuery { - - private static final Logger LOG = LoggerFactory.getLogger(TestSQLSaveQuery.class); - - private static String CRYPT_KEY = ""; - - //@Test - public void testSQLSaveQuery() { - - try { - String message = FileUtils.readFileToString(new File("src/test/resources/query/sampledata.txt")); - System.out.println("TestSQLSaveQuery.testSQLSaveQuery()" + message); - - SvcLogicContext ctx = new SvcLogicContext(); - ctx.setAttribute("request-id", "1234"); - - String escapedMessage = StringEscapeUtils.escapeSql(message); - ctx.setAttribute("log_message", escapedMessage); - - //String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , message_type = 'request' , message = '" + escapedMessage + "' ;"; - String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , message_type = 'request' , message = $log_message ;"; - System.out.println("Query : " + key); - String resolvedContext = resolveCtxVars(key, ctx); - - System.out.println("Resolved : " + resolvedContext); - - ctx.setAttribute("log_message", null); - - - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - - } - - - private String resolveCtxVars(String key, - SvcLogicContext ctx) { - if (key == null) { - return (null); - } - - if (key.startsWith("'") && key.endsWith("'")) { - key = key.substring(1, key.length() - 1); - - LOG.debug("Stripped outer single quotes - key is now [" + key + "]"); - } - - String[] keyTerms = key.split("\\s+"); - - StringBuffer sqlBuffer = new StringBuffer(); - - - for (int i = 0; i < keyTerms.length; i++) { - sqlBuffer.append(resolveTerm(keyTerms[i], ctx)); - sqlBuffer.append(" "); - } - - - return (sqlBuffer.toString()); - } - - - private String resolveTerm(String term, SvcLogicContext ctx) { - if (term == null) { - return (null); - } - - LOG.debug("resolveTerm: term is " + term); - - if (term.startsWith("$") && (ctx != null)) { - // Resolve any index variables. - /* - String value = term; - String resolved = resolveCtxVariable(term.substring(1), ctx); - if(resolved != null){ - value = "'" +resolved+ "'"; - } - System.out.println("Dollar Term : " + term + " Value :"+ value); - return value; -*/ - return ("'" + resolveCtxVariable(term.substring(1), ctx) + "'"); - - } else { - return (term); - } - - } - - private String resolveCtxVariable(String ctxVarName, SvcLogicContext ctx) { - - if (ctxVarName.indexOf('[') == -1) { - // Ctx variable contains no arrays - if ("CRYPT_KEY".equals(ctxVarName)) { - - // Handle crypt key as special case. If it's set as a context variable, use it. Otherwise, use - // configured crypt key. - String cryptKey = ctx.getAttribute(ctxVarName); - if ((cryptKey != null) && (cryptKey.length() > 0)) { - return(cryptKey); - } else { - return(CRYPT_KEY); - } - - } - return (ctx.getAttribute(ctxVarName)); - } - - // Resolve any array references - StringBuffer sbuff = new StringBuffer(); - String[] ctxVarParts = ctxVarName.split("\\["); - sbuff.append(ctxVarParts[0]); - for (int i = 1; i < ctxVarParts.length; i++) { - if (ctxVarParts[i].startsWith("$")) { - int endBracketLoc = ctxVarParts[i].indexOf("]"); - if (endBracketLoc == -1) { - // Missing end bracket ... give up parsing - LOG.warn("Variable reference " + ctxVarName - + " seems to be missing a ']'"); - return (ctx.getAttribute(ctxVarName)); - } - - String idxVarName = ctxVarParts[i].substring(1, endBracketLoc); - String remainder = ctxVarParts[i].substring(endBracketLoc); - - sbuff.append("["); - sbuff.append(ctx.getAttribute(idxVarName)); - sbuff.append(remainder); - - } else { - // Index is not a variable reference - sbuff.append("["); - sbuff.append(ctxVarParts[i]); - } - } - - return (ctx.getAttribute(sbuff.toString())); - } - + private static final Logger LOG = LoggerFactory.getLogger(TestSQLSaveQuery.class); + private static String CRYPT_KEY = ""; + + @Test + public void testSQLSaveQuery() { + try { + String message = FileUtils.readFileToString(new File("src/test/resources/query/sampledata.txt")); + System.out.println("TestSQLSaveQuery.testSQLSaveQuery()" + message); + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("request-id", "1234"); + String escapedMessage = StringEscapeUtils.escapeSql(message); + ctx.setAttribute("log_message", escapedMessage); + String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + + " SET request_id = $request-id , message_type = 'request' , message = $log_message ;"; + String resolvedContext = resolveCtxVars(key, ctx); + ctx.setAttribute("log_message", null); + } catch (IOException e) { + + } + + } + + private String resolveCtxVars(String key, SvcLogicContext ctx) { + if (key == null) { + return (null); + } + if (key.startsWith("'") && key.endsWith("'")) { + key = key.substring(1, key.length() - 1); + LOG.debug("Stripped outer single quotes - key is now [" + key + "]"); + } + String[] keyTerms = key.split("\\s+"); + StringBuffer sqlBuffer = new StringBuffer(); + for (int i = 0; i < keyTerms.length; i++) { + sqlBuffer.append(resolveTerm(keyTerms[i], ctx)); + sqlBuffer.append(" "); + } + return (sqlBuffer.toString()); + } + + private String resolveTerm(String term, SvcLogicContext ctx) { + if (term == null) { + return (null); + } + LOG.debug("resolveTerm: term is " + term); + if (term.startsWith("$") && (ctx != null)) { + // Resolve any index variables. + return ("'" + resolveCtxVariable(term.substring(1), ctx) + "'"); + + } else { + return (term); + } + + } + + private String resolveCtxVariable(String ctxVarName, SvcLogicContext ctx) { + + if (ctxVarName.indexOf('[') == -1) { + // Ctx variable contains no arrays + if ("CRYPT_KEY".equals(ctxVarName)) { + // Handle crypt key as special case. If it's set as a context + // variable, use it. Otherwise, use + // configured crypt key. + String cryptKey = ctx.getAttribute(ctxVarName); + if ((cryptKey != null) && (cryptKey.length() > 0)) { + return (cryptKey); + } else { + return (CRYPT_KEY); + } + + } + return (ctx.getAttribute(ctxVarName)); + } + // Resolve any array references + StringBuffer sbuff = new StringBuffer(); + String[] ctxVarParts = ctxVarName.split("\\["); + sbuff.append(ctxVarParts[0]); + for (int i = 1; i < ctxVarParts.length; i++) { + if (ctxVarParts[i].startsWith("$")) { + int endBracketLoc = ctxVarParts[i].indexOf("]"); + if (endBracketLoc == -1) { + // Missing end bracket ... give up parsing + LOG.warn("Variable reference " + ctxVarName + " seems to be missing a ']'"); + return (ctx.getAttribute(ctxVarName)); + } + + String idxVarName = ctxVarParts[i].substring(1, endBracketLoc); + String remainder = ctxVarParts[i].substring(endBracketLoc); + sbuff.append("["); + sbuff.append(ctx.getAttribute(idxVarName)); + sbuff.append(remainder); + + } else { + // Index is not a variable reference + sbuff.append("["); + sbuff.append(ctxVarParts[i]); + } + } + return (ctx.getAttribute(sbuff.toString())); + } } |