diff options
author | kjaniak <kornel.janiak@nokia.com> | 2019-05-16 16:53:14 +0200 |
---|---|---|
committer | kjaniak <kornel.janiak@nokia.com> | 2019-05-16 16:54:46 +0200 |
commit | 1706275bec82d24b52f858b72d4d4c35a862d070 (patch) | |
tree | f94b6500fc6e768030caba60cf57916911b60648 /rest-services/dmaap-client/src | |
parent | e8beff45a8cde47485d44b692ef579ce2b755f69 (diff) |
Write unit tests for MR Subscriber
Change-Id: I9ce78cc74898ded8ba6b01513cca9463cb78c345
Issue-ID: DCAEGEN2-1422
Signed-off-by: kjaniak <kornel.janiak@nokia.com>
Diffstat (limited to 'rest-services/dmaap-client/src')
3 files changed, 97 insertions, 32 deletions
diff --git a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/impl/MessageRouterSubscriberImpl.java b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/impl/MessageRouterSubscriberImpl.java index 2f49ddf5..2f2e4214 100644 --- a/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/impl/MessageRouterSubscriberImpl.java +++ b/rest-services/dmaap-client/src/main/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/impl/MessageRouterSubscriberImpl.java @@ -60,6 +60,14 @@ public class MessageRouterSubscriberImpl implements MessageRouterSubscriber { } + private @NotNull HttpRequest buildGetHttpRequest(MessageRouterSubscribeRequest request) { + return ImmutableHttpRequest.builder() + .method(HttpMethod.GET) + .url(buildSubscribeUrl(request)) + .diagnosticContext(request.diagnosticContext().withNewInvocationId()) + .build(); + } + private @NotNull MessageRouterSubscribeResponse buildGetResponse(HttpResponse httpResponse) { final ImmutableMessageRouterSubscribeResponse.Builder builder = ImmutableMessageRouterSubscribeResponse.builder(); @@ -69,14 +77,6 @@ public class MessageRouterSubscriberImpl implements MessageRouterSubscriber { } - private @NotNull HttpRequest buildGetHttpRequest(MessageRouterSubscribeRequest request) { - return ImmutableHttpRequest.builder() - .method(HttpMethod.GET) - .url(buildSubscribeUrl(request)) - .diagnosticContext(request.diagnosticContext().withNewInvocationId()) - .build(); - } - private String buildSubscribeUrl(MessageRouterSubscribeRequest request) { return String.format("%s/%s/%s", request.sourceDefinition().topicUrl(), request.consumerGroup(), request.consumerId()); diff --git a/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/impl/MessageRouterPublisherImplTest.java b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/impl/MessageRouterPublisherImplTest.java index 103b4807..f0138d2c 100644 --- a/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/impl/MessageRouterPublisherImplTest.java +++ b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/impl/MessageRouterPublisherImplTest.java @@ -77,7 +77,7 @@ class MessageRouterPublisherImplTest { .statusCode(200) .statusReason("OK") .url(sinkDefinition.topicUrl()) - .rawBody("{}".getBytes()) + .rawBody("[]".getBytes()) .build(); @Test diff --git a/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/impl/MessageRouterSubscriberImplTest.java b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/impl/MessageRouterSubscriberImplTest.java index c9587206..ef2cb5ef 100644 --- a/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/impl/MessageRouterSubscriberImplTest.java +++ b/rest-services/dmaap-client/src/test/java/org/onap/dcaegen2/services/sdk/rest/services/dmaap/client/impl/MessageRouterSubscriberImplTest.java @@ -20,48 +20,113 @@ package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.impl; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; -import com.google.gson.JsonPrimitive; -import org.junit.jupiter.api.Disabled; +import com.google.gson.JsonSyntaxException; import org.junit.jupiter.api.Test; -import org.onap.dcaegen2.services.sdk.model.streams.dmaap.MessageRouterSink; +import org.mockito.ArgumentCaptor; +import org.onap.dcaegen2.services.sdk.model.streams.dmaap.ImmutableMessageRouterSource; import org.onap.dcaegen2.services.sdk.model.streams.dmaap.MessageRouterSource; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.*; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.api.MessageRouterSubscriber; -import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.DmaapResponse; -import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterPublishRequest; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.ImmutableMessageRouterSubscribeRequest; -import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishRequest; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeRequest; import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterSubscribeResponse; -import reactor.core.publisher.Flux; +import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.config.MessageRouterSubscriberConfig; +import reactor.core.publisher.Mono; /** * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a> - * @since March 2019 + * @since May 2019 */ -// TODO: Write proper unit tests -@Disabled class MessageRouterSubscriberImplTest { - private final MessageRouterSubscriber cut = mock(MessageRouterSubscriber.class); - private final MessageRouterSource sinkDefinition = mock(MessageRouterSource.class); - private final MessageRouterSubscribeRequest request = ImmutableMessageRouterSubscribeRequest.builder() - .sourceDefinition(sinkDefinition) + private final RxHttpClient httpClient = mock(RxHttpClient.class); + private final MessageRouterSubscriberConfig clientConfig = MessageRouterSubscriberConfig.createDefault(); + private final MessageRouterSubscriber cut = new MessageRouterSubscriberImpl(httpClient, clientConfig.gsonInstance()); + + private final ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor = ArgumentCaptor.forClass(HttpRequest.class); + private final MessageRouterSource sourceDefinition = ImmutableMessageRouterSource.builder() + .name("sample topic") + .topicUrl("https://dmaap-mr/TOPIC") + .build(); + private final MessageRouterSubscribeRequest mrRequest = ImmutableMessageRouterSubscribeRequest.builder() + .consumerGroup("SAMPLE-GROUP") + .sourceDefinition(sourceDefinition) + .build(); + private final HttpResponse httpResponse = ImmutableHttpResponse.builder() + .statusCode(200) + .statusReason("OK") + .url(sourceDefinition.topicUrl()) + .rawBody("[]".getBytes()) + .build(); + private final HttpResponse httpResponseWithWrongStatusCode = ImmutableHttpResponse.builder() + .statusCode(301) + .statusReason("Something braked") + .url(sourceDefinition.topicUrl()) + .rawBody("[]".getBytes()) + .build(); + private final HttpResponse httpResponseWithIncorrectJson = ImmutableHttpResponse.builder() + .statusCode(200) + .statusReason("OK") + .url(sourceDefinition.topicUrl()) + .rawBody("{}".getBytes()) .build(); @Test - void getShouldBeUsable() { - cut.get(request) - .filter(DmaapResponse::successful) - .map(MessageRouterSubscribeResponse::items) - .subscribe(System.out::println); + void getWithProperRequest_shouldReturnCorrectResponse() { + // given + given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponse)); + + // when + final Mono<MessageRouterSubscribeResponse> responses = cut + .get(mrRequest); + final MessageRouterSubscribeResponse response = responses.block(); + + // then + assertThat(response.successful()).isTrue(); + assertThat(response.failReason()).isNull(); + assertThat(response.hasElements()).isFalse(); + + + verify(httpClient).call(httpRequestArgumentCaptor.capture()); + final HttpRequest httpRequest = httpRequestArgumentCaptor.getValue(); + assertThat(httpRequest.method()).isEqualTo(HttpMethod.GET); + assertThat(httpRequest.url()).isEqualTo(String.format("%s/%s/%s", sourceDefinition.topicUrl(), + mrRequest.consumerGroup(), mrRequest.consumerId())); + assertThat(httpRequest.body()).isNull(); } @Test - void getElementsShouldBeUsable() { - cut.getElements(request) - .collectList() - .subscribe(System.out::println); + void getWithProperRequestButNotSuccessfulHttpRequest_shouldReturnMonoWithFailReason() { + // given + given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithWrongStatusCode)); + + // when + final Mono<MessageRouterSubscribeResponse> responses = cut + .get(mrRequest); + final MessageRouterSubscribeResponse response = responses.block(); + + // then + assertThat(response.failed()).isTrue(); + assertThat(response.failReason()). + isEqualTo(String.format("%d %s%n%s", httpResponseWithWrongStatusCode.statusCode(), + httpResponseWithWrongStatusCode.statusReason(), + httpResponseWithWrongStatusCode.bodyAsString())); + } + + @Test + void getWithImproperRawBody_shouldThrowNPE() { + // given + given(httpClient.call(any(HttpRequest.class))).willReturn(Mono.just(httpResponseWithIncorrectJson)); + + // when + // then + assertThatExceptionOfType(JsonSyntaxException.class).isThrownBy(() -> cut.get(mrRequest).block()); } }
\ No newline at end of file |