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.onap.so.apihandler.common;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import org.apache.http.HttpEntity;
32 import org.apache.http.client.HttpClient;
33 import org.apache.http.client.methods.HttpGet;
34 import org.apache.http.client.methods.HttpPost;
35 import org.apache.http.client.methods.HttpRequestBase;
36 import org.junit.Assert;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.ArgumentCaptor;
41 import org.mockito.Mock;
42 import org.mockito.junit.MockitoJUnitRunner;
43 import org.springframework.core.env.Environment;
45 @RunWith(MockitoJUnitRunner.class)
46 public class CamundaTaskClientTest {
49 private Environment env;
50 private CamundaTaskClient testedObject = new CamundaTaskClient();
51 private HttpClient httpClientMock;
52 private static final String JSON_REQUEST = "{\"value1\": \"aaa\",\"value2\": \"bbb\"}";
53 private static final String URL_SCHEMA = "http";
54 private static final String HOST = "testhost";
55 private static final int PORT = 1234;
56 private static final String URL_PATH = "/requestMethodSuccessful";
57 private static final String URL = URL_SCHEMA + "://" + HOST + ":" + PORT + URL_PATH;
58 private static final String AUTHORIZATION_HEADER_NAME = "Authorization";
62 when(env.getProperty(eq(CommonConstants.CAMUNDA_AUTH))).thenReturn(
63 "E8E19DD16CC90D2E458E8FF9A884CC0452F8F3EB8E321F96038DE38D5C1B0B02DFAE00B88E2CF6E2A4101AB2C011FC161212EE");
64 when(env.getProperty(eq(CommonConstants.ENCRYPTION_KEY_PROP))).thenReturn("aa3871669d893c7fb8abbcda31b88b4f");
65 testedObject = new CamundaTaskClient();
66 httpClientMock = mock(HttpClient.class);
67 testedObject.setClient(httpClientMock);
68 testedObject.setUrl(URL);
72 public void postMethodSuccessful() throws IOException {
73 ArgumentCaptor<HttpPost> httpPostCaptor = ArgumentCaptor.forClass(HttpPost.class);
74 testedObject.post(JSON_REQUEST);
75 verify(httpClientMock).execute(httpPostCaptor.capture());
76 checkUri(httpPostCaptor.getValue());
77 assertThat(httpPostCaptor.getValue().getEntity().getContentType().getValue())
78 .isEqualTo(CommonConstants.CONTENT_TYPE_JSON);
79 assertThat(getJsonFromEntity(httpPostCaptor.getValue().getEntity())).isEqualTo(JSON_REQUEST);
83 public void postMethodSuccessfulWithCredentials() throws IOException {
84 ArgumentCaptor<HttpPost> httpPostCaptor = ArgumentCaptor.forClass(HttpPost.class);
85 testedObject.setProps(env);
86 testedObject.post(JSON_REQUEST);
87 verify(httpClientMock).execute(httpPostCaptor.capture());
88 assertThat(httpPostCaptor.getValue().getHeaders(AUTHORIZATION_HEADER_NAME)).isNotEmpty();
89 Assert.assertEquals("Basic YXBpaEJwbW46Y2FtdW5kYS1SMTUxMiE=",
90 httpPostCaptor.getValue().getHeaders(AUTHORIZATION_HEADER_NAME)[0].getValue());
94 public void getMethodSuccessful() throws IOException {
95 ArgumentCaptor<HttpGet> httpGetCaptor = ArgumentCaptor.forClass(HttpGet.class);
97 verify(httpClientMock).execute(httpGetCaptor.capture());
98 checkUri(httpGetCaptor.getValue());
102 public void getMethodSuccessfulWithCredentials() throws IOException {
103 ArgumentCaptor<HttpGet> httpGetCaptor = ArgumentCaptor.forClass(HttpGet.class);
104 testedObject.setUrl(URL);
105 testedObject.setProps(env);
107 verify(httpClientMock).execute(httpGetCaptor.capture());
108 assertThat(httpGetCaptor.getValue().getHeaders(AUTHORIZATION_HEADER_NAME)).isNotEmpty();
109 Assert.assertEquals("Basic YXBpaEJwbW46Y2FtdW5kYS1SMTUxMiE=",
110 httpGetCaptor.getValue().getHeaders(AUTHORIZATION_HEADER_NAME)[0].getValue());
113 @Test(expected = UnsupportedOperationException.class)
114 public void postMethodUnsupported() {
115 testedObject.post("", "", "", "", "", "");
118 @Test(expected = UnsupportedOperationException.class)
119 public void postMethodUnsupported2() {
120 testedObject.post(new RequestClientParameter.Builder().build());
123 private void checkUri(HttpRequestBase httpRequestBase) {
124 assertThat(httpRequestBase.getURI().getScheme()).isEqualTo(URL_SCHEMA);
125 assertThat(httpRequestBase.getURI().getHost()).isEqualTo(HOST);
126 assertThat(httpRequestBase.getURI().getPort()).isEqualTo(PORT);
127 assertThat(httpRequestBase.getURI().getPath()).isEqualTo(URL_PATH);
130 private String getJsonFromEntity(HttpEntity httpEntity) throws IOException {
131 BufferedReader rd = new BufferedReader(new InputStreamReader(httpEntity.getContent()));
132 StringBuilder result = new StringBuilder();
134 while ((line = rd.readLine()) != null) {
137 return result.toString();