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-16 14:53:36 +0100
committerTakamune Cho <tc012c@att.com>2018-03-20 12:49:47 +0000
commit1f03b498a2bf4c125f8cbd3a0b6a2eddffbbea4f (patch)
tree9f3beb7c535c29f8b3d589afc7fdad5605587d19 /appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test
parentb7a9acc09f070ed2cff1820b799afaa3e351006f (diff)
ChefAdapterImpl Junits
- Added new Unit tests for ChefAdapterImpl checkPushJob method to class ChefAdapterImplJobPusherTest - Added param check in implementation method - Small refactor of nested conditions for improved readability Change-Id: Id65b897535874eea6aafda66b7ac227832446cf4 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/ChefAdapterImplJobPusherTest.java144
1 files changed, 132 insertions, 12 deletions
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
index 633fe01e1..f4d19e22b 100644
--- 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
@@ -19,12 +19,15 @@
*/
package org.onap.appc.adapter.chef.impl;
+import static com.google.common.collect.Maps.immutableEntry;
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 com.google.common.collect.ImmutableMap.Builder;
import java.util.Map;
+import java.util.Map.Entry;
import org.apache.http.HttpStatus;
import org.junit.Before;
import org.junit.Test;
@@ -51,12 +54,6 @@ public class ChefAdapterImplJobPusherTest {
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
@@ -91,13 +88,16 @@ public class ChefAdapterImplJobPusherTest {
public void assertSuccessfulPostCallForStatus(int expectedHttpStatus) throws SvcLogicException {
// GIVEN
+ Map<String, String> params = givenInputParams(
+ immutableEntry("chefAction", ACTION_PARAM),
+ immutableEntry("pushRequest", REQUEST_BODY_DATA));
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);
+ chefAdapterFactory.create().pushJob(params, svcLogicContext);
// THEN
assertThat(svcLogicContext.getStatus()).isEqualTo("success");
@@ -109,19 +109,139 @@ public class ChefAdapterImplJobPusherTest {
@Test
public void pushJob_shouldHandleAllOccurringExceptions_duringMethodExecution() {
// GIVEN
- String EXPECTED_ERROR_MSG = "Something went wrong";
+ Map<String, String> params = givenInputParams();
+ String expectedErrorMessage = "Something went wrong";
given(chefApiClientFactory.create("https://localhost/organizations/onap", ORGANIZATIONS, USERNAME,
- CLIENT_PRIVATE_KEY_PATH)).willThrow(new NullPointerException(EXPECTED_ERROR_MSG));
+ CLIENT_PRIVATE_KEY_PATH)).willThrow(new NullPointerException(expectedErrorMessage));
// WHEN // THEN
assertThatExceptionOfType(SvcLogicException.class)
- .isThrownBy(() -> chefAdapterFactory.create().pushJob(PARAMS, svcLogicContext))
- .withMessage("Chef Adapter error:" + EXPECTED_ERROR_MSG);
+ .isThrownBy(() -> chefAdapterFactory.create().pushJob(params, svcLogicContext))
+ .withMessage("Chef Adapter error:" + expectedErrorMessage);
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(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedErrorMessage);
assertThat(svcLogicContext.getAttribute(JOB_ID)).isBlank();
}
+
+ @Test
+ public void checkPushJob_shouldSetFailStatusAndMsgInContext_andThrowException_whenRetryTimesParamIsMissing() {
+ // GIVEN
+ Map<String, String> params = givenInputParams(
+ immutableEntry("retryInterval", "1"),
+ immutableEntry("jobid", "666"));
+
+ // WHEN // THEN
+ assertIfInputParamsAreValidated(params);
+ }
+
+ @Test
+ public void checkPushJob_shouldSetFailStatusAndMsgInContext_andThrowException_whenRetryIntervalParamIsMissing() {
+ // GIVEN
+ Map<String, String> params = givenInputParams(
+ immutableEntry("retryTimes", "4"),
+ immutableEntry("jobid", "666"));
+
+ // WHEN // THEN
+ assertIfInputParamsAreValidated(params);
+ }
+
+ @Test
+ public void checkPushJob_shouldSetFailStatusAndMsgInContext_andThrowException_whenJobIdParamIsMissing() {
+ // GIVEN
+ Map<String, String> params = givenInputParams(
+ immutableEntry("retryTimes", "4"),
+ immutableEntry("retryInterval", "1"));
+ assertIfInputParamsAreValidated(params);
+ }
+
+ public void assertIfInputParamsAreValidated(Map<String, String> params) {
+ // WHEN // THEN
+ assertThatExceptionOfType(SvcLogicException.class)
+ .isThrownBy(() -> chefAdapterFactory.create().checkPushJob(params, svcLogicContext))
+ .withMessage("Chef Adapter error:" + "Missing Mandatory param(s) retryTimes , retryInterval ");
+
+ 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("Missing Mandatory param(s) retryTimes , retryInterval ");
+ }
+
+ @Test
+ public void checkPushJob_shouldCheckJobStatusOnlyOnce_withoutAdditionalRetries_whenFirstReturnedJobStatusIs_Complete()
+ throws SvcLogicException {
+ String expectedHttpStatus = Integer.toString(HttpStatus.SC_OK);
+ String expectedMessage = "{status:complete}";
+
+ assertCheckJobStatusFor(
+ expectedHttpStatus,
+ expectedMessage,
+ ChefResponse.create(HttpStatus.SC_OK, "{status:complete}"),
+ ChefResponse.create(HttpStatus.SC_OK, "{status:running}"));
+ }
+
+ @Test
+ public void checkPushJob_shouldCheckJobStatusExpectedNumberOf_ThreeRetryTimes_whenEachReturnedStatusIs_Running()
+ throws SvcLogicException {
+ String expectedHttpStatus = Integer.toString(HttpStatus.SC_ACCEPTED);
+ String expectedMessage = "chef client runtime out";
+
+ assertCheckJobStatusFor(
+ expectedHttpStatus,
+ expectedMessage,
+ ChefResponse.create(HttpStatus.SC_OK, "{status:running}"),
+ ChefResponse.create(HttpStatus.SC_OK, "{status:running}"),
+ ChefResponse.create(HttpStatus.SC_OK, "{status:running}"));
+ }
+
+ @Test
+ public void checkPushJob_shouldCheckJobStatusOnlyOnce_withoutAdditionalRetries_whenFirstReturnedJobStatusIsNot_Running()
+ throws SvcLogicException {
+
+ String expectedHttpStatus = Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR);
+ String expectedMessage = "{status:unexpectedStatus}";
+
+ assertCheckJobStatusFor(
+ expectedHttpStatus,
+ expectedMessage,
+ ChefResponse.create(HttpStatus.SC_OK, "{status:unexpectedStatus}"),
+ ChefResponse.create(HttpStatus.SC_OK, "{status:running}"));
+ }
+
+ public void assertCheckJobStatusFor(String expectedHttpStatus, String expectedMessage, ChefResponse firstResponse,
+ ChefResponse... nextResponses) throws SvcLogicException {
+
+ // GIVEN
+ Map<String, String> params = givenInputParams(
+ immutableEntry("jobid", "666"),
+ immutableEntry("retryTimes", "3"),
+ immutableEntry("retryInterval", "1"));
+ given(chefApiClientFactory.create("https://localhost/organizations/onap", ORGANIZATIONS, USERNAME,
+ CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient);
+ given(chefApiClient.get(ACTION_PARAM + "/" + params.get("jobid")))
+ .willReturn(firstResponse, nextResponses);
+
+ // WHEN
+ chefAdapterFactory.create().checkPushJob(params, svcLogicContext);
+
+ // THEN
+ assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
+ .isEqualTo(expectedHttpStatus);
+ assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedMessage);
+ }
+
+ private Map<String, String> givenInputParams(Entry<String, String>... entries) {
+ Builder<String, String> paramsBuilder = ImmutableMap.builder();
+ paramsBuilder.put("username", USERNAME)
+ .put("serverAddress", SERVER_ADDRESS)
+ .put("organizations", ORGANIZATIONS);
+
+ for (Entry<String, String> entry : entries) {
+ paramsBuilder.put(entry);
+ }
+ return paramsBuilder.build();
+ }
}