ChefAdapterImpl 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
25 import com.google.common.collect.ImmutableList;
26 import com.google.common.collect.ImmutableMap;
27 import java.util.Collections;
28 import java.util.Map;
29 import org.json.JSONObject;
30 import org.junit.Test;
31 import org.onap.appc.adapter.chef.ChefAdapter;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
33
34 public class ChefAdapterImplTest {
35
36     private static final String EXPECTED_NODE_OBJECT_ATTR_NAME = "chef.nodeObject";
37
38     @Test
39     public void nodeObjectBuilder_shouldBuildJsonNodeObject_forPassedParams_andAddToSvcLogicContext() {
40         // GIVEN
41         Map<String, String> params = givenInputParams();
42         SvcLogicContext svcLogicContext = new SvcLogicContext();
43
44         // WHEN
45         ChefAdapter chefAdapter = new ChefAdapterFactory().create();
46         chefAdapter.nodeObejctBuilder(params, svcLogicContext);
47
48         // THEN
49         assertThat(resultJson(svcLogicContext)).isEqualTo(expectedJson());
50     }
51
52     private String resultJson(SvcLogicContext svcLogicContext) {
53         String resultJsonString = svcLogicContext.getAttribute(EXPECTED_NODE_OBJECT_ATTR_NAME);
54         return new JSONObject(resultJsonString).toString();
55     }
56
57     private Map<String, String> givenInputParams() {
58         return ImmutableMap.<String, String>builder()
59             .put("nodeobject.name", "testNodeName")
60             .put("nodeobject.normal", "val:normal")
61             .put("nodeobject.overrides", "val:override")
62             .put("nodeobject.defaults", "val:default")
63             .put("nodeobject.run_list", "val1,val2,val3")
64             .put("nodeobject.chef_environment", "testChefEnvVal")
65             .build();
66     }
67
68     private String expectedJson() {
69         JSONObject expectedJson = new JSONObject();
70         expectedJson.put("json_class", "Chef::Node");
71         expectedJson.put("chef_type", "node");
72         expectedJson.put("automatic", Collections.emptyMap());
73         expectedJson.put("name", "testNodeName");
74         expectedJson.put("normal", ImmutableMap.of("val", "normal"));
75         expectedJson.put("override", ImmutableMap.of("val", "override"));
76         expectedJson.put("default", ImmutableMap.of("val", "default"));
77         expectedJson.put("run_list", ImmutableList.of("val1", "val2", "val3"));
78         expectedJson.put("chef_environment", "testChefEnvVal");
79         return expectedJson.toString();
80     }
81
82     @Test
83     public void combineStrings_shouldConcatenateTwoParamStrings_andSetThemInSvcContext() {
84         // GIVEN
85         Map<String, String> params = ImmutableMap
86             .of("dgContext", "contextValue", "String1", "paramString1", "String2", "paramString2");
87         SvcLogicContext svcLogicContext = new SvcLogicContext();
88
89         // WHEN
90         ChefAdapter chefAdapter = new ChefAdapterFactory().create();
91         chefAdapter.combineStrings(params, svcLogicContext);
92
93         // THEN
94         assertThat(svcLogicContext.getAttribute("contextValue")).isEqualTo("paramString1paramString2");
95     }
96 }