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