2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Nokia. 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=========================================================
20 package org.onap.appc.adapter.chef.chefclient;
22 import static junit.framework.TestCase.assertEquals;
23 import static org.mockito.BDDMockito.given;
24 import static org.mockito.Matchers.argThat;
25 import static org.mockito.Mockito.mock;
27 import com.google.common.collect.ImmutableMap;
28 import java.io.IOException;
30 import java.net.URISyntaxException;
31 import java.util.function.Supplier;
32 import org.apache.http.Header;
33 import org.apache.http.HttpResponse;
34 import org.apache.http.HttpStatus;
35 import org.apache.http.StatusLine;
36 import org.apache.http.client.HttpClient;
37 import org.apache.http.client.methods.HttpRequestBase;
38 import org.apache.http.entity.StringEntity;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.ArgumentMatcher;
43 import org.mockito.InjectMocks;
44 import org.mockito.Mock;
45 import org.mockito.runners.MockitoJUnitRunner;
47 @RunWith(MockitoJUnitRunner.class)
48 public class ChefApiClientTest {
50 private static final String END_POINT = "https://chefServer";
51 private static final String ORGANIZATIONS_PATH = "onap";
52 private static final String USER_ID = "testUser";
53 private static final String REQUEST_PATH = "/test/path";
54 private static final String BODY = "SOME BODY STRING";
55 private static final ImmutableMap<String, String> HEADERS = ImmutableMap.<String, String>builder()
56 .put("Content-type", "application/json")
57 .put("Accept", "application/json")
58 .put("X-Ops-Timestamp", "1970-01-15T06:56:07Z")
59 .put("X-Ops-UserId", USER_ID)
60 .put("X-Chef-Version", "12.4.1")
61 .put("X-Ops-Content-Hash", BODY)
62 .put("X-Ops-Sign", "version=1.0").build();
65 private HttpClient httpClient;
67 private ChefApiHeaderFactory chefHttpHeaderFactory;
70 private ChefApiClientFactory chefApiClientFactory;
71 private static final String PEM_FILEPATH = "path/to/pemFile";
72 private ChefApiClient chefApiClient;
76 chefApiClient = chefApiClientFactory.create(
84 public void execute_HttpGet_shouldReturnResponseObject_whenRequestIsSuccessful() throws IOException {
86 String methodName = "GET";
88 Supplier<ChefResponse> chefClientApiCall = () -> chefApiClient.get(REQUEST_PATH);
91 assertChefApiClientCall(methodName, body, chefClientApiCall);
95 public void execute_HttpDelete_shouldReturnResponseObject_whenRequestIsSuccessful() throws IOException {
97 String methodName = "DELETE";
99 Supplier<ChefResponse> chefClientApiCall = () -> chefApiClient.delete(REQUEST_PATH);
102 assertChefApiClientCall(methodName, body, chefClientApiCall);
106 public void execute_HttpPost_shouldReturnResponseObject_whenRequestIsSuccessful() throws IOException {
108 String methodName = "POST";
109 Supplier<ChefResponse> chefClientApiCall = () -> chefApiClient.post(REQUEST_PATH, BODY);
112 assertChefApiClientCall(methodName, BODY, chefClientApiCall);
116 public void execute_HttpPut_shouldReturnResponseObject_whenRequestIsSuccessful() throws IOException {
118 String methodName = "PUT";
119 Supplier<ChefResponse> chefClientApiCall = () -> chefApiClient.put(REQUEST_PATH, BODY);
122 assertChefApiClientCall(methodName, BODY, chefClientApiCall);
125 private void assertChefApiClientCall(String methodName, String body, Supplier<ChefResponse> httpMethod)
128 given(chefHttpHeaderFactory.create(methodName, REQUEST_PATH, body, USER_ID, ORGANIZATIONS_PATH, PEM_FILEPATH))
129 .willReturn(HEADERS);
131 StatusLine statusLine = mock(StatusLine.class);
132 given(statusLine.getStatusCode()).willReturn(HttpStatus.SC_OK);
133 HttpResponse httpResponse = mock(HttpResponse.class);
134 given(httpResponse.getStatusLine()).willReturn(statusLine);
135 given(httpResponse.getEntity()).willReturn(new StringEntity("Successful Response String"));
136 given(httpClient.execute(argThat(new HttpRequestBaseMatcher(methodName))))
137 .willReturn(httpResponse);
140 ChefResponse chefResponse = httpMethod.get();
143 assertEquals("Successful Response String", chefResponse.getBody());
144 assertEquals(HttpStatus.SC_OK, chefResponse.getStatusCode());
148 public void execute_shouldHandleException_whenHttpClientExecutionFails() throws IOException {
151 given(chefHttpHeaderFactory.create("GET", REQUEST_PATH, "", USER_ID, ORGANIZATIONS_PATH, PEM_FILEPATH))
152 .willReturn(HEADERS);
154 String expectedErrorMsg = "HttpClient call failed";
155 given(httpClient.execute(argThat(new HttpRequestBaseMatcher("GET"))))
156 .willThrow(new IOException(expectedErrorMsg));
159 ChefResponse chefResponse = chefApiClient.get(REQUEST_PATH);
162 assertEquals(expectedErrorMsg, chefResponse.getBody());
163 assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, chefResponse.getStatusCode());
167 public void execute_shouldHandleException_whenEndpointURIisMalformed() {
169 String expectedErrorMsg = "Malformed escape pair at index 1: /%#@/";
172 ChefApiClient chefApiClient = chefApiClientFactory.create(
177 ChefResponse chefResponse = chefApiClient.get(REQUEST_PATH);
180 assertEquals(expectedErrorMsg, chefResponse.getBody());
181 assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, chefResponse.getStatusCode());
184 private class HttpRequestBaseMatcher extends ArgumentMatcher<HttpRequestBase> {
186 private final String methodName;
188 public HttpRequestBaseMatcher(String methodName) {
189 this.methodName = methodName;
193 public boolean matches(Object argument) {
194 HttpRequestBase httpRequestBase = (HttpRequestBase) argument;
196 boolean headersMatch = checkIfHeadersMatch(httpRequestBase);
198 return methodName.equals(httpRequestBase.getMethod())
199 && new URI(END_POINT + REQUEST_PATH).equals(httpRequestBase.getURI())
201 } catch (URISyntaxException e) {
207 private boolean checkIfHeadersMatch(HttpRequestBase httpRequestBase) {
208 Header[] generatedHeaders = httpRequestBase.getAllHeaders();
209 return generatedHeaders.length > 0
210 && generatedHeaders.length == HEADERS.size()
211 && HEADERS.entrySet().stream()
212 .allMatch(p -> httpRequestBase.getFirstHeader(p.getKey()).getValue().equals(p.getValue()));