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;
25 import com.google.common.collect.ImmutableList;
26 import com.google.common.collect.ImmutableMap;
27 import java.util.Collections;
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;
34 public class ChefAdapterImplTest {
36 private static final String EXPECTED_NODE_OBJECT_ATTR_NAME = "chef.nodeObject";
39 public void nodeObjectBuilder_shouldBuildJsonNodeObject_forPassedParams_andAddToSvcLogicContext() {
41 Map<String, String> params = givenInputParams();
42 SvcLogicContext svcLogicContext = new SvcLogicContext();
45 ChefAdapter chefAdapter = new ChefAdapterImpl();
46 chefAdapter.nodeObejctBuilder(params, svcLogicContext);
49 assertThat(resultJson(svcLogicContext)).isEqualTo(expectedJson());
52 private String resultJson(SvcLogicContext svcLogicContext) {
53 String resultJsonString = svcLogicContext.getAttribute(EXPECTED_NODE_OBJECT_ATTR_NAME);
54 return new JSONObject(resultJsonString).toString();
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")
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();
83 public void combineStrings_shouldConcatenateTwoParamStrings_andSetThemInSvcContext() {
85 Map<String, String> params = ImmutableMap
86 .of("dgContext", "contextValue", "String1", "paramString1", "String2", "paramString2");
87 SvcLogicContext svcLogicContext = new SvcLogicContext();
90 ChefAdapter chefAdapter = new ChefAdapterImpl();
91 chefAdapter.combineStrings(params, svcLogicContext);
94 assertThat(svcLogicContext.getAttribute("contextValue")).isEqualTo("paramString1paramString2");