Sync Integ to Master
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / servlets / GroupEndpointTest.java
1 //package org.openecomp.sdc.be.servlets;
2 //
3 //import com.fasterxml.jackson.databind.DeserializationFeature;
4 //import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJaxbJsonProvider;
5 //import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJsonProvider;
6 //import fj.data.Either;
7 //import org.eclipse.jetty.http.HttpStatus;
8 //import org.glassfish.hk2.utilities.binding.AbstractBinder;
9 //import org.glassfish.jersey.client.ClientConfig;
10 //import org.glassfish.jersey.server.ResourceConfig;
11 //import org.glassfish.jersey.test.JerseyTest;
12 //import org.glassfish.jersey.test.TestProperties;
13 //import org.junit.Before;
14 //import org.junit.Test;
15 //import org.junit.runner.RunWith;
16 //import org.mockito.Mock;
17 //import org.mockito.junit.MockitoJUnitRunner;
18 //import org.openecomp.sdc.be.DummyConfigurationManager;
19 //import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
20 //import org.openecomp.sdc.be.dao.api.ActionStatus;
21 //import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
22 //import org.openecomp.sdc.be.impl.ComponentsUtils;
23 //import org.openecomp.sdc.be.impl.ServletUtils;
24 //import org.openecomp.sdc.be.impl.WebAppContextWrapper;
25 //import org.openecomp.sdc.be.info.GroupDefinitionInfo;
26 //import org.openecomp.sdc.be.model.GroupDefinition;
27 //import org.openecomp.sdc.common.api.Constants;
28 //import org.openecomp.sdc.exception.ResponseFormat;
29 //import org.springframework.web.context.WebApplicationContext;
30 //
31 //import javax.servlet.ServletContext;
32 //import javax.servlet.http.HttpServletRequest;
33 //import javax.servlet.http.HttpSession;
34 //import javax.ws.rs.client.ClientBuilder;
35 //import javax.ws.rs.client.Invocation;
36 //import javax.ws.rs.core.Application;
37 //import javax.ws.rs.core.MediaType;
38 //import javax.ws.rs.core.Response;
39 //
40 //import static org.assertj.core.api.Assertions.assertThat;
41 //import static org.junit.Assert.assertEquals;
42 //import static org.mockito.ArgumentMatchers.*;
43 //import static org.mockito.Mockito.when;
44 //
45 //@RunWith(MockitoJUnitRunner.class)
46 //public class GroupEndpointTest extends JerseyTest {
47 //
48 //    public static final String USER_ID = "jh0003";
49 //    public static final String INVALID_USER_ID = "jh0001";
50 //    final static String RESOURCE_TYPE = "resources";
51 //    private static final String COMPONENT_ID = "1234";
52 //    private static final String VALID_GROUP_ID = "1";
53 //    private static final String INVALID_GROUP_ID = "2";
54 //    private static final String NEW_GROUP_NAME = "new group";
55 //    private static final String VALID_GROUP_TYPE = "networkConnection";
56 //
57 //    @Mock
58 //    private GroupBusinessLogic groupBusinessLogic;
59 //    @Mock
60 //    private ServletContext servletContext;
61 //    @Mock
62 //    private WebAppContextWrapper webAppContextWrapper;
63 //    @Mock
64 //    private WebApplicationContext webApplicationContext;
65 //    @Mock
66 //    private HttpServletRequest request;
67 //    @Mock
68 //    private HttpSession session;
69 //    @Mock
70 //    private ComponentsUtils componentUtils;
71 //    @Mock
72 //    private ServletUtils servletUtils;
73 //    @Mock
74 //    private ResponseFormat responseFormat;
75 //
76 //    @Override
77 //    protected Application configure() {
78 //        ResourceConfig resourceConfig = new ResourceConfig()
79 //                .register(GroupServlet.class)
80 //                //.register(mapper)
81 //                ;
82 //        forceSet(TestProperties.CONTAINER_PORT, "0");
83 //        resourceConfig.register(new AbstractBinder() {
84 //            @Override
85 //            protected void configure() {
86 //                bind(request).to(HttpServletRequest.class);
87 //            }
88 //        });
89 //        return resourceConfig;
90 //    }
91 //
92 //    @Before
93 //    public void before() {
94 //        when(request.getSession()).thenReturn(session);
95 //        when(session.getServletContext()).thenReturn(servletContext);
96 //        when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
97 //        when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
98 //        when(webApplicationContext.getBean(GroupBusinessLogic.class)).thenReturn(groupBusinessLogic);
99 ////        when(webApplicationContext.getBean(ComponentsUtils.class)).thenReturn(componentUtils);
100 //        when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils);
101 //        when(servletUtils.getComponentsUtils()).thenReturn(componentUtils);
102 ////        when(request.getHeader("USER_ID")).thenReturn(USER_ID);
103 //        final JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
104 //        setClient(ClientBuilder.newClient(new ClientConfig(jacksonJsonProvider)));
105 //        new DummyConfigurationManager();
106 ////        when(ConfigurationManager.getConfigurationManager().getConfiguration().getExcludedGroupTypesMapping()).thenReturn(buildExcludePolicyTypesMap());
107 //    }
108 //
109 //    //@Test
110 //    public void createGroup() {
111 //        GroupDefinition groupDefinition = new GroupDefinition();
112 //        groupDefinition.setName(NEW_GROUP_NAME);
113 //        groupDefinition.setType(VALID_GROUP_TYPE);
114 //        when(groupBusinessLogic.createGroup(eq(NEW_GROUP_NAME), eq(ComponentTypeEnum.RESOURCE), eq(COMPONENT_ID), eq(USER_ID)))
115 //                .thenReturn(groupDefinition);
116 //        //List<GroupDefinition> gdList = buildCreateGroupCall().post(Response.class);
117 //
118 //    }
119 //
120 //    //@Test
121 //    public void deleteGroup_withInvalidUser_shouldReturn_401() {
122 //        int unauthorized401 = HttpStatus.UNAUTHORIZED_401;
123 //        int unauthorized403 = HttpStatus.FORBIDDEN_403;
124 //        when(groupBusinessLogic.validateUserExists(eq(INVALID_USER_ID), anyString(), anyBoolean()))
125 //                .thenReturn(Either.right(new ResponseFormat(unauthorized401)));
126 //
127 //        Response response = buildDeleteGroupCall(INVALID_USER_ID, VALID_GROUP_ID).delete();
128 //        assertThat(response.getStatus()).isEqualTo(unauthorized401);
129 //    }
130 //
131 //    @Test
132 //    public void getGroupById_internalServerError() {
133 //        when(groupBusinessLogic.getGroupWithArtifactsById(eq(ComponentTypeEnum.RESOURCE), eq(COMPONENT_ID), eq(VALID_GROUP_ID), eq(USER_ID), eq(false) ))
134 //                .thenReturn(Either.right(new ResponseFormat(HttpStatus.INTERNAL_SERVER_ERROR_500)));
135 //        Response response = buildGetGroupCall(VALID_GROUP_ID).get();
136 //        assertEquals(response.getStatus(), 500);
137 //    }
138 //
139 //    @Test
140 //    public void getGroupById_Success() {
141 //        GroupDefinitionInfo groupDefinitionInfo = new GroupDefinitionInfo();
142 //        groupDefinitionInfo.setUniqueId(VALID_GROUP_ID);
143 //        when(groupBusinessLogic.getGroupWithArtifactsById(eq(ComponentTypeEnum.RESOURCE), eq(COMPONENT_ID), eq(VALID_GROUP_ID), eq(USER_ID), eq(false) ))
144 //                .thenReturn(Either.left(groupDefinitionInfo));
145 //        when(componentUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
146 //        when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
147 //        GroupDefinitionInfo gdi = buildGetGroupCall(VALID_GROUP_ID).get(GroupDefinitionInfo.class);
148 //        assertEquals(gdi.getUniqueId(), VALID_GROUP_ID);
149 //    }
150 //
151 //    @Test
152 //    public void getGroupById_Failure() {
153 //        when(groupBusinessLogic.getGroupWithArtifactsById(eq(ComponentTypeEnum.RESOURCE), eq(COMPONENT_ID), eq(INVALID_GROUP_ID), eq(USER_ID), eq(false) ))
154 //                .thenReturn(Either.right(new ResponseFormat(HttpStatus.NOT_FOUND_404)));
155 //        Response response = buildGetGroupCall(INVALID_GROUP_ID).get();
156 //        assertEquals(response.getStatus(), 404);
157 //    }
158 //
159 //    private Invocation.Builder buildGetGroupCall(String groupId) {
160 //        String path = "/v1/catalog/" + RESOURCE_TYPE + "/" + COMPONENT_ID + "/groups/" + groupId;
161 //        return target(path)
162 //                .request(MediaType.APPLICATION_JSON)
163 //                .header(Constants.USER_ID_HEADER, USER_ID);
164 //    }
165 //
166 //    private Invocation.Builder buildDeleteGroupCall(String userId, String groupId) {
167 //        String path = "/v1/catalog/" + RESOURCE_TYPE + "/" + COMPONENT_ID + "/groups/" + groupId;
168 //        return target(path)
169 //                .request(MediaType.APPLICATION_JSON)
170 //                .header(Constants.USER_ID_HEADER, userId);
171 //    }
172 //
173 //    private Invocation.Builder buildCreateGroupCall() {
174 //        String path = "/v1/catalog/" + RESOURCE_TYPE + "/" + COMPONENT_ID + "/groups";
175 //        return target(path)
176 //                .request(MediaType.APPLICATION_JSON)
177 //                .header(Constants.USER_ID_HEADER, USER_ID);
178 //    }
179 //}