source: ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestServiceTestCase.java @ 6963

Last change on this file since 6963 was 6963, checked in by Twan Goosen, 8 years ago

merged 2.0 branch into trunk. Trunk now 2.1-SNAPSHOT

File size: 5.9 KB
Line 
1package clarin.cmdi.componentregistry.rest;
2
3import java.util.List;
4
5import javax.ws.rs.core.HttpHeaders;
6
7import clarin.cmdi.componentregistry.model.Comment;
8import clarin.cmdi.componentregistry.model.ComponentDescription;
9import clarin.cmdi.componentregistry.model.Group;
10import clarin.cmdi.componentregistry.model.ProfileDescription;
11import clarin.cmdi.componentregistry.model.RegistryUser;
12import clarin.cmdi.componentregistry.persistence.jpa.UserDao;
13
14import com.sun.jersey.api.client.GenericType;
15import com.sun.jersey.api.client.WebResource;
16import com.sun.jersey.api.client.WebResource.Builder;
17import com.sun.jersey.api.core.ClassNamesResourceConfig;
18import com.sun.jersey.core.util.Base64;
19import com.sun.jersey.multipart.impl.FormDataMultiPartDispatchProvider;
20import com.sun.jersey.spi.container.servlet.WebComponent;
21import com.sun.jersey.spi.spring.container.servlet.SpringServlet;
22import com.sun.jersey.test.framework.AppDescriptor;
23import com.sun.jersey.test.framework.JerseyTest;
24import com.sun.jersey.test.framework.WebAppDescriptor;
25import com.sun.jersey.test.framework.spi.container.TestContainerFactory;
26import java.security.Principal;
27
28import org.junit.runner.RunWith;
29import org.springframework.beans.factory.annotation.Autowired;
30import org.springframework.test.context.ContextConfiguration;
31import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
32import org.springframework.web.context.ContextLoaderListener;
33import org.springframework.web.context.request.RequestContextListener;
34
35/**
36 * Base test that starts a servlet container with the component registry
37 *
38 * @author george.georgovassilis@mpi.nl
39 *
40 */
41@RunWith(SpringJUnit4ClassRunner.class)
42@ContextConfiguration(locations = {
43    "classpath:spring-config/applicationContext.xml",
44    "classpath:spring-config/test-applicationContext-fragment.xml"})
45//Important: these tests can not be configured with @Transactional because it spawns two (mutually deadlocking) transactions: the test itself and jersey services
46public abstract class ComponentRegistryRestServiceTestCase extends JerseyTest {
47    // CommandLine test e.g.: curl -i -H "Accept:application/json" -X GET
48    // http://localhost:8080/ComponentRegistry/rest/registry/profiles
49
50    protected final static GenericType<List<ProfileDescription>> PROFILE_LIST_GENERICTYPE = new GenericType<List<ProfileDescription>>() {
51    };
52    protected final static GenericType<List<ComponentDescription>> COMPONENT_LIST_GENERICTYPE = new GenericType<List<ComponentDescription>>() {
53    };
54    protected final static GenericType<List<Comment>> COMMENT_LIST_GENERICTYPE = new GenericType<List<Comment>>() {
55    };
56    protected final static GenericType<List<Group>> GROUP_LIST_GENERICTYPE = new GenericType<List<Group>>() {
57    };
58    protected final static GenericType<List<String>> STRING_LIST_GENERICTYPE = new GenericType<List<String>>() {
59    };
60
61    private static SingletonTestContainerFactory _testContainerFactory;
62
63    @Override
64    public void setUp() throws Exception {
65        if (!_testContainerFactory.isTestContainerRunning()) {
66            _testContainerFactory.startTestContainer();
67        }
68    }
69
70    @Override
71    public void tearDown() throws Exception {
72    }
73
74    @Override
75    protected TestContainerFactory getTestContainerFactory() {
76        if (_testContainerFactory == null) {
77            _testContainerFactory = new SingletonTestContainerFactory(
78                    super.getTestContainerFactory());
79        }
80        ;
81        return _testContainerFactory;
82    }
83
84    @Autowired
85    private UserDao userDao;
86
87    protected String getApplicationContextFile() {
88        // sorry for the duplication, but JerseyTest is not aware of
89        // @ContextConfiguration
90        return "classpath:spring-config/applicationContext.xml, classpath:spring-config/test-applicationContext-fragment.xml";
91    }
92
93    @Override
94    protected AppDescriptor configure() {
95        WebAppDescriptor.Builder builder = new WebAppDescriptor.Builder()
96                .contextParam("contextConfigLocation",
97                        getApplicationContextFile())
98                .contextParam("eu.clarin.cmdi.componentregistry.serviceUrlBase", "localhost") // deliberately inaccurate,
99                .contextParam("eu.clarin.cmdi.componentregistry.serviceUrlPath", "test")      // for use of user/front end only
100                .servletClass(SpringServlet.class)
101                .initParam(WebComponent.RESOURCE_CONFIG_CLASS,
102                        ClassNamesResourceConfig.class.getName())
103                .initParam(
104                        ClassNamesResourceConfig.PROPERTY_CLASSNAMES,
105                        FormDataMultiPartDispatchProvider.class.getName() + ";"
106                        + ComponentRegistryRestService.class.getName())
107                .addFilter(DummySecurityFilter.class, "DummySecurityFilter")
108                .requestListenerClass(RequestContextListener.class)
109                .contextListenerClass(ContextLoaderListener.class);
110        return builder.build();
111    }
112
113    protected WebResource getResource() {
114        return resource();
115    }
116
117    protected Builder getAuthenticatedResource(String path) {
118        return getAuthenticatedResource(getResource().path(path));
119    }
120
121    protected Builder getAuthenticatedResource(WebResource resource) {
122        return getAuthenticatedResource(DummyPrincipal.DUMMY_PRINCIPAL, resource);
123    }
124
125    protected Builder getAuthenticatedResource(Principal principal, WebResource resource) {
126        return resource.header(HttpHeaders.AUTHORIZATION,
127                "Basic "
128                + new String(Base64
129                        .encode(principal
130                                .getName() + ":dummy")));
131    }
132
133    protected void createUserRecord() {
134        RegistryUser user = new RegistryUser();
135        user.setName("Database test user");
136        user.setPrincipalName(DummyPrincipal.DUMMY_PRINCIPAL.getName());
137        userDao.save(user);
138    }
139
140    protected UserDao getUserDao() {
141        return userDao;
142    }
143
144}
Note: See TracBrowser for help on using the repository browser.