ChefAdapterImpl Junits
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / test / java / org / onap / appc / adapter / chef / impl / ChefAdapterImplHttpMethodTest.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.mockito.BDDMockito.given;
24 import static org.mockito.BDDMockito.verifyZeroInteractions;
25
26 import com.google.common.collect.ImmutableMap;
27 import java.util.Map;
28 import java.util.function.Consumer;
29 import java.util.function.Supplier;
30 import org.apache.http.HttpStatus;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.runners.MockitoJUnitRunner;
37 import org.onap.appc.adapter.chef.ChefAdapter;
38 import org.onap.appc.adapter.chef.chefclient.ChefApiClientFactory;
39 import org.onap.appc.adapter.chef.chefclient.api.ChefApiClient;
40 import org.onap.appc.adapter.chef.chefclient.api.ChefResponse;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
43
44 @RunWith(MockitoJUnitRunner.class)
45 public class ChefAdapterImplHttpMethodTest {
46
47     private static final String CLIENT_PRIVATE_KEY_PATH = "/opt/onap/appc/chef/localhost/onap/testclient.pem";
48     private static final String RESULT_CODE_ATTR_KEY = "chefServerResult.code";
49     private static final String RESULT_MESSAGE_ATTR_KEY = "chefServerResult.message";
50     private static final String EXPECTED_RESPONSE_MSG = "chefResponseMessage";
51     private static final String ACTION_PARAM = "action";
52     private static final String REQUEST_BODY_DATA = "requestBodyData";
53     private static final Map<String, String> PARAMS = ImmutableMap
54         .of("username", "testclient",
55             "serverAddress", "localhost",
56             "organizations", "onap",
57             "chefAction", ACTION_PARAM,
58             "chefRequestBody", REQUEST_BODY_DATA);
59     @Mock
60     private PrivateKeyChecker privateKeyChecker;
61     @Mock
62     private ChefApiClientFactory chefApiClientFactory;
63     @Mock
64     private ChefApiClient chefApiClient;
65
66     @InjectMocks
67     private ChefAdapterFactory chefAdapterFactory;
68     private SvcLogicContext svcLogicContext;
69
70     @Before
71     public void setUp() {
72         svcLogicContext = new SvcLogicContext();
73     }
74
75     @Test
76     public void chefGet_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() {
77         assertSuccessfulChefHttpCallFor(() -> chefApiClient.get(ACTION_PARAM), this::chefGet);
78     }
79
80     @Test
81     public void chefGet_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() {
82         assertNoChefCallOccurFor(this::chefGet);
83     }
84
85     @Test
86     public void chefDelete_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() {
87         assertSuccessfulChefHttpCallFor(() -> chefApiClient.delete(ACTION_PARAM), this::chefDelete);
88     }
89
90     @Test
91     public void chefDelete_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() {
92         assertNoChefCallOccurFor(this::chefDelete);
93     }
94
95     @Test
96     public void chefPut_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() {
97         assertSuccessfulChefHttpCallFor(() -> chefApiClient.put(ACTION_PARAM, REQUEST_BODY_DATA), this::chefPut);
98     }
99
100     @Test
101     public void chefPut_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() {
102         assertNoChefCallOccurFor(this::chefPut);
103     }
104
105     @Test
106     public void chefPost_shouldExecuteHttpClient_andSetChefResponseInContext_whenPrivateKeyFileExists() {
107         assertSuccessfulChefHttpCallFor(() -> chefApiClient.post(ACTION_PARAM, REQUEST_BODY_DATA), this::chefPost);
108     }
109
110     @Test
111     public void chefPost_shouldNotExecuteHttpClient_andSetErrorResponseInContext_whenPrivateKeyFileDoesNotExist() {
112         assertNoChefCallOccurFor(this::chefPost);
113     }
114
115     public void assertSuccessfulChefHttpCallFor(Supplier<ChefResponse> responseSupplier,
116         Consumer<ChefAdapter> chefAdapterCall) {
117         // GIVEN
118         given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(true);
119         given(chefApiClientFactory.create("https://localhost/organizations/onap",
120             "onap",
121             "testclient",
122             CLIENT_PRIVATE_KEY_PATH)).willReturn(chefApiClient);
123         given(responseSupplier.get()).willReturn(ChefResponse.create(HttpStatus.SC_OK, EXPECTED_RESPONSE_MSG));
124
125         // WHEN
126         chefAdapterCall.accept(chefAdapterFactory.create());
127
128         // THEN
129         assertThat(svcLogicContext.getStatus()).isEqualTo("success");
130         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY)).isEqualTo(Integer.toString(HttpStatus.SC_OK));
131         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(EXPECTED_RESPONSE_MSG);
132     }
133
134     public void assertNoChefCallOccurFor(Consumer<ChefAdapter> chefAdapterCall) {
135         // GIVEN
136         given(privateKeyChecker.doesExist(CLIENT_PRIVATE_KEY_PATH)).willReturn(false);
137
138         // WHEN
139         chefAdapterCall.accept(chefAdapterFactory.create());
140
141         // THEN
142         verifyZeroInteractions(chefApiClient);
143         assertThat(svcLogicContext.getStatus()).isEqualTo("success");
144         assertThat(svcLogicContext.getAttribute(RESULT_CODE_ATTR_KEY))
145             .isEqualTo(Integer.toString(HttpStatus.SC_INTERNAL_SERVER_ERROR));
146         assertThat(svcLogicContext.getAttribute(RESULT_MESSAGE_ATTR_KEY)).isEqualTo(
147             "Cannot find the private key in the APPC file system, please load the private key to "
148                 + CLIENT_PRIVATE_KEY_PATH);
149     }
150
151     public void chefGet(ChefAdapter chefAdapter) {
152         try {
153             chefAdapter.chefGet(PARAMS, svcLogicContext);
154         } catch (SvcLogicException e) {
155             e.printStackTrace();
156         }
157     }
158
159     public void chefDelete(ChefAdapter chefAdapter) {
160         try {
161             chefAdapter.chefDelete(PARAMS, svcLogicContext);
162         } catch (SvcLogicException e) {
163             e.printStackTrace();
164         }
165     }
166
167     public void chefPost(ChefAdapter chefAdapter) {
168         try {
169             chefAdapter.chefPost(PARAMS, svcLogicContext);
170         } catch (SvcLogicException e) {
171             e.printStackTrace();
172         }
173     }
174
175     public void chefPut(ChefAdapter chefAdapter) {
176         try {
177             chefAdapter.chefPut(PARAMS, svcLogicContext);
178         } catch (SvcLogicException e) {
179             e.printStackTrace();
180         }
181     }
182 }