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.mockito.Answers.RETURNS_DEEP_STUBS;
25 import static org.mockito.BDDMockito.given;
27 import com.google.common.collect.ImmutableList;
28 import com.google.common.collect.ImmutableMap;
29 import java.util.Collections;
31 import org.apache.http.HttpStatus;
32 import org.json.JSONObject;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.runners.MockitoJUnitRunner;
38 import org.onap.appc.adapter.chef.chefclient.ChefApiClientFactory;
39 import org.onap.appc.adapter.chef.chefclient.api.ChefResponse;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
42 @RunWith(MockitoJUnitRunner.class)
43 public class ChefAdapterImplTest {
45 private static final String EXPECTED_NODE_OBJECT_ATTR_NAME = "chef.nodeObject";
46 private static final String RESULT_CODE_ATTR_KEY = "chefClientResult.code";
47 private static final String RESULT_MESSAGE_ATTR_KEY = "chefClientResult.message";
48 private static final String EXPECTED_RESPONSE_MSG = "chefResponseMessage";
49 private static final String IP_PARAM = "ip";
50 private static final String ENDPOINT_IP = "http://127.0.0.1";
51 private static final String CHEF_AGENT_CODE_KEY = "chefAgent.code";
52 private static final String CHEF_AGENT_MESSAGE_KEY = "chefAgent.message";
54 @Mock(answer = RETURNS_DEEP_STUBS)
55 private ChefApiClientFactory chefApiClientFactory;
57 private PrivateKeyChecker privateKeyChecker;
60 private ChefAdapterFactory chefAdapterFactory;
63 public void nodeObjectBuilder_shouldBuildJsonNodeObject_forPassedParams_andAddToSvcLogicContext() {
65 Map<String, String> params = givenInputParams();
66 SvcLogicContext svcLogicContext = new SvcLogicContext();
69 chefAdapterFactory.create().nodeObejctBuilder(params, svcLogicContext);
72 assertThat(resultJson(svcLogicContext)).isEqualTo(expectedJson());
75 private String resultJson(SvcLogicContext svcLogicContext) {
76 String resultJsonString = svcLogicContext.getAttribute(EXPECTED_NODE_OBJECT_ATTR_NAME);
77 return new JSONObject(resultJsonString).toString();
80 private Map<String, String> givenInputParams() {
81 return ImmutableMap.<String, String>builder()
82 .put("nodeobject.name", "testNodeName")
83 .put("nodeobject.normal", "val:normal")
84 .put("nodeobject.overrides", "val:override")
85 .put("nodeobject.defaults", "val:default")
86 .put("nodeobject.run_list", "val1,val2,val3")
87 .put("nodeobject.chef_environment", "testChefEnvVal")
91 private String expectedJson() {
92 JSONObject expectedJson = new JSONObject();
93 expectedJson.put("json_class", "Chef::Node");
94 expectedJson.put("chef_type", "node");
95 expectedJson.put("automatic", Collections.emptyMap());
96 expectedJson.put("name", "testNodeName");
97 expectedJson.put("normal", ImmutableMap.of("val", "normal"));
98 expectedJson.put("override", ImmutableMap.of("val", "override"));
99 expectedJson.put("default", ImmutableMap.of("val", "default"));
100 expectedJson.put("run_list", ImmutableList.of("val1", "val2", "val3"));
101 expectedJson.put("chef_environment", "testChefEnvVal");
102 return expectedJson.toString();
106 public void combineStrings_shouldConcatenateTwoParamStrings_andSetThemInSvcContext() {
108 Map<String, String> params = ImmutableMap
109 .of("dgContext", "contextValue", "String1", "paramString1", "String2", "paramString2");
110 SvcLogicContext svcLogicContext = new SvcLogicContext();
113 chefAdapterFactory.create().combineStrings(params, svcLogicContext);
116 assertThat(svcLogicContext.getAttribute("contextValue")).isEqualTo("paramString1paramString2");
120 public void trigger_shouldTriggerTargetEndpoint_andUpdateSvclogicContext() {
122 Map<String, String> params = ImmutableMap.of(IP_PARAM, ENDPOINT_IP);
123 SvcLogicContext svcLogicContext = new SvcLogicContext();
124 given(chefApiClientFactory.create(ENDPOINT_IP).get(""))
125 .willReturn(ChefResponse.create(HttpStatus.SC_OK, EXPECTED_RESPONSE_MSG));
128 chefAdapterFactory.create().trigger(params, svcLogicContext);
131 assertThat(svcLogicContext.getAttribute(CHEF_AGENT_CODE_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_OK));
132 assertThat(svcLogicContext.getStatus()).isEqualTo("success");
133 assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_OK));
134 assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(EXPECTED_RESPONSE_MSG);
138 public void trigger_shouldUpdateSvcLogicContext_withFailStatusAndMsg_whenExceptionOccurs() {
140 Map<String, String> params = ImmutableMap.of(IP_PARAM, ENDPOINT_IP);
141 SvcLogicContext svcLogicContext = new SvcLogicContext();
142 given(chefApiClientFactory.create(ENDPOINT_IP)).willThrow(new RuntimeException());
145 chefAdapterFactory.create().trigger(params, svcLogicContext);
148 assertThat(svcLogicContext.getAttribute(CHEF_AGENT_CODE_KEY))
149 .isEqualTo(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR));
150 assertThat(svcLogicContext.getAttribute(CHEF_AGENT_MESSAGE_KEY)).isEqualTo(new RuntimeException().toString());