diff options
author | Andrew Gauld <agauld@att.com> | 2020-01-23 13:38:51 +0000 |
---|---|---|
committer | Andrew Gauld <agauld@att.com> | 2020-01-23 20:13:04 +0000 |
commit | 6f2d7c7bd510345436f54fd1554280cc731a1dc4 (patch) | |
tree | 41e9e45255cd70ef0c8d03fa8039abec508628a9 | |
parent | 3fb91dc34bcde5489681f6470cae7f01c8b246d0 (diff) |
Improve mod/genprocessor code coverage
Change-Id: I954f6b9d0dafd348ffd0df629fd181c2350e78f0
Issue-ID: DCAEGEN2-1860
Signed-off-by: Andrew Gauld <agauld@att.com>
-rw-r--r-- | mod/genprocessor/pom.xml | 65 | ||||
-rw-r--r-- | mod/genprocessor/src/main/java/org/onap/dcae/genprocessor/App.java | 18 | ||||
-rw-r--r-- | mod/genprocessor/src/test/java/sandbox/AppTest.java | 163 |
3 files changed, 196 insertions, 50 deletions
diff --git a/mod/genprocessor/pom.xml b/mod/genprocessor/pom.xml index 3b916ca..0aed60a 100644 --- a/mod/genprocessor/pom.xml +++ b/mod/genprocessor/pom.xml @@ -1,5 +1,21 @@ <?xml version="1.0" encoding="UTF-8"?> +<!-- +============LICENSE_START======================================================= +Copyright (c) 2020 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========================================================= +--> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> @@ -22,6 +38,7 @@ <start-class>org.onap.dcae.genprocessor.App</start-class> <!--NOTE: Nifi jars used here are version 1.9.2 but dcae mod is on 1.9.3 because 1.9.3 is not in Maven Central --> <nifi.version>1.9.2</nifi.version> + <docker.fabric.version>0.32.0</docker.fabric.version> </properties> <dependencies> <dependency> @@ -80,18 +97,25 @@ <version>4.11</version> <scope>test</scope> </dependency> + <dependency> + <groupId>com.github.stefanbirkner</groupId> + <artifactId>system-rules</artifactId> + <version>1.19.0</version> + <scope>test</scope> + </dependency> </dependencies> <build> <plugins> <plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> + <version>${docker.fabric.version}</version> <configuration> <verbose>true</verbose> <pullRegistry>${docker.pull.registry}</pullRegistry> <pushRegistry>${docker.push.registry}</pushRegistry> <images> - <image> + <image> <name>onap/${project.groupId}.${project.artifactId}-http</name> <registry>${onap.nexus.dockerregistry.daily}</registry> <build> @@ -101,27 +125,27 @@ <tag>${project.version}</tag> <tag>${project.version}-${maven.build.timestamp}Z</tag> </tags> - <assembly> - <targetDir>/</targetDir> - <inline> - <files> - <file> - <source>./nginx.conf</source> - <outputDirectory>/etc/nginx/conf.d</outputDirectory> - <destName>default.conf</destName> - </file> - </files> - </inline> - </assembly> + <assembly> + <targetDir>/</targetDir> + <inline> + <files> + <file> + <source>./nginx.conf</source> + <outputDirectory>/etc/nginx/conf.d</outputDirectory> + <destName>default.conf</destName> + </file> + </files> + </inline> + </assembly> <entryPoint> <exec> - <arg>nginx</arg> - <arg>-g</arg> - <arg>daemon off;</arg> - </exec> - </entryPoint> - </build> - </image> + <arg>nginx</arg> + <arg>-g</arg> + <arg>daemon off;</arg> + </exec> + </entryPoint> + </build> + </image> <image> <name>onap/${project.groupId}.${project.artifactId}-job</name> <registry>${onap.nexus.dockerregistry.daily}</registry> @@ -139,7 +163,6 @@ <env> <GENPROC_WORKING_DIR>/work</GENPROC_WORKING_DIR> <GENPROC_ONBOARDING_API_HOST>http://onboarding-api/onboarding</GENPROC_ONBOARDING_API_HOST> - <GENPROC_PROCESSOR_CLASSFILE_PATH>/code/target/classes/sandbox/DCAEPROCESSOR.class</GENPROC_PROCESSOR_CLASSFILE_PATH> <GENPROC_SLEEP_SEC>10</GENPROC_SLEEP_SEC> </env> <entryPoint> diff --git a/mod/genprocessor/src/main/java/org/onap/dcae/genprocessor/App.java b/mod/genprocessor/src/main/java/org/onap/dcae/genprocessor/App.java index 7cee8b0..9c50991 100644 --- a/mod/genprocessor/src/main/java/org/onap/dcae/genprocessor/App.java +++ b/mod/genprocessor/src/main/java/org/onap/dcae/genprocessor/App.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 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. @@ -23,6 +23,7 @@ import javassist.CtClass; import java.io.File; import java.io.FilenameFilter; +import java.io.InputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URI; @@ -160,13 +161,13 @@ public class App { } } - private static boolean copyProcessorClassFile(File pathClassFile, File dirBuild) { + private static boolean copyProcessorClassFile(String classResourceName, File dirBuild) { File dirSandbox = new File(dirBuild, "org/onap/dcae/genprocessor"); if (dirSandbox.exists() || dirSandbox.mkdir()) { - try { - File dest = new File(dirSandbox, pathClassFile.getName()); - Files.copy(pathClassFile.toPath(), dest.toPath()); + try (InputStream asStream = App.class.getResourceAsStream(classResourceName)) { + File dest = new File(dirSandbox, classResourceName); + Files.copy(asStream, dest.toPath()); return true; } catch (FileAlreadyExistsException e) { // Do nothing, class file already exists @@ -308,7 +309,7 @@ public class App { return false; } - public static void main(String[] args) throws InterruptedException { + public static void main(String[] args) throws InterruptedException, URISyntaxException { if (args.length == 0) { args = new String[] { "gen" }; String sleepstr = System.getenv("GENPROC_SLEEP_SEC"); @@ -328,7 +329,7 @@ public class App { } - public static void main2(String[] args) { + public static void main2(String[] args) throws URISyntaxException { String argsStr = String.join(", ", args); if (argsStr.contains("-h")) { LOG.info("Here are the possible args:"); @@ -343,7 +344,6 @@ public class App { // Config from env variables File dirWorking = new File(System.getenv("GENPROC_WORKING_DIR")); String hostOnboardingAPI = System.getenv("GENPROC_ONBOARDING_API_HOST"); - File processorClassFile = new File(System.getenv("GENPROC_PROCESSOR_CLASSFILE_PATH")); String urlToJarIndex = System.getenv("GENPROC_JAR_INDEX_URL"); String[] paramsToPrint = new String[] { @@ -380,7 +380,7 @@ public class App { writeManifestThing(dirBuild, generateManifestMF(comp.compSpec), "META-INF", "MANIFEST.MF"); writeManifestThing(dirBuild, Arrays.asList(createClassName(comp.compSpec)), "META-INF/services", "org.apache.nifi.processor.Processor"); - copyProcessorClassFile(processorClassFile, dirBuild); + copyProcessorClassFile("DCAEProcessor.class", dirBuild); packageJar(dirWorking, dirBuild, jarName); } } diff --git a/mod/genprocessor/src/test/java/sandbox/AppTest.java b/mod/genprocessor/src/test/java/sandbox/AppTest.java index dddd0fd..d6a1ca3 100644 --- a/mod/genprocessor/src/test/java/sandbox/AppTest.java +++ b/mod/genprocessor/src/test/java/sandbox/AppTest.java @@ -15,41 +15,164 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package sandbox; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; + +import java.net.URISyntaxException; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; + +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.ProcessSession; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.processor.exception.ProcessException; +import org.junit.Rule; import org.junit.Test; +import org.junit.contrib.java.lang.system.EnvironmentVariables; +import org.junit.rules.TemporaryFolder; +import org.onap.dcae.genprocessor.App; import org.onap.dcae.genprocessor.CompSpec; +import org.onap.dcae.genprocessor.DCAEProcessor; +import org.onap.dcae.genprocessor.OnboardingAPIClient; import org.onap.dcae.genprocessor.Utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * Unit test for simple App. */ -public class AppTest -{ - /** - * Rigorous Test :-) - */ +public class AppTest { + static final Logger LOG = LoggerFactory.getLogger(AppTest.class); + + @Rule + public final EnvironmentVariables evars = new EnvironmentVariables(); + + @Rule + public TemporaryFolder tfolder = new TemporaryFolder(); + + @Test - public void shouldAnswerWithTrue() - { - assertTrue( true ); + public void testUtils() { + new Utils(); + assertEquals(Utils.formatNameForJavaClass("part1.a-bee"), "Part1ABee"); + HashMap<String, String> mx = new HashMap<>(); + mx.put("name", "SomeJar"); + mx.put("version", "2.0"); + mx.put("description", "desc"); + CompSpec cs = new CompSpec(); + cs.unpackSelf(mx); + assertEquals(Utils.formatNameForJar(cs), "SomeJar-2.0"); } - /** - * Test Utils - */ + @Test - public void testUtils() - { - new Utils(); - assertEquals(Utils.formatNameForJavaClass("part1.a-bee"), "Part1ABee"); - CompSpec cs = new CompSpec(); - cs.name = "SomeJar"; - cs.version = "2.0"; - assertEquals(Utils.formatNameForJar(cs), "SomeJar-2.0"); + public void testDcaeProcessor() throws ProcessException { + DCAEProcessor px = new DCAEProcessor() { + public String getName() { + return (null); + } + + public String getVersion() { + return (null); + } + + public String getComponentId() { + return (null); + } + + public String getComponentUrl() { + return (null); + } + + protected List<PropertyDescriptor> buildSupportedPropertyDescriptors() { + return (new LinkedList<>()); + } + + protected Set<Relationship> buildRelationships() { + return (new HashSet<>()); + } + + public DCAEProcessor xxx() { + getSupportedPropertyDescriptors(); + getSupportedPropertyDescriptors(); + return (this); + } + }.xxx(); + px.ping(); + px.onTrigger((ProcessContext)null, (ProcessSession)null); + px.getRelationships(); + px.getRelationships(); + } + + + @Test + public void testPaths() throws InterruptedException, IOException, URISyntaxException { + /* some trivial cases */ + new OnboardingAPIClient(); + try { + OnboardingAPIClient.getComponents("6:invalidURI"); + } catch (OnboardingAPIClient.OnboardingAPIClientError oace) { + // expected case + } + try { + OnboardingAPIClient.getComponent(null); + } catch (OnboardingAPIClient.OnboardingAPIClientError oace) { + // expected case + } + /* background one shot failure cases */ + evars.clear("GENPROC_SLEEP_SEC"); + App.main(new String[0]); + evars.set("GENPROC_SLEEP_SEC", "0"); + String wdir = tfolder.newFolder("work").getPath(); + evars.set("GENPROC_WORKING_DIR", wdir); + String onboardingdir = tfolder.newFolder("onboarding").getPath(); + evars.set("GENPROC_ONBOARDING_API_HOST", (new File(onboardingdir)).toURI().toURL().toString()); + String compfile = onboardingdir + "/compone"; + try (Writer w = new FileWriter(compfile)) { + w.write("{ \"id\": \"1\", \"spec\": { \"name\": \"one-collector\"," + + " \"version\": \"1.0.0\", \"description\": \"desc\"," + + " \"parameters\": [{\"name\": \"p1\", \"value\": \"v1\"," + + " \"description\": \"d1\"}], \"streams\":" + + " {\"publishes\":[{\"format\": \"f1\", \"version\": \"v1\"," + + " \"type\": \"t1\", \"config_key\": \"ck1\"}]," + + " \"subscribes\":[{\"format\": \"f2\", \"version\": \"v2\"," + + " \"type\": \"t2\", \"config_key\": \"ck2\"}]}}," + + " \"selfUrl\": \"file:" + compfile + "\"}"); + } + try (Writer w = new FileWriter(onboardingdir + "/components")) { + w.write("{\"components\": [{\"id\": \"1\", \"name\": \"one\"," + + " \"version\": \"1.0.0\", \"description\": \"desc\"," + + " \"componentType\": \"apple\", \"owner\": \"John Doe\"," + + " \"componentUrl\": \"file:" + compfile + "\"," + + " \"whenAdded\": \"never\" }]}"); + } + String indexfile = tfolder.newFile("index").getPath(); + try (Writer w = new FileWriter(indexfile)) { + w.write("[]"); + } + evars.set("GENPROC_JAR_INDEX_URL", (new File(indexfile)).toURI().toURL().toString()); + App.main(new String[0]); + /* help case */ + App.main(new String[] { "-h" }); + /* load case */ + App.main(new String[] { "load" }); + /* gen case */ + App.main(new String[] { "gen" }); } } |