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;
23 import static com.google.common.collect.Maps.immutableEntry;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
26 import static org.mockito.BDDMockito.given;
28 import com.google.common.collect.ImmutableMap;
29 import com.google.common.collect.ImmutableMap.Builder;
31 import java.util.Map.Entry;
32 import org.apache.http.HttpStatus;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.InjectMocks;
37 import org.mockito.Mock;
38 import org.mockito.runners.MockitoJUnitRunner;
39 import org.onap.appc.adapter.chef.chefclient.ChefApiClientFactory;
40 import org.onap.appc.adapter.chef.chefclient.api.ChefApiClient;
41 import org.onap.appc.adapter.chef.chefclient.api.ChefResponse;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
45 @RunWith(MockitoJUnitRunner.class)
46 public class ChefAdapterImplVNFCOperationsTest {
48 private static final String CLIENT_PRIVATE_KEY_PATH = "/opt/onap/appc/chef/localhost/onap/testclient.pem";
49 private static final String RESULT_CODE_ATTR_KEY = "chefServerResult.code";
50 private static final String RESULT_MESSAGE_ATTR_KEY = "chefServerResult.message";
51 private static final String USERNAME = "testclient";
52 private static final String SERVER_ADDRESS = "localhost";
53 private static final String ORGANIZATIONS = "onap";
54 private static final String ENV_PARAM_KEY = "Environment";
55 private static final String ENV_JSON_VALUE = "{name:envName}";
56 private static final String FAILURE_STATUS = "failure";
57 private static final String SUCCESS_STATUS = "success";
58 private static final String CHEF_ADAPTER_ERROR_PREFIX = "Chef Adapter error:";
61 private PrivateKeyChecker privateKeyChecker;
63 private ChefApiClientFactory chefApiClientFactory;
65 private ChefApiClient chefApiClient;
68 private ChefAdapterFactory chefAdapterFactory;
69 private SvcLogicContext svcLogicContext;
73 svcLogicContext = new SvcLogicContext();
77 public void vnfcEnvironment_shouldSkipEnvironmentCreation_whenEnvParamIsEmpty() throws SvcLogicException {
79 Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, ""));
82 chefAdapterFactory.create().vnfcEnvironment(params, svcLogicContext);
85 assertThat(svcLogicContext.getStatus()).isEqualTo(SUCCESS_STATUS);
86 assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
87 .isEqualTo(Integer.toString(HttpStatus.SC_OK));
88 assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo("Skip Environment block ");
92 public void vnfcEnvironment_shouldCreateNewEnvironment_forEnvParam_whenRequestedEnvDoesNotExist()
93 throws SvcLogicException {
95 String expectedErrorMessage = "New Environment Created";
96 Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, ENV_JSON_VALUE));
97 given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
98 given(chefApiClientFactory.create("https://localhost/organizations/onap", ORGANIZATIONS, USERNAME,
99 CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient);
100 given(chefApiClient.put("/environments/" + "envName", ENV_JSON_VALUE))
101 .willReturn(ChefResponse.create(HttpStatus.SC_NOT_FOUND, ""));
102 given(chefApiClient.post("/environments", ENV_JSON_VALUE))
103 .willReturn(ChefResponse.create(HttpStatus.SC_CREATED, expectedErrorMessage));
106 chefAdapterFactory.create().vnfcEnvironment(params, svcLogicContext);
109 assertThat(svcLogicContext.getStatus()).isEqualTo(SUCCESS_STATUS);
110 assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
111 .isEqualTo(Integer.toString(HttpStatus.SC_CREATED));
112 assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedErrorMessage);
116 public void vnfcEnvironment_shouldNotAttemptEnvCreation_andThrowException_whenPrivateKeyCheckFails() {
118 String expectedErrorMsg = "Cannot find the private key in the APPC file system, please load the private key to ";
119 Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, ENV_JSON_VALUE));
120 given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(false);
123 assertThatExceptionOfType(SvcLogicException.class)
124 .isThrownBy(() -> chefAdapterFactory.create().vnfcEnvironment(params, svcLogicContext))
125 .withMessage(CHEF_ADAPTER_ERROR_PREFIX + expectedErrorMsg + CLIENT_PRIVATE_KEY_PATH);
127 assertThat(svcLogicContext.getStatus()).isEqualTo(FAILURE_STATUS);
128 assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
129 .isEqualTo(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR));
130 assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY))
131 .isEqualTo(expectedErrorMsg + CLIENT_PRIVATE_KEY_PATH);
135 public void vnfcEnvironment_shouldNotAttemptEnvCreation_andHandleJSONException_whenJSONParamsAreMalformed() {
137 String expectedErrorMessage = "Error posting request due to invalid JSON block: ";
138 Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, "MALFORMED_JSON"));
139 given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
142 assertThatExceptionOfType(SvcLogicException.class)
143 .isThrownBy(() -> chefAdapterFactory.create().vnfcEnvironment(params, svcLogicContext))
144 .withMessageStartingWith(CHEF_ADAPTER_ERROR_PREFIX + expectedErrorMessage);
146 assertThat(svcLogicContext.getStatus()).isEqualTo(FAILURE_STATUS);
147 assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
148 .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
149 assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY))
150 .startsWith(expectedErrorMessage);
154 public void vnfcEnvironment_shouldNotAttemptEnvCreation_andHandleException_whenExceptionOccursDuringExecution() {
156 String expectedErrorMessage = "Error posting request: ";
157 Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, ENV_JSON_VALUE));
158 given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
159 given(chefApiClientFactory.create("https://localhost/organizations/onap", ORGANIZATIONS, USERNAME,
160 CLIENT_PRIVATE_KEY_PATH)).willThrow(new NullPointerException("Null value encountered"));
163 assertThatExceptionOfType(SvcLogicException.class)
164 .isThrownBy(() -> chefAdapterFactory.create().vnfcEnvironment(params, svcLogicContext))
165 .withMessage(CHEF_ADAPTER_ERROR_PREFIX + expectedErrorMessage + "Null value encountered");
167 assertThat(svcLogicContext.getStatus()).isEqualTo(FAILURE_STATUS);
168 assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
169 .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
170 assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).startsWith(expectedErrorMessage);
173 private Map<String, String> givenInputParams(Entry<String, String>... entries) {
174 Builder<String, String> paramsBuilder = ImmutableMap.builder();
175 paramsBuilder.put("username", USERNAME)
176 .put("serverAddress", SERVER_ADDRESS)
177 .put("organizations", ORGANIZATIONS);
179 for (Entry<String, String> entry : entries) {
180 paramsBuilder.put(entry);
182 return paramsBuilder.build();