+
+ @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();
+ }