2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Nokia. All rights reserved.
6 * =============================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.appc.adapter.chef.impl;
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
24 import static org.mockito.BDDMockito.given;
26 import com.google.common.collect.ImmutableMap;
28 import org.apache.http.HttpStatus;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.runners.MockitoJUnitRunner;
35 import org.onap.appc.adapter.chef.chefclient.ChefApiClientFactory;
36 import org.onap.appc.adapter.chef.chefclient.api.ChefApiClient;
37 import org.onap.appc.adapter.chef.chefclient.api.ChefResponse;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
41 @RunWith(MockitoJUnitRunner.class)
42 public class ChefAdapterImplJobPusherTest {
44 private static final String CLIENT_PRIVATE_KEY_PATH = "/opt/onap/appc/chef/localhost/onap/testclient.pem";
45 private static final String RESULT_CODE_ATTR_KEY = "chefServerResult.code";
46 private static final String RESULT_MESSAGE_ATTR_KEY = "chefServerResult.message";
47 private static final String EXPECTED_RESPONSE_MSG = "jobs/{666}/";
49 private static final String USERNAME = "testclient";
50 private static final String SERVER_ADDRESS = "localhost";
51 private static final String ORGANIZATIONS = "onap";
52 private static final String ACTION_PARAM = "/pushy/jobs";
53 private static final String REQUEST_BODY_DATA = "requestBodyData";
54 private static final Map<String, String> PARAMS = ImmutableMap
55 .of("username", USERNAME,
56 "serverAddress", SERVER_ADDRESS,
57 "organizations", ORGANIZATIONS,
58 "chefAction", ACTION_PARAM,
59 "pushRequest", REQUEST_BODY_DATA);
60 private static final String JOB_ID = "jobID";
63 private PrivateKeyChecker privateKeyChecker;
65 private ChefApiClientFactory chefApiClientFactory;
67 private ChefApiClient chefApiClient;
70 private ChefAdapterFactory chefAdapterFactory;
71 private SvcLogicContext svcLogicContext;
75 svcLogicContext = new SvcLogicContext();
79 public void pushJob_shouldSuccessfullyMakePostCall_andUpdateSvcLogicContext_whenReturnedStatusIsDifferentThan_201()
80 throws SvcLogicException {
81 assertSuccessfulPostCallForStatus(HttpStatus.SC_OK);
82 assertThat(svcLogicContext.getAttribute(JOB_ID)).isBlank();
86 public void pushJob_shouldSuccessfullyMakePostCall_andUpdateSvcLogicContext_withReturnedStatusIs_201()
87 throws SvcLogicException {
88 assertSuccessfulPostCallForStatus(HttpStatus.SC_CREATED);
89 assertThat(svcLogicContext.getAttribute(JOB_ID)).isEqualTo("666");
92 public void assertSuccessfulPostCallForStatus(int expectedHttpStatus) throws SvcLogicException {
94 given(chefApiClientFactory.create("https://localhost/organizations/onap", ORGANIZATIONS, USERNAME,
95 CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient);
96 given(chefApiClient.post(ACTION_PARAM, REQUEST_BODY_DATA))
97 .willReturn(ChefResponse.create(expectedHttpStatus, EXPECTED_RESPONSE_MSG));
100 chefAdapterFactory.create().pushJob(PARAMS, svcLogicContext);
103 assertThat(svcLogicContext.getStatus()).isEqualTo("success");
104 assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
105 .isEqualTo(Integer.toString(expectedHttpStatus));
106 assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(EXPECTED_RESPONSE_MSG);
110 public void pushJob_shouldHandleAllOccurringExceptions_duringMethodExecution() {
112 String EXPECTED_ERROR_MSG = "Something went wrong";
113 given(chefApiClientFactory.create("https://localhost/organizations/onap", ORGANIZATIONS, USERNAME,
114 CLIENT_PRIVATE_KEY_PATH)).willThrow(new NullPointerException(EXPECTED_ERROR_MSG));
117 assertThatExceptionOfType(SvcLogicException.class)
118 .isThrownBy(() -> chefAdapterFactory.create().pushJob(PARAMS, svcLogicContext))
119 .withMessage("Chef Adapter error:" + EXPECTED_ERROR_MSG);
121 assertThat(svcLogicContext.getStatus()).isEqualTo("failure");
122 assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
123 .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
124 assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(EXPECTED_ERROR_MSG);
125 assertThat(svcLogicContext.getAttribute(JOB_ID)).isBlank();