2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.servlets;
23 import static org.mockito.Mockito.mock;
25 import com.fasterxml.jackson.databind.DeserializationFeature;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.ws.rs.core.Feature;
30 import org.glassfish.grizzly.servlet.HttpSessionImpl;
31 import org.glassfish.grizzly.servlet.WebappContext;
32 import org.glassfish.hk2.utilities.binding.AbstractBinder;
33 import org.glassfish.jersey.client.ClientConfig;
34 import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJaxbJsonProvider;
35 import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJsonProvider;
36 import org.glassfish.jersey.logging.LoggingFeature;
37 import org.glassfish.jersey.media.multipart.MultiPartFeature;
38 import org.glassfish.jersey.server.ResourceConfig;
39 import org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature;
40 import org.glassfish.jersey.test.JerseyTest;
41 import org.glassfish.jersey.test.TestProperties;
42 import org.junit.jupiter.api.BeforeAll;
43 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
44 import org.openecomp.sdc.be.servlets.exception.ComponentExceptionMapper;
45 import org.openecomp.sdc.be.servlets.exception.DefaultExceptionMapper;
46 import org.openecomp.sdc.be.servlets.exception.StorageExceptionMapper;
47 import org.springframework.context.ApplicationContext;
48 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
49 import org.springframework.web.context.WebApplicationContext;
51 public abstract class JerseySpringBaseTest extends JerseyTest {
53 private static final Logger log = Logger.getLogger(JerseySpringBaseTest.class.getName());
54 protected static HttpServletRequest request;
55 protected static HttpSessionImpl session;
56 protected static WebappContext context;
57 protected static WebAppContextWrapper contextWrapper;
58 protected static WebApplicationContext applicationContext;
59 private final static JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider()
60 .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
61 private final static Feature loggingFeature = new LoggingFeature(log, Level.INFO,
62 LoggingFeature.Verbosity.PAYLOAD_ANY, null);
65 public static void initBaseClass() {
66 request = mock(HttpServletRequest.class);
67 session = mock(HttpSessionImpl.class);
68 context = mock(WebappContext.class);
69 contextWrapper = mock(WebAppContextWrapper.class);
70 applicationContext = mock(WebApplicationContext.class);
74 protected void configureClient(ClientConfig config) {
75 config.register(MultiPartFeature.class)
76 .register(loggingFeature)
77 .register(jacksonJsonProvider);
80 protected ResourceConfig configure() {
81 forceSet(TestProperties.CONTAINER_PORT, "0");
82 return configure(BaseTestConfig.class);
85 protected ResourceConfig configure(Class<?> springConfig) {
86 ApplicationContext context = new AnnotationConfigApplicationContext(springConfig);
87 forceSet(TestProperties.CONTAINER_PORT, "0");
88 return new ResourceConfig()
89 .register(new AbstractBinder() {
91 protected void configure() {
92 bind(request).to(HttpServletRequest.class);
95 .register(RolesAllowedDynamicFeature.class)
96 .register(DefaultExceptionMapper.class)
97 .register(ComponentExceptionMapper.class)
98 .register(StorageExceptionMapper.class)
99 .register(MultiPartFeature.class)
100 .register(jacksonJsonProvider)
101 .register(loggingFeature)
102 .property("jersey.config.server.provider.classnames", "org.openecomp.sdc.be.view.MixinModelWriter")
103 .property("contextConfig", context);