328df2dcde58879ce596412664051dd9715451d7
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA Intellectual Property. 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.dcaegen2.services.sdk.rest.services.adapters.http;
22
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import org.junit.jupiter.api.Test;
26 import reactor.core.publisher.Mono;
27 import reactor.netty.DisposableServer;
28 import reactor.netty.http.client.HttpClient;
29 import reactor.netty.http.server.HttpServer;
30 import reactor.netty.resources.ConnectionProvider;
31 import reactor.test.StepVerifier;
32
33 class CloudHttpClientIT {
34
35     private static final String SAMPLE_URL = "/sampleURL";
36     private static final int MAX_CONNECTIONS = 1;
37     private static final ConnectionProvider connectionProvider = ConnectionProvider.fixed("test", MAX_CONNECTIONS);
38
39     @Test
40     public void post() {
41         assertTrue(true);
42     }
43
44     @Test
45     public void patch() {
46         assertTrue(true);
47     }
48
49     @Test
50     void successfulGetResponse() {
51         String sampleString = "sampleString";
52         Mono<String> response = Mono.just(sampleString);
53         DisposableServer server =
54             HttpServer.create()
55                 .handle((req, resp) -> resp.sendString(response))
56                 .wiretap(true)
57                 .bindNow();
58         HttpClient httpClient = createHttpClientForContextWithAddress(server, connectionProvider);
59         CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient);
60
61         Mono<String> content = cloudHttpClient.get(SAMPLE_URL, String.class);
62
63         StepVerifier.create(content)
64             .expectNext(sampleString)
65             .expectComplete()
66             .verify();
67         server.disposeNow();
68     }
69
70     @Test
71     void errorGetRequest() {
72         DisposableServer server =
73             HttpServer.create()
74                 .handle((req, resp) -> Mono.error(new Exception("returnError")))
75                 .wiretap(true)
76                 .bindNow();
77         HttpClient httpClient = createHttpClientForContextWithAddress(server, connectionProvider);
78         CloudHttpClient cloudHttpClient = new CloudHttpClient(httpClient);
79
80         Mono<String> content = cloudHttpClient.get(SAMPLE_URL, String.class);
81
82         StepVerifier.create(content)
83             .expectError()
84             .verify();
85         server.disposeNow();
86     }
87
88     private HttpClient createHttpClientForContextWithAddress(DisposableServer context) {
89         return createHttpClientForContextWithAddress(context, null);
90     }
91
92     private HttpClient createHttpClientForContextWithAddress(DisposableServer disposableServer,
93         ConnectionProvider connectionProvider) {
94         HttpClient client = connectionProvider == null? HttpClient.create() : HttpClient.create(connectionProvider);
95         return client.addressSupplier(disposableServer::address).wiretap(true);
96     }
97 }