1 package org.openecomp.portalapp.portal.test.controller;
3 import static org.junit.Assert.assertTrue;
5 import java.util.ArrayList;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
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;
25 import com.orbitz.consul.ConsulException;
26 import com.orbitz.consul.model.health.ServiceHealth;
28 import io.searchbox.client.config.exception.NoServerConfiguredException;
30 public class ConsulClientControllerTest {
33 ConsulHealthService consulHealthService = new ConsulHealthServiceImpl();
36 ConsulClientController consulClientController = new ConsulClientController();
38 NoServerConfiguredException noServerConfiguredException = new NoServerConfiguredException(null);
40 String service = "Test";
44 MockitoAnnotations.initMocks(this);
47 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
49 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
50 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
51 NullPointerException nullPointerException = new NullPointerException();
52 ConsulException consulException = new ConsulException(nullPointerException);
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));
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()));
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()));
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;
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;
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));
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));
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));
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));