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 org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
25 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
26 import static org.mockito.BDDMockito.given;
28 import com.google.common.collect.ImmutableList;
29 import com.google.common.collect.ImmutableMap;
30 import java.util.Collections;
32 import org.apache.http.HttpStatus;
33 import org.json.JSONObject;
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.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 ChefAdapterImplTest {
47 private static final String EXPECTED_NODE_OBJECT_ATTR_NAME = "chef.nodeObject";
48 private static final String RESULT_CODE_ATTR_KEY = "chefClientResult.code";
49 private static final String RESULT_MESSAGE_ATTR_KEY = "chefClientResult.message";
50 private static final String EXPECTED_RESPONSE_MSG = "chefResponseMessage";
51 private static final String IP_PARAM = "ip";
52 private static final String ENDPOINT_IP = "http://127.0.0.1";
53 private static final String CHEF_AGENT_CODE_KEY = "chefAgent.code";
54 private static final String CHEF_AGENT_MESSAGE_KEY = "chefAgent.message";
56 @Mock(answer = RETURNS_DEEP_STUBS)
57 private ChefApiClientFactory chefApiClientFactory;
59 private PrivateKeyChecker privateKeyChecker;
62 private ChefAdapterFactory chefAdapterFactory;
65 public void nodeObjectBuilder_shouldBuildJsonNodeObject_forPassedParams_andAddToSvcLogicContext() {
67 Map<String, String> params = givenInputParams();
68 SvcLogicContext svcLogicContext = new SvcLogicContext();
71 chefAdapterFactory.create().nodeObejctBuilder(params, svcLogicContext);
74 assertThat(resultJson(svcLogicContext)).isEqualTo(expectedJson());
77 private String resultJson(SvcLogicContext svcLogicContext) {
78 String resultJsonString = svcLogicContext.getAttribute(EXPECTED_NODE_OBJECT_ATTR_NAME);
79 return new JSONObject(resultJsonString).toString();
82 private Map<String, String> givenInputParams() {
83 return ImmutableMap.<String, String>builder()
84 .put("nodeobject.name", "testNodeName")
85 .put("nodeobject.normal", "val:normal")
86 .put("nodeobject.overrides", "val:override")
87 .put("nodeobject.defaults", "val:default")
88 .put("nodeobject.run_list", "val1,val2,val3")
89 .put("nodeobject.chef_environment", "testChefEnvVal")
93 private String expectedJson() {
94 JSONObject expectedJson = new JSONObject();
95 expectedJson.put("json_class", "Chef::Node");
96 expectedJson.put("chef_type", "node");
97 expectedJson.put("automatic", Collections.emptyMap());
98 expectedJson.put("name", "testNodeName");
99 expectedJson.put("normal", ImmutableMap.of("val", "normal"));
100 expectedJson.put("override", ImmutableMap.of("val", "override"));
101 expectedJson.put("default", ImmutableMap.of("val", "default"));
102 expectedJson.put("run_list", ImmutableList.of("val1", "val2", "val3"));
103 expectedJson.put("chef_environment", "testChefEnvVal");
104 return expectedJson.toString();
108 public void combineStrings_shouldConcatenateTwoParamStrings_andSetThemInSvcContext() {
110 Map<String, String> params = ImmutableMap
111 .of("dgContext", "contextValue", "String1", "paramString1", "String2", "paramString2");
112 SvcLogicContext svcLogicContext = new SvcLogicContext();
115 chefAdapterFactory.create().combineStrings(params, svcLogicContext);
118 assertThat(svcLogicContext.getAttribute("contextValue")).isEqualTo("paramString1paramString2");
122 public void trigger_shouldTriggerTargetEndpoint_andUpdateSvclogicContext() {
124 Map<String, String> params = ImmutableMap.of(IP_PARAM, ENDPOINT_IP);
125 SvcLogicContext svcLogicContext = new SvcLogicContext();
126 given(chefApiClientFactory.create(ENDPOINT_IP).get(""))
127 .willReturn(ChefResponse.create(HttpStatus.SC_OK, EXPECTED_RESPONSE_MSG));
130 chefAdapterFactory.create().trigger(params, svcLogicContext);
133 assertThat(svcLogicContext.getAttribute(CHEF_AGENT_CODE_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_OK));
134 assertThat(svcLogicContext.getStatus()).isEqualTo("success");
135 assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_OK));
136 assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(EXPECTED_RESPONSE_MSG);
140 public void trigger_shouldUpdateSvcLogicContext_withFailStatusAndMsg_whenExceptionOccurs() {
142 Map<String, String> params = ImmutableMap.of(IP_PARAM, ENDPOINT_IP);
143 SvcLogicContext svcLogicContext = new SvcLogicContext();
144 given(chefApiClientFactory.create(ENDPOINT_IP)).willThrow(new RuntimeException());
147 chefAdapterFactory.create().trigger(params, svcLogicContext);
150 assertThat(svcLogicContext.getAttribute(CHEF_AGENT_CODE_KEY))
151 .isEqualTo(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR));
152 assertThat(svcLogicContext.getAttribute(CHEF_AGENT_MESSAGE_KEY)).isEqualTo(new RuntimeException().toString());
156 public void chefInfo_shouldUpdateSvcLogicContext_withFailStatusAndMsg_andThrowException_whenUsernameParamIsMissing() {
157 Map<String, String> params = ImmutableMap.of(
158 "serverAddress", "http://chefAddress",
159 "organizations", "onap");
160 checkIfInputParamsAreValidated(params);
164 public void chefInfo_shouldUpdateSvcLogicContext_withFailStatusAndMsg_andThrowException_whenServerAddressParamIsMissing() {
165 Map<String, String> params = ImmutableMap.of(
166 "username", "TestUsername",
167 "organizations", "onap");
168 checkIfInputParamsAreValidated(params);
172 public void chefInfo_shouldUpdateSvcLogicContext_withFailStatusAndMsg_andThrowException_whenOrganizationsParamIsMissing() {
173 Map<String, String> params = ImmutableMap.of(
174 "username", "TestUsername",
175 "serverAddress", "http://chefAddress");
176 checkIfInputParamsAreValidated(params);
179 private void checkIfInputParamsAreValidated(Map<String, String> params) {
181 String expectedErrorMsg = "Missing mandatory param(s) such as username, serverAddress, organizations";
182 SvcLogicContext svcLogicContext = new SvcLogicContext();
185 assertThatExceptionOfType(SvcLogicException.class)
186 .isThrownBy(() -> chefAdapterFactory.create().chefGet(params, svcLogicContext))
187 .withMessage("Chef Adapter error:"
189 assertThat(svcLogicContext.getStatus()).isEqualTo("failure");
190 assertThat(svcLogicContext.getAttribute("chefServerResult.code"))
191 .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
192 assertThat(svcLogicContext.getAttribute("chefServerResult.message"))
193 .isEqualTo(expectedErrorMsg);