From 67414f17e30d04ad278fe7ab73c0bed8c44c0523 Mon Sep 17 00:00:00 2001 From: "Timoney, Dan (dt5972)" Date: Fri, 16 Mar 2018 13:58:47 -0400 Subject: Add test cases for sli Added test cases for sli in sli/core Change-Id: Ic8614f199ae843a46ec3441f0ae247f065b33322 Issue-ID: CCSDK-213 Signed-off-by: Timoney, Dan (dt5972) --- .../core/sli/provider/SvcLogicAdaptorFactory.java | 46 ++---- .../core/sli/provider/SvcLogicClassResolver.java | 41 +++++ .../core/sli/provider/SvcLogicNodeExecutor.java | 48 ++---- .../ccsdk/sli/core/sli/provider/DummyAdaptor.java | 42 +++++ .../ccsdk/sli/core/sli/provider/DummyRecorder.java | 14 ++ .../ccsdk/sli/core/sli/provider/DummyResource.java | 97 ++++++++++++ .../sli/provider/ITCaseSvcLogicGraphExecutor.java | 8 +- sli/provider/src/test/resources/executor.tests | 6 +- .../src/test/resources/l3sdn_logic_v10.xml | 172 ++++++++++++++++----- .../sli/core/sli/recording/TestFileRecorder.java | 35 +++++ .../sli/core/sli/recording/TestSlf4jRecorder.java | 33 ++++ 11 files changed, 429 insertions(+), 113 deletions(-) create mode 100644 sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicClassResolver.java create mode 100644 sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/DummyAdaptor.java create mode 100644 sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/DummyRecorder.java create mode 100644 sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/DummyResource.java create mode 100644 sli/recording/src/test/java/org/onap/ccsdk/sli/core/sli/recording/TestFileRecorder.java create mode 100644 sli/recording/src/test/java/org/onap/ccsdk/sli/core/sli/recording/TestSlf4jRecorder.java diff --git a/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicAdaptorFactory.java b/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicAdaptorFactory.java index 7316db57..ecfe6b69 100644 --- a/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicAdaptorFactory.java +++ b/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicAdaptorFactory.java @@ -8,9 +8,9 @@ * 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. @@ -35,7 +35,7 @@ public class SvcLogicAdaptorFactory { private static final Logger LOG = LoggerFactory .getLogger(SvcLogicAdaptorFactory.class); - private static HashMap adaptorMap = new HashMap(); + private static HashMap adaptorMap = new HashMap<>(); public static void registerAdaptor(SvcLogicAdaptor adaptor) { String name = adaptor.getClass().getName(); @@ -51,36 +51,18 @@ public class SvcLogicAdaptorFactory { } } - public static SvcLogicAdaptor getInstance(String name) { - if (adaptorMap.containsKey(name)) { - return (adaptorMap.get(name)); - } else { - BundleContext bctx = null; - try - { - bctx = FrameworkUtil.getBundle(SvcLogicAdaptorFactory.class) - .getBundleContext(); - } - catch (Exception e) - { - LOG.debug("Caught exception trying to locate device adaptor "+name, e); - return(null); - } - - ServiceReference sref = bctx.getServiceReference(name); + public static SvcLogicAdaptor getInstance(String name) { + if (adaptorMap.containsKey(name)) { + return adaptorMap.get(name); + } else { - if (sref != null) { - SvcLogicAdaptor adaptor = (SvcLogicAdaptor) bctx - .getService(sref); + SvcLogicAdaptor adaptor = (SvcLogicAdaptor) SvcLogicClassResolver.resolve(name); - if (adaptor != null) { - registerAdaptor(adaptor); + if (adaptor != null) { + registerAdaptor(adaptor); + } - return (adaptor); - } - return (null); - } - } - return(null); - } + return adaptor; + } + } } diff --git a/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicClassResolver.java b/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicClassResolver.java new file mode 100644 index 00000000..d2b733fc --- /dev/null +++ b/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicClassResolver.java @@ -0,0 +1,41 @@ +package org.onap.ccsdk.sli.core.sli.provider; + +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; +import org.osgi.framework.ServiceReference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SvcLogicClassResolver { + private static final Logger LOG = LoggerFactory.getLogger(SvcLogicClassResolver.class); + + public static Object resolve(String className) { + + Bundle bundle = FrameworkUtil.getBundle(SvcLogicClassResolver.class); + + if (bundle == null) { + // Running outside OSGi container (e.g. jUnit). Use Reflection + // to resolve class + try { + return(Class.forName(className).newInstance()); + } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { + + LOG.error("Could not resolve class "+className, e); + return null; + } + + } else { + BundleContext bctx = bundle.getBundleContext(); + ServiceReference sref = bctx.getServiceReference(className); + if (sref != null) { + return bctx.getService(sref); + } else { + + LOG.warn("Could not find service reference object for class " + className); + return null; + } + } + } + +} diff --git a/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicNodeExecutor.java b/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicNodeExecutor.java index 593c972a..f34c261d 100644 --- a/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicNodeExecutor.java +++ b/sli/provider/src/main/java/org/onap/ccsdk/sli/core/sli/provider/SvcLogicNodeExecutor.java @@ -29,6 +29,7 @@ import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin; import org.onap.ccsdk.sli.core.sli.SvcLogicNode; import org.onap.ccsdk.sli.core.sli.SvcLogicRecorder; import org.onap.ccsdk.sli.core.sli.SvcLogicResource; +import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceReference; @@ -53,56 +54,25 @@ public abstract class SvcLogicNodeExecutor { } + protected SvcLogicAdaptor getAdaptor(String adaptorName) { return SvcLogicAdaptorFactory.getInstance(adaptorName); } protected SvcLogicResource getSvcLogicResource(String plugin) { - BundleContext bctx = FrameworkUtil.getBundle(this.getClass()) - .getBundleContext(); - - ServiceReference sref = bctx.getServiceReference(plugin); - if (sref != null) { - SvcLogicResource resourcePlugin = (SvcLogicResource) bctx - .getService(sref); - return resourcePlugin; - } - else { - LOG.warn("Could not find service reference object for plugin " + plugin); - return null; - } + + return((SvcLogicResource) SvcLogicClassResolver.resolve(plugin)); } protected SvcLogicRecorder getSvcLogicRecorder(String plugin) { - BundleContext bctx = FrameworkUtil.getBundle(this.getClass()) - .getBundleContext(); - - ServiceReference sref = bctx.getServiceReference(plugin); - if (sref != null) { - SvcLogicRecorder resourcePlugin = (SvcLogicRecorder) bctx - .getService(sref); - return resourcePlugin; - } - else { - return null; - } + return((SvcLogicRecorder) SvcLogicClassResolver.resolve(plugin)); } protected SvcLogicJavaPlugin getSvcLogicJavaPlugin(String pluginName){ - BundleContext bctx = FrameworkUtil.getBundle(this.getClass()) - .getBundleContext(); - - ServiceReference sref = bctx.getServiceReference(pluginName); - - if (sref == null) { - LOG.warn("Could not find service reference object for plugin " + pluginName); - return null; - } else { - SvcLogicJavaPlugin plugin = (SvcLogicJavaPlugin) bctx - .getService(sref); - return plugin; - } - } + return((SvcLogicJavaPlugin) SvcLogicClassResolver.resolve(pluginName)); + + } + protected SvcLogicNode getNextNode(SvcLogicNode node, String outValue) { MetricLogger.resetContext(); SvcLogicNode nextNode = node.getOutcomeValue(outValue); diff --git a/sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/DummyAdaptor.java b/sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/DummyAdaptor.java new file mode 100644 index 00000000..87765344 --- /dev/null +++ b/sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/DummyAdaptor.java @@ -0,0 +1,42 @@ +/** + * + */ +package org.onap.ccsdk.sli.core.sli.provider; + +import java.util.Map; +import org.onap.ccsdk.sli.core.sli.SvcLogicAdaptor; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; + +/** + * @author dt5972 + * + */ + +public class DummyAdaptor implements SvcLogicAdaptor { + + /* (non-Javadoc) + * @see org.onap.ccsdk.sli.core.sli.SvcLogicAdaptor#configure(java.lang.String, java.util.Map, org.onap.ccsdk.sli.core.sli.SvcLogicContext) + */ + @Override + public ConfigStatus configure(String key, Map parameters, SvcLogicContext ctx) { + + return ConfigStatus.SUCCESS; + } + + /* (non-Javadoc) + * @see org.onap.ccsdk.sli.core.sli.SvcLogicAdaptor#activate(java.lang.String, org.onap.ccsdk.sli.core.sli.SvcLogicContext) + */ + @Override + public ConfigStatus activate(String key, SvcLogicContext ctx) { + return ConfigStatus.SUCCESS; + } + + /* (non-Javadoc) + * @see org.onap.ccsdk.sli.core.sli.SvcLogicAdaptor#deactivate(java.lang.String, org.onap.ccsdk.sli.core.sli.SvcLogicContext) + */ + @Override + public ConfigStatus deactivate(String key, SvcLogicContext ctx) { + return ConfigStatus.SUCCESS; + } + +} diff --git a/sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/DummyRecorder.java b/sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/DummyRecorder.java new file mode 100644 index 00000000..e87f9628 --- /dev/null +++ b/sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/DummyRecorder.java @@ -0,0 +1,14 @@ +package org.onap.ccsdk.sli.core.sli.provider; + +import java.util.Map; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.onap.ccsdk.sli.core.sli.SvcLogicRecorder; + +public class DummyRecorder implements SvcLogicRecorder { + + @Override + public void record(Map parmMap) throws SvcLogicException { + return; + } + +} diff --git a/sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/DummyResource.java b/sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/DummyResource.java new file mode 100644 index 00000000..2f784186 --- /dev/null +++ b/sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/DummyResource.java @@ -0,0 +1,97 @@ +/** + * + */ +package org.onap.ccsdk.sli.core.sli.provider; + +import java.util.Map; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.onap.ccsdk.sli.core.sli.SvcLogicResource; + +/** + * @author dt5972 + * + */ +public class DummyResource implements SvcLogicResource { + + /* (non-Javadoc) + * @see org.onap.ccsdk.sli.core.sli.SvcLogicResource#isAvailable(java.lang.String, java.lang.String, java.lang.String, org.onap.ccsdk.sli.core.sli.SvcLogicContext) + */ + @Override + public QueryStatus isAvailable(String resource, String key, String prefix, SvcLogicContext ctx) + throws SvcLogicException { + return QueryStatus.SUCCESS; + } + + /* (non-Javadoc) + * @see org.onap.ccsdk.sli.core.sli.SvcLogicResource#exists(java.lang.String, java.lang.String, java.lang.String, org.onap.ccsdk.sli.core.sli.SvcLogicContext) + */ + @Override + public QueryStatus exists(String resource, String key, String prefix, SvcLogicContext ctx) + throws SvcLogicException { + return QueryStatus.SUCCESS; + } + + /* (non-Javadoc) + * @see org.onap.ccsdk.sli.core.sli.SvcLogicResource#query(java.lang.String, boolean, java.lang.String, java.lang.String, java.lang.String, java.lang.String, org.onap.ccsdk.sli.core.sli.SvcLogicContext) + */ + @Override + public QueryStatus query(String resource, boolean localOnly, String select, String key, String prefix, + String orderBy, SvcLogicContext ctx) throws SvcLogicException { + return QueryStatus.SUCCESS; + } + + /* (non-Javadoc) + * @see org.onap.ccsdk.sli.core.sli.SvcLogicResource#reserve(java.lang.String, java.lang.String, java.lang.String, java.lang.String, org.onap.ccsdk.sli.core.sli.SvcLogicContext) + */ + @Override + public QueryStatus reserve(String resource, String select, String key, String prefix, SvcLogicContext ctx) + throws SvcLogicException { + return QueryStatus.SUCCESS; + } + + /* (non-Javadoc) + * @see org.onap.ccsdk.sli.core.sli.SvcLogicResource#save(java.lang.String, boolean, boolean, java.lang.String, java.util.Map, java.lang.String, org.onap.ccsdk.sli.core.sli.SvcLogicContext) + */ + @Override + public QueryStatus save(String resource, boolean force, boolean localOnly, String key, Map parms, + String prefix, SvcLogicContext ctx) throws SvcLogicException { + return QueryStatus.SUCCESS; + } + + /* (non-Javadoc) + * @see org.onap.ccsdk.sli.core.sli.SvcLogicResource#release(java.lang.String, java.lang.String, org.onap.ccsdk.sli.core.sli.SvcLogicContext) + */ + @Override + public QueryStatus release(String resource, String key, SvcLogicContext ctx) throws SvcLogicException { + + return QueryStatus.SUCCESS; + } + + /* (non-Javadoc) + * @see org.onap.ccsdk.sli.core.sli.SvcLogicResource#delete(java.lang.String, java.lang.String, org.onap.ccsdk.sli.core.sli.SvcLogicContext) + */ + @Override + public QueryStatus delete(String resource, String key, SvcLogicContext ctx) throws SvcLogicException { + return QueryStatus.SUCCESS; + } + + /* (non-Javadoc) + * @see org.onap.ccsdk.sli.core.sli.SvcLogicResource#notify(java.lang.String, java.lang.String, java.lang.String, org.onap.ccsdk.sli.core.sli.SvcLogicContext) + */ + @Override + public QueryStatus notify(String resource, String action, String key, SvcLogicContext ctx) + throws SvcLogicException { + return QueryStatus.SUCCESS; + } + + /* (non-Javadoc) + * @see org.onap.ccsdk.sli.core.sli.SvcLogicResource#update(java.lang.String, java.lang.String, java.util.Map, java.lang.String, org.onap.ccsdk.sli.core.sli.SvcLogicContext) + */ + @Override + public QueryStatus update(String resource, String key, Map parms, String prefix, + SvcLogicContext ctx) throws SvcLogicException { + return QueryStatus.SUCCESS; + } + +} diff --git a/sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/ITCaseSvcLogicGraphExecutor.java b/sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/ITCaseSvcLogicGraphExecutor.java index 5e627cdb..4c112ab2 100644 --- a/sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/ITCaseSvcLogicGraphExecutor.java +++ b/sli/provider/src/test/java/org/onap/ccsdk/sli/core/sli/provider/ITCaseSvcLogicGraphExecutor.java @@ -45,6 +45,9 @@ import org.onap.ccsdk.sli.core.sli.SvcLogicGraph; import org.onap.ccsdk.sli.core.sli.SvcLogicParser; import org.onap.ccsdk.sli.core.sli.SvcLogicStore; import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory; +import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; +import org.osgi.framework.ServiceRegistration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -101,7 +104,6 @@ public class ITCaseSvcLogicGraphExecutor { SvcLogicParser parser = new SvcLogicParser(); - // Loop through executor tests SvcLogicPropertiesProvider resourceProvider = new SvcLogicPropertiesProviderImpl(); SvcLogicServiceImpl svc = new SvcLogicServiceImpl(resourceProvider); @@ -109,6 +111,10 @@ public class ITCaseSvcLogicGraphExecutor { LOG.info("SLI - registering node executor for node type " + nodeType); svc.registerExecutor(nodeType, BUILTIN_NODES.get(nodeType)); } + + + + } @AfterClass diff --git a/sli/provider/src/test/resources/executor.tests b/sli/provider/src/test/resources/executor.tests index e7547e63..e4e60208 100755 --- a/sli/provider/src/test/resources/executor.tests +++ b/sli/provider/src/test/resources/executor.tests @@ -1,2 +1,6 @@ l3sdn_logic_v10.xml:switchTester:test-value="" -l3sdn_logic_v10.xml:switchTester:test-value="hi" \ No newline at end of file +l3sdn_logic_v10.xml:switchTester:test-value="hi" +l3sdn_logic_v10.xml:forRecordTester +l3sdn_logic_v10.xml:resourceTester +l3sdn_logic_v10.xml:configureTester +l3sdn_logic_v10.xml:javaPluginTester \ No newline at end of file diff --git a/sli/provider/src/test/resources/l3sdn_logic_v10.xml b/sli/provider/src/test/resources/l3sdn_logic_v10.xml index 127c181f..c1123d13 100644 --- a/sli/provider/src/test/resources/l3sdn_logic_v10.xml +++ b/sli/provider/src/test/resources/l3sdn_logic_v10.xml @@ -1,22 +1,14 @@ - + - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - - - - - - + + + + + + + + - + diff --git a/sli/recording/src/test/java/org/onap/ccsdk/sli/core/sli/recording/TestFileRecorder.java b/sli/recording/src/test/java/org/onap/ccsdk/sli/core/sli/recording/TestFileRecorder.java new file mode 100644 index 00000000..c879d8f6 --- /dev/null +++ b/sli/recording/src/test/java/org/onap/ccsdk/sli/core/sli/recording/TestFileRecorder.java @@ -0,0 +1,35 @@ +/** + * + */ +package org.onap.ccsdk.sli.core.sli.recording; + +import static org.junit.Assert.*; +import java.util.HashMap; +import org.junit.Test; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; + +/** + * @author dt5972 + * + */ +public class TestFileRecorder { + + /** + * Test method for {@link org.onap.ccsdk.sli.core.sli.recording.FileRecorder#record(java.util.Map)}. + */ + @Test + public void testRecord() { + + FileRecorder recorder = new FileRecorder(); + + HashMap parms = new HashMap<>(); + parms.put("file", "/dev/null"); + parms.put("field1","hi"); + try { + recorder.record(parms); + } catch (SvcLogicException e) { + fail("Caught SvcLogicException : "+e.getMessage()); + } + } + +} diff --git a/sli/recording/src/test/java/org/onap/ccsdk/sli/core/sli/recording/TestSlf4jRecorder.java b/sli/recording/src/test/java/org/onap/ccsdk/sli/core/sli/recording/TestSlf4jRecorder.java new file mode 100644 index 00000000..c696f25b --- /dev/null +++ b/sli/recording/src/test/java/org/onap/ccsdk/sli/core/sli/recording/TestSlf4jRecorder.java @@ -0,0 +1,33 @@ +/** + * + */ +package org.onap.ccsdk.sli.core.sli.recording; + +import static org.junit.Assert.*; +import java.util.HashMap; +import org.junit.Test; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; + +/** + * @author dt5972 + * + */ +public class TestSlf4jRecorder { + + /** + * Test method for {@link org.onap.ccsdk.sli.core.sli.recording.Slf4jRecorder#record(java.util.Map)}. + */ + @Test + public void testRecord() { + Slf4jRecorder recorder = new Slf4jRecorder(); + + HashMap parms = new HashMap<>(); + parms.put("field1","hi"); + try { + recorder.record(parms); + } catch (SvcLogicException e) { + fail("Caught SvcLogicException : "+e.getMessage()); + } + } + +} -- cgit 1.2.3-korg