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