+ @Test
+ public void vnfcNodeObjects_shouldUpdateNodeObjects_andSetCodeAndMessageFromLastSuccessfulResponseInSvcLogicContext()
+ throws SvcLogicException {
+ // GIVEN
+ ChefResponse firstNodeResponse = ChefResponse.create(HttpStatus.SC_OK, "firstMessage");
+ ChefResponse secondNodeResponse = ChefResponse.create(HttpStatus.SC_OK, "secondMessage");
+ int expectedHttpStatus = HttpStatus.SC_OK;
+ String expectedMessage = "secondMessage";
+
+ assertNodeObjectsAreUpdatedFor(firstNodeResponse, secondNodeResponse, expectedHttpStatus, expectedMessage);
+ }
+
+ @Test
+ public void vnfcNodeObjects_shouldStopProcessingNodeObjectUpdates_whenFirstReturnedResponseIsOtherThan_200()
+ throws SvcLogicException {
+ ChefResponse firstNodeResponse = ChefResponse.create(HttpStatus.SC_ACCEPTED, "firstMessage");
+ ChefResponse secondNodeResponse = ChefResponse.create(HttpStatus.SC_OK, "secondMessage");
+ int expectedHttpStatus = HttpStatus.SC_ACCEPTED;
+ String expectedMessage = "firstMessage";
+
+ assertNodeObjectsAreUpdatedFor(firstNodeResponse, secondNodeResponse, expectedHttpStatus, expectedMessage);
+ }
+
+ public void assertNodeObjectsAreUpdatedFor(ChefResponse firstNodeResponse, ChefResponse secondNodeResponse,
+ int expectedHttpStatus, String expectedMessage) throws SvcLogicException {
+ // GIVEN
+ Map<String, String> params = givenInputParams(
+ immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]"),
+ immutableEntry("Node", "{name:nodeName}"));
+
+ given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
+ given(chefApiClientFactory.create(CHEF_END_POINT, ORGANIZATIONS, USERNAME,
+ CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient);
+ given(chefApiClient.put("/nodes/" + "test1.vnf_b.onap.com", "{\"name\":\"test1.vnf_b.onap.com\"}"))
+ .willReturn(firstNodeResponse);
+ given(chefApiClient
+ .put("/nodes/" + "test2.vnf_b.onap.com", "{\"name\":\"test2.vnf_b.onap.com\"}"))
+ .willReturn(secondNodeResponse);
+
+ // WHEN
+ chefAdapterFactory.create().vnfcNodeobjects(params, svcLogicContext);
+
+ // THEN
+ assertThat(svcLogicContext.getStatus()).isEqualTo(SUCCESS_STATUS);
+ assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
+ .isEqualTo(Integer.toString(expectedHttpStatus));
+ assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedMessage);
+ }
+
+ @Test
+ public void vnfcNodeObjects_shouldThrowSvcLogicException_whenNodeListParamIsEmpty() {
+ Map<String, String> params = givenInputParams(
+ immutableEntry("NodeList", ""),
+ immutableEntry("Node", "{name:nodeName}"));
+ checkMissingParamsAreValidated(params);
+ }
+
+ @Test
+ public void vnfcNodeObjects_shouldThrowSvcLogicException_whenNodeParamIsEmpty() {
+ Map<String, String> params = givenInputParams(
+ immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]"),
+ immutableEntry("Node", ""));
+ checkMissingParamsAreValidated(params);
+ }
+
+ public void checkMissingParamsAreValidated(Map<String, String> params) {
+ // GIVEN
+ String expectedErrorMsg = "Missing Mandatory param(s) Node , NodeList ";
+
+ // WHEN // THEN
+ assertThatExceptionOfType(SvcLogicException.class)
+ .isThrownBy(() -> chefAdapterFactory.create().vnfcNodeobjects(params, svcLogicContext))
+ .withMessage(CHEF_ADAPTER_ERROR_PREFIX + "Error posting request: " + expectedErrorMsg);
+
+ assertThat(svcLogicContext.getStatus()).isEqualTo(FAILURE_STATUS);
+ assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
+ .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
+ assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY))
+ .isEqualTo("Error posting request: " + expectedErrorMsg);
+ }
+
+ @Test
+ public void vnfcNodeObjects_shouldNotUpdateNodes_andHandleJSONException_whenJSONParamsAreMalformed() {
+ // GIVEN
+ Map<String, String> params = givenInputParams(
+ immutableEntry("NodeList", "[\"test1.vnf_b.onap.com\", \"test2.vnf_b.onap.com\"]"),
+ immutableEntry("Node", "MALFORMED_JSON"));
+ String expectedErrorMessage = "Error posting request due to invalid JSON block: ";
+ given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
+
+ // WHEN // THEN
+ assertThatExceptionOfType(SvcLogicException.class)
+ .isThrownBy(() -> chefAdapterFactory.create().vnfcNodeobjects(params, svcLogicContext))
+ .withMessageStartingWith(CHEF_ADAPTER_ERROR_PREFIX + expectedErrorMessage);
+
+ assertThat(svcLogicContext.getStatus()).isEqualTo(FAILURE_STATUS);
+ assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
+ .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
+ assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY))
+ .startsWith(expectedErrorMessage);
+ }
+