2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.client;
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;
45 import java.security.GeneralSecurityException;
46 import javax.net.ssl.SSLContext;
47 import java.util.Collections;
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;
57 public class SyncRestClientForHttpsServerTest {
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();
64 private SyncRestClient syncRestClient;
67 public void setUp() throws GeneralSecurityException {
68 stubServer = new StubServer();
69 stubServer.secured().run();
70 syncRestClient = new SyncRestClient(createNaiveHttpClient());
74 public void tearDown() {
79 public void testJsonResponseFromGet() throws JsonProcessingException {
82 String securedUrl = "https://0.0.0.0:" + stubServer.getPort() + "/test";
83 String notSecuredUrl = "http://0.0.0.0:" + stubServer.getPort() + "/test";
85 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient
86 .get(securedUrl, Collections.emptyMap(), Collections.emptyMap());
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");
96 public void testObjectResponseFromGet() throws JsonProcessingException {
100 String securedUrl = "https://0.0.0.0:" + stubServer.getPort() + "/test";
101 String notSecuredUrl = "http://0.0.0.0:" + stubServer.getPort() + "/test";
103 HttpResponse<SyncRestClientModel.TestModel> testModelHttpResponse = syncRestClient
104 .get(securedUrl, Collections.emptyMap(), Collections.emptyMap(), SyncRestClientModel.TestModel.class);
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");
113 private Action jsonContent() throws JsonProcessingException {
114 return stringContent(objectMapper.writeValueAsString(testObject));
117 private void stubGetCall() throws JsonProcessingException {
119 .match(Condition.get("/test"))
120 .then(ok(), jsonContent(), contentType("application/json"));
123 private CloseableHttpClient createNaiveHttpClient() throws GeneralSecurityException {
124 final SSLContext context = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy())
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)
133 return HttpClientBuilder.create()
134 .setDefaultRequestConfig(custom().setConnectionRequestTimeout(10000).build())
135 .setConnectionManager(new PoolingHttpClientConnectionManager(registry))
136 .setSSLSocketFactory(socketFactory).build();