Fixes for Chef Adapter bundle
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / test / java / org / onap / appc / adapter / chef / impl / ChefAdapterImplTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.appc.adapter.chef.impl;
22
23
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;
28
29 import com.google.common.collect.ImmutableList;
30 import com.google.common.collect.ImmutableMap;
31 import java.util.Collections;
32 import java.util.Map;
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;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class ChefAdapterImplTest {
47
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";
56
57     @Mock(answer = RETURNS_DEEP_STUBS)
58     private ChefApiClientFactory chefApiClientFactory;
59     @Mock
60     private PrivateKeyChecker privateKeyChecker;
61
62     @InjectMocks
63     private ChefAdapterFactory chefAdapterFactory;
64
65     @Test
66     public void nodeObjectBuilder_shouldBuildJsonNodeObject_forPassedParams_andAddToSvcLogicContext() {
67         // GIVEN
68         Map<String, String> params = givenInputParams();
69         SvcLogicContext svcLogicContext = new SvcLogicContext();
70
71         // WHEN
72         chefAdapterFactory.create().nodeObejctBuilder(params, svcLogicContext);
73
74         // THEN
75         assertThat(resultJson(svcLogicContext)).isEqualTo(expectedJson());
76     }
77
78     private String resultJson(SvcLogicContext svcLogicContext) {
79         String resultJsonString = svcLogicContext.getAttribute(EXPECTED_NODE_OBJECT_ATTR_NAME);
80         return new JSONObject(resultJsonString).toString();
81     }
82
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")
91             .build();
92     }
93
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();
106     }
107
108     @Test
109     public void combineStrings_shouldConcatenateTwoParamStrings_andSetThemInSvcContext() {
110         // GIVEN
111         Map<String, String> params = ImmutableMap
112             .of("dgContext", "contextValue", "String1", "paramString1", "String2", "paramString2");
113         SvcLogicContext svcLogicContext = new SvcLogicContext();
114
115         // WHEN
116         chefAdapterFactory.create().combineStrings(params, svcLogicContext);
117
118         // THEN
119         assertThat(svcLogicContext.getAttribute("contextValue")).isEqualTo("paramString1paramString2");
120     }
121
122     @Test
123     public void trigger_shouldTriggerTargetEndpoint_andUpdateSvclogicContext() {
124         // GIVEN
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));
129
130         // WHEN
131         chefAdapterFactory.create().trigger(params, svcLogicContext);
132
133         // THEN
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);
138     }
139
140     @Test
141     public void trigger_shouldUpdateSvcLogicContext_withFailStatusAndMsg_whenExceptionOccurs() {
142         // GIVEN
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());
146
147         // WHEN
148         chefAdapterFactory.create().trigger(params, svcLogicContext);
149
150         // THEN
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());
154     }
155
156     @Test
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);
162     }
163
164     @Test
165     public void chefInfo_shouldUpdateSvcLogicContext_withFailStatusAndMsg_andThrowException_whenServerAddressParamIsMissing() {
166         Map<String, String> params = ImmutableMap.of(
167             "username", "TestUsername",
168             "organizations", "onap");
169         checkIfInputParamsAreValidated(params);
170     }
171
172     @Test
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);
178     }
179
180     private void checkIfInputParamsAreValidated(Map<String, String> params) {
181         // GIVEN
182         String expectedErrorMsg = "Missing mandatory param(s) such as username, serverAddress, organizations";
183         SvcLogicContext svcLogicContext = new SvcLogicContext();
184
185         // WHEN// THEN
186         assertThatExceptionOfType(SvcLogicException.class)
187             .isThrownBy(() -> chefAdapterFactory.create().chefGet(params, svcLogicContext))
188             .withMessage("Chef Adapter error:"
189                 + expectedErrorMsg);
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);
195     }
196 }