Merge "CodeCoverage improvement for dcaegen2-platform-mod-genprocessor"
[dcaegen2/platform.git] / mod / genprocessor / src / test / java / sandbox / AppTest.java
index d6a1ca3..a4d58bc 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2022 Huawei. 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.
@@ -20,6 +21,7 @@ package sandbox;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.File;
 import java.io.FileWriter;
@@ -28,12 +30,16 @@ import java.io.Writer;
 
 import java.net.URISyntaxException;
 
+import java.net.URL;
+import java.net.URLClassLoader;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
 
+import javassist.ClassPool;
+import javassist.CtClass;
 import org.apache.nifi.components.PropertyDescriptor;
 import org.apache.nifi.processor.ProcessContext;
 import org.apache.nifi.processor.ProcessSession;
@@ -50,10 +56,13 @@ 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.onap.dcae.genprocessor.CompList;
+import org.onap.dcae.genprocessor.ProcessorBuilder;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+
 /**
  * Unit test for simple App.
  */
@@ -78,6 +87,28 @@ public class AppTest {
         CompSpec cs = new CompSpec();
         cs.unpackSelf(mx);
         assertEquals(Utils.formatNameForJar(cs), "SomeJar-2.0");
+        try {
+            CompSpec.loadComponentSpec(new File("sandbox/temp.txt"));
+        } catch (RuntimeException e) {
+            // expected case
+            return;
+        }
+        fail("Exception is not thrown");
+    }
+
+    @Test
+    public void testGetNameForJavaClass() {
+        CompList.CompShort compShort = new CompList.CompShort();
+        compShort.name = "test";
+        compShort.getNameForJavaClass();
+        compShort.componentUrl = "6:invalidURI";
+        try {
+            compShort.getComponentUrlAsURI();
+        } catch (RuntimeException e) {
+            // expected case
+            return;
+        }
+        fail("Exception is not thrwon");
     }
 
 
@@ -174,5 +205,32 @@ public class AppTest {
         App.main(new String[] { "load" });
         /* gen case */
         App.main(new String[] { "gen" });
+
+        URL[] jarURLs = new URL[1];
+        try {
+            App.loadFromJars(jarURLs);
+        } catch (NullPointerException e) {
+            // expected case
+            return;
+        }
+        fail("Exception is not thrown");
+    }
+
+    @Test
+    public void testAddMethod() {
+        try {
+            ClassPool pool = ClassPool.getDefault();
+            CtClass base = pool.get(DCAEProcessor.class.getName());
+
+            CtClass cc = pool.makeClass(String.format("org.onap.dcae.%s", DCAEProcessor.class));
+            cc.setSuperclass(base);
+
+            ProcessorBuilder.addMethod(cc, "test");
+        } catch (Exception e) {
+            // expected case
+            return;
+        }
+        fail("Exception is not thrown");
     }
 }
+