Fixes for Chef Adapter bundle
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / test / java / org / onap / appc / adapter / chef / impl / ChefAdapterImplTest.java
index 9289330..3d4ee36 100644 (file)
@@ -3,6 +3,7 @@
  * ONAP : APPC
  * ================================================================================
  * Copyright (C) 2018 Nokia. All rights reserved.
+ * Copyright (C) 2018 AT&T Intellectual Property. 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.
@@ -21,6 +22,7 @@ package org.onap.appc.adapter.chef.impl;
 
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
 import static org.mockito.BDDMockito.given;
 
@@ -38,6 +40,7 @@ import org.mockito.runners.MockitoJUnitRunner;
 import org.onap.appc.adapter.chef.chefclient.ChefApiClientFactory;
 import org.onap.appc.adapter.chef.chefclient.api.ChefResponse;
 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
+import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
 @RunWith(MockitoJUnitRunner.class)
 public class ChefAdapterImplTest {
@@ -121,7 +124,7 @@ public class ChefAdapterImplTest {
         // GIVEN
         Map<String, String> params = ImmutableMap.of(IP_PARAM, ENDPOINT_IP);
         SvcLogicContext svcLogicContext = new SvcLogicContext();
-        given(chefApiClientFactory.create(ENDPOINT_IP).get(""))
+        given(chefApiClientFactory.create(ENDPOINT_IP, "").get(""))
             .willReturn(ChefResponse.create(HttpStatus.SC_OK, EXPECTED_RESPONSE_MSG));
 
         // WHEN
@@ -139,7 +142,7 @@ public class ChefAdapterImplTest {
         // GIVEN
         Map<String, String> params = ImmutableMap.of(IP_PARAM, ENDPOINT_IP);
         SvcLogicContext svcLogicContext = new SvcLogicContext();
-        given(chefApiClientFactory.create(ENDPOINT_IP)).willThrow(new RuntimeException());
+        given(chefApiClientFactory.create(ENDPOINT_IP, "")).willThrow(new RuntimeException());
 
         // WHEN
         chefAdapterFactory.create().trigger(params, svcLogicContext);
@@ -149,4 +152,45 @@ public class ChefAdapterImplTest {
             .isEqualTo(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR));
         assertThat(svcLogicContext.getAttribute(CHEF_AGENT_MESSAGE_KEY)).isEqualTo(new RuntimeException().toString());
     }
+
+    @Test
+    public void chefInfo_shouldUpdateSvcLogicContext_withFailStatusAndMsg_andThrowException_whenUsernameParamIsMissing() {
+        Map<String, String> params = ImmutableMap.of(
+            "serverAddress", "http://chefAddress",
+            "organizations", "onap");
+        checkIfInputParamsAreValidated(params);
+    }
+
+    @Test
+    public void chefInfo_shouldUpdateSvcLogicContext_withFailStatusAndMsg_andThrowException_whenServerAddressParamIsMissing() {
+        Map<String, String> params = ImmutableMap.of(
+            "username", "TestUsername",
+            "organizations", "onap");
+        checkIfInputParamsAreValidated(params);
+    }
+
+    @Test
+    public void chefInfo_shouldUpdateSvcLogicContext_withFailStatusAndMsg_andThrowException_whenOrganizationsParamIsMissing() {
+        Map<String, String> params = ImmutableMap.of(
+            "username", "TestUsername",
+            "serverAddress", "http://chefAddress");
+        checkIfInputParamsAreValidated(params);
+    }
+
+    private void checkIfInputParamsAreValidated(Map<String, String> params) {
+        // GIVEN
+        String expectedErrorMsg = "Missing mandatory param(s) such as username, serverAddress, organizations";
+        SvcLogicContext svcLogicContext = new SvcLogicContext();
+
+        // WHEN// THEN
+        assertThatExceptionOfType(SvcLogicException.class)
+            .isThrownBy(() -> chefAdapterFactory.create().chefGet(params, svcLogicContext))
+            .withMessage("Chef Adapter error:"
+                + expectedErrorMsg);
+        assertThat(svcLogicContext.getStatus()).isEqualTo("failure");
+        assertThat(svcLogicContext.getAttribute("chefServerResult.code"))
+            .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
+        assertThat(svcLogicContext.getAttribute("chefServerResult.message"))
+            .isEqualTo(expectedErrorMsg);
+    }
 }