Merge "Reorder modifiers"
[so.git] / mso-api-handlers / mso-api-handler-common / src / test / java / org / openecomp / mso / 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.openecomp.mso.apihandler.common;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26
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;
39
40 public class CamundaTaskClientTest {
41
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";
51
52     @Before
53     public void init() {
54         testedObject = new CamundaTaskClient();
55         httpClientMock = mock(HttpClient.class);
56         testedObject.setClient(httpClientMock);
57         testedObject.setUrl(URL);
58     }
59
60     @Test
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);
69     }
70
71     @Test
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();
78     }
79
80     @Test
81     public void getMethodSuccessful() throws IOException {
82         ArgumentCaptor<HttpGet> httpGetCaptor = ArgumentCaptor.forClass(HttpGet.class);
83         testedObject.get();
84         verify(httpClientMock).execute(httpGetCaptor.capture());
85         checkUri(httpGetCaptor.getValue());
86     }
87
88     @Test
89     public void getMethodSuccessfulWithCredentials() throws IOException {
90         ArgumentCaptor<HttpGet> httpGetCaptor = ArgumentCaptor.forClass(HttpGet.class);
91         testedObject.setUrl(URL);
92         testedObject.setProps(createMsoJavaProperties());
93         testedObject.get();
94         verify(httpClientMock).execute(httpGetCaptor.capture());
95         assertThat(httpGetCaptor.getValue().getHeaders(AUTHORIZATION_HEADER_NAME)).isNotEmpty();
96     }
97
98     @Test(expected = UnsupportedOperationException.class)
99     public void postMethodUnsupported() {
100         testedObject.post("", "", "", "", "", "");
101     }
102
103     @Test(expected = UnsupportedOperationException.class)
104     public void postMethodUnsupported2() {
105         testedObject.post(new RequestClientParamater.Builder().build());
106     }
107
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);
113     }
114
115     private MsoJavaProperties createMsoJavaProperties() {
116         MsoJavaProperties msoJavaProperties = new MsoJavaProperties();
117         msoJavaProperties.setProperty(CommonConstants.CAMUNDA_AUTH, "");
118         return msoJavaProperties;
119     }
120
121     private String getJsonFromEntity(HttpEntity httpEntity) throws IOException {
122         BufferedReader rd = new BufferedReader(
123                 new InputStreamReader(httpEntity.getContent()));
124         StringBuilder result = new StringBuilder();
125         String line;
126         while ((line = rd.readLine()) != null) {
127             result.append(line);
128         }
129         return result.toString();
130     }
131
132 }