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