Added support for https
[portal/nonrtric-controlpanel.git] / webapp-backend / src / test / java / org / oransc / portal / nonrtric / controlpanel / util / AsyncRestClientTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
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.oransc.portal.nonrtric.controlpanel.util;
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 public class AsyncRestClientTest {
43     private static final String BASE_URL = "BaseUrl";
44     private static final String REQUEST_URL = "/test";
45     private static final String TEST_JSON = "{\"type\":\"type1\"}";
46     private static final int SUCCESS_CODE = 200;
47     private static final int ERROR_CODE = 500;
48
49     private static MockWebServer mockWebServer;
50
51     private static AsyncRestClient clientUnderTest;
52
53     @BeforeAll
54     public static void init() {
55         // skip a lot of unnecessary logs from MockWebServer
56         InternalLoggerFactory.setDefaultFactory(JdkLoggerFactory.INSTANCE);
57         Loggers.useJdkLoggers();
58         mockWebServer = new MockWebServer();
59         clientUnderTest = new AsyncRestClient(mockWebServer.url(BASE_URL).toString());
60     }
61
62     @AfterAll
63     public static void tearDown() throws IOException {
64         mockWebServer.shutdown();
65     }
66
67     @Test
68     public void testGetNoError() {
69         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
70             .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
71             .setBody(TEST_JSON));
72
73         Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
74         StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
75     }
76
77     @Test
78     public void testGetError() {
79         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
80
81         Mono<String> returnedMono = clientUnderTest.get(REQUEST_URL);
82         StepVerifier.create(returnedMono)
83             .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
84     }
85
86     @Test
87     public void testPutNoError() {
88         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE) //
89             .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) //
90             .setBody(TEST_JSON));
91
92         Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
93         StepVerifier.create(returnedMono).expectNext(TEST_JSON).expectComplete().verify();
94     }
95
96     @Test
97     public void testPutError() {
98         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
99
100         Mono<String> returnedMono = clientUnderTest.put(REQUEST_URL, TEST_JSON);
101         StepVerifier.create(returnedMono)
102             .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
103     }
104
105     @Test
106     public void testDeleteNoError() {
107         mockWebServer.enqueue(new MockResponse().setResponseCode(SUCCESS_CODE));
108
109         Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
110         StepVerifier.create(returnedMono).expectNext("").expectComplete().verify();
111     }
112
113     @Test
114     public void testDeleteError() {
115         mockWebServer.enqueue(new MockResponse().setResponseCode(ERROR_CODE));
116
117         Mono<String> returnedMono = clientUnderTest.delete(REQUEST_URL);
118         StepVerifier.create(returnedMono)
119             .expectErrorMatches(throwable -> throwable instanceof WebClientResponseException).verify();
120     }
121
122 }