diff options
Diffstat (limited to 'plugins/plugins-event/plugins-event-carrier')
23 files changed, 763 insertions, 769 deletions
diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/ApexGrpcConsumerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/ApexGrpcConsumerTest.java index bd4103f92..fd447a205 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/ApexGrpcConsumerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/ApexGrpcConsumerTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2020 Nordix Foundation. + * Copyright (C) 2020, 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,59 +21,59 @@ package org.onap.policy.apex.plugins.event.carrier.grpc; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import org.assertj.core.api.Assertions; -import org.junit.Before; -import org.junit.Test; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.onap.policy.apex.service.engine.event.ApexEventException; import org.onap.policy.apex.service.engine.event.ApexEventReceiver; import org.onap.policy.apex.service.engine.event.PeeredReference; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; -public class ApexGrpcConsumerTest { +class ApexGrpcConsumerTest { private static final String CONSUMER_NAME = "TestApexGrpcConsumer"; private ApexGrpcConsumer grpcConsumer = null; private ApexGrpcProducer grpcProducer = null; private EventHandlerParameters consumerParameters = null; - private ApexEventReceiver incomingEventReceiver = null; + private final ApexEventReceiver incomingEventReceiver = null; /** * Set up testing. - * - * @throws ApexEventException on test set up errors. */ - @Before - public void setUp() throws ApexEventException { + @BeforeEach + void setUp() { grpcConsumer = new ApexGrpcConsumer(); grpcProducer = new ApexGrpcProducer(); } @Test - public void testInit() { - consumerParameters = populateConsumerParameters(true, true); + void testInit() { + consumerParameters = populateConsumerParameters(true); Assertions.assertThatCode(() -> grpcConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver)) .doesNotThrowAnyException(); } @Test - public void testInit_invalidPeeredMode() { - consumerParameters = populateConsumerParameters(true, false); + void testInit_invalidPeeredMode() { + consumerParameters = populateConsumerParameters(false); assertThatThrownBy(() -> grpcConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver)) .hasMessageContaining( "gRPC consumer (" + CONSUMER_NAME + ") must run in peered requestor mode with a gRPC producer"); } @Test - public void testGetName() { - assertEquals(null, new ApexGrpcConsumer().getName()); + void testGetName() { + assertNull(new ApexGrpcConsumer().getName()); } @Test - public void testPeeredReference() throws ApexEventException { - consumerParameters = populateConsumerParameters(true, true); + void testPeeredReference() throws ApexEventException { + consumerParameters = populateConsumerParameters(true); grpcConsumer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, new PeeredReference(EventHandlerPeeredMode.REQUESTOR, grpcConsumer, grpcProducer)); grpcConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver); @@ -83,19 +83,10 @@ public class ApexGrpcConsumerTest { assertEquals(grpcProducer, peeredReference.getPeeredProducer()); } - private EventHandlerParameters populateConsumerParameters(boolean isConsumer, boolean isPeeredMode) { + private EventHandlerParameters populateConsumerParameters(boolean isPeeredMode) { consumerParameters = new EventHandlerParameters(); - GrpcCarrierTechnologyParameters params = new GrpcCarrierTechnologyParameters(); - params.setLabel("GRPC"); - params.setEventProducerPluginClass(ApexGrpcProducer.class.getName()); - params.setEventConsumerPluginClass(ApexGrpcConsumer.class.getName()); - if (!isConsumer) { - params.setHost("hostname"); - params.setPort(3214); - params.setUsername("dummyUser"); - params.setPassword("dummyPassword"); - params.setTimeout(1); - } + GrpcCarrierTechnologyParameters params = + getGrpcCarrierTechnologyParameters(); consumerParameters.setCarrierTechnologyParameters(params); if (isPeeredMode) { consumerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true); @@ -103,4 +94,12 @@ public class ApexGrpcConsumerTest { } return consumerParameters; } + + private static @NotNull GrpcCarrierTechnologyParameters getGrpcCarrierTechnologyParameters() { + GrpcCarrierTechnologyParameters params = new GrpcCarrierTechnologyParameters(); + params.setLabel("GRPC"); + params.setEventProducerPluginClass(ApexGrpcProducer.class.getName()); + params.setEventConsumerPluginClass(ApexGrpcConsumer.class.getName()); + return params; + } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/ApexGrpcProducerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/ApexGrpcProducerTest.java index 31be69e3a..7411737fe 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/ApexGrpcProducerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/ApexGrpcProducerTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2020 Nordix Foundation. + * Copyright (C) 2020, 2024 Nordix Foundation. * Modifications Copyright (C) 2020 Bell Canada. All rights reserved. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ @@ -22,61 +22,61 @@ package org.onap.policy.apex.plugins.event.carrier.grpc; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.spy; import java.nio.file.Files; import java.nio.file.Paths; import org.assertj.core.api.Assertions; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.onap.policy.apex.service.engine.event.ApexEventException; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; import org.onap.policy.cds.client.CdsProcessorGrpcClient; -@RunWith(MockitoJUnitRunner.class) -public class ApexGrpcProducerTest { +@ExtendWith(MockitoExtension.class) +class ApexGrpcProducerTest { private static final String PRODUCER_NAME = "TestApexGrpcProducer"; private static final String HOST = "localhost"; @Mock private CdsProcessorGrpcClient grpcClient; - private ApexGrpcProducer apexGrpcProducer = spy(new ApexGrpcProducer()); + private final ApexGrpcProducer apexGrpcProducer = spy(new ApexGrpcProducer()); @Mock private EventHandlerParameters eventHandlerParameters; /** * Set up testing. - * - * @throws ApexEventException on test set up errors. */ - @Before - public void setUp() throws ApexEventException { - populateEventHandlerParameters(HOST, 5); + @BeforeEach + void setUp() { + populateEventHandlerParameters(); } - @Test(expected = ApexEventException.class) - public void testInit_fail() throws ApexEventException { - apexGrpcProducer.init(PRODUCER_NAME, new EventHandlerParameters()); + @Test + void testInit_fail() { + assertThrows(ApexEventException.class, + () -> apexGrpcProducer.init(PRODUCER_NAME, new EventHandlerParameters())); } @Test - public void testInit_pass() { + void testInit_pass() { // should not throw an exception Assertions.assertThatCode(() -> apexGrpcProducer.init(PRODUCER_NAME, eventHandlerParameters)) .doesNotThrowAnyException(); } @Test - public void testStop() throws ApexEventException { + void testStop() throws ApexEventException { apexGrpcProducer.init(PRODUCER_NAME, eventHandlerParameters); // should not throw an exception - Assertions.assertThatCode(() -> apexGrpcProducer.stop()).doesNotThrowAnyException(); + Assertions.assertThatCode(apexGrpcProducer::stop).doesNotThrowAnyException(); } @Test - public void testSendEvent() throws ApexEventException { + void testSendEvent() throws ApexEventException { apexGrpcProducer.init(PRODUCER_NAME, eventHandlerParameters); Assertions .assertThatCode(() -> apexGrpcProducer.sendEvent(123, null, "grpcEvent", @@ -84,17 +84,17 @@ public class ApexGrpcProducerTest { .doesNotThrowAnyException(); } - private void populateEventHandlerParameters(String host, int timeout) { + private void populateEventHandlerParameters() { eventHandlerParameters = new EventHandlerParameters(); GrpcCarrierTechnologyParameters params = new GrpcCarrierTechnologyParameters(); params.setLabel("GRPC"); params.setEventProducerPluginClass(ApexGrpcProducer.class.getName()); params.setEventConsumerPluginClass(ApexGrpcConsumer.class.getName()); - params.setHost(host); + params.setHost(ApexGrpcProducerTest.HOST); params.setPort(3214); params.setUsername("dummyUser"); params.setPassword("dummyPassword"); - params.setTimeout(timeout); + params.setTimeout(5); eventHandlerParameters.setCarrierTechnologyParameters(params); } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/GrpcCarrierTechnologyParametersTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/GrpcCarrierTechnologyParametersTest.java index 7c79e95b6..22643fedd 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/GrpcCarrierTechnologyParametersTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-grpc/src/test/java/org/onap/policy/apex/plugins/event/carrier/grpc/GrpcCarrierTechnologyParametersTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2020 Nordix Foundation. + * Copyright (C) 2020, 2024 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,16 +22,15 @@ package org.onap.policy.apex.plugins.event.carrier.grpc; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.assertj.core.api.Assertions; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.onap.policy.common.parameters.ValidationResult; -public class GrpcCarrierTechnologyParametersTest { +class GrpcCarrierTechnologyParametersTest { private static final String USERNAME = "username"; private static final String PASSWORD = "password"; @@ -39,23 +38,26 @@ public class GrpcCarrierTechnologyParametersTest { private GrpcCarrierTechnologyParameters params; - @Before - public void setUp() { + @BeforeEach + void setUp() { params = new GrpcCarrierTechnologyParameters(); } @Test - public void testGrpcCarrierTechnologyParameters_invalid_producer_params() throws ApexEventException { + void testGrpcCarrierTechnologyParameters_invalid_producer_params() { ValidationResult result = params.validate(); assertTrue(result.isValid()); assertThatThrownBy(() -> params.validateGrpcParameters(true)) - .hasMessage("Issues in specifying gRPC Producer parameters:\ntimeout should have a positive value.\n" - + "port range should be between 1024 and 65535\n" + "host should be specified.\n" - + "username should be specified.\n" + "password should be specified.\n"); + .hasMessageContaining("Issues in specifying gRPC Producer parameters") + .hasMessageContaining("timeout should have a positive value") + .hasMessageContaining("port range should be between 1024 and 65535") + .hasMessageContaining("host should be specified") + .hasMessageContaining("username should be specified") + .hasMessageContaining("password should be specified"); } @Test - public void testGrpcCarrierTechnologyParameters_valid() { + void testGrpcCarrierTechnologyParameters_valid() { assertEquals("GRPC", params.getName()); assertEquals(ApexGrpcConsumer.class.getName(), params.getEventConsumerPluginClass()); assertEquals(ApexGrpcProducer.class.getName(), params.getEventProducerPluginClass()); @@ -71,7 +73,7 @@ public class GrpcCarrierTechnologyParametersTest { } @Test - public void testGrpcCarrierTechnologyParameters_invalid_values() { + void testGrpcCarrierTechnologyParameters_invalid_values() { params.setHost(HOST); params.setPassword(PASSWORD); params.setTimeout(1000); diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsConsumerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsConsumerTest.java index ab1155641..5ea2fab05 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsConsumerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsConsumerTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Samsung. All rights reserved. - * Modifications Copyright (C) 2019-2021, 2023 Nordix Foundation. + * Modifications Copyright (C) 2019-2021, 2023-2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,8 +23,8 @@ package org.onap.policy.apex.plugins.event.carrier.jms; import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import jakarta.jms.Connection; import jakarta.jms.ConnectionFactory; @@ -34,8 +34,9 @@ import jakarta.jms.Topic; import java.util.Properties; import javax.naming.InitialContext; import javax.naming.NamingException; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.ArgumentMatchers; import org.mockito.Mockito; import org.onap.policy.apex.service.engine.event.ApexEventException; @@ -47,7 +48,7 @@ import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnolo import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; -public class ApexJmsConsumerTest { +class ApexJmsConsumerTest { private static final String CONSUMER_NAME = "TestApexJmsConsumer"; private ApexJmsConsumer apexJmsConsumer = null; @@ -56,38 +57,36 @@ public class ApexJmsConsumerTest { private ApexEventProducer apexJmsProducer = null; private JmsCarrierTechnologyParameters jmsCarrierTechnologyParameters = null; + AutoCloseable closeable; + /** * Set up testing. */ - @Before - public void setUp() { + @BeforeEach + void setUp() { apexJmsConsumer = Mockito.spy(new ApexJmsConsumer()); consumerParameters = new EventHandlerParameters(); apexJmsProducer = new ApexJmsProducer(); } @Test - public void testInitWithNonJmsCarrierTechnologyParameters() { + void testInitWithNonJmsCarrierTechnologyParameters() { consumerParameters.setCarrierTechnologyParameters(new CarrierTechnologyParameters() { }); - assertThatThrownBy( - () -> apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver) - ) + assertThatThrownBy(() -> apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver)) .isInstanceOf(ApexEventException.class); } @Test - public void testInitWithJmsCarrierTechnologyParameters() { + void testInitWithJmsCarrierTechnologyParameters() { jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); consumerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); - assertThatThrownBy( - () -> apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver) - ) + assertThatThrownBy(() -> apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver)) .isInstanceOf(ApexEventException.class); } @Test - public void testInitNoConnectionFactory() throws NamingException { + void testInitNoConnectionFactory() throws NamingException { jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); consumerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); @@ -95,14 +94,12 @@ public class ApexJmsConsumerTest { Mockito.doReturn(null).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); Mockito.doReturn(context).when(apexJmsConsumer).getInitialContext(); - assertThatThrownBy( - () -> apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver) - ) + assertThatThrownBy(() -> apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver)) .isInstanceOf(ApexEventException.class); } @Test - public void testInitNoConsumerTopic() throws NamingException { + void testInitNoConsumerTopic() throws NamingException { jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); consumerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); @@ -113,14 +110,12 @@ public class ApexJmsConsumerTest { Mockito.doReturn(null).when(context).lookup(jmsCarrierTechnologyParameters.getConsumerTopic()); Mockito.doReturn(context).when(apexJmsConsumer).getInitialContext(); - assertThatThrownBy( - () -> apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver) - ) + assertThatThrownBy(() -> apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver)) .isInstanceOf(ApexEventException.class); } @Test - public void testInitNoConnection() throws NamingException, JMSException { + void testInitNoConnection() throws NamingException, JMSException { jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); consumerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); @@ -130,21 +125,18 @@ public class ApexJmsConsumerTest { Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getConsumerTopic()); - Mockito.doThrow(JMSException.class) - .when(connectionFactory) + closeable = Mockito.doThrow(JMSException.class).when(connectionFactory) .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), jmsCarrierTechnologyParameters.getSecurityCredentials()); Mockito.doReturn(context).when(apexJmsConsumer).getInitialContext(); - assertThatThrownBy( - () -> apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver) - ) + assertThatThrownBy(() -> apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver)) .isInstanceOf(ApexEventException.class); } @Test - public void testInitConnectionError() throws NamingException, JMSException { + void testInitConnectionError() throws NamingException, JMSException { jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); consumerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); @@ -155,21 +147,18 @@ public class ApexJmsConsumerTest { Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getConsumerTopic()); - Mockito.doReturn(connection) - .when(connectionFactory) + closeable = Mockito.doReturn(connection).when(connectionFactory) .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), jmsCarrierTechnologyParameters.getSecurityCredentials()); Mockito.doThrow(JMSException.class).when(connection).start(); Mockito.doReturn(context).when(apexJmsConsumer).getInitialContext(); - assertThatThrownBy( - () -> apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver) - ) + assertThatThrownBy(() -> apexJmsConsumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver)) .isInstanceOf(ApexEventException.class); } @Test - public void testInit() throws NamingException, JMSException, ApexEventException { + void testInit() throws NamingException, JMSException, ApexEventException { jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); consumerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); @@ -180,8 +169,7 @@ public class ApexJmsConsumerTest { Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getConsumerTopic()); - Mockito.doReturn(connection) - .when(connectionFactory) + closeable = Mockito.doReturn(connection).when(connectionFactory) .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), jmsCarrierTechnologyParameters.getSecurityCredentials()); Mockito.doNothing().when(connection).start(); @@ -193,22 +181,22 @@ public class ApexJmsConsumerTest { } @Test - public void testStart() { + void testStart() { assertThatCode(apexJmsConsumer::start).doesNotThrowAnyException(); } @Test - public void testGetName() { + void testGetName() { assertNull(apexJmsConsumer.getName()); } @Test - public void testGetPeeredReference() { + void testGetPeeredReference() { assertNull(apexJmsConsumer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR)); } @Test - public void testSetPeeredReference() { + void testSetPeeredReference() { PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR, apexJmsConsumer, apexJmsProducer); apexJmsConsumer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, peeredReference); @@ -216,19 +204,19 @@ public class ApexJmsConsumerTest { } @Test - public void testRun() { + void testRun() { assertThatThrownBy(apexJmsConsumer::run).isInstanceOf(ApexEventRuntimeException.class); } @Test - public void testOnMessageUninitialized() { + void testOnMessageUninitialized() { Message jmsMessage = null; assertThatThrownBy(() -> apexJmsConsumer.onMessage(jmsMessage)) .isInstanceOf(ApexEventRuntimeException.class); } @Test - public void testOnMessage() throws JMSException, NamingException, ApexEventException { + void testOnMessage() throws JMSException, NamingException, ApexEventException { // prepare ApexJmsConsumer jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); consumerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); @@ -242,8 +230,7 @@ public class ApexJmsConsumerTest { Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getConsumerTopic()); - Mockito.doReturn(connection) - .when(connectionFactory) + closeable = Mockito.doReturn(connection).when(connectionFactory) .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), jmsCarrierTechnologyParameters.getSecurityCredentials()); Mockito.doNothing().when(connection).start(); @@ -260,7 +247,7 @@ public class ApexJmsConsumerTest { } @Test - public void testConnectionError() throws NamingException, JMSException, ApexEventException { + void testConnectionError() throws NamingException, JMSException, ApexEventException { // prepare ApexJmsConsumer jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); consumerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); @@ -272,8 +259,7 @@ public class ApexJmsConsumerTest { Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getConsumerTopic()); - Mockito.doReturn(connection) - .when(connectionFactory) + closeable = Mockito.doReturn(connection).when(connectionFactory) .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), jmsCarrierTechnologyParameters.getSecurityCredentials()); Mockito.doNothing().when(connection).start(); @@ -290,7 +276,7 @@ public class ApexJmsConsumerTest { } @Test - public void testStop() throws NamingException, JMSException, ApexEventException { + void testStop() throws NamingException, JMSException, ApexEventException { // prepare ApexJmsConsumer jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); consumerParameters.setCarrierTechnologyParameters(jmsCarrierTechnologyParameters); @@ -302,8 +288,7 @@ public class ApexJmsConsumerTest { Mockito.doReturn(connectionFactory).when(context).lookup(jmsCarrierTechnologyParameters.getConnectionFactory()); Mockito.doReturn(topic).when(context).lookup(jmsCarrierTechnologyParameters.getConsumerTopic()); - Mockito.doReturn(connection) - .when(connectionFactory) + closeable = Mockito.doReturn(connection).when(connectionFactory) .createConnection(jmsCarrierTechnologyParameters.getSecurityPrincipal(), jmsCarrierTechnologyParameters.getSecurityCredentials()); Mockito.doNothing().when(connection).start(); @@ -321,7 +306,14 @@ public class ApexJmsConsumerTest { } @Test - public void testStopNoJmsProperties() { + void testStopNoJmsProperties() { assertThatThrownBy(apexJmsConsumer::stop).isInstanceOf(NullPointerException.class); } + + @AfterEach + void after() throws Exception { + if (closeable != null) { + closeable.close(); + } + } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsProducerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsProducerTest.java index ffc5c5b1e..35130648f 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsProducerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/ApexJmsProducerTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Samsung. All rights reserved. - * Modifications Copyright (C) 2019-2021, 2023 Nordix Foundation. + * Modifications Copyright (C) 2019-2021, 2023-2024 Nordix Foundation. * Modifications Copyright (C) 2022 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -25,8 +25,8 @@ package org.onap.policy.apex.plugins.event.carrier.jms; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; import jakarta.jms.Connection; import jakarta.jms.ConnectionFactory; @@ -43,9 +43,9 @@ import java.util.Random; import javax.naming.InitialContext; import javax.naming.NamingException; import org.apache.commons.lang3.RandomStringUtils; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.onap.policy.apex.service.engine.event.ApexEvent; import org.onap.policy.apex.service.engine.event.ApexEventException; @@ -71,14 +71,14 @@ public class ApexJmsProducerTest { * Set up testing. * */ - @Before + @BeforeEach public void setUp() { apexJmsConsumer = new ApexJmsConsumer(); producerParameters = new EventHandlerParameters(); apexJmsProducer = Mockito.spy(new ApexJmsProducer()); } - @After + @AfterEach public void tearDown() { // restore system.out System.setOut(out); diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/JmsCarrierTechnologyParametersTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/JmsCarrierTechnologyParametersTest.java index 36f2e31ce..e8c223482 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/JmsCarrierTechnologyParametersTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-jms/src/test/java/org/onap/policy/apex/plugins/event/carrier/jms/JmsCarrierTechnologyParametersTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Samsung. All rights reserved. - * Modifications Copyright (C) 2019,2021 Nordix Foundation. + * Modifications Copyright (C) 2019, 2021, 2024 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,32 +22,31 @@ package org.onap.policy.apex.plugins.event.carrier.jms; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Properties; import javax.naming.Context; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.onap.policy.common.parameters.ParameterRuntimeException; import org.onap.policy.common.parameters.ValidationResult; -public class JmsCarrierTechnologyParametersTest { +class JmsCarrierTechnologyParametersTest { JmsCarrierTechnologyParameters jmsCarrierTechnologyParameters = null; - Properties jmsProducerProperties = null; - Properties jmsConsumerProperties = null; ValidationResult result = null; - public static final String JMS_CARRIER_TECHNOLOGY_LABEL = "JMS"; + static final String JMS_CARRIER_TECHNOLOGY_LABEL = "JMS"; - public static final String JMS_EVENT_PRODUCER_PLUGIN_CLASS = ApexJmsProducer.class.getName(); + static final String JMS_EVENT_PRODUCER_PLUGIN_CLASS = ApexJmsProducer.class.getName(); - public static final String JMS_EVENT_CONSUMER_PLUGIN_CLASS = ApexJmsConsumer.class.getName(); + static final String JMS_EVENT_CONSUMER_PLUGIN_CLASS = ApexJmsConsumer.class.getName(); private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory"; private static final String DEFAULT_INITIAL_CTXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory"; @@ -61,13 +60,13 @@ public class JmsCarrierTechnologyParametersTest { * * @throws Exception on test set up errors. */ - @Before - public void setUp() throws Exception { + @BeforeEach + void setUp() throws Exception { jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters(); } @Test - public void testValidate() { + void testValidate() { result = jmsCarrierTechnologyParameters.validate(); assertNotNull(result); assertFalse(result.getStatus().isValid()); @@ -82,12 +81,12 @@ public class JmsCarrierTechnologyParametersTest { } @Test - public void testJmsCarrierTechnologyParameters() { + void testJmsCarrierTechnologyParameters() { assertNotNull(jmsCarrierTechnologyParameters); } @Test - public void testGetJmsProducerProperties() { + void testGetJmsProducerProperties() { Properties producerProperties = jmsCarrierTechnologyParameters.getJmsProducerProperties(); assertNotNull(producerProperties); @@ -117,7 +116,7 @@ public class JmsCarrierTechnologyParametersTest { } @Test - public void testGetJmsConsumerProperties() { + void testGetJmsConsumerProperties() { Properties consumerProperties = jmsCarrierTechnologyParameters.getJmsConsumerProperties(); assertNotNull(consumerProperties); assertNull(consumerProperties.get(Context.SECURITY_CREDENTIALS)); @@ -128,24 +127,24 @@ public class JmsCarrierTechnologyParametersTest { } @Test - public void testEqualityOfJmsConsumerAndProducerProperties() { + void testEqualityOfJmsConsumerAndProducerProperties() { assertEquals(jmsCarrierTechnologyParameters.getJmsProducerProperties(), - jmsCarrierTechnologyParameters.getJmsConsumerProperties()); + jmsCarrierTechnologyParameters.getJmsConsumerProperties()); } @Test - public void testGetConnectionFactory() { + void testGetConnectionFactory() { assertEquals(DEFAULT_CONNECTION_FACTORY, jmsCarrierTechnologyParameters.getConnectionFactory()); } @Test - public void testSetConnectionFactory() { + void testSetConnectionFactory() { jmsCarrierTechnologyParameters.setConnectionFactory("QueueConnectionFactory"); assertNotEquals(DEFAULT_CONNECTION_FACTORY, jmsCarrierTechnologyParameters.getConnectionFactory()); } @Test - public void testSetConsumerTopic() { + void testSetConsumerTopic() { assertEquals(DEFAULT_CONSUMER_TOPIC, jmsCarrierTechnologyParameters.getConsumerTopic()); jmsCarrierTechnologyParameters.setConsumerTopic(null); result = jmsCarrierTechnologyParameters.validate(); @@ -153,28 +152,28 @@ public class JmsCarrierTechnologyParametersTest { } @Test - public void testSetConsumerWaitTime() { + void testSetConsumerWaitTime() { assertEquals(DEFAULT_CONSUMER_WAIT_TIME, jmsCarrierTechnologyParameters.getConsumerWaitTime()); jmsCarrierTechnologyParameters.setConsumerWaitTime(-1); assertNotEquals(DEFAULT_CONSUMER_WAIT_TIME, jmsCarrierTechnologyParameters.getConsumerWaitTime()); } @Test - public void testSetEventConsumerPluginClass() { + void testSetEventConsumerPluginClass() { assertEquals(JMS_EVENT_CONSUMER_PLUGIN_CLASS, jmsCarrierTechnologyParameters.getEventConsumerPluginClass()); jmsCarrierTechnologyParameters.setEventConsumerPluginClass("TestEventConsumerPluginClass"); assertNotEquals(JMS_EVENT_CONSUMER_PLUGIN_CLASS, jmsCarrierTechnologyParameters.getEventConsumerPluginClass()); } @Test - public void testSetEventProducerPluginClass() { + void testSetEventProducerPluginClass() { assertEquals(JMS_EVENT_PRODUCER_PLUGIN_CLASS, jmsCarrierTechnologyParameters.getEventProducerPluginClass()); jmsCarrierTechnologyParameters.setEventProducerPluginClass("TestEventProducerPluginClass"); assertNotEquals(JMS_EVENT_PRODUCER_PLUGIN_CLASS, jmsCarrierTechnologyParameters.getEventProducerPluginClass()); } @Test - public void testSetLabel() { + void testSetLabel() { assertEquals(JMS_CARRIER_TECHNOLOGY_LABEL, jmsCarrierTechnologyParameters.getLabel()); jmsCarrierTechnologyParameters.setLabel("TestLable"); assertNotEquals(JMS_CARRIER_TECHNOLOGY_LABEL, jmsCarrierTechnologyParameters.getLabel()); @@ -182,14 +181,14 @@ public class JmsCarrierTechnologyParametersTest { } @Test - public void testSetObjectMessageSending() { + void testSetObjectMessageSending() { assertTrue(jmsCarrierTechnologyParameters.isObjectMessageSending()); jmsCarrierTechnologyParameters.setObjectMessageSending(!DEFAULT_TO_OBJECT_MSG_SENDING); assertFalse(jmsCarrierTechnologyParameters.isObjectMessageSending()); } @Test - public void testSetProducerTopic() { + void testSetProducerTopic() { assertEquals(DEFAULT_PRODUCER_TOPIC, jmsCarrierTechnologyParameters.getProducerTopic()); jmsCarrierTechnologyParameters.setProducerTopic(""); result = jmsCarrierTechnologyParameters.validate(); @@ -197,7 +196,7 @@ public class JmsCarrierTechnologyParametersTest { } @Test - public void testSetProviderUrl() { + void testSetProviderUrl() { assertNull(jmsCarrierTechnologyParameters.getProviderUrl()); jmsCarrierTechnologyParameters.setProviderUrl(null); result = jmsCarrierTechnologyParameters.validate(); @@ -205,7 +204,7 @@ public class JmsCarrierTechnologyParametersTest { } @Test - public void testSetSecurityCredentials() { + void testSetSecurityCredentials() { assertNull(jmsCarrierTechnologyParameters.getSecurityCredentials()); jmsCarrierTechnologyParameters.setSecurityCredentials(""); result = jmsCarrierTechnologyParameters.validate(); @@ -213,7 +212,7 @@ public class JmsCarrierTechnologyParametersTest { } @Test - public void testSetSecurityPrincipal() { + void testSetSecurityPrincipal() { assertNull(jmsCarrierTechnologyParameters.getSecurityPrincipal()); jmsCarrierTechnologyParameters.setSecurityPrincipal(null); result = jmsCarrierTechnologyParameters.validate(); @@ -221,7 +220,7 @@ public class JmsCarrierTechnologyParametersTest { } @Test - public void testSetInitialContextFactory() { + void testSetInitialContextFactory() { assertEquals(DEFAULT_INITIAL_CTXT_FACTORY, jmsCarrierTechnologyParameters.getInitialContextFactory()); @@ -233,8 +232,8 @@ public class JmsCarrierTechnologyParametersTest { assertNotEquals(DEFAULT_INITIAL_CTXT_FACTORY, jmsCarrierTechnologyParameters.getInitialContextFactory()); } - @Test(expected = ParameterRuntimeException.class) - public void testSetName() { - jmsCarrierTechnologyParameters.setName("TestName"); + @Test + void testSetName() { + assertThrows(ParameterRuntimeException.class, () -> jmsCarrierTechnologyParameters.setName("TestName")); } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/test/java/org/onap/policy/apex/plugins/event/carrier/kafka/ApexKafkaConsumerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/test/java/org/onap/policy/apex/plugins/event/carrier/kafka/ApexKafkaConsumerTest.java index 1ef3550e4..f1e420bf0 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/test/java/org/onap/policy/apex/plugins/event/carrier/kafka/ApexKafkaConsumerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/test/java/org/onap/policy/apex/plugins/event/carrier/kafka/ApexKafkaConsumerTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Samsung. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation + * Modifications Copyright (C) 2020, 2024 Nordix Foundation * Modifications Copyright (C) 2022 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,12 +23,13 @@ package org.onap.policy.apex.plugins.event.carrier.kafka; import static org.assertj.core.api.Assertions.assertThatCode; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.onap.policy.apex.service.engine.event.ApexEventException; import org.onap.policy.apex.service.engine.event.ApexEventProducer; import org.onap.policy.apex.service.engine.event.ApexEventReceiver; @@ -37,7 +38,7 @@ import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnolo import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; -public class ApexKafkaConsumerTest { +class ApexKafkaConsumerTest { ApexKafkaConsumer apexKafkaConsumer = null; ApexKafkaConsumer apexKafkaConsumer2 = null; EventHandlerParameters consumerParameters = null; @@ -51,13 +52,14 @@ public class ApexKafkaConsumerTest { * * @throws ApexEventException on test set up errors. */ - @Before - public void setUp() throws ApexEventException { + @BeforeEach + void setUp() throws ApexEventException { apexKafkaConsumer = new ApexKafkaConsumer(); consumerParameters = new EventHandlerParameters(); apexKafkaProducer = new ApexKafkaProducer(); consumerParameters - .setCarrierTechnologyParameters(new KafkaCarrierTechnologyParameters() {}); + .setCarrierTechnologyParameters(new KafkaCarrierTechnologyParameters() { + }); apexKafkaConsumer.init("TestApexKafkaConsumer", consumerParameters, incomingEventReceiver); apexKafkaConsumer2 = new ApexKafkaConsumer(); @@ -65,63 +67,66 @@ public class ApexKafkaConsumerTest { kafkaParameters = new KafkaCarrierTechnologyParameters(); String[][] kafkaProperties = { {"value.deserializer", "io.confluent.kafka.serializers.KafkaAvroDeserializer"}, - {"schema.registry.url", "[http://test-registory:8080]"} + {"schema.registry.url", "[https://test-registory:8080]"} }; kafkaParameters.setKafkaProperties(kafkaProperties); consumerParameters2 - .setCarrierTechnologyParameters(kafkaParameters); + .setCarrierTechnologyParameters(kafkaParameters); apexKafkaConsumer2.init("TestApexKafkaConsumer2", consumerParameters2, incomingEventReceiver); } @Test - public void testStart() { + void testStart() { assertThatCode(apexKafkaConsumer::start).doesNotThrowAnyException(); assertThatCode(apexKafkaConsumer2::start).doesNotThrowAnyException(); } @Test - public void testGetName() { + void testGetName() { assertEquals("TestApexKafkaConsumer", apexKafkaConsumer.getName()); assertEquals("TestApexKafkaConsumer2", apexKafkaConsumer2.getName()); } @Test - public void testGetPeeredReference() { + void testGetPeeredReference() { assertNull(apexKafkaConsumer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR)); assertNull(apexKafkaConsumer2.getPeeredReference(EventHandlerPeeredMode.REQUESTOR)); } @Test - public void testSetPeeredReference() { + void testSetPeeredReference() { PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR, - apexKafkaConsumer, apexKafkaProducer); + apexKafkaConsumer, apexKafkaProducer); apexKafkaConsumer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, peeredReference); assertNotNull(apexKafkaConsumer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR)); PeeredReference peeredReference2 = new PeeredReference(EventHandlerPeeredMode.REQUESTOR, - apexKafkaConsumer2, apexKafkaProducer); + apexKafkaConsumer2, apexKafkaProducer); apexKafkaConsumer2.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, peeredReference2); assertNotNull(apexKafkaConsumer2.getPeeredReference(EventHandlerPeeredMode.REQUESTOR)); } - @Test(expected = java.lang.NullPointerException.class) - public void testRun() { - apexKafkaConsumer.run(); - apexKafkaConsumer2.run(); + @Test + void testRun() { + assertThrows(NullPointerException.class, () -> apexKafkaConsumer.run()); + assertThrows(NullPointerException.class, () -> apexKafkaConsumer2.run()); } - @Test(expected = java.lang.NullPointerException.class) - public void testStop() { - apexKafkaConsumer.stop(); - apexKafkaConsumer2.stop(); + @Test + void testStop() { + assertThrows(NullPointerException.class, () -> apexKafkaConsumer.stop()); + assertThrows(NullPointerException.class, () -> apexKafkaConsumer2.stop()); } - @Test(expected = ApexEventException.class) - public void testInitWithNonKafkaCarrierTechnologyParameters() throws ApexEventException { - consumerParameters.setCarrierTechnologyParameters(new CarrierTechnologyParameters() {}); - apexKafkaConsumer.init("TestApexKafkaConsumer", consumerParameters, incomingEventReceiver); - apexKafkaConsumer2.init("TestApexKafkaConsumer2", consumerParameters, incomingEventReceiver); + @Test + void testInitWithNonKafkaCarrierTechnologyParameters() { + consumerParameters.setCarrierTechnologyParameters(new CarrierTechnologyParameters() { + }); + assertThrows(ApexEventException.class, () -> + apexKafkaConsumer.init("TestApexKafkaConsumer", consumerParameters, incomingEventReceiver)); + assertThrows(ApexEventException.class, () -> + apexKafkaConsumer2.init("TestApexKafkaConsumer2", consumerParameters, incomingEventReceiver)); } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/test/java/org/onap/policy/apex/plugins/event/carrier/kafka/ApexKafkaProducerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/test/java/org/onap/policy/apex/plugins/event/carrier/kafka/ApexKafkaProducerTest.java index 7300474c7..f5606eb57 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/test/java/org/onap/policy/apex/plugins/event/carrier/kafka/ApexKafkaProducerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/test/java/org/onap/policy/apex/plugins/event/carrier/kafka/ApexKafkaProducerTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Samsung. All rights reserved. + * Modifications Copyright (C) 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,19 +21,20 @@ package org.onap.policy.apex.plugins.event.carrier.kafka; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.onap.policy.apex.service.engine.event.ApexEventException; import org.onap.policy.apex.service.engine.event.PeeredReference; import org.onap.policy.apex.service.engine.event.SynchronousEventCache; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; -public class ApexKafkaProducerTest { +class ApexKafkaProducerTest { ApexKafkaProducer apexKafkaProducer = null; ApexKafkaConsumer apexKafkaConsumer = null; EventHandlerParameters producerParameters = null; @@ -43,37 +45,38 @@ public class ApexKafkaProducerTest { /** * Set up testing. */ - @Before - public void setUp() throws Exception { + @BeforeEach + void setUp() { apexKafkaProducer = new ApexKafkaProducer(); apexKafkaConsumer = new ApexKafkaConsumer(); producerParameters = new EventHandlerParameters(); } - @Test(expected = ApexEventException.class) - public void testInit() throws ApexEventException { - apexKafkaProducer.init("TestApexKafkaProducer", producerParameters); + @Test + void testInit() { + assertThrows(ApexEventException.class, + () -> apexKafkaProducer.init("TestApexKafkaProducer", producerParameters)); } @Test - public void testGetName() { + void testGetName() { assertNull(apexKafkaProducer.getName()); } @Test - public void testGetPeeredReference() { + void testGetPeeredReference() { assertNull(apexKafkaProducer.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); } @Test - public void testWithProperValues() throws ApexEventException { + void testWithProperValues() throws ApexEventException { producerParameters - .setCarrierTechnologyParameters(new KafkaCarrierTechnologyParameters() {}); + .setCarrierTechnologyParameters(new KafkaCarrierTechnologyParameters() { }); synchronousEventCache = new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, - apexKafkaConsumer, apexKafkaProducer, DEFAULT_SYNCHRONOUS_EVENT_TIMEOUT); + apexKafkaConsumer, apexKafkaProducer, DEFAULT_SYNCHRONOUS_EVENT_TIMEOUT); apexKafkaProducer.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, - synchronousEventCache); + synchronousEventCache); apexKafkaProducer.init("TestApexKafkaProducer", producerParameters); assertEquals("TestApexKafkaProducer", apexKafkaProducer.getName()); assertNotNull(apexKafkaProducer.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/test/java/org/onap/policy/apex/plugins/event/carrier/kafka/KafkaCarrierTechnologyParametersTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/test/java/org/onap/policy/apex/plugins/event/carrier/kafka/KafkaCarrierTechnologyParametersTest.java index 6b0f7d920..11bafc9b1 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/test/java/org/onap/policy/apex/plugins/event/carrier/kafka/KafkaCarrierTechnologyParametersTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-kafka/src/test/java/org/onap/policy/apex/plugins/event/carrier/kafka/KafkaCarrierTechnologyParametersTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Samsung. All rights reserved. - * Modifications Copyright (C) 2019,2023 Nordix Foundation. + * Modifications Copyright (C) 2019, 2023-2024 Nordix Foundation. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,17 +22,18 @@ package org.onap.policy.apex.plugins.event.carrier.kafka; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Properties; -import org.junit.Test; +import org.junit.jupiter.api.Test; -public class KafkaCarrierTechnologyParametersTest { +class KafkaCarrierTechnologyParametersTest { @Test - public void testKafkaCarrierTechnologyParameters() { + void testKafkaCarrierTechnologyParameters() { KafkaCarrierTechnologyParameters kafkaCarrierTechnologyParameters = new KafkaCarrierTechnologyParameters(); assertNotNull(kafkaCarrierTechnologyParameters); @@ -40,17 +41,17 @@ public class KafkaCarrierTechnologyParametersTest { } @Test - public void testGetKafkaProducerProperties() { + void testGetKafkaProducerProperties() { KafkaCarrierTechnologyParameters kafkaCarrierTechnologyParameters = new KafkaCarrierTechnologyParameters(); Properties kafkaProducerProperties = kafkaCarrierTechnologyParameters.getKafkaProducerProperties(); assertNotNull(kafkaProducerProperties); assertEquals("localhost:9092", kafkaProducerProperties.get("bootstrap.servers")); assertEquals("1", kafkaProducerProperties.get("linger.ms")); - assertEquals(null, kafkaProducerProperties.get("group.id")); - assertEquals(null, kafkaProducerProperties.get("Property0")); - assertEquals(null, kafkaProducerProperties.get("Property1")); - assertEquals(null, kafkaProducerProperties.get("Property2")); + assertNull(kafkaProducerProperties.get("group.id")); + assertNull(kafkaProducerProperties.get("Property0")); + assertNull(kafkaProducerProperties.get("Property1")); + assertNull(kafkaProducerProperties.get("Property2")); // @formatter:off String[][] kafkaProperties = { @@ -68,24 +69,24 @@ public class KafkaCarrierTechnologyParametersTest { assertNotNull(kafkaProducerProperties); assertEquals("localhost:9092", kafkaProducerProperties.get("bootstrap.servers")); assertEquals("1", kafkaProducerProperties.get("linger.ms")); - assertEquals(null, kafkaProducerProperties.get("group.id")); + assertNull(kafkaProducerProperties.get("group.id")); assertEquals("Value0", kafkaProducerProperties.get("Property0")); assertEquals("Value1", kafkaProducerProperties.get("Property1")); - assertEquals(null, kafkaProducerProperties.get("Property2")); + assertNull(kafkaProducerProperties.get("Property2")); } @Test - public void testGetKafkaConsumerProperties() { + void testGetKafkaConsumerProperties() { KafkaCarrierTechnologyParameters kafkaCarrierTechnologyParameters = new KafkaCarrierTechnologyParameters(); Properties kafkaConsumerProperties = kafkaCarrierTechnologyParameters.getKafkaConsumerProperties(); assertNotNull(kafkaConsumerProperties); assertEquals("localhost:9092", kafkaConsumerProperties.get("bootstrap.servers")); assertEquals("default-group-id", kafkaConsumerProperties.get("group.id")); - assertEquals(null, kafkaConsumerProperties.get("linger.ms")); - assertEquals(null, kafkaConsumerProperties.get("Property0")); - assertEquals(null, kafkaConsumerProperties.get("Property1")); - assertEquals(null, kafkaConsumerProperties.get("Property2")); + assertNull(kafkaConsumerProperties.get("linger.ms")); + assertNull(kafkaConsumerProperties.get("Property0")); + assertNull(kafkaConsumerProperties.get("Property1")); + assertNull(kafkaConsumerProperties.get("Property2")); // @formatter:off String[][] kafkaProperties = { @@ -103,204 +104,30 @@ public class KafkaCarrierTechnologyParametersTest { assertNotNull(kafkaConsumerProperties); assertEquals("localhost:9092", kafkaConsumerProperties.get("bootstrap.servers")); assertEquals("default-group-id", kafkaConsumerProperties.get("group.id")); - assertEquals(null, kafkaConsumerProperties.get("linger.ms")); + assertNull(kafkaConsumerProperties.get("linger.ms")); assertEquals("Value0", kafkaConsumerProperties.get("Property0")); assertEquals("Value1", kafkaConsumerProperties.get("Property1")); - assertEquals(null, kafkaConsumerProperties.get("Property2")); + assertNull(kafkaConsumerProperties.get("Property2")); } @Test - public void testValidate() { + void testValidate() { KafkaCarrierTechnologyParameters kafkaCarrierTechnologyParameters = new KafkaCarrierTechnologyParameters(); assertNotNull(kafkaCarrierTechnologyParameters); assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - String origStringValue = kafkaCarrierTechnologyParameters.getBootstrapServers(); - kafkaCarrierTechnologyParameters.setBootstrapServers(" "); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setBootstrapServers(origStringValue); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - origStringValue = kafkaCarrierTechnologyParameters.getAcks(); - kafkaCarrierTechnologyParameters.setAcks(" "); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setAcks(origStringValue); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - origStringValue = kafkaCarrierTechnologyParameters.getGroupId(); - kafkaCarrierTechnologyParameters.setGroupId(" "); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setGroupId(origStringValue); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - origStringValue = kafkaCarrierTechnologyParameters.getProducerTopic(); - kafkaCarrierTechnologyParameters.setProducerTopic(" "); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setProducerTopic(origStringValue); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - int origIntValue = kafkaCarrierTechnologyParameters.getRetries(); - kafkaCarrierTechnologyParameters.setRetries(-1); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setRetries(origIntValue); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - origIntValue = kafkaCarrierTechnologyParameters.getBatchSize(); - kafkaCarrierTechnologyParameters.setBatchSize(-1); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setBatchSize(origIntValue); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - origIntValue = kafkaCarrierTechnologyParameters.getLingerTime(); - kafkaCarrierTechnologyParameters.setLingerTime(-1); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setLingerTime(origIntValue); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - long origLongValue = kafkaCarrierTechnologyParameters.getBufferMemory(); - kafkaCarrierTechnologyParameters.setBufferMemory(-1); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setBufferMemory(origLongValue); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - origIntValue = kafkaCarrierTechnologyParameters.getAutoCommitTime(); - kafkaCarrierTechnologyParameters.setAutoCommitTime(-1); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setAutoCommitTime(origIntValue); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - origIntValue = kafkaCarrierTechnologyParameters.getSessionTimeout(); - kafkaCarrierTechnologyParameters.setSessionTimeout(-1); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setSessionTimeout(origIntValue); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - origIntValue = kafkaCarrierTechnologyParameters.getConsumerPollTime(); - kafkaCarrierTechnologyParameters.setConsumerPollTime(-1); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setConsumerPollTime(origIntValue); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - origStringValue = kafkaCarrierTechnologyParameters.getKeySerializer(); - kafkaCarrierTechnologyParameters.setKeySerializer(" "); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setKeySerializer(origStringValue); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - origStringValue = kafkaCarrierTechnologyParameters.getValueSerializer(); - kafkaCarrierTechnologyParameters.setValueSerializer(" "); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setValueSerializer(origStringValue); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - origStringValue = kafkaCarrierTechnologyParameters.getKeyDeserializer(); - kafkaCarrierTechnologyParameters.setKeyDeserializer(" "); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setKeyDeserializer(origStringValue); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - origStringValue = kafkaCarrierTechnologyParameters.getValueDeserializer(); - kafkaCarrierTechnologyParameters.setValueDeserializer(" "); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setValueDeserializer(origStringValue); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - String[] origConsumerTopcList = kafkaCarrierTechnologyParameters.getConsumerTopicList(); - kafkaCarrierTechnologyParameters.setConsumerTopicList(null); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setConsumerTopicList(origConsumerTopcList); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - kafkaCarrierTechnologyParameters.setConsumerTopicList(new String[0]); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setConsumerTopicList(origConsumerTopcList); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - String[] blankStringList = { null, "" }; - kafkaCarrierTechnologyParameters.setConsumerTopicList(blankStringList); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setConsumerTopicList(origConsumerTopcList); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - String[][] origKafkaProperties = kafkaCarrierTechnologyParameters.getKafkaProperties(); - kafkaCarrierTechnologyParameters.setKafkaProperties(null); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setKafkaProperties(origKafkaProperties); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); + assertValidateStringProperties(kafkaCarrierTechnologyParameters); - kafkaCarrierTechnologyParameters.setKafkaProperties(new String[0][0]); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setKafkaProperties(origKafkaProperties); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); + assertValidateNumberProperties(kafkaCarrierTechnologyParameters); - // @formatter:offkafkaCarrierTechnologyParameters - String[][] kafkaProperties0 = { - { - null, "Value0" - } - }; - // @formatter:on + assertValidateTopicList(kafkaCarrierTechnologyParameters); - kafkaCarrierTechnologyParameters.setKafkaProperties(kafkaProperties0); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setKafkaProperties(origKafkaProperties); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - // @formatter:off - String[][] kafkaProperties1 = { - { - "Property1", null - } - }; - // @formatter:on - - kafkaCarrierTechnologyParameters.setKafkaProperties(kafkaProperties1); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setKafkaProperties(origKafkaProperties); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - // @formatter:off - String[][] kafkaProperties2 = { - { - "Property1", null - } - }; - // @formatter:on - - kafkaCarrierTechnologyParameters.setKafkaProperties(kafkaProperties2); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - - // @formatter:off - String[][] kafkaPropertiesWithEmptyValue = { - { - "Property1", "" - } - }; - // @formatter:on - - kafkaCarrierTechnologyParameters.setKafkaProperties(kafkaPropertiesWithEmptyValue); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - kafkaCarrierTechnologyParameters.setKafkaProperties(origKafkaProperties); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); - - // @formatter:off - String[][] kafkaProperties3 = { - { - "Property1", "Value0", "Value1" - } - }; - // @formatter:on - - kafkaCarrierTechnologyParameters.setKafkaProperties(kafkaProperties3); - assertFalse(kafkaCarrierTechnologyParameters.validate().isValid()); - kafkaCarrierTechnologyParameters.setKafkaProperties(origKafkaProperties); - assertTrue(kafkaCarrierTechnologyParameters.validate().isValid()); + assertValidateKafkaProperties(kafkaCarrierTechnologyParameters); } @Test - public void testExplicitImplicit() { + void testExplicitImplicit() { KafkaCarrierTechnologyParameters kafkaCtp = new KafkaCarrierTechnologyParameters(); assertNotNull(kafkaCtp); @@ -378,4 +205,194 @@ public class KafkaCarrierTechnologyParametersTest { kafkaCtp.setKafkaProperties(kafkaProperties3); assertEquals("localhost:7777", kafkaCtp.getKafkaConsumerProperties().get("bootstrap.servers")); } + + private static void assertValidateStringProperties(KafkaCarrierTechnologyParameters kafkaParameters) { + String origStringValue = kafkaParameters.getBootstrapServers(); + kafkaParameters.setBootstrapServers(" "); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setBootstrapServers(origStringValue); + assertTrue(kafkaParameters.validate().isValid()); + + origStringValue = kafkaParameters.getAcks(); + kafkaParameters.setAcks(" "); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setAcks(origStringValue); + assertTrue(kafkaParameters.validate().isValid()); + + origStringValue = kafkaParameters.getGroupId(); + kafkaParameters.setGroupId(" "); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setGroupId(origStringValue); + assertTrue(kafkaParameters.validate().isValid()); + + origStringValue = kafkaParameters.getProducerTopic(); + kafkaParameters.setProducerTopic(" "); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setProducerTopic(origStringValue); + assertTrue(kafkaParameters.validate().isValid()); + + origStringValue = kafkaParameters.getKeySerializer(); + kafkaParameters.setKeySerializer(" "); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setKeySerializer(origStringValue); + assertTrue(kafkaParameters.validate().isValid()); + + origStringValue = kafkaParameters.getValueSerializer(); + kafkaParameters.setValueSerializer(" "); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setValueSerializer(origStringValue); + assertTrue(kafkaParameters.validate().isValid()); + + origStringValue = kafkaParameters.getKeyDeserializer(); + kafkaParameters.setKeyDeserializer(" "); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setKeyDeserializer(origStringValue); + assertTrue(kafkaParameters.validate().isValid()); + + origStringValue = kafkaParameters.getValueDeserializer(); + kafkaParameters.setValueDeserializer(" "); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setValueDeserializer(origStringValue); + assertTrue(kafkaParameters.validate().isValid()); + } + + private static void assertValidateTopicList(KafkaCarrierTechnologyParameters kafkaParameters) { + String[] origConsumerTopicList = kafkaParameters.getConsumerTopicList(); + kafkaParameters.setConsumerTopicList(null); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setConsumerTopicList(origConsumerTopicList); + assertTrue(kafkaParameters.validate().isValid()); + + kafkaParameters.setConsumerTopicList(new String[0]); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setConsumerTopicList(origConsumerTopicList); + assertTrue(kafkaParameters.validate().isValid()); + + String[] blankStringList = { null, "" }; + kafkaParameters.setConsumerTopicList(blankStringList); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setConsumerTopicList(origConsumerTopicList); + assertTrue(kafkaParameters.validate().isValid()); + } + + private static void assertValidateNumberProperties(KafkaCarrierTechnologyParameters kafkaParameters) { + int origIntValue = kafkaParameters.getRetries(); + kafkaParameters.setRetries(-1); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setRetries(origIntValue); + assertTrue(kafkaParameters.validate().isValid()); + + origIntValue = kafkaParameters.getBatchSize(); + kafkaParameters.setBatchSize(-1); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setBatchSize(origIntValue); + assertTrue(kafkaParameters.validate().isValid()); + + origIntValue = kafkaParameters.getLingerTime(); + kafkaParameters.setLingerTime(-1); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setLingerTime(origIntValue); + assertTrue(kafkaParameters.validate().isValid()); + + long origLongValue = kafkaParameters.getBufferMemory(); + kafkaParameters.setBufferMemory(-1); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setBufferMemory(origLongValue); + assertTrue(kafkaParameters.validate().isValid()); + + origIntValue = kafkaParameters.getAutoCommitTime(); + kafkaParameters.setAutoCommitTime(-1); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setAutoCommitTime(origIntValue); + assertTrue(kafkaParameters.validate().isValid()); + + origIntValue = kafkaParameters.getSessionTimeout(); + kafkaParameters.setSessionTimeout(-1); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setSessionTimeout(origIntValue); + assertTrue(kafkaParameters.validate().isValid()); + + origIntValue = kafkaParameters.getConsumerPollTime(); + kafkaParameters.setConsumerPollTime(-1); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setConsumerPollTime(origIntValue); + assertTrue(kafkaParameters.validate().isValid()); + } + + private static void assertValidateKafkaProperties(KafkaCarrierTechnologyParameters kafkaParameters) { + String[][] origKafkaProperties = kafkaParameters.getKafkaProperties(); + kafkaParameters.setKafkaProperties(null); + assertTrue(kafkaParameters.validate().isValid()); + kafkaParameters.setKafkaProperties(origKafkaProperties); + assertTrue(kafkaParameters.validate().isValid()); + + kafkaParameters.setKafkaProperties(new String[0][0]); + assertTrue(kafkaParameters.validate().isValid()); + kafkaParameters.setKafkaProperties(origKafkaProperties); + assertTrue(kafkaParameters.validate().isValid()); + + // @formatter:off + String[][] kafkaProperties0 = { + { + null, "Value0" + } + }; + // @formatter:on + + kafkaParameters.setKafkaProperties(kafkaProperties0); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setKafkaProperties(origKafkaProperties); + assertTrue(kafkaParameters.validate().isValid()); + + // @formatter:off + String[][] kafkaProperties1 = { + { + "Property1", null + } + }; + // @formatter:on + + kafkaParameters.setKafkaProperties(kafkaProperties1); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setKafkaProperties(origKafkaProperties); + assertTrue(kafkaParameters.validate().isValid()); + + // @formatter:off + String[][] kafkaProperties2 = { + { + "Property1", null + } + }; + // @formatter:on + + kafkaParameters.setKafkaProperties(kafkaProperties2); + assertFalse(kafkaParameters.validate().isValid()); + + // @formatter:off + String[][] kafkaPropertiesWithEmptyValue = { + { + "Property1", "" + } + }; + // @formatter:on + + kafkaParameters.setKafkaProperties(kafkaPropertiesWithEmptyValue); + assertTrue(kafkaParameters.validate().isValid()); + + kafkaParameters.setKafkaProperties(origKafkaProperties); + assertTrue(kafkaParameters.validate().isValid()); + + // @formatter:off + String[][] kafkaProperties3 = { + { + "Property1", "Value0", "Value1" + } + }; + // @formatter:on + + kafkaParameters.setKafkaProperties(kafkaProperties3); + assertFalse(kafkaParameters.validate().isValid()); + kafkaParameters.setKafkaProperties(origKafkaProperties); + assertTrue(kafkaParameters.validate().isValid()); + } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConusmerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConsumerTest.java index 61155c8cf..b3a6bca06 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConusmerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConsumerTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2020, 2023 Nordix Foundation. + * Modifications Copyright (C) 2019-2020, 2023-2024 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,9 +24,10 @@ package org.onap.policy.apex.plugins.event.carrier.restclient; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.awaitility.Awaitility.await; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import jakarta.ws.rs.client.Client; import jakarta.ws.rs.client.Invocation.Builder; @@ -35,22 +36,23 @@ import jakarta.ws.rs.core.Response; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.util.concurrent.TimeUnit; -import org.junit.After; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.onap.policy.apex.service.engine.event.ApexEventException; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; /** - * This class tests the ApexRestClientConusmer class. - * + * This class tests the ApexRestClientConsumer class. */ -@RunWith(MockitoJUnitRunner.class) -public class ApexRestClientConusmerTest { +@ExtendWith(MockitoExtension.class) +class ApexRestClientConsumerTest { private final PrintStream stdout = System.out; @Mock @@ -65,13 +67,13 @@ public class ApexRestClientConusmerTest { @Mock private Response responseMock; - @After - public void after() { + @AfterEach + void after() { System.setOut(stdout); } @Test - public void testApexRestClientConsumerErrors() throws ApexEventException { + void testApexRestClientConsumerErrors() throws ApexEventException { ApexRestClientConsumer arcc = new ApexRestClientConsumer(); assertNotNull(arcc); @@ -99,7 +101,7 @@ public class ApexRestClientConusmerTest { assertEquals("RestClientConsumer", arcc.getName()); arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); - assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + assertNull(arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); rcctp.setUrl("http://some.place.that.does.not/exist"); Mockito.doReturn(builderMock).when(targetMock).request("application/json"); @@ -120,7 +122,7 @@ public class ApexRestClientConusmerTest { } @Test - public void testApexRestClientConsumerHttpError() throws ApexEventException { + void testApexRestClientConsumerHttpError() throws ApexEventException { ApexRestClientConsumer arcc = new ApexRestClientConsumer(); assertNotNull(arcc); @@ -139,7 +141,7 @@ public class ApexRestClientConusmerTest { assertEquals("RestClientConsumer", arcc.getName()); arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); - assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + assertNull(arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); Mockito.doReturn(Response.Status.BAD_REQUEST.getStatusCode()).when(responseMock).getStatus(); Mockito.doReturn(responseMock).when(builderMock).get(); @@ -159,7 +161,7 @@ public class ApexRestClientConusmerTest { } @Test - public void testApexRestClientConsumerJsonError() throws ApexEventException { + void testApexRestClientConsumerJsonError() throws ApexEventException { ApexRestClientConsumer arcc = new ApexRestClientConsumer(); assertNotNull(arcc); @@ -175,7 +177,7 @@ public class ApexRestClientConusmerTest { arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); - assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + assertNull(arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); rcctp.setUrl("http://some.place.that.does.not/exist"); Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); @@ -197,7 +199,7 @@ public class ApexRestClientConusmerTest { } @Test - public void testApexRestClientConsumerJsonEmpty() throws ApexEventException { + void testApexRestClientConsumerJsonEmpty() throws ApexEventException { ApexRestClientConsumer arcc = new ApexRestClientConsumer(); assertNotNull(arcc); @@ -214,7 +216,7 @@ public class ApexRestClientConusmerTest { arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); - assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + assertNull(arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); rcctp.setUrl("http://some.place.that.does.not/exist"); Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); @@ -242,7 +244,7 @@ public class ApexRestClientConusmerTest { } @Test - public void testApexRestClientConsumerJsonOk() throws ApexEventException { + void testApexRestClientConsumerJsonOk() throws ApexEventException { ApexRestClientConsumer arcc = new ApexRestClientConsumer(); assertNotNull(arcc); @@ -259,7 +261,7 @@ public class ApexRestClientConusmerTest { arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); - assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + assertNull(arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); rcctp.setUrl("http://some.place.that.does.not/exist"); Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); @@ -278,7 +280,7 @@ public class ApexRestClientConusmerTest { } @Test - public void testApexRestClientConsumerInvalidStatusCode() throws ApexEventException { + void testApexRestClientConsumerInvalidStatusCode() throws ApexEventException { ApexRestClientConsumer arcc = new ApexRestClientConsumer(); assertNotNull(arcc); @@ -295,7 +297,7 @@ public class ApexRestClientConusmerTest { arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); - assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + assertNull(arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); rcctp.setUrl("http://some.place.that.does.not/exist"); Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java index cc5ebbb9f..ec454af00 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2020, 2023 Nordix Foundation. + * Modifications Copyright (C) 2019-2020, 2023-2024 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,19 +23,23 @@ package org.onap.policy.apex.plugins.event.carrier.restclient; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import jakarta.ws.rs.client.Client; import jakarta.ws.rs.client.Invocation.Builder; import jakarta.ws.rs.client.WebTarget; import jakarta.ws.rs.core.Response; import java.util.Properties; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.onap.policy.apex.service.engine.event.ApexEventConsumer; import org.onap.policy.apex.service.engine.event.ApexEventException; import org.onap.policy.apex.service.engine.event.SynchronousEventCache; @@ -47,8 +51,8 @@ import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMo * Test the ApexRestClientProducer class. * */ -@RunWith(MockitoJUnitRunner.class) -public class ApexRestClientProducerTest { +@ExtendWith(MockitoExtension.class) +class ApexRestClientProducerTest { @Mock private Client httpClientMock; @@ -61,8 +65,10 @@ public class ApexRestClientProducerTest { @Mock private Response responseMock; + AutoCloseable closeable; + @Test - public void testApexRestClientProducerErrors() throws ApexEventException { + void testApexRestClientProducerErrors() throws ApexEventException { ApexRestClientProducer arcp = new ApexRestClientProducer(); assertNotNull(arcp); @@ -84,7 +90,7 @@ public class ApexRestClientProducerTest { assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); assertEquals("RestClientConsumer", arcp.getName()); arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); - assertEquals(null, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + assertNull(arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); arcp.stop(); rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.POST); @@ -92,7 +98,7 @@ public class ApexRestClientProducerTest { assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); assertEquals("RestClientConsumer", arcp.getName()); arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); - assertEquals(null, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + assertNull(arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); arcp.stop(); rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.PUT); @@ -100,12 +106,12 @@ public class ApexRestClientProducerTest { assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.PUT, rcctp.getHttpMethod()); assertEquals("RestClientConsumer", arcp.getName()); arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); - assertEquals(null, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + assertNull(arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); arcp.stop(); } @Test - public void testApexRestClientProducerPutEvent() throws ApexEventException { + void testApexRestClientProducerPutEvent() throws ApexEventException { ApexRestClientProducer arcp = new ApexRestClientProducer(); assertNotNull(arcp); @@ -120,7 +126,7 @@ public class ApexRestClientProducerTest { rcctp.setUrl("http://some.place.that.does.not/exist"); Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); - Mockito.doReturn(responseMock).when(builderMock).put(Mockito.any()); + closeable = Mockito.doReturn(responseMock).when(builderMock).put(Mockito.any()); Mockito.doReturn(builderMock).when(targetMock).request("application/json"); Mockito.doReturn(builderMock).when(builderMock).headers(Mockito.any()); Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); @@ -131,7 +137,7 @@ public class ApexRestClientProducerTest { } @Test - public void testApexRestClientProducerPostEventFail() throws ApexEventException { + void testApexRestClientProducerPostEventFail() throws ApexEventException { ApexRestClientProducer arcp = new ApexRestClientProducer(); assertNotNull(arcp); @@ -160,7 +166,7 @@ public class ApexRestClientProducerTest { } @Test - public void testApexRestClientProducerPostEventOK() throws ApexEventException { + void testApexRestClientProducerPostEventOK() throws ApexEventException { ApexRestClientProducer arcp = new ApexRestClientProducer(); assertNotNull(arcp); @@ -179,7 +185,7 @@ public class ApexRestClientProducerTest { properties.put("tag", "exist"); properties.put("key", "that"); Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); - Mockito.doReturn(responseMock).when(builderMock).put(Mockito.any()); + closeable = Mockito.doReturn(responseMock).when(builderMock).put(Mockito.any()); Mockito.doReturn(builderMock).when(targetMock).request("application/json"); Mockito.doReturn(builderMock).when(builderMock).headers(Mockito.any()); Mockito.doReturn(targetMock).when(httpClientMock).target("http://some.place.that.does.not/exist"); @@ -190,7 +196,7 @@ public class ApexRestClientProducerTest { } @Test - public void testApexRestClientProducerPostEventAccepted() throws ApexEventException { + void testApexRestClientProducerPostEventAccepted() throws ApexEventException { ApexRestClientProducer arcp = new ApexRestClientProducer(); assertNotNull(arcp); @@ -219,7 +225,7 @@ public class ApexRestClientProducerTest { } @Test - public void testApexRestClientProducerPostEventCache() throws ApexEventException { + void testApexRestClientProducerPostEventCache() throws ApexEventException { ApexRestClientProducer arcp = new ApexRestClientProducer(); assertNotNull(arcp); @@ -251,39 +257,7 @@ public class ApexRestClientProducerTest { } @Test - public void testApexRestClientProducerPostEventCacheTrace() throws ApexEventException { - ApexRestClientProducer arcp = new ApexRestClientProducer(); - assertNotNull(arcp); - - EventHandlerParameters producerParameters = new EventHandlerParameters(); - RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); - producerParameters.setCarrierTechnologyParameters(rcctp); - - rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.POST); - - ApexEventConsumer consumer = new ApexFileEventConsumer(); - SynchronousEventCache cache = - new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, arcp, 1000); - arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, cache); - assertEquals(cache, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); - arcp.init("RestClientConsumer", producerParameters); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); - assertEquals("RestClientConsumer", arcp.getName()); - - rcctp.setUrl("http://some.place.that.does.not/exist"); - Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); - Mockito.doReturn(responseMock).when(builderMock).post(Mockito.any()); - Mockito.doReturn(builderMock).when(targetMock).request("application/json"); - Mockito.doReturn(builderMock).when(builderMock).headers(Mockito.any()); - Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); - arcp.setClient(httpClientMock); - - arcp.sendEvent(123, null, "EventName", "This is an Event"); - arcp.stop(); - } - - @Test - public void testApexRestClientProducerHttpError() throws ApexEventException { + void testApexRestClientProducerHttpError() throws ApexEventException { ApexRestClientProducer arcp = new ApexRestClientProducer(); assertNotNull(arcp); @@ -298,7 +272,7 @@ public class ApexRestClientProducerTest { rcctp.setUrl("http://some.place.that.does.not/exist"); Mockito.doReturn(Response.Status.BAD_REQUEST.getStatusCode()).when(responseMock).getStatus(); - Mockito.doReturn(responseMock).when(builderMock).post(Mockito.any()); + closeable = Mockito.doReturn(responseMock).when(builderMock).post(Mockito.any()); Mockito.doReturn(builderMock).when(targetMock).request("application/json"); Mockito.doReturn(builderMock).when(builderMock).headers(Mockito.any()); Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); @@ -308,4 +282,11 @@ public class ApexRestClientProducerTest { .hasMessageContaining("send of event to URL \"http://some.place.that.does.not/exist\" using HTTP \"POST\" " + "failed with status code 400"); } + + @AfterEach + void after() throws Exception { + if (closeable != null) { + closeable.close(); + } + } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java index 58d265a97..4539c5181 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019-2020, 2024 Nordix Foundation. * Modifications Copyright (C) 2020 Bell Canada. All rights reserved. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ @@ -25,12 +25,13 @@ package org.onap.policy.apex.plugins.event.carrier.restclient; import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Set; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments; import org.onap.policy.apex.service.parameters.ApexParameterHandler; import org.onap.policy.apex.service.parameters.ApexParameters; @@ -39,10 +40,10 @@ import org.onap.policy.common.parameters.ParameterException; /** * Test REST client carrier technology parameters. */ -public class RestClientCarrierTechnologyParametersTest { +class RestClientCarrierTechnologyParametersTest { @Test - public void testRestClientCarrierTechnologyParametersBadList() { + void testRestClientCarrierTechnologyParametersBadList() { ApexCommandLineArguments arguments = new ApexCommandLineArguments(); arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderBadList.json"); arguments.setRelativeFileRoot("."); @@ -55,7 +56,7 @@ public class RestClientCarrierTechnologyParametersTest { } @Test - public void testRestClientCarrierTechnologyParametersNotKvPairs() { + void testRestClientCarrierTechnologyParametersNotKvPairs() { ApexCommandLineArguments arguments = new ApexCommandLineArguments(); arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNotKvPairs.json"); arguments.setRelativeFileRoot("."); @@ -67,7 +68,7 @@ public class RestClientCarrierTechnologyParametersTest { } @Test - public void testRestClientCarrierTechnologyParametersNulls() { + void testRestClientCarrierTechnologyParametersNulls() { ApexCommandLineArguments arguments = new ApexCommandLineArguments(); arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderNulls.json"); arguments.setRelativeFileRoot("."); @@ -79,7 +80,7 @@ public class RestClientCarrierTechnologyParametersTest { } @Test - public void testRestClientCarrierTechnologyParameterFilterInvalid() { + void testRestClientCarrierTechnologyParameterFilterInvalid() { ApexCommandLineArguments arguments = new ApexCommandLineArguments(); arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTClientWithHTTPFilterInvalid.json"); arguments.setRelativeFileRoot("."); @@ -92,7 +93,7 @@ public class RestClientCarrierTechnologyParametersTest { } @Test - public void testRestClientCarrierTechnologyParametersOk() throws ParameterException { + void testRestClientCarrierTechnologyParametersOk() throws ParameterException { ApexCommandLineArguments arguments = new ApexCommandLineArguments(); arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json"); arguments.setRelativeFileRoot("."); @@ -111,11 +112,11 @@ public class RestClientCarrierTechnologyParametersTest { assertEquals("fff", rrctp1.getHttpHeadersAsMultivaluedMap().get("eee").get(0)); rrctp1.setHttpHeaders(null); - assertEquals(null, rrctp1.getHttpHeadersAsMultivaluedMap()); + assertNull(rrctp1.getHttpHeadersAsMultivaluedMap()); } @Test - public void testRestClientCarrierTechnologyHttpCodeFilterOk() throws ParameterException { + void testRestClientCarrierTechnologyHttpCodeFilterOk() throws ParameterException { ApexCommandLineArguments arguments = new ApexCommandLineArguments(); arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json"); arguments.setRelativeFileRoot("."); @@ -128,7 +129,7 @@ public class RestClientCarrierTechnologyParametersTest { } @Test - public void testGettersAndSetters() { + void testGettersAndSetters() { RestClientCarrierTechnologyParameters rrctp = new RestClientCarrierTechnologyParameters(); rrctp.setUrl("http://some.where"); @@ -168,7 +169,7 @@ public class RestClientCarrierTechnologyParametersTest { } @Test - public void testUrlValidation() { + void testUrlValidation() { RestClientCarrierTechnologyParameters rrctp = new RestClientCarrierTechnologyParameters(); rrctp.setUrl("http://some.where.no.tag.in.url"); @@ -186,29 +187,27 @@ public class RestClientCarrierTechnologyParametersTest { assertEquals("ccc", rrctp.getHttpHeaders()[1][0]); assertEquals("ddd", rrctp.getHttpHeaders()[1][1]); - assertEquals(true, rrctp.validate().isValid()); + assertTrue(rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/is{that}.{one}"); - assertEquals(true, rrctp.validate().isValid()); + assertTrue(rrctp.validate().isValid()); Set<String> keymap = rrctp.getKeysFromUrl(); - assertEquals(true, keymap.contains("place")); - assertEquals(true, keymap.contains("that")); - assertEquals(true, keymap.contains("one")); + assertTrue(keymap.contains("place") && keymap.contains("that") && keymap.contains("one")); rrctp.setUrl("http://{place.{that}/{is}.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validate().isValid()); + assertFalse(rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/{is}.{not}/{what}.{exist"); - assertEquals(false, rrctp.validate().isValid()); + assertFalse(rrctp.validate().isValid()); rrctp.setUrl("http://place.that/is.not/what.{exist"); - assertEquals(false, rrctp.validate().isValid()); + assertFalse(rrctp.validate().isValid()); rrctp.setUrl("http://place}.{that}/{is}.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validate().isValid()); + assertFalse(rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/is}.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validate().isValid()); + assertFalse(rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/{}.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validate().isValid()); + assertFalse(rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/{ }.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validate().isValid()); + assertFalse(rrctp.validate().isValid()); } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestTest.java index d7b418a78..fe779eb89 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019, 2024 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,18 +22,18 @@ package org.onap.policy.apex.plugins.event.carrier.restrequestor; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Properties; -import org.junit.Test; +import org.junit.jupiter.api.Test; /** * Test the ApexRestRequest class. */ -public class ApexRestRequestTest { +class ApexRestRequestTest { @Test - public void testApexRestRequest() { + void testApexRestRequest() { final String eventName = "EventName"; final String eventString = "The Event String"; @@ -50,6 +50,6 @@ public class ApexRestRequestTest { assertEquals(1234567, rr.getTimestamp()); assertEquals("ApexRestRequest(executionId=1, eventName=EventName, event=The Event String, timestamp=1234567)", - rr.toString()); + rr.toString()); } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorConsumerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorConsumerTest.java index e72fa9030..47f90454c 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorConsumerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorConsumerTest.java @@ -23,13 +23,13 @@ package org.onap.policy.apex.plugins.event.carrier.restrequestor; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.awaitility.Awaitility.await; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import java.util.Properties; import java.util.concurrent.TimeUnit; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; import org.onap.policy.apex.service.engine.event.ApexEventException; import org.onap.policy.apex.service.engine.event.ApexEventReceiver; @@ -38,45 +38,40 @@ import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMo /** * Test the ApexRestRequestorConsumer class. - * */ -public class ApexRestRequestorConsumerTest { +class ApexRestRequestorConsumerTest { // String constants private static final String CONSUMER_NAME = "ConsumerName"; private static final String EVENT_NAME = "EventName"; private static final String EVENT_BODY = "Event body"; @Test - public void testApexRestRequestorConsumerSetup() throws ApexEventException { + void testApexRestRequestorConsumerSetup() throws ApexEventException { ApexRestRequestorConsumer consumer = new ApexRestRequestorConsumer(); assertNotNull(consumer); EventHandlerParameters consumerParameters = new EventHandlerParameters(); ApexEventReceiver incomingEventReceiver = null; - assertThatThrownBy(() -> { - consumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver); - }).hasMessage("specified consumer properties are not applicable to REST Requestor consumer (ConsumerName)"); + assertThatThrownBy(() -> consumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver)) + .hasMessage("specified consumer properties are not applicable to REST Requestor consumer (ConsumerName)"); RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters(); consumerParameters.setCarrierTechnologyParameters(rrctp); - assertThatThrownBy(() -> { - consumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver); - }).hasMessage("REST Requestor consumer (ConsumerName) must run in peered requestor mode " - + "with a REST Requestor producer"); + assertThatThrownBy(() -> consumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver)) + .hasMessage("REST Requestor consumer (ConsumerName) must run in peered requestor mode " + + "with a REST Requestor producer"); consumerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true); rrctp.setHttpMethod(null); - assertThatThrownBy(() -> { - consumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver); - }).hasMessage("no URL has been specified on REST Requestor consumer (ConsumerName)"); + assertThatThrownBy(() -> consumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver)) + .hasMessage("no URL has been specified on REST Requestor consumer (ConsumerName)"); rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.GET); rrctp.setUrl("ZZZZ"); - assertThatThrownBy(() -> { - consumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver); - }).hasMessage("invalid URL has been specified on REST Requestor consumer (ConsumerName)"); + assertThatThrownBy(() -> consumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver)) + .hasMessage("invalid URL has been specified on REST Requestor consumer (ConsumerName)"); rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.GET); rrctp.setUrl("http://www.onap.org"); @@ -85,9 +80,8 @@ public class ApexRestRequestorConsumerTest { consumer.init(CONSUMER_NAME, consumerParameters, incomingEventReceiver); - assertThatThrownBy(() -> { - consumer.processRestRequest(null); - }).hasMessage("could not queue request \"null\" on REST Requestor consumer (ConsumerName)"); + assertThatThrownBy(() -> consumer.processRestRequest(null)) + .hasMessage("could not queue request \"null\" on REST Requestor consumer (ConsumerName)"); assertEquals(CONSUMER_NAME, consumer.getName()); assertEquals(0, consumer.getEventsReceived()); @@ -95,7 +89,7 @@ public class ApexRestRequestorConsumerTest { } @Test - public void testApexRestRequestorConsumerRequest() throws ApexEventException { + void testApexRestRequestorConsumerRequest() throws ApexEventException { ApexRestRequestorConsumer consumer = new ApexRestRequestorConsumer(); assertNotNull(consumer); @@ -119,7 +113,7 @@ public class ApexRestRequestorConsumerTest { } @Test - public void testApexRestRequestorConsumerUrlUpdate() throws ApexEventException { + void testApexRestRequestorConsumerUrlUpdate() throws ApexEventException { ApexRestRequestorConsumer consumer = new ApexRestRequestorConsumer(); assertNotNull(consumer); @@ -145,7 +139,7 @@ public class ApexRestRequestorConsumerTest { } @Test - public void testApexRestRequestorConsumerUrlUpdateError() throws ApexEventException { + void testApexRestRequestorConsumerUrlUpdateError() throws ApexEventException { ApexRestRequestorConsumer consumer = new ApexRestRequestorConsumer(); assertNotNull(consumer); diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorProducerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorProducerTest.java index 450a21f01..0e57a31e9 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorProducerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorProducerTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. + * Modifications Copyright (C) 2020, 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,11 +22,12 @@ package org.onap.policy.apex.plugins.event.carrier.restrequestor; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.fail; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.onap.policy.apex.service.engine.event.ApexEventConsumer; import org.onap.policy.apex.service.engine.event.ApexEventException; import org.onap.policy.apex.service.engine.event.PeeredReference; @@ -38,33 +39,30 @@ import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMo /** * Test the ApexRestRequestorProducer class. */ -public class ApexRestRequestorProducerTest { +class ApexRestRequestorProducerTest { // String constants private static final String PRODUCER_NAME = "ProducerName"; @Test - public void testApexRestRequestorProducerMethods() throws ApexEventException { + void testApexRestRequestorProducerMethods() throws ApexEventException { ApexRestRequestorProducer producer = new ApexRestRequestorProducer(); assertNotNull(producer); EventHandlerParameters producerParameters = new EventHandlerParameters(); - assertThatThrownBy(() -> { - producer.init(PRODUCER_NAME, producerParameters); - }).hasMessage("specified producer properties are not applicable to REST requestor producer (ProducerName)"); + assertThatThrownBy(() -> producer.init(PRODUCER_NAME, producerParameters)) + .hasMessage("specified producer properties are not applicable to REST requestor producer (ProducerName)"); RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters(); producerParameters.setCarrierTechnologyParameters(rrctp); - assertThatThrownBy(() -> { - producer.init(PRODUCER_NAME, producerParameters); - }).hasMessage("REST Requestor producer (ProducerName) must run in peered requestor mode " - + "with a REST Requestor consumer"); + assertThatThrownBy(() -> producer.init(PRODUCER_NAME, producerParameters)) + .hasMessage("REST Requestor producer (ProducerName) must run in peered requestor mode " + + "with a REST Requestor consumer"); producerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true); rrctp.setUrl("ZZZZ"); - assertThatThrownBy(() -> { - producer.init(PRODUCER_NAME, producerParameters); - }).hasMessage("URL may not be specified on REST Requestor producer (ProducerName)"); + assertThatThrownBy(() -> producer.init(PRODUCER_NAME, producerParameters)) + .hasMessage("URL may not be specified on REST Requestor producer (ProducerName)"); rrctp.setUrl(null); rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.GET); @@ -80,11 +78,11 @@ public class ApexRestRequestorProducerTest { assertEquals(PRODUCER_NAME, producer.getName()); assertEquals(0, producer.getEventsSent()); - assertEquals(null, producer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR)); + assertNull(producer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR)); } @Test - public void testApexRestRequestorProducerRequest() throws ApexEventException { + void testApexRestRequestorProducerRequest() throws ApexEventException { EventHandlerParameters producerParameters = new EventHandlerParameters(); RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters(); @@ -100,9 +98,8 @@ public class ApexRestRequestorProducerTest { String eventName = "EventName"; String event = "This is the event"; - assertThatThrownBy(() -> { - producer.sendEvent(12345, null, eventName, event); - }).hasMessage("send of event failed, REST response consumer is not defined\n" + "This is the event"); + assertThatThrownBy(() -> producer.sendEvent(12345, null, eventName, event)) + .hasMessage("send of event failed, REST response consumer is not defined\n" + "This is the event"); ApexEventConsumer consumer = new ApexFileEventConsumer(); SynchronousEventCache eventCache = @@ -111,9 +108,8 @@ public class ApexRestRequestorProducerTest { PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR, consumer, producer); producer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, peeredReference); - assertThatThrownBy(() -> { - producer.sendEvent(12345, null, eventName, event); - }).hasMessage("send of event failed, REST response consumer " - + "is not an instance of ApexRestRequestorConsumer\n" + "This is the event"); + assertThatThrownBy(() -> producer.sendEvent(12345, null, eventName, event)) + .hasMessage("send of event failed, REST response consumer " + + "is not an instance of ApexRestRequestorConsumer\n" + "This is the event"); } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParametersTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParametersTest.java index f17721bdc..d953cf8ce 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParametersTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParametersTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019-2020, 2024 Nordix Foundation. * Modifications Copyright (C) 2020 Bell Canada. All rights reserved. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ @@ -24,12 +24,13 @@ package org.onap.policy.apex.plugins.event.carrier.restrequestor; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Set; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments; import org.onap.policy.apex.service.parameters.ApexParameterHandler; import org.onap.policy.apex.service.parameters.ApexParameters; @@ -38,24 +39,24 @@ import org.onap.policy.common.parameters.ParameterException; /** * Test REST Requestor carrier technology parameters. */ -public class RestRequestorCarrierTechnologyParametersTest { +class RestRequestorCarrierTechnologyParametersTest { @Test - public void testRestRequestorCarrierTechnologyParametersBadList() { + void testRestRequestorCarrierTechnologyParametersBadList() { verifyException("src/test/resources/prodcons/RESTRequestorWithHTTPHeaderBadList.json", - "item \"entry 2\" value \"null\" INVALID, is null"); + "item \"entry 2\" value \"null\" INVALID, is null"); } @Test - public void testRestRequestorCarrierTechnologyParametersNotKvPairs() { + void testRestRequestorCarrierTechnologyParametersNotKvPairs() { verifyException("src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNotKvPairs.json", - "item \"entry 0\" value \"[aaa, bbb, ccc]\" INVALID, must have one key"); + "item \"entry 0\" value \"[aaa, bbb, ccc]\" INVALID, must have one key"); } @Test - public void testRestRequestorCarrierTechnologyParametersNulls() { + void testRestRequestorCarrierTechnologyParametersNulls() { verifyException("src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNulls.json", - "\"key\""); + "\"key\""); } private void verifyException(String fileName, String expectedMsg) { @@ -64,11 +65,11 @@ public class RestRequestorCarrierTechnologyParametersTest { arguments.setRelativeFileRoot("."); assertThatThrownBy(() -> new ApexParameterHandler().getParameters(arguments)).describedAs(fileName) - .hasMessageContaining(expectedMsg); + .hasMessageContaining(expectedMsg); } @Test - public void testRestRequestorCarrierTechnologyParametersOk() throws ParameterException { + void testRestRequestorCarrierTechnologyParametersOk() throws ParameterException { ApexCommandLineArguments arguments = new ApexCommandLineArguments(); arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTRequestorWithHTTPHeaderOK.json"); arguments.setRelativeFileRoot("."); @@ -88,7 +89,7 @@ public class RestRequestorCarrierTechnologyParametersTest { } @Test - public void testRestClientCarrierTechnologyParameterFilterInvalid() { + void testRestClientCarrierTechnologyParameterFilterInvalid() { ApexCommandLineArguments arguments = new ApexCommandLineArguments(); arguments.setToscaPolicyFilePath("src/test/resources/prodcons/RESTClientWithHTTPFilterInvalid.json"); arguments.setRelativeFileRoot("."); @@ -103,11 +104,11 @@ public class RestRequestorCarrierTechnologyParametersTest { } @Test - public void testGettersAndSetters() { + void testGettersAndSetters() { RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters(); rrctp.setHttpHeaders(null); - assertEquals(null, rrctp.getHttpHeadersAsMultivaluedMap()); + assertNull(rrctp.getHttpHeadersAsMultivaluedMap()); rrctp.setUrl("http://some.where"); assertEquals("http://some.where", rrctp.getUrl()); @@ -146,7 +147,7 @@ public class RestRequestorCarrierTechnologyParametersTest { } @Test - public void testUrlValidation() { + void testUrlValidation() { RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters(); rrctp.setUrl("http://some.where.no.tag.in.url"); @@ -164,30 +165,28 @@ public class RestRequestorCarrierTechnologyParametersTest { assertEquals("ccc", rrctp.getHttpHeaders()[1][0]); assertEquals("ddd", rrctp.getHttpHeaders()[1][1]); - assertEquals(true, rrctp.validate().isValid()); + assertTrue(rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/is{that}.{one}"); - assertEquals(true, rrctp.validate().isValid()); + assertTrue(rrctp.validate().isValid()); Set<String> keymap = rrctp.getKeysFromUrl(); - assertEquals(true, keymap.contains("place")); - assertEquals(true, keymap.contains("that")); - assertEquals(true, keymap.contains("one")); + assertTrue(keymap.contains("place") && keymap.contains("that") && keymap.contains("one")); rrctp.setUrl("http://{place.{that}/{is}.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validate().isValid()); + assertFalse(rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/{is}.{not}/{what}.{exist"); - assertEquals(false, rrctp.validate().isValid()); + assertFalse(rrctp.validate().isValid()); rrctp.setUrl("http://place.that/is.not/what.{exist"); - assertEquals(false, rrctp.validate().isValid()); + assertFalse(rrctp.validate().isValid()); rrctp.setUrl("http://place}.{that}/{is}.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validate().isValid()); + assertFalse(rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/is}.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validate().isValid()); + assertFalse(rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/{}.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validate().isValid()); + assertFalse(rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/{ }.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validate().isValid()); + assertFalse(rrctp.validate().isValid()); } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorTest.java index 257e533c4..3fe836485 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorTest.java @@ -24,7 +24,7 @@ package org.onap.policy.apex.plugins.event.carrier.restrequestor; import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import com.google.gson.Gson; import jakarta.ws.rs.client.Client; @@ -34,11 +34,11 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.util.Map; import java.util.concurrent.TimeUnit; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.onap.policy.apex.core.infrastructure.messaging.MessagingException; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.apex.service.engine.main.ApexMain; @@ -50,12 +50,12 @@ import org.onap.policy.common.utils.network.NetworkUtil; /** * The Class TestRestRequestor. */ -public class RestRequestorTest { +class RestRequestorTest { private static final int PORT = 32801; private static HttpServletServer server; - private ByteArrayOutputStream outContent = new ByteArrayOutputStream(); - private ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); private final PrintStream stdout = System.out; private final PrintStream stderr = System.err; @@ -65,8 +65,8 @@ public class RestRequestorTest { * * @throws Exception the exception */ - @BeforeClass - public static void setUp() throws Exception { + @BeforeAll + static void setUp() throws Exception { server = HttpServletServerFactoryInstance.getServerFactory().build(null, false, null, PORT, false, "/TestRESTRequestor", false, false); @@ -82,10 +82,9 @@ public class RestRequestorTest { /** * Tear down. - * */ - @AfterClass - public static void tearDown() { + @AfterAll + static void tearDown() { if (server != null) { server.stop(); } @@ -94,8 +93,8 @@ public class RestRequestorTest { /** * Before test. */ - @Before - public void beforeTest() { + @BeforeEach + void beforeTest() { SupportRestRequestorEndpoint.resetCounters(); System.setOut(new PrintStream(outContent)); System.setErr(new PrintStream(errContent)); @@ -104,8 +103,8 @@ public class RestRequestorTest { /** * After test. */ - @After - public void afterTest() { + @AfterEach + void afterTest() { System.setOut(stdout); System.setErr(stderr); } @@ -114,10 +113,10 @@ public class RestRequestorTest { * Test rest requestor get. * * @throws MessagingException the messaging exception - * @throws Exception an exception + * @throws Exception an exception */ @Test - public void testRestRequestorGet() throws Exception { + void testRestRequestorGet() throws Exception { final Client client = ClientBuilder.newClient(); final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGet.json"}; @@ -139,7 +138,7 @@ public class RestRequestorTest { * @throws ApexException the apex exception */ @Test - public void testRestRequestorGetEmpty() throws ApexException { + void testRestRequestorGetEmpty() throws ApexException { final Client client = ClientBuilder.newClient(); final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetEmpty.json"}; @@ -149,7 +148,7 @@ public class RestRequestorTest { Response response = null; // Wait for the required amount of events to be received or for 10 seconds - double getsSoFar = 0.0; + double getsSoFar; for (int i = 0; i < 40; i++) { response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats") .request("application/json").get(); @@ -160,8 +159,8 @@ public class RestRequestorTest { final String responseString = response.readEntity(String.class); - @SuppressWarnings("unchecked") - final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class); + @SuppressWarnings("unchecked") final Map<String, Object> jsonMap = + new Gson().fromJson(responseString, Map.class); getsSoFar = Double.parseDouble(jsonMap.get("GET").toString()); if (getsSoFar >= 50.0) { @@ -183,7 +182,7 @@ public class RestRequestorTest { * @throws ApexException the apex exception */ @Test - public void testRestRequestorPut() throws ApexException { + void testRestRequestorPut() throws ApexException { final Client client = ClientBuilder.newClient(); final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FilePut.json"}; @@ -205,7 +204,7 @@ public class RestRequestorTest { * @throws ApexException the apex exception */ @Test - public void testRestRequestorPost() throws ApexException { + void testRestRequestorPost() throws ApexException { final Client client = ClientBuilder.newClient(); final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FilePost.json"}; @@ -227,7 +226,7 @@ public class RestRequestorTest { * @throws ApexException the apex exception */ @Test - public void testRestRequestorDelete() throws ApexException { + void testRestRequestorDelete() throws ApexException { final Client client = ClientBuilder.newClient(); final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileDelete.json"}; @@ -250,7 +249,7 @@ public class RestRequestorTest { * @throws ApexException the apex exception */ @Test - public void testRestRequestorMultiInputs() throws ApexException { + void testRestRequestorMultiInputs() throws ApexException { final Client client = ClientBuilder.newClient(); final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetMulti.json"}; @@ -272,7 +271,7 @@ public class RestRequestorTest { * @throws ApexException the apex exception */ @Test - public void testRestRequestorProducerAlone() throws ApexException { + void testRestRequestorProducerAlone() throws ApexException { final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetProducerAlone.json"}; @@ -291,7 +290,7 @@ public class RestRequestorTest { * @throws ApexException the apex exception */ @Test - public void testRestRequestorConsumerAlone() throws ApexException { + void testRestRequestorConsumerAlone() throws ApexException { final String[] args = {"src/test/resources/prodcons/File2RESTRequest2FileGetConsumerAlone.json"}; ApexMain apexMain = new ApexMain(args); apexMain.shutdown(); @@ -307,8 +306,8 @@ public class RestRequestorTest { assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); final String responseString = response.readEntity(String.class); - @SuppressWarnings("unchecked") - final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class); + @SuppressWarnings("unchecked") final Map<String, Object> jsonMap = + new Gson().fromJson(responseString, Map.class); return Double.parseDouble(jsonMap.get(statToGet).toString()); } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/SupportRestRequestorEndpoint.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/SupportRestRequestorEndpoint.java index b05c24333..f82add13f 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/SupportRestRequestorEndpoint.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/SupportRestRequestorEndpoint.java @@ -46,9 +46,9 @@ public class SupportRestRequestorEndpoint { private static int getMessagesReceived = 0; private static int deleteMessagesReceived = 0; - private static String EVENT_STRING = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.events\",\n" - + "\"name\": \"ResponseEvent\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_" - + getMessagesReceived + "\",\n" + "\"target\": \"apex\",\n" + "\"intPar\": 9080\n" + "}"; + private static final String EVENT_STRING = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.events\",\n" + + "\"name\": \"ResponseEvent\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_" + + getMessagesReceived + "\",\n" + "\"target\": \"apex\",\n" + "\"intPar\": 9080\n" + "}"; /** * Reset counters. @@ -73,10 +73,10 @@ public class SupportRestRequestorEndpoint { statMessagesReceived++; } return Response.status(200) - .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": " - + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + ",\"DELETE\": " - + deleteMessagesReceived + "}") - .build(); + .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + ",\"POST\": " + + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + ",\"DELETE\": " + + deleteMessagesReceived + "}") + .build(); } /** @@ -129,8 +129,7 @@ public class SupportRestRequestorEndpoint { postMessagesReceived++; } - @SuppressWarnings("unchecked") - final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class); + @SuppressWarnings("unchecked") final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class); assertTrue(jsonMap.containsKey("name")); assertEquals("0.0.1", jsonMap.get("version")); assertEquals("org.onap.policy.apex.events", jsonMap.get("nameSpace")); @@ -165,8 +164,7 @@ public class SupportRestRequestorEndpoint { putMessagesReceived++; } - @SuppressWarnings("unchecked") - final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class); + @SuppressWarnings("unchecked") final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class); assertTrue(jsonMap.containsKey("name")); assertEquals("0.0.1", jsonMap.get("version")); assertEquals("org.onap.policy.apex.events", jsonMap.get("nameSpace")); diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/AccessControlFilterTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/AccessControlFilterTest.java index 1ec0d52bb..b2e158f1a 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/AccessControlFilterTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/AccessControlFilterTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2021, 2023 Nordix Foundation. + * Copyright (C) 2021, 2023-2024 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,7 +21,7 @@ package org.onap.policy.apex.plugins.event.carrier.restserver; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import jakarta.ws.rs.container.ContainerRequestContext; import jakarta.ws.rs.container.ContainerResponseContext; @@ -29,16 +29,16 @@ import jakarta.ws.rs.core.MultivaluedHashMap; import jakarta.ws.rs.core.MultivaluedMap; import java.io.IOException; import org.apache.commons.lang3.RandomStringUtils; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; -@RunWith(MockitoJUnitRunner.class) -public class AccessControlFilterTest { +@ExtendWith(MockitoExtension.class) +class AccessControlFilterTest { private AccessControlFilter acf; @@ -47,13 +47,13 @@ public class AccessControlFilterTest { @Mock private ContainerResponseContext responseContext; - @Before + @BeforeEach public void beforeEach() { acf = new AccessControlFilter(); } @Test - public void filterAddToExisting() throws IOException { + void filterAddToExisting() throws IOException { // prepare mocks final String origin = RandomStringUtils.randomAlphanumeric(14, 16); final MultivaluedHashMap<String, Object> map = new MultivaluedHashMap<>(); diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/ApexRestServerConsumerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/ApexRestServerConsumerTest.java index 5f7cb257b..291a97c70 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/ApexRestServerConsumerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/ApexRestServerConsumerTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Samsung. All rights reserved. - * Modifications Copyright (C) 2019-2021, 2023 Nordix Foundation. + * Modifications Copyright (C) 2019-2021, 2023-2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,84 +21,84 @@ package org.onap.policy.apex.plugins.event.carrier.restserver; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import jakarta.ws.rs.core.Response; import java.io.IOException; import java.lang.reflect.Field; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.onap.policy.apex.service.engine.event.ApexEventException; import org.onap.policy.apex.service.engine.event.ApexEventReceiver; import org.onap.policy.apex.service.engine.event.ApexPluginsEventConsumer; import org.onap.policy.apex.service.engine.event.PeeredReference; -import org.onap.policy.apex.service.engine.event.SynchronousEventCache; import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; import org.onap.policy.common.endpoints.http.server.HttpServletServer; import org.onap.policy.common.utils.network.NetworkUtil; -public class ApexRestServerConsumerTest { +class ApexRestServerConsumerTest { ApexRestServerConsumer apexRestServerConsumer = null; EventHandlerParameters consumerParameters = null; ApexEventReceiver incomingEventReceiver = null; ApexRestServerProducer apexRestServerProducer = null; RestServerCarrierTechnologyParameters restServerCarrierTechnologyParameters = null; - SynchronousEventCache synchronousEventCache = null; + + AutoCloseable closeable; /** * Set up testing. - * - * @throws Exception on test set up errors. */ - @Before - public void setUp() throws Exception { + @BeforeEach + void setUp() { apexRestServerConsumer = new ApexRestServerConsumer(); consumerParameters = new EventHandlerParameters(); apexRestServerProducer = new ApexRestServerProducer(); apexRestServerConsumer.start(); } - @After - public void tearDown() { + @AfterEach + void tearDown() { apexRestServerConsumer.stop(); } - @Test(expected = ApexEventException.class) - public void testInitWithNonWebSocketCarrierTechnologyParameters() throws ApexEventException { - consumerParameters.setCarrierTechnologyParameters(new CarrierTechnologyParameters() {}); - apexRestServerConsumer.init("TestApexRestServerConsumer", consumerParameters, - incomingEventReceiver); + @Test + void testInitWithNonWebSocketCarrierTechnologyParameters() { + consumerParameters.setCarrierTechnologyParameters(new CarrierTechnologyParameters() { + }); + assertThrows(ApexEventException.class, () -> + apexRestServerConsumer.init("TestApexRestServerConsumer", consumerParameters, incomingEventReceiver)); } - @Test(expected = ApexEventException.class) - public void testInitWithWebSocketCarrierTechnologyParameters() throws ApexEventException { + @Test + void testInitWithWebSocketCarrierTechnologyParameters() { restServerCarrierTechnologyParameters = new RestServerCarrierTechnologyParameters(); consumerParameters.setCarrierTechnologyParameters(restServerCarrierTechnologyParameters); - apexRestServerConsumer.init("TestApexRestServerConsumer", consumerParameters, - incomingEventReceiver); + assertThrows(ApexEventException.class, () -> + apexRestServerConsumer.init("TestApexRestServerConsumer", consumerParameters, incomingEventReceiver)); } - @Test(expected = ApexEventException.class) - public void testInitWithSynchronousMode() throws ApexEventException, SecurityException, IllegalArgumentException { + @Test + void testInitWithSynchronousMode() throws SecurityException, IllegalArgumentException { restServerCarrierTechnologyParameters = new RestServerCarrierTechnologyParameters(); restServerCarrierTechnologyParameters.setStandalone(true); consumerParameters.setCarrierTechnologyParameters(restServerCarrierTechnologyParameters); consumerParameters.setPeeredMode(EventHandlerPeeredMode.SYNCHRONOUS, true); - apexRestServerConsumer.init("TestApexRestServerConsumer", consumerParameters, - incomingEventReceiver); + + assertThrows(ApexEventException.class, () -> apexRestServerConsumer.init("TestApexRestServerConsumer", + consumerParameters, incomingEventReceiver)); } - @Test(expected = IllegalArgumentException.class) - public void testInitWithSynchronousModeAndProperValues() - throws ApexEventException, SecurityException, IllegalArgumentException { + @Test + void testInitWithSynchronousModeAndProperValues() throws SecurityException, IllegalArgumentException { restServerCarrierTechnologyParameters = new RestServerCarrierTechnologyParameters(); @@ -109,12 +109,14 @@ public class ApexRestServerConsumerTest { consumerParameters.setCarrierTechnologyParameters(restServerCarrierTechnologyParameters); consumerParameters.setPeeredMode(EventHandlerPeeredMode.SYNCHRONOUS, true); - apexRestServerConsumer.init("TestApexRestServerConsumer", consumerParameters, - incomingEventReceiver); + + assertThrows(IllegalArgumentException.class, + () -> apexRestServerConsumer.init("TestApexRestServerConsumer", consumerParameters, + incomingEventReceiver)); } @Test - public void testInitAndStop() throws ApexEventException, IOException { + void testInitAndStop() throws ApexEventException, IOException { restServerCarrierTechnologyParameters = new RestServerCarrierTechnologyParameters(); restServerCarrierTechnologyParameters.setStandalone(true); @@ -138,39 +140,39 @@ public class ApexRestServerConsumerTest { } @Test - public void testGetName() { + void testGetName() { assertNull(apexRestServerConsumer.getName()); } @Test - public void testGetPeeredReference() { + void testGetPeeredReference() { assertNull(apexRestServerConsumer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR)); } @Test - public void testSetPeeredReference() { + void testSetPeeredReference() { PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR, - apexRestServerConsumer, apexRestServerProducer); + apexRestServerConsumer, apexRestServerProducer); apexRestServerConsumer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, - peeredReference); + peeredReference); assertNotNull(apexRestServerConsumer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR)); } @Test - public void testReceiveEvent() throws ApexEventException { + void testReceiveEvent() { Response response = apexRestServerConsumer.receiveEvent(""); assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus()); + response.close(); } - @Test(expected = NullPointerException.class) - public void testReceiveEventWithNonDefaultValues() - throws ApexEventException, NoSuchFieldException, SecurityException, - IllegalArgumentException, IllegalAccessException { + @Test + void testReceiveEventWithNonDefaultValues() throws NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR, - apexRestServerConsumer, apexRestServerProducer); + apexRestServerConsumer, apexRestServerProducer); apexRestServerConsumer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, - peeredReference); + peeredReference); ApexEventReceiver apexEventReceiver = new SupportApexEventReceiver(); @@ -178,8 +180,14 @@ public class ApexRestServerConsumerTest { Field field = ApexPluginsEventConsumer.class.getDeclaredField("name"); field.setAccessible(true); field.set(apexRestServerConsumer, "TestApexRestServerConsumer"); + assertThrows(NullPointerException.class, () -> + closeable = apexRestServerConsumer.receiveEvent("TestApexRestServerConsumer")); + } - apexRestServerConsumer.receiveEvent("TestApexRestServerConsumer"); - + @AfterEach + void after() throws Exception { + if (closeable != null) { + closeable.close(); + } } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/ApexRestServerProducerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/ApexRestServerProducerTest.java index 34992ff1d..435af0996 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/ApexRestServerProducerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/ApexRestServerProducerTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Samsung. All rights reserved. - * Modifications Copyright (C) 2019-2021 Nordix Foundation. + * Modifications Copyright (C) 2019-2021, 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,34 +21,32 @@ package org.onap.policy.apex.plugins.event.carrier.restserver; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.lang.reflect.Field; import java.util.Random; import org.apache.commons.lang3.RandomStringUtils; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.onap.policy.apex.service.engine.event.ApexEventException; -import org.onap.policy.apex.service.engine.event.ApexEventReceiver; import org.onap.policy.apex.service.engine.event.PeeredReference; import org.onap.policy.apex.service.engine.event.SynchronousEventCache; import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; -public class ApexRestServerProducerTest { +class ApexRestServerProducerTest { ApexRestServerProducer apexRestServerProducer = null; EventHandlerParameters producerParameters = null; - ApexEventReceiver incomingEventReceiver = null; ApexRestServerConsumer apexRestServerConsumer = null; RestServerCarrierTechnologyParameters restServerCarrierTechnologyParameters = null; - SynchronousEventCache synchronousEventCache = null; Random random = new Random(); /** @@ -56,35 +54,38 @@ public class ApexRestServerProducerTest { * * @throws Exception on test set up errors. */ - @Before - public void setUp() throws Exception { + @BeforeEach + void setUp() throws Exception { apexRestServerConsumer = new ApexRestServerConsumer(); producerParameters = new EventHandlerParameters(); apexRestServerProducer = new ApexRestServerProducer(); } - @After - public void tearDown() { + @AfterEach + void tearDown() { apexRestServerProducer.stop(); } - @Test(expected = ApexEventException.class) - public void testInitWithNonWebSocketCarrierTechnologyParameters() throws ApexEventException { - producerParameters.setCarrierTechnologyParameters(new CarrierTechnologyParameters() {}); - apexRestServerProducer.init("TestApexRestServerProducer", producerParameters); + @Test + void testInitWithNonWebSocketCarrierTechnologyParameters() { + producerParameters.setCarrierTechnologyParameters(new CarrierTechnologyParameters() { + }); + assertThrows(ApexEventException.class, () -> + apexRestServerProducer.init("TestApexRestServerProducer", producerParameters)); } - @Test(expected = ApexEventException.class) - public void testInitWithWebSocketCarrierTechnologyParameters() throws ApexEventException { + @Test + void testInitWithWebSocketCarrierTechnologyParameters() { restServerCarrierTechnologyParameters = new RestServerCarrierTechnologyParameters(); producerParameters.setCarrierTechnologyParameters(restServerCarrierTechnologyParameters); - apexRestServerProducer.init("TestApexRestServerProducer", producerParameters); + assertThrows(ApexEventException.class, () -> + apexRestServerProducer.init("TestApexRestServerProducer", producerParameters)); } - @Test(expected = ApexEventException.class) - public void testInitWithNonDefaultValue() throws ApexEventException, NoSuchFieldException, - SecurityException, IllegalArgumentException, IllegalAccessException { + @Test + void testInitWithNonDefaultValue() throws NoSuchFieldException, + SecurityException, IllegalArgumentException, IllegalAccessException { restServerCarrierTechnologyParameters = new RestServerCarrierTechnologyParameters(); Field field = RestServerCarrierTechnologyParameters.class.getDeclaredField("host"); field.setAccessible(true); @@ -93,11 +94,12 @@ public class ApexRestServerProducerTest { field.setAccessible(true); field.set(restServerCarrierTechnologyParameters, 65535); producerParameters.setCarrierTechnologyParameters(restServerCarrierTechnologyParameters); - apexRestServerProducer.init("TestApexRestServerProducer", producerParameters); + assertThrows(ApexEventException.class, () -> + apexRestServerProducer.init("TestApexRestServerProducer", producerParameters)); } @Test - public void testInitWithSynchronousMode() throws ApexEventException { + void testInitWithSynchronousMode() throws ApexEventException { restServerCarrierTechnologyParameters = new RestServerCarrierTechnologyParameters(); producerParameters.setCarrierTechnologyParameters(restServerCarrierTechnologyParameters); producerParameters.setPeeredMode(EventHandlerPeeredMode.SYNCHRONOUS, true); @@ -106,26 +108,26 @@ public class ApexRestServerProducerTest { } @Test - public void testGetName() { + void testGetName() { assertNull(apexRestServerProducer.getName()); } @Test - public void testGetPeeredReference() { + void testGetPeeredReference() { assertNull(apexRestServerProducer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR)); } @Test - public void testSetPeeredReference() { + void testSetPeeredReference() { PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR, - apexRestServerConsumer, apexRestServerProducer); + apexRestServerConsumer, apexRestServerProducer); apexRestServerProducer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, - peeredReference); + peeredReference); assertNotNull(apexRestServerProducer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR)); } @Test - public void testSendEventNotExistingEventToApex() { + void testSendEventNotExistingEventToApex() { final long executionId = random.nextLong(); final String eventName = RandomStringUtils.randomAlphabetic(7); final Object event = new Object(); @@ -143,7 +145,7 @@ public class ApexRestServerProducerTest { } @Test - public void testSendEvent() { + void testSendEvent() { final long executionId = random.nextLong(); final String eventName = RandomStringUtils.randomAlphabetic(7); final Object expected = new Object(); diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/RestServerCarrierTechnologyParametersTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/RestServerCarrierTechnologyParametersTest.java index ef78d3211..ab7a54cdd 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/RestServerCarrierTechnologyParametersTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/RestServerCarrierTechnologyParametersTest.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 Samsung. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019, 2024 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,16 +23,16 @@ package org.onap.policy.apex.plugins.event.carrier.restserver; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.reflect.Field; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.onap.policy.common.parameters.ValidationResult; -public class RestServerCarrierTechnologyParametersTest { +class RestServerCarrierTechnologyParametersTest { RestServerCarrierTechnologyParameters restServerCarrierTechnologyParameters = null; ValidationResult result = null; @@ -40,28 +40,27 @@ public class RestServerCarrierTechnologyParametersTest { /** * Set up testing. * - * @throws Exception on test set up errors. */ - @Before - public void setUp() throws Exception { + @BeforeEach + void setUp() { restServerCarrierTechnologyParameters = new RestServerCarrierTechnologyParameters(); } @Test - public void testRestServerCarrierTechnologyParameters() { + void testRestServerCarrierTechnologyParameters() { assertNotNull(restServerCarrierTechnologyParameters); assertFalse(restServerCarrierTechnologyParameters.isStandalone()); } @Test - public void testValidate() { + void testValidate() { result = restServerCarrierTechnologyParameters.validate(); assertNotNull(result); assertTrue(result.isValid()); } @Test - public void testValidateWithNonDefaultValues() throws NoSuchFieldException, SecurityException, + void testValidateWithNonDefaultValues() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { Field field = RestServerCarrierTechnologyParameters.class.getDeclaredField("standalone"); @@ -89,7 +88,7 @@ public class RestServerCarrierTechnologyParametersTest { } @Test - public void testValidateWithValidValues() throws NoSuchFieldException, SecurityException, + void testValidateWithValidValues() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { Field field = RestServerCarrierTechnologyParameters.class.getDeclaredField("standalone"); @@ -119,7 +118,7 @@ public class RestServerCarrierTechnologyParametersTest { } @Test - public void testValidateWithInvalidValues() throws NoSuchFieldException, SecurityException, + void testValidateWithInvalidValues() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { Field field = RestServerCarrierTechnologyParameters.class.getDeclaredField("standalone"); diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/SupportApexEventReceiver.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/SupportApexEventReceiver.java index 773ecafb2..b10eecf21 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/SupportApexEventReceiver.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restserver/src/test/java/org/onap/policy/apex/plugins/event/carrier/restserver/SupportApexEventReceiver.java @@ -27,7 +27,7 @@ import org.onap.policy.apex.service.engine.event.ApexEventException; import org.onap.policy.apex.service.engine.event.ApexEventReceiver; /** - * Support Apex event reveiver for unit test. + * Support Apex event receiver for unit test. * */ @Getter |