69a74b67674b562599e9e8cb627bac937971ea39
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020 Nordix Foundation. All rights reserved.
6  * ======================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.clients;
22
23 import io.netty.util.internal.logging.InternalLoggerFactory;
24 import io.netty.util.internal.logging.JdkLoggerFactory;
25
26 import java.io.IOException;
27
28 import okhttp3.mockwebserver.MockResponse;
29 import okhttp3.mockwebserver.MockWebServer;
30
31 import org.junit.jupiter.api.AfterAll;
32 import org.junit.jupiter.api.BeforeAll;
33 import org.junit.jupiter.api.Test;
34 import org.springframework.http.HttpHeaders;
35 import org.springframework.http.MediaType;
36 import org.springframework.web.reactive.function.client.WebClientResponseException;
37
38 import reactor.core.publisher.Mono;
39 import reactor.test.StepVerifier;
40 import reactor.util.Loggers;
41
42 class AsyncRestClientTest {
43     private static final String BASE_URL = "BaseUrl";
44     private static final String REQUEST_URL = "/test";
45     private static final String USERNAME = "username";
46     private static final String PASSWORD = "password";
47     private static final String TEST_JSON = "{\"type\":\"type1\"}";
48     private static final int SUCCESS_CODE = 200;
49     private static final int ERROR_CODE = 500;
50
51     private static MockWebServer mockWebServer;
52
53     private static AsyncRestClient clientUnderTest;
54
55     @BeforeAll
56     static void init() {
57         // skip a lot of unnecessary logs from MockWebServer
58         InternalLoggerFactory.setDefaultFactory(JdkLoggerFactory.INSTANCE);
59         Loggers.useJdkLoggers();
60         mockWebServer = new MockWebServer();
61         clientUnderTest =
62                 new AsyncRestClient(mockWebServer.url(BASE_URL).toString(), null, null, new SecurityContext(""));
63     }
64
65     @AfterAll
66     static void tearDown() throws IOException {
67         mockWebServer.shutdown();
68     }
69
70     @Test
71     void testGetNoError() {
72         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
73                 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
74                 .setBody(TEST_JSON));
75
76         Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
77         StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
78     }
79
80     @Test
81     void testGetError() {
82         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
83
84         Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
85         StepVerifier.create(returnedMono)
86                 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
87     }
88
89     @Test
90     void testPutNoError() {
91         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
92                 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
93                 .setBody(TEST_JSON));
94
95         Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
96         StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
97     }
98
99     @Test
100     void testPutError() {
101         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
102
103         Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
104         StepVerifier.create(returnedMono)
105                 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
106     }
107
108     @Test
109     void testDeleteNoError() {
110         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE));
111
112         Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
113         StepVerifier.create(returnedMono).expectNext("").expectComplete().verify();
114     }
115
116     @Test
117     void testDeleteError() {
118         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
119
120         Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
121         StepVerifier.create(returnedMono)
122                 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
123     }
124
125     @Test
126     void testPostNoError() {
127         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
128                 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
129                 .setBody(TEST_JSON));
130
131         Mono<String> returnedMono = clientUnderTest.post(REQUEST_URL, TEST_JSON);
132         StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
133     }
134
135     @Test
136     void testPostError() {
137         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
138
139         Mono<String> returnedMono = clientUnderTest.post(REQUEST_URL, TEST_JSON);
140         StepVerifier.create(returnedMono)
141                 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
142     }
143
144     @Test
145     void testPostWithAuthHeaderNoError() {
146         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
147                 .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
148                 .setBody(TEST_JSON));
149
150         Mono<String> returnedMono = clientUnderTest.postWithAuthHeader(REQUEST_URL, TEST_JSON, USERNAME, PASSWORD);
151         StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
152     }
153
154     @Test
155     void testPostWithAuthHeaderError() {
156         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
157
158         Mono<String> returnedMono = clientUnderTest.postWithAuthHeader(REQUEST_URL, TEST_JSON, USERNAME, PASSWORD);
159         StepVerifier.create(returnedMono)
160                 .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
161     }
162 }