2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 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============================================
38 package org.onap.portalapp.portal.controller;
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertTrue;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
46 import org.junit.Test;
47 import org.mockito.InjectMocks;
48 import org.mockito.Mock;
49 import org.mockito.Mockito;
50 import org.onap.portalapp.portal.controller.MicroserviceProxyController;
51 import org.onap.portalapp.portal.core.MockEPUser;
52 import org.onap.portalapp.portal.domain.EPUser;
53 import org.onap.portalapp.portal.framework.MockitoTestSuite;
54 import org.onap.portalapp.portal.service.MicroserviceProxyService;
55 import org.onap.portalapp.portal.service.MicroserviceProxyServiceImpl;
56 import org.onap.portalapp.util.EPUserUtils;
57 import org.springframework.http.HttpStatus;
58 import org.springframework.web.client.HttpClientErrorException;
60 import com.fasterxml.jackson.databind.ObjectMapper;
64 public class MicroserviceProxyControllerTest extends MockitoTestSuite {
67 MicroserviceProxyService microserviceProxyService = new MicroserviceProxyServiceImpl();
70 MicroserviceProxyController microserviceProxyController = new MicroserviceProxyController();
72 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
74 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
75 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
76 NullPointerException nullPointerException = new NullPointerException();
79 EPUserUtils ePUserUtils = new EPUserUtils();
81 ObjectMapper objectMapper = new ObjectMapper();
82 MockEPUser mockUser = new MockEPUser();
85 public void getMicroserviceProxyTest() throws Exception {
86 EPUser user = mockUser.mockEPUser();
87 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
88 Mockito.when(microserviceProxyService.proxyToDestination(1, user, mockedRequest)).thenReturn("Success");
89 String acutualString = microserviceProxyController.getMicroserviceProxy(mockedRequest, getMockedResponse(), 1);
90 assertTrue(acutualString.equals("{\"error\":\"Success\"}"));
93 @Test(expected = NullPointerException.class)
94 public void getMicroserviceProxyNullPoniterExceptionTest() throws Exception {
95 EPUser user = mockUser.mockEPUser();
96 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
97 Mockito.when(microserviceProxyService.proxyToDestination(1, user, mockedRequest))
98 .thenThrow(nullPointerException);
99 microserviceProxyController.getMicroserviceProxy(mockedRequest, getMockedResponse(), 1);
103 public void getMicroserviceProxyExceptionTest() throws Exception {
104 HttpClientErrorException httpClientErrorException = new HttpClientErrorException(HttpStatus.OK, "Success");
105 EPUser user = mockUser.mockEPUser();
106 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
107 Mockito.when(microserviceProxyService.proxyToDestination(1, user, mockedRequest))
108 .thenThrow(httpClientErrorException);
109 String acutualString = microserviceProxyController.getMicroserviceProxy(mockedRequest, getMockedResponse(), 1);
110 assertTrue(acutualString.equals("{\"error\":\"\"}"));
114 public void getMicroserviceProxyByWidgetIdTest() throws Exception {
115 EPUser user = mockUser.mockEPUser();
116 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
117 Mockito.when(microserviceProxyService.proxyToDestinationByWidgetId(1, user, mockedRequest))
118 .thenReturn("Success");
119 String acutualString = microserviceProxyController.getMicroserviceProxyByWidgetId(mockedRequest,
120 getMockedResponse(), 1);
121 assertTrue(acutualString.equals("{\"error\":\"Success\"}"));
124 @Test(expected = NullPointerException.class)
125 public void getMicroserviceProxyByWidgetIdNullPointerExceptionTest() throws Exception {
126 EPUser user = mockUser.mockEPUser();
127 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
128 Mockito.when(microserviceProxyService.proxyToDestinationByWidgetId(1, user, mockedRequest))
129 .thenThrow(nullPointerException);
130 microserviceProxyController.getMicroserviceProxyByWidgetId(mockedRequest, getMockedResponse(), 1);
134 public void getMicroserviceProxyByWidgetIdExceptionTest() throws Exception {
135 HttpClientErrorException httpClientErrorException = new HttpClientErrorException(HttpStatus.OK, "Success");
136 EPUser user = mockUser.mockEPUser();
137 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
138 Mockito.when(microserviceProxyService.proxyToDestinationByWidgetId(1, user, mockedRequest))
139 .thenThrow(httpClientErrorException);
140 String acutualString = microserviceProxyController.getMicroserviceProxyByWidgetId(mockedRequest,
141 getMockedResponse(), 1);
142 assertTrue(acutualString.equals("{\"error\":\"\"}"));