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.apihandler.common;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
27 import java.io.BufferedReader;
28 import java.io.IOException;
29 import java.io.InputStreamReader;
30 import org.apache.http.HttpEntity;
31 import org.apache.http.client.HttpClient;
32 import org.apache.http.client.methods.HttpGet;
33 import org.apache.http.client.methods.HttpPost;
34 import org.apache.http.client.methods.HttpRequestBase;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.ArgumentCaptor;
38 import org.openecomp.mso.properties.MsoJavaProperties;
40 public class CamundaTaskClientTest {
42 private CamundaTaskClient testedObject = new CamundaTaskClient();
43 private HttpClient httpClientMock;
44 private static final String JSON_REQUEST = "{\"value1\": \"aaa\",\"value2\": \"bbb\"}";
45 private static final String URL_SCHEMA = "http";
46 private static final String HOST = "testhost";
47 private static final int PORT = 1234;
48 private static final String URL_PATH = "/requestMethodSuccessful";
49 private static final String URL = URL_SCHEMA + "://" + HOST + ":" + PORT + URL_PATH;
50 private static final String AUTHORIZATION_HEADER_NAME = "Authorization";
54 testedObject = new CamundaTaskClient();
55 httpClientMock = mock(HttpClient.class);
56 testedObject.setClient(httpClientMock);
57 testedObject.setUrl(URL);
61 public void postMethodSuccessful() throws IOException {
62 ArgumentCaptor<HttpPost> httpPostCaptor = ArgumentCaptor.forClass(HttpPost.class);
63 testedObject.post(JSON_REQUEST);
64 verify(httpClientMock).execute(httpPostCaptor.capture());
65 checkUri(httpPostCaptor.getValue());
66 assertThat(httpPostCaptor.getValue().getEntity().getContentType().getValue()).
67 isEqualTo(CommonConstants.CONTENT_TYPE_JSON);
68 assertThat(getJsonFromEntity(httpPostCaptor.getValue().getEntity())).isEqualTo(JSON_REQUEST);
72 public void postMethodSuccessfulWithCredentials() throws IOException {
73 ArgumentCaptor<HttpPost> httpPostCaptor = ArgumentCaptor.forClass(HttpPost.class);
74 testedObject.setProps(createMsoJavaProperties());
75 testedObject.post(JSON_REQUEST);
76 verify(httpClientMock).execute(httpPostCaptor.capture());
77 assertThat(httpPostCaptor.getValue().getHeaders(AUTHORIZATION_HEADER_NAME)).isNotEmpty();
81 public void getMethodSuccessful() throws IOException {
82 ArgumentCaptor<HttpGet> httpGetCaptor = ArgumentCaptor.forClass(HttpGet.class);
84 verify(httpClientMock).execute(httpGetCaptor.capture());
85 checkUri(httpGetCaptor.getValue());
89 public void getMethodSuccessfulWithCredentials() throws IOException {
90 ArgumentCaptor<HttpGet> httpGetCaptor = ArgumentCaptor.forClass(HttpGet.class);
91 testedObject.setUrl(URL);
92 testedObject.setProps(createMsoJavaProperties());
94 verify(httpClientMock).execute(httpGetCaptor.capture());
95 assertThat(httpGetCaptor.getValue().getHeaders(AUTHORIZATION_HEADER_NAME)).isNotEmpty();
98 @Test(expected = UnsupportedOperationException.class)
99 public void postMethodUnsupported() {
100 testedObject.post("", "", "", "", "", "");
103 @Test(expected = UnsupportedOperationException.class)
104 public void postMethodUnsupported2() {
105 testedObject.post(new RequestClientParamater.Builder().build());
108 private void checkUri(HttpRequestBase httpRequestBase) {
109 assertThat(httpRequestBase.getURI().getScheme()).isEqualTo(URL_SCHEMA);
110 assertThat(httpRequestBase.getURI().getHost()).isEqualTo(HOST);
111 assertThat(httpRequestBase.getURI().getPort()).isEqualTo(PORT);
112 assertThat(httpRequestBase.getURI().getPath()).isEqualTo(URL_PATH);
115 private MsoJavaProperties createMsoJavaProperties() {
116 MsoJavaProperties msoJavaProperties = new MsoJavaProperties();
117 msoJavaProperties.setProperty(CommonConstants.CAMUNDA_AUTH, "");
118 return msoJavaProperties;
121 private String getJsonFromEntity(HttpEntity httpEntity) throws IOException {
122 BufferedReader rd = new BufferedReader(
123 new InputStreamReader(httpEntity.getContent()));
124 StringBuilder result = new StringBuilder();
126 while ((line = rd.readLine()) != null) {
129 return result.toString();