1 | /* |
---|
2 | * To change this template, choose Tools | Templates |
---|
3 | * and open the template in the editor. |
---|
4 | */ |
---|
5 | package clarin.cmdi.componentregistry.rest; |
---|
6 | |
---|
7 | import clarin.cmdi.componentregistry.ComponentRegistry; |
---|
8 | import clarin.cmdi.componentregistry.ComponentRegistryFactory; |
---|
9 | import clarin.cmdi.componentregistry.ItemNotFoundException; |
---|
10 | import clarin.cmdi.componentregistry.RegistrySpace; |
---|
11 | import clarin.cmdi.componentregistry.components.CMDComponentSpec; |
---|
12 | import clarin.cmdi.componentregistry.impl.database.ComponentRegistryTestDatabase; |
---|
13 | import clarin.cmdi.componentregistry.impl.database.GroupService; |
---|
14 | import clarin.cmdi.componentregistry.model.BaseDescription; |
---|
15 | import clarin.cmdi.componentregistry.model.Comment; |
---|
16 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
17 | import clarin.cmdi.componentregistry.model.Ownership; |
---|
18 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
19 | import clarin.cmdi.componentregistry.model.RegisterResponse; |
---|
20 | import clarin.cmdi.componentregistry.model.RegistryUser; |
---|
21 | import clarin.cmdi.componentregistry.persistence.jpa.CommentsDao; |
---|
22 | import clarin.cmdi.componentregistry.persistence.jpa.UserDao; |
---|
23 | import com.sun.jersey.api.client.ClientResponse; |
---|
24 | import com.sun.jersey.multipart.FormDataMultiPart; |
---|
25 | import java.text.ParseException; |
---|
26 | import java.util.Date; |
---|
27 | import java.util.List; |
---|
28 | import javax.ws.rs.core.MediaType; |
---|
29 | import javax.xml.bind.JAXBException; |
---|
30 | import org.junit.Before; |
---|
31 | import org.springframework.beans.factory.annotation.Autowired; |
---|
32 | import org.springframework.jdbc.core.JdbcTemplate; |
---|
33 | |
---|
34 | import static org.junit.Assert.*; |
---|
35 | import org.junit.Test; |
---|
36 | |
---|
37 | /** |
---|
38 | * |
---|
39 | * @author olhsha |
---|
40 | */ |
---|
41 | public class SanboxTest extends ComponentRegistryRestServiceTestCase { |
---|
42 | |
---|
43 | @Autowired |
---|
44 | private ComponentRegistryFactory componentRegistryFactory; |
---|
45 | |
---|
46 | @Autowired |
---|
47 | private JdbcTemplate jdbcTemplate; |
---|
48 | @Autowired |
---|
49 | private GroupService groupService; |
---|
50 | @Autowired |
---|
51 | private UserDao userDao; |
---|
52 | @Autowired |
---|
53 | private CommentsDao commentsDao; |
---|
54 | private ComponentRegistry baseRegistry; |
---|
55 | |
---|
56 | @Before |
---|
57 | public void init() { |
---|
58 | ComponentRegistryTestDatabase.resetAndCreateAllTables(jdbcTemplate); |
---|
59 | createUserRecord(); |
---|
60 | baseRegistry = componentRegistryFactory.getBaseRegistry(DummyPrincipal.DUMMY_CREDENTIALS); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | private ComponentDescription component1; |
---|
65 | private ComponentDescription component2; |
---|
66 | private ProfileDescription profile1; |
---|
67 | private ProfileDescription profile2; |
---|
68 | private ComponentDescription component3; |
---|
69 | private ProfileDescription profile3; |
---|
70 | private Comment profile1Comment1; |
---|
71 | private Comment profile1Comment2; |
---|
72 | private Comment component1Comment3; |
---|
73 | private Comment component1Comment4; |
---|
74 | private Comment profile3Comment5; |
---|
75 | private Comment component3Comment7; |
---|
76 | |
---|
77 | private void fillUpPublicItems() throws Exception { |
---|
78 | |
---|
79 | profile1 = RegistryTestHelper.addProfile(baseRegistry, "profile2", true); |
---|
80 | profile2 = RegistryTestHelper.addProfile(baseRegistry, "profile1", true); |
---|
81 | component1 = RegistryTestHelper.addComponent(baseRegistry, |
---|
82 | "component2", true); |
---|
83 | component2 = RegistryTestHelper.addComponent(baseRegistry, |
---|
84 | "component1", true); |
---|
85 | profile1Comment2 = RegistryTestHelper.addComment(baseRegistry, "comment2", |
---|
86 | ProfileDescription.PROFILE_PREFIX + "profile1", |
---|
87 | "JUnit@test.com"); |
---|
88 | profile1Comment1 = RegistryTestHelper.addComment(baseRegistry, "comment1", |
---|
89 | ProfileDescription.PROFILE_PREFIX + "profile1", |
---|
90 | "JUnit@test.com"); |
---|
91 | component1Comment3 = RegistryTestHelper.addComment(baseRegistry, "comment3", |
---|
92 | ComponentDescription.COMPONENT_PREFIX + "component1", |
---|
93 | "JUnit@test.com"); |
---|
94 | component1Comment4 = RegistryTestHelper.addComment(baseRegistry, "comment4", |
---|
95 | ComponentDescription.COMPONENT_PREFIX + "component1", |
---|
96 | "JUnit@test.com"); |
---|
97 | } |
---|
98 | |
---|
99 | private void fillUpPrivateItems() throws Exception { |
---|
100 | profile3 = RegistryTestHelper.addProfile(baseRegistry, "profile3", false); |
---|
101 | component3 = RegistryTestHelper.addComponent(baseRegistry, |
---|
102 | "component3", false); |
---|
103 | profile3Comment5 = RegistryTestHelper.addComment(baseRegistry, "comment5", |
---|
104 | ProfileDescription.PROFILE_PREFIX + "profile3", |
---|
105 | "JUnit@test.com"); |
---|
106 | component3Comment7 = RegistryTestHelper.addComment(baseRegistry, "comment7", |
---|
107 | ComponentDescription.COMPONENT_PREFIX + "component3", |
---|
108 | "JUnit@test.com"); |
---|
109 | } |
---|
110 | |
---|
111 | protected void createAntherUserRecord() { |
---|
112 | RegistryUser user = new RegistryUser(); |
---|
113 | user.setName("Another database test user"); |
---|
114 | user.setPrincipalName("anotherPrincipal"); |
---|
115 | userDao.save(user); |
---|
116 | } |
---|
117 | |
---|
118 | private void MakeGroupA(){ |
---|
119 | groupService.createNewGroup("group A", DummyPrincipal.DUMMY_PRINCIPAL.getName()); |
---|
120 | } |
---|
121 | |
---|
122 | private void fillUpGroupA() throws ParseException, JAXBException, ItemNotFoundException{ |
---|
123 | |
---|
124 | MakeGroupA(); |
---|
125 | |
---|
126 | RegistryTestHelper.addProfile(baseRegistry, "profile-1", false); |
---|
127 | RegistryTestHelper.addComponent(baseRegistry, "component-1", false); |
---|
128 | RegistryTestHelper.addComponent(baseRegistry, "component-2", false); |
---|
129 | |
---|
130 | Ownership ownership = new Ownership(); |
---|
131 | ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"profile-1"); |
---|
132 | ownership.setGroupId(1); |
---|
133 | ownership.setUserId(0); |
---|
134 | groupService.addOwnership(ownership); |
---|
135 | |
---|
136 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-1"); |
---|
137 | ownership.setGroupId(1); |
---|
138 | ownership.setUserId(0); |
---|
139 | groupService.addOwnership(ownership); |
---|
140 | |
---|
141 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-2"); |
---|
142 | ownership.setGroupId(1); |
---|
143 | ownership.setUserId(0); |
---|
144 | groupService.addOwnership(ownership); |
---|
145 | |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | private void MakeGroupB() throws ItemNotFoundException{ |
---|
151 | createAntherUserRecord(); |
---|
152 | groupService.createNewGroup("group B", "anotherPrincipal"); |
---|
153 | groupService.makeMember(DummyPrincipal.DUMMY_PRINCIPAL.getName(), "group B"); |
---|
154 | } |
---|
155 | |
---|
156 | private void MakeGroupC() throws ItemNotFoundException{ |
---|
157 | groupService.createNewGroup("group C", "anotherPrincipal"); |
---|
158 | } |
---|
159 | |
---|
160 | private void fillUpGroupB() throws ParseException, JAXBException, ItemNotFoundException{ |
---|
161 | |
---|
162 | MakeGroupB(); |
---|
163 | |
---|
164 | RegistryTestHelper.addProfile(baseRegistry, "Bprofile-1", false); |
---|
165 | RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-1", false); |
---|
166 | RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-2", false); |
---|
167 | |
---|
168 | Ownership ownership = new Ownership(); |
---|
169 | ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Bprofile-1"); |
---|
170 | ownership.setGroupId(2); |
---|
171 | ownership.setUserId(0); |
---|
172 | groupService.addOwnership(ownership); |
---|
173 | |
---|
174 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1"); |
---|
175 | ownership.setGroupId(2); |
---|
176 | ownership.setUserId(0); |
---|
177 | groupService.addOwnership(ownership); |
---|
178 | |
---|
179 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-2"); |
---|
180 | ownership.setGroupId(2); |
---|
181 | ownership.setUserId(0); |
---|
182 | groupService.addOwnership(ownership); |
---|
183 | |
---|
184 | } |
---|
185 | |
---|
186 | private void fillUpGroupC() throws ParseException, JAXBException, ItemNotFoundException{ |
---|
187 | |
---|
188 | MakeGroupC(); |
---|
189 | |
---|
190 | RegistryTestHelper.addProfileAnotherPrincipal(baseRegistry, "Cprofile-1", false); |
---|
191 | RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-1", false); |
---|
192 | RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-2", false); |
---|
193 | |
---|
194 | Ownership ownership = new Ownership(); |
---|
195 | ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Cprofile-1"); |
---|
196 | ownership.setGroupId(3); |
---|
197 | ownership.setUserId(0); |
---|
198 | groupService.addOwnership(ownership); |
---|
199 | |
---|
200 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1"); |
---|
201 | ownership.setGroupId(3); |
---|
202 | ownership.setUserId(0); |
---|
203 | groupService.addOwnership(ownership); |
---|
204 | |
---|
205 | ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-2"); |
---|
206 | ownership.setGroupId(3); |
---|
207 | ownership.setUserId(0); |
---|
208 | groupService.addOwnership(ownership); |
---|
209 | |
---|
210 | } |
---|
211 | |
---|
212 | |
---|
213 | |
---|
214 | private FormDataMultiPart createFormData(Object content) { |
---|
215 | return createFormData(content, "My Test"); |
---|
216 | } |
---|
217 | |
---|
218 | private FormDataMultiPart createFormData(Object content, String description) { |
---|
219 | FormDataMultiPart form = new FormDataMultiPart(); |
---|
220 | form.field(IComponentRegistryRestService.DATA_FORM_FIELD, content, |
---|
221 | MediaType.APPLICATION_OCTET_STREAM_TYPE); |
---|
222 | form.field(IComponentRegistryRestService.NAME_FORM_FIELD, "Test1"); |
---|
223 | form.field(IComponentRegistryRestService.DESCRIPTION_FORM_FIELD, |
---|
224 | description); |
---|
225 | form.field(IComponentRegistryRestService.DOMAIN_FORM_FIELD, "My domain"); |
---|
226 | form.field(IComponentRegistryRestService.GROUP_FORM_FIELD, "TestGroup"); |
---|
227 | return form; |
---|
228 | } |
---|
229 | |
---|
230 | |
---|
231 | |
---|
232 | @Test |
---|
233 | public void testDeleteProfileAndComponentFromGroup() throws Exception { |
---|
234 | |
---|
235 | System.out.println("test deleteProfileAndComponentFromGroup"); |
---|
236 | |
---|
237 | fillUpGroupA(); |
---|
238 | fillUpGroupB(); |
---|
239 | fillUpGroupC(); |
---|
240 | |
---|
241 | ClientResponse response = getAuthenticatedResource( |
---|
242 | "/registry/components/" + ComponentDescription.COMPONENT_PREFIX |
---|
243 | + "component-1").delete(ClientResponse.class); |
---|
244 | assertEquals(200, response.getStatus()); |
---|
245 | |
---|
246 | response = getAuthenticatedResource( |
---|
247 | "/registry/components/" + ComponentDescription.COMPONENT_PREFIX |
---|
248 | + "component-1").get(ClientResponse.class); |
---|
249 | assertEquals(404, response.getStatus()); |
---|
250 | |
---|
251 | // my group, not my component |
---|
252 | |
---|
253 | response = getAuthenticatedResource( |
---|
254 | "/registry/components/" + ComponentDescription.COMPONENT_PREFIX |
---|
255 | + "Bcomponent-1").delete(ClientResponse.class); |
---|
256 | assertEquals(200, response.getStatus()); |
---|
257 | |
---|
258 | response = getAuthenticatedResource( |
---|
259 | "/registry/components/" + ComponentDescription.COMPONENT_PREFIX |
---|
260 | + "Bcomponent-1").get(ClientResponse.class); |
---|
261 | assertEquals(404, response.getStatus()); |
---|
262 | |
---|
263 | // not my group |
---|
264 | response = getAuthenticatedResource( |
---|
265 | "/registry/components/" + ComponentDescription.COMPONENT_PREFIX |
---|
266 | + "Ccomponent-1").delete(ClientResponse.class); |
---|
267 | assertEquals(403, response.getStatus()); |
---|
268 | |
---|
269 | baseRegistry.setRegistrySpace(RegistrySpace.GROUP); |
---|
270 | baseRegistry.setGroupId(1); |
---|
271 | assertEquals(1, baseRegistry.getComponentDescriptions().size()); |
---|
272 | baseRegistry.setGroupId(2); |
---|
273 | assertEquals(1, baseRegistry.getComponentDescriptions().size()); |
---|
274 | // baseRegistry.setGroupId(3); |
---|
275 | // assertEquals(2, baseRegistry.getComponentDescriptions().size()); |
---|
276 | |
---|
277 | baseRegistry.setRegistrySpace(null); |
---|
278 | baseRegistry.setGroupId(null); |
---|
279 | |
---|
280 | // profiles |
---|
281 | |
---|
282 | response = getAuthenticatedResource( |
---|
283 | "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX |
---|
284 | + "profile-1").delete(ClientResponse.class); |
---|
285 | assertEquals(200, response.getStatus()); |
---|
286 | |
---|
287 | response = getAuthenticatedResource( |
---|
288 | "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX |
---|
289 | + "profile-1").get(ClientResponse.class); |
---|
290 | assertEquals(404, response.getStatus()); |
---|
291 | |
---|
292 | // my group, not my component |
---|
293 | |
---|
294 | response = getAuthenticatedResource( |
---|
295 | "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX |
---|
296 | + "Bprofile-1").delete(ClientResponse.class); |
---|
297 | assertEquals(200, response.getStatus()); |
---|
298 | |
---|
299 | response = getAuthenticatedResource( |
---|
300 | "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX |
---|
301 | + "Bprofile-1").get(ClientResponse.class); |
---|
302 | assertEquals(404, response.getStatus()); |
---|
303 | |
---|
304 | // not my group |
---|
305 | response = getAuthenticatedResource( |
---|
306 | "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX |
---|
307 | + "Cprofile-1").delete(ClientResponse.class); |
---|
308 | assertEquals(403, response.getStatus()); |
---|
309 | |
---|
310 | baseRegistry.setRegistrySpace(RegistrySpace.GROUP); |
---|
311 | baseRegistry.setGroupId(1); |
---|
312 | assertEquals(0, baseRegistry.getProfileDescriptions().size()); |
---|
313 | baseRegistry.setGroupId(2); |
---|
314 | assertEquals(0, baseRegistry.getProfileDescriptions().size()); |
---|
315 | } |
---|
316 | |
---|
317 | } |
---|