[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-BE-common-test / src / main / java / org / openecomp / portalapp / portal / test / controller / ConsulClientControllerTest.java
1 package org.openecomp.portalapp.portal.test.controller;
2
3 import static org.junit.Assert.assertTrue;
4
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.mockito.InjectMocks;
14 import org.mockito.Mock;
15 import org.mockito.Mockito;
16 import org.mockito.MockitoAnnotations;
17 import org.openecomp.portalapp.portal.controller.ConsulClientController;
18 import org.openecomp.portalapp.portal.domain.BEProperty;
19 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
20 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
21 import org.openecomp.portalapp.portal.service.ConsulHealthService;
22 import org.openecomp.portalapp.portal.service.ConsulHealthServiceImpl;
23 import org.openecomp.portalapp.test.framework.MockitoTestSuite;
24
25 import com.orbitz.consul.ConsulException;
26 import com.orbitz.consul.model.health.ServiceHealth;
27
28 import io.searchbox.client.config.exception.NoServerConfiguredException;
29
30 public class ConsulClientControllerTest {
31
32         @Mock
33         ConsulHealthService consulHealthService = new ConsulHealthServiceImpl();
34
35         @InjectMocks
36         ConsulClientController consulClientController = new ConsulClientController();
37
38         NoServerConfiguredException noServerConfiguredException = new NoServerConfiguredException(null);
39
40         String service = "Test";
41
42         @Before
43         public void setup() {
44                 MockitoAnnotations.initMocks(this);
45         }
46
47         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
48
49         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
50         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
51         NullPointerException nullPointerException = new NullPointerException();
52         ConsulException consulException = new ConsulException(nullPointerException);
53
54         @Test
55         public void getServiceLocationTest() {
56                 PortalRestResponse<BEProperty> ecpectedPortalRestResponse = new PortalRestResponse<BEProperty>();
57                 ecpectedPortalRestResponse.setMessage("Success!");
58                 ecpectedPortalRestResponse.setResponse(null);
59                 ecpectedPortalRestResponse.setStatus(PortalRestStatusEnum.OK);
60                 PortalRestResponse<String> actualPortalRestRespone = new PortalRestResponse<String>();
61                 actualPortalRestRespone = consulClientController.getServiceLocation(mockedRequest, mockedResponse, service);
62                 assertTrue(actualPortalRestRespone.equals(ecpectedPortalRestResponse));
63         }
64
65         @Test
66         public void getServiceLocationExceptionTest() {
67                 PortalRestResponse<BEProperty> ecpectedPortalRestResponse = new PortalRestResponse<BEProperty>();
68                 ecpectedPortalRestResponse.setMessage("Warning!");
69                 ecpectedPortalRestResponse.setStatus(PortalRestStatusEnum.WARN);
70                 PortalRestResponse<String> actualPortalRestRespone = new PortalRestResponse<String>();
71                 Mockito.when(consulHealthService.getServiceLocation(service, null)).thenThrow(noServerConfiguredException);
72                 actualPortalRestRespone = consulClientController.getServiceLocation(mockedRequest, mockedResponse, service);
73                 assertTrue(actualPortalRestRespone.getMessage().equals(ecpectedPortalRestResponse.getMessage()));
74                 assertTrue(actualPortalRestRespone.getStatus().equals(ecpectedPortalRestResponse.getStatus()));
75
76         }
77
78         @Test
79         public void getServiceLocationExceptionConsulExceptionTest() {
80                 PortalRestResponse<BEProperty> ecpectedPortalRestResponse = new PortalRestResponse<BEProperty>();
81                 ecpectedPortalRestResponse.setMessage("Error!");
82                 ecpectedPortalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
83                 PortalRestResponse<String> actualPortalRestRespone = new PortalRestResponse<String>();
84                 Mockito.when(consulHealthService.getServiceLocation(service, null)).thenThrow(consulException);
85                 actualPortalRestRespone = consulClientController.getServiceLocation(mockedRequest, mockedResponse, service);
86                 assertTrue(actualPortalRestRespone.getMessage().equals(ecpectedPortalRestResponse.getMessage()));
87                 assertTrue(actualPortalRestRespone.getStatus().equals(ecpectedPortalRestResponse.getStatus()));
88         }
89
90         public PortalRestResponse<List<ServiceHealth>> successResponse() {
91                 PortalRestResponse<List<ServiceHealth>> ecpectedPortalRestResponse = new PortalRestResponse<List<ServiceHealth>>();
92                 List<ServiceHealth> healths = new ArrayList<ServiceHealth>();
93                 ecpectedPortalRestResponse.setMessage("Success!");
94                 ecpectedPortalRestResponse.setResponse(healths);
95                 ecpectedPortalRestResponse.setStatus(PortalRestStatusEnum.OK);
96                 return ecpectedPortalRestResponse;
97         }
98
99         public PortalRestResponse<List<ServiceHealth>> errorResponse() {
100                 PortalRestResponse<List<ServiceHealth>> ecpectedPortalRestResponse = new PortalRestResponse<List<ServiceHealth>>();
101                 List<ServiceHealth> healths = new ArrayList<ServiceHealth>();
102                 ecpectedPortalRestResponse.setMessage("Error!");
103                 ecpectedPortalRestResponse.setResponse(healths);
104                 ecpectedPortalRestResponse.setStatus(PortalRestStatusEnum.ERROR);
105                 return ecpectedPortalRestResponse;
106         }
107
108         @Test
109         public void getAllHealthyNodesTest() {
110                 PortalRestResponse<List<ServiceHealth>> ecpectedPortalRestResponse = successResponse();
111                 PortalRestResponse<List<ServiceHealth>> actualPortalRestRespone = new PortalRestResponse<List<ServiceHealth>>();
112                 actualPortalRestRespone = consulClientController.getAllHealthyNodes(mockedRequest, mockedResponse, service);
113                 assertTrue(actualPortalRestRespone.equals(ecpectedPortalRestResponse));
114
115         }
116
117         @Test
118         public void getAllHealthyNodesExceptionTest() {
119                 PortalRestResponse<List<ServiceHealth>> ecpectedPortalRestResponse = errorResponse();
120                 PortalRestResponse<List<ServiceHealth>> actualPortalRestRespone = new PortalRestResponse<List<ServiceHealth>>();
121                 Mockito.when(consulHealthService.getAllHealthyNodes(service)).thenThrow(consulException);
122                 actualPortalRestRespone = consulClientController.getAllHealthyNodes(mockedRequest, mockedResponse, service);
123                 assertTrue(actualPortalRestRespone.equals(ecpectedPortalRestResponse));
124         }
125
126         @Test
127         public void getAllNodesTest() {
128                 PortalRestResponse<List<ServiceHealth>> ecpectedPortalRestResponse = successResponse();
129                 PortalRestResponse<List<ServiceHealth>> actualPortalRestRespone = new PortalRestResponse<List<ServiceHealth>>();
130                 actualPortalRestRespone = consulClientController.getAllNodes(mockedRequest, mockedResponse, service);
131                 assertTrue(actualPortalRestRespone.equals(ecpectedPortalRestResponse));
132         }
133
134         @Test
135         public void getAllNodesExceptionTest() {
136                 PortalRestResponse<List<ServiceHealth>> ecpectedPortalRestResponse = errorResponse();
137                 PortalRestResponse<List<ServiceHealth>> actualPortalRestRespone = new PortalRestResponse<List<ServiceHealth>>();
138                 Mockito.when(consulHealthService.getAllNodes(service)).thenThrow(consulException);
139                 actualPortalRestRespone = consulClientController.getAllNodes(mockedRequest, mockedResponse, service);
140                 assertTrue(actualPortalRestRespone.equals(ecpectedPortalRestResponse));
141         }
142 }