[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-BE-common-test / src / main / java / org / openecomp / portalapp / portal / test / controller / MicroserviceProxyControllerTest.java
1 package org.openecomp.portalapp.portal.test.controller;
2
3 import static org.junit.Assert.assertTrue;
4
5 import javax.servlet.http.HttpServletRequest;
6 import javax.servlet.http.HttpServletResponse;
7
8 import org.junit.Test;
9 import org.mockito.InjectMocks;
10 import org.mockito.Mock;
11 import org.mockito.Mockito;
12 import org.openecomp.portalapp.portal.controller.MicroserviceProxyController;
13 import org.openecomp.portalapp.portal.domain.EPUser;
14 import org.openecomp.portalapp.portal.service.MicroserviceProxyService;
15 import org.openecomp.portalapp.portal.service.MicroserviceProxyServiceImpl;
16 import org.openecomp.portalapp.portal.test.core.MockEPUser;
17 import org.openecomp.portalapp.test.framework.MockitoTestSuite;
18 import org.openecomp.portalapp.util.EPUserUtils;
19 import org.springframework.http.HttpStatus;
20 import org.springframework.web.client.HttpClientErrorException;
21
22 import com.fasterxml.jackson.databind.ObjectMapper;
23
24
25
26 public class MicroserviceProxyControllerTest extends MockitoTestSuite {
27
28         @Mock
29         MicroserviceProxyService microserviceProxyService = new MicroserviceProxyServiceImpl();
30
31         @InjectMocks
32         MicroserviceProxyController microserviceProxyController = new MicroserviceProxyController();
33
34         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
35
36         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
37         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
38         NullPointerException nullPointerException = new NullPointerException();
39
40         @Mock
41         EPUserUtils ePUserUtils = new EPUserUtils();
42         @Mock
43         ObjectMapper objectMapper = new ObjectMapper();
44         MockEPUser mockUser = new MockEPUser();
45
46         @Test
47         public void getMicroserviceProxyTest() throws Exception {
48                 EPUser user = mockUser.mockEPUser();
49                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
50                 Mockito.when(microserviceProxyService.proxyToDestination(1, user, mockedRequest)).thenReturn("Success");
51                 String acutualString = microserviceProxyController.getMicroserviceProxy(mockedRequest, getMockedResponse(), 1);
52                 assertTrue(acutualString.equals("Success"));
53         }
54
55         @Test(expected = NullPointerException.class)
56         public void getMicroserviceProxyNullPoniterExceptionTest() throws Exception {
57                 EPUser user = mockUser.mockEPUser();
58                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
59                 Mockito.when(microserviceProxyService.proxyToDestination(1, user, mockedRequest))
60                                 .thenThrow(nullPointerException);
61                 microserviceProxyController.getMicroserviceProxy(mockedRequest, getMockedResponse(), 1);
62         }
63
64         @Test
65         public void getMicroserviceProxyExceptionTest() throws Exception {
66                 HttpClientErrorException httpClientErrorException = new HttpClientErrorException(HttpStatus.OK, "Success");
67                 EPUser user = mockUser.mockEPUser();
68                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
69                 Mockito.when(microserviceProxyService.proxyToDestination(1, user, mockedRequest))
70                                 .thenThrow(httpClientErrorException);
71                 String acutualString = microserviceProxyController.getMicroserviceProxy(mockedRequest, getMockedResponse(), 1);
72                 assertTrue(acutualString.equals("{\"error\":\"\"}"));
73         }
74
75         @Test
76         public void getMicroserviceProxyByWidgetIdTest() throws Exception {
77                 EPUser user = mockUser.mockEPUser();
78                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
79                 Mockito.when(microserviceProxyService.proxyToDestinationByWidgetId(1, user, mockedRequest))
80                                 .thenReturn("Success");
81                 String acutualString = microserviceProxyController.getMicroserviceProxyByWidgetId(mockedRequest,
82                                 getMockedResponse(), 1);
83                 assertTrue(acutualString.equals("Success"));
84         }
85
86         @Test(expected = NullPointerException.class)
87         public void getMicroserviceProxyByWidgetIdNullPointerExceptionTest() throws Exception {
88                 EPUser user = mockUser.mockEPUser();
89                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
90                 Mockito.when(microserviceProxyService.proxyToDestinationByWidgetId(1, user, mockedRequest))
91                                 .thenThrow(nullPointerException);
92                 microserviceProxyController.getMicroserviceProxyByWidgetId(mockedRequest, getMockedResponse(), 1);
93         }
94
95         @Test
96         public void getMicroserviceProxyByWidgetIdExceptionTest() throws Exception {
97                 HttpClientErrorException httpClientErrorException = new HttpClientErrorException(HttpStatus.OK, "Success");
98                 EPUser user = mockUser.mockEPUser();
99                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(user);
100                 Mockito.when(microserviceProxyService.proxyToDestinationByWidgetId(1, user, mockedRequest))
101                                 .thenThrow(httpClientErrorException);
102                 String acutualString = microserviceProxyController.getMicroserviceProxyByWidgetId(mockedRequest,
103                                 getMockedResponse(), 1);
104                 assertTrue(acutualString.equals("{\"error\":\"\"}"));
105         }
106 }