diff options
author | waynedunican <wayne.dunican@est.tech> | 2024-06-13 09:24:00 +0100 |
---|---|---|
committer | waynedunican <wayne.dunican@est.tech> | 2024-06-24 16:30:48 +0100 |
commit | 7b11ab5c03a438aa29ff2242fa06bb8ff28c2867 (patch) | |
tree | 2c76324c1d60b761f09b82fb1786e4633510dbb9 /models-interactions/model-impl/cds | |
parent | bacd5a6f57e79f26d447644329b585991f989123 (diff) |
Convert models to JUnit 5
Review for models-impl
Issue-ID: POLICY-5042
Change-Id: I22ff90b12da3fb2ba4d0eead7afb9282eac6921f
Signed-off-by: waynedunican <wayne.dunican@est.tech>
Diffstat (limited to 'models-interactions/model-impl/cds')
2 files changed, 68 insertions, 51 deletions
diff --git a/models-interactions/model-impl/cds/src/test/java/org/onap/policy/cds/client/BasicAuthClientHeaderInterceptorTest.java b/models-interactions/model-impl/cds/src/test/java/org/onap/policy/cds/client/BasicAuthClientHeaderInterceptorTest.java index 3192520ae..83107aef8 100644 --- a/models-interactions/model-impl/cds/src/test/java/org/onap/policy/cds/client/BasicAuthClientHeaderInterceptorTest.java +++ b/models-interactions/model-impl/cds/src/test/java/org/onap/policy/cds/client/BasicAuthClientHeaderInterceptorTest.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2019 Bell Canada. * Modifications Copyright (C) 2019 AT&T Intellectual Property. 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. @@ -19,7 +20,7 @@ package org.onap.policy.cds.client; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.AdditionalAnswers.delegatesTo; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -40,9 +41,8 @@ import io.grpc.testing.GrpcCleanupRule; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Base64; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; import org.mockito.ArgumentMatchers; import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc; @@ -51,15 +51,14 @@ import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessin import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput; import org.onap.policy.cds.properties.CdsServerProperties; -public class BasicAuthClientHeaderInterceptorTest { +class BasicAuthClientHeaderInterceptorTest { // Generate a unique in-process server name. private static final String SERVER_NAME = InProcessServerBuilder.generateName(); private static final String CREDS = "test"; // Manages automatic graceful shutdown for the registered server and client channels at the end of test. - @Rule - public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule(); + private final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule(); private final ServerInterceptor mockCdsGrpcServerInterceptor = mock(ServerInterceptor.class, delegatesTo(new TestServerInterceptor())); @@ -73,8 +72,8 @@ public class BasicAuthClientHeaderInterceptorTest { * * @throws IOException on failure to register the test grpc server for graceful shutdown */ - @Before - public void setUp() throws IOException { + @BeforeEach + void setUp() throws IOException { // Setup the CDS properties props.setHost(SERVER_NAME); props.setPort(2000); @@ -95,7 +94,7 @@ public class BasicAuthClientHeaderInterceptorTest { } @Test - public void testIfBasicAuthHeaderIsDeliveredToCdsServer() { + void testIfBasicAuthHeaderIsDeliveredToCdsServer() { BluePrintProcessingServiceStub bpProcessingSvcStub = BluePrintProcessingServiceGrpc .newStub(ClientInterceptors.intercept(channel, new BasicAuthClientHeaderInterceptor(props))); ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class); @@ -129,10 +128,9 @@ public class BasicAuthClientHeaderInterceptorTest { @Override public <Q, P> Listener<Q> interceptCall(final ServerCall<Q, P> serverCall, - final Metadata metadata, final ServerCallHandler<Q, P> serverCallHandler) { + final Metadata metadata, final ServerCallHandler<Q, P> + serverCallHandler) { return serverCallHandler.startCall(serverCall, metadata); } } } - - diff --git a/models-interactions/model-impl/cds/src/test/java/org/onap/policy/cds/client/CdsProcessorGrpcClientTest.java b/models-interactions/model-impl/cds/src/test/java/org/onap/policy/cds/client/CdsProcessorGrpcClientTest.java index 0922fc4cb..1a35064ca 100644 --- a/models-interactions/model-impl/cds/src/test/java/org/onap/policy/cds/client/CdsProcessorGrpcClientTest.java +++ b/models-interactions/model-impl/cds/src/test/java/org/onap/policy/cds/client/CdsProcessorGrpcClientTest.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2019 Bell Canada. * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. 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,8 +21,9 @@ package org.onap.policy.cds.client; import static org.assertj.core.api.Assertions.assertThatCode; -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.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; @@ -30,7 +32,6 @@ import io.grpc.ManagedChannel; import io.grpc.inprocess.InProcessChannelBuilder; import io.grpc.inprocess.InProcessServerBuilder; import io.grpc.stub.StreamObserver; -import io.grpc.testing.GrpcCleanupRule; import io.grpc.util.MutableHandlerRegistry; import java.io.IOException; import java.util.ArrayList; @@ -39,10 +40,9 @@ import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -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.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers; import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase; import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput; @@ -51,22 +51,16 @@ import org.onap.policy.cds.api.CdsProcessorListener; import org.onap.policy.cds.api.TestCdsProcessorListenerImpl; import org.onap.policy.cds.properties.CdsServerProperties; -public class CdsProcessorGrpcClientTest { +class CdsProcessorGrpcClientTest { - // Generate a unique in-process server name. - private static final String SERVER_NAME = InProcessServerBuilder.generateName(); - - // Manages automatic graceful shutdown for the registered server and client channels at the end of test. - @Rule - public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule(); - - private final CdsProcessorListener listener = spy(new TestCdsProcessorListenerImpl()); - private final CdsServerProperties props = new CdsServerProperties(); - private final MutableHandlerRegistry serviceRegistry = new MutableHandlerRegistry(); - private final AtomicReference<StreamObserver<ExecutionServiceOutput>> responseObserverRef = new AtomicReference<>(); - private final List<String> messagesDelivered = new ArrayList<>(); - private final CountDownLatch allRequestsDelivered = new CountDownLatch(1); + private CdsProcessorListener listener; + private CdsServerProperties props; + private MutableHandlerRegistry serviceRegistry; + private AtomicReference<StreamObserver<ExecutionServiceOutput>> responseObserverRef; + private List<String> messagesDelivered; + private CountDownLatch allRequestsDelivered; + private ManagedChannel channel; private CdsProcessorGrpcClient client; /** @@ -74,22 +68,31 @@ public class CdsProcessorGrpcClientTest { * * @throws IOException on failure to register the test grpc server for graceful shutdown */ - @Before - public void setUp() throws IOException { + @BeforeEach + void setUp() throws IOException { + + listener = spy(new TestCdsProcessorListenerImpl()); + props = new CdsServerProperties(); + serviceRegistry = new MutableHandlerRegistry(); + responseObserverRef = new AtomicReference<>(); + messagesDelivered = new ArrayList<>(); + allRequestsDelivered = new CountDownLatch(1); + // Setup the CDS properties - props.setHost(SERVER_NAME); + // Generate a unique in-process server name. + String serverName = InProcessServerBuilder.generateName(); + props.setHost(serverName); props.setPort(2000); props.setUsername("testUser"); props.setPassword("testPassword"); props.setTimeout(60); // Create a server, add service, start, and register for automatic graceful shutdown. - grpcCleanup.register(InProcessServerBuilder.forName(SERVER_NAME) - .fallbackHandlerRegistry(serviceRegistry).directExecutor().build().start()); + InProcessServerBuilder.forName(serverName) + .fallbackHandlerRegistry(serviceRegistry).directExecutor().build().start(); - // Create a client channel and register for automatic graceful shutdown - ManagedChannel channel = grpcCleanup - .register(InProcessChannelBuilder.forName(SERVER_NAME).directExecutor().build()); + // Create a client channel + channel = InProcessChannelBuilder.forName(serverName).directExecutor().build(); // Create an instance of the gRPC client client = new CdsProcessorGrpcClient(channel, new CdsProcessorHandler(listener, "gRPC://localhost:1234/")); @@ -122,24 +125,40 @@ public class CdsProcessorGrpcClientTest { serviceRegistry.addService(testCdsBlueprintServerImpl); } - @After - public void tearDown() { - client.close(); + /** + * Cleans up resources after each test execution. + * This method ensures that the gRPC client and channel are properly closed and released after each test. + * It is annotated with {@code @AfterEach} to automatically run after each test method in the class. + * If the {@code client} is not {@code null}, it calls the {@code close} method to release resources + * used by the client. + * If the {@code channel} is not {@code null}, it calls the {@code shutdownNow} method + * to forcefully close the channel. + */ + @AfterEach + void tearDown() { + if (client != null) { + client.close(); + } + if (channel != null) { + channel.shutdownNow(); + } } @Test - public void testCdsProcessorGrpcClientConstructor() { + void testCdsProcessorGrpcClientConstructor() { assertThatCode(() -> new CdsProcessorGrpcClient(listener, props).close()).doesNotThrowAnyException(); } - @Test(expected = IllegalStateException.class) - public void testCdsProcessorGrpcClientConstructorFailure() { + @Test + void testCdsProcessorGrpcClientConstructorFailure() { props.setHost(null); - new CdsProcessorGrpcClient(listener, props).close(); + assertThrows(IllegalStateException.class, () -> { + new CdsProcessorGrpcClient(listener, props).close(); + }); } @Test - public void testSendRequestFail() throws InterruptedException { + void testSendRequestFail() throws InterruptedException { // Setup ExecutionServiceInput testReq = ExecutionServiceInput.newBuilder() .setActionIdentifiers(ActionIdentifiers.newBuilder().setActionName("policy-to-cds").build()) @@ -154,7 +173,7 @@ public class CdsProcessorGrpcClientTest { } @Test - public void testSendRequestSuccess() throws InterruptedException { + void testSendRequestSuccess() throws InterruptedException { // Setup request ExecutionServiceInput testReq1 = ExecutionServiceInput.newBuilder() .setActionIdentifiers(ActionIdentifiers.newBuilder().setActionName("policy-to-cds-req1").build()).build(); |