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.impl;
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.mockito.BDDMockito.given;
24 import static org.mockito.BDDMockito.verifyZeroInteractions;
26 import com.google.common.collect.ImmutableMap;
28 import java.util.function.Consumer;
29 import java.util.function.Supplier;
30 import org.apache.http.HttpStatus;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.runners.MockitoJUnitRunner;
36 import org.onap.appc.adapter.chef.ChefAdapter;
37 import org.onap.appc.adapter.chef.chefclient.ChefApiClientFactory;
38 import org.onap.appc.adapter.chef.chefclient.api.ChefApiClient;
39 import org.onap.appc.adapter.chef.chefclient.api.ChefResponse;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
43 @RunWith(MockitoJUnitRunner.class)
44 public class ChefAdapterImplHttpMethodTest {
46 private static final String CLIENT_PRIVATE_KEY_PATH = "/opt/onap/appc/chef/localhost/onap/testclient.pem";
47 private static final String RESULT_CODE_ATTR_KEY = "chefServerResult.code";
48 private static final String RESULT_MESSAGE_ATTR_KEY = "chefServerResult.message";
49 private static final String EXPECTED_RESPONSE_MSG = "chefResponseMessage";
50 private static final String ACTION_PARAM = "action";
51 private static final String REQUEST_BODY_DATA = "requestBodyData";
52 private static final Map<String, String> PARAMS = ImmutableMap
53 .of("username", "testclient",
54 "serverAddress", "localhost",
55 "organizations", "onap",
56 "chefAction", ACTION_PARAM,
57 "chefRequestBody", REQUEST_BODY_DATA);
59 private PrivateKeyChecker privateKeyChecker;
61 private ChefApiClientFactory chefApiClientFactory;
63 private ChefApiClient chefApiClient;
65 private ChefAdapter chefAdapter;
66 private SvcLogicContext svcLogicContext;
70 chefAdapter = new ChefAdapterImpl(chefApiClientFactory, privateKeyChecker);
71 svcLogicContext = new SvcLogicContext();
75 public void chefGet_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() {
76 assertSuccessfulChefHttpCallFor(() -> chefApiClient.get(ACTION_PARAM), this::chefGet);
80 public void chefGet_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() {
81 assertNoChefCallOccurFor(this::chefGet);
85 public void chefDelete_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() {
86 assertSuccessfulChefHttpCallFor(() -> chefApiClient.delete(ACTION_PARAM), this::chefDelete);
90 public void chefDelete_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() {
91 assertNoChefCallOccurFor(this::chefDelete);
95 public void chefPut_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() {
96 assertSuccessfulChefHttpCallFor(() -> chefApiClient.put(ACTION_PARAM, REQUEST_BODY_DATA), this::chefPut);
100 public void chefPut_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() {
101 assertNoChefCallOccurFor(this::chefPut);
105 public void chefPost_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() {
106 assertSuccessfulChefHttpCallFor(() -> chefApiClient.post(ACTION_PARAM, REQUEST_BODY_DATA), this::chefPost);
110 public void chefPost_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() {
111 assertNoChefCallOccurFor(this::chefPost);
114 public void assertSuccessfulChefHttpCallFor(Supplier<ChefResponse> responseSupplier,
115 Consumer<ChefAdapter> chefAdapterCall) {
117 given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
118 given(chefApiClientFactory.create("https://localhost/organizations/onap",
121 CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient);
122 given(responseSupplier.get()).willReturn(ChefResponse.create(HttpStatus.SC_OK, EXPECTED_RESPONSE_MSG));
125 chefAdapterCall.accept(chefAdapter);
128 assertThat(svcLogicContext.getStatus()).isEqualTo("success");
129 assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_OK));
130 assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(EXPECTED_RESPONSE_MSG);
133 public void assertNoChefCallOccurFor(Consumer<ChefAdapter> chefAdapterCall) {
135 given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(false);
138 chefAdapterCall.accept(chefAdapter);
141 verifyZeroInteractions(chefApiClient);
142 assertThat(svcLogicContext.getStatus()).isEqualTo("success");
143 assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
144 .isEqualTo(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR));
145 assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(
146 "Cannot find the private key in the APPC file system, please load the private key to "
147 + CLIENT_PRIVATE_KEY_PATH);
150 public void chefGet(ChefAdapter chefAdapter) {
152 chefAdapter.chefGet(PARAMS, svcLogicContext);
153 } catch (SvcLogicException e) {
158 public void chefDelete(ChefAdapter chefAdapter) {
160 chefAdapter.chefDelete(PARAMS, svcLogicContext);
161 } catch (SvcLogicException e) {
166 public void chefPost(ChefAdapter chefAdapter) {
168 chefAdapter.chefPost(PARAMS, svcLogicContext);
169 } catch (SvcLogicException e) {
174 public void chefPut(ChefAdapter chefAdapter) {
176 chefAdapter.chefPut(PARAMS, svcLogicContext);
177 } catch (SvcLogicException e) {