2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Nokia. All rights reserved.
6 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
7 * =============================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.appc.adapter.chef.impl;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
26 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
27 import static org.mockito.BDDMockito.given;
29 import com.google.common.collect.ImmutableList;
30 import com.google.common.collect.ImmutableMap;
31 import java.util.Collections;
33 import org.apache.http.HttpStatus;
34 import org.json.JSONObject;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.runners.MockitoJUnitRunner;
40 import org.onap.appc.adapter.chef.chefclient.ChefApiClientFactory;
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 ChefAdapterImplTest {
48 private static final String EXPECTED_NODE_OBJECT_ATTR_NAME = "chef.nodeObject";
49 private static final String RESULT_CODE_ATTR_KEY = "chefClientResult.code";
50 private static final String RESULT_MESSAGE_ATTR_KEY = "chefClientResult.message";
51 private static final String EXPECTED_RESPONSE_MSG = "chefResponseMessage";
52 private static final String IP_PARAM = "ip";
53 private static final String ENDPOINT_IP = "http://127.0.0.1";
54 private static final String CHEF_AGENT_CODE_KEY = "chefAgent.code";
55 private static final String CHEF_AGENT_MESSAGE_KEY = "chefAgent.message";
57 @Mock(answer = RETURNS_DEEP_STUBS)
58 private ChefApiClientFactory chefApiClientFactory;
60 private PrivateKeyChecker privateKeyChecker;
63 private ChefAdapterFactory chefAdapterFactory;
66 public void nodeObjectBuilder_shouldBuildJsonNodeObject_forPassedParams_andAddToSvcLogicContext() {
68 Map<String, String> params = givenInputParams();
69 SvcLogicContext svcLogicContext = new SvcLogicContext();
72 chefAdapterFactory.create().nodeObejctBuilder(params, svcLogicContext);
75 assertThat(resultJson(svcLogicContext)).isEqualTo(expectedJson());
78 private String resultJson(SvcLogicContext svcLogicContext) {
79 String resultJsonString = svcLogicContext.getAttribute(EXPECTED_NODE_OBJECT_ATTR_NAME);
80 return new JSONObject(resultJsonString).toString();
83 private Map<String, String> givenInputParams() {
84 return ImmutableMap.<String, String>builder()
85 .put("nodeobject.name", "testNodeName")
86 .put("nodeobject.normal", "val:normal")
87 .put("nodeobject.overrides", "val:override")
88 .put("nodeobject.defaults", "val:default")
89 .put("nodeobject.run_list", "val1,val2,val3")
90 .put("nodeobject.chef_environment", "testChefEnvVal")
94 private String expectedJson() {
95 JSONObject expectedJson = new JSONObject();
96 expectedJson.put("json_class", "Chef::Node");
97 expectedJson.put("chef_type", "node");
98 expectedJson.put("automatic", Collections.emptyMap());
99 expectedJson.put("name", "testNodeName");
100 expectedJson.put("normal", ImmutableMap.of("val", "normal"));
101 expectedJson.put("override", ImmutableMap.of("val", "override"));
102 expectedJson.put("default", ImmutableMap.of("val", "default"));
103 expectedJson.put("run_list", ImmutableList.of("val1", "val2", "val3"));
104 expectedJson.put("chef_environment", "testChefEnvVal");
105 return expectedJson.toString();
109 public void combineStrings_shouldConcatenateTwoParamStrings_andSetThemInSvcContext() {
111 Map<String, String> params = ImmutableMap
112 .of("dgContext", "contextValue", "String1", "paramString1", "String2", "paramString2");
113 SvcLogicContext svcLogicContext = new SvcLogicContext();
116 chefAdapterFactory.create().combineStrings(params, svcLogicContext);
119 assertThat(svcLogicContext.getAttribute("contextValue")).isEqualTo("paramString1paramString2");
123 public void trigger_shouldTriggerTargetEndpoint_andUpdateSvclogicContext() {
125 Map<String, String> params = ImmutableMap.of(IP_PARAM, ENDPOINT_IP);
126 SvcLogicContext svcLogicContext = new SvcLogicContext();
127 given(chefApiClientFactory.create(ENDPOINT_IP, "").get(""))
128 .willReturn(ChefResponse.create(HttpStatus.SC_OK, EXPECTED_RESPONSE_MSG));
131 chefAdapterFactory.create().trigger(params, svcLogicContext);
134 assertThat(svcLogicContext.getAttribute(CHEF_AGENT_CODE_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_OK));
135 assertThat(svcLogicContext.getStatus()).isEqualTo("success");
136 assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_OK));
137 assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(EXPECTED_RESPONSE_MSG);
141 public void trigger_shouldUpdateSvcLogicContext_withFailStatusAndMsg_whenExceptionOccurs() {
143 Map<String, String> params = ImmutableMap.of(IP_PARAM, ENDPOINT_IP);
144 SvcLogicContext svcLogicContext = new SvcLogicContext();
145 given(chefApiClientFactory.create(ENDPOINT_IP, "")).willThrow(new RuntimeException());
148 chefAdapterFactory.create().trigger(params, svcLogicContext);
151 assertThat(svcLogicContext.getAttribute(CHEF_AGENT_CODE_KEY))
152 .isEqualTo(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR));
153 assertThat(svcLogicContext.getAttribute(CHEF_AGENT_MESSAGE_KEY)).isEqualTo(new RuntimeException().toString());
157 public void chefInfo_shouldUpdateSvcLogicContext_withFailStatusAndMsg_andThrowException_whenUsernameParamIsMissing() {
158 Map<String, String> params = ImmutableMap.of(
159 "serverAddress", "http://chefAddress",
160 "organizations", "onap");
161 checkIfInputParamsAreValidated(params);
165 public void chefInfo_shouldUpdateSvcLogicContext_withFailStatusAndMsg_andThrowException_whenServerAddressParamIsMissing() {
166 Map<String, String> params = ImmutableMap.of(
167 "username", "TestUsername",
168 "organizations", "onap");
169 checkIfInputParamsAreValidated(params);
173 public void chefInfo_shouldUpdateSvcLogicContext_withFailStatusAndMsg_andThrowException_whenOrganizationsParamIsMissing() {
174 Map<String, String> params = ImmutableMap.of(
175 "username", "TestUsername",
176 "serverAddress", "http://chefAddress");
177 checkIfInputParamsAreValidated(params);
180 private void checkIfInputParamsAreValidated(Map<String, String> params) {
182 String expectedErrorMsg = "Missing mandatory param(s) such as username, serverAddress, organizations";
183 SvcLogicContext svcLogicContext = new SvcLogicContext();
186 assertThatExceptionOfType(SvcLogicException.class)
187 .isThrownBy(() -> chefAdapterFactory.create().chefGet(params, svcLogicContext))
188 .withMessage("Chef Adapter error:"
190 assertThat(svcLogicContext.getStatus()).isEqualTo("failure");
191 assertThat(svcLogicContext.getAttribute("chefServerResult.code"))
192 .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
193 assertThat(svcLogicContext.getAttribute("chefServerResult.message"))
194 .isEqualTo(expectedErrorMsg);