Fix Java 17 jUnit errors 89/133089/6
authorDan Timoney <dtimoney@att.com>
Tue, 31 Jan 2023 13:15:34 +0000 (08:15 -0500)
committerDan Timoney <dtimoney@att.com>
Tue, 7 Feb 2023 15:24:42 +0000 (10:24 -0500)
Add explicit lombok dependency to resolve compile error when compiling
under java 17

Fixed code issues in jUnit tests due to restrictions on reflection
in Java 17, which disallow jUnit from setting system properties
or environment variables.

Issue-ID: CCSDK-3813
Signed-off-by: Dan Timoney <dtimoney@att.com>
Change-Id: I278ee869d2ce0568b29ecc29c37d00d7fce757a8

15 files changed:
adaptors/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIClientRESTExecutor.java
adaptors/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIService.java
adaptors/sql-resource/provider/pom.xml
adaptors/sql-resource/provider/src/test/java/org/onap/ccsdk/sli/adaptors/resource/sql/SqlResourceProviderTest.java
northbound/dmaap-listener/pom.xml
northbound/dmaap-listener/src/test/java/org/onap/ccsdk/sli/northbound/dmaapclient/TestSdncEsrDmaapReceiver.java
northbound/dmaap-listener/src/test/java/org/onap/ccsdk/sli/northbound/dmaapclient/TestSdncJsonDmaapConsumer.java
northbound/dmaap-listener/src/test/java/org/onap/ccsdk/sli/northbound/dmaapclient/TestSdncPserverDmaapReceiver.java
northbound/dmaap-listener/src/test/resources/dmaap-consumer-esrsysteminfo.properties
northbound/dmaap-listener/src/test/resources/dmaap-consumer-pserver.properties
northbound/ueb-listener/pom.xml
plugins/grToolkit/provider/pom.xml
plugins/grToolkit/provider/src/test/java/org/onap/ccsdk/sli/plugins/grtoolkit/GrToolkitProviderTest.java
plugins/properties-node/provider/pom.xml
plugins/properties-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/prop/TestPropertiesNode.java

index 3ededcc..6083e89 100755 (executable)
@@ -31,6 +31,7 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.lang.reflect.Field;
+import java.lang.reflect.InaccessibleObjectException;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
@@ -173,6 +174,7 @@ public class AAIClientRESTExecutor implements AAIExecutorInterface {
             }
         }
 
+        /** This does not work in Java 17 
         try {
             Field methodsField = HttpURLConnection.class.getDeclaredField("methods");
             methodsField.setAccessible(true);
@@ -184,7 +186,7 @@ public class AAIClientRESTExecutor implements AAIExecutorInterface {
             // remove the "final" modifier
             modifiersField.setInt(methodsField, methodsField.getModifiers() & ~Modifier.FINAL);
 
-            /* valid HTTP methods */
+            // valid HTTP methods
             String[] methods = {
                        "GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE", "PATCH"
             };
@@ -194,6 +196,7 @@ public class AAIClientRESTExecutor implements AAIExecutorInterface {
         } catch (SecurityException | IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) {
             LOG.warn("Adding PATCH method", e);
         }
+        **/
         LOG.info("AAIResource.ctor initialized.");
 
     }
index 1420628..f69020f 100755 (executable)
@@ -314,6 +314,7 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe
 
         LOG.info("AAIResource.ctor initialized.");
 
+        /* Not permitted in Java 17
         try {
             Field methodsField = HttpURLConnection.class.getDeclaredField("methods");
             methodsField.setAccessible(true);
@@ -325,7 +326,7 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe
             // remove the "final" modifier
             modifiersField.setInt(methodsField, methodsField.getModifiers() & ~Modifier.FINAL);
 
-            /* valid HTTP methods */
+            // valid HTTP methods 
             String[] methods = {
                        "GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE", "PATCH"
             };
@@ -335,6 +336,7 @@ public class AAIService extends AAIDeclarations implements AAIClient, SvcLogicRe
         } catch (SecurityException | IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) {
             LOG.error("Exception occured", e);
         }
+        */
 
     }
 
index 4a53413..c6d256d 100755 (executable)
             <scope>provided</scope>
         </dependency>
     </dependencies>
+
+    <build>
+        <plugins>
+          <plugin>
+            <groupId>org.apache.maven.plugins</groupId>
+            <artifactId>maven-surefire-plugin</artifactId>
+            <configuration>
+              <systemPropertyVariables>
+                <SDNC_CONFIG_DIR>${basedir}/src/test/resources</SDNC_CONFIG_DIR>
+              </systemPropertyVariables>
+            </configuration>
+          </plugin>
+        </plugins>
+      </build>
 </project>
index 6436f1e..4b97e1d 100755 (executable)
@@ -36,16 +36,7 @@ public class SqlResourceProviderTest {
 
     @Test
     public void testSqlResourceProvider() {
-        try{
-            Map<String, String> env = System.getenv();
-            Class<?> cl = env.getClass();
-            Field field = cl.getDeclaredField("m");
-            field.setAccessible(true);
-            Map<String, String> writableEnv = (Map<String, String>) field.get(env);
-            writableEnv.put(SDNC_CONFIG_DIR, "./src/test/resources");
-        } catch (Exception e) {
-            throw new IllegalStateException("Failed to set environment variable", e);
-        }
+
 
         provider = new SqlResourcePropertiesProviderImpl();
         assertNotNull(provider);
@@ -59,16 +50,6 @@ public class SqlResourceProviderTest {
 
     @Test
     public void testReportSuccess() {
-        try{
-            Map<String, String> env = System.getenv();
-            Class<?> cl = env.getClass();
-            Field field = cl.getDeclaredField("m");
-            field.setAccessible(true);
-            Map<String, String> writableEnv = (Map<String, String>) field.get(env);
-            writableEnv.put(SDNC_CONFIG_DIR, "./src/test/resources");
-        } catch (Exception e) {
-            throw new IllegalStateException("Failed to set environment variable", e);
-        }
 
         provider = new SqlResourcePropertiesProviderImpl();
         Properties properties = provider.getProperties();
index 17137bc..db426f7 100755 (executable)
                         <DMAAP_AUTHKEY>ABC123</DMAAP_AUTHKEY>
                         <ODL_USER>admin</ODL_USER>
                         <ODL_PASSWORD>admin</ODL_PASSWORD>
+                        <DMAAPLISTENERROOT>.</DMAAPLISTENERROOT>
                     </environmentVariables>
                 </configuration>
             </plugin>
index 623c964..cd198e1 100644 (file)
@@ -117,16 +117,7 @@ public class TestSdncEsrDmaapReceiver {
             e.printStackTrace();
         }
 
-        try {
-            Map<String, String> env = System.getenv();
-            Class<?> cl = env.getClass();
-            Field field = cl.getDeclaredField("m");
-            field.setAccessible(true);
-            Map<String, String> writableEnv = (Map<String, String>) field.get(env);
-            writableEnv.put(DMAAPLISTENERROOT, ".");
-        } catch (Exception e) {
-            throw new IllegalStateException("Failed to set environment variable", e);
-        }
+
         Properties props = new Properties();
         InputStream propStr = TestSdncEsrDmaapReceiver.class.getResourceAsStream("/dmaap-consumer-esrsysteminfo.properties");
 
index 620098c..bf7b4e2 100644 (file)
@@ -52,16 +52,6 @@ public class TestSdncJsonDmaapConsumer {
             e.printStackTrace();
         }
 
-        try {
-            Map<String, String> env = System.getenv();
-            Class<?> cl = env.getClass();
-            Field field = cl.getDeclaredField("m");
-            field.setAccessible(true);
-            Map<String, String> writableEnv = (Map<String, String>) field.get(env);
-            writableEnv.put(DMAAPLISTENERROOT, ".");
-        } catch (Exception e) {
-            throw new IllegalStateException("Failed to set environment variable", e);
-        }
 
         String msg = "{\n" +
                 "    \"input\" : {        \n" +
@@ -99,17 +89,6 @@ public class TestSdncJsonDmaapConsumer {
             e.printStackTrace();
         }
 
-        try {
-            Map<String, String> env = System.getenv();
-            Class<?> cl = env.getClass();
-            Field field = cl.getDeclaredField("m");
-            field.setAccessible(true);
-            Map<String, String> writableEnv = (Map<String, String>) field.get(env);
-            writableEnv.put(DMAAPLISTENERROOT, ".");
-        } catch (Exception e) {
-            throw new IllegalStateException("Failed to set environment variable", e);
-        }
-
         String msg = "{\n" +
                 "    \"input\" : {        \n" +
                 "    }\n" +
index 1120126..0687608 100644 (file)
@@ -118,16 +118,6 @@ public class TestSdncPserverDmaapReceiver {
                        e.printStackTrace();
                }
 
-               try {
-                       Map<String, String> env = System.getenv();
-                       Class<?> cl = env.getClass();
-                       Field field = cl.getDeclaredField("m");
-                       field.setAccessible(true);
-                       Map<String, String> writableEnv = (Map<String, String>) field.get(env);
-                       writableEnv.put(DMAAPLISTENERROOT, ".");
-               } catch (Exception e) {
-                       throw new IllegalStateException("Failed to set environment variable", e);
-               }
                Properties props = new Properties();
 
                SdncAaiDmaapConsumer consumer = new SdncAaiDmaapConsumer();
index 785d636..6492316 100644 (file)
@@ -31,4 +31,5 @@ sessionstickinessrequired=NO
 DME2preferredRouterFilePath=src/test/resources/dmaap-listener.preferredRoute.txt
 sdnc.odl.user=${ODL_USER}
 sdnc.odl.password=${ODL_PASSWORD}
-sdnc.odl.url-base=http://localhost:8181/restconf/operations
\ No newline at end of file
+sdnc.odl.url-base=http://localhost:8181/restconf/operations
+routeOffer=MR1
\ No newline at end of file
index 2fb525e..6492316 100644 (file)
@@ -32,3 +32,4 @@ DME2preferredRouterFilePath=src/test/resources/dmaap-listener.preferredRoute.txt
 sdnc.odl.user=${ODL_USER}
 sdnc.odl.password=${ODL_PASSWORD}
 sdnc.odl.url-base=http://localhost:8181/restconf/operations
+routeOffer=MR1
\ No newline at end of file
index db62093..293c5ba 100755 (executable)
             <groupId>commons-codec</groupId>
             <artifactId>commons-codec</artifactId>
         </dependency>
+       <dependency>
+            <groupId>org.projectlombok</groupId>
+           <artifactId>lombok</artifactId>
+           <version>1.18.24</version>
+       </dependency>
     </dependencies>
 
     <build>
index 87f2a78..e39e174 100755 (executable)
                     <environmentVariables>
                         <ODL_USER>admin</ODL_USER>
                         <ODL_PASSWORD>admin</ODL_PASSWORD>
+                        <SDNC_CONFIG_DIR>src/test/resources</SDNC_CONFIG_DIR>
                     </environmentVariables>
                 </configuration>
             </plugin>
                     <environmentVariables>
                         <ODL_USER>admin</ODL_USER>
                         <ODL_PASSWORD>admin</ODL_PASSWORD>
+                        <SDNC_CONFIG_DIR>src/test/resources</SDNC_CONFIG_DIR>
                     </environmentVariables>
                 </configuration>
             </plugin>
index 6495c89..98e55e5 100644 (file)
@@ -46,7 +46,6 @@ import java.util.stream.Stream;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
-import org.junit.contrib.java.lang.system.EnvironmentVariables;
 import org.onap.ccsdk.sli.core.dblib.DBLibConnection;
 import org.onap.ccsdk.sli.core.dblib.DbLibService;
 import org.onap.ccsdk.sli.plugins.grtoolkit.data.ClusterActor;
@@ -80,14 +79,11 @@ public class GrToolkitProviderTest {
     DBLibConnection connection;
     Properties properties;
 
-    @Rule
-    public final EnvironmentVariables environmentVariables = new EnvironmentVariables();
     @Rule
     public WireMockRule wireMockRule = new WireMockRule(9999);
 
     @Before
     public void setup() {
-        environmentVariables.set("SDNC_CONFIG_DIR","src/test/resources");
         dataBroker = mock(DataBroker.class);
         notificationProviderService = mock(NotificationPublishService.class);
         rpcProviderRegistry = mock(RpcProviderService.class);
index ddae8ed..66b79b1 100755 (executable)
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
-        <dependency>
-            <groupId>com.github.stefanbirkner</groupId>
-            <artifactId>system-rules</artifactId>
-            <version>1.19.0</version>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
-            <groupId>org.springframework</groupId>
-            <artifactId>spring-test</artifactId>
-            <scope>test</scope>
-        </dependency>
         <dependency>
             <groupId>org.onap.ccsdk.sli.core</groupId>
             <artifactId>sli-common</artifactId>
             <artifactId>jettison</artifactId>
         </dependency>
     </dependencies>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <configuration>
+                  <environmentVariables>
+                    <deployer_pass>sdncp-123</deployer_pass>
+                  </environmentVariables>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
 </project>
index 34ac468..f525984 100644 (file)
@@ -1,13 +1,8 @@
 package jtest.org.onap.ccsdk.sli.plugins.prop;
 
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.Map;
-import java.util.Set;
-
-import org.junit.Rule;
 import org.junit.Test;
-import org.junit.contrib.java.lang.system.EnvironmentVariables;
 import static org.junit.Assert.assertEquals;
 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
@@ -18,8 +13,7 @@ import org.slf4j.LoggerFactory;
 public class TestPropertiesNode {
 
     private static final Logger log = LoggerFactory.getLogger(TestPropertiesNode.class);
-    @Rule
-    public EnvironmentVariables environmentVariables = new EnvironmentVariables();
+
 
     @Test
     public void testJSONFileParsing() throws SvcLogicException {
@@ -134,7 +128,7 @@ public class TestPropertiesNode {
     @Test
     public void testTXTFileParsing() throws SvcLogicException {
 
-        environmentVariables.set("deployer_pass", "sdncp-123");
+
         assertEquals("sdncp-123", System.getenv("deployer_pass"));
 
         SvcLogicContext ctx = new SvcLogicContext();