e712e4d232653f4d1070786a5f9bb1f3061bd884
[portal.git] / ecomp-portal-BE-common / src / test / java / org / openecomp / portalapp / portal / controller / MicroserviceProxyControllerTest.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.assertTrue;
41
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44
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;
58
59 import com.fasterxml.jackson.databind.ObjectMapper;
60
61
62
63 public class MicroserviceProxyControllerTest extends MockitoTestSuite {
64
65         @Mock
66         MicroserviceProxyService microserviceProxyService = new MicroserviceProxyServiceImpl();
67
68         @InjectMocks
69         MicroserviceProxyController microserviceProxyController = new MicroserviceProxyController();
70
71         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
72
73         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
74         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
75         NullPointerException nullPointerException = new NullPointerException();
76
77         @Mock
78         EPUserUtils ePUserUtils = new EPUserUtils();
79         @Mock
80         ObjectMapper objectMapper = new ObjectMapper();
81         MockEPUser mockUser = new MockEPUser();
82
83         @Test
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\"}"));
90         }
91
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);
99         }
100
101         @Test
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\":\"\"}"));
110         }
111
112         @Test
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\"}"));
121         }
122
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);
130         }
131
132         @Test
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\":\"\"}"));
142         }
143 }