re base code
[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.assertEquals;
33 import static org.junit.Assert.assertTrue;
34 import static org.mockito.ArgumentMatchers.eq;
35 import static org.mockito.Mockito.when;
36
37 /**
38  * The test suite designed for test functionality of ComponentInstanceServlet class
39  */
40 public class ComponentInstanceServletTest extends JerseyTest {
41
42     private final static String USER_ID = "jh0003";
43     private static HttpServletRequest request;
44     private static HttpSession session;
45     private static ServletContext servletContext;
46     private static WebAppContextWrapper webAppContextWrapper;
47     private static WebApplicationContext webApplicationContext;
48     private static ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
49     private static ComponentsUtils componentsUtils;
50     private static ServletUtils servletUtils;
51     private static ResponseFormat responseFormat;
52
53     @BeforeClass
54     public static void setup() {
55         createMocks();
56         stubMethods();
57     }
58
59     @Test
60     public void testGetRelationByIdSuccess(){
61
62         String containerComponentType = "resources";
63         String componentId = "componentId";
64         String relationId = "relationId";
65         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/" + relationId + "/relationId";
66         Either<RequirementCapabilityRelDef, ResponseFormat> successResponse = Either.left(new RequirementCapabilityRelDef());
67         when(componentInstanceBusinessLogic.getRelationById(eq(componentId), eq(relationId), eq(USER_ID), eq(ComponentTypeEnum.RESOURCE))).thenReturn(successResponse);
68         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
69         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
70         Response response = target()
71                 .path(path)
72                 .request(MediaType.APPLICATION_JSON)
73                 .header("USER_ID", USER_ID)
74                 .get( Response.class);
75
76         assertEquals(response.getStatus(), HttpStatus.OK_200);
77     }
78
79     @Test
80     public void testGetRelationByIdFailure(){
81
82         String containerComponentType = "unknown_type";
83         String componentId = "componentId";
84         String relationId = "relationId";
85         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/" + relationId + "/relationId";
86         when(responseFormat.getStatus()).thenReturn(HttpStatus.BAD_REQUEST_400);
87         when(componentsUtils.getResponseFormat(eq(ActionStatus.UNSUPPORTED_ERROR), eq(containerComponentType))).thenReturn(responseFormat);
88         Response response = target()
89                 .path(path)
90                 .request(MediaType.APPLICATION_JSON)
91                 .header("USER_ID", USER_ID)
92                 .get( Response.class);
93
94         assertEquals(response.getStatus(), HttpStatus.BAD_REQUEST_400);
95     }
96
97     @Override
98     protected ResourceConfig configure() {
99         forceSet(TestProperties.CONTAINER_PORT, "0");
100         ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
101         return new ResourceConfig(ComponentInstanceServlet.class)
102                 .register(new AbstractBinder() {
103                     @Override
104                     protected void configure() {
105                         bind(request).to(HttpServletRequest.class);
106                     }
107                 })
108                 .property("contextConfig", context);
109     }
110
111     private static void createMocks() {
112         request = Mockito.mock(HttpServletRequest.class);
113         session = Mockito.mock(HttpSession.class);
114         servletContext = Mockito.mock(ServletContext.class);
115         webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
116         webApplicationContext = Mockito.mock(WebApplicationContext.class);
117         componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class);
118         componentsUtils = Mockito.mock(ComponentsUtils.class);
119         servletUtils = Mockito.mock(ServletUtils.class);
120         responseFormat = Mockito.mock(ResponseFormat.class);
121     }
122
123     private static void stubMethods() {
124         when(request.getSession()).thenReturn(session);
125         when(session.getServletContext()).thenReturn(servletContext);
126         when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
127         when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
128         when(webApplicationContext.getBean(ComponentInstanceBusinessLogic.class)).thenReturn(componentInstanceBusinessLogic);
129         when(request.getHeader("USER_ID")).thenReturn(USER_ID);
130         when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils);
131         when(servletUtils.getComponentsUtils()).thenReturn(componentsUtils);
132     }
133 }