Null check for ClientResponse in PolicyUril.java
[portal.git] / ecomp-portal-BE-common / src / test / java / org / openecomp / portalapp / portal / controller / MicroserviceControllerTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the “License”);
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.portal.controller;
39
40 import static org.junit.Assert.assertEquals;
41
42 import java.util.ArrayList;
43 import java.util.List;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.mockito.MockitoAnnotations;
55 import org.openecomp.portalapp.portal.domain.MicroserviceData;
56 import org.openecomp.portalapp.portal.domain.WidgetCatalog;
57 import org.openecomp.portalapp.portal.domain.WidgetServiceHeaders;
58 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
59 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
60 import org.openecomp.portalapp.portal.framework.MockitoTestSuite;
61 import org.openecomp.portalapp.portal.service.ConsulHealthService;
62 import org.openecomp.portalapp.portal.service.ConsulHealthServiceImpl;
63 import org.openecomp.portalapp.portal.service.MicroserviceService;
64 import org.openecomp.portalapp.portal.service.MicroserviceServiceImpl;
65 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
66 import org.openecomp.portalsdk.core.util.SystemProperties;
67 import org.powermock.api.mockito.PowerMockito;
68 import org.powermock.core.classloader.annotations.PrepareForTest;
69 import org.powermock.modules.junit4.PowerMockRunner;
70 import org.springframework.core.ParameterizedTypeReference;
71 import org.springframework.http.HttpEntity;
72 import org.springframework.http.HttpMethod;
73 import org.springframework.http.HttpStatus;
74 import org.springframework.http.ResponseEntity;
75 import org.springframework.web.client.RestTemplate;
76
77 @RunWith(PowerMockRunner.class)
78 @PrepareForTest({WidgetServiceHeaders.class, EcompPortalUtils.class})
79 public class MicroserviceControllerTest extends MockitoTestSuite{
80
81         @InjectMocks
82         MicroserviceController microserviceController = new MicroserviceController();
83
84         @Mock
85         ConsulHealthService consulHealthService = new ConsulHealthServiceImpl();
86
87         @Mock
88         MicroserviceService microserviceService = new MicroserviceServiceImpl();
89
90         @Mock
91         RestTemplate template = new RestTemplate();
92
93         @Mock
94         MicroserviceData microserviceData = new MicroserviceData();
95
96         @SuppressWarnings("rawtypes")
97         @Mock
98         ResponseEntity<List<WidgetCatalog>> ans = new ResponseEntity<List<WidgetCatalog>>(HttpStatus.OK);
99
100         @Before
101         public void setup() {
102                 MockitoAnnotations.initMocks(this);
103         }
104
105         @Mock
106         EcompPortalUtils EcompPortalUtils = new EcompPortalUtils();
107
108         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
109
110         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
111         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
112         NullPointerException nullPointerException = new NullPointerException();
113
114         @Test
115         public void createMicroserviceIfServiceDataNullTest() throws Exception {
116                 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
117                 expectedportalRestResponse.setMessage("FAILURE");
118                 expectedportalRestResponse.setResponse("MicroserviceData cannot be null or empty");
119                 PortalRestStatusEnum portalRestStatusEnum = null;
120                 expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR);
121                 MicroserviceData microserviceData = null;
122                 PortalRestResponse<String> actualportalRestResponse = microserviceController.createMicroservice(mockedRequest,
123                                 mockedResponse, microserviceData);
124                 assertEquals(actualportalRestResponse, expectedportalRestResponse);
125         }
126
127         @Test
128         public void createMicroserviceTest() throws Exception {
129                 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
130                 expectedportalRestResponse.setMessage("SUCCESS");
131                 expectedportalRestResponse.setResponse("");
132                 PortalRestStatusEnum portalRestStatusEnum = null;
133                 expectedportalRestResponse.setStatus(portalRestStatusEnum.OK);
134                 PortalRestResponse<String> actualportalRestResponse = microserviceController.createMicroservice(mockedRequest,
135                                 mockedResponse, microserviceData);
136                 assertEquals(actualportalRestResponse, expectedportalRestResponse);
137         }
138
139         @Test
140         public void createMicroserviceExceptionTest() throws Exception {
141                 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
142                 expectedportalRestResponse.setMessage("FAILURE");
143                 expectedportalRestResponse.setResponse(null);
144                 PortalRestStatusEnum portalRestStatusEnum = null;
145                 expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR);
146                 Mockito.when(microserviceService.saveMicroservice(microserviceData)).thenReturn((long) 1);
147                 Mockito.when(microserviceData.getParameterList()).thenThrow(nullPointerException);
148                 PortalRestResponse<String> actualportalRestResponse = microserviceController.createMicroservice(mockedRequest,
149                                 mockedResponse, microserviceData);
150                 assertEquals(actualportalRestResponse, expectedportalRestResponse);
151         }
152
153         @Test
154         public void getMicroserviceTest() throws Exception {
155                 Mockito.when(microserviceService.getMicroserviceData()).thenReturn(null);
156                 List<MicroserviceData> list = microserviceController.getMicroservice(mockedRequest, mockedResponse);
157                 assertEquals(list, null);
158         }
159
160         @Test
161         public void updateMicroserviceIfServiceISNullTest() throws Exception {
162                 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
163                 expectedportalRestResponse.setMessage("FAILURE");
164                 expectedportalRestResponse.setResponse("MicroserviceData cannot be null or empty");
165                 PortalRestStatusEnum portalRestStatusEnum = null;
166                 expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR);
167                 MicroserviceData microserviceData = null;
168                 PortalRestResponse<String> actualportalRestResponse = microserviceController.updateMicroservice(mockedRequest,
169                                 mockedResponse, 1, microserviceData);
170                 assertEquals(actualportalRestResponse, expectedportalRestResponse);
171         }
172
173         @Test
174         public void updateMicroserviceTest() throws Exception {
175                 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
176                 expectedportalRestResponse.setMessage("SUCCESS");
177                 expectedportalRestResponse.setResponse("");
178                 PortalRestStatusEnum portalRestStatusEnum = null;
179                 expectedportalRestResponse.setStatus(portalRestStatusEnum.OK);
180                 PortalRestResponse<String> actualportalRestResponse = microserviceController.updateMicroservice(mockedRequest,
181                                 mockedResponse, 1, microserviceData);
182                 assertEquals(actualportalRestResponse, expectedportalRestResponse);
183         }
184
185         @Test
186         public void updateMicroserviceExceptionTest() throws Exception {
187                 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
188                 expectedportalRestResponse.setMessage("FAILURE");
189                 expectedportalRestResponse.setResponse(null);
190                 PortalRestStatusEnum portalRestStatusEnum = null;
191                 expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR);
192                 Mockito.when(microserviceController.updateMicroservice(mockedRequest, mockedResponse, 1, microserviceData))
193                                 .thenThrow(nullPointerException);
194                 PortalRestResponse<String> actualportalRestResponse = microserviceController.updateMicroservice(mockedRequest,
195                                 mockedResponse, 1, microserviceData);
196                 assertEquals(actualportalRestResponse, expectedportalRestResponse);
197         }
198
199         @Test
200         public void deleteMicroserviceExceptionTest() throws Exception {
201                 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
202                 expectedportalRestResponse.setMessage("FAILURE");
203                 PowerMockito.mockStatic(EcompPortalUtils.class);
204                 expectedportalRestResponse.setResponse(
205                                 "I/O error on GET request for \""  + EcompPortalUtils.widgetMsProtocol() + "://null/widget/microservices/widgetCatalog/service/1\":null; nested exception is java.net.UnknownHostException: null");
206                 PortalRestStatusEnum portalRestStatusEnum = null;
207                 expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR);
208                 PowerMockito.mockStatic(WidgetServiceHeaders.class);
209                 PortalRestResponse<String> actuaPportalRestResponse = microserviceController.deleteMicroservice(mockedRequest,
210                                 mockedResponse, 1);
211                 assertEquals(actuaPportalRestResponse.getStatus(), expectedportalRestResponse.getStatus());
212         }
213
214         @SuppressWarnings("unchecked")
215         @Test
216         public void deleteMicroserviceTest() throws Exception {
217                 String HTTPS = "https://";
218                 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
219                 expectedportalRestResponse.setMessage("SOME WIDGETS ASSOICATE WITH THIS SERVICE");
220                 expectedportalRestResponse.setResponse("'null' ,'null' ");
221                 PortalRestStatusEnum portalRestStatusEnum = null;
222                 expectedportalRestResponse.setStatus(portalRestStatusEnum.WARN);
223                 List<WidgetCatalog> List = new ArrayList<WidgetCatalog>();
224                 WidgetCatalog widgetCatalog = new WidgetCatalog();
225                 widgetCatalog.setId(1);
226                 WidgetCatalog widgetCatalog1 = new WidgetCatalog();
227                 widgetCatalog.setId(2);
228                 List.add(widgetCatalog);
229                 List.add(widgetCatalog1);
230                 PowerMockito.mockStatic(WidgetServiceHeaders.class);
231                 PowerMockito.mockStatic(EcompPortalUtils.class);
232                 String whatService = "widgets-service";
233                 Mockito.when(consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))).thenReturn("Test");
234                 Mockito.when(ans.getBody()).thenReturn(List);
235                 ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
236                 };
237                 Mockito.when(template.exchange(
238                                 EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))
239                                                 + "/widget/microservices/widgetCatalog/service/" + 1,
240                                 HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), typeRef)).thenReturn(ans);
241
242                 PortalRestResponse<String> actuaPportalRestResponse = microserviceController.deleteMicroservice(mockedRequest,
243                                 mockedResponse, 1);
244                 assertEquals(actuaPportalRestResponse, expectedportalRestResponse);
245         }
246
247         @SuppressWarnings("unchecked")
248         @Test
249         public void deleteMicroserviceWhenNoWidgetsAssociatedTest() throws Exception {
250                 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
251                 expectedportalRestResponse.setMessage("SUCCESS");
252                 expectedportalRestResponse.setResponse("");
253                 PortalRestStatusEnum portalRestStatusEnum = null;
254                 expectedportalRestResponse.setStatus(portalRestStatusEnum.OK);
255                 List<WidgetCatalog> List = new ArrayList<WidgetCatalog>();
256                 PowerMockito.mockStatic(WidgetServiceHeaders.class);
257                 PowerMockito.mockStatic(EcompPortalUtils.class);
258                 String whatService = "widgets-service";
259                 Mockito.when(consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))).thenReturn("Test");
260                 Mockito.when(ans.getBody()).thenReturn(List);
261                 ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
262                 };
263                 Mockito.when(template.exchange(
264                                 EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))
265                                                 + "/widget/microservices/widgetCatalog/service/" + 1,
266                                 HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), typeRef)).thenReturn(ans);
267                 PortalRestResponse<String> actuaPportalRestResponse = microserviceController.deleteMicroservice(mockedRequest,
268                                 mockedResponse, 1);
269                 assertEquals(actuaPportalRestResponse, expectedportalRestResponse);
270         }
271 }