ChefAdapterImpl junits
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / test / java / org / onap / appc / adapter / chef / impl / ChefAdapterImplVNFCOperationsTest.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
23 import static com.google.common.collect.Maps.immutableEntry;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
26 import static org.mockito.BDDMockito.given;
27
28 import com.google.common.collect.ImmutableMap;
29 import com.google.common.collect.ImmutableMap.Builder;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import org.apache.http.HttpStatus;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.InjectMocks;
37 import org.mockito.Mock;
38 import org.mockito.runners.MockitoJUnitRunner;
39 import org.onap.appc.adapter.chef.chefclient.ChefApiClientFactory;
40 import org.onap.appc.adapter.chef.chefclient.api.ChefApiClient;
41 import org.onap.appc.adapter.chef.chefclient.api.ChefResponse;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class ChefAdapterImplVNFCOperationsTest {
47
48     private static final String CLIENT_PRIVATE_KEY_PATH = "/opt/onap/appc/chef/localhost/onap/testclient.pem";
49     private static final String RESULT_CODE_ATTR_KEY = "chefServerResult.code";
50     private static final String RESULT_MESSAGE_ATTR_KEY = "chefServerResult.message";
51     private static final String USERNAME = "testclient";
52     private static final String SERVER_ADDRESS = "localhost";
53     private static final String ORGANIZATIONS = "onap";
54     private static final String ENV_PARAM_KEY = "Environment";
55     private static final String ENV_JSON_VALUE = "{name:envName}";
56     private static final String FAILURE_STATUS = "failure";
57     private static final String SUCCESS_STATUS = "success";
58     private static final String CHEF_ADAPTER_ERROR_PREFIX = "Chef Adapter error:";
59
60     @Mock
61     private PrivateKeyChecker privateKeyChecker;
62     @Mock
63     private ChefApiClientFactory chefApiClientFactory;
64     @Mock
65     private ChefApiClient chefApiClient;
66
67     @InjectMocks
68     private ChefAdapterFactory chefAdapterFactory;
69     private SvcLogicContext svcLogicContext;
70
71     @Before
72     public void setUp() {
73         svcLogicContext = new SvcLogicContext();
74     }
75
76     @Test
77     public void vnfcEnvironment_shouldSkipEnvironmentCreation_whenEnvParamIsEmpty() throws SvcLogicException {
78         // GIVEN
79         Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, ""));
80
81         // WHEN
82         chefAdapterFactory.create().vnfcEnvironment(params, svcLogicContext);
83
84         // THEN
85         assertThat(svcLogicContext.getStatus()).isEqualTo(SUCCESS_STATUS);
86         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
87             .isEqualTo(Integer.toString(HttpStatus.SC_OK));
88         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo("Skip Environment block ");
89     }
90
91     @Test
92     public void vnfcEnvironment_shouldCreateNewEnvironment_forEnvParam_whenRequestedEnvDoesNotExist()
93         throws SvcLogicException {
94         // GIVEN
95         String expectedErrorMessage = "New Environment Created";
96         Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, ENV_JSON_VALUE));
97         given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
98         given(chefApiClientFactory.create("https://localhost/organizations/onap", ORGANIZATIONS, USERNAME,
99             CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient);
100         given(chefApiClient.put("/environments/" + "envName", ENV_JSON_VALUE))
101             .willReturn(ChefResponse.create(HttpStatus.SC_NOT_FOUND, ""));
102         given(chefApiClient.post("/environments", ENV_JSON_VALUE))
103             .willReturn(ChefResponse.create(HttpStatus.SC_CREATED, expectedErrorMessage));
104
105         // WHEN
106         chefAdapterFactory.create().vnfcEnvironment(params, svcLogicContext);
107
108         // THEN
109         assertThat(svcLogicContext.getStatus()).isEqualTo(SUCCESS_STATUS);
110         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
111             .isEqualTo(Integer.toString(HttpStatus.SC_CREATED));
112         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(expectedErrorMessage);
113     }
114
115     @Test
116     public void vnfcEnvironment_shouldNotAttemptEnvCreation_andThrowException_whenPrivateKeyCheckFails() {
117         // GIVEN
118         String expectedErrorMsg = "Cannot find the private key in the APPC file system, please load the private key to ";
119         Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, ENV_JSON_VALUE));
120         given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(false);
121
122         // WHEN  // THEN
123         assertThatExceptionOfType(SvcLogicException.class)
124             .isThrownBy(() -> chefAdapterFactory.create().vnfcEnvironment(params, svcLogicContext))
125             .withMessage(CHEF_ADAPTER_ERROR_PREFIX + expectedErrorMsg + CLIENT_PRIVATE_KEY_PATH);
126
127         assertThat(svcLogicContext.getStatus()).isEqualTo(FAILURE_STATUS);
128         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
129             .isEqualTo(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR));
130         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY))
131             .isEqualTo(expectedErrorMsg + CLIENT_PRIVATE_KEY_PATH);
132     }
133
134     @Test
135     public void vnfcEnvironment_shouldNotAttemptEnvCreation_andHandleJSONException_whenJSONParamsAreMalformed() {
136         // GIVEN
137         String expectedErrorMessage = "Error posting request due to invalid JSON block: ";
138         Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, "MALFORMED_JSON"));
139         given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
140
141         // WHEN  // THEN
142         assertThatExceptionOfType(SvcLogicException.class)
143             .isThrownBy(() -> chefAdapterFactory.create().vnfcEnvironment(params, svcLogicContext))
144             .withMessageStartingWith(CHEF_ADAPTER_ERROR_PREFIX + expectedErrorMessage);
145
146         assertThat(svcLogicContext.getStatus()).isEqualTo(FAILURE_STATUS);
147         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
148             .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
149         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY))
150             .startsWith(expectedErrorMessage);
151     }
152
153     @Test
154     public void vnfcEnvironment_shouldNotAttemptEnvCreation_andHandleException_whenExceptionOccursDuringExecution() {
155         // GIVEN
156         String expectedErrorMessage = "Error posting request: ";
157         Map<String, String> params = givenInputParams(immutableEntry(ENV_PARAM_KEY, ENV_JSON_VALUE));
158         given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
159         given(chefApiClientFactory.create("https://localhost/organizations/onap", ORGANIZATIONS, USERNAME,
160             CLIENT_PRIVATE_KEY_PATH)).willThrow(new NullPointerException("Null value encountered"));
161
162         // WHEN  // THEN
163         assertThatExceptionOfType(SvcLogicException.class)
164             .isThrownBy(() -> chefAdapterFactory.create().vnfcEnvironment(params, svcLogicContext))
165             .withMessage(CHEF_ADAPTER_ERROR_PREFIX + expectedErrorMessage + "Null value encountered");
166
167         assertThat(svcLogicContext.getStatus()).isEqualTo(FAILURE_STATUS);
168         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
169             .isEqualTo(Integer.toString(HttpStatus.SC_UNAUTHORIZED));
170         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).startsWith(expectedErrorMessage);
171     }
172
173     private Map<String, String> givenInputParams(Entry<String, String>... entries) {
174         Builder<String, String> paramsBuilder = ImmutableMap.builder();
175         paramsBuilder.put("username", USERNAME)
176             .put("serverAddress", SERVER_ADDRESS)
177             .put("organizations", ORGANIZATIONS);
178
179         for (Entry<String, String> entry : entries) {
180             paramsBuilder.put(entry);
181         }
182         return paramsBuilder.build();
183     }
184 }