ChefAdapterImpl Junits
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / test / java / org / onap / appc / adapter / chef / impl / ChefAdapterImplJobPusherTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.appc.adapter.chef.impl;
21
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;
25
26 import com.google.common.collect.ImmutableMap;
27 import java.util.Map;
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;
40
41 @RunWith(MockitoJUnitRunner.class)
42 public class ChefAdapterImplJobPusherTest {
43
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}/";
48
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";
61
62     @Mock
63     private PrivateKeyChecker privateKeyChecker;
64     @Mock
65     private ChefApiClientFactory chefApiClientFactory;
66     @Mock
67     private ChefApiClient chefApiClient;
68
69     @InjectMocks
70     private ChefAdapterFactory chefAdapterFactory;
71     private SvcLogicContext svcLogicContext;
72
73     @Before
74     public void setUp() {
75         svcLogicContext = new SvcLogicContext();
76     }
77
78     @Test
79     public void pushJob_shouldSuccessfullyMakePostCall_andUpdateSvcLogicContext_whenReturnedStatusIsDifferentThan_201()
80         throws SvcLogicException {
81         assertSuccessfulPostCallForStatus(HttpStatus.SC_OK);
82         assertThat(svcLogicContext.getAttribute(JOB_ID)).isBlank();
83     }
84
85     @Test
86     public void pushJob_shouldSuccessfullyMakePostCall_andUpdateSvcLogicContext_withReturnedStatusIs_201()
87         throws SvcLogicException {
88         assertSuccessfulPostCallForStatus(HttpStatus.SC_CREATED);
89         assertThat(svcLogicContext.getAttribute(JOB_ID)).isEqualTo("666");
90     }
91
92     public void assertSuccessfulPostCallForStatus(int expectedHttpStatus) throws SvcLogicException {
93         // GIVEN
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));
98
99         // WHEN
100         chefAdapterFactory.create().pushJob(PARAMS, svcLogicContext);
101
102         // THEN
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);
107     }
108
109     @Test
110     public void pushJob_shouldHandleAllOccurringExceptions_duringMethodExecution() {
111         // GIVEN
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));
115
116         // WHEN // THEN
117         assertThatExceptionOfType(SvcLogicException.class)
118             .isThrownBy(() -> chefAdapterFactory.create().pushJob(PARAMS, svcLogicContext))
119             .withMessage("Chef Adapter error:" + EXPECTED_ERROR_MSG);
120
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();
126     }
127 }