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