ChefAdapterImpl JUnits 51/34051/2
authorMichal Kabaj <michal.kabaj@nokia.com>
Mon, 5 Mar 2018 12:42:17 +0000 (13:42 +0100)
committerPatrick Brady <pb071s@att.com>
Tue, 6 Mar 2018 17:11:29 +0000 (17:11 +0000)
-Added unit testcases for retrieveData method - extracted new test class
-Added unit test for combineStrings method

Change-Id: I8adcc9d2dc17e4ebf305fc2a8139095d5841a445
Issue-ID: APPC-437
Signed-off-by: Michal Kabaj <michal.kabaj@nokia.com>
appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/ChefAdapterImpl.java
appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplDataRetrieverTest.java [new file with mode: 0644]
appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplTest.java

index 69b57f7..4a4081e 100644 (file)
@@ -416,10 +416,7 @@ public class ChefAdapterImpl implements ChefAdapter {
         JSONObject jsonConfig = new JSONObject(allConfigData);
         String contextData = fetchContextData(key, jsonConfig);
 
-        RequestContext rc = new RequestContext(ctx);
-        rc.isAlive();
-        SvcLogicContext svcLogic = rc.getSvcLogicContext();
-        svcLogic.setAttribute(dgContext, contextData);
+        ctx.setAttribute(dgContext, contextData);
     }
 
     private String fetchContextData(String key, JSONObject jsonConfig) {
@@ -443,10 +440,7 @@ public class ChefAdapterImpl implements ChefAdapter {
         String string2 = params.get("String2");
         String dgContext = params.get("dgContext");
         String contextData = string1 + string2;
-        RequestContext rc = new RequestContext(ctx);
-        rc.isAlive();
-        SvcLogicContext svcLogic = rc.getSvcLogicContext();
-        svcLogic.setAttribute(dgContext, contextData);
+        ctx.setAttribute(dgContext, contextData);
     }
 
     /**
diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplDataRetrieverTest.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplDataRetrieverTest.java
new file mode 100644 (file)
index 0000000..1f447a4
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP : APPC
+ * ================================================================================
+ * Copyright (C) 2018 Nokia. All rights reserved.
+ * =============================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.appc.adapter.chef.impl;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.Map;
+import org.assertj.core.api.Assertions;
+import org.junit.Test;
+import org.onap.appc.adapter.chef.ChefAdapter;
+import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
+
+public class ChefAdapterImplDataRetrieverTest {
+
+    private static final String KEY_PARAM = "key";
+    private static final String DG_CONTEXT_PARAM = "dgContext";
+    private static final String ALL_CONFIG_PARAM = "allConfig";
+    private static final String KEY_VALUE = "keyValue";
+    private static final String DG_CONTEXT_VALUE = "contextValue";
+
+    @Test
+    public void retrieveData_shouldSetContextData_withExtractedJsonString() {
+        // GIVEN
+        Map<String, String> params = givenParamMapWithJson("{" + KEY_VALUE + ":testValue}");
+        SvcLogicContext svcLogicContext = new SvcLogicContext();
+
+        // WHEN
+        ChefAdapter chefAdapter = new ChefAdapterImpl();
+        chefAdapter.retrieveData(params, svcLogicContext);
+
+        // THEN
+        Assertions.assertThat(svcLogicContext.getAttribute(DG_CONTEXT_VALUE)).isEqualTo("testValue");
+    }
+
+    @Test
+    public void retrieveData_shouldSetContextData_withExtractedJsonObject() {
+        // GIVEN
+        Map<String, String> params = givenParamMapWithJson("{" + KEY_VALUE + ": {param : testValue} }");
+        SvcLogicContext svcLogicContext = new SvcLogicContext();
+
+        // WHEN
+        ChefAdapter chefAdapter = new ChefAdapterImpl();
+        chefAdapter.retrieveData(params, svcLogicContext);
+
+        // THEN
+        Assertions.assertThat(svcLogicContext.getAttribute(DG_CONTEXT_VALUE)).isEqualTo("{\"param\":\"testValue\"}");
+    }
+
+    @Test
+    public void retrieveData_shouldSetContextData_withExtractedJsonArray() {
+        // GIVEN
+        Map<String, String> params = givenParamMapWithJson("{" + KEY_VALUE + ": [val1, val2, val3] }");
+        SvcLogicContext svcLogicContext = new SvcLogicContext();
+
+        // WHEN
+        ChefAdapter chefAdapter = new ChefAdapterImpl();
+        chefAdapter.retrieveData(params, svcLogicContext);
+
+        // THEN
+        Assertions.assertThat(svcLogicContext.getAttribute(DG_CONTEXT_VALUE)).isEqualTo("[\"val1\",\"val2\",\"val3\"]");
+    }
+
+    private Map<String, String> givenParamMapWithJson(String json) {
+        return ImmutableMap
+            .of(KEY_PARAM, KEY_VALUE,
+                DG_CONTEXT_PARAM, DG_CONTEXT_VALUE,
+                ALL_CONFIG_PARAM, json);
+    }
+}
\ No newline at end of file
index c4c3e30..df83267 100644 (file)
@@ -39,10 +39,10 @@ public class ChefAdapterImplTest {
     public void nodeObjectBuilder_shouldBuildJsonNodeObject_forPassedParams_andAddToSvcLogicContext() {
         // GIVEN
         Map<String, String> params = givenInputParams();
+        SvcLogicContext svcLogicContext = new SvcLogicContext();
 
         // WHEN
         ChefAdapter chefAdapter = new ChefAdapterImpl();
-        SvcLogicContext svcLogicContext = new SvcLogicContext();
         chefAdapter.nodeObejctBuilder(params, svcLogicContext);
 
         // THEN
@@ -78,4 +78,19 @@ public class ChefAdapterImplTest {
         expectedJson.put("chef_environment", "testChefEnvVal");
         return expectedJson.toString();
     }
+
+    @Test
+    public void combineStrings_shouldConcatenateTwoParamStrings_andSetThemInSvcContext() {
+        // GIVEN
+        Map<String, String> params = ImmutableMap
+            .of("dgContext", "contextValue", "String1", "paramString1", "String2", "paramString2");
+        SvcLogicContext svcLogicContext = new SvcLogicContext();
+
+        // WHEN
+        ChefAdapter chefAdapter = new ChefAdapterImpl();
+        chefAdapter.combineStrings(params, svcLogicContext);
+
+        // THEN
+        assertThat(svcLogicContext.getAttribute("contextValue")).isEqualTo("paramString1paramString2");
+    }
 }
\ No newline at end of file