Sync Integ to Master
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / servlets / ComponentInstanceServletTest.java
1 package org.openecomp.sdc.be.servlets;
2
3 import fj.data.Either;
4 import org.eclipse.jetty.http.HttpStatus;
5 import org.glassfish.hk2.utilities.binding.AbstractBinder;
6 import org.glassfish.jersey.server.ResourceConfig;
7 import org.glassfish.jersey.test.JerseyTest;
8 import org.glassfish.jersey.test.TestProperties;
9 import org.junit.BeforeClass;
10 import org.junit.Test;
11 import org.mockito.Mockito;
12 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
13 import org.openecomp.sdc.be.config.SpringConfig;
14 import org.openecomp.sdc.be.dao.api.ActionStatus;
15 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
16 import org.openecomp.sdc.be.impl.ComponentsUtils;
17 import org.openecomp.sdc.be.impl.ServletUtils;
18 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
19 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
20 import org.openecomp.sdc.common.api.Constants;
21 import org.openecomp.sdc.exception.ResponseFormat;
22 import org.springframework.context.ApplicationContext;
23 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
24 import org.springframework.web.context.WebApplicationContext;
25
26 import javax.servlet.ServletContext;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpSession;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31
32 import static org.junit.Assert.assertTrue;
33 import static org.mockito.ArgumentMatchers.eq;
34 import static org.mockito.Mockito.when;
35
36 /**
37  * The test suite designed for test functionality of ComponentInstanceServlet class
38  */
39 public class ComponentInstanceServletTest extends JerseyTest {
40
41     private final static String USER_ID = "jh0003";
42     private static HttpServletRequest request;
43     private static HttpSession session;
44     private static ServletContext servletContext;
45     private static WebAppContextWrapper webAppContextWrapper;
46     private static WebApplicationContext webApplicationContext;
47     private static ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
48     private static ComponentsUtils componentsUtils;
49     private static ServletUtils servletUtils;
50     private static ResponseFormat responseFormat;
51
52     @BeforeClass
53     public static void setup() {
54         createMocks();
55         stubMethods();
56     }
57
58     @Test
59     public void testGetRelationByIdSuccess(){
60
61         String containerComponentType = "resources";
62         String componentId = "componentId";
63         String relationId = "relationId";
64         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/" + relationId + "/relationId";
65         Either<RequirementCapabilityRelDef, ResponseFormat> successResponse = Either.left(new RequirementCapabilityRelDef());
66         when(componentInstanceBusinessLogic.getRelationById(eq(componentId), eq(relationId), eq(USER_ID), eq(ComponentTypeEnum.RESOURCE))).thenReturn(successResponse);
67         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
68         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
69         Response response = target()
70                 .path(path)
71                 .request(MediaType.APPLICATION_JSON)
72                 .header("USER_ID", USER_ID)
73                 .get( Response.class);
74
75         assertTrue(response.getStatus() == HttpStatus.OK_200);
76     }
77
78     @Test
79     public void testGetRelationByIdFailure(){
80
81         String containerComponentType = "unknown_type";
82         String componentId = "componentId";
83         String relationId = "relationId";
84         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/" + relationId + "/relationId";
85         when(responseFormat.getStatus()).thenReturn(HttpStatus.BAD_REQUEST_400);
86         when(componentsUtils.getResponseFormat(eq(ActionStatus.UNSUPPORTED_ERROR), eq(containerComponentType))).thenReturn(responseFormat);
87         Response response = target()
88                 .path(path)
89                 .request(MediaType.APPLICATION_JSON)
90                 .header("USER_ID", USER_ID)
91                 .get( Response.class);
92
93         assertTrue(response.getStatus() == HttpStatus.BAD_REQUEST_400);
94     }
95
96     @Override
97     protected ResourceConfig configure() {
98         forceSet(TestProperties.CONTAINER_PORT, "0");
99         ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
100         return new ResourceConfig(ComponentInstanceServlet.class)
101                 .register(new AbstractBinder() {
102                     @Override
103                     protected void configure() {
104                         bind(request).to(HttpServletRequest.class);
105                     }
106                 })
107                 .property("contextConfig", context);
108     }
109
110     private static void createMocks() {
111         request = Mockito.mock(HttpServletRequest.class);
112         session = Mockito.mock(HttpSession.class);
113         servletContext = Mockito.mock(ServletContext.class);
114         webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
115         webApplicationContext = Mockito.mock(WebApplicationContext.class);
116         componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class);
117         componentsUtils = Mockito.mock(ComponentsUtils.class);
118         servletUtils = Mockito.mock(ServletUtils.class);
119         responseFormat = Mockito.mock(ResponseFormat.class);
120     }
121
122     private static void stubMethods() {
123         when(request.getSession()).thenReturn(session);
124         when(session.getServletContext()).thenReturn(servletContext);
125         when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
126         when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
127         when(webApplicationContext.getBean(ComponentInstanceBusinessLogic.class)).thenReturn(componentInstanceBusinessLogic);
128         when(request.getHeader("USER_ID")).thenReturn(USER_ID);
129         when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils);
130         when(servletUtils.getComponentsUtils()).thenReturn(componentsUtils);
131     }
132 }