ChefAdapterImpl junits 31/43031/3
authorTomasz Gwozdecki <tomasz.gwozdecki@nokia.com>
Mon, 16 Apr 2018 12:22:40 +0000 (08:22 -0400)
committerRanda Maher <rx196w@att.com>
Wed, 25 Apr 2018 00:57:41 +0000 (00:57 +0000)
-Added junit tests for fetchResults method
-Support for Michal Kabaj as part of APPC-437

Change-Id: I3219a5ec0ffd530338ce364f608a9706d5a92c03
Issue-ID: APPC-858
Signed-off-by: Tomasz Gwozdecki <tomasz.gwozdecki@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/ChefAdapterImplVNFCOperationsTest.java

index d26c85c..64aedcc 100644 (file)
@@ -28,6 +28,7 @@ import com.att.eelf.configuration.EELFManager;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import org.apache.commons.lang.StringUtils;
 import org.json.JSONException;
 import org.json.JSONObject;
@@ -288,12 +289,13 @@ public class ChefAdapterImpl implements ChefAdapter {
                         allNodeData = allNodeData.getJSONObject("normal");
                         String attribute = "PushJobOutput";
 
-                        String resultData = allNodeData.optString(attribute);
+                        String resultData = allNodeData.optString(attribute, null);
                         if (resultData == null) {
-                            resultData = allNodeData.optJSONObject(attribute).toString();
-
+                            resultData = Optional.ofNullable(allNodeData.optJSONObject(attribute))
+                                .map(p -> p.toString()).orElse(null);
                             if (resultData == null) {
-                                resultData = allNodeData.optJSONArray(attribute).toString();
+                                resultData = Optional.ofNullable(allNodeData.optJSONArray(attribute))
+                                    .map(p -> p.toString()).orElse(null);
 
                                 if (resultData == null) {
                                     code = 500;
@@ -653,4 +655,4 @@ public class ChefAdapterImpl implements ChefAdapter {
 
         throw new SvcLogicException("Chef Adapter error:" + cutMessage);
     }
-}
+}
\ No newline at end of file
index dadcca5..7f9c505 100644 (file)
@@ -373,9 +373,9 @@ public class ChefAdapterImplVNFCOperationsTest {
             immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]"));
         String expectedErrorMessage =
             "Error posting request: "
-            + CHEF_ADAPTER_ERROR_PREFIX
-            + "Cannot find the private key in the APPC file system, please load the private key to "
-            + CLIENT_PRIVATE_KEY_PATH;
+                + CHEF_ADAPTER_ERROR_PREFIX
+                + "Cannot find the private key in the APPC file system, please load the private key to "
+                + CLIENT_PRIVATE_KEY_PATH;
         given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(false);
 
         // WHEN  // THEN
@@ -390,6 +390,54 @@ public class ChefAdapterImplVNFCOperationsTest {
             .isEqualTo(expectedErrorMessage);
     }
 
+    @Test
+    public void fetchResults_shouldUpdateSvcLogicContextWithJsonResponse_fromSuccessfulChefServerCall()
+        throws SvcLogicException {
+        // GIVEN
+        String json = "{normal:{PushJobOutput : \"ssh start/running, process 1090\"}}";
+        Map<String, String> params = givenInputParams(
+            immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\"]"));
+        given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
+        given(chefApiClientFactory.create(CHEF_END_POINT, ORGANIZATIONS, USERNAME,
+            CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient);
+        given(chefApiClient.get("/nodes/" + "test1.vnf_b.onap.com"))
+            .willReturn(ChefResponse.create(HttpStatus.SC_OK, json));
+
+        // WHEN
+        chefAdapterFactory.create().fetchResults(params, svcLogicContext);
+
+        // THEN
+        assertThat(svcLogicContext.getStatus()).isEqualTo(SUCCESS_STATUS);
+        assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
+            .isEqualTo(Integer.toString(HttpStatus.SC_OK));
+        assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY))
+            .isEqualTo("{\"test1.vnf_b.onap.com\":{\"PushJobOutput\":\"ssh start/running, process 1090\"}}");
+    }
+
+    @Test
+    public void fetchResults_shouldUpdateSvcLogicContextWithFailedMessage_whenReturnedJSONMessageIsMissingAttribute()
+        throws SvcLogicException {
+        // GIVEN
+        String json = "{normal:{invalidKey : \"ssh start/running, process 1090\"}}";
+        Map<String, String> params = givenInputParams(
+            immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\"]"));
+        given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
+        given(chefApiClientFactory.create(CHEF_END_POINT, ORGANIZATIONS, USERNAME,
+            CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient);
+        given(chefApiClient.get("/nodes/" + "test1.vnf_b.onap.com"))
+            .willReturn(ChefResponse.create(HttpStatus.SC_OK, json));
+
+        // WHEN
+        chefAdapterFactory.create().fetchResults(params, svcLogicContext);
+
+        // THEN
+        assertThat(svcLogicContext.getStatus()).isEqualTo(SUCCESS_STATUS);
+        assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
+            .isEqualTo(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR));
+        assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY))
+            .isEqualTo("Cannot find PushJobOutput");
+    }
+
     private Map<String, String> givenInputParams(Entry<String, String>... entries) {
         Builder<String, String> paramsBuilder = ImmutableMap.builder();
         paramsBuilder.put("username", USERNAME)