92893303d16c636c23d5be6d477bb6a1e4142a68
[appc.git] /
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.mockito.Answers.RETURNS_DEEP_STUBS;
25 import static org.mockito.BDDMockito.given;
26
27 import com.google.common.collect.ImmutableList;
28 import com.google.common.collect.ImmutableMap;
29 import java.util.Collections;
30 import java.util.Map;
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;
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class ChefAdapterImplTest {
44
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";
53
54     @Mock(answer = RETURNS_DEEP_STUBS)
55     private ChefApiClientFactory chefApiClientFactory;
56     @Mock
57     private PrivateKeyChecker privateKeyChecker;
58
59     @InjectMocks
60     private ChefAdapterFactory chefAdapterFactory;
61
62     @Test
63     public void nodeObjectBuilder_shouldBuildJsonNodeObject_forPassedParams_andAddToSvcLogicContext() {
64         // GIVEN
65         Map<String, String> params = givenInputParams();
66         SvcLogicContext svcLogicContext = new SvcLogicContext();
67
68         // WHEN
69         chefAdapterFactory.create().nodeObejctBuilder(params, svcLogicContext);
70
71         // THEN
72         assertThat(resultJson(svcLogicContext)).isEqualTo(expectedJson());
73     }
74
75     private String resultJson(SvcLogicContext svcLogicContext) {
76         String resultJsonString = svcLogicContext.getAttribute(EXPECTED_NODE_OBJECT_ATTR_NAME);
77         return new JSONObject(resultJsonString).toString();
78     }
79
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")
88             .build();
89     }
90
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();
103     }
104
105     @Test
106     public void combineStrings_shouldConcatenateTwoParamStrings_andSetThemInSvcContext() {
107         // GIVEN
108         Map<String, String> params = ImmutableMap
109             .of("dgContext", "contextValue", "String1", "paramString1", "String2", "paramString2");
110         SvcLogicContext svcLogicContext = new SvcLogicContext();
111
112         // WHEN
113         chefAdapterFactory.create().combineStrings(params, svcLogicContext);
114
115         // THEN
116         assertThat(svcLogicContext.getAttribute("contextValue")).isEqualTo("paramString1paramString2");
117     }
118
119     @Test
120     public void trigger_shouldTriggerTargetEndpoint_andUpdateSvclogicContext() {
121         // GIVEN
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));
126
127         // WHEN
128         chefAdapterFactory.create().trigger(params, svcLogicContext);
129
130         // THEN
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);
135     }
136
137     @Test
138     public void trigger_shouldUpdateSvcLogicContext_withFailStatusAndMsg_whenExceptionOccurs() {
139         // GIVEN
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());
143
144         // WHEN
145         chefAdapterFactory.create().trigger(params, svcLogicContext);
146
147         // THEN
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());
151     }
152 }