Replace ecomp references
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / controller / MicroserviceControllerTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 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  * 
37  */
38 package org.onap.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.onap.portalapp.portal.controller.MicroserviceController;
56 import org.onap.portalapp.portal.domain.MicroserviceData;
57 import org.onap.portalapp.portal.domain.WidgetCatalog;
58 import org.onap.portalapp.portal.domain.WidgetServiceHeaders;
59 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
60 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
61 import org.onap.portalapp.portal.framework.MockitoTestSuite;
62 import org.onap.portalapp.portal.service.ConsulHealthService;
63 import org.onap.portalapp.portal.service.ConsulHealthServiceImpl;
64 import org.onap.portalapp.portal.service.MicroserviceService;
65 import org.onap.portalapp.portal.service.MicroserviceServiceImpl;
66 import org.onap.portalapp.portal.utils.EcompPortalUtils;
67 import org.onap.portalsdk.core.util.SystemProperties;
68 import org.powermock.api.mockito.PowerMockito;
69 import org.powermock.core.classloader.annotations.PrepareForTest;
70 import org.powermock.modules.junit4.PowerMockRunner;
71 import org.springframework.core.ParameterizedTypeReference;
72 import org.springframework.http.HttpEntity;
73 import org.springframework.http.HttpMethod;
74 import org.springframework.http.HttpStatus;
75 import org.springframework.http.ResponseEntity;
76 import org.springframework.web.client.RestTemplate;
77
78 @RunWith(PowerMockRunner.class)
79 @PrepareForTest({WidgetServiceHeaders.class, EcompPortalUtils.class})
80 public class MicroserviceControllerTest extends MockitoTestSuite{
81
82         @InjectMocks
83         MicroserviceController microserviceController = new MicroserviceController();
84
85         @Mock
86         ConsulHealthService consulHealthService = new ConsulHealthServiceImpl();
87
88         @Mock
89         MicroserviceService microserviceService = new MicroserviceServiceImpl();
90
91         @Mock
92         RestTemplate template = new RestTemplate();
93
94         @Mock
95         MicroserviceData microserviceData = new MicroserviceData();
96
97         @SuppressWarnings("rawtypes")
98         @Mock
99         ResponseEntity<List<WidgetCatalog>> ans = new ResponseEntity<List<WidgetCatalog>>(HttpStatus.OK);
100
101         @Before
102         public void setup() {
103                 MockitoAnnotations.initMocks(this);
104         }
105
106         @Mock
107         EcompPortalUtils EcompPortalUtils = new EcompPortalUtils();
108
109         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
110
111         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
112         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
113         NullPointerException nullPointerException = new NullPointerException();
114
115         @Test
116         public void createMicroserviceIfServiceDataNullTest() throws Exception {
117                 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
118                 expectedportalRestResponse.setMessage("FAILURE");
119                 expectedportalRestResponse.setResponse("MicroserviceData cannot be null or empty");
120                 PortalRestStatusEnum portalRestStatusEnum = null;
121                 expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR);
122                 MicroserviceData microserviceData = null;
123                 PortalRestResponse<String> actualportalRestResponse = microserviceController.createMicroservice(mockedRequest,
124                                 mockedResponse, microserviceData);
125                 assertEquals(actualportalRestResponse, expectedportalRestResponse);
126         }
127
128         @Test
129         public void createMicroserviceTest() throws Exception {
130                 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
131                 expectedportalRestResponse.setMessage("SUCCESS");
132                 expectedportalRestResponse.setResponse("");
133                 PortalRestStatusEnum portalRestStatusEnum = null;
134                 expectedportalRestResponse.setStatus(portalRestStatusEnum.OK);
135                 PortalRestResponse<String> actualportalRestResponse = microserviceController.createMicroservice(mockedRequest,
136                                 mockedResponse, microserviceData);
137                 assertEquals(actualportalRestResponse, expectedportalRestResponse);
138         }
139
140         @Test
141         public void createMicroserviceExceptionTest() throws Exception {
142                 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
143                 expectedportalRestResponse.setMessage("FAILURE");
144                 expectedportalRestResponse.setResponse(null);
145                 PortalRestStatusEnum portalRestStatusEnum = null;
146                 expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR);
147                 Mockito.when(microserviceService.saveMicroservice(microserviceData)).thenReturn((long) 1);
148                 Mockito.when(microserviceData.getParameterList()).thenThrow(nullPointerException);
149                 PortalRestResponse<String> actualportalRestResponse = microserviceController.createMicroservice(mockedRequest,
150                                 mockedResponse, microserviceData);
151                 assertEquals(actualportalRestResponse, expectedportalRestResponse);
152         }
153
154         @Test
155         public void getMicroserviceTest() throws Exception {
156                 Mockito.when(microserviceService.getMicroserviceData()).thenReturn(null);
157                 List<MicroserviceData> list = microserviceController.getMicroservice(mockedRequest, mockedResponse);
158                 assertEquals(list, null);
159         }
160
161         @Test
162         public void updateMicroserviceIfServiceISNullTest() throws Exception {
163                 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
164                 expectedportalRestResponse.setMessage("FAILURE");
165                 expectedportalRestResponse.setResponse("MicroserviceData cannot be null or empty");
166                 PortalRestStatusEnum portalRestStatusEnum = null;
167                 expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR);
168                 MicroserviceData microserviceData = null;
169                 PortalRestResponse<String> actualportalRestResponse = microserviceController.updateMicroservice(mockedRequest,
170                                 mockedResponse, 1, microserviceData);
171                 assertEquals(actualportalRestResponse, expectedportalRestResponse);
172         }
173
174         @Test
175         public void updateMicroserviceTest() throws Exception {
176                 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
177                 expectedportalRestResponse.setMessage("SUCCESS");
178                 expectedportalRestResponse.setResponse("");
179                 PortalRestStatusEnum portalRestStatusEnum = null;
180                 expectedportalRestResponse.setStatus(portalRestStatusEnum.OK);
181                 PortalRestResponse<String> actualportalRestResponse = microserviceController.updateMicroservice(mockedRequest,
182                                 mockedResponse, 1, microserviceData);
183                 assertEquals(actualportalRestResponse, expectedportalRestResponse);
184         }
185
186         @Test
187         public void updateMicroserviceExceptionTest() throws Exception {
188                 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
189                 expectedportalRestResponse.setMessage("FAILURE");
190                 expectedportalRestResponse.setResponse(null);
191                 PortalRestStatusEnum portalRestStatusEnum = null;
192                 expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR);
193                 Mockito.when(microserviceController.updateMicroservice(mockedRequest, mockedResponse, 1, microserviceData))
194                                 .thenThrow(nullPointerException);
195                 PortalRestResponse<String> actualportalRestResponse = microserviceController.updateMicroservice(mockedRequest,
196                                 mockedResponse, 1, microserviceData);
197                 assertEquals(actualportalRestResponse, expectedportalRestResponse);
198         }
199
200         @Test
201         public void deleteMicroserviceExceptionTest() throws Exception {
202                 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
203                 expectedportalRestResponse.setMessage("FAILURE");
204                 PowerMockito.mockStatic(EcompPortalUtils.class);
205                 expectedportalRestResponse.setResponse(
206                                 "I/O error on GET request for \""  + EcompPortalUtils.widgetMsProtocol() + "://null/widget/microservices/widgetCatalog/service/1\":null; nested exception is java.net.UnknownHostException: null");
207                 PortalRestStatusEnum portalRestStatusEnum = null;
208                 expectedportalRestResponse.setStatus(portalRestStatusEnum.ERROR);
209                 PowerMockito.mockStatic(WidgetServiceHeaders.class);
210                 PortalRestResponse<String> actuaPportalRestResponse = microserviceController.deleteMicroservice(mockedRequest,
211                                 mockedResponse, 1);
212                 assertEquals(actuaPportalRestResponse.getStatus(), expectedportalRestResponse.getStatus());
213         }
214
215         @SuppressWarnings("unchecked")
216         @Test
217         public void deleteMicroserviceTest() throws Exception {
218                 String HTTPS = "https://";
219                 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
220                 expectedportalRestResponse.setMessage("SOME WIDGETS ASSOICATE WITH THIS SERVICE");
221                 expectedportalRestResponse.setResponse("'null' ,'null' ");
222                 PortalRestStatusEnum portalRestStatusEnum = null;
223                 expectedportalRestResponse.setStatus(portalRestStatusEnum.WARN);
224                 List<WidgetCatalog> List = new ArrayList<WidgetCatalog>();
225                 WidgetCatalog widgetCatalog = new WidgetCatalog();
226                 widgetCatalog.setId(1);
227                 WidgetCatalog widgetCatalog1 = new WidgetCatalog();
228                 widgetCatalog.setId(2);
229                 List.add(widgetCatalog);
230                 List.add(widgetCatalog1);
231                 PowerMockito.mockStatic(WidgetServiceHeaders.class);
232                 PowerMockito.mockStatic(EcompPortalUtils.class);
233                 String whatService = "widgets-service";
234                 Mockito.when(consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))).thenReturn("Test");
235                 Mockito.when(ans.getBody()).thenReturn(List);
236                 ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
237                 };
238                 Mockito.when(template.exchange(
239                                 EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))
240                                                 + "/widget/microservices/widgetCatalog/service/" + 1,
241                                 HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), typeRef)).thenReturn(ans);
242
243                 PortalRestResponse<String> actuaPportalRestResponse = microserviceController.deleteMicroservice(mockedRequest,
244                                 mockedResponse, 1);
245                 assertEquals(actuaPportalRestResponse, expectedportalRestResponse);
246         }
247
248         @SuppressWarnings("unchecked")
249         @Test
250         public void deleteMicroserviceWhenNoWidgetsAssociatedTest() throws Exception {
251                 PortalRestResponse<String> expectedportalRestResponse = new PortalRestResponse<String>();
252                 expectedportalRestResponse.setMessage("SUCCESS");
253                 expectedportalRestResponse.setResponse("");
254                 PortalRestStatusEnum portalRestStatusEnum = null;
255                 expectedportalRestResponse.setStatus(portalRestStatusEnum.OK);
256                 List<WidgetCatalog> List = new ArrayList<WidgetCatalog>();
257                 PowerMockito.mockStatic(WidgetServiceHeaders.class);
258                 PowerMockito.mockStatic(EcompPortalUtils.class);
259                 String whatService = "widgets-service";
260                 Mockito.when(consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))).thenReturn("Test");
261                 Mockito.when(ans.getBody()).thenReturn(List);
262                 ParameterizedTypeReference<List<WidgetCatalog>> typeRef = new ParameterizedTypeReference<List<WidgetCatalog>>() {
263                 };
264                 Mockito.when(template.exchange(
265                                 EcompPortalUtils.widgetMsProtocol() + "://" + consulHealthService.getServiceLocation(whatService, SystemProperties.getProperty("microservices.widget.local.port"))
266                                                 + "/widget/microservices/widgetCatalog/service/" + 1,
267                                 HttpMethod.GET, new HttpEntity(WidgetServiceHeaders.getInstance()), typeRef)).thenReturn(ans);
268                 PortalRestResponse<String> actuaPportalRestResponse = microserviceController.deleteMicroservice(mockedRequest,
269                                 mockedResponse, 1);
270                 assertEquals(actuaPportalRestResponse, expectedportalRestResponse);
271         }
272 }