Containerization feature of SO
[so.git] / mso-api-handlers / mso-api-handler-common / src / test / java / org / onap / so / apihandler / common / CamundaTaskClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
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.so.apihandler.common;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Matchers.eq;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28
29 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.io.InputStreamReader;
32
33 import org.apache.http.HttpEntity;
34 import org.apache.http.client.HttpClient;
35 import org.apache.http.client.methods.HttpGet;
36 import org.apache.http.client.methods.HttpPost;
37 import org.apache.http.client.methods.HttpRequestBase;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.ArgumentCaptor;
42 import org.mockito.Mock;
43 import org.mockito.runners.MockitoJUnitRunner;
44 import org.onap.so.apihandler.common.CamundaTaskClient;
45 import org.onap.so.apihandler.common.CommonConstants;
46 import org.onap.so.apihandler.common.RequestClientParameter;
47 import org.springframework.core.env.Environment;
48
49 @RunWith(MockitoJUnitRunner.class)
50 public class CamundaTaskClientTest {
51
52         @Mock
53         private Environment env;
54     private CamundaTaskClient testedObject = new CamundaTaskClient();
55     private HttpClient httpClientMock;
56     private static final String JSON_REQUEST = "{\"value1\": \"aaa\",\"value2\": \"bbb\"}";
57     private static final String URL_SCHEMA = "http";
58     private static final String HOST = "testhost";
59     private static final int PORT = 1234;
60     private static final String URL_PATH = "/requestMethodSuccessful";
61     private static final String URL = URL_SCHEMA + "://" + HOST + ":" + PORT + URL_PATH;
62     private static final String AUTHORIZATION_HEADER_NAME = "Authorization";
63
64     @Before
65     public void init() {
66         when(env.getProperty(eq(CommonConstants.CAMUNDA_AUTH))).thenReturn("");
67         testedObject = new CamundaTaskClient();
68         httpClientMock = mock(HttpClient.class);
69         testedObject.setClient(httpClientMock);
70         testedObject.setUrl(URL);
71     }
72
73     @Test
74     public void postMethodSuccessful() throws IOException {
75         ArgumentCaptor<HttpPost> httpPostCaptor = ArgumentCaptor.forClass(HttpPost.class);
76         testedObject.post(JSON_REQUEST);
77         verify(httpClientMock).execute(httpPostCaptor.capture());
78         checkUri(httpPostCaptor.getValue());
79         assertThat(httpPostCaptor.getValue().getEntity().getContentType().getValue()).
80                 isEqualTo(CommonConstants.CONTENT_TYPE_JSON);
81         assertThat(getJsonFromEntity(httpPostCaptor.getValue().getEntity())).isEqualTo(JSON_REQUEST);
82     }
83
84     @Test
85     public void postMethodSuccessfulWithCredentials() throws IOException {
86         ArgumentCaptor<HttpPost> httpPostCaptor = ArgumentCaptor.forClass(HttpPost.class);
87         testedObject.setProps(env);
88         testedObject.post(JSON_REQUEST);
89         verify(httpClientMock).execute(httpPostCaptor.capture());
90         assertThat(httpPostCaptor.getValue().getHeaders(AUTHORIZATION_HEADER_NAME)).isNotEmpty();
91     }
92
93     @Test
94     public void getMethodSuccessful() throws IOException {
95         ArgumentCaptor<HttpGet> httpGetCaptor = ArgumentCaptor.forClass(HttpGet.class);
96         testedObject.get();
97         verify(httpClientMock).execute(httpGetCaptor.capture());
98         checkUri(httpGetCaptor.getValue());
99     }
100
101     @Test
102     public void getMethodSuccessfulWithCredentials() throws IOException {
103         ArgumentCaptor<HttpGet> httpGetCaptor = ArgumentCaptor.forClass(HttpGet.class);
104         testedObject.setUrl(URL);
105         testedObject.setProps(env);
106         testedObject.get();
107         verify(httpClientMock).execute(httpGetCaptor.capture());
108         assertThat(httpGetCaptor.getValue().getHeaders(AUTHORIZATION_HEADER_NAME)).isNotEmpty();
109     }
110
111     @Test(expected = UnsupportedOperationException.class)
112     public void postMethodUnsupported() {
113         testedObject.post("", "", "", "", "", "");
114     }
115
116     @Test(expected = UnsupportedOperationException.class)
117     public void postMethodUnsupported2() {
118         testedObject.post(new RequestClientParameter.Builder().build());
119     }
120
121     private void checkUri(HttpRequestBase httpRequestBase) {
122         assertThat(httpRequestBase.getURI().getScheme()).isEqualTo(URL_SCHEMA);
123         assertThat(httpRequestBase.getURI().getHost()).isEqualTo(HOST);
124         assertThat(httpRequestBase.getURI().getPort()).isEqualTo(PORT);
125         assertThat(httpRequestBase.getURI().getPath()).isEqualTo(URL_PATH);
126     }
127
128     private String getJsonFromEntity(HttpEntity httpEntity) throws IOException {
129         BufferedReader rd = new BufferedReader(
130                 new InputStreamReader(httpEntity.getContent()));
131         StringBuilder result = new StringBuilder();
132         String line;
133         while ((line = rd.readLine()) != null) {
134             result.append(line);
135         }
136         return result.toString();
137     }
138
139 }