Improve code coverage of AsyncRestClient 66/4866/2
authorRehanRaza <muhammad.rehan.raza@est.tech>
Tue, 20 Oct 2020 13:32:48 +0000 (15:32 +0200)
committerRehanRaza <muhammad.rehan.raza@est.tech>
Tue, 20 Oct 2020 15:28:49 +0000 (17:28 +0200)
Change-Id: I78bc0108fba4efa2074969b379405ae7487f24fd
Issue-ID: NONRTRIC-281
Signed-off-by: RehanRaza <muhammad.rehan.raza@est.tech>
webapp-backend/src/main/java/org/oransc/portal/nonrtric/controlpanel/util/AsyncRestClient.java
webapp-backend/src/test/java/org/oransc/portal/nonrtric/controlpanel/util/AsyncRestClientTest.java

index 17f3664..0519700 100644 (file)
 
 package org.oransc.portal.nonrtric.controlpanel.util;
 
-import io.netty.channel.ChannelOption;
-import io.netty.handler.ssl.SslContext;
-import io.netty.handler.ssl.SslContextBuilder;
-import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
-import io.netty.handler.timeout.ReadTimeoutHandler;
-import io.netty.handler.timeout.WriteTimeoutHandler;
-
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.lang.invoke.MethodHandles;
@@ -40,20 +33,23 @@ import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
-
 import org.immutables.value.Value;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
 import org.springframework.http.client.reactive.ReactorClientHttpConnector;
-import org.springframework.lang.Nullable;
 import org.springframework.util.ResourceUtils;
 import org.springframework.web.reactive.function.client.ExchangeStrategies;
 import org.springframework.web.reactive.function.client.WebClient;
 import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec;
 import org.springframework.web.reactive.function.client.WebClientResponseException;
-
+import io.netty.channel.ChannelOption;
+import io.netty.handler.ssl.SslContext;
+import io.netty.handler.ssl.SslContextBuilder;
+import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
+import io.netty.handler.timeout.ReadTimeoutHandler;
+import io.netty.handler.timeout.WriteTimeoutHandler;
 import reactor.core.publisher.Mono;
 import reactor.netty.http.client.HttpClient;
 import reactor.netty.resources.ConnectionProvider;
@@ -92,42 +88,6 @@ public class AsyncRestClient {
         this.clientConfig = config;
     }
 
-    public Mono<ResponseEntity<String>> postForEntity(String uri, @Nullable String body) {
-        Object traceTag = createTraceTag();
-        logger.debug("{} POST uri = '{}{}''", traceTag, baseUrl, uri);
-        logger.trace("{} POST body: {}", traceTag, body);
-        Mono<String> bodyProducer = body != null ? Mono.just(body) : Mono.empty();
-        return getWebClient() //
-            .flatMap(client -> {
-                RequestHeadersSpec<?> request = client.post() //
-                    .uri(uri) //
-                    .contentType(MediaType.APPLICATION_JSON) //
-                    .body(bodyProducer, String.class);
-                return retrieve(traceTag, request);
-            });
-    }
-
-    public Mono<String> post(String uri, @Nullable String body) {
-        return postForEntity(uri, body) //
-            .flatMap(this::toBody);
-    }
-
-    public Mono<String> postWithAuthHeader(String uri, String body, String username, String password) {
-        Object traceTag = createTraceTag();
-        logger.debug("{} POST (auth) uri = '{}{}''", traceTag, baseUrl, uri);
-        logger.trace("{} POST body: {}", traceTag, body);
-        return getWebClient() //
-            .flatMap(client -> {
-                RequestHeadersSpec<?> request = client.post() //
-                    .uri(uri) //
-                    .headers(headers -> headers.setBasicAuth(username, password)) //
-                    .contentType(MediaType.APPLICATION_JSON) //
-                    .bodyValue(body);
-                return retrieve(traceTag, request) //
-                    .flatMap(this::toBody);
-            });
-    }
-
     public Mono<ResponseEntity<String>> putForEntity(String uri, String body) {
         Object traceTag = createTraceTag();
         logger.debug("{} PUT uri = '{}{}''", traceTag, baseUrl, uri);
index 33527a4..f898956 100644 (file)
 
 package org.oransc.portal.nonrtric.controlpanel.util;
 
-import io.netty.util.internal.logging.InternalLoggerFactory;
-import io.netty.util.internal.logging.JdkLoggerFactory;
-
 import java.io.IOException;
-
-import okhttp3.mockwebserver.MockResponse;
-import okhttp3.mockwebserver.MockWebServer;
-
 import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.reactive.function.client.WebClientResponseException;
-
+import io.netty.util.internal.logging.InternalLoggerFactory;
+import io.netty.util.internal.logging.JdkLoggerFactory;
+import okhttp3.mockwebserver.MockResponse;
+import okhttp3.mockwebserver.MockWebServer;
 import reactor.core.publisher.Mono;
 import reactor.test.StepVerifier;
 import reactor.util.Loggers;
@@ -43,6 +40,8 @@ class AsyncRestClientTest {
     private static final String BASE_URL = "BaseUrl";
     private static final String REQUEST_URL = "/test";
     private static final String TEST_JSON = "{\"type\":\"type1\"}";
+    private static final String USERNAME = "user";
+    private static final String PASSWORD = "pass";
     private static final int SUCCESS_CODE = 200;
     private static final int ERROR_CODE = 500;
 
@@ -93,6 +92,15 @@ class AsyncRestClientTest {
         StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
     }
 
+    @Test
+    void testPutWithoutBodyNoError() {
+        mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
+            .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE));
+
+        Mono<ResponseEntity<String>> returnedMono = clientUnderTest.putForEntity(REQUEST_URL);
+        StepVerifier.create(returnedMono).expectNextCount(1).expectComplete().verify();
+    }
+
     @Test
     void testPutError() {
         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));