2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T 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
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.openecomp.mso.rest;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
26 import java.io.IOException;
28 import org.apache.http.HttpResponse;
29 import org.apache.http.HttpVersion;
30 import org.apache.http.client.ClientProtocolException;
31 import org.apache.http.client.HttpClient;
32 import org.apache.http.client.methods.HttpUriRequest;
33 import org.apache.http.entity.StringEntity;
34 import org.apache.http.message.BasicHttpResponse;
35 import org.junit.Assert;
36 import org.junit.Test;
37 import org.mockito.Mockito;
39 import org.openecomp.mso.rest.APIResponse;
40 import org.openecomp.mso.rest.RESTClient;
41 import org.openecomp.mso.rest.RESTException;
47 public class RESTClientTest {
50 public void testSimpleHTTP() throws RESTException, ClientProtocolException, IOException {
51 HttpClient mockHttpClient = mock(HttpClient.class);
52 HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
53 response.setEntity(new StringEntity("test","UTF-8"));
54 when(mockHttpClient.execute(Mockito.<HttpUriRequest>any())).thenReturn(response);
56 RESTClient restClient = new RESTClient("http://localhost");
57 restClient.setUnitTestClient(mockHttpClient);
58 APIResponse apiResponse = restClient.get();
59 Assert.assertEquals(200, apiResponse.getStatusCode());
60 Assert.assertEquals("test", apiResponse.getResponseBodyAsString());
64 public void testSimpleHTTPS() throws RESTException, ClientProtocolException, IOException {
65 HttpClient mockHttpClient = mock(HttpClient.class);
66 HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
67 response.setEntity(new StringEntity("test","UTF-8"));
68 when(mockHttpClient.execute(Mockito.<HttpUriRequest>any())).thenReturn(response);
70 RESTClient restClient = new RESTClient("https://localhost");
71 restClient.setUnitTestClient(mockHttpClient);
72 APIResponse apiResponse = restClient.get();
73 Assert.assertEquals(200, apiResponse.getStatusCode());
74 Assert.assertEquals("test", apiResponse.getResponseBodyAsString());