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