ChefAdapterImpl JUnits
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / test / java / org / onap / appc / adapter / chef / impl / ChefAdapterImplDataRetrieverTest.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 import com.google.common.collect.ImmutableMap;
23 import java.util.Map;
24 import org.assertj.core.api.Assertions;
25 import org.junit.Test;
26 import org.onap.appc.adapter.chef.ChefAdapter;
27 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
28
29 public class ChefAdapterImplDataRetrieverTest {
30
31     private static final String KEY_PARAM = "key";
32     private static final String DG_CONTEXT_PARAM = "dgContext";
33     private static final String ALL_CONFIG_PARAM = "allConfig";
34     private static final String KEY_VALUE = "keyValue";
35     private static final String DG_CONTEXT_VALUE = "contextValue";
36
37     @Test
38     public void retrieveData_shouldSetContextData_withExtractedJsonString() {
39         // GIVEN
40         Map<String, String> params = givenParamMapWithJson("{" + KEY_VALUE + ":testValue}");
41         SvcLogicContext svcLogicContext = new SvcLogicContext();
42
43         // WHEN
44         ChefAdapter chefAdapter = new ChefAdapterFactory().create();
45         chefAdapter.retrieveData(params, svcLogicContext);
46
47         // THEN
48         Assertions.assertThat(svcLogicContext.getAttribute(DG_CONTEXT_VALUE)).isEqualTo("testValue");
49     }
50
51     @Test
52     public void retrieveData_shouldSetContextData_withExtractedJsonObject() {
53         // GIVEN
54         Map<String, String> params = givenParamMapWithJson("{" + KEY_VALUE + ": {param : testValue} }");
55         SvcLogicContext svcLogicContext = new SvcLogicContext();
56
57         // WHEN
58         ChefAdapter chefAdapter = new ChefAdapterFactory().create();
59         chefAdapter.retrieveData(params, svcLogicContext);
60
61         // THEN
62         Assertions.assertThat(svcLogicContext.getAttribute(DG_CONTEXT_VALUE)).isEqualTo("{\"param\":\"testValue\"}");
63     }
64
65     @Test
66     public void retrieveData_shouldSetContextData_withExtractedJsonArray() {
67         // GIVEN
68         Map<String, String> params = givenParamMapWithJson("{" + KEY_VALUE + ": [val1, val2, val3] }");
69         SvcLogicContext svcLogicContext = new SvcLogicContext();
70
71         // WHEN
72         ChefAdapter chefAdapter = new ChefAdapterFactory().create();
73         chefAdapter.retrieveData(params, svcLogicContext);
74
75         // THEN
76         Assertions.assertThat(svcLogicContext.getAttribute(DG_CONTEXT_VALUE)).isEqualTo("[\"val1\",\"val2\",\"val3\"]");
77     }
78
79     private Map<String, String> givenParamMapWithJson(String json) {
80         return ImmutableMap
81             .of(KEY_PARAM, KEY_VALUE,
82                 DG_CONTEXT_PARAM, DG_CONTEXT_VALUE,
83                 ALL_CONFIG_PARAM, json);
84     }
85 }