1 | package clarin.cmdi.componentregistry.rest; |
---|
2 | |
---|
3 | import java.util.List; |
---|
4 | |
---|
5 | import javax.ws.rs.core.HttpHeaders; |
---|
6 | |
---|
7 | import clarin.cmdi.componentregistry.model.Comment; |
---|
8 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
9 | import clarin.cmdi.componentregistry.model.Group; |
---|
10 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
11 | import clarin.cmdi.componentregistry.model.RegistryUser; |
---|
12 | import clarin.cmdi.componentregistry.persistence.jpa.UserDao; |
---|
13 | |
---|
14 | import com.sun.jersey.api.client.GenericType; |
---|
15 | import com.sun.jersey.api.client.WebResource; |
---|
16 | import com.sun.jersey.api.client.WebResource.Builder; |
---|
17 | import com.sun.jersey.api.core.ClassNamesResourceConfig; |
---|
18 | import com.sun.jersey.core.util.Base64; |
---|
19 | import com.sun.jersey.multipart.impl.FormDataMultiPartDispatchProvider; |
---|
20 | import com.sun.jersey.spi.container.servlet.WebComponent; |
---|
21 | import com.sun.jersey.spi.spring.container.servlet.SpringServlet; |
---|
22 | import com.sun.jersey.test.framework.AppDescriptor; |
---|
23 | import com.sun.jersey.test.framework.JerseyTest; |
---|
24 | import com.sun.jersey.test.framework.WebAppDescriptor; |
---|
25 | import com.sun.jersey.test.framework.spi.container.TestContainerFactory; |
---|
26 | import java.security.Principal; |
---|
27 | |
---|
28 | import org.junit.runner.RunWith; |
---|
29 | import org.springframework.beans.factory.annotation.Autowired; |
---|
30 | import org.springframework.test.context.ContextConfiguration; |
---|
31 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
---|
32 | import org.springframework.web.context.ContextLoaderListener; |
---|
33 | import 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 |
---|
46 | public 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 | } |
---|