aboutsummaryrefslogtreecommitdiffstats
path: root/appc-adapters/appc-chef-adapter
diff options
context:
space:
mode:
authorMichal Kabaj <michal.kabaj@nokia.com>2018-03-15 14:37:19 +0100
committerTakamune Cho <tc012c@att.com>2018-03-16 13:44:33 +0000
commit0bddcbbc0bc1001c52be6e86b6914285d28a90e2 (patch)
tree335b84c0015a642f98a193917037c7421a394507 /appc-adapters/appc-chef-adapter
parent3cbf289d3e7319338031e0ddf270404c8ba16df2 (diff)
ChefAdapterImpl Junits
-Added new Unit tests for ChefAdapterImpl pushJob method -Added new Junit class ChefAdapterImplJobPusher -Some minor construction refactor in other junits Change-Id: Id6fbcba2c5f5e11bfad2c7dccf6c06f777756a47 Issue-ID: APPC-437 Signed-off-by: Michal Kabaj <michal.kabaj@nokia.com>
Diffstat (limited to 'appc-adapters/appc-chef-adapter')
-rw-r--r--appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplDataRetrieverTest.java10
-rw-r--r--appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplHttpMethodTest.java9
-rw-r--r--appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplJobPusherTest.java127
3 files changed, 135 insertions, 11 deletions
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
index d4850cda2..f7891f5bd 100644
--- 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
@@ -23,7 +23,6 @@ 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 {
@@ -41,8 +40,7 @@ public class ChefAdapterImplDataRetrieverTest {
SvcLogicContext svcLogicContext = new SvcLogicContext();
// WHEN
- ChefAdapter chefAdapter = new ChefAdapterFactory().create();
- chefAdapter.retrieveData(params, svcLogicContext);
+ new ChefAdapterFactory().create().retrieveData(params, svcLogicContext);
// THEN
Assertions.assertThat(svcLogicContext.getAttribute(DG_CONTEXT_VALUE)).isEqualTo("testValue");
@@ -55,8 +53,7 @@ public class ChefAdapterImplDataRetrieverTest {
SvcLogicContext svcLogicContext = new SvcLogicContext();
// WHEN
- ChefAdapter chefAdapter = new ChefAdapterFactory().create();
- chefAdapter.retrieveData(params, svcLogicContext);
+ new ChefAdapterFactory().create().retrieveData(params, svcLogicContext);
// THEN
Assertions.assertThat(svcLogicContext.getAttribute(DG_CONTEXT_VALUE)).isEqualTo("{\"param\":\"testValue\"}");
@@ -69,8 +66,7 @@ public class ChefAdapterImplDataRetrieverTest {
SvcLogicContext svcLogicContext = new SvcLogicContext();
// WHEN
- ChefAdapter chefAdapter = new ChefAdapterFactory().create();
- chefAdapter.retrieveData(params, svcLogicContext);
+ new ChefAdapterFactory().create().retrieveData(params, svcLogicContext);
// THEN
Assertions.assertThat(svcLogicContext.getAttribute(DG_CONTEXT_VALUE)).isEqualTo("[\"val1\",\"val2\",\"val3\"]");
diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplHttpMethodTest.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplHttpMethodTest.java
index 82d15366b..d2cc58de2 100644
--- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplHttpMethodTest.java
+++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplHttpMethodTest.java
@@ -31,6 +31,7 @@ import org.apache.http.HttpStatus;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.onap.appc.adapter.chef.ChefAdapter;
@@ -62,12 +63,12 @@ public class ChefAdapterImplHttpMethodTest {
@Mock
private ChefApiClient chefApiClient;
- private ChefAdapter chefAdapter;
+ @InjectMocks
+ private ChefAdapterFactory chefAdapterFactory;
private SvcLogicContext svcLogicContext;
@Before
public void setUp() {
- chefAdapter = new ChefAdapterImpl(chefApiClientFactory, privateKeyChecker);
svcLogicContext = new SvcLogicContext();
}
@@ -122,7 +123,7 @@ public class ChefAdapterImplHttpMethodTest {
given(responseSupplier.get()).willReturn(ChefResponse.create(HttpStatus.SC_OK, EXPECTED_RESPONSE_MSG));
// WHEN
- chefAdapterCall.accept(chefAdapter);
+ chefAdapterCall.accept(chefAdapterFactory.create());
// THEN
assertThat(svcLogicContext.getStatus()).isEqualTo("success");
@@ -135,7 +136,7 @@ public class ChefAdapterImplHttpMethodTest {
given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(false);
// WHEN
- chefAdapterCall.accept(chefAdapter);
+ chefAdapterCall.accept(chefAdapterFactory.create());
// THEN
verifyZeroInteractions(chefApiClient);
diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplJobPusherTest.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplJobPusherTest.java
new file mode 100644
index 000000000..633fe01e1
--- /dev/null
+++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/impl/ChefAdapterImplJobPusherTest.java
@@ -0,0 +1,127 @@
+/*
+ * ============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 static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.mockito.BDDMockito.given;
+
+import com.google.common.collect.ImmutableMap;
+import java.util.Map;
+import org.apache.http.HttpStatus;
+import org.junit.Before;
+import org.junit.Test;
+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.ChefApiClient;
+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 ChefAdapterImplJobPusherTest {
+
+ private static final String CLIENT_PRIVATE_KEY_PATH = "/opt/onap/appc/chef/localhost/onap/testclient.pem";
+ private static final String RESULT_CODE_ATTR_KEY = "chefServerResult.code";
+ private static final String RESULT_MESSAGE_ATTR_KEY = "chefServerResult.message";
+ private static final String EXPECTED_RESPONSE_MSG = "jobs/{666}/";
+
+ private static final String USERNAME = "testclient";
+ private static final String SERVER_ADDRESS = "localhost";
+ private static final String ORGANIZATIONS = "onap";
+ private static final String ACTION_PARAM = "/pushy/jobs";
+ private static final String REQUEST_BODY_DATA = "requestBodyData";
+ private static final Map<String, String> PARAMS = ImmutableMap
+ .of("username", USERNAME,
+ "serverAddress", SERVER_ADDRESS,
+ "organizations", ORGANIZATIONS,
+ "chefAction", ACTION_PARAM,
+ "pushRequest", REQUEST_BODY_DATA);
+ private static final String JOB_ID = "jobID";
+
+ @Mock
+ private PrivateKeyChecker privateKeyChecker;
+ @Mock
+ private ChefApiClientFactory chefApiClientFactory;
+ @Mock
+ private ChefApiClient chefApiClient;
+
+ @InjectMocks
+ private ChefAdapterFactory chefAdapterFactory;
+ private SvcLogicContext svcLogicContext;
+
+ @Before
+ public void setUp() {
+ svcLogicContext = new SvcLogicContext();
+ }
+
+ @Test
+ public void pushJob_shouldSuccessfullyMakePostCall_andUpdateSvcLogicContext_whenReturnedStatusIsDifferentThan_201()
+ throws SvcLogicException {
+ assertSuccessfulPostCallForStatus(HttpStatus.SC_OK);
+ assertThat(svcLogicContext.getAttribute(JOB_ID)).isBlank();
+ }
+
+ @Test
+ public void pushJob_shouldSuccessfullyMakePostCall_andUpdateSvcLogicContext_withReturnedStatusIs_201()
+ throws SvcLogicException {
+ assertSuccessfulPostCallForStatus(HttpStatus.SC_CREATED);
+ assertThat(svcLogicContext.getAttribute(JOB_ID)).isEqualTo("666");
+ }
+
+ public void assertSuccessfulPostCallForStatus(int expectedHttpStatus) throws SvcLogicException {
+ // GIVEN
+ given(chefApiClientFactory.create("https://localhost/organizations/onap", ORGANIZATIONS, USERNAME,
+ CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient);
+ given(chefApiClient.post(ACTION_PARAM, REQUEST_BODY_DATA))
+ .willReturn(ChefResponse.create(expectedHttpStatus, EXPECTED_RESPONSE_MSG));
+
+ // WHEN
+ chefAdapterFactory.create().pushJob(PARAMS, svcLogicContext);
+
+ // THEN
+ assertThat(svcLogicContext.getStatus()).isEqualTo("success");
+ assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
+ .isEqualTo(Integer.toString(expectedHttpStatus));
+ assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(EXPECTED_RESPONSE_MSG);
+ }
+
+ @Test
+ public void pushJob_shouldHandleAllOccurringExceptions_duringMethodExecution() {
+ // GIVEN
+ String EXPECTED_ERROR_MSG = "Something went wrong";
+ given(chefApiClientFactory.create("https://localhost/organizations/onap", ORGANIZATIONS, USERNAME,
+ CLIENT_PRIVATE_KEY_PATH)).willThrow(new NullPointerException(EXPECTED_ERROR_MSG));
+
+ // WHEN // THEN
+ assertThatExceptionOfType(SvcLogicException.class)
+ .isThrownBy(() -> chefAdapterFactory.create().pushJob(PARAMS, svcLogicContext))
+ .withMessage("Chef Adapter error:" + EXPECTED_ERROR_MSG);
+
+ assertThat(svcLogicContext.getStatus()).isEqualTo("failure");
+ assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
+ .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
+ assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(EXPECTED_ERROR_MSG);
+ assertThat(svcLogicContext.getAttribute(JOB_ID)).isBlank();
+ }
+}