ca94c8ceace3d4230cc3604787135a0e636cd002
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / controller / MicroserviceProxyControllerTest.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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.portal.controller;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertTrue;
42
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45
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;
59
60 import com.fasterxml.jackson.databind.ObjectMapper;
61
62
63
64 public class MicroserviceProxyControllerTest extends MockitoTestSuite {
65
66         @Mock
67         MicroserviceProxyService microserviceProxyService = new MicroserviceProxyServiceImpl();
68
69         @InjectMocks
70         MicroserviceProxyController microserviceProxyController = new MicroserviceProxyController();
71
72         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
73
74         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
75         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
76         NullPointerException nullPointerException = new NullPointerException();
77
78         @Mock
79         EPUserUtils ePUserUtils = new EPUserUtils();
80         @Mock
81         ObjectMapper objectMapper = new ObjectMapper();
82         MockEPUser mockUser = new MockEPUser();
83
84         @Test
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\"}"));
91         }
92
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);
100         }
101
102         @Test
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\":\"\"}"));
111         }
112
113         @Test
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\"}"));
122         }
123
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);
131         }
132
133         @Test
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\":\"\"}"));
143                 }
144 }