From 0d63d99ea283749d2b5c24b073b585faf8a62e7b Mon Sep 17 00:00:00 2001 From: Marcus G K Williams Date: Fri, 1 Sep 2017 15:44:56 -0700 Subject: Enable surefire unit testing Issue-Id: CCSDK-67 Change-Id: I3d03ff1a3e312edd4af7ab69508760d2fe49be78 Signed-off-by: Marcus G K Williams --- sliPluginUtils/provider/src/test/resources/dme2.e2e.properties | 7 +++++++ sliPluginUtils/provider/src/test/resources/dme2.prod.properties | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100644 sliPluginUtils/provider/src/test/resources/dme2.e2e.properties create mode 100644 sliPluginUtils/provider/src/test/resources/dme2.prod.properties (limited to 'sliPluginUtils/provider/src/test/resources') diff --git a/sliPluginUtils/provider/src/test/resources/dme2.e2e.properties b/sliPluginUtils/provider/src/test/resources/dme2.e2e.properties new file mode 100644 index 000000000..373448447 --- /dev/null +++ b/sliPluginUtils/provider/src/test/resources/dme2.e2e.properties @@ -0,0 +1,7 @@ +aafUserName=user@sample.com +aafPassword=fake +envContext=UAT +routeOffer=UAT +proxyUrl=http://sample.com:25055,http://sample.com:25055 +commonServiceVersion=1702.0 +partner= \ No newline at end of file diff --git a/sliPluginUtils/provider/src/test/resources/dme2.prod.properties b/sliPluginUtils/provider/src/test/resources/dme2.prod.properties new file mode 100644 index 000000000..4326b0048 --- /dev/null +++ b/sliPluginUtils/provider/src/test/resources/dme2.prod.properties @@ -0,0 +1,7 @@ +aafUserName=user@sample.com +aafPassword=fake +envContext=PROD +routeOffer= +proxyUrl=http://sample.com:25055,http://sample.com:25055 +commonServiceVersion=1.0 +partner=LPP_PROD \ No newline at end of file -- cgit 1.2.3-korg From f175ecd3ba0a785ac1737ab3a4196d5141c05045 Mon Sep 17 00:00:00 2001 From: "Smokowski, Kevin (ks6305)" Date: Fri, 10 May 2019 18:41:59 +0000 Subject: Remove DME2 plugin dme2 plugin was used to create dme2ingress proxy urls. This class is not generic enough to be useful to a wide audience. Change-Id: Iadc68d8f22393c3a9a5d21754137cae22b23fafe Issue-ID: CCSDK-1323 Signed-off-by: Smokowski, Kevin (ks6305) --- .../onap/ccsdk/sli/core/slipluginutils/DME2.java | 171 ------------ .../ccsdk/sli/core/slipluginutils/Dme2Factory.java | 109 -------- .../provider/src/main/resources/dme2.properties | 7 - .../blueprint/slipluginutils-blueprint.xml | 9 +- .../ccsdk/sli/core/slipluginutils/Dme2Test.java | 302 --------------------- .../src/test/resources/dme2.e2e.properties | 7 - .../src/test/resources/dme2.prod.properties | 7 - 7 files changed, 1 insertion(+), 611 deletions(-) delete mode 100644 sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/DME2.java delete mode 100644 sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/Dme2Factory.java delete mode 100644 sliPluginUtils/provider/src/main/resources/dme2.properties delete mode 100644 sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/Dme2Test.java delete mode 100644 sliPluginUtils/provider/src/test/resources/dme2.e2e.properties delete mode 100644 sliPluginUtils/provider/src/test/resources/dme2.prod.properties (limited to 'sliPluginUtils/provider/src/test/resources') diff --git a/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/DME2.java b/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/DME2.java deleted file mode 100644 index 65b597e11..000000000 --- a/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/DME2.java +++ /dev/null @@ -1,171 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : CCSDK - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights - * reserved. - * ================================================================================ - * Modifications Copyright (C) 2018 IBM. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.ccsdk.sli.core.slipluginutils; - -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Properties; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; -import org.onap.ccsdk.sli.core.sli.SvcLogicException; -import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * A SvcLogicJavaPlugin that generates DME2 proxy urls (for calling the DME2 ingress proxy) using - * parameters from context memory. - */ -public class DME2 implements SvcLogicJavaPlugin { - - private static final Logger LOG = LoggerFactory.getLogger(DME2.class); - // the key for proxyUrl, which represents a CSV list of urls - public static final String PROXY_URL_KEY = "proxyUrl"; - public static final String PROXY_URLS_VALUE_SEPARATOR = ","; - public static final String AAF_USERNAME_KEY = "aafUserName"; - public static final String AAF_PASSWORD_KEY = "aafPassword"; - public static final String ENV_CONTEXT_KEY = "envContext"; - public static final String ROUTE_OFFER_KEY = "routeOffer"; - public static final String COMMON_SERVICE_VERSION_KEY = "commonServiceVersion"; - public static final String PARTNER_KEY = "partner"; - public static final String VERSION_KEY = "version"; - public static final String SERVICE_KEY = "service"; - public static final String SUBCONTEXT_KEY = "subContext"; - public static final String ENDPOINT_READ_TIMEOUT_KEY = "endpointReadTimeout"; - public static final String OUTPUT_PATH_KEY = "outputPath"; - - final String aafUserName; - final String aafPassword; - final String envContext; - final String routeOffer; - final String[] proxyUrls; - final String commonServiceVersion; - final String partner; - final String endpointReadTimeout; - Integer index; - - public DME2(Properties properties) { - Iterator> it = properties.entrySet().iterator(); - while (it.hasNext()) { - Entry entry = it.next(); - if (entry.getValue() == null || entry.getValue().toString().length() < 1) { - it.remove(); - } - } - this.aafUserName = properties.getProperty(AAF_USERNAME_KEY, null); - this.aafPassword = properties.getProperty(AAF_PASSWORD_KEY, null); - this.envContext = properties.getProperty(ENV_CONTEXT_KEY, null); - this.routeOffer = properties.getProperty(ROUTE_OFFER_KEY, null); - this.commonServiceVersion = properties.getProperty(COMMON_SERVICE_VERSION_KEY, null); - this.partner = properties.getProperty(PARTNER_KEY, null); - this.endpointReadTimeout = properties.getProperty(ENDPOINT_READ_TIMEOUT_KEY, null); - String proxyUrlString = properties.getProperty(PROXY_URL_KEY, null); - if (proxyUrlString != null && proxyUrlString.length() > 0) { - this.proxyUrls = proxyUrlString.split(PROXY_URLS_VALUE_SEPARATOR); - } else { - String[] local = {"http://localhost:5000"}; - this.proxyUrls = local; - } - this.index = 0; - } - - // constructs a URL to contact the proxy which contacts a DME2 service - public String constructUrl(Map parameters) { - StringBuilder sb = new StringBuilder(); - - // The hostname is assigned in a round robin fashion - sb.append(acquireHostName()); - sb.append("/service=" + parameters.get(SERVICE_KEY)); - - // If the directedGraph passes an explicit version use that, if not use the commonServiceVersion - // found in the properties file - String version = parameters.get(VERSION_KEY); - if (version != null && version.length() > 0) { - sb.append("/version=" + version); - }else { - sb.append("/version=" + this.commonServiceVersion); - } - String envContext = parameters.getOrDefault(ENV_CONTEXT_KEY, this.envContext); - sb.append("/envContext=" + envContext); - - String routeOffer = parameters.getOrDefault(ROUTE_OFFER_KEY, this.routeOffer); - if (routeOffer != null && routeOffer.length() > 0) { - sb.append("/routeOffer=" + routeOffer); - } - - String subContext = parameters.get(SUBCONTEXT_KEY); - if (subContext != null && subContext.length() > 0) { - sb.append("/subContext=" + subContext); - } - sb.append("?dme2.password=" + this.aafPassword); - sb.append("&dme2.username=" + this.aafUserName); - if (this.partner != null) { - sb.append("&partner=" + this.partner); - } - sb.append("&dme2.allowhttpcode=true"); - String endpointReadTimeout = parameters.getOrDefault(ENDPOINT_READ_TIMEOUT_KEY, this.endpointReadTimeout); - if (endpointReadTimeout != null) { - sb.append("&dme2.endpointReadTimeout=" + endpointReadTimeout); - } - String incompleteUrl = sb.toString(); - - // Support optional parameters in a flexible way - for (Entry param : parameters.entrySet()) { - if (!incompleteUrl.contains(param.getKey() + "=") && param.getValue() != null - && param.getValue().length() > 0 && !OUTPUT_PATH_KEY.equals(param.getKey()) && !PARTNER_KEY.equals(param.getKey())) { - sb.append("&" + param.getKey() + "=" + param.getValue()); - } - } - return sb.toString(); - } - - public synchronized String acquireHostName() { - String retVal = proxyUrls[index]; - index++; - if (index == this.proxyUrls.length) { - index = 0; - } - return retVal; - } - - // Node entry point - public void constructUrl(Map parameters, SvcLogicContext ctx) throws SvcLogicException { - SliPluginUtils.checkParameters(parameters, new String[] {SERVICE_KEY, OUTPUT_PATH_KEY}, LOG); - String completeProxyUrl = constructUrl(parameters); - ctx.setAttribute(parameters.get(OUTPUT_PATH_KEY), completeProxyUrl); - } - - // Support legacy direct java call - public String constructUrl(String service, String version, String subContext) { - Map parameters = new HashMap<>(); - parameters.put(SERVICE_KEY, service); - if (version != null) { - parameters.put(VERSION_KEY, version); - } - parameters.put(SUBCONTEXT_KEY, subContext); - return constructUrl(parameters); - } - -} diff --git a/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/Dme2Factory.java b/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/Dme2Factory.java deleted file mode 100644 index 7013a9c5d..000000000 --- a/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/Dme2Factory.java +++ /dev/null @@ -1,109 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : CCSDK - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights - * reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.ccsdk.sli.core.slipluginutils; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.util.Optional; -import java.util.Properties; -import java.util.Vector; -import org.onap.ccsdk.sli.core.utils.JREFileResolver; -import org.onap.ccsdk.sli.core.utils.KarafRootFileResolver; -import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver; -import org.onap.ccsdk.sli.core.utils.common.CoreDefaultFileResolver; -import org.onap.ccsdk.sli.core.utils.common.SdncConfigEnvVarFileResolver; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Immutable properties container for dme2 properties. Since the initial design decision was made to - * utilize Properties instead of an OSGi ManagedService, it was decided to - * make these properties immutable. - */ -public final class Dme2Factory { - - private static final Logger LOG = LoggerFactory.getLogger(Dme2Factory.class); - private static final String DME2_PROPERTIES_FILE_NAME = "dme2.properties"; - - static Properties properties; - private Vector dme2PropertiesFileResolvers = new Vector<>(); - - public Dme2Factory() { - dme2PropertiesFileResolvers - .add(new SdncConfigEnvVarFileResolver("Using property file (1) from environment variable")); - dme2PropertiesFileResolvers.add(new CoreDefaultFileResolver("Using property file (2) from default directory")); - dme2PropertiesFileResolvers - .add(new JREFileResolver("Using property file (3) from JRE argument", Dme2Factory.class)); - dme2PropertiesFileResolvers.add(new KarafRootFileResolver("Using property file (4) from karaf root", this)); - File dme2File = getDme2File(DME2_PROPERTIES_FILE_NAME); - init(dme2File); - } - - private void init(final File dme2propertiesFile) { - try { - properties = getProperties(dme2propertiesFile); - } catch (final FileNotFoundException e) { - LOG.error("dme2.properties file could not be found at path: {}", dme2propertiesFile, e); - } catch (final IOException e) { - LOG.error("fatal error reading dme2.properties at path: {}", dme2propertiesFile, e); - } - } - - /** - * Reports the method chosen for properties resolution to the Logger. - * - * @param message Some user friendly message - * @param fileOptional The file location of the chosen properties file - * @return the file location of the chosen properties file - */ - private static File reportSuccess(final String message, final Optional fileOptional) { - if (fileOptional.isPresent()) { - final File file = fileOptional.get(); - LOG.info("{} {}", message, file.getPath()); - return file; - } - return null; - } - - private File getDme2File(final String dme2Filename) { - for (final PropertiesFileResolver dblibPropertiesFileResolver : dme2PropertiesFileResolvers) { - final Optional fileOptional = dblibPropertiesFileResolver.resolveFile(dme2Filename); - if (fileOptional.isPresent()) { - return reportSuccess(dblibPropertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional); - } - } - return (new File(dme2Filename)); - } - - private static Properties getProperties(final File dme2File) throws IOException { - final Properties properties = new Properties(); - properties.load(new FileReader(dme2File)); - return properties; - } - - public DME2 createDme2() { - return new DME2(properties); - } - -} diff --git a/sliPluginUtils/provider/src/main/resources/dme2.properties b/sliPluginUtils/provider/src/main/resources/dme2.properties deleted file mode 100644 index 373448447..000000000 --- a/sliPluginUtils/provider/src/main/resources/dme2.properties +++ /dev/null @@ -1,7 +0,0 @@ -aafUserName=user@sample.com -aafPassword=fake -envContext=UAT -routeOffer=UAT -proxyUrl=http://sample.com:25055,http://sample.com:25055 -commonServiceVersion=1702.0 -partner= \ No newline at end of file diff --git a/sliPluginUtils/provider/src/main/resources/org/opendaylight/blueprint/slipluginutils-blueprint.xml b/sliPluginUtils/provider/src/main/resources/org/opendaylight/blueprint/slipluginutils-blueprint.xml index 283dbbaa9..534b92c93 100644 --- a/sliPluginUtils/provider/src/main/resources/org/opendaylight/blueprint/slipluginutils-blueprint.xml +++ b/sliPluginUtils/provider/src/main/resources/org/opendaylight/blueprint/slipluginutils-blueprint.xml @@ -10,11 +10,4 @@ - - - - - - - \ No newline at end of file + diff --git a/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/Dme2Test.java b/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/Dme2Test.java deleted file mode 100644 index a06def652..000000000 --- a/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/Dme2Test.java +++ /dev/null @@ -1,302 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : CCSDK - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights - * reserved. - * ================================================================================ - * Modifications Copyright (C) 2018 IBM. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.ccsdk.sli.core.slipluginutils; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; -import org.junit.Assert; -import org.junit.Test; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; -import org.onap.ccsdk.sli.core.sli.SvcLogicException; - -public class Dme2Test { - public Properties makesProperties(String aafUserName, String aafPassword, String envContext, String routeOffer, - String proxyUrls, String commonServiceVersion) { - Properties props = new Properties(); - props.put(DME2.AAF_USERNAME_KEY, aafUserName); - props.put(DME2.AAF_PASSWORD_KEY, aafPassword); - props.put(DME2.ENV_CONTEXT_KEY, envContext); - props.put(DME2.ROUTE_OFFER_KEY, routeOffer); - props.put(DME2.PROXY_URL_KEY, proxyUrls); - props.put(DME2.COMMON_SERVICE_VERSION_KEY, commonServiceVersion); - return props; - } - - @Test - public void createUrl() { - String localUrl = - "http://localhost:25055/service=sample.com/services/eim/v1/rest/version=1702.0/envContext=TEST/routeOffer=DEFAULT/subContext=/enterpriseConnection/getEnterpriseConnectionDetails/v1?dme2.password=fake&dme2.username=user@sample.com&dme2.allowhttpcode=true"; - Properties props = - makesProperties("user@sample.com", "fake", "TEST", "DEFAULT", "http://localhost:25055", "common"); - DME2 dme = new DME2(props); - Map parameters = new HashMap(); - parameters.put(DME2.SERVICE_KEY, "sample.com/services/eim/v1/rest"); - parameters.put(DME2.VERSION_KEY, "1702.0"); - parameters.put(DME2.SUBCONTEXT_KEY, "/enterpriseConnection/getEnterpriseConnectionDetails/v1"); - parameters.put(DME2.OUTPUT_PATH_KEY, "tmp.test"); - - String constructedUrl = dme.constructUrl(parameters); - assertEquals(localUrl, constructedUrl); - } - - @Test - public void createUrlNoSubContext() { - String localUrl = - "http://localhost:25055/service=sample.com/services/eim/v1/rest/version=1702.0/envContext=TEST/routeOffer=DEFAULT?dme2.password=fake&dme2.username=user@sample.com&dme2.allowhttpcode=true"; - Properties props = - makesProperties("user@sample.com", "fake", "TEST", "DEFAULT", "http://localhost:25055", "common"); - DME2 dme = new DME2(props); - Map parameters = new HashMap(); - parameters.put(DME2.SERVICE_KEY, "sample.com/services/eim/v1/rest"); - parameters.put(DME2.VERSION_KEY, "1702.0"); - parameters.put(DME2.SUBCONTEXT_KEY, null); - String constructedUrl = dme.constructUrl(parameters); - assertEquals(localUrl, constructedUrl); - } - - @Test - public void testRoundRobin() { - String[] proxyHostNames = new String[] {"http://one:25055", "http://two:25055", "http://three:25055"}; - String proxyHostNameString = proxyHostNames[0] + "," + proxyHostNames[1] + "," + proxyHostNames[2]; - - String urlSuffix = - "/service=sample.com/services/eim/v1/rest/version=1702.0/envContext=TEST/routeOffer=DEFAULT/subContext=/enterpriseConnection/getEnterpriseConnectionDetails/v1?dme2.password=fake&dme2.username=user@sample.com&dme2.allowhttpcode=true"; - Properties props = makesProperties("user@sample.com", "fake", "TEST", "DEFAULT", proxyHostNameString, "common"); - DME2 dme = new DME2(props); - Map parameters = new HashMap(); - parameters.put(DME2.SERVICE_KEY, "sample.com/services/eim/v1/rest"); - parameters.put(DME2.VERSION_KEY, "1702.0"); - parameters.put(DME2.SUBCONTEXT_KEY, "/enterpriseConnection/getEnterpriseConnectionDetails/v1"); - String constructedUrl = dme.constructUrl(parameters); - assertEquals(proxyHostNames[0] + urlSuffix, constructedUrl); - constructedUrl = dme.constructUrl(parameters); - assertEquals(proxyHostNames[1] + urlSuffix, constructedUrl); - constructedUrl = dme.constructUrl(parameters); - assertEquals(proxyHostNames[2] + urlSuffix, constructedUrl); - constructedUrl = dme.constructUrl(parameters); - assertEquals(proxyHostNames[0] + urlSuffix, constructedUrl); - constructedUrl = dme.constructUrl(parameters); - assertEquals(proxyHostNames[1] + urlSuffix, constructedUrl); - constructedUrl = dme.constructUrl(parameters); - assertEquals(proxyHostNames[2] + urlSuffix, constructedUrl); - constructedUrl = dme.constructUrl(parameters); - assertEquals(proxyHostNames[0] + urlSuffix, constructedUrl); - } - - @Test - public void createDme2EndtoEnd() throws FileNotFoundException, IOException { - Properties props = new Properties(); - props.load(new FileInputStream("src/test/resources/dme2.e2e.properties")); - DME2 dme2 = new DME2(props); - assertEquals("user@sample.com", dme2.aafUserName); - assertEquals("fake", dme2.aafPassword); - assertEquals("UAT", dme2.envContext); - assertEquals("UAT", dme2.routeOffer); - Assert.assertArrayEquals( - "http://sample.com:25055,http://sample.com:25055".split(DME2.PROXY_URLS_VALUE_SEPARATOR), - dme2.proxyUrls); - assertEquals("1702.0", dme2.commonServiceVersion); - assertEquals(null, dme2.partner); - Map parameters = new HashMap(); - parameters.put(DME2.PARTNER_KEY, "I SHOULD BE FILTERED NOW!"); - parameters.put(DME2.SERVICE_KEY, "sample.com/restservices/sys/v1/assetSearch"); - parameters.put(DME2.VERSION_KEY, null); - parameters.put(DME2.SUBCONTEXT_KEY, "/mySubContext"); - String constructedUrl = dme2.constructUrl(parameters); - assertNotNull(constructedUrl); - String expected = - "http://sample.com:25055/service=sample.com/restservices/sys/v1/assetSearch/version=1702.0/envContext=UAT/routeOffer=UAT/subContext=/mySubContext?dme2.password=fake&dme2.username=user@sample.com&dme2.allowhttpcode=true"; - assertEquals(expected, constructedUrl); - } - - @Test - public void createDme2Prod() throws FileNotFoundException, IOException { - Properties props = new Properties(); - props.load(new FileInputStream("src/test/resources/dme2.prod.properties")); - DME2 dme2 = new DME2(props); - assertEquals("user@sample.com", dme2.aafUserName); - assertEquals("fake", dme2.aafPassword); - assertEquals("PROD", dme2.envContext); - assertEquals(null, dme2.routeOffer); - Assert.assertArrayEquals( - "http://sample.com:25055,http://sample.com:25055".split(DME2.PROXY_URLS_VALUE_SEPARATOR), - dme2.proxyUrls); - assertEquals("1.0", dme2.commonServiceVersion); - assertEquals("LPP_PROD", dme2.partner); - Map parameters = new HashMap(); - parameters.put(DME2.SERVICE_KEY, "sample.com/services/eim/v1/rest"); - parameters.put(DME2.VERSION_KEY, "1702.0"); - parameters.put(DME2.SUBCONTEXT_KEY, "/enterpriseConnection/getEnterpriseConnectionDetails/v1"); - String constructedUrl = dme2.constructUrl(parameters); - assertNotNull(constructedUrl); - String expected = - "http://sample.com:25055/service=sample.com/services/eim/v1/rest/version=1702.0/envContext=PROD/subContext=/enterpriseConnection/getEnterpriseConnectionDetails/v1?dme2.password=fake&dme2.username=user@sample.com&partner=LPP_PROD&dme2.allowhttpcode=true"; - assertEquals(expected, constructedUrl); - } - - @Test - public void blankProperties() throws Exception { - Properties props = new Properties(); - DME2 dme2 = new DME2(props); - Map parameters = new HashMap(); - parameters.put(DME2.SERVICE_KEY, "easyService"); - parameters.put(DME2.VERSION_KEY, "3"); - parameters.put(DME2.SUBCONTEXT_KEY, "/sub"); - assertEquals( - "http://localhost:5000/service=easyService/version=3/envContext=null/subContext=/sub?dme2.password=null&dme2.username=null&dme2.allowhttpcode=true", - dme2.constructUrl(parameters)); - } - - @Test - public void optionalParameters() { - String localUrl = - "http://localhost:25055/service=serv/version=4/envContext=TEST/routeOffer=DEFAULT/subContext=/sub?dme2.password=fake&dme2.username=user@sample.com&dme2.allowhttpcode=true&test=123"; - Properties props = - makesProperties("user@sample.com", "fake", "TEST", "DEFAULT", "http://localhost:25055", "common"); - DME2 dme = new DME2(props); - Map parameters = new HashMap(); - parameters.put(DME2.SERVICE_KEY, "serv"); - parameters.put(DME2.VERSION_KEY, "4"); - parameters.put(DME2.SUBCONTEXT_KEY, "/sub"); - parameters.put("test", "123"); - - String constructedUrl = dme.constructUrl(parameters); - assertEquals(localUrl, constructedUrl); - } - - @Test - public void createLocalUrlLegacy() { - String localUrl = - "http://localhost:25055/service=sample.com/services/eim/v1/rest/version=1702.0/envContext=TEST/routeOffer=DEFAULT/subContext=/enterpriseConnection/getEnterpriseConnectionDetails/v1?dme2.password=fake&dme2.username=user@sample.com&dme2.allowhttpcode=true"; - Properties props = - makesProperties("user@sample.com", "fake", "TEST", "DEFAULT", "http://localhost:25055", "common"); - DME2 dme = new DME2(props); - String constructedUrl = dme.constructUrl("sample.com/services/eim/v1/rest", "1702.0", - "/enterpriseConnection/getEnterpriseConnectionDetails/v1"); - assertEquals(localUrl, constructedUrl); - } - - @Test - public void createLocalUrlNoSubContextLegacy() { - String localUrl = - "http://localhost:25055/service=sample.com/services/eim/v1/rest/version=1702.0/envContext=TEST/routeOffer=DEFAULT?dme2.password=fake&dme2.username=user@sample.com&dme2.allowhttpcode=true"; - Properties props = - makesProperties("user@sample.com", "fake", "TEST", "DEFAULT", "http://localhost:25055", "common"); - DME2 dme = new DME2(props); - Map parameters = new HashMap(); - String constructedUrl = dme.constructUrl("sample.com/services/eim/v1/rest", "1702.0", parameters.get(null)); - assertEquals(localUrl, constructedUrl); - } - - @Test - public void testRoundRobinLegacy() { - String[] proxyHostNames = new String[] {"http://one:25055", "http://two:25055", "http://three:25055"}; - String urlSuffix = - "/service=sample.com/services/eim/v1/rest/version=1702.0/envContext=TEST/routeOffer=DEFAULT/subContext=/enterpriseConnection/getEnterpriseConnectionDetails/v1?dme2.password=fake&dme2.username=user@sample.com&dme2.allowhttpcode=true"; - Properties props = makesProperties("user@sample.com", "fake", "TEST", "DEFAULT", - "http://one:25055,http://two:25055,http://three:25055", "common"); - DME2 dme = new DME2(props); - String constructedUrl = dme.constructUrl("sample.com/services/eim/v1/rest", "1702.0", - "/enterpriseConnection/getEnterpriseConnectionDetails/v1"); - assertEquals(proxyHostNames[0] + urlSuffix, constructedUrl); - constructedUrl = dme.constructUrl("sample.com/services/eim/v1/rest", "1702.0", - "/enterpriseConnection/getEnterpriseConnectionDetails/v1"); - assertEquals(proxyHostNames[1] + urlSuffix, constructedUrl); - constructedUrl = dme.constructUrl("sample.com/services/eim/v1/rest", "1702.0", - "/enterpriseConnection/getEnterpriseConnectionDetails/v1"); - assertEquals(proxyHostNames[2] + urlSuffix, constructedUrl); - constructedUrl = dme.constructUrl("sample.com/services/eim/v1/rest", "1702.0", - "/enterpriseConnection/getEnterpriseConnectionDetails/v1"); - assertEquals(proxyHostNames[0] + urlSuffix, constructedUrl); - constructedUrl = dme.constructUrl("sample.com/services/eim/v1/rest", "1702.0", - "/enterpriseConnection/getEnterpriseConnectionDetails/v1"); - assertEquals(proxyHostNames[1] + urlSuffix, constructedUrl); - constructedUrl = dme.constructUrl("sample.com/services/eim/v1/rest", "1702.0", - "/enterpriseConnection/getEnterpriseConnectionDetails/v1"); - assertEquals(proxyHostNames[2] + urlSuffix, constructedUrl); - constructedUrl = dme.constructUrl("sample.com/services/eim/v1/rest", "1702.0", - "/enterpriseConnection/getEnterpriseConnectionDetails/v1"); - assertEquals(proxyHostNames[0] + urlSuffix, constructedUrl); - } - - @Test - public void createDme2EndtoEndLegacy() throws Exception { - Properties props = new Properties(); - props.load(new FileInputStream("src/test/resources/dme2.e2e.properties")); - DME2 dme2 = new DME2(props); - assertEquals("user@sample.com", dme2.aafUserName); - assertEquals("fake", dme2.aafPassword); - assertEquals("UAT", dme2.envContext); - assertEquals("UAT", dme2.routeOffer); - Assert.assertArrayEquals("http://sample.com:25055,http://sample.com:25055".split(","), dme2.proxyUrls); - assertEquals("1702.0", dme2.commonServiceVersion); - assertEquals(null, dme2.partner); - String constructedUrl = dme2.constructUrl("sample.com/restservices/sys/v1/assetSearch", null, "/mySubContext"); - assertNotNull(constructedUrl); - String expected = - "http://sample.com:25055/service=sample.com/restservices/sys/v1/assetSearch/version=1702.0/envContext=UAT/routeOffer=UAT/subContext=/mySubContext?dme2.password=fake&dme2.username=user@sample.com&dme2.allowhttpcode=true"; - assertEquals(expected, constructedUrl); - } - - @Test - public void createDme2ProdLegacy() throws Exception { - Properties props = new Properties(); - props.load(new FileInputStream("src/test/resources/dme2.prod.properties")); - DME2 dme2 = new DME2(props); - assertEquals("user@sample.com", dme2.aafUserName); - assertEquals("fake", dme2.aafPassword); - assertEquals("PROD", dme2.envContext); - assertEquals(null, dme2.routeOffer); - Assert.assertArrayEquals("http://sample.com:25055,http://sample.com:25055".split(","), dme2.proxyUrls); - assertEquals("1.0", dme2.commonServiceVersion); - assertEquals("LPP_PROD", dme2.partner); - String constructedUrl = dme2.constructUrl("sample.com/restservices/sys/v1/assetSearch", null, "/mySubContext"); - assertNotNull(constructedUrl); - String expected = - "http://sample.com:25055/service=sample.com/restservices/sys/v1/assetSearch/version=1.0/envContext=PROD/subContext=/mySubContext?dme2.password=fake&dme2.username=user@sample.com&partner=LPP_PROD&dme2.allowhttpcode=true"; - assertEquals(expected, constructedUrl); - } - - @Test - public void testConstructUrl() throws FileNotFoundException, IOException, SvcLogicException - { - Properties props = new Properties(); - props.load(new FileInputStream("src/test/resources/dme2.prod.properties")); - DME2 dme2 = new DME2(props); - Map parameters= new HashMap<>(); - SvcLogicContext ctx= new SvcLogicContext(); - parameters.put(DME2.SERVICE_KEY, "sample.com/services/eim/v1/rest"); - parameters.put(DME2.OUTPUT_PATH_KEY, "tmp.test"); - dme2.constructUrl(parameters,ctx); - String expected= "http://sample.com:25055/service=sample.com/services/eim/v1/rest/version=1.0/envContext=PROD?dme2.password=fake&dme2.username=user@sample.com&partner=LPP_PROD&dme2.allowhttpcode=true"; - assertEquals(expected,ctx.getAttribute(parameters.get(DME2.OUTPUT_PATH_KEY))); - } - -} diff --git a/sliPluginUtils/provider/src/test/resources/dme2.e2e.properties b/sliPluginUtils/provider/src/test/resources/dme2.e2e.properties deleted file mode 100644 index 373448447..000000000 --- a/sliPluginUtils/provider/src/test/resources/dme2.e2e.properties +++ /dev/null @@ -1,7 +0,0 @@ -aafUserName=user@sample.com -aafPassword=fake -envContext=UAT -routeOffer=UAT -proxyUrl=http://sample.com:25055,http://sample.com:25055 -commonServiceVersion=1702.0 -partner= \ No newline at end of file diff --git a/sliPluginUtils/provider/src/test/resources/dme2.prod.properties b/sliPluginUtils/provider/src/test/resources/dme2.prod.properties deleted file mode 100644 index 4326b0048..000000000 --- a/sliPluginUtils/provider/src/test/resources/dme2.prod.properties +++ /dev/null @@ -1,7 +0,0 @@ -aafUserName=user@sample.com -aafPassword=fake -envContext=PROD -routeOffer= -proxyUrl=http://sample.com:25055,http://sample.com:25055 -commonServiceVersion=1.0 -partner=LPP_PROD \ No newline at end of file -- cgit 1.2.3-korg From 2fa35ec7af06c9f1d1982dc500726de703f7dede Mon Sep 17 00:00:00 2001 From: "Smokowski, Kevin (ks6305)" Date: Tue, 17 Dec 2019 18:27:42 +0000 Subject: enhance SliPluginUtils writeJsonToCtx now supports top level json arrays, add urlDecode to SliStringUtils, rename CheckParametersTest and move unrelated methods into another test class and Add test cases to improve test coverage Issue-ID: CCSDK-2006 Signed-off-by: Smokowski, Kevin (ks6305) Change-Id: If1f654ea9c7046b00c977434f87740e54f15b7d3 --- .../sli/core/slipluginutils/SliPluginUtils.java | 40 ++- .../sli/core/slipluginutils/SliStringUtils.java | 15 + .../core/slipluginutils/CheckParametersTest.java | 216 ------------- .../SliPluginUtils_StaticFunctionsTest.java | 342 +++++++++++++++++++-- .../SliPluginUtils_checkParametersTest.java | 117 +++++++ .../core/slipluginutils/SliStringUtilsTest.java | 17 +- .../provider/src/test/resources/2dArray.json | 4 + .../provider/src/test/resources/3dArray.json | 4 + .../provider/src/test/resources/ArrayMenu.json | 41 +++ .../src/test/resources/EmbeddedEscapedJson.json | 16 + .../provider/src/test/resources/EscapedJson.json | 1 + .../provider/src/test/resources/ObjectMenu.json | 43 +++ .../provider/src/test/resources/Widget.json | 27 ++ 13 files changed, 626 insertions(+), 257 deletions(-) delete mode 100644 sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/CheckParametersTest.java create mode 100644 sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_checkParametersTest.java create mode 100644 sliPluginUtils/provider/src/test/resources/2dArray.json create mode 100644 sliPluginUtils/provider/src/test/resources/3dArray.json create mode 100644 sliPluginUtils/provider/src/test/resources/ArrayMenu.json create mode 100644 sliPluginUtils/provider/src/test/resources/EmbeddedEscapedJson.json create mode 100644 sliPluginUtils/provider/src/test/resources/EscapedJson.json create mode 100644 sliPluginUtils/provider/src/test/resources/ObjectMenu.json create mode 100644 sliPluginUtils/provider/src/test/resources/Widget.json (limited to 'sliPluginUtils/provider/src/test/resources') diff --git a/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java b/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java index b8e88f652..1fc052532 100644 --- a/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java +++ b/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java @@ -821,32 +821,46 @@ public class SliPluginUtils implements SvcLogicJavaPlugin { protected static void writeJsonToCtx(String resp, SvcLogicContext ctx, String prefix){ JsonParser jp = new JsonParser(); JsonElement element = jp.parse(resp); - writeJsonObject(element.getAsJsonObject(), ctx, prefix + "."); + String root = prefix + "."; + if (element.isJsonObject()) { + writeJsonObject(element.getAsJsonObject(), ctx, root); + } else if (element.isJsonArray()) { + handleJsonArray("", element.getAsJsonArray(), ctx, root); + } } protected static void writeJsonObject(JsonObject obj, SvcLogicContext ctx, String root) { for (Entry entry : obj.entrySet()) { + String key = entry.getKey(); if (entry.getValue().isJsonObject()) { - writeJsonObject(entry.getValue().getAsJsonObject(), ctx, root + entry.getKey() + "."); + writeJsonObject(entry.getValue().getAsJsonObject(), ctx, root + key + "."); } else if (entry.getValue().isJsonArray()) { JsonArray array = entry.getValue().getAsJsonArray(); - ctx.setAttribute(root + entry.getKey() + LENGTH, String.valueOf(array.size())); - Integer arrayIdx = 0; - for (JsonElement element : array) { - if (element.isJsonObject()) { - writeJsonObject(element.getAsJsonObject(), ctx, root + entry.getKey() + "[" + arrayIdx + "]."); - } else if (element.isJsonPrimitive()) { - ctx.setAttribute(root + entry.getKey() + "[" + arrayIdx + "]", element.getAsString()); - } - arrayIdx++; - } + handleJsonArray(key, array, ctx, root); } else { //Handles when a JSON obj is nested within a JSON obj if(!root.endsWith(".")){ root = root + "."; } - ctx.setAttribute(root + entry.getKey(), entry.getValue().getAsString()); + ctx.setAttribute(root + key, entry.getValue().getAsString()); + } + } + } + + protected static void handleJsonArray(String key, JsonArray array, SvcLogicContext ctx, String root) { + ctx.setAttribute(root + key + LENGTH, String.valueOf(array.size())); + Integer arrayIdx = 0; + for (JsonElement element : array) { + String prefix = root + key + "[" + arrayIdx + "]"; + + if (element.isJsonArray()) { + handleJsonArray(key, element.getAsJsonArray(), ctx, prefix); + } else if (element.isJsonObject()) { + writeJsonObject(element.getAsJsonObject(), ctx, prefix + "."); + } else if (element.isJsonPrimitive()) { + ctx.setAttribute(prefix, element.getAsString()); } + arrayIdx++; } } diff --git a/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtils.java b/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtils.java index 269c3766c..d343ce25f 100644 --- a/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtils.java +++ b/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtils.java @@ -24,6 +24,7 @@ package org.onap.ccsdk.sli.core.slipluginutils; import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; import java.net.URLEncoder; import java.util.Map; import org.apache.commons.lang3.StringEscapeUtils; @@ -427,6 +428,20 @@ public class SliStringUtils implements SvcLogicJavaPlugin { } } + public static void urlDecode(Map parameters, SvcLogicContext ctx) throws SvcLogicException { + SliPluginUtils.checkParameters(parameters, new String[] {INPUT_PARAM_SOURCE, "outputPath"}, LOG); + String encoding = parameters.get("encoding"); + if (encoding == null) { + encoding = "UTF-8"; + } + try { + String result = URLDecoder.decode(parameters.get(INPUT_PARAM_SOURCE), encoding); + ctx.setAttribute(parameters.get("outputPath"), result); + } catch (UnsupportedEncodingException e) { + throw new SvcLogicException("Url decode failed.", e); + } + } + /** * xmlEscapeText() will be used to format input xml with text. * diff --git a/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/CheckParametersTest.java b/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/CheckParametersTest.java deleted file mode 100644 index 21aa4a356..000000000 --- a/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/CheckParametersTest.java +++ /dev/null @@ -1,216 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : CCSDK - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights - * reserved. - * ================================================================================ - * Modifications Copyright (C) 2018 IBM. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.ccsdk.sli.core.slipluginutils; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.Test; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; -import org.onap.ccsdk.sli.core.sli.SvcLogicException; -import org.onap.ccsdk.sli.core.slipluginutils.SliPluginUtils.LogLevel; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.Marker; - -import com.google.gson.JsonObject; - -public class CheckParametersTest { - - @Test - public void nullRequiredParameters() throws Exception { - Map parametersMap = new HashMap(); - String[] requiredParams = null; - Logger Log = LoggerFactory.getLogger(SliPluginUtils.class); - SliPluginUtils.checkParameters(parametersMap, requiredParams, Log); - } - - @Test(expected = SvcLogicException.class) - public void emptyParametersMap() throws Exception { - Map parametersMap = new HashMap(); - String[] requiredParams = new String[] { "param1", "param2", "param3" }; - Logger Log = LoggerFactory.getLogger(SliPluginUtils.class); - SliPluginUtils.checkParameters(parametersMap, requiredParams, Log); - } - - @Test(expected = SvcLogicException.class) - public void paramNotFound() throws Exception { - Map parametersMap = new HashMap(); - parametersMap.put("tst", "me"); - String[] requiredParams = new String[] { "param1", "parm2", "param3" }; - Logger Log = LoggerFactory.getLogger(SliPluginUtils.class); - SliPluginUtils.checkParameters(parametersMap, requiredParams, Log); - } - - @Test - public void testSunnyRequiredParameters() throws Exception { - SvcLogicContext ctx = new SvcLogicContext(); - ctx.setAttribute("param1", "hello"); - ctx.setAttribute("param2", "world"); - ctx.setAttribute("param3", "!"); - - Map parameters = new HashMap(); - parameters.put("param1", "dog"); - parameters.put("param2", "cat"); - parameters.put("param3", "fish"); - - SliPluginUtils.requiredParameters(parameters, ctx); - } - - @Test - public void testSunnyRequiredParametersWithPrefix() throws Exception { - String prefixValue = "my.unique.path."; - SvcLogicContext ctx = new SvcLogicContext(); - ctx.setAttribute(prefixValue + "param1", "hello"); - ctx.setAttribute(prefixValue + "param2", "world"); - ctx.setAttribute(prefixValue + "param3", "!"); - - Map parameters = new HashMap(); - parameters.put("prefix", prefixValue); - parameters.put("param1", "dog"); - parameters.put("param2", "cat"); - parameters.put("param3", "fish"); - - SliPluginUtils.requiredParameters(parameters, ctx); - } - - @Test(expected = SvcLogicException.class) - public void testRainyMissingRequiredParameters() throws Exception { - SvcLogicContext ctx = new SvcLogicContext(); - ctx.setAttribute("param1", "hello"); - ctx.setAttribute("param3", "!"); - - Map parameters = new HashMap(); - parameters.put("param1", null); - parameters.put("param2", null); - parameters.put("param3", null); - - SliPluginUtils.requiredParameters(parameters, ctx); - } - - @Test(expected = SvcLogicException.class) - public void testEmptyRequiredParameters() throws Exception { - SvcLogicContext ctx = new SvcLogicContext(); - ctx.setAttribute("param1", "hello"); - ctx.setAttribute("param3", "!"); - - Map parameters = new HashMap(); - - SliPluginUtils.requiredParameters(parameters, ctx); - } - - @Test(expected = SvcLogicException.class) - public void testJsonStringToCtx() throws Exception { - SvcLogicContext ctx = new SvcLogicContext(); - Map parameters = new HashMap(); - parameters.put("outputPath", "testPath"); - parameters.put("isEscaped", "true"); - parameters.put("source", "//{/name1/:value1/}//"); - SliPluginUtils.jsonStringToCtx(parameters, ctx); - } - - @Test - public void testGetAttributeValue() throws Exception { - SvcLogicContext ctx = new SvcLogicContext(); - Map parameters = new HashMap(); - parameters.put("outputPath", "testPath"); - parameters.put("source", "testSource"); - SliPluginUtils.getAttributeValue(parameters, ctx); - assertNull(ctx.getAttribute(parameters.get("outputPath"))); - } - - @Test - public void testCtxListContains() throws Exception { - SvcLogicContext ctx = new SvcLogicContext(); - Map parameters = new HashMap(); - parameters.put("list", "10_length"); - parameters.put("keyName", "testName"); - parameters.put("keyValue", "testValue"); - ctx.setAttribute("10_length", "10"); - assertEquals("false", SliPluginUtils.ctxListContains(parameters, ctx)); - - } - - @Test(expected= SvcLogicException.class) - public void testPrintContextForNullParameters() throws SvcLogicException - { - SvcLogicContext ctx = new SvcLogicContext(); - Map parameters = new HashMap(); - SliPluginUtils.printContext(parameters, ctx); - } - - @Test - public void testPrintContext() throws SvcLogicException - { - SvcLogicContext ctx = new SvcLogicContext(); - Map parameters = new HashMap(); - parameters.put("filename","testFileName"); - SliPluginUtils.printContext(parameters, ctx); - } - - @Test - public void testWriteJsonObject() throws SvcLogicException - { - JsonObject obj=new JsonObject(); - obj.addProperty("name","testName"); - obj.addProperty("age",27); - obj.addProperty("salary",600000); - SvcLogicContext ctx = new SvcLogicContext(); - SliPluginUtils.writeJsonObject(obj, ctx,"root"); - assertEquals("testName", ctx.getAttribute("root.name")); - assertEquals("27", ctx.getAttribute("root.age")); - assertEquals("600000", ctx.getAttribute("root.salary")); - } - - @Test - public void testCtxKeyEmpty() - { - SvcLogicContext ctx = new SvcLogicContext(); - ctx.setAttribute("key", ""); - assertTrue(SliPluginUtils.ctxKeyEmpty(ctx, "key")); - } - - @Test - public void testGetArrayLength() - { - SvcLogicContext ctx = new SvcLogicContext(); - ctx.setAttribute("key_length", "test"); - Logger log = LoggerFactory.getLogger(getClass()); - SliPluginUtils.getArrayLength(ctx, "key", log , LogLevel.INFO, "invalid input"); - } - - @Test - public void testSetPropertiesForRoot() - { - SvcLogicContext ctx = new SvcLogicContext(); - Map parameters= new HashMap<>(); - parameters.put("root","RootVal"); - parameters.put("valueRoot", "ValueRootVal"); - assertEquals("success",SliPluginUtils.setPropertiesForRoot(parameters,ctx)); - } -} diff --git a/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java b/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java index 08adc9731..64645b363 100644 --- a/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java +++ b/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java @@ -24,14 +24,20 @@ package org.onap.ccsdk.sli.core.slipluginutils; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; import org.junit.Before; import org.junit.Test; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.onap.ccsdk.sli.core.slipluginutils.SliPluginUtils.LogLevel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.gson.JsonObject; public class SliPluginUtils_StaticFunctionsTest { private static final Logger LOG = LoggerFactory.getLogger(SliPluginUtils_StaticFunctionsTest.class); @@ -216,28 +222,6 @@ public class SliPluginUtils_StaticFunctionsTest { SliPluginUtils.ctxSetAttribute(ctx, "test", i, LOG, SliPluginUtils.LogLevel.TRACE); } - /*@Test - public void printContext() throws SvcLogicException, IOException { - String filePath = "/src/test/resources/printContext.txt"; - parameters.put("filename", filePath); - File f = new File(filePath); - assert (f.exists()); - assert (!f.isDirectory()); - ctx.setAttribute("hello", "world"); - ctx.setAttribute("name", "value"); - - SliPluginUtils.printContext(parameters, ctx); - BufferedReader br = new BufferedReader(new FileReader(f)); - String line = br.readLine(); - assertEquals("#######################################", line); - line = br.readLine(); - assertEquals("hello = world", line); - line = br.readLine(); - assertEquals("name = value", line); - br.close(); - Files.delete(Paths.get(filePath)); - }*/ - @Test public void setTime() throws SvcLogicException { String outputPath = "output"; @@ -264,4 +248,318 @@ public class SliPluginUtils_StaticFunctionsTest { assertEquals(SliStringUtils.TRUE_CONSTANT, result); } + @Test + public void testGetAttributeValue() throws Exception { + parameters.put("outputPath", "testPath"); + parameters.put("source", "testSource"); + SliPluginUtils.getAttributeValue(parameters, ctx); + assertNull(ctx.getAttribute(parameters.get("outputPath"))); + } + + @Test + public void testCtxListContains() throws Exception { + parameters.put("list", "10_length"); + parameters.put("keyName", "testName"); + parameters.put("keyValue", "testValue"); + ctx.setAttribute("10_length", "10"); + assertEquals("false", SliPluginUtils.ctxListContains(parameters, ctx)); + + } + + @Test(expected = SvcLogicException.class) + public void testPrintContextForEmptyParameters() throws SvcLogicException { + SliPluginUtils.printContext(parameters, ctx); + } + + @Test(expected = SvcLogicException.class) + public void testPrintContextForNullParameters() throws SvcLogicException { + SliPluginUtils.printContext(null, ctx); + } + + @Test + public void testPrintContext() throws SvcLogicException { + parameters.put("filename", "testFileName"); + SliPluginUtils.printContext(parameters, ctx); + } + + @Test + public void testWriteJsonObject() throws SvcLogicException { + JsonObject obj = new JsonObject(); + obj.addProperty("name", "testName"); + obj.addProperty("age", 27); + obj.addProperty("salary", 600000); + SvcLogicContext ctx = new SvcLogicContext(); + SliPluginUtils.writeJsonObject(obj, ctx, "root"); + assertEquals("testName", ctx.getAttribute("root.name")); + assertEquals("27", ctx.getAttribute("root.age")); + assertEquals("600000", ctx.getAttribute("root.salary")); + } + + @Test + public void testCtxKeyEmpty() { + ctx.setAttribute("key", ""); + assertTrue(SliPluginUtils.ctxKeyEmpty(ctx, "key")); + } + + @Test + public void testGetArrayLength() { + ctx.setAttribute("key_length", "test"); + Logger log = LoggerFactory.getLogger(getClass()); + SliPluginUtils.getArrayLength(ctx, "key", log, LogLevel.INFO, "invalid input"); + } + + @Test + public void testSetPropertiesForRoot() { + Map parameters = new HashMap<>(); + parameters.put("root", "RootVal"); + parameters.put("valueRoot", "ValueRootVal"); + assertEquals("success", SliPluginUtils.setPropertiesForRoot(parameters, ctx)); + } + + @Test + public void testJsonStringToCtxToplevelArray() throws Exception { + String path = "src/test/resources/ArrayMenu.json"; + String content = new String(Files.readAllBytes(Paths.get(path))); + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("input", content); + Map parameters = new HashMap(); + parameters.put("outputPath", "testPath"); + parameters.put("isEscaped", "false"); + parameters.put("source", "input"); + + SliPluginUtils.jsonStringToCtx(parameters, ctx); + + assertEquals("1000", ctx.getAttribute("testPath.[0].calories")); + assertEquals("1", ctx.getAttribute("testPath.[0].id")); + assertEquals("plain", ctx.getAttribute("testPath.[0].name")); + assertEquals("pizza", ctx.getAttribute("testPath.[0].type")); + assertEquals("true", ctx.getAttribute("testPath.[0].vegetarian")); + assertEquals("2000", ctx.getAttribute("testPath.[1].calories")); + assertEquals("2", ctx.getAttribute("testPath.[1].id")); + assertEquals("Tuesday Special", ctx.getAttribute("testPath.[1].name")); + assertEquals("1", ctx.getAttribute("testPath.[1].topping[0].id")); + assertEquals("onion", ctx.getAttribute("testPath.[1].topping[0].name")); + assertEquals("2", ctx.getAttribute("testPath.[1].topping[1].id")); + assertEquals("pepperoni", ctx.getAttribute("testPath.[1].topping[1].name")); + assertEquals("2", ctx.getAttribute("testPath.[1].topping_length")); + assertEquals("pizza", ctx.getAttribute("testPath.[1].type")); + assertEquals("false", ctx.getAttribute("testPath.[1].vegetarian")); + assertEquals("1500", ctx.getAttribute("testPath.[2].calories")); + assertEquals("3", ctx.getAttribute("testPath.[2].id")); + assertEquals("House Special", ctx.getAttribute("testPath.[2].name")); + assertEquals("3", ctx.getAttribute("testPath.[2].topping[0].id")); + assertEquals("basil", ctx.getAttribute("testPath.[2].topping[0].name")); + assertEquals("4", ctx.getAttribute("testPath.[2].topping[1].id")); + assertEquals("fresh mozzarella", ctx.getAttribute("testPath.[2].topping[1].name")); + assertEquals("5", ctx.getAttribute("testPath.[2].topping[2].id")); + assertEquals("tomato", ctx.getAttribute("testPath.[2].topping[2].name")); + assertEquals("3", ctx.getAttribute("testPath.[2].topping_length")); + assertEquals("pizza", ctx.getAttribute("testPath.[2].type")); + assertEquals("true", ctx.getAttribute("testPath.[2].vegetarian")); + assertEquals("3", ctx.getAttribute("testPath._length")); + } + + @Test + public void testJsonStringToCtx() throws Exception { + String path = "src/test/resources/ObjectMenu.json"; + String content = new String(Files.readAllBytes(Paths.get(path))); + + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("input", content); + + Map parameters = new HashMap(); + parameters.put("outputPath", "testPath"); + parameters.put("isEscaped", "false"); + parameters.put("source", "input"); + + SliPluginUtils.jsonStringToCtx(parameters, ctx); + + + assertEquals("1000", ctx.getAttribute("testPath.menu[0].calories")); + assertEquals("1", ctx.getAttribute("testPath.menu[0].id")); + assertEquals("plain", ctx.getAttribute("testPath.menu[0].name")); + assertEquals("pizza", ctx.getAttribute("testPath.menu[0].type")); + assertEquals("true", ctx.getAttribute("testPath.menu[0].vegetarian")); + assertEquals("2000", ctx.getAttribute("testPath.menu[1].calories")); + assertEquals("2", ctx.getAttribute("testPath.menu[1].id")); + assertEquals("Tuesday Special", ctx.getAttribute("testPath.menu[1].name")); + assertEquals("1", ctx.getAttribute("testPath.menu[1].topping[0].id")); + assertEquals("onion", ctx.getAttribute("testPath.menu[1].topping[0].name")); + assertEquals("2", ctx.getAttribute("testPath.menu[1].topping[1].id")); + assertEquals("pepperoni", ctx.getAttribute("testPath.menu[1].topping[1].name")); + assertEquals("2", ctx.getAttribute("testPath.menu[1].topping_length")); + assertEquals("pizza", ctx.getAttribute("testPath.menu[1].type")); + assertEquals("false", ctx.getAttribute("testPath.menu[1].vegetarian")); + assertEquals("1500", ctx.getAttribute("testPath.menu[2].calories")); + assertEquals("3", ctx.getAttribute("testPath.menu[2].id")); + assertEquals("House Special", ctx.getAttribute("testPath.menu[2].name")); + assertEquals("3", ctx.getAttribute("testPath.menu[2].topping[0].id")); + assertEquals("basil", ctx.getAttribute("testPath.menu[2].topping[0].name")); + assertEquals("4", ctx.getAttribute("testPath.menu[2].topping[1].id")); + assertEquals("fresh mozzarella", ctx.getAttribute("testPath.menu[2].topping[1].name")); + assertEquals("5", ctx.getAttribute("testPath.menu[2].topping[2].id")); + assertEquals("tomato", ctx.getAttribute("testPath.menu[2].topping[2].name")); + assertEquals("3", ctx.getAttribute("testPath.menu[2].topping_length")); + assertEquals("pizza", ctx.getAttribute("testPath.menu[2].type")); + assertEquals("true", ctx.getAttribute("testPath.menu[2].vegetarian")); + assertEquals("3", ctx.getAttribute("testPath.menu_length")); + } + + @Test + public void test2dJsonStringToCtx() throws Exception { + String path = "src/test/resources/2dArray.json"; + String content = new String(Files.readAllBytes(Paths.get(path))); + + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("input", content); + + Map parameters = new HashMap(); + parameters.put("outputPath", "testPath"); + parameters.put("isEscaped", "false"); + parameters.put("source", "input"); + + SliPluginUtils.jsonStringToCtx(parameters, ctx); + assertEquals("apple", ctx.getAttribute("testPath.[0][0]")); + assertEquals("orange", ctx.getAttribute("testPath.[0][1]")); + assertEquals("banana", ctx.getAttribute("testPath.[0][2]")); + assertEquals("3", ctx.getAttribute("testPath.[0]_length")); + assertEquals("squash", ctx.getAttribute("testPath.[1][0]")); + assertEquals("broccoli", ctx.getAttribute("testPath.[1][1]")); + assertEquals("cauliflower", ctx.getAttribute("testPath.[1][2]")); + assertEquals("3", ctx.getAttribute("testPath.[1]_length")); + assertEquals("2", ctx.getAttribute("testPath._length")); + } + + @Test + public void test3dJsonStringToCtx() throws Exception { + String path = "src/test/resources/3dArray.json"; + String content = new String(Files.readAllBytes(Paths.get(path))); + + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("input", content); + + Map parameters = new HashMap(); + parameters.put("outputPath", "testPath"); + parameters.put("isEscaped", "false"); + parameters.put("source", "input"); + + SliPluginUtils.jsonStringToCtx(parameters, ctx); + assertEquals("a", ctx.getAttribute("testPath.[0][0][0]")); + assertEquals("b", ctx.getAttribute("testPath.[0][0][1]")); + assertEquals("c", ctx.getAttribute("testPath.[0][0][2]")); + assertEquals("3", ctx.getAttribute("testPath.[0][0]_length")); + assertEquals("d", ctx.getAttribute("testPath.[0][1][0]")); + assertEquals("e", ctx.getAttribute("testPath.[0][1][1]")); + assertEquals("f", ctx.getAttribute("testPath.[0][1][2]")); + assertEquals("3", ctx.getAttribute("testPath.[0][1]_length")); + assertEquals("2", ctx.getAttribute("testPath.[0]_length")); + assertEquals("x", ctx.getAttribute("testPath.[1][0][0]")); + assertEquals("y", ctx.getAttribute("testPath.[1][0][1]")); + assertEquals("z", ctx.getAttribute("testPath.[1][0][2]")); + assertEquals("3", ctx.getAttribute("testPath.[1][0]_length")); + assertEquals("1", ctx.getAttribute("testPath.[1]_length")); + assertEquals("2", ctx.getAttribute("testPath._length")); + } + + @Test + public void testJsonWidgetStringToCtx() throws Exception { + String path = "src/test/resources/Widget.json"; + String content = new String(Files.readAllBytes(Paths.get(path))); + + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("input", content); + + Map parameters = new HashMap(); + parameters.put("outputPath", "testPath"); + parameters.put("isEscaped", "false"); + parameters.put("source", "input"); + + SliPluginUtils.jsonStringToCtx(parameters, ctx); + assertEquals("false", ctx.getAttribute("testPath.widget.debug")); + assertEquals("center", ctx.getAttribute("testPath.widget.image.alignment")); + assertEquals("150", ctx.getAttribute("testPath.widget.image.hOffset")); + assertEquals("moon", ctx.getAttribute("testPath.widget.image.name")); + assertEquals("images/moon.png", ctx.getAttribute("testPath.widget.image.src")); + assertEquals("150", ctx.getAttribute("testPath.widget.image.vOffset")); + assertEquals("center", ctx.getAttribute("testPath.widget.text.alignment")); + assertEquals("Click Me", ctx.getAttribute("testPath.widget.text.data")); + assertEquals("350", ctx.getAttribute("testPath.widget.text.hOffset")); + assertEquals("text1", ctx.getAttribute("testPath.widget.text.name")); + assertEquals("21", ctx.getAttribute("testPath.widget.text.size")); + assertEquals("bold", ctx.getAttribute("testPath.widget.text.style")); + assertEquals("200", ctx.getAttribute("testPath.widget.text.vOffset")); + assertEquals("300", ctx.getAttribute("testPath.widget.window.height")); + assertEquals("main_window", ctx.getAttribute("testPath.widget.window.name")); + assertEquals("ONAP Widget", ctx.getAttribute("testPath.widget.window.title")); + assertEquals("200", ctx.getAttribute("testPath.widget.window.width")); + } + + @Test + public void testEmbeddedEscapedJsonJsonStringToCtx() throws Exception { + String path = "src/test/resources/EmbeddedEscapedJson.json"; + String content = new String(Files.readAllBytes(Paths.get(path))); + + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("input", content); + + Map parameters = new HashMap(); + parameters.put("outputPath", "testPath"); + parameters.put("isEscaped", "false"); + parameters.put("source", "input"); + + SliPluginUtils.jsonStringToCtx(parameters, ctx); + + assertEquals("escapedJsonObject", ctx.getAttribute("testPath.input.parameters[0].name")); + assertEquals("[{\"id\":\"0.2.0.0/16\"},{\"id\":\"ge04::/64\"}]", + ctx.getAttribute("testPath.input.parameters[0].value")); + assertEquals("Hello/World", ctx.getAttribute("testPath.input.parameters[1].value")); + assertEquals("resourceName", ctx.getAttribute("testPath.input.parameters[2].name")); + assertEquals("The\t\"Best\"\tName", ctx.getAttribute("testPath.input.parameters[2].value")); + assertEquals("3", ctx.getAttribute("testPath.input.parameters_length")); + + + // Break the embedded json object into properties + parameters.put("outputPath", "testPath.input.parameters[0].value"); + parameters.put("source", "testPath.input.parameters[0].value"); + SliPluginUtils.jsonStringToCtx(parameters, ctx); + + assertEquals("0.2.0.0/16", ctx.getAttribute("testPath.input.parameters[0].value.[0].id")); + assertEquals("ge04::/64", ctx.getAttribute("testPath.input.parameters[0].value.[1].id")); + assertEquals("2", ctx.getAttribute("testPath.input.parameters[0].value._length")); + } + + @Test + public void testEscapedJsonStringToCtx() throws Exception { + String path = "src/test/resources/EscapedJson.json"; + String content = new String(Files.readAllBytes(Paths.get(path))); + + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("input", content); + + Map parameters = new HashMap(); + parameters.put("outputPath", "testPath"); + parameters.put("isEscaped", "true"); // set to true because the entire json content has been escaped + parameters.put("source", "input"); + + + SliPluginUtils.jsonStringToCtx(parameters, ctx); + assertEquals("false", ctx.getAttribute("testPath.widget.debug")); + assertEquals("center", ctx.getAttribute("testPath.widget.image.alignment")); + assertEquals("150", ctx.getAttribute("testPath.widget.image.hOffset")); + assertEquals("moon", ctx.getAttribute("testPath.widget.image.name")); + assertEquals("images/moon.png", ctx.getAttribute("testPath.widget.image.src")); + assertEquals("150", ctx.getAttribute("testPath.widget.image.vOffset")); + assertEquals("center", ctx.getAttribute("testPath.widget.text.alignment")); + assertEquals("Click Me", ctx.getAttribute("testPath.widget.text.data")); + assertEquals("350", ctx.getAttribute("testPath.widget.text.hOffset")); + assertEquals("text1", ctx.getAttribute("testPath.widget.text.name")); + assertEquals("21", ctx.getAttribute("testPath.widget.text.size")); + assertEquals("bold", ctx.getAttribute("testPath.widget.text.style")); + assertEquals("200", ctx.getAttribute("testPath.widget.text.vOffset")); + assertEquals("300", ctx.getAttribute("testPath.widget.window.height")); + assertEquals("main_window", ctx.getAttribute("testPath.widget.window.name")); + assertEquals("ONAP Widget", ctx.getAttribute("testPath.widget.window.title")); + assertEquals("200", ctx.getAttribute("testPath.widget.window.width")); + } + } diff --git a/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_checkParametersTest.java b/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_checkParametersTest.java new file mode 100644 index 000000000..f92271f7c --- /dev/null +++ b/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_checkParametersTest.java @@ -0,0 +1,117 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : CCSDK + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Modifications Copyright (C) 2018 IBM. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.core.slipluginutils; + +import java.util.HashMap; +import java.util.Map; +import org.junit.Test; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SliPluginUtils_checkParametersTest { + + @Test + public void nullRequiredParameters() throws Exception { + Map parametersMap = new HashMap(); + String[] requiredParams = null; + Logger Log = LoggerFactory.getLogger(SliPluginUtils.class); + SliPluginUtils.checkParameters(parametersMap, requiredParams, Log); + } + + @Test(expected = SvcLogicException.class) + public void emptyParametersMap() throws Exception { + Map parametersMap = new HashMap(); + String[] requiredParams = new String[] { "param1", "param2", "param3" }; + Logger Log = LoggerFactory.getLogger(SliPluginUtils.class); + SliPluginUtils.checkParameters(parametersMap, requiredParams, Log); + } + + @Test(expected = SvcLogicException.class) + public void paramNotFound() throws Exception { + Map parametersMap = new HashMap(); + parametersMap.put("tst", "me"); + String[] requiredParams = new String[] { "param1", "parm2", "param3" }; + Logger Log = LoggerFactory.getLogger(SliPluginUtils.class); + SliPluginUtils.checkParameters(parametersMap, requiredParams, Log); + } + + @Test + public void testSunnyRequiredParameters() throws Exception { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("param1", "hello"); + ctx.setAttribute("param2", "world"); + ctx.setAttribute("param3", "!"); + + Map parameters = new HashMap(); + parameters.put("param1", "dog"); + parameters.put("param2", "cat"); + parameters.put("param3", "fish"); + + SliPluginUtils.requiredParameters(parameters, ctx); + } + + @Test + public void testSunnyRequiredParametersWithPrefix() throws Exception { + String prefixValue = "my.unique.path."; + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute(prefixValue + "param1", "hello"); + ctx.setAttribute(prefixValue + "param2", "world"); + ctx.setAttribute(prefixValue + "param3", "!"); + + Map parameters = new HashMap(); + parameters.put("prefix", prefixValue); + parameters.put("param1", "dog"); + parameters.put("param2", "cat"); + parameters.put("param3", "fish"); + + SliPluginUtils.requiredParameters(parameters, ctx); + } + + @Test(expected = SvcLogicException.class) + public void testRainyMissingRequiredParameters() throws Exception { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("param1", "hello"); + ctx.setAttribute("param3", "!"); + + Map parameters = new HashMap(); + parameters.put("param1", null); + parameters.put("param2", null); + parameters.put("param3", null); + + SliPluginUtils.requiredParameters(parameters, ctx); + } + + @Test(expected = SvcLogicException.class) + public void testEmptyRequiredParameters() throws Exception { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("param1", "hello"); + ctx.setAttribute("param3", "!"); + + Map parameters = new HashMap(); + + SliPluginUtils.requiredParameters(parameters, ctx); + } +} diff --git a/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtilsTest.java b/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtilsTest.java index d8d78a0b1..3a6c31a3c 100644 --- a/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtilsTest.java +++ b/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliStringUtilsTest.java @@ -271,6 +271,17 @@ public class SliStringUtilsTest { assertEquals("102%2FGE100%2FSNJSCAMCJP8%2FSNJSCAMCJT4", ctx.getAttribute(outputPath)); } + @Test + public void urlDecode() throws SvcLogicException { + String sourceString = "102%2FGE100%2FSNJSCAMCJP8%2FSNJSCAMCJT4"; + String outputPath = "out"; + + param.put("source", sourceString); + param.put("outputPath", outputPath); + SliStringUtils.urlDecode(param, ctx); + assertEquals("102/GE100/SNJSCAMCJP8/SNJSCAMCJT4", ctx.getAttribute(outputPath)); + } + @Test public void testXmlEscapeText() { param.put("source", "102/GE100/SNJSCAMCJP8/SNJSCAMCJT4"); @@ -317,8 +328,6 @@ public class SliStringUtilsTest { @Test public void isEmpty() throws Exception { - ctx = new SvcLogicContext(); - param = new HashMap<>(); String result = SliStringUtils.isEmpty(param, ctx); param.put(SliStringUtils.INPUT_PARAM_KEY, "key_does_not_exist"); assertEquals(SliStringUtils.TRUE_CONSTANT, result); @@ -339,8 +348,6 @@ public class SliStringUtilsTest { @Test public void isBlank() throws Exception { - ctx = new SvcLogicContext(); - param = new HashMap<>(); String result = SliStringUtils.isBlank(param, ctx); param.put(SliStringUtils.INPUT_PARAM_KEY, "key_does_not_exist"); assertEquals(SliStringUtils.TRUE_CONSTANT, result); @@ -361,8 +368,6 @@ public class SliStringUtilsTest { @Test public void isNull() throws Exception { - ctx = new SvcLogicContext(); - param = new HashMap<>(); String result = SliStringUtils.isNull(param, ctx); param.put(SliStringUtils.INPUT_PARAM_KEY, "key_does_not_exist"); assertEquals(SliStringUtils.TRUE_CONSTANT, result); diff --git a/sliPluginUtils/provider/src/test/resources/2dArray.json b/sliPluginUtils/provider/src/test/resources/2dArray.json new file mode 100644 index 000000000..b473864d8 --- /dev/null +++ b/sliPluginUtils/provider/src/test/resources/2dArray.json @@ -0,0 +1,4 @@ +[ + ["apple", "orange", "banana"], + ["squash", "broccoli", "cauliflower"] +] \ No newline at end of file diff --git a/sliPluginUtils/provider/src/test/resources/3dArray.json b/sliPluginUtils/provider/src/test/resources/3dArray.json new file mode 100644 index 000000000..149955596 --- /dev/null +++ b/sliPluginUtils/provider/src/test/resources/3dArray.json @@ -0,0 +1,4 @@ +[ + [["a","b","c"], ["d","e","f"]], + [["x","y","z"]] +] \ No newline at end of file diff --git a/sliPluginUtils/provider/src/test/resources/ArrayMenu.json b/sliPluginUtils/provider/src/test/resources/ArrayMenu.json new file mode 100644 index 000000000..b12f16315 --- /dev/null +++ b/sliPluginUtils/provider/src/test/resources/ArrayMenu.json @@ -0,0 +1,41 @@ +[{ + "id": "1", + "type": "pizza", + "name": "plain", + "calories": 1000, + "vegetarian": true + }, { + "id": "2", + "type": "pizza", + "name": "Tuesday Special", + "calories": 2000, + "vegetarian": false, + "topping": + [{ + "id": "1", + "name": "onion" + }, { + "id": "2", + "name": "pepperoni" + } + ] + }, { + "id": "3", + "type": "pizza", + "name": "House Special", + "calories": 1500, + "vegetarian": true, + "topping": + [{ + "id": "3", + "name": "basil" + }, { + "id": "4", + "name": "fresh mozzarella" + }, { + "id": "5", + "name": "tomato" + } + ] + } +] diff --git a/sliPluginUtils/provider/src/test/resources/EmbeddedEscapedJson.json b/sliPluginUtils/provider/src/test/resources/EmbeddedEscapedJson.json new file mode 100644 index 000000000..dbb6d8d3a --- /dev/null +++ b/sliPluginUtils/provider/src/test/resources/EmbeddedEscapedJson.json @@ -0,0 +1,16 @@ +{ + "input": { + "parameters": + [{ + "name": "escapedJsonObject", + "value": "[{\"id\":\"0.2.0.0\/16\"},{\"id\":\"ge04::\/64\"}]" + }, { + "name": "password", + "value": "Hello\/World" + }, { + "name": "resourceName", + "value": "The\t\"Best\"\tName" + } + ] + } +} \ No newline at end of file diff --git a/sliPluginUtils/provider/src/test/resources/EscapedJson.json b/sliPluginUtils/provider/src/test/resources/EscapedJson.json new file mode 100644 index 000000000..a7719e819 --- /dev/null +++ b/sliPluginUtils/provider/src/test/resources/EscapedJson.json @@ -0,0 +1 @@ +{\"widget\":{\"debug\":false,\"window\":{\"title\":\"ONAP Widget\",\"name\":\"main_window\",\"width\":200,\"height\":300},\"image\":{\"src\":\"images\/moon.png\",\"name\":\"moon\",\"hOffset\":150,\"vOffset\":150,\"alignment\":\"center\"},\"text\":{\"data\":\"Click Me\",\"size\":21,\"style\":\"bold\",\"name\":\"text1\",\"hOffset\":350,\"vOffset\":200,\"alignment\":\"center\"}}} \ No newline at end of file diff --git a/sliPluginUtils/provider/src/test/resources/ObjectMenu.json b/sliPluginUtils/provider/src/test/resources/ObjectMenu.json new file mode 100644 index 000000000..56f842d48 --- /dev/null +++ b/sliPluginUtils/provider/src/test/resources/ObjectMenu.json @@ -0,0 +1,43 @@ +{ + "menu": [{ + "id": "1", + "type": "pizza", + "name": "plain", + "calories": 1000, + "vegetarian": true + }, { + "id": "2", + "type": "pizza", + "name": "Tuesday Special", + "calories": 2000, + "vegetarian": false, + "topping": + [{ + "id": "1", + "name": "onion" + }, { + "id": "2", + "name": "pepperoni" + } + ] + }, { + "id": "3", + "type": "pizza", + "name": "House Special", + "calories": 1500, + "vegetarian": true, + "topping": + [{ + "id": "3", + "name": "basil" + }, { + "id": "4", + "name": "fresh mozzarella" + }, { + "id": "5", + "name": "tomato" + } + ] + } + ] +} diff --git a/sliPluginUtils/provider/src/test/resources/Widget.json b/sliPluginUtils/provider/src/test/resources/Widget.json new file mode 100644 index 000000000..1e25282c2 --- /dev/null +++ b/sliPluginUtils/provider/src/test/resources/Widget.json @@ -0,0 +1,27 @@ +{ + "widget": { + "debug": false, + "window": { + "title": "ONAP Widget", + "name": "main_window", + "width": 200, + "height": 300 + }, + "image": { + "src": "images/moon.png", + "name": "moon", + "hOffset": 150, + "vOffset": 150, + "alignment": "center" + }, + "text": { + "data": "Click Me", + "size": 21, + "style": "bold", + "name": "text1", + "hOffset": 350, + "vOffset": 200, + "alignment": "center" + } + } +} \ No newline at end of file -- cgit 1.2.3-korg From 3a325d8192addf27423494d8ed8b621dedcf7888 Mon Sep 17 00:00:00 2001 From: Enbo Wang Date: Fri, 21 Feb 2020 14:57:01 +0800 Subject: Support updating JSON object string for sliPluginUtils Issue-ID: CCSDK-2111 Signed-off-by: Enbo Wang Change-Id: Ide6bb06a05fcbb3b88e73cc0fbec6fd46bd29033 --- .../sli/core/slipluginutils/SliPluginUtils.java | 55 ++++++++++++++++++++++ .../SliPluginUtils_StaticFunctionsTest.java | 37 +++++++++++++++ .../provider/src/test/resources/JsonObject.json | 5 ++ 3 files changed, 97 insertions(+) create mode 100644 sliPluginUtils/provider/src/test/resources/JsonObject.json (limited to 'sliPluginUtils/provider/src/test/resources') diff --git a/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java b/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java index 0d9ab217e..ce0f50800 100644 --- a/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java +++ b/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java @@ -33,12 +33,15 @@ import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; import java.util.Properties; import java.util.Set; import java.util.UUID; +import java.util.stream.Collectors; + import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.StringEscapeUtils; import org.onap.ccsdk.sli.core.sli.SvcLogicConstants; @@ -865,6 +868,58 @@ public class SliPluginUtils implements SvcLogicJavaPlugin { } } + /** + * updateJsonObjectString takes a json object string, and adds or deletes the properties of it + * @param parameters - requires source, outputPath and keys to be added or deleted. + * The key of parameter starts with "add.", e.g. "add.A", and then "A" and its value will be added + * to the JSON object. + * The key of parameter starts with "delete.", e.g. "delete.B", and then "B" will be deleted from + * the JSON object. + * @param ctx Reference to context memory + * @throws SvcLogicException if a required parameter is missing an exception is thrown + */ + public static void updateJsonObjectString(Map parameters, SvcLogicContext ctx) throws SvcLogicException { + checkParameters(parameters, new String[] {"source", "outputPath"}, LOG); + try { + String source = ctx.getAttribute(parameters.get("source")); + JsonParser jp = new JsonParser(); + JsonElement element = jp.parse(source); + if (element.isJsonObject()) { + JsonObject jsonObject = element.getAsJsonObject(); + updateJsonObject(jsonObject, parameters); + + String target = jsonObject.toString(); + ctx.setAttribute(parameters.get("outputPath"), target); + } else { + throw new SvcLogicException("just update JSON object string"); + } + } catch (Exception ex) { + throw new SvcLogicException("problem with updateJsonObjectString", ex); + } + } + + protected static void updateJsonObject(JsonObject jsonObject, Map parameters) throws SvcLogicException { + List deleted_params = parameters.keySet().stream().filter(param -> param.startsWith("delete.")). + collect(Collectors.toList()); + for (String param: deleted_params) { + String[] action_key = param.split("\\.", 2); + if (action_key.length < 2) { + throw new SvcLogicException("error parameter format: " + param + ", must be \"delete.\""); + } + jsonObject.remove(action_key[1]); + } + + List added_params = parameters.keySet().stream().filter(param -> param.startsWith("add.")). + collect(Collectors.toList()); + for (String param: added_params) { + String[] action_key = param.split("\\.", 2); + if (action_key.length < 2) { + throw new SvcLogicException("error parameter format: " + param + ", must be \"add.\""); + } + jsonObject.addProperty(action_key[1], parameters.get(param)); + } + } + /** * getAttributeValue takes a ctx memory path as a string, gets the value stored at this path and set this value in context memory at * outputPath diff --git a/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java b/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java index 5c222c83f..ad039d7f4 100644 --- a/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java +++ b/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java @@ -495,6 +495,43 @@ public class SliPluginUtils_StaticFunctionsTest { assertEquals("200", ctx.getAttribute("testPath.widget.window.width")); } + @Test + public void testUpdateJsonObjectString() throws Exception { + String path = "src/test/resources/JsonObject.json"; + String content = new String(Files.readAllBytes(Paths.get(path))); + + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("input", content); + + Map parametersUpdateJson = new HashMap(); + parametersUpdateJson.put("source", "input"); + parametersUpdateJson.put("outputPath", "newJsonString"); + + // add key "ccc" and its value + parametersUpdateJson.put("add.ccc", "abcxyz"); + + // update keys and their values of "aaa" and "c.d" + parametersUpdateJson.put("add.aaa", "4567"); + parametersUpdateJson.put("add.c.d", "defg"); + + // delete key "bbb" + parametersUpdateJson.put("delete.bbb", ""); + + SliPluginUtils.updateJsonObjectString(parametersUpdateJson, ctx); + + Map parametersJsonToCtx = new HashMap(); + parametersJsonToCtx.put("source", "newJsonString"); + parametersJsonToCtx.put("outputPath", "testPath"); + parametersJsonToCtx.put("isEscaped", "false"); + + SliPluginUtils.jsonStringToCtx(parametersJsonToCtx, ctx); + + assertEquals("abcxyz", ctx.getAttribute("testPath.ccc")); + assertEquals("4567", ctx.getAttribute("testPath.aaa")); + assertEquals("defg", ctx.getAttribute("testPath.c.d")); + assertEquals(null, ctx.getAttribute("testPath.bbb")); + } + @Test public void testEmbeddedEscapedJsonJsonStringToCtx() throws Exception { String path = "src/test/resources/EmbeddedEscapedJson.json"; diff --git a/sliPluginUtils/provider/src/test/resources/JsonObject.json b/sliPluginUtils/provider/src/test/resources/JsonObject.json new file mode 100644 index 000000000..0578368f8 --- /dev/null +++ b/sliPluginUtils/provider/src/test/resources/JsonObject.json @@ -0,0 +1,5 @@ +{ + "aaa": "123", + "bbb": "xyz", + "c.d": "abc" +} \ No newline at end of file -- cgit 1.2.3-korg From b970a7b46fac32e626acfa23b1e2de9b22da7ff7 Mon Sep 17 00:00:00 2001 From: "Smokowski, Kevin (ks6305)" Date: Wed, 1 Apr 2020 19:58:23 +0000 Subject: SliPluginUtils jsonStringToCtx fix Support null values in jsonStringToCtx in Slipluginutils Issue-ID: CCSDK-2250 Signed-off-by: Smokowski, Kevin (ks6305) Change-Id: I3316fa9e80c9e684fc27d83356bbfad9b1151992 --- .../org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java | 9 ++++++++- .../core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java | 7 ++++--- sliPluginUtils/provider/src/test/resources/2dArray.json | 2 +- sliPluginUtils/provider/src/test/resources/ArrayMenu.json | 2 +- sliPluginUtils/provider/src/test/resources/Widget.json | 2 +- 5 files changed, 15 insertions(+), 7 deletions(-) (limited to 'sliPluginUtils/provider/src/test/resources') diff --git a/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java b/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java index ce0f50800..60bb4fd31 100644 --- a/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java +++ b/sliPluginUtils/provider/src/main/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils.java @@ -70,6 +70,7 @@ public class SliPluginUtils implements SvcLogicJavaPlugin { private static final String LOG_MSG="extracting list from context memory"; private static final String LOG_MSG1="removing elements from list"; private static final String LENGTH="_length"; + public static final String CTX_NULL_VALUE=""; // ========== CONSTRUCTORS ========== @@ -846,7 +847,11 @@ public class SliPluginUtils implements SvcLogicJavaPlugin { if(!root.endsWith(".")){ root = root + "."; } - ctx.setAttribute(root + key, entry.getValue().getAsString()); + if(entry.getValue().isJsonNull()) { + ctx.setAttribute(root + key, CTX_NULL_VALUE); + }else { + ctx.setAttribute(root + key, entry.getValue().getAsString()); + } } } } @@ -861,6 +866,8 @@ public class SliPluginUtils implements SvcLogicJavaPlugin { handleJsonArray(key, element.getAsJsonArray(), ctx, prefix); } else if (element.isJsonObject()) { writeJsonObject(element.getAsJsonObject(), ctx, prefix + "."); + } else if (element.isJsonNull()) { + ctx.setAttribute(prefix, CTX_NULL_VALUE); } else if (element.isJsonPrimitive()) { ctx.setAttribute(prefix, element.getAsString()); } diff --git a/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java b/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java index ad039d7f4..bc2c9226d 100644 --- a/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java +++ b/sliPluginUtils/provider/src/test/java/org/onap/ccsdk/sli/core/slipluginutils/SliPluginUtils_StaticFunctionsTest.java @@ -335,7 +335,7 @@ public class SliPluginUtils_StaticFunctionsTest { assertEquals("plain", ctx.getAttribute("testPath.[0].name")); assertEquals("pizza", ctx.getAttribute("testPath.[0].type")); assertEquals("true", ctx.getAttribute("testPath.[0].vegetarian")); - assertEquals("2000", ctx.getAttribute("testPath.[1].calories")); + assertEquals(SliPluginUtils.CTX_NULL_VALUE, ctx.getAttribute("testPath.[1].calories")); assertEquals("2", ctx.getAttribute("testPath.[1].id")); assertEquals("Tuesday Special", ctx.getAttribute("testPath.[1].name")); assertEquals("1", ctx.getAttribute("testPath.[1].topping[0].id")); @@ -423,7 +423,8 @@ public class SliPluginUtils_StaticFunctionsTest { assertEquals("apple", ctx.getAttribute("testPath.[0][0]")); assertEquals("orange", ctx.getAttribute("testPath.[0][1]")); assertEquals("banana", ctx.getAttribute("testPath.[0][2]")); - assertEquals("3", ctx.getAttribute("testPath.[0]_length")); + assertEquals(SliPluginUtils.CTX_NULL_VALUE, ctx.getAttribute("testPath.[0][3]")); + assertEquals("4", ctx.getAttribute("testPath.[0]_length")); assertEquals("squash", ctx.getAttribute("testPath.[1][0]")); assertEquals("broccoli", ctx.getAttribute("testPath.[1][1]")); assertEquals("cauliflower", ctx.getAttribute("testPath.[1][2]")); @@ -488,7 +489,7 @@ public class SliPluginUtils_StaticFunctionsTest { assertEquals("text1", ctx.getAttribute("testPath.widget.text.name")); assertEquals("21", ctx.getAttribute("testPath.widget.text.size")); assertEquals("bold", ctx.getAttribute("testPath.widget.text.style")); - assertEquals("200", ctx.getAttribute("testPath.widget.text.vOffset")); + assertEquals(SliPluginUtils.CTX_NULL_VALUE, ctx.getAttribute("testPath.widget.text.vOffset")); assertEquals("300", ctx.getAttribute("testPath.widget.window.height")); assertEquals("main_window", ctx.getAttribute("testPath.widget.window.name")); assertEquals("ONAP Widget", ctx.getAttribute("testPath.widget.window.title")); diff --git a/sliPluginUtils/provider/src/test/resources/2dArray.json b/sliPluginUtils/provider/src/test/resources/2dArray.json index b473864d8..2a94b46f4 100644 --- a/sliPluginUtils/provider/src/test/resources/2dArray.json +++ b/sliPluginUtils/provider/src/test/resources/2dArray.json @@ -1,4 +1,4 @@ [ - ["apple", "orange", "banana"], + ["apple", "orange", "banana", null], ["squash", "broccoli", "cauliflower"] ] \ No newline at end of file diff --git a/sliPluginUtils/provider/src/test/resources/ArrayMenu.json b/sliPluginUtils/provider/src/test/resources/ArrayMenu.json index b12f16315..26a24f292 100644 --- a/sliPluginUtils/provider/src/test/resources/ArrayMenu.json +++ b/sliPluginUtils/provider/src/test/resources/ArrayMenu.json @@ -8,7 +8,7 @@ "id": "2", "type": "pizza", "name": "Tuesday Special", - "calories": 2000, + "calories": null, "vegetarian": false, "topping": [{ diff --git a/sliPluginUtils/provider/src/test/resources/Widget.json b/sliPluginUtils/provider/src/test/resources/Widget.json index 1e25282c2..6b90907ce 100644 --- a/sliPluginUtils/provider/src/test/resources/Widget.json +++ b/sliPluginUtils/provider/src/test/resources/Widget.json @@ -20,7 +20,7 @@ "style": "bold", "name": "text1", "hOffset": 350, - "vOffset": 200, + "vOffset": null, "alignment": "center" } } -- cgit 1.2.3-korg