2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.openecomp.portalapp.portal.controller;
40 import static org.junit.Assert.assertTrue;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
45 import org.junit.Test;
46 import org.mockito.InjectMocks;
47 import org.mockito.Mock;
48 import org.mockito.Mockito;
49 import org.openecomp.portalapp.portal.framework.MockitoTestSuite;
50 import org.openecomp.portalapp.portal.controller.MicroserviceProxyController;
51 import org.openecomp.portalapp.portal.core.MockEPUser;
52 import org.openecomp.portalapp.portal.domain.EPUser;
53 import org.openecomp.portalapp.portal.service.MicroserviceProxyService;
54 import org.openecomp.portalapp.portal.service.MicroserviceProxyServiceImpl;
55 import org.openecomp.portalapp.util.EPUserUtils;
56 import org.springframework.http.HttpStatus;
57 import org.springframework.web.client.HttpClientErrorException;
59 import com.fasterxml.jackson.databind.ObjectMapper;
63 public class MicroserviceProxyControllerTest extends MockitoTestSuite {
66 MicroserviceProxyService microserviceProxyService = new MicroserviceProxyServiceImpl();
69 MicroserviceProxyController microserviceProxyController = new MicroserviceProxyController();
71 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
73 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
74 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
75 NullPointerException nullPointerException = new NullPointerException();
78 EPUserUtils ePUserUtils = new EPUserUtils();
80 ObjectMapper objectMapper = new ObjectMapper();
81 MockEPUser mockUser = new MockEPUser();
84 public void getMicroserviceProxyTest() throws Exception {
85 EPUser user = mockUser.mockEPUser();
86 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
87 Mockito.when(microserviceProxyService.proxyToDestination(1, user, mockedRequest)).thenReturn("Success");
88 String acutualString = microserviceProxyController.getMicroserviceProxy(mockedRequest, getMockedResponse(), 1);
89 assertTrue(acutualString.equals("{\"error\":\"Success\"}"));
92 @Test(expected = NullPointerException.class)
93 public void getMicroserviceProxyNullPoniterExceptionTest() throws Exception {
94 EPUser user = mockUser.mockEPUser();
95 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
96 Mockito.when(microserviceProxyService.proxyToDestination(1, user, mockedRequest))
97 .thenThrow(nullPointerException);
98 microserviceProxyController.getMicroserviceProxy(mockedRequest, getMockedResponse(), 1);
102 public void getMicroserviceProxyExceptionTest() throws Exception {
103 HttpClientErrorException httpClientErrorException = new HttpClientErrorException(HttpStatus.OK, "Success");
104 EPUser user = mockUser.mockEPUser();
105 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
106 Mockito.when(microserviceProxyService.proxyToDestination(1, user, mockedRequest))
107 .thenThrow(httpClientErrorException);
108 String acutualString = microserviceProxyController.getMicroserviceProxy(mockedRequest, getMockedResponse(), 1);
109 assertTrue(acutualString.equals("{\"error\":\"\"}"));
113 public void getMicroserviceProxyByWidgetIdTest() throws Exception {
114 EPUser user = mockUser.mockEPUser();
115 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
116 Mockito.when(microserviceProxyService.proxyToDestinationByWidgetId(1, user, mockedRequest))
117 .thenReturn("Success");
118 String acutualString = microserviceProxyController.getMicroserviceProxyByWidgetId(mockedRequest,
119 getMockedResponse(), 1);
120 assertTrue(acutualString.equals("{\"error\":\"Success\"}"));
123 @Test(expected = NullPointerException.class)
124 public void getMicroserviceProxyByWidgetIdNullPointerExceptionTest() throws Exception {
125 EPUser user = mockUser.mockEPUser();
126 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
127 Mockito.when(microserviceProxyService.proxyToDestinationByWidgetId(1, user, mockedRequest))
128 .thenThrow(nullPointerException);
129 microserviceProxyController.getMicroserviceProxyByWidgetId(mockedRequest, getMockedResponse(), 1);
133 public void getMicroserviceProxyByWidgetIdExceptionTest() throws Exception {
134 HttpClientErrorException httpClientErrorException = new HttpClientErrorException(HttpStatus.OK, "Success");
135 EPUser user = mockUser.mockEPUser();
136 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
137 Mockito.when(microserviceProxyService.proxyToDestinationByWidgetId(1, user, mockedRequest))
138 .thenThrow(httpClientErrorException);
139 String acutualString = microserviceProxyController.getMicroserviceProxyByWidgetId(mockedRequest,
140 getMockedResponse(), 1);
141 assertTrue(acutualString.equals("{\"error\":\"\"}"));