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.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.runners.MockitoJUnitRunner;
37 import org.onap.appc.adapter.chef.ChefAdapter;
38 import org.onap.appc.adapter.chef.chefclient.ChefApiClientFactory;
39 import org.onap.appc.adapter.chef.chefclient.api.ChefApiClient;
40 import org.onap.appc.adapter.chef.chefclient.api.ChefResponse;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
44 @RunWith(MockitoJUnitRunner.class)
45 public class ChefAdapterImplHttpMethodTest {
47 private static final String CLIENT_PRIVATE_KEY_PATH = "/opt/onap/appc/chef/localhost/onap/testclient.pem";
48 private static final String RESULT_CODE_ATTR_KEY = "chefServerResult.code";
49 private static final String RESULT_MESSAGE_ATTR_KEY = "chefServerResult.message";
50 private static final String EXPECTED_RESPONSE_MSG = "chefResponseMessage";
51 private static final String ACTION_PARAM = "action";
52 private static final String REQUEST_BODY_DATA = "requestBodyData";
53 private static final Map<String, String> PARAMS = ImmutableMap
54 .of("username", "testclient",
55 "serverAddress", "localhost",
56 "organizations", "onap",
57 "chefAction", ACTION_PARAM,
58 "chefRequestBody", REQUEST_BODY_DATA);
60 private PrivateKeyChecker privateKeyChecker;
62 private ChefApiClientFactory chefApiClientFactory;
64 private ChefApiClient chefApiClient;
67 private ChefAdapterFactory chefAdapterFactory;
68 private SvcLogicContext svcLogicContext;
72 svcLogicContext = new SvcLogicContext();
76 public void chefGet_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() {
77 assertSuccessfulChefHttpCallFor(() -> chefApiClient.get(ACTION_PARAM), this::chefGet);
81 public void chefGet_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() {
82 assertNoChefCallOccurFor(this::chefGet);
86 public void chefDelete_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() {
87 assertSuccessfulChefHttpCallFor(() -> chefApiClient.delete(ACTION_PARAM), this::chefDelete);
91 public void chefDelete_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() {
92 assertNoChefCallOccurFor(this::chefDelete);
96 public void chefPut_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() {
97 assertSuccessfulChefHttpCallFor(() -> chefApiClient.put(ACTION_PARAM, REQUEST_BODY_DATA), this::chefPut);
101 public void chefPut_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() {
102 assertNoChefCallOccurFor(this::chefPut);
106 public void chefPost_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() {
107 assertSuccessfulChefHttpCallFor(() -> chefApiClient.post(ACTION_PARAM, REQUEST_BODY_DATA), this::chefPost);
111 public void chefPost_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() {
112 assertNoChefCallOccurFor(this::chefPost);
115 public void assertSuccessfulChefHttpCallFor(Supplier<ChefResponse> responseSupplier,
116 Consumer<ChefAdapter> chefAdapterCall) {
118 given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
119 given(chefApiClientFactory.create("https://localhost/organizations/onap",
122 CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient);
123 given(responseSupplier.get()).willReturn(ChefResponse.create(HttpStatus.SC_OK, EXPECTED_RESPONSE_MSG));
126 chefAdapterCall.accept(chefAdapterFactory.create());
129 assertThat(svcLogicContext.getStatus()).isEqualTo("success");
130 assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_OK));
131 assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(EXPECTED_RESPONSE_MSG);
134 public void assertNoChefCallOccurFor(Consumer<ChefAdapter> chefAdapterCall) {
136 given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(false);
139 chefAdapterCall.accept(chefAdapterFactory.create());
142 verifyZeroInteractions(chefApiClient);
143 assertThat(svcLogicContext.getStatus()).isEqualTo("success");
144 assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
145 .isEqualTo(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR));
146 assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(
147 "Cannot find the private key in the APPC file system, please load the private key to "
148 + CLIENT_PRIVATE_KEY_PATH);
151 public void chefGet(ChefAdapter chefAdapter) {
153 chefAdapter.chefGet(PARAMS, svcLogicContext);
154 } catch (SvcLogicException e) {
159 public void chefDelete(ChefAdapter chefAdapter) {
161 chefAdapter.chefDelete(PARAMS, svcLogicContext);
162 } catch (SvcLogicException e) {
167 public void chefPost(ChefAdapter chefAdapter) {
169 chefAdapter.chefPost(PARAMS, svcLogicContext);
170 } catch (SvcLogicException e) {
175 public void chefPut(ChefAdapter chefAdapter) {
177 chefAdapter.chefPut(PARAMS, svcLogicContext);
178 } catch (SvcLogicException e) {