aboutsummaryrefslogtreecommitdiffstats
path: root/mso-api-handlers/mso-api-handler-common/src/test/java/org/onap/so/apihandler/common/CamundaTaskClientTest.java
blob: 69772ee7e665c50359f932361d0fb99195a41fd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.apihandler.common;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.onap.so.apihandler.common.CamundaTaskClient;
import org.onap.so.apihandler.common.CommonConstants;
import org.onap.so.apihandler.common.RequestClientParameter;
import org.springframework.core.env.Environment;

@RunWith(MockitoJUnitRunner.class)
public class CamundaTaskClientTest {

	@Mock
	private Environment env;
    private CamundaTaskClient testedObject = new CamundaTaskClient();
    private HttpClient httpClientMock;
    private static final String JSON_REQUEST = "{\"value1\": \"aaa\",\"value2\": \"bbb\"}";
    private static final String URL_SCHEMA = "http";
    private static final String HOST = "testhost";
    private static final int PORT = 1234;
    private static final String URL_PATH = "/requestMethodSuccessful";
    private static final String URL = URL_SCHEMA + "://" + HOST + ":" + PORT + URL_PATH;
    private static final String AUTHORIZATION_HEADER_NAME = "Authorization";

    @Before
    public void init() {
    	when(env.getProperty(eq(CommonConstants.CAMUNDA_AUTH))).thenReturn("");
        testedObject = new CamundaTaskClient();
        httpClientMock = mock(HttpClient.class);
        testedObject.setClient(httpClientMock);
        testedObject.setUrl(URL);
    }

    @Test
    public void postMethodSuccessful() throws IOException {
        ArgumentCaptor<HttpPost> httpPostCaptor = ArgumentCaptor.forClass(HttpPost.class);
        testedObject.post(JSON_REQUEST);
        verify(httpClientMock).execute(httpPostCaptor.capture());
        checkUri(httpPostCaptor.getValue());
        assertThat(httpPostCaptor.getValue().getEntity().getContentType().getValue()).
                isEqualTo(CommonConstants.CONTENT_TYPE_JSON);
        assertThat(getJsonFromEntity(httpPostCaptor.getValue().getEntity())).isEqualTo(JSON_REQUEST);
    }

    @Test
    public void postMethodSuccessfulWithCredentials() throws IOException {
        ArgumentCaptor<HttpPost> httpPostCaptor = ArgumentCaptor.forClass(HttpPost.class);
        testedObject.setProps(env);
        testedObject.post(JSON_REQUEST);
        verify(httpClientMock).execute(httpPostCaptor.capture());
        assertThat(httpPostCaptor.getValue().getHeaders(AUTHORIZATION_HEADER_NAME)).isNotEmpty();
    }

    @Test
    public void getMethodSuccessful() throws IOException {
        ArgumentCaptor<HttpGet> httpGetCaptor = ArgumentCaptor.forClass(HttpGet.class);
        testedObject.get();
        verify(httpClientMock).execute(httpGetCaptor.capture());
        checkUri(httpGetCaptor.getValue());
    }

    @Test
    public void getMethodSuccessfulWithCredentials() throws IOException {
        ArgumentCaptor<HttpGet> httpGetCaptor = ArgumentCaptor.forClass(HttpGet.class);
        testedObject.setUrl(URL);
        testedObject.setProps(env);
        testedObject.get();
        verify(httpClientMock).execute(httpGetCaptor.capture());
        assertThat(httpGetCaptor.getValue().getHeaders(AUTHORIZATION_HEADER_NAME)).isNotEmpty();
    }

    @Test(expected = UnsupportedOperationException.class)
    public void postMethodUnsupported() {
        testedObject.post("", "", "", "", "", "");
    }

    @Test(expected = UnsupportedOperationException.class)
    public void postMethodUnsupported2() {
        testedObject.post(new RequestClientParameter.Builder().build());
    }

    private void checkUri(HttpRequestBase httpRequestBase) {
        assertThat(httpRequestBase.getURI().getScheme()).isEqualTo(URL_SCHEMA);
        assertThat(httpRequestBase.getURI().getHost()).isEqualTo(HOST);
        assertThat(httpRequestBase.getURI().getPort()).isEqualTo(PORT);
        assertThat(httpRequestBase.getURI().getPath()).isEqualTo(URL_PATH);
    }

    private String getJsonFromEntity(HttpEntity httpEntity) throws IOException {
        BufferedReader rd = new BufferedReader(
                new InputStreamReader(httpEntity.getContent()));
        StringBuilder result = new StringBuilder();
        String line;
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        return result.toString();
    }

}