diff options
Diffstat (limited to 'prh-app-server/src')
14 files changed, 199 insertions, 271 deletions
diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTask.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTask.java index ffc81324..09de5cca 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTask.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTask.java @@ -20,11 +20,14 @@ package org.onap.dcaegen2.services.prh.tasks; +import java.util.Optional; import org.onap.dcaegen2.services.prh.exceptions.AAINotFoundException; +import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; +import org.onap.dcaegen2.services.prh.service.AAIConsumerClient; -public abstract class AAIConsumerTask<T, U, V, Z> extends Task<Z> { +public abstract class AAIConsumerTask<Request, Response, Conf> extends Task<Request, Response, Conf> { - protected abstract V consume(U message) throws AAINotFoundException; + abstract Optional<String> consume(ConsumerDmaapModel message) throws AAINotFoundException; - protected abstract T resolveClient(); + abstract AAIConsumerClient resolveClient(); } diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImpl.java index 3c2b2974..d0948aea 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImpl.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImpl.java @@ -1,4 +1,4 @@ -/*- +/* * ============LICENSE_START======================================================= * PNF-REGISTRATION-HANDLER * ================================================================================ @@ -17,10 +17,10 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.onap.dcaegen2.services.prh.tasks; import java.io.IOException; +import java.util.Optional; import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.onap.dcaegen2.services.prh.configuration.Config; @@ -34,7 +34,7 @@ import org.springframework.stereotype.Component; @Component public class AAIConsumerTaskImpl extends - AAIConsumerTask<AAIConsumerClient, ConsumerDmaapModel, Object, AAIClientConfiguration> { + AAIConsumerTask<ConsumerDmaapModel, String, AAIClientConfiguration> { private static final Logger logger = LoggerFactory.getLogger(AAIConsumerTaskImpl.class); @@ -47,7 +47,7 @@ public class AAIConsumerTaskImpl extends } @Override - protected Object consume(ConsumerDmaapModel consumerDmaapModel) throws AAINotFoundException { + Optional<String> consume(ConsumerDmaapModel consumerDmaapModel) throws AAINotFoundException { logger.trace("Method called with arg {}", consumerDmaapModel); try { return aaiConsumerClient.getHttpResponse(consumerDmaapModel); @@ -58,17 +58,12 @@ public class AAIConsumerTaskImpl extends } @Override - public Object execute(Object object) throws AAINotFoundException { - setAAIClientConfig(); - logger.trace("Method called with arg {}", object); - if (object instanceof ConsumerDmaapModel) { - return consume((ConsumerDmaapModel) object); - } - throw new AAINotFoundException("Incorrect object type"); - } - - protected void setAAIClientConfig() { + public String execute(ConsumerDmaapModel consumerDmaapModel) throws AAINotFoundException { + consumerDmaapModel = Optional.ofNullable(consumerDmaapModel) + .orElseThrow(() -> new AAINotFoundException("Invoked null object to AAI task")); + logger.trace("Method called with arg {}", consumerDmaapModel); aaiConsumerClient = resolveClient(); + return consume(consumerDmaapModel).orElseThrow(() -> new AAINotFoundException("Null response code")); } @Override @@ -77,7 +72,7 @@ public class AAIConsumerTaskImpl extends } @Override - protected AAIConsumerClient resolveClient() { - return new AAIConsumerClient(resolveConfiguration()); + AAIConsumerClient resolveClient() { + return Optional.ofNullable(aaiConsumerClient).orElseGet(() -> new AAIConsumerClient(resolveConfiguration())); } } diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTask.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTask.java index 23d5e93d..82362809 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTask.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTask.java @@ -20,13 +20,15 @@ package org.onap.dcaegen2.services.prh.tasks; import org.onap.dcaegen2.services.prh.exceptions.AAINotFoundException; +import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; +import org.onap.dcaegen2.services.prh.service.AAIProducerClient; /** * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/13/18 */ -public abstract class AAIProducerTask<T, U, V> extends Task { +public abstract class AAIProducerTask<Request, Response, Config> extends Task<Request, Response, Config> { - protected abstract V publish(U message) throws AAINotFoundException; + abstract ConsumerDmaapModel publish(ConsumerDmaapModel message) throws AAINotFoundException; - protected abstract T resolveClient(); + abstract AAIProducerClient resolveClient(); } diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImpl.java index 79d9b88d..7487d084 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImpl.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImpl.java @@ -20,7 +20,9 @@ package org.onap.dcaegen2.services.prh.tasks; import java.io.IOException; +import java.util.Optional; import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; +import org.onap.dcaegen2.services.prh.exceptions.DmaapNotFoundException; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.onap.dcaegen2.services.prh.configuration.Config; @@ -36,7 +38,8 @@ import org.springframework.stereotype.Component; * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/13/18 */ @Component -public class AAIProducerTaskImpl extends AAIProducerTask<AAIProducerClient, ConsumerDmaapModel, Object> { +public class AAIProducerTaskImpl extends + AAIProducerTask<ConsumerDmaapModel, ConsumerDmaapModel, AAIClientConfiguration> { private static final Logger logger = LoggerFactory.getLogger(AAIProducerTaskImpl.class); @@ -49,9 +52,8 @@ public class AAIProducerTaskImpl extends AAIProducerTask<AAIProducerClient, Cons } @Override - protected Object publish(ConsumerDmaapModel consumerDmaapModel) throws AAINotFoundException { + ConsumerDmaapModel publish(ConsumerDmaapModel consumerDmaapModel) throws AAINotFoundException { logger.trace("Method called with arg {}", consumerDmaapModel); - try { return aaiProducerClient.getHttpResponse(consumerDmaapModel) .filter(HttpUtils::isSuccessfulResponseCode).map(response -> consumerDmaapModel).orElseThrow(() -> @@ -63,19 +65,12 @@ public class AAIProducerTaskImpl extends AAIProducerTask<AAIProducerClient, Cons } @Override - public Object execute(Object object) throws AAINotFoundException { - setAAIClientConfig(); - logger.trace("Method called with arg {}", object); - - if (object instanceof ConsumerDmaapModel) { - return publish((ConsumerDmaapModel) object); - } - - throw new AAINotFoundException("Incorrect object type"); - } - - void setAAIClientConfig() { + public ConsumerDmaapModel execute(ConsumerDmaapModel consumerDmaapModel) throws AAINotFoundException { + consumerDmaapModel = Optional.ofNullable(consumerDmaapModel) + .orElseThrow(() -> new AAINotFoundException("Invoked null object to AAI task")); + logger.trace("Method called with arg {}", consumerDmaapModel); aaiProducerClient = resolveClient(); + return publish(consumerDmaapModel); } AAIClientConfiguration resolveConfiguration() { @@ -83,7 +78,7 @@ public class AAIProducerTaskImpl extends AAIProducerTask<AAIProducerClient, Cons } @Override - protected AAIProducerClient resolveClient() { - return new AAIProducerClient(resolveConfiguration()); + AAIProducerClient resolveClient() { + return Optional.ofNullable(aaiProducerClient).orElseGet(() -> new AAIProducerClient(resolveConfiguration())); } -} +}
\ No newline at end of file diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTask.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTask.java index 195eee7c..4bd8731c 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTask.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTask.java @@ -20,15 +20,17 @@ package org.onap.dcaegen2.services.prh.tasks; import org.onap.dcaegen2.services.prh.exceptions.DmaapNotFoundException; +import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; +import org.onap.dcaegen2.services.prh.service.consumer.ExtendedDmaapConsumerHttpClientImpl; /** * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 4/13/18 */ -public abstract class DmaapConsumerTask<T, U, V, Z> extends Task<Z> { +abstract class DmaapConsumerTask<Request, Response, Config> extends Task<Request, Response, Config> { - protected abstract V consume(U message) throws DmaapNotFoundException; + abstract ConsumerDmaapModel consume(String message) throws DmaapNotFoundException; - protected abstract T resolveClient(); + abstract ExtendedDmaapConsumerHttpClientImpl resolveClient(); abstract void initConfigs(); } diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java index dc868b3e..a78d33b2 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImpl.java @@ -19,6 +19,7 @@ */ package org.onap.dcaegen2.services.prh.tasks; +import java.util.Optional; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; @@ -38,11 +39,9 @@ import org.springframework.stereotype.Component; */ @Component public class DmaapConsumerTaskImpl extends - DmaapConsumerTask<ExtendedDmaapConsumerHttpClientImpl, String, ConsumerDmaapModel, DmaapConsumerConfiguration> { - + DmaapConsumerTask<String, ConsumerDmaapModel, DmaapConsumerConfiguration> { private static final Logger logger = LoggerFactory.getLogger(DmaapConsumerTaskImpl.class); - private final Config prhAppConfig; private ExtendedDmaapConsumerHttpClientImpl extendedDmaapConsumerHttpClient; @@ -52,14 +51,14 @@ public class DmaapConsumerTaskImpl extends } @Override - protected ConsumerDmaapModel consume(String message) throws DmaapNotFoundException { + ConsumerDmaapModel consume(String message) throws DmaapNotFoundException { logger.trace("Method called with arg {}", message); return DmaapConsumerJsonParser.getJsonObject(message); } @Override - public Object execute(Object object) throws PrhTaskException { - setDmaapClientConfig(); + public ConsumerDmaapModel execute(String object) throws PrhTaskException { + extendedDmaapConsumerHttpClient = resolveClient(); logger.trace("Method called with arg {}", object); return consume((extendedDmaapConsumerHttpClient.getHttpConsumerResponse().orElseThrow(() -> new PrhTaskException("DmaapConsumerTask has returned null")))); @@ -75,14 +74,9 @@ public class DmaapConsumerTaskImpl extends return prhAppConfig.getDmaapConsumerConfiguration(); } - protected void setDmaapClientConfig() { - extendedDmaapConsumerHttpClient = resolveClient(); - } - @Override - protected ExtendedDmaapConsumerHttpClientImpl resolveClient() { - return new ExtendedDmaapConsumerHttpClientImpl(resolveConfiguration()); + ExtendedDmaapConsumerHttpClientImpl resolveClient() { + return Optional.ofNullable(extendedDmaapConsumerHttpClient) + .orElseGet(() -> new ExtendedDmaapConsumerHttpClientImpl(resolveConfiguration())); } - - }
\ No newline at end of file diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTask.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTask.java index 8985133a..ab3e4696 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTask.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTask.java @@ -20,13 +20,15 @@ package org.onap.dcaegen2.services.prh.tasks; import org.onap.dcaegen2.services.prh.exceptions.DmaapNotFoundException; +import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; +import org.onap.dcaegen2.services.prh.service.producer.ExtendedDmaapProducerHttpClientImpl; /** * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/23/18 */ -public abstract class DmaapPublisherTask<T, U, V> extends Task<V> { +abstract class DmaapPublisherTask<Request, Response, Config> extends Task<Request, Response, Config> { - protected abstract String publish(U message) throws DmaapNotFoundException; + abstract String publish(ConsumerDmaapModel consumerDmaapModel) throws DmaapNotFoundException; - protected abstract T resolveClient(); + abstract ExtendedDmaapProducerHttpClientImpl resolveClient(); } diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java index 6450b18a..1a8f60fc 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImpl.java @@ -19,6 +19,7 @@ */ package org.onap.dcaegen2.services.prh.tasks; +import java.util.Optional; import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.onap.dcaegen2.services.prh.configuration.Config; @@ -37,7 +38,7 @@ import org.springframework.stereotype.Component; */ @Component public class DmaapPublisherTaskImpl extends - DmaapPublisherTask<ExtendedDmaapProducerHttpClientImpl, ConsumerDmaapModel, DmaapPublisherConfiguration> { + DmaapPublisherTask<ConsumerDmaapModel, String, DmaapPublisherConfiguration> { private static final Logger logger = LoggerFactory.getLogger(DmaapPublisherTaskImpl.class); private final Config prhAppConfig; @@ -49,7 +50,7 @@ public class DmaapPublisherTaskImpl extends } @Override - protected String publish(ConsumerDmaapModel consumerDmaapModel) throws DmaapNotFoundException { + String publish(ConsumerDmaapModel consumerDmaapModel) throws DmaapNotFoundException { logger.trace("Method called with arg {}", consumerDmaapModel); return extendedDmaapProducerHttpClient.getHttpProducerResponse(consumerDmaapModel) .filter(response -> !response.isEmpty() && response.equals(String.valueOf(HttpStatus.OK.value()))) @@ -57,13 +58,12 @@ public class DmaapPublisherTaskImpl extends } @Override - public Object execute(Object object) throws PrhTaskException { - if (object instanceof ConsumerDmaapModel) { - setDmaapClientConfig(); - logger.trace("Method called with arg {}", object); - return publish((ConsumerDmaapModel) object); - } - throw new DmaapNotFoundException("Incorrect object type"); + public String execute(ConsumerDmaapModel consumerDmaapModel) throws DmaapNotFoundException { + consumerDmaapModel = Optional.ofNullable(consumerDmaapModel) + .orElseThrow(() -> new DmaapNotFoundException("Invoked null object to Dmaap task")); + extendedDmaapProducerHttpClient = resolveClient(); + logger.trace("Method called with arg {}", consumerDmaapModel); + return publish(consumerDmaapModel); } @Override @@ -72,11 +72,8 @@ public class DmaapPublisherTaskImpl extends } @Override - protected ExtendedDmaapProducerHttpClientImpl resolveClient() { - return null; - } - - protected void setDmaapClientConfig() { - extendedDmaapProducerHttpClient = resolveClient(); + ExtendedDmaapProducerHttpClientImpl resolveClient() { + return Optional.ofNullable(extendedDmaapProducerHttpClient) + .orElseGet(() -> new ExtendedDmaapProducerHttpClientImpl(resolveConfiguration())); } }
\ No newline at end of file diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/ScheduledTasks.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/ScheduledTasks.java index 6b868d67..052de058 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/ScheduledTasks.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/ScheduledTasks.java @@ -50,7 +50,7 @@ public class ScheduledTasks { setTaskExecutionFlow(); try { dmaapConsumerTask.initConfigs(); - dmaapConsumerTask.receiveRequest(null); + dmaapConsumerTask.receiveRequest(""); } catch (PrhTaskException e) { logger .warn("Chain of tasks have been aborted, because some errors occur in prh workflow ", e); diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/Task.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/Task.java index 42f0405c..46e19dad 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/Task.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/Task.java @@ -26,7 +26,7 @@ import org.onap.dcaegen2.services.prh.exceptions.PrhTaskException; */ -public abstract class Task<T> { +public abstract class Task<Request, Response, Config> { private Task taskProcess; @@ -34,15 +34,15 @@ public abstract class Task<T> { this.taskProcess = task; } - public void receiveRequest(Object body) throws PrhTaskException { + public void receiveRequest(Request body) throws PrhTaskException { - Object response = execute(body); + Response response = execute(body); if (taskProcess != null) { taskProcess.receiveRequest(response); } } - abstract Object execute(Object object) throws PrhTaskException; + abstract Response execute(Request object) throws PrhTaskException; - abstract T resolveConfiguration(); + abstract Config resolveConfiguration(); } diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImplTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImplTest.java index 112b6960..570cb2a0 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImplTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIConsumerTaskImplTest.java @@ -33,6 +33,7 @@ import java.util.Optional; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; import org.onap.dcaegen2.services.prh.config.ImmutableAAIClientConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; @@ -81,91 +82,66 @@ class AAIConsumerTaskImplTest { } @Test - public void whenPassedObjectDoesntFit_ThrowsPrhTaskException() throws IOException { - //given - Object response = null; - - //when + public void whenPassedObjectDoesntFit_ThrowsPrhTaskException() { + //given/when when(appConfig.getAAIClientConfiguration()).thenReturn(aaiClientConfiguration); - try { - aaiConsumerTask = new AAIConsumerTaskImpl(appConfig); - response = aaiConsumerTask.execute("Some string"); - } catch (PrhTaskException e) { - e.printStackTrace(); - } + aaiConsumerTask = new AAIConsumerTaskImpl(appConfig); + Executable executableCode = () -> aaiConsumerTask.execute(null); //then - Assertions.assertNull(response); + Assertions + .assertThrows(PrhTaskException.class, executableCode, "Passing wrong object type to execute function"); + } @Test public void whenPassedObjectFits_ReturnsCorrectStatus() throws PrhTaskException, IOException { - //given - Object response; - aaiConsumerClient = mock(AAIConsumerClient.class); - - //when - when(aaiConsumerClient.getHttpResponse(consumerDmaapModel)).thenReturn(Optional.of("200")); - when(appConfig.getAAIClientConfiguration()).thenReturn(aaiClientConfiguration); - aaiConsumerTask = spy(new AAIConsumerTaskImpl(appConfig)); - when(aaiConsumerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration); - doReturn(aaiConsumerClient).when(aaiConsumerTask).resolveClient(); - aaiConsumerTask.setAAIClientConfig(); - response = aaiConsumerTask.execute(consumerDmaapModel); + //given/when + getAAIConsumerTask_WhenMockingHttpResponseCode("200", false); + String response = aaiConsumerTask.execute(consumerDmaapModel); //then verify(aaiConsumerClient, times(1)).getHttpResponse(any(ConsumerDmaapModel.class)); verifyNoMoreInteractions(aaiConsumerClient); - Assertions.assertNotNull(response); - Assertions.assertEquals(Optional.of("200"), response); - + Assertions.assertEquals("200", response); } @Test - public void whenPassedObjectFits_butIncorrectResponseReturns() throws IOException { - //given - Object response = null; - aaiConsumerClient = mock(AAIConsumerClient.class); - //when - when(aaiConsumerClient.getHttpResponse(consumerDmaapModel)).thenReturn(Optional.of("400")); - when(appConfig.getAAIClientConfiguration()).thenReturn(aaiClientConfiguration); - aaiConsumerTask = spy(new AAIConsumerTaskImpl(appConfig)); - when(aaiConsumerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration); - doReturn(aaiConsumerClient).when(aaiConsumerTask).resolveClient(); - aaiConsumerTask.setAAIClientConfig(); - try { - response = aaiConsumerTask.execute(consumerDmaapModel); - } catch (PrhTaskException e) { - e.printStackTrace(); - } + public void whenPassedObjectFits_butIncorrectResponseReturns() throws IOException, AAINotFoundException { + //given/when + getAAIConsumerTask_WhenMockingHttpResponseCode("400", false); + String response = aaiConsumerTask.execute(consumerDmaapModel); //then verify(aaiConsumerClient, times(1)).getHttpResponse(any(ConsumerDmaapModel.class)); verifyNoMoreInteractions(aaiConsumerClient); - Assertions.assertEquals(Optional.of("400"), response); + Assertions.assertEquals("400", response); } @Test - public void whenPassedObjectFits_ThrowsIOException() throws IOException { - //given - Object response = null; + public void whenPassedObjectFits_ThrowsIOExceptionAndHandleIt() throws IOException { + //given/when + getAAIConsumerTask_WhenMockingHttpResponseCode(null, true); + Executable executableCode = () -> aaiConsumerTask.execute(any(ConsumerDmaapModel.class)); + Assertions + .assertThrows(PrhTaskException.class, executableCode, "HttpClient throws IOException"); + + //then + verifyNoMoreInteractions(aaiConsumerClient); + } + + + private static void getAAIConsumerTask_WhenMockingHttpResponseCode(String httpResponseCode, boolean throwsException) + throws IOException { aaiConsumerClient = mock(AAIConsumerClient.class); - //when + if (throwsException) { + when(aaiConsumerClient.getHttpResponse(consumerDmaapModel)).thenThrow(IOException.class); + } else { + when(aaiConsumerClient.getHttpResponse(consumerDmaapModel)).thenReturn(Optional.of(httpResponseCode)); + } when(appConfig.getAAIClientConfiguration()).thenReturn(aaiClientConfiguration); - when(aaiConsumerClient.getHttpResponse(consumerDmaapModel)).thenThrow(IOException.class); aaiConsumerTask = spy(new AAIConsumerTaskImpl(appConfig)); when(aaiConsumerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration); doReturn(aaiConsumerClient).when(aaiConsumerTask).resolveClient(); - aaiConsumerTask.setAAIClientConfig(); - try { - response = aaiConsumerTask.execute(consumerDmaapModel); - } catch (AAINotFoundException e) { - e.printStackTrace(); - } - - //then - verify(aaiConsumerClient, times(1)).getHttpResponse(any(ConsumerDmaapModel.class)); - verifyNoMoreInteractions(aaiConsumerClient); - Assertions.assertNull(response); } }
\ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImplTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImplTest.java index 40fd20af..c82a98e4 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImplTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/AAIProducerTaskImplTest.java @@ -34,13 +34,14 @@ import java.util.Optional; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import org.onap.dcaegen2.services.prh.config.AAIClientConfiguration; import org.onap.dcaegen2.services.prh.config.ImmutableAAIClientConfiguration; +import org.onap.dcaegen2.services.prh.exceptions.PrhTaskException; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; import org.onap.dcaegen2.services.prh.model.ImmutableConsumerDmaapModel; import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.onap.dcaegen2.services.prh.exceptions.AAINotFoundException; -import org.onap.dcaegen2.services.prh.exceptions.PrhTaskException; import org.onap.dcaegen2.services.prh.service.AAIProducerClient; /** @@ -48,8 +49,6 @@ import org.onap.dcaegen2.services.prh.service.AAIProducerClient; */ class AAIProducerTaskImplTest { - private static ConsumerDmaapModel consumerDmaapModel; - private static AAIProducerTaskImpl aaiProducerTask; private static final String AAI_HOST = "/aai/v11/network/pnfs/pnf/NOKQTFCOC540002E"; private static final Integer PORT = 1234; @@ -58,6 +57,8 @@ class AAIProducerTaskImplTest { private static final String BASE_PATH = "/aai/v11"; private static final String PNF_PATH = "/network/pnfs/pnf"; + private static ConsumerDmaapModel consumerDmaapModel; + private static AAIProducerTaskImpl aaiProducerTask; private static AAIClientConfiguration aaiClientConfiguration; private static AAIProducerClient aaiProducerClient; private static AppConfig appConfig; @@ -82,91 +83,68 @@ class AAIProducerTaskImplTest { } @Test - public void whenPassedObjectDoesntFit_ThrowsPrhTaskException() throws IOException { - //given - Object response = null; - - //when + public void whenPassedObjectDoesntFit_ThrowsPrhTaskException() { + //given/when/ when(appConfig.getAAIClientConfiguration()).thenReturn(aaiClientConfiguration); - try { - aaiProducerTask = new AAIProducerTaskImpl(appConfig); - response = aaiProducerTask.execute("Some string"); - } catch (PrhTaskException e) { - e.printStackTrace(); - } + aaiProducerTask = new AAIProducerTaskImpl(appConfig); + Executable executableCode = () -> aaiProducerTask.execute(null); //then - Assertions.assertNull(response); + Assertions + .assertThrows(PrhTaskException.class, executableCode, "Passing wrong object type to execute function"); } @Test public void whenPassedObjectFits_ReturnsCorrectStatus() throws AAINotFoundException, IOException { - //given - Object response; - aaiProducerClient = mock(AAIProducerClient.class); - - //when - when(aaiProducerClient.getHttpResponse(consumerDmaapModel)).thenReturn(Optional.of(200)); - when(appConfig.getAAIClientConfiguration()).thenReturn(aaiClientConfiguration); - aaiProducerTask = spy(new AAIProducerTaskImpl(appConfig)); - when(aaiProducerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration); - doReturn(aaiProducerClient).when(aaiProducerTask).resolveClient(); - aaiProducerTask.setAAIClientConfig(); - response = aaiProducerTask.execute(consumerDmaapModel); + //given/when + getAAIProducerTask_whenMockingResponseObject(200, false); + ConsumerDmaapModel response = aaiProducerTask.execute(consumerDmaapModel); //then verify(aaiProducerClient, times(1)).getHttpResponse(any(ConsumerDmaapModel.class)); verifyNoMoreInteractions(aaiProducerClient); - Assertions.assertNotNull(response); Assertions.assertEquals(consumerDmaapModel, response); } + @Test public void whenPassedObjectFits_butIncorrectResponseReturns() throws IOException { - //given - Object response = null; - aaiProducerClient = mock(AAIProducerClient.class); - //when - when(aaiProducerClient.getHttpResponse(consumerDmaapModel)).thenReturn(Optional.of(400)); - when(appConfig.getAAIClientConfiguration()).thenReturn(aaiClientConfiguration); - aaiProducerTask = spy(new AAIProducerTaskImpl(appConfig)); - when(aaiProducerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration); - doReturn(aaiProducerClient).when(aaiProducerTask).resolveClient(); - aaiProducerTask.setAAIClientConfig(); - try { - response = aaiProducerTask.execute(consumerDmaapModel); - } catch (AAINotFoundException e) { - e.printStackTrace(); - } - + //given/when + getAAIProducerTask_whenMockingResponseObject(400, false); + Executable executableCode = () -> aaiProducerTask.execute(consumerDmaapModel); + Assertions + .assertThrows(PrhTaskException.class, executableCode, "Incorrect status code in response message"); //then verify(aaiProducerClient, times(1)).getHttpResponse(any(ConsumerDmaapModel.class)); verifyNoMoreInteractions(aaiProducerClient); - Assertions.assertNull(response); } @Test - public void whenPassedObjectFits_ThrowsIOException() throws IOException { - //given - Object response = null; + public void whenPassedObjectFits_butHTTPClientThrowsIOExceptionHandleIt() throws IOException { + //given/when + getAAIProducerTask_whenMockingResponseObject(0, true); + + Executable executableCode = () -> aaiProducerTask.execute(consumerDmaapModel); + Assertions + .assertThrows(PrhTaskException.class, executableCode, ""); + //then + verify(aaiProducerClient, times(1)).getHttpResponse(any(ConsumerDmaapModel.class)); + verifyNoMoreInteractions(aaiProducerClient); + } + + + private static void getAAIProducerTask_whenMockingResponseObject(int statusCode, boolean throwsException) + throws IOException { aaiProducerClient = mock(AAIProducerClient.class); - //when + if (throwsException) { + when(aaiProducerClient.getHttpResponse(consumerDmaapModel)).thenThrow(IOException.class); + } else { + when(aaiProducerClient.getHttpResponse(consumerDmaapModel)).thenReturn(Optional.of(statusCode)); + } when(appConfig.getAAIClientConfiguration()).thenReturn(aaiClientConfiguration); - when(aaiProducerClient.getHttpResponse(consumerDmaapModel)).thenThrow(IOException.class); aaiProducerTask = spy(new AAIProducerTaskImpl(appConfig)); when(aaiProducerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration); doReturn(aaiProducerClient).when(aaiProducerTask).resolveClient(); - aaiProducerTask.setAAIClientConfig(); - try { - response = aaiProducerTask.execute(consumerDmaapModel); - } catch (AAINotFoundException e) { - e.printStackTrace(); - } - - //then - verify(aaiProducerClient, times(1)).getHttpResponse(any(ConsumerDmaapModel.class)); - verifyNoMoreInteractions(aaiProducerClient); - Assertions.assertNull(response); } }
\ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImplTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImplTest.java index 786c04f9..373fc28a 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImplTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskImplTest.java @@ -31,6 +31,7 @@ import java.util.Optional; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; import org.onap.dcaegen2.services.prh.config.ImmutableDmaapConsumerConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; @@ -49,7 +50,7 @@ class DmaapConsumerTaskImplTest { private static ExtendedDmaapConsumerHttpClientImpl extendedDmaapConsumerHttpClient; private static AppConfig appConfig; private static DmaapConsumerConfiguration dmaapConsumerConfiguration; - + private static String message; @BeforeAll public static void setUp() { @@ -62,56 +63,40 @@ class DmaapConsumerTaskImplTest { .ipv6("0:0:0:0:0:FFFF:0A10:7BEA") .pnfName("NOKQTFCOC540002E").build(); appConfig = mock(AppConfig.class); + message = + "{\"event\":{\"commonEventHeader\":{\"domain\":\"other\",\"eventId\":\"<<SerialNumber>>-reg\",\"eventName\"" + + ":\"pnfRegistration_5GDU\",\"eventType\":\"pnfRegistration\",\"internalHeaderFields\":{}," + + "\"lastEpochMicrosec\":1519837825682,\"nfNamingCode\":\"5GRAN\",\"nfcNamingCode\":\"5DU\"," + + "\"priority\":\"Normal\",\"reportingEntityName\":\"5GRAN_DU\",\"sequence\":0,\"sourceId\":" + + "\"<<SerialNumber>>\",\"sourceName\":\"5GRAN_DU\",\"startEpochMicrosec\":1519837825682,\"version\":3}," + + "\"otherFields\":{\"otherFieldsVersion\":1,\"pnfFamily\":\"BBU\",\"pnfLastServiceDate\":1517206400," + + "\"pnfManufactureDate\":1516406400,\"pnfModelNumber\":\"AJ02\",\"pnfOamIpv4Address\":\"10.16.123.234\"," + + "\"pnfOamIpv6Address\":\"0:0:0:0:0:FFFF:0A10:7BEA\",\"pnfSerialNumber\":\"QTFCOC540002E\"," + + "\"pnfSoftwareVersion\":\"v4.5.0.1\",\"pnfType\":\"AirScale\",\"pnfVendorName\":\"Nokia\"}}}"; } @Test - public void whenPassedObjectDoesntFit_DoNotThrowsPrhTaskException() { + public void whenPassedObjectDoesntFit_DoesNotThrowPrhTaskException() { //given - Object response = null; - extendedDmaapConsumerHttpClient = mock(ExtendedDmaapConsumerHttpClientImpl.class); + prepareMocksForDmaapConsumer(Optional.empty()); //when - when(extendedDmaapConsumerHttpClient.getHttpConsumerResponse()).thenReturn(Optional.empty()); - when(appConfig.getDmaapConsumerConfiguration()).thenReturn(dmaapConsumerConfiguration); - dmaapConsumerTask = spy(new DmaapConsumerTaskImpl(appConfig)); - when(dmaapConsumerTask.resolveConfiguration()).thenReturn(dmaapConsumerConfiguration); - doReturn(extendedDmaapConsumerHttpClient).when(dmaapConsumerTask).resolveClient(); - dmaapConsumerTask.setDmaapClientConfig(); - try { - response = dmaapConsumerTask.execute(consumerDmaapModel); - } catch (PrhTaskException e) { - e.printStackTrace(); - } + Executable executableFunction = () -> dmaapConsumerTask.execute("Sample input"); //then + Assertions + .assertThrows(PrhTaskException.class, executableFunction, + "Throwing exception when http response code won't fit to assignment range"); verify(extendedDmaapConsumerHttpClient, times(1)).getHttpConsumerResponse(); verifyNoMoreInteractions(extendedDmaapConsumerHttpClient); - Assertions.assertNull(response); } @Test public void whenPassedObjectFits_ReturnsCorrectResponse() throws PrhTaskException { //given - Object response; - extendedDmaapConsumerHttpClient = mock(ExtendedDmaapConsumerHttpClientImpl.class); - String message = - "{\"event\":{\"commonEventHeader\":{\"domain\":\"other\",\"eventId\":\"<<SerialNumber>>-reg\",\"eventName\"" - + ":\"pnfRegistration_5GDU\",\"eventType\":\"pnfRegistration\",\"internalHeaderFields\":{}," - + "\"lastEpochMicrosec\":1519837825682,\"nfNamingCode\":\"5GRAN\",\"nfcNamingCode\":\"5DU\"," - + "\"priority\":\"Normal\",\"reportingEntityName\":\"5GRAN_DU\",\"sequence\":0,\"sourceId\":" - + "\"<<SerialNumber>>\",\"sourceName\":\"5GRAN_DU\",\"startEpochMicrosec\":1519837825682,\"version\":3}," - + "\"otherFields\":{\"otherFieldsVersion\":1,\"pnfFamily\":\"BBU\",\"pnfLastServiceDate\":1517206400," - + "\"pnfManufactureDate\":1516406400,\"pnfModelNumber\":\"AJ02\",\"pnfOamIpv4Address\":\"10.16.123.234\"," - + "\"pnfOamIpv6Address\":\"0:0:0:0:0:FFFF:0A10:7BEA\",\"pnfSerialNumber\":\"QTFCOC540002E\"," - + "\"pnfSoftwareVersion\":\"v4.5.0.1\",\"pnfType\":\"AirScale\",\"pnfVendorName\":\"Nokia\"}}}"; + prepareMocksForDmaapConsumer(Optional.of(message)); //when - when(extendedDmaapConsumerHttpClient.getHttpConsumerResponse()).thenReturn(Optional.of(message)); - when(appConfig.getDmaapConsumerConfiguration()).thenReturn(dmaapConsumerConfiguration); - dmaapConsumerTask = spy(new DmaapConsumerTaskImpl(appConfig)); - when(dmaapConsumerTask.resolveConfiguration()).thenReturn(dmaapConsumerConfiguration); - doReturn(extendedDmaapConsumerHttpClient).when(dmaapConsumerTask).resolveClient(); - dmaapConsumerTask.setDmaapClientConfig(); - response = dmaapConsumerTask.execute(consumerDmaapModel); + ConsumerDmaapModel response = dmaapConsumerTask.execute("Sample input"); //then verify(extendedDmaapConsumerHttpClient, times(1)).getHttpConsumerResponse(); @@ -120,4 +105,13 @@ class DmaapConsumerTaskImplTest { Assertions.assertEquals(consumerDmaapModel, response); } + + private void prepareMocksForDmaapConsumer(Optional<String> message) { + extendedDmaapConsumerHttpClient = mock(ExtendedDmaapConsumerHttpClientImpl.class); + when(extendedDmaapConsumerHttpClient.getHttpConsumerResponse()).thenReturn(message); + when(appConfig.getDmaapConsumerConfiguration()).thenReturn(dmaapConsumerConfiguration); + dmaapConsumerTask = spy(new DmaapConsumerTaskImpl(appConfig)); + when(dmaapConsumerTask.resolveConfiguration()).thenReturn(dmaapConsumerConfiguration); + doReturn(extendedDmaapConsumerHttpClient).when(dmaapConsumerTask).resolveClient(); + } }
\ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java index e4609193..41d46f66 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapPublisherTaskImplTest.java @@ -28,11 +28,11 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; -import java.io.IOException; import java.util.Optional; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration; import org.onap.dcaegen2.services.prh.config.ImmutableDmaapPublisherConfiguration; import org.onap.dcaegen2.services.prh.configuration.AppConfig; @@ -66,67 +66,57 @@ class DmaapPublisherTaskImplTest { } @Test - public void whenPassedObjectDoesntFit_ThrowsPrhTaskException() throws IOException { + public void whenPassedObjectDoesntFit_ThrowsPrhTaskException() { //given - Object response = null; + when(appConfig.getDmaapPublisherConfiguration()).thenReturn(dmaapPublisherConfiguration); + dmaapPublisherTask = new DmaapPublisherTaskImpl(appConfig); //when - when(appConfig.getDmaapPublisherConfiguration()).thenReturn(dmaapPublisherConfiguration); - try { - dmaapPublisherTask = new DmaapPublisherTaskImpl(appConfig); - response = dmaapPublisherTask.execute(""); - } catch (PrhTaskException e) { - e.printStackTrace(); - } + Executable executableFunction = () -> dmaapPublisherTask.execute(null); //then - Assertions.assertNull(response); + Assertions + .assertThrows(PrhTaskException.class, executableFunction, "The specified parameter is incorrect"); } @Test public void whenPassedObjectFits_ReturnsCorrectStatus() throws PrhTaskException { //given - Object response; - extendedDmaapProducerHttpClient = mock(ExtendedDmaapProducerHttpClientImpl.class); + prepareMocksForTests(HttpStatus.OK.toString()); //when - when(extendedDmaapProducerHttpClient.getHttpProducerResponse(consumerDmaapModel)) - .thenReturn(Optional.of(HttpStatus.OK.toString())); - when(appConfig.getDmaapPublisherConfiguration()).thenReturn(dmaapPublisherConfiguration); - dmaapPublisherTask = spy(new DmaapPublisherTaskImpl(appConfig)); - when(dmaapPublisherTask.resolveConfiguration()).thenReturn(dmaapPublisherConfiguration); - doReturn(extendedDmaapProducerHttpClient).when(dmaapPublisherTask).resolveClient(); - response = dmaapPublisherTask.execute(consumerDmaapModel); + String response = dmaapPublisherTask.execute(consumerDmaapModel); //then verify(extendedDmaapProducerHttpClient, times(1)) .getHttpProducerResponse(any(ConsumerDmaapModel.class)); verifyNoMoreInteractions(extendedDmaapProducerHttpClient); - Assertions.assertNotNull(response); Assertions.assertEquals(HttpStatus.OK.toString(), response); } @Test public void whenPassedObjectFits_butIncorrectResponseReturns() { //given - Object response = null; - extendedDmaapProducerHttpClient = mock(ExtendedDmaapProducerHttpClientImpl.class); + prepareMocksForTests("400"); + //when + Executable executableFunction = () -> dmaapPublisherTask.execute(consumerDmaapModel); + + //then + Assertions + .assertThrows(PrhTaskException.class, executableFunction, "Incorrect response from DMAAP"); + verify(extendedDmaapProducerHttpClient, times(1)).getHttpProducerResponse(any(ConsumerDmaapModel.class)); + verifyNoMoreInteractions(extendedDmaapProducerHttpClient); + } + + + private void prepareMocksForTests(String httpResponseCode) { + extendedDmaapProducerHttpClient = mock(ExtendedDmaapProducerHttpClientImpl.class); when(extendedDmaapProducerHttpClient.getHttpProducerResponse(consumerDmaapModel)) - .thenReturn(Optional.of("400")); + .thenReturn(Optional.of(httpResponseCode)); when(appConfig.getDmaapPublisherConfiguration()).thenReturn(dmaapPublisherConfiguration); dmaapPublisherTask = spy(new DmaapPublisherTaskImpl(appConfig)); when(dmaapPublisherTask.resolveConfiguration()).thenReturn(dmaapPublisherConfiguration); doReturn(extendedDmaapProducerHttpClient).when(dmaapPublisherTask).resolveClient(); - try { - response = dmaapPublisherTask.execute(consumerDmaapModel); - } catch (PrhTaskException e) { - e.printStackTrace(); - } - - //then - verify(extendedDmaapProducerHttpClient, times(1)).getHttpProducerResponse(any(ConsumerDmaapModel.class)); - verifyNoMoreInteractions(extendedDmaapProducerHttpClient); - Assertions.assertNull(response); } }
\ No newline at end of file |