a1297bd7c0a6e9ec4fc6f76d9d3519bbe3379fec
[vid.git] / vid-app-common / src / test / java / org / onap / vid / client / SyncRestClientForHttpsServerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2018 - 2019 Nokia. 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.vid.client;
22
23 import io.joshworks.restclient.http.HttpResponse;
24 import io.joshworks.restclient.http.JsonNode;
25 import org.apache.http.impl.client.CloseableHttpClient;
26 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
27 import org.apache.http.conn.socket.ConnectionSocketFactory;
28 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
29 import com.fasterxml.jackson.core.JsonProcessingException;
30 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import org.apache.http.impl.client.HttpClientBuilder;
33 import org.apache.http.conn.ssl.SSLContextBuilder;
34 import com.xebialabs.restito.semantics.Condition;
35 import com.xebialabs.restito.server.StubServer;
36 import com.xebialabs.restito.semantics.Action;
37 import org.apache.http.config.RegistryBuilder;
38 import org.testng.annotations.BeforeMethod;
39 import org.testng.annotations.AfterMethod;
40 import org.glassfish.grizzly.http.Method;
41 import org.apache.http.client.HttpClient;
42 import org.apache.http.config.Registry;
43 import org.testng.annotations.Test;
44
45 import java.security.GeneralSecurityException;
46 import javax.net.ssl.SSLContext;
47 import java.util.Collections;
48
49 import static com.xebialabs.restito.builder.verify.VerifyHttp.verifyHttp;
50 import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp;
51 import static com.xebialabs.restito.semantics.Action.stringContent;
52 import static com.xebialabs.restito.semantics.Action.contentType;
53 import static org.apache.http.client.config.RequestConfig.custom;
54 import static com.xebialabs.restito.semantics.Action.ok;
55 import static org.testng.Assert.assertEquals;
56
57 public class SyncRestClientForHttpsServerTest {
58
59     private static final SyncRestClientModel.TestModel testObject = new SyncRestClientModel.TestModel(1, "test");
60     private static final String[] SUPPORTED_PROTOCOLS = {"TLSv1", "TLSv1.2"};
61     private StubServer stubServer;
62     private ObjectMapper objectMapper = new ObjectMapper();
63
64     private SyncRestClient syncRestClient;
65
66     @BeforeMethod
67     public void setUp() throws GeneralSecurityException {
68         stubServer = new StubServer();
69         stubServer.secured().run();
70         syncRestClient = new SyncRestClient(createNaiveHttpClient());
71     }
72
73     @AfterMethod
74     public void tearDown() {
75         stubServer.stop();
76     }
77
78     @Test
79     public void testJsonResponseFromGet() throws JsonProcessingException {
80         // given
81         stubGetCall();
82         String securedUrl = "https://0.0.0.0:" + stubServer.getPort() + "/test";
83         String notSecuredUrl = "http://0.0.0.0:" + stubServer.getPort() + "/test";
84         // when
85         HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient
86             .get(securedUrl, Collections.emptyMap(), Collections.emptyMap());
87         // then
88         verifyHttp(stubServer)
89             .once(Condition.method(Method.GET), Condition.url(securedUrl), Condition.not(Condition.url(notSecuredUrl)));
90         assertEquals(jsonNodeHttpResponse.getStatus(), 200);
91         assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1);
92         assertEquals(jsonNodeHttpResponse.getBody().getObject().get("value"), "test");
93     }
94
95     @Test
96     public void testObjectResponseFromGet() throws JsonProcessingException {
97         // given
98         stubServer.run();
99         stubGetCall();
100         String securedUrl = "https://0.0.0.0:" + stubServer.getPort() + "/test";
101         String notSecuredUrl = "http://0.0.0.0:" + stubServer.getPort() + "/test";
102         // when
103         HttpResponse<SyncRestClientModel.TestModel> testModelHttpResponse = syncRestClient
104             .get(securedUrl, Collections.emptyMap(), Collections.emptyMap(), SyncRestClientModel.TestModel.class);
105         // then
106         verifyHttp(stubServer)
107             .once(Condition.method(Method.GET), Condition.url(securedUrl), Condition.not(Condition.url(notSecuredUrl)));
108         assertEquals(testModelHttpResponse.getStatus(), 200);
109         assertEquals(testModelHttpResponse.getBody().getKey(), 1);
110         assertEquals(testModelHttpResponse.getBody().getValue(), "test");
111     }
112
113     private Action jsonContent() throws JsonProcessingException {
114         return stringContent(objectMapper.writeValueAsString(testObject));
115     }
116
117     private void stubGetCall() throws JsonProcessingException {
118         whenHttp(stubServer)
119             .match(Condition.get("/test"))
120             .then(ok(), jsonContent(), contentType("application/json"));
121     }
122
123     private CloseableHttpClient createNaiveHttpClient() throws GeneralSecurityException {
124         final SSLContext context = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy())
125             .build();
126
127         final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(context, SUPPORTED_PROTOCOLS,
128             null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
129         Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
130             .register("https", socketFactory)
131             .build();
132
133         return HttpClientBuilder.create()
134             .setDefaultRequestConfig(custom().setConnectionRequestTimeout(10000).build())
135             .setConnectionManager(new PoolingHttpClientConnectionManager(registry))
136             .setSSLSocketFactory(socketFactory).build();
137     }
138
139 }