summaryrefslogtreecommitdiffstats
path: root/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test
diff options
context:
space:
mode:
authorMichal Kabaj <michal.kabaj@nokia.com>2018-03-13 12:18:08 +0100
committerMichal Kabaj <michal.kabaj@nokia.com>2018-03-13 12:18:08 +0100
commitd206edd8b89ddd251b24e635227fae2814b0152e (patch)
tree8b29c086ac731de4154bdda6b4d9fcd71d72333d /appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test
parentbbc49d304603dd4cccade55cc5f5711df3024592 (diff)
ChefAdapterImpl Junits
-Added new Unit tests for ChefAdapterImpl#trigger method -Refactored ChefAdapterFactory to allow mock injection -Added new create() method to ChefApiClientFactory which does not set any Chef specific headers for the purpose of trigger method -Cleaned up the trigger method - simplified usage, removed useless doFailure method and inlined its logic and unified HttClient usage -> HttpClient is not used directly but through existing ChefApiClient -removed redundant TestChefAdapterImpl tests which have already been rewritten Change-Id: I830d203877224aa2c5e1b0dff39d1171f1f2f0ef Issue-ID: APPC-437 Signed-off-by: Michal Kabaj <michal.kabaj@nokia.com>
Diffstat (limited to 'appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test')
-rw-r--r--appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplTest.java68
-rw-r--r--appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/TestChefAdapterImpl.java115
2 files changed, 62 insertions, 121 deletions
diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplTest.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplTest.java
index 7b3546765..92893303d 100644
--- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplTest.java
+++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplTest.java
@@ -21,19 +21,43 @@ package org.onap.appc.adapter.chef.impl;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Answers.RETURNS_DEEP_STUBS;
+import static org.mockito.BDDMockito.given;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.Collections;
import java.util.Map;
+import org.apache.http.HttpStatus;
import org.json.JSONObject;
import org.junit.Test;
-import org.onap.appc.adapter.chef.ChefAdapter;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+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;
+@RunWith(MockitoJUnitRunner.class)
public class ChefAdapterImplTest {
private static final String EXPECTED_NODE_OBJECT_ATTR_NAME = "chef.nodeObject";
+ private static final String RESULT_CODE_ATTR_KEY = "chefClientResult.code";
+ private static final String RESULT_MESSAGE_ATTR_KEY = "chefClientResult.message";
+ private static final String EXPECTED_RESPONSE_MSG = "chefResponseMessage";
+ private static final String IP_PARAM = "ip";
+ private static final String ENDPOINT_IP = "http://127.0.0.1";
+ private static final String CHEF_AGENT_CODE_KEY = "chefAgent.code";
+ private static final String CHEF_AGENT_MESSAGE_KEY = "chefAgent.message";
+
+ @Mock(answer = RETURNS_DEEP_STUBS)
+ private ChefApiClientFactory chefApiClientFactory;
+ @Mock
+ private PrivateKeyChecker privateKeyChecker;
+
+ @InjectMocks
+ private ChefAdapterFactory chefAdapterFactory;
@Test
public void nodeObjectBuilder_shouldBuildJsonNodeObject_forPassedParams_andAddToSvcLogicContext() {
@@ -42,8 +66,7 @@ public class ChefAdapterImplTest {
SvcLogicContext svcLogicContext = new SvcLogicContext();
// WHEN
- ChefAdapter chefAdapter = new ChefAdapterFactory().create();
- chefAdapter.nodeObejctBuilder(params, svcLogicContext);
+ chefAdapterFactory.create().nodeObejctBuilder(params, svcLogicContext);
// THEN
assertThat(resultJson(svcLogicContext)).isEqualTo(expectedJson());
@@ -87,10 +110,43 @@ public class ChefAdapterImplTest {
SvcLogicContext svcLogicContext = new SvcLogicContext();
// WHEN
- ChefAdapter chefAdapter = new ChefAdapterFactory().create();
- chefAdapter.combineStrings(params, svcLogicContext);
+ chefAdapterFactory.create().combineStrings(params, svcLogicContext);
// THEN
assertThat(svcLogicContext.getAttribute("contextValue")).isEqualTo("paramString1paramString2");
}
-} \ No newline at end of file
+
+ @Test
+ public void trigger_shouldTriggerTargetEndpoint_andUpdateSvclogicContext() {
+ // GIVEN
+ Map<String, String> params = ImmutableMap.of(IP_PARAM, ENDPOINT_IP);
+ SvcLogicContext svcLogicContext = new SvcLogicContext();
+ given(chefApiClientFactory.create(ENDPOINT_IP).get(""))
+ .willReturn(ChefResponse.create(HttpStatus.SC_OK, EXPECTED_RESPONSE_MSG));
+
+ // WHEN
+ chefAdapterFactory.create().trigger(params, svcLogicContext);
+
+ // THEN
+ assertThat(svcLogicContext.getAttribute(CHEF_AGENT_CODE_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_OK));
+ assertThat(svcLogicContext.getStatus()).isEqualTo("success");
+ assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_OK));
+ assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(EXPECTED_RESPONSE_MSG);
+ }
+
+ @Test
+ public void trigger_shouldUpdateSvcLogicContext_withFailStatusAndMsg_whenExceptionOccurs() {
+ // GIVEN
+ Map<String, String> params = ImmutableMap.of(IP_PARAM, ENDPOINT_IP);
+ SvcLogicContext svcLogicContext = new SvcLogicContext();
+ given(chefApiClientFactory.create(ENDPOINT_IP)).willThrow(new RuntimeException());
+
+ // WHEN
+ chefAdapterFactory.create().trigger(params, svcLogicContext);
+
+ // THEN
+ assertThat(svcLogicContext.getAttribute(CHEF_AGENT_CODE_KEY))
+ .isEqualTo(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR));
+ assertThat(svcLogicContext.getAttribute(CHEF_AGENT_MESSAGE_KEY)).isEqualTo(new RuntimeException().toString());
+ }
+}
diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/TestChefAdapterImpl.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/TestChefAdapterImpl.java
deleted file mode 100644
index e7d7e77b9..000000000
--- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/TestChefAdapterImpl.java
+++ /dev/null
@@ -1,115 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP : APPC
- * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- * Copyright (C) 2017 Amdocs
- * =============================================================================
- * 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.
- *
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.appc.adapter.chef.impl;
-
-import static org.junit.Assert.assertEquals;
-
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.onap.appc.adapter.chef.ChefAdapter;
-import org.onap.appc.exceptions.APPCException;
-import com.att.cdp.exceptions.ZoneException;
-import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
-import org.onap.ccsdk.sli.core.sli.SvcLogicException;
-
-public class TestChefAdapterImpl {
- private SvcLogicContext svcContext;
-
- private ChefAdapter adapter;
-
- private Map<String, String> params;
- private String getAttribute;
-
- @Before
- public void setup() {
- adapter = new ChefAdapterFactory().create();
- params = new HashMap<>();
- params.put("pemPath",
- "/src/test/resources/testclient.pem");
- }
-
- @After
- public void tearDown() {
- params = null;
- svcContext = null;
- getAttribute = null;
- }
-
- @Test(expected=Exception.class)
- public void testChefGetFail() throws IOException, IllegalStateException, IllegalArgumentException,
- ZoneException, APPCException,SvcLogicException {
- params.put("chefAction", "/nodes");
-
- givenParams(params, "chefGet");
- thenResponseShouldFail();
- }
-
- @Test(expected=Exception.class)
- public void testChefPutFail() throws IOException, IllegalStateException, IllegalArgumentException,
- ZoneException, APPCException,SvcLogicException {
- params.put("chefAction", "/nodes/testnode");
- params.put("runList", "recipe[commandtest]");
- params.put("attributes", "");
- params.put("chefRequestBody", "Test Body");
-
- givenParams(params, "chefPut");
- thenResponseShouldFail();
- }
-
- @Test
- public void testTriggerFail() throws IOException, IllegalStateException, IllegalArgumentException,
- ZoneException, APPCException,SvcLogicException {
- params.put("ip", "");
-
- givenParams(params, "trigger");
- thenResponseShouldFail();
- }
-
- private void givenParams(Map<String, String> adapterParams, String method) throws SvcLogicException {
- svcContext = new SvcLogicContext();
- if (method == "chefGet"){
- adapter.chefGet(adapterParams, svcContext);
- getAttribute = "chefServerResult.code";
- }
- if (method == "chefPut"){
- adapter.chefPut(adapterParams, svcContext);
- getAttribute = "chefServerResult.code";
- }
- if (method == "trigger"){
- adapter.trigger(adapterParams, svcContext);
- getAttribute = "chefAgent.code";
- }
- }
-
- private void thenResponseShouldFail(){
- String status = svcContext.getAttribute(this.getAttribute);
- assertEquals("500", status);
- }
-}