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