From 72aef3a6324c78a62d0eff65217d4861440841f5 Mon Sep 17 00:00:00 2001 From: Dan Timoney Date: Tue, 31 Jan 2023 08:15:34 -0500 Subject: Fix Java 17 jUnit errors 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 Change-Id: I278ee869d2ce0568b29ecc29c37d00d7fce757a8 --- .../sli/adaptors/aai/AAIClientRESTExecutor.java | 5 ++++- .../onap/ccsdk/sli/adaptors/aai/AAIService.java | 4 +++- adaptors/sql-resource/provider/pom.xml | 14 +++++++++++++ .../resource/sql/SqlResourceProviderTest.java | 21 +------------------ northbound/dmaap-listener/pom.xml | 1 + .../dmaapclient/TestSdncEsrDmaapReceiver.java | 11 +--------- .../dmaapclient/TestSdncJsonDmaapConsumer.java | 21 ------------------- .../dmaapclient/TestSdncPserverDmaapReceiver.java | 10 --------- .../dmaap-consumer-esrsysteminfo.properties | 3 ++- .../resources/dmaap-consumer-pserver.properties | 1 + northbound/ueb-listener/pom.xml | 5 +++++ plugins/grToolkit/provider/pom.xml | 2 ++ .../plugins/grtoolkit/GrToolkitProviderTest.java | 4 ---- plugins/properties-node/provider/pom.xml | 24 ++++++++++++---------- .../ccsdk/sli/plugins/prop/TestPropertiesNode.java | 10 ++------- 15 files changed, 49 insertions(+), 87 deletions(-) diff --git a/adaptors/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIClientRESTExecutor.java b/adaptors/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIClientRESTExecutor.java index 3ededcc54..6083e89ba 100755 --- a/adaptors/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIClientRESTExecutor.java +++ b/adaptors/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIClientRESTExecutor.java @@ -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."); } diff --git a/adaptors/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIService.java b/adaptors/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIService.java index 14206288d..f69020f75 100755 --- a/adaptors/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIService.java +++ b/adaptors/aai-service/provider/src/main/java/org/onap/ccsdk/sli/adaptors/aai/AAIService.java @@ -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); } + */ } diff --git a/adaptors/sql-resource/provider/pom.xml b/adaptors/sql-resource/provider/pom.xml index 4a53413cb..c6d256d34 100755 --- a/adaptors/sql-resource/provider/pom.xml +++ b/adaptors/sql-resource/provider/pom.xml @@ -79,4 +79,18 @@ provided + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + ${basedir}/src/test/resources + + + + + diff --git a/adaptors/sql-resource/provider/src/test/java/org/onap/ccsdk/sli/adaptors/resource/sql/SqlResourceProviderTest.java b/adaptors/sql-resource/provider/src/test/java/org/onap/ccsdk/sli/adaptors/resource/sql/SqlResourceProviderTest.java index 6436f1ef1..4b97e1d5a 100755 --- a/adaptors/sql-resource/provider/src/test/java/org/onap/ccsdk/sli/adaptors/resource/sql/SqlResourceProviderTest.java +++ b/adaptors/sql-resource/provider/src/test/java/org/onap/ccsdk/sli/adaptors/resource/sql/SqlResourceProviderTest.java @@ -36,16 +36,7 @@ public class SqlResourceProviderTest { @Test public void testSqlResourceProvider() { - try{ - Map env = System.getenv(); - Class cl = env.getClass(); - Field field = cl.getDeclaredField("m"); - field.setAccessible(true); - Map writableEnv = (Map) 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 env = System.getenv(); - Class cl = env.getClass(); - Field field = cl.getDeclaredField("m"); - field.setAccessible(true); - Map writableEnv = (Map) 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(); diff --git a/northbound/dmaap-listener/pom.xml b/northbound/dmaap-listener/pom.xml index 17137bcd0..db426f73c 100755 --- a/northbound/dmaap-listener/pom.xml +++ b/northbound/dmaap-listener/pom.xml @@ -162,6 +162,7 @@ ABC123 admin admin + . diff --git a/northbound/dmaap-listener/src/test/java/org/onap/ccsdk/sli/northbound/dmaapclient/TestSdncEsrDmaapReceiver.java b/northbound/dmaap-listener/src/test/java/org/onap/ccsdk/sli/northbound/dmaapclient/TestSdncEsrDmaapReceiver.java index 623c96426..cd198e1d3 100644 --- a/northbound/dmaap-listener/src/test/java/org/onap/ccsdk/sli/northbound/dmaapclient/TestSdncEsrDmaapReceiver.java +++ b/northbound/dmaap-listener/src/test/java/org/onap/ccsdk/sli/northbound/dmaapclient/TestSdncEsrDmaapReceiver.java @@ -117,16 +117,7 @@ public class TestSdncEsrDmaapReceiver { e.printStackTrace(); } - try { - Map env = System.getenv(); - Class cl = env.getClass(); - Field field = cl.getDeclaredField("m"); - field.setAccessible(true); - Map writableEnv = (Map) 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"); diff --git a/northbound/dmaap-listener/src/test/java/org/onap/ccsdk/sli/northbound/dmaapclient/TestSdncJsonDmaapConsumer.java b/northbound/dmaap-listener/src/test/java/org/onap/ccsdk/sli/northbound/dmaapclient/TestSdncJsonDmaapConsumer.java index 620098cb7..bf7b4e236 100644 --- a/northbound/dmaap-listener/src/test/java/org/onap/ccsdk/sli/northbound/dmaapclient/TestSdncJsonDmaapConsumer.java +++ b/northbound/dmaap-listener/src/test/java/org/onap/ccsdk/sli/northbound/dmaapclient/TestSdncJsonDmaapConsumer.java @@ -52,16 +52,6 @@ public class TestSdncJsonDmaapConsumer { e.printStackTrace(); } - try { - Map env = System.getenv(); - Class cl = env.getClass(); - Field field = cl.getDeclaredField("m"); - field.setAccessible(true); - Map writableEnv = (Map) 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 env = System.getenv(); - Class cl = env.getClass(); - Field field = cl.getDeclaredField("m"); - field.setAccessible(true); - Map writableEnv = (Map) field.get(env); - writableEnv.put(DMAAPLISTENERROOT, "."); - } catch (Exception e) { - throw new IllegalStateException("Failed to set environment variable", e); - } - String msg = "{\n" + " \"input\" : { \n" + " }\n" + diff --git a/northbound/dmaap-listener/src/test/java/org/onap/ccsdk/sli/northbound/dmaapclient/TestSdncPserverDmaapReceiver.java b/northbound/dmaap-listener/src/test/java/org/onap/ccsdk/sli/northbound/dmaapclient/TestSdncPserverDmaapReceiver.java index 11201265d..06876086d 100644 --- a/northbound/dmaap-listener/src/test/java/org/onap/ccsdk/sli/northbound/dmaapclient/TestSdncPserverDmaapReceiver.java +++ b/northbound/dmaap-listener/src/test/java/org/onap/ccsdk/sli/northbound/dmaapclient/TestSdncPserverDmaapReceiver.java @@ -118,16 +118,6 @@ public class TestSdncPserverDmaapReceiver { e.printStackTrace(); } - try { - Map env = System.getenv(); - Class cl = env.getClass(); - Field field = cl.getDeclaredField("m"); - field.setAccessible(true); - Map writableEnv = (Map) 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(); diff --git a/northbound/dmaap-listener/src/test/resources/dmaap-consumer-esrsysteminfo.properties b/northbound/dmaap-listener/src/test/resources/dmaap-consumer-esrsysteminfo.properties index 785d636bb..6492316fe 100644 --- a/northbound/dmaap-listener/src/test/resources/dmaap-consumer-esrsysteminfo.properties +++ b/northbound/dmaap-listener/src/test/resources/dmaap-consumer-esrsysteminfo.properties @@ -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 diff --git a/northbound/dmaap-listener/src/test/resources/dmaap-consumer-pserver.properties b/northbound/dmaap-listener/src/test/resources/dmaap-consumer-pserver.properties index 2fb525e89..6492316fe 100644 --- a/northbound/dmaap-listener/src/test/resources/dmaap-consumer-pserver.properties +++ b/northbound/dmaap-listener/src/test/resources/dmaap-consumer-pserver.properties @@ -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 diff --git a/northbound/ueb-listener/pom.xml b/northbound/ueb-listener/pom.xml index db620937b..293c5ba49 100755 --- a/northbound/ueb-listener/pom.xml +++ b/northbound/ueb-listener/pom.xml @@ -93,6 +93,11 @@ commons-codec commons-codec + + org.projectlombok + lombok + 1.18.24 + diff --git a/plugins/grToolkit/provider/pom.xml b/plugins/grToolkit/provider/pom.xml index 87f2a789a..e39e17434 100755 --- a/plugins/grToolkit/provider/pom.xml +++ b/plugins/grToolkit/provider/pom.xml @@ -150,6 +150,7 @@ admin admin + src/test/resources @@ -160,6 +161,7 @@ admin admin + src/test/resources diff --git a/plugins/grToolkit/provider/src/test/java/org/onap/ccsdk/sli/plugins/grtoolkit/GrToolkitProviderTest.java b/plugins/grToolkit/provider/src/test/java/org/onap/ccsdk/sli/plugins/grtoolkit/GrToolkitProviderTest.java index 6495c89cf..98e55e50d 100644 --- a/plugins/grToolkit/provider/src/test/java/org/onap/ccsdk/sli/plugins/grtoolkit/GrToolkitProviderTest.java +++ b/plugins/grToolkit/provider/src/test/java/org/onap/ccsdk/sli/plugins/grtoolkit/GrToolkitProviderTest.java @@ -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); diff --git a/plugins/properties-node/provider/pom.xml b/plugins/properties-node/provider/pom.xml index ddae8edca..66b79b14b 100755 --- a/plugins/properties-node/provider/pom.xml +++ b/plugins/properties-node/provider/pom.xml @@ -32,17 +32,6 @@ junit test - - com.github.stefanbirkner - system-rules - 1.19.0 - test - - - org.springframework - spring-test - test - org.onap.ccsdk.sli.core sli-common @@ -57,4 +46,17 @@ jettison + + + + org.apache.maven.plugins + maven-surefire-plugin + + + sdncp-123 + + + + + diff --git a/plugins/properties-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/prop/TestPropertiesNode.java b/plugins/properties-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/prop/TestPropertiesNode.java index 34ac4685f..f5259845d 100644 --- a/plugins/properties-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/prop/TestPropertiesNode.java +++ b/plugins/properties-node/provider/src/test/java/jtest/org/onap/ccsdk/sli/plugins/prop/TestPropertiesNode.java @@ -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(); -- cgit 1.2.3-korg