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 | |
---|
27 | import org.junit.runner.RunWith; |
---|
28 | import org.springframework.beans.factory.annotation.Autowired; |
---|
29 | import org.springframework.test.context.ContextConfiguration; |
---|
30 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
---|
31 | import org.springframework.web.context.ContextLoaderListener; |
---|
32 | import org.springframework.web.context.request.RequestContextListener; |
---|
33 | |
---|
34 | /** |
---|
35 | * Base test that starts a servlet container with the component registry |
---|
36 | * @author george.georgovassilis@mpi.nl |
---|
37 | * |
---|
38 | */ |
---|
39 | @RunWith(SpringJUnit4ClassRunner.class) |
---|
40 | @ContextConfiguration(locations = { |
---|
41 | "classpath:spring-config/applicationContext.xml", |
---|
42 | "classpath:spring-config/test-applicationContext-fragment.xml" }) |
---|
43 | //Important: these tests can not be configured with @Transactional because it spawns two (mutually deadlocking) transactions: the test itself and jersey services |
---|
44 | public abstract class ComponentRegistryRestServiceTestCase extends JerseyTest { |
---|
45 | // CommandLine test e.g.: curl -i -H "Accept:application/json" -X GET |
---|
46 | // http://localhost:8080/ComponentRegistry/rest/registry/profiles |
---|
47 | |
---|
48 | protected final static GenericType<List<ProfileDescription>> PROFILE_LIST_GENERICTYPE = new GenericType<List<ProfileDescription>>() { |
---|
49 | }; |
---|
50 | protected final static GenericType<List<ComponentDescription>> COMPONENT_LIST_GENERICTYPE = new GenericType<List<ComponentDescription>>() { |
---|
51 | }; |
---|
52 | protected final static GenericType<List<Comment>> COMMENT_LIST_GENERICTYPE = new GenericType<List<Comment>>() { |
---|
53 | }; |
---|
54 | protected final static GenericType<List<Group>> GROUP_LIST_GENERICTYPE = new GenericType<List<Group>>() { |
---|
55 | }; |
---|
56 | protected final static GenericType<List<String>> STRING_LIST_GENERICTYPE = new GenericType<List<String>>() { |
---|
57 | }; |
---|
58 | |
---|
59 | private static SingletonTestContainerFactory _testContainerFactory; |
---|
60 | |
---|
61 | @Override |
---|
62 | public void setUp() throws Exception { |
---|
63 | if (!_testContainerFactory.isTestContainerRunning()) { |
---|
64 | _testContainerFactory.startTestContainer(); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | @Override |
---|
69 | public void tearDown() throws Exception { |
---|
70 | } |
---|
71 | |
---|
72 | @Override |
---|
73 | protected TestContainerFactory getTestContainerFactory() { |
---|
74 | if (_testContainerFactory == null) { |
---|
75 | _testContainerFactory = new SingletonTestContainerFactory( |
---|
76 | super.getTestContainerFactory()); |
---|
77 | } |
---|
78 | ; |
---|
79 | return _testContainerFactory; |
---|
80 | } |
---|
81 | |
---|
82 | @Autowired |
---|
83 | private UserDao userDao; |
---|
84 | |
---|
85 | protected String getApplicationContextFile() { |
---|
86 | // sorry for the duplication, but JerseyTest is not aware of |
---|
87 | // @ContextConfiguration |
---|
88 | return "classpath:spring-config/applicationContext.xml, classpath:spring-config/test-applicationContext-fragment.xml"; |
---|
89 | } |
---|
90 | |
---|
91 | @Override |
---|
92 | protected AppDescriptor configure() { |
---|
93 | WebAppDescriptor.Builder builder = new WebAppDescriptor.Builder() |
---|
94 | .contextParam("contextConfigLocation", |
---|
95 | getApplicationContextFile()) |
---|
96 | .servletClass(SpringServlet.class) |
---|
97 | .initParam(WebComponent.RESOURCE_CONFIG_CLASS, |
---|
98 | ClassNamesResourceConfig.class.getName()) |
---|
99 | .initParam( |
---|
100 | ClassNamesResourceConfig.PROPERTY_CLASSNAMES, |
---|
101 | FormDataMultiPartDispatchProvider.class.getName() + ";" |
---|
102 | + ComponentRegistryRestService.class.getName()) |
---|
103 | .addFilter(DummySecurityFilter.class, "DummySecurityFilter") |
---|
104 | .requestListenerClass(RequestContextListener.class) |
---|
105 | .contextListenerClass(ContextLoaderListener.class); |
---|
106 | return builder.build(); |
---|
107 | } |
---|
108 | |
---|
109 | protected WebResource getResource() { |
---|
110 | return resource(); |
---|
111 | } |
---|
112 | |
---|
113 | protected Builder getAuthenticatedResource(String path) { |
---|
114 | return getAuthenticatedResource(getResource().path(path)); |
---|
115 | } |
---|
116 | |
---|
117 | protected Builder getAuthenticatedResource(WebResource resource) { |
---|
118 | return resource.header( |
---|
119 | HttpHeaders.AUTHORIZATION, |
---|
120 | "Basic " |
---|
121 | + new String(Base64 |
---|
122 | .encode(DummyPrincipal.DUMMY_PRINCIPAL |
---|
123 | .getName() + ":dummy"))); |
---|
124 | } |
---|
125 | |
---|
126 | protected void createUserRecord() { |
---|
127 | RegistryUser user = new RegistryUser(); |
---|
128 | user.setName("Database test user"); |
---|
129 | user.setPrincipalName(DummyPrincipal.DUMMY_PRINCIPAL.getName()); |
---|
130 | userDao.save(user); |
---|
131 | } |
---|
132 | |
---|
133 | protected UserDao getUserDao() { |
---|
134 | return userDao; |
---|
135 | } |
---|
136 | |
---|
137 | } |
---|