CodeCoverage improvement for dcaegen2-platform-mod-genprocessor
[dcaegen2/platform.git] / mod / genprocessor / src / main / java / org / onap / dcae / genprocessor / App.java
index 9996b71..445291e 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2020 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.
@@ -23,6 +24,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 +162,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
@@ -204,9 +206,7 @@ public class App {
                     return true;
                 }
             }
-        } catch (InterruptedException e) {
-            throw new RuntimeException("Error while creating jar", e);
-        } catch (IOException e) {
+        } catch (InterruptedException | IOException e) {
             throw new RuntimeException("Error while creating jar", e);
         }
 
@@ -308,13 +308,34 @@ public class App {
         return false;
     }
 
-    public static void main(String[] args) {
+    public static void main(String[] args) throws InterruptedException, URISyntaxException {
         if (args.length == 0) {
+            args = new String[] { "gen" };
+            String sleepstr = System.getenv("GENPROC_SLEEP_SEC");
+            long sleepdur = (sleepstr != null)? 1000 * Long.parseLong(sleepstr): 0;
+            do {
+                try {
+                    main2(args);
+                } catch (Exception e) {
+                    LOG.error(e.toString(), e);
+                }
+                Thread.sleep(sleepdur);
+            } while (sleepdur > 0);
+            return;
+        } else {
+            main2(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:");
             LOG.info("<gen> <load>");
+            return;
         }
 
-        String argsStr = String.join(", ", args);
         boolean shouldGenerate = argsStr.contains("gen") ? true : false;
         boolean shouldLoad = argsStr.contains("load") ? true : false;
         boolean shouldPackage = argsStr.contains("package") ? true : false;
@@ -322,7 +343,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[] {
@@ -359,7 +379,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);
                 }
             }
@@ -380,3 +400,4 @@ public class App {
         }
     }
 }
+