Changeset 5560
- Timestamp:
- 08/18/14 14:22:07 (10 years ago)
- Location:
- ComponentRegistry/trunk/ComponentRegistry/src
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImpl.java
r5556 r5560 278 278 public List<Comment> getCommentsInProfile(String profileId) throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException { 279 279 try { 280 //Olha was here281 //if (componentDao.isInRegistry(profileId, getUserId())) {282 280 if (this.canCurrentUserAccessDescription(profileId)) { 283 281 final List<Comment> commentsFromProfile = commentsDao.getCommentsFromItem(profileId); -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java
r5556 r5560 524 524 LOG.debug("Comments of component with id {} are requested.", commentId); 525 525 try { 526 527 return this.getBaseRegistry().getSpecifiedCommentInComponent(componentId, commentId);526 Comment result = this.getBaseRegistry().getSpecifiedCommentInComponent(componentId, commentId); 527 return result; 528 528 } catch (ComponentRegistryException e) { 529 529 response.sendError(Status.INTERNAL_SERVER_ERROR.getStatusCode(), e.getMessage()); -
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/RegistryTestHelper.java
r5556 r5560 23 23 import clarin.cmdi.componentregistry.model.ComponentDescription; 24 24 import clarin.cmdi.componentregistry.model.ProfileDescription; 25 import clarin.cmdi.componentregistry.persistence.jpa.CommentsDao; 25 26 26 27 import java.io.BufferedReader; … … 28 29 import java.io.FileOutputStream; 29 30 import java.io.InputStreamReader; 31 import java.util.Date; 30 32 31 33 import org.springframework.beans.factory.annotation.Autowired; … … 38 40 39 41 private static MDMarshaller marshaller; 42 43 44 40 45 41 46 @Autowired 42 47 public void setMarshaller(MDMarshaller marshaller) { 43 RegistryTestHelper.marshaller = marshaller; 44 } 45 46 48 RegistryTestHelper.marshaller = marshaller; 49 } 47 50 48 51 public static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, boolean isPublic) throws ParseException, JAXBException { 49 return addComponent(testRegistry, id, getComponentTestContent(), isPublic); 50 } 51 52 52 return addComponent(testRegistry, id, getComponentTestContent(), isPublic); 53 } 54 53 55 public static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, String content, boolean isPublic) throws ParseException, 54 55 56 JAXBException, UnsupportedEncodingException { 57 return addComponent(testRegistry, id, new ByteArrayInputStream(content.getBytes("UTF-8")), isPublic); 56 58 } 57 59 58 60 private static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, InputStream content, boolean isPublic) throws ParseException, 59 60 61 61 JAXBException { 62 ComponentDescription desc = ComponentDescription.createNewDescription(); 63 desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName()); 62 64 desc.setUserId(DummyPrincipal.DUMMY_PRINCIPAL.getName()); 63 64 65 66 65 desc.setName(id); 66 desc.setDescription("Test Description"); 67 desc.setId(ComponentDescription.COMPONENT_PREFIX + id); 68 desc.setHref("link:" + desc.getId()); 67 69 desc.setPublic(isPublic); 68 69 70 71 } 72 70 CMDComponentSpec spec = marshaller.unmarshal(CMDComponentSpec.class, content, marshaller.getCMDComponentSchema()); 71 testRegistry.register(desc, spec); 72 return desc; 73 } 74 73 75 public static ComponentDescription addComponentAnotherPrincipal(ComponentRegistry testRegistry, String id, boolean isPublic) throws ParseException, JAXBException { 74 75 } 76 77 78 79 80 76 return addComponentAnotherPrincipal(testRegistry, id, getComponentTestContent(), isPublic); 77 } 78 79 private static ComponentDescription addComponentAnotherPrincipal(ComponentRegistry testRegistry, String id, InputStream content, boolean isPublic) throws ParseException, 80 JAXBException { 81 ComponentDescription desc = ComponentDescription.createNewDescription(); 82 desc.setCreatorName("AnotherPrincipal"); 81 83 desc.setUserId("AnotherPrincipal"); 82 84 desc.setDbUserId(2); 83 84 85 86 85 desc.setName(id); 86 desc.setDescription("Test Description"); 87 desc.setId(ComponentDescription.COMPONENT_PREFIX + id); 88 desc.setHref("link:" + desc.getId()); 87 89 desc.setPublic(isPublic); 88 89 90 90 CMDComponentSpec spec = marshaller.unmarshal(CMDComponentSpec.class, content, marshaller.getCMDComponentSchema()); 91 testRegistry.register(desc, spec); 92 return desc; 91 93 } 92 94 93 95 public static String getProfileTestContentString() { 94 96 return getProfileTestContentString("Actor"); 95 97 } 96 98 97 99 private static String getProfileTestContentString(String name) { 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 100 String profileContent = ""; 101 profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 102 profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 103 profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; 104 profileContent += " <Header />\n"; 105 profileContent += " <CMD_Component name=\"" + name + "\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n"; 106 profileContent += " <AttributeList>\n"; 107 profileContent += " <Attribute>\n"; 108 profileContent += " <Name>Name</Name>\n"; 109 profileContent += " <Type>string</Type>\n"; 110 profileContent += " </Attribute>\n"; 111 profileContent += " </AttributeList>\n"; 112 profileContent += " <CMD_Element name=\"Age\">\n"; 113 profileContent += " <ValueScheme>\n"; 114 profileContent += " <pattern>[23][0-9]</pattern>\n"; 115 profileContent += " </ValueScheme>\n"; 116 profileContent += " </CMD_Element>\n"; 117 profileContent += " </CMD_Component>\n"; 118 profileContent += "</CMD_ComponentSpec>\n"; 119 return profileContent; 118 120 } 119 121 120 122 public static InputStream getTestProfileContent() { 121 123 return getTestProfileContent("Actor"); 122 124 } 123 125 124 126 public static InputStream getTestProfileContent(String name) { 125 127 return new ByteArrayInputStream(getProfileTestContentString(name).getBytes()); 126 128 } 127 129 128 130 public static ProfileDescription addProfile(ComponentRegistry testRegistry, String id, boolean isPublic) throws ParseException, JAXBException, ItemNotFoundException { 129 131 return addProfile(testRegistry, id, RegistryTestHelper.getTestProfileContent(), isPublic); 130 132 } 131 133 132 134 public static ProfileDescription addProfile(ComponentRegistry testRegistry, String id, String content, boolean isPublic) throws ParseException, 133 JAXBException, ItemNotFoundException { 134 return addProfile(testRegistry, id, new ByteArrayInputStream(content.getBytes()), isPublic); 135 } 136 137 135 JAXBException, ItemNotFoundException { 136 return addProfile(testRegistry, id, new ByteArrayInputStream(content.getBytes()), isPublic); 137 } 138 138 139 139 private static ProfileDescription addProfile(ComponentRegistry testRegistry, String id, InputStream content, boolean isPublic) throws ParseException, 140 141 142 143 144 145 146 147 140 JAXBException, ItemNotFoundException { 141 ProfileDescription desc = ProfileDescription.createNewDescription(); 142 desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName()); 143 desc.setUserId(DummyPrincipal.DUMMY_CREDENTIALS.getPrincipalName()); 144 desc.setName(id); 145 desc.setDescription("Test Description"); 146 desc.setId(ProfileDescription.PROFILE_PREFIX + id); 147 desc.setHref("link:" + ProfileDescription.PROFILE_PREFIX + id); 148 148 desc.setPublic(isPublic); 149 150 151 152 } 153 149 CMDComponentSpec spec = marshaller.unmarshal(CMDComponentSpec.class, content, marshaller.getCMDComponentSchema()); 150 testRegistry.register(desc, spec); 151 return desc; 152 } 153 154 154 public static ProfileDescription addProfileAnotherPrincipal(ComponentRegistry testRegistry, String id, boolean isPublic) throws ParseException, JAXBException, ItemNotFoundException { 155 156 } 157 155 return addProfileAnotherPrincipal(testRegistry, id, RegistryTestHelper.getTestProfileContent(), isPublic); 156 } 157 158 158 private static ProfileDescription addProfileAnotherPrincipal(ComponentRegistry testRegistry, String id, InputStream content, boolean isPublic) throws ParseException, 159 160 161 162 159 JAXBException, ItemNotFoundException { 160 ProfileDescription desc = ProfileDescription.createNewDescription(); 161 desc.setCreatorName("AnotherPrincipal"); 162 desc.setUserId("AnotherPrincipal"); 163 163 desc.setDbUserId(2); 164 165 166 167 164 desc.setName(id); 165 desc.setDescription("Test Description"); 166 desc.setId(ProfileDescription.PROFILE_PREFIX + id); 167 desc.setHref("link:" + ProfileDescription.PROFILE_PREFIX + id); 168 168 desc.setPublic(isPublic); 169 170 171 169 CMDComponentSpec spec = marshaller.unmarshal(CMDComponentSpec.class, content, marshaller.getCMDComponentSchema()); 170 testRegistry.register(desc, spec); 171 return desc; 172 172 } 173 173 174 174 public static CMDComponentSpec getTestProfile() throws JAXBException { 175 175 return marshaller.unmarshal(CMDComponentSpec.class, getTestProfileContent(), marshaller.getCMDComponentSchema()); 176 176 } 177 177 178 178 public static String getComponentTestContentString() { 179 179 return getComponentTestContentString("Access"); 180 180 } 181 181 182 182 public static InputStream getComponentTestContent() { 183 183 return getComponentTestContentAsStream("Access"); 184 184 } 185 185 186 186 public static String getComponentTestContentString(String componentName) { 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 187 String compContent = ""; 188 compContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 189 compContent += "\n"; 190 compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 191 compContent += " xsi:noNamespaceSchemaLocation=\"../../general-component-schema.xsd\">\n"; 192 compContent += " \n"; 193 compContent += " <Header/>\n"; 194 compContent += " \n"; 195 compContent += " <CMD_Component name=\"" + componentName + "\" CardinalityMin=\"1\" CardinalityMax=\"1\">\n"; 196 compContent += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; 197 compContent += " <CMD_Element name=\"Date\">\n"; 198 compContent += " <ValueScheme>\n"; 199 compContent += " <!-- matching dates of the pattern yyyy-mm-dd (ISO 8601); this only matches dates from the years 1000 through 2999 and does allow some invalid dates (e.g. February, the 30th) -->\n"; 200 compContent += " <pattern>(1|2)\\d{3}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])</pattern> \n"; 201 compContent += " </ValueScheme>\n"; 202 compContent += " </CMD_Element>\n"; 203 compContent += " <CMD_Element name=\"Owner\" ValueScheme=\"string\" />\n"; 204 compContent += " <CMD_Element name=\"Publisher\" ValueScheme=\"string\" />\n"; 205 compContent += " </CMD_Component>\n"; 206 compContent += "\n"; 207 compContent += "</CMD_ComponentSpec>\n"; 208 return compContent; 209 209 } 210 210 211 211 public static String getStringFromStream(InputStream largeProfileStream) throws IOException { 212 213 214 215 216 217 218 219 220 221 222 212 try { 213 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(largeProfileStream)); 214 StringBuilder profileStringBuilder = new StringBuilder(); 215 String line; 216 while (null != (line = bufferedReader.readLine())) { 217 profileStringBuilder.append(line); 218 } 219 return profileStringBuilder.toString(); 220 } finally { 221 largeProfileStream.close(); 222 } 223 223 } 224 224 /////////////////////////////////////////////////////// 225 225 226 226 public static String getLargeProfileContent() throws IOException { 227 228 227 InputStream largeProfileStream = RegistryTestHelper.class.getResourceAsStream("/xml/largeProfile.xml"); 228 return getStringFromStream(largeProfileStream); 229 229 } 230 230 231 231 ////////////////////// 232 232 public static CMDComponentSpec getComponentFromString(String contentString) throws JAXBException { 233 233 return marshaller.unmarshal(CMDComponentSpec.class, getComponentContentAsStream(contentString), marshaller.getCMDComponentSchema()); 234 234 } 235 235 236 236 public static InputStream getComponentContentAsStream(String content) { 237 237 return new ByteArrayInputStream(content.getBytes()); 238 238 } 239 239 240 240 public static InputStream getComponentTestContentAsStream(String componentName) { 241 241 return getComponentContentAsStream(getComponentTestContentString(componentName)); 242 242 } 243 243 244 244 public static CMDComponentSpec getTestComponent() throws JAXBException { 245 245 return marshaller.unmarshal(CMDComponentSpec.class, getComponentTestContent(), marshaller.getCMDComponentSchema()); 246 246 } 247 247 248 248 public static CMDComponentSpec getTestComponent(String name) throws JAXBException { 249 249 return marshaller.unmarshal(CMDComponentSpec.class, getComponentTestContentAsStream(name), marshaller.getCMDComponentSchema()); 250 250 } 251 251 252 252 public static String getXml(CMDComponentSpec componentSpec) throws JAXBException, UnsupportedEncodingException { 253 254 255 256 257 258 259 260 261 } 262 263 public static Comment addComment(ComponentRegistry testRegistry, String id, String descriptionId, String principal) throws ParseException, JAXBException, ComponentRegistryException, ItemNotFoundException, UserUnauthorizedException {264 253 ByteArrayOutputStream os = new ByteArrayOutputStream(); 254 marshaller.marshal(componentSpec, os); 255 String xml = os.toString(); 256 try { 257 os.close(); 258 } catch (IOException ex) { 259 } 260 return xml; 261 } 262 263 public static Comment addComment(ComponentRegistry testRegistry, String id, String descriptionId, String principal) throws ParseException, JAXBException, ComponentRegistryException, ItemNotFoundException, UserUnauthorizedException { 264 return addComment(testRegistry, RegistryTestHelper.getTestCommentContent(id, descriptionId), principal); 265 265 } 266 266 267 267 private static Comment addComment(ComponentRegistry testRegistry, InputStream content, String principal) throws ParseException, 268 JAXBException, 269 ComponentRegistryException, ItemNotFoundException,UserUnauthorizedException { 270 Comment spec = marshaller.unmarshal(Comment.class, content, null); 271 testRegistry.registerComment(spec, principal); 272 return spec; 268 JAXBException, 269 ComponentRegistryException, ItemNotFoundException, UserUnauthorizedException { 270 Comment spec = marshaller.unmarshal(Comment.class, content, null); 271 testRegistry.registerComment(spec, principal); 272 return spec; 273 } 274 275 public Comment addCommentBypassAuthorisation(CommentsDao commentsDao, String id, String descriptionId, String principal) throws ParseException, JAXBException, ComponentRegistryException, ItemNotFoundException, UserUnauthorizedException { 276 return addCommentBypassAuthorisation(commentsDao, RegistryTestHelper.getTestCommentContent(id, descriptionId), principal); 277 } 278 279 280 private Comment addCommentBypassAuthorisation(CommentsDao commentsDao, InputStream content, String principal) throws ParseException, 281 JAXBException, 282 ComponentRegistryException, ItemNotFoundException, UserUnauthorizedException { 283 Comment comment = marshaller.unmarshal(Comment.class, content, null); 284 comment.setCommentDate(new Date()); 285 comment.setUserId(2); 286 commentsDao.saveAndFlush(comment); 287 return comment; 273 288 } 274 289 275 290 public static String getCommentTestContentStringForProfile(String commentName, String profileId) { 276 String comContent = ""; 277 comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; 278 comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; 279 comContent += " <comments>" + commentName + "</comments>\n"; 280 comContent += " <commentDate>" + DatesHelper.createNewDate() + "</commentDate>\n"; 281 comContent += " <componentId>" + profileId + "</componentId>\n"; 282 comContent += " <userName>J. Unit</userName>\n"; 283 comContent += "</comment>\n"; 284 return comContent; 291 String comContent = ""; 292 comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; 293 comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; 294 comContent += " <comments>" + commentName + "</comments>\n"; 295 comContent += " <commentDate>" + DatesHelper.createNewDate() + "</commentDate>\n"; 296 comContent += " <componentId>" + profileId + "</componentId>\n"; 297 comContent += " <userName>JUnit@test.com</userName>\n"; 298 comContent += "</comment>\n"; 299 return comContent; 300 } 301 302 public static String getanotherPrincipalCommentTestContentStringForProfile(String commentName, String profileId) { 303 String comContent = ""; 304 comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; 305 comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; 306 comContent += " <comments>" + commentName + "</comments>\n"; 307 comContent += " <commentDate>" + DatesHelper.createNewDate() + "</commentDate>\n"; 308 comContent += " <componentId>" + profileId + "</componentId>\n"; 309 comContent += " <userName>anotherPrincipal</userName>\n"; 310 comContent += "</comment>\n"; 311 return comContent; 285 312 } 286 313 287 314 public static String getCommentTestContentStringForComponent(String commentName, String componentId) { 288 String comContent = ""; 289 comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; 290 comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; 291 comContent += " <comments>" + commentName + "</comments>\n"; 292 comContent += " <commentDate>" + DatesHelper.createNewDate() + "</commentDate>\n"; 293 comContent += " <componentId>" + componentId + "</componentId>"; 294 comContent += " <userName>J. Unit</userName>\n"; 295 comContent += "</comment>\n"; 296 return comContent; 315 String comContent = ""; 316 comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; 317 comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; 318 comContent += " <comments>" + commentName + "</comments>\n"; 319 comContent += " <commentDate>" + DatesHelper.createNewDate() + "</commentDate>\n"; 320 comContent += " <componentId>" + componentId + "</componentId>"; 321 comContent += " <userName>JUnit@test.com</userName>\n"; 322 comContent += "</comment>\n"; 323 return comContent; 324 } 325 326 public static String getAnotherPrincipalCommentTestContentStringForComponent(String commentName, String componentId) { 327 String comContent = ""; 328 comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; 329 comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; 330 comContent += " <comments>" + commentName + "</comments>\n"; 331 comContent += " <commentDate>" + DatesHelper.createNewDate() + "</commentDate>\n"; 332 comContent += " <componentId>" + componentId + "</componentId>"; 333 comContent += " <userName>anotherPrincipal</userName>\n"; 334 comContent += "</comment>\n"; 335 return comContent; 297 336 } 298 337 299 338 public static InputStream getTestCommentContent(String content, String descriptionId) { 300 301 302 303 304 339 if (descriptionId.contains("profile")) { 340 return new ByteArrayInputStream(getCommentTestContentStringForProfile(content, descriptionId).getBytes()); 341 } else { 342 return new ByteArrayInputStream(getCommentTestContentStringForComponent(content, descriptionId).getBytes()); 343 } 305 344 } 306 345 307 346 public static String getCommentTestContent(String commentId, String descriptionId) { 308 347 return getCommentTestContentStringForProfile(commentId, descriptionId); 309 348 } 310 349 311 350 public static InputStream getCommentTestContent() { 312 return getTestCommentContent("Actual", ProfileDescription.PROFILE_PREFIX+"profile1"); 313 } 314 315 /** 316 * Testing a big xsd string is a bit hard, so doing a best effort by checking the xs:element which represent the nested components used 317 * in a profile/component 351 return getTestCommentContent("Actual", ProfileDescription.PROFILE_PREFIX + "profile1"); 352 } 353 354 /** 355 * Testing a big xsd string is a bit hard, so doing a best effort by 356 * checking the xs:element which represent the nested components used in a 357 * profile/component 318 358 */ 319 359 public static boolean hasComponent(String xsd, String name, String min, String max) { 320 321 322 360 Pattern pattern = Pattern.compile("<xs:element name=\"" + name + "\" minOccurs=\"" + min + "\" maxOccurs=\"" + max + "\">"); 361 Matcher matcher = pattern.matcher(xsd); 362 return matcher.find() && !matcher.find(); //find only one 323 363 } 324 364 325 365 /** 326 366 * 327 * @param bytes is an array of bytes to be written in the file filename (from scratch!) 328 * @param filename is the name of the file where the array "bytes" is to be written to 367 * @param bytes is an array of bytes to be written in the file filename 368 * (from scratch!) 369 * @param filename is the name of the file where the array "bytes" is to be 370 * written to 329 371 * @throws IOException 330 372 * @throws JAXBException … … 332 374 public static void writeBytesToFile(byte[] bytes, String filename) throws IOException, JAXBException { 333 375 334 335 336 337 338 339 340 376 File file = new File(filename); 377 FileOutputStream fop = new FileOutputStream(file); 378 379 fop.write(bytes); 380 381 fop.flush(); 382 fop.close(); 341 383 342 384 … … 345 387 /** 346 388 * 347 * @param str is a string which is to be written into the filename (from scratch!) 389 * @param str is a string which is to be written into the filename (from 390 * scratch!) 348 391 * @param filename is a filename where the string is to be written to 349 392 * @throws IOException … … 352 395 public static void writeStringToFile(String str, String filename) throws IOException, JAXBException { 353 396 354 397 writeBytesToFile(str.getBytes(), filename); 355 398 356 399 … … 359 402 /** 360 403 * 361 * @param os is an output stream which is to be written into the filename (from scratch!) 404 * @param os is an output stream which is to be written into the filename 405 * (from scratch!) 362 406 * @param filename is a filename where the stream is to be written to 363 407 * @throws IOException … … 366 410 public static void writeStreamToFile(ByteArrayOutputStream os, String filename) throws IOException, JAXBException { 367 411 368 412 writeBytesToFile(os.toByteArray(), filename); 369 413 370 414 … … 373 417 /** 374 418 * 375 * @param cdesc is a component which is to be written into the filename (from scratch!) 419 * @param cdesc is a component which is to be written into the filename 420 * (from scratch!) 376 421 * @param filename is a filename where the component is to be written to 377 422 * @throws IOException … … 381 426 382 427 383 384 385 386 428 ByteArrayOutputStream os = new ByteArrayOutputStream(); 429 marshaller.marshal(cdesc, os); 430 431 writeStreamToFile(os, filename); 387 432 388 433 } … … 391 436 * opens a temporary sub-directory dirName in /target/ 392 437 * 393 * @param dirName is the name of the temporary subdirectory which is to be opened 438 * @param dirName is the name of the temporary subdirectory which is to be 439 * opened 394 440 * @return the absolute part for this directory 395 441 */ 396 442 public static String openTestDir(String dirName) { 397 443 398 399 400 401 402 403 404 405 406 407 444 File testDir = new File("target/" + dirName); 445 446 447 testDir.mkdir(); 448 449 System.out.println(dirName); 450 //String retval = new File(testDir, dirName).getAbsolutePath(); 451 String retval = new File(testDir, "/").getAbsolutePath(); 452 453 return (retval); 408 454 409 455 } -
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/RestGroupServiceTest.java
r5556 r5560 17 17 import clarin.cmdi.componentregistry.model.ProfileDescription; 18 18 import clarin.cmdi.componentregistry.model.RegistryUser; 19 import clarin.cmdi.componentregistry.persistence.jpa.CommentsDao; 19 20 import clarin.cmdi.componentregistry.persistence.jpa.UserDao; 20 21 import com.sun.jersey.api.client.ClientResponse; … … 22 23 import java.util.List; 23 24 import javax.ws.rs.core.MediaType; 24 import javax.ws.rs.core.MultivaluedMap;25 25 import javax.xml.bind.JAXBException; 26 26 import org.junit.Before; … … 46 46 @Autowired 47 47 private UserDao userDao; 48 @Autowired 49 private CommentsDao commentsDao; 48 50 49 51 private ComponentRegistry baseRegistry; … … 507 509 fillUpGroupC(); 508 510 511 // lists 509 512 510 513 List<ProfileDescription> response = this.getAuthenticatedResource(getResource() … … 534 537 assertEquals(403, clientResponse.getStatus()); 535 538 539 // particular components and profiles 536 540 537 541 CMDComponentSpec component = this.getAuthenticatedResource(getResource() … … 570 574 571 575 } 576 577 @Test 578 public void testGetGroupComments() throws Exception { 579 580 System.out.println("test getGroupComments"); 581 582 fillUpGroupA(); 583 fillUpGroupB(); 584 fillUpGroupC(); 585 586 587 RegistryTestHelper.addComment(baseRegistry, "COMMENTc1", ComponentDescription.COMPONENT_PREFIX + "component-1", 588 "JUnit@test.com"); 589 RegistryTestHelper.addComment(baseRegistry, "COMMENTp1", ProfileDescription.PROFILE_PREFIX + "profile-1", 590 "JUnit@test.com"); 591 RegistryTestHelper.addComment(baseRegistry, "COMMENTBc1", ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1", 592 "anotherPrincipal"); 593 RegistryTestHelper.addComment(baseRegistry, "COMMENTBp1", ProfileDescription.PROFILE_PREFIX + "Bprofile-1", 594 "anotherPrincipal"); 595 (new RegistryTestHelper()).addCommentBypassAuthorisation(commentsDao, "COMMENTCc1", ComponentDescription.COMPONENT_PREFIX + "Ccomponent-1", 596 "anotherPrincipal"); 597 (new RegistryTestHelper()).addCommentBypassAuthorisation(commentsDao, "COMMENTCp1", ProfileDescription.PROFILE_PREFIX + "Cprofile-1","anotherPrincipal"); 598 599 600 // lists 601 602 List<Comment> response = this.getAuthenticatedResource(getResource() 603 .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "component-1" + "/comments/")) 604 .accept(MediaType.APPLICATION_XML) 605 .get(COMMENT_LIST_GENERICTYPE); 606 assertEquals(1, response.size()); 607 608 response = this.getAuthenticatedResource(getResource() 609 .path("/registry/components/" + ProfileDescription.PROFILE_PREFIX + "profile-1" + "/comments/")) 610 .accept(MediaType.APPLICATION_XML) 611 .get(COMMENT_LIST_GENERICTYPE); 612 assertEquals(1, response.size()); 613 614 response = this.getAuthenticatedResource(getResource() 615 .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1" + "/comments/")) 616 .accept(MediaType.APPLICATION_XML) 617 .get(COMMENT_LIST_GENERICTYPE); 618 assertEquals(1, response.size()); 619 620 response = this.getAuthenticatedResource(getResource() 621 .path("/registry/components/" + ProfileDescription.PROFILE_PREFIX + "Bprofile-1" + "/comments/")) 622 .accept(MediaType.APPLICATION_XML) 623 .get(COMMENT_LIST_GENERICTYPE); 624 assertEquals(1, response.size()); 625 626 627 628 ClientResponse clientResponse = this.getAuthenticatedResource(getResource() 629 .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1")) 630 .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); 631 assertEquals(403, clientResponse.getStatus()); 632 633 clientResponse = this.getAuthenticatedResource(getResource() 634 .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1")) 635 .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); 636 assertEquals(403, clientResponse.getStatus()); 637 638 // particular comments 639 640 Comment responseComment = this.getAuthenticatedResource(getResource() 641 .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "component-1" + "/comments/1")) 642 .accept(MediaType.APPLICATION_XML) 643 .get(Comment.class); 644 assertNotNull(responseComment); 645 646 assertEquals(1, Long.parseLong(responseComment.getId())); 647 648 responseComment = this.getAuthenticatedResource(getResource() 649 .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "profile-1" + "/comments/2")) 650 .accept(MediaType.APPLICATION_XML) 651 .get(Comment.class); 652 assertNotNull(responseComment); 653 654 assertEquals(2, Long.parseLong(responseComment.getId())); 655 656 responseComment = this.getAuthenticatedResource(getResource() 657 .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1" + "/comments/3")) 658 .accept(MediaType.APPLICATION_XML) 659 .get(Comment.class); 660 assertNotNull(responseComment); 661 assertEquals(3, Long.parseLong(responseComment.getId())); 662 663 responseComment = this.getAuthenticatedResource(getResource() 664 .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "Bprofile-1" + "/comments/4")) 665 .accept(MediaType.APPLICATION_XML) 666 .get(Comment.class); 667 assertNotNull(responseComment); 668 assertEquals(4, Long.parseLong(responseComment.getId())); 669 670 clientResponse = this.getAuthenticatedResource(getResource() 671 .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1/comments/6")) 672 .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); 673 assertEquals(403, clientResponse.getStatus()); 674 675 clientResponse = this.getAuthenticatedResource(getResource() 676 .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1/comments/5")) 677 .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); 678 assertEquals(403, clientResponse.getStatus()); 679 680 } 572 681 573 682 } -
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/SanboxTest.java
r5552 r5560 7 7 import clarin.cmdi.componentregistry.ComponentRegistry; 8 8 import clarin.cmdi.componentregistry.ComponentRegistryFactory; 9 import clarin.cmdi.componentregistry. impl.database.ComponentRegistryBeanFactory;9 import clarin.cmdi.componentregistry.ItemNotFoundException; 10 10 import clarin.cmdi.componentregistry.impl.database.ComponentRegistryTestDatabase; 11 import clarin.cmdi.componentregistry.impl.database.GroupService; 11 12 import clarin.cmdi.componentregistry.model.Comment; 12 13 import clarin.cmdi.componentregistry.model.ComponentDescription; 14 import clarin.cmdi.componentregistry.model.Ownership; 13 15 import clarin.cmdi.componentregistry.model.ProfileDescription; 16 import clarin.cmdi.componentregistry.model.RegistryUser; 17 import clarin.cmdi.componentregistry.persistence.jpa.CommentsDao; 18 import clarin.cmdi.componentregistry.persistence.jpa.UserDao; 14 19 import com.sun.jersey.api.client.ClientResponse; 15 import com.sun.jersey.multipart.FormDataMultiPart;20 import java.text.ParseException; 16 21 import java.util.List; 17 import javax.ws.rs.WebApplicationException;18 22 import javax.ws.rs.core.MediaType; 23 import javax.xml.bind.JAXBException; 19 24 import org.junit.Before; 20 import org.junit.Test;21 25 import org.springframework.beans.factory.annotation.Autowired; 22 26 import org.springframework.jdbc.core.JdbcTemplate; 23 27 24 28 import static org.junit.Assert.*; 29 import org.junit.Test; 25 30 26 31 /** … … 29 34 */ 30 35 public class SanboxTest extends ComponentRegistryRestServiceTestCase { 31 36 32 37 @Autowired 33 38 private ComponentRegistryFactory componentRegistryFactory; 34 @Autowired 35 private ComponentRegistryBeanFactory componentRegistryBeanFactory; 39 36 40 @Autowired 37 41 private JdbcTemplate jdbcTemplate; 42 @Autowired 43 private GroupService groupService; 44 @Autowired 45 private UserDao userDao; 46 @Autowired 47 private CommentsDao commentsDao; 38 48 private ComponentRegistry baseRegistry; 39 49 … … 45 55 } 46 56 47 private String expectedUserId(String principal) { 48 return getUserDao().getByPrincipalName(principal).getId().toString(); 49 } 57 50 58 private ComponentDescription component1; 51 59 private ComponentDescription component2; … … 94 102 "JUnit@test.com"); 95 103 } 96 104 105 protected void createAntherUserRecord() { 106 RegistryUser user = new RegistryUser(); 107 user.setName("Another database test user"); 108 user.setPrincipalName("anotherPrincipal"); 109 userDao.save(user); 110 } 111 112 private void MakeGroupA(){ 113 groupService.createNewGroup("group A", DummyPrincipal.DUMMY_PRINCIPAL.getName()); 114 } 115 116 private void fillUpGroupA() throws ParseException, JAXBException, ItemNotFoundException{ 117 118 MakeGroupA(); 119 120 RegistryTestHelper.addProfile(baseRegistry, "profile-1", false); 121 RegistryTestHelper.addComponent(baseRegistry, "component-1", false); 122 RegistryTestHelper.addComponent(baseRegistry, "component-2", false); 123 124 Ownership ownership = new Ownership(); 125 ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"profile-1"); 126 ownership.setGroupId(1); 127 ownership.setUserId(0); 128 groupService.addOwnership(ownership); 129 130 ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-1"); 131 ownership.setGroupId(1); 132 ownership.setUserId(0); 133 groupService.addOwnership(ownership); 134 135 ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-2"); 136 ownership.setGroupId(1); 137 ownership.setUserId(0); 138 groupService.addOwnership(ownership); 139 140 } 141 142 143 144 private void MakeGroupB() throws ItemNotFoundException{ 145 createAntherUserRecord(); 146 groupService.createNewGroup("group B", "anotherPrincipal"); 147 groupService.makeMember(DummyPrincipal.DUMMY_PRINCIPAL.getName(), "group B"); 148 } 149 150 private void MakeGroupC() throws ItemNotFoundException{ 151 groupService.createNewGroup("group C", "anotherPrincipal"); 152 } 153 154 private void fillUpGroupB() throws ParseException, JAXBException, ItemNotFoundException{ 155 156 MakeGroupB(); 157 158 RegistryTestHelper.addProfile(baseRegistry, "Bprofile-1", false); 159 RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-1", false); 160 RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-2", false); 161 162 Ownership ownership = new Ownership(); 163 ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Bprofile-1"); 164 ownership.setGroupId(2); 165 ownership.setUserId(0); 166 groupService.addOwnership(ownership); 167 168 ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1"); 169 ownership.setGroupId(2); 170 ownership.setUserId(0); 171 groupService.addOwnership(ownership); 172 173 ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-2"); 174 ownership.setGroupId(2); 175 ownership.setUserId(0); 176 groupService.addOwnership(ownership); 177 178 } 179 180 private void fillUpGroupC() throws ParseException, JAXBException, ItemNotFoundException{ 181 182 MakeGroupC(); 183 184 RegistryTestHelper.addProfileAnotherPrincipal(baseRegistry, "Cprofile-1", false); 185 RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-1", false); 186 RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-2", false); 187 188 Ownership ownership = new Ownership(); 189 ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Cprofile-1"); 190 ownership.setGroupId(3); 191 ownership.setUserId(0); 192 groupService.addOwnership(ownership); 193 194 ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1"); 195 ownership.setGroupId(3); 196 ownership.setUserId(0); 197 groupService.addOwnership(ownership); 198 199 ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-2"); 200 ownership.setGroupId(3); 201 ownership.setUserId(0); 202 groupService.addOwnership(ownership); 203 204 } 205 206 207 97 208 @Test 98 public void testDeleteCommentFromPublicProfile() throws Exception { 99 100 System.out.println("testDeleteCommentFromPublicProfile"); 101 102 fillUpPublicItems(); 103 104 List<Comment> comments = this.getAuthenticatedResource(getResource().path( 105 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX 106 + "profile1/comments")).get(COMMENT_LIST_GENERICTYPE); 107 assertEquals(2, comments.size()); 108 Comment aComment = this.getAuthenticatedResource(getResource().path( 109 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX 110 + "profile1/comments/" + profile1Comment1.getId())).get(Comment.class); 111 assertNotNull(aComment); 112 113 // Try to delete from other profile 114 ClientResponse response = getAuthenticatedResource( 115 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX 116 + "profile2/comments/9999").delete(ClientResponse.class); 117 assertEquals(404, response.getStatus()); 118 // Delete from correct profile 119 response = getAuthenticatedResource( 120 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX 121 + "profile1/comments/" + profile1Comment1.getId()).delete(ClientResponse.class); 122 assertEquals(200, response.getStatus()); 123 124 comments = this.getAuthenticatedResource(getResource().path( 125 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX 126 + "profile1/comments/")).get(COMMENT_LIST_GENERICTYPE); 127 assertEquals(1, comments.size()); 128 129 response = getAuthenticatedResource( 130 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX 131 + "profile1/comments/" + profile1Comment2.getId()).delete(ClientResponse.class); 132 assertEquals(200, response.getStatus()); 133 134 comments = this.getAuthenticatedResource(getResource().path( 135 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX 136 + "profile1/comments")).get(COMMENT_LIST_GENERICTYPE); 137 assertEquals(0, comments.size()); 138 } 139 140 private FormDataMultiPart createFormData(Object content) { 141 return createFormData(content, "My Test"); 142 } 143 144 private FormDataMultiPart createFormData(Object content, String description) { 145 FormDataMultiPart form = new FormDataMultiPart(); 146 form.field(IComponentRegistryRestService.DATA_FORM_FIELD, content, 147 MediaType.APPLICATION_OCTET_STREAM_TYPE); 148 form.field(IComponentRegistryRestService.NAME_FORM_FIELD, "Test1"); 149 form.field(IComponentRegistryRestService.DESCRIPTION_FORM_FIELD, 150 description); 151 form.field(IComponentRegistryRestService.DOMAIN_FORM_FIELD, "My domain"); 152 form.field(IComponentRegistryRestService.GROUP_FORM_FIELD, "TestGroup"); 153 return form; 154 } 209 public void testGetGroupComments() throws Exception { 210 211 System.out.println("test getGroupComments"); 212 213 fillUpGroupA(); 214 fillUpGroupB(); 215 fillUpGroupC(); 216 217 218 RegistryTestHelper.addComment(baseRegistry, "COMMENTc1", ComponentDescription.COMPONENT_PREFIX + "component-1", 219 "JUnit@test.com"); 220 RegistryTestHelper.addComment(baseRegistry, "COMMENTp1", ProfileDescription.PROFILE_PREFIX + "profile-1", 221 "JUnit@test.com"); 222 RegistryTestHelper.addComment(baseRegistry, "COMMENTBc1", ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1", 223 "anotherPrincipal"); 224 RegistryTestHelper.addComment(baseRegistry, "COMMENTBp1", ProfileDescription.PROFILE_PREFIX + "Bprofile-1", 225 "anotherPrincipal"); 226 (new RegistryTestHelper()).addCommentBypassAuthorisation(commentsDao, "COMMENTCc1", ComponentDescription.COMPONENT_PREFIX + "Ccomponent-1", 227 "anotherPrincipal"); 228 (new RegistryTestHelper()).addCommentBypassAuthorisation(commentsDao, "COMMENTCp1", ProfileDescription.PROFILE_PREFIX + "Cprofile-1","anotherPrincipal"); 229 230 231 // lists 232 233 List<Comment> response = this.getAuthenticatedResource(getResource() 234 .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "component-1" + "/comments/")) 235 .accept(MediaType.APPLICATION_XML) 236 .get(COMMENT_LIST_GENERICTYPE); 237 assertEquals(1, response.size()); 238 239 response = this.getAuthenticatedResource(getResource() 240 .path("/registry/components/" + ProfileDescription.PROFILE_PREFIX + "profile-1" + "/comments/")) 241 .accept(MediaType.APPLICATION_XML) 242 .get(COMMENT_LIST_GENERICTYPE); 243 assertEquals(1, response.size()); 244 245 response = this.getAuthenticatedResource(getResource() 246 .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1" + "/comments/")) 247 .accept(MediaType.APPLICATION_XML) 248 .get(COMMENT_LIST_GENERICTYPE); 249 assertEquals(1, response.size()); 250 251 response = this.getAuthenticatedResource(getResource() 252 .path("/registry/components/" + ProfileDescription.PROFILE_PREFIX + "Bprofile-1" + "/comments/")) 253 .accept(MediaType.APPLICATION_XML) 254 .get(COMMENT_LIST_GENERICTYPE); 255 assertEquals(1, response.size()); 256 257 258 259 ClientResponse clientResponse = this.getAuthenticatedResource(getResource() 260 .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1")) 261 .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); 262 assertEquals(403, clientResponse.getStatus()); 263 264 clientResponse = this.getAuthenticatedResource(getResource() 265 .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1")) 266 .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); 267 assertEquals(403, clientResponse.getStatus()); 268 269 // particular comments 270 271 Comment responseComment = this.getAuthenticatedResource(getResource() 272 .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "component-1" + "/comments/1")) 273 .accept(MediaType.APPLICATION_XML) 274 .get(Comment.class); 275 assertNotNull(responseComment); 276 277 assertEquals(1, Long.parseLong(responseComment.getId())); 278 279 responseComment = this.getAuthenticatedResource(getResource() 280 .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "profile-1" + "/comments/2")) 281 .accept(MediaType.APPLICATION_XML) 282 .get(Comment.class); 283 assertNotNull(responseComment); 284 285 assertEquals(2, Long.parseLong(responseComment.getId())); 286 287 responseComment = this.getAuthenticatedResource(getResource() 288 .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1" + "/comments/3")) 289 .accept(MediaType.APPLICATION_XML) 290 .get(Comment.class); 291 assertNotNull(responseComment); 292 assertEquals(3, Long.parseLong(responseComment.getId())); 293 294 responseComment = this.getAuthenticatedResource(getResource() 295 .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "Bprofile-1" + "/comments/4")) 296 .accept(MediaType.APPLICATION_XML) 297 .get(Comment.class); 298 assertNotNull(responseComment); 299 assertEquals(4, Long.parseLong(responseComment.getId())); 300 301 clientResponse = this.getAuthenticatedResource(getResource() 302 .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1/comments/6")) 303 .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); 304 assertEquals(403, clientResponse.getStatus()); 305 306 clientResponse = this.getAuthenticatedResource(getResource() 307 .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1/comments/5")) 308 .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); 309 assertEquals(403, clientResponse.getStatus()); 310 311 } 312 155 313 }
Note: See TracChangeset
for help on using the changeset viewer.