Changeset 1916
- Timestamp:
- 04/27/12 13:28:30 (12 years ago)
- Location:
- ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestServiceTest.java
r1894 r1916 1110 1110 1111 1111 @Test 1112 public void testRegisterLargeProfile() throws Exception { 1113 FormDataMultiPart form = new FormDataMultiPart(); 1114 form.field(ComponentRegistryRestService.DATA_FORM_FIELD, RegistryTestHelper.getLargeProfileContent(), 1115 MediaType.APPLICATION_OCTET_STREAM_TYPE); 1116 form.field(ComponentRegistryRestService.NAME_FORM_FIELD, "ProfileTest1"); 1117 form.field(ComponentRegistryRestService.DOMAIN_FORM_FIELD, "TestDomain"); 1118 form.field(ComponentRegistryRestService.DESCRIPTION_FORM_FIELD, "My Test Profile"); 1119 form.field(ComponentRegistryRestService.GROUP_FORM_FIELD, "My Test Group"); 1120 ClientResponse response = getAuthenticatedResource("/registry/profiles").type(MediaType.MULTIPART_FORM_DATA).post( 1121 ClientResponse.class, form); 1122 assertEquals(200, response.getStatus()); 1123 } 1124 1125 @Test 1112 1126 public void testRegisterComponentAsProfile() throws Exception { 1113 1127 FormDataMultiPart form = new FormDataMultiPart(); -
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/RegistryTestHelper.java
r1717 r1916 19 19 import clarin.cmdi.componentregistry.model.ComponentDescription; 20 20 import clarin.cmdi.componentregistry.model.ProfileDescription; 21 import java.io.BufferedReader; 22 import java.io.InputStreamReader; 21 23 22 24 /** 23 25 * Static helper methods to be used in tests 24 * 26 * 25 27 */ 26 28 public final class RegistryTestHelper { … … 30 32 31 33 public static ComponentDescription addComponent(ComponentRegistry testRegistry, String id) throws ParseException, JAXBException { 32 34 return addComponent(testRegistry, id, getComponentTestContent()); 33 35 } 34 36 35 37 public static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, String content) throws ParseException, 36 37 38 JAXBException, UnsupportedEncodingException { 39 return addComponent(testRegistry, id, new ByteArrayInputStream(content.getBytes("UTF-8"))); 38 40 } 39 41 40 42 private static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, InputStream content) throws ParseException, 41 42 43 44 45 46 47 48 49 50 51 43 JAXBException { 44 ComponentDescription desc = ComponentDescription.createNewDescription(); 45 desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName()); 46 desc.setUserId(DummyPrincipal.DUMMY_CREDENTIALS.getPrincipalName()); 47 desc.setName(id); 48 desc.setDescription("Test Description"); 49 desc.setId(ComponentRegistry.REGISTRY_ID + id); 50 desc.setHref("link:" + ComponentRegistry.REGISTRY_ID + id); 51 CMDComponentSpec spec = MDMarshaller.unmarshal(CMDComponentSpec.class, content, MDMarshaller.getCMDComponentSchema()); 52 testRegistry.register(desc, spec); 53 return desc; 52 54 } 53 55 54 56 public static int updateComponent(ComponentRegistry testRegistry, ComponentDescription description, String content) throws JAXBException { 55 57 return testRegistry.update(description, getComponentFromString(content), DummyPrincipal.DUMMY_CREDENTIALS.getPrincipal(), true); 56 58 } 57 59 58 60 public static String getProfileTestContentString() { 59 61 return getProfileTestContentString("Actor"); 60 62 } 61 63 62 64 private static String getProfileTestContentString(String name) { 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 65 String profileContent = ""; 66 profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 67 profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 68 profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; 69 profileContent += " <Header />\n"; 70 profileContent += " <CMD_Component name=\"" + name + "\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n"; 71 profileContent += " <AttributeList>\n"; 72 profileContent += " <Attribute>\n"; 73 profileContent += " <Name>Name</Name>\n"; 74 profileContent += " <Type>string</Type>\n"; 75 profileContent += " </Attribute>\n"; 76 profileContent += " </AttributeList>\n"; 77 profileContent += " <CMD_Element name=\"Age\">\n"; 78 profileContent += " <ValueScheme>\n"; 79 profileContent += " <pattern>[23][0-9]</pattern>\n"; 80 profileContent += " </ValueScheme>\n"; 81 profileContent += " </CMD_Element>\n"; 82 profileContent += " </CMD_Component>\n"; 83 profileContent += "</CMD_ComponentSpec>\n"; 84 return profileContent; 83 85 } 84 86 85 87 public static InputStream getTestProfileContent() { 86 88 return getTestProfileContent("Actor"); 87 89 } 88 90 89 91 public static InputStream getTestProfileContent(String name) { 90 92 return new ByteArrayInputStream(getProfileTestContentString(name).getBytes()); 91 93 } 92 94 93 95 public static ProfileDescription addProfile(ComponentRegistry testRegistry, String id) throws ParseException, JAXBException { 94 96 return addProfile(testRegistry, id, RegistryTestHelper.getTestProfileContent()); 95 97 } 96 98 97 99 public static ProfileDescription addProfile(ComponentRegistry testRegistry, String id, String content) throws ParseException, 98 99 100 JAXBException { 101 return addProfile(testRegistry, id, new ByteArrayInputStream(content.getBytes())); 100 102 } 101 103 102 104 private static ProfileDescription addProfile(ComponentRegistry testRegistry, String id, InputStream content) throws ParseException, 103 104 105 106 107 108 109 110 111 112 113 105 JAXBException { 106 ProfileDescription desc = ProfileDescription.createNewDescription(); 107 desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName()); 108 desc.setUserId(DummyPrincipal.DUMMY_CREDENTIALS.getPrincipalName()); 109 desc.setName(id); 110 desc.setDescription("Test Description"); 111 desc.setId(ComponentRegistry.REGISTRY_ID + id); 112 desc.setHref("link:" + ComponentRegistry.REGISTRY_ID + id); 113 CMDComponentSpec spec = MDMarshaller.unmarshal(CMDComponentSpec.class, content, MDMarshaller.getCMDComponentSchema()); 114 testRegistry.register(desc, spec); 115 return desc; 114 116 } 115 117 116 118 public static CMDComponentSpec getTestProfile() throws JAXBException { 117 119 return MDMarshaller.unmarshal(CMDComponentSpec.class, getTestProfileContent(), MDMarshaller.getCMDComponentSchema()); 118 120 } 119 121 120 122 public static String getComponentTestContentString() { 121 123 return getComponentTestContentString("Access"); 122 124 } 123 125 124 126 public static InputStream getComponentTestContent() { 125 127 return getComponentTestContent("Access"); 126 128 } 127 129 128 130 public static String getComponentTestContentString(String componentName) { 129 String compContent = ""; 130 compContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 131 compContent += "\n"; 132 compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 133 compContent += " xsi:noNamespaceSchemaLocation=\"../../general-component-schema.xsd\">\n"; 134 compContent += " \n"; 135 compContent += " <Header/>\n"; 136 compContent += " \n"; 137 compContent += " <CMD_Component name=\"" + componentName + "\" CardinalityMin=\"1\" CardinalityMax=\"1\">\n"; 138 compContent += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; 139 compContent += " <CMD_Element name=\"Date\">\n"; 140 compContent += " <ValueScheme>\n"; 141 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"; 142 compContent += " <pattern>(1|2)\\d{3}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])</pattern> \n"; 143 compContent += " </ValueScheme>\n"; 144 compContent += " </CMD_Element>\n"; 145 compContent += " <CMD_Element name=\"Owner\" ValueScheme=\"string\" />\n"; 146 compContent += " <CMD_Element name=\"Publisher\" ValueScheme=\"string\" />\n"; 147 compContent += " </CMD_Component>\n"; 148 compContent += "\n"; 149 compContent += "</CMD_ComponentSpec>\n"; 150 return compContent; 131 String compContent = ""; 132 compContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 133 compContent += "\n"; 134 compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 135 compContent += " xsi:noNamespaceSchemaLocation=\"../../general-component-schema.xsd\">\n"; 136 compContent += " \n"; 137 compContent += " <Header/>\n"; 138 compContent += " \n"; 139 compContent += " <CMD_Component name=\"" + componentName + "\" CardinalityMin=\"1\" CardinalityMax=\"1\">\n"; 140 compContent += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; 141 compContent += " <CMD_Element name=\"Date\">\n"; 142 compContent += " <ValueScheme>\n"; 143 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"; 144 compContent += " <pattern>(1|2)\\d{3}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])</pattern> \n"; 145 compContent += " </ValueScheme>\n"; 146 compContent += " </CMD_Element>\n"; 147 compContent += " <CMD_Element name=\"Owner\" ValueScheme=\"string\" />\n"; 148 compContent += " <CMD_Element name=\"Publisher\" ValueScheme=\"string\" />\n"; 149 compContent += " </CMD_Component>\n"; 150 compContent += "\n"; 151 compContent += "</CMD_ComponentSpec>\n"; 152 return compContent; 153 } 154 155 public static String getLargeProfileContent() throws IOException { 156 InputStream largeProfileStream = RegistryTestHelper.class.getResourceAsStream("/xml/largeProfile.xml"); 157 try { 158 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(largeProfileStream)); 159 StringBuilder profileStringBuilder = new StringBuilder(); 160 String line; 161 while (null != (line = bufferedReader.readLine())) { 162 profileStringBuilder.append(line); 163 } 164 return profileStringBuilder.toString(); 165 } finally { 166 largeProfileStream.close(); 167 } 151 168 } 152 169 153 170 public static CMDComponentSpec getComponentFromString(String contentString) throws JAXBException { 154 171 return MDMarshaller.unmarshal(CMDComponentSpec.class, getComponentContent(contentString), MDMarshaller.getCMDComponentSchema()); 155 172 } 156 173 157 174 public static InputStream getComponentContent(String content) { 158 175 return new ByteArrayInputStream(content.getBytes()); 159 176 } 160 177 161 178 public static InputStream getComponentTestContent(String componentName) { 162 179 return getComponentContent(getComponentTestContentString(componentName)); 163 180 } 164 181 165 182 public static CMDComponentSpec getTestComponent() throws JAXBException { 166 183 return MDMarshaller.unmarshal(CMDComponentSpec.class, getComponentTestContent(), MDMarshaller.getCMDComponentSchema()); 167 184 } 168 185 169 186 public static CMDComponentSpec getTestComponent(String name) throws JAXBException { 170 187 return MDMarshaller.unmarshal(CMDComponentSpec.class, getComponentTestContent(name), MDMarshaller.getCMDComponentSchema()); 171 188 } 172 189 173 190 public static String getXml(CMDComponentSpec componentSpec) throws JAXBException, UnsupportedEncodingException { 174 175 176 177 178 179 180 181 191 ByteArrayOutputStream os = new ByteArrayOutputStream(); 192 MDMarshaller.marshal(componentSpec, os); 193 String xml = os.toString(); 194 try { 195 os.close(); 196 } catch (IOException ex) { 197 } 198 return xml; 182 199 } 183 200 184 201 public static Comment addComment(ComponentRegistry testRegistry, String id, String descriptionId, String principal) throws ParseException, JAXBException, ComponentRegistryException { 185 202 return addComment(testRegistry, RegistryTestHelper.getTestCommentContent(id, descriptionId), principal); 186 203 } 187 204 188 205 private static Comment addComment(ComponentRegistry testRegistry, InputStream content, String principal) throws ParseException, 189 JAXBException, 190 191 192 193 206 JAXBException, 207 ComponentRegistryException { 208 Comment spec = MDMarshaller.unmarshal(Comment.class, content, null); 209 testRegistry.registerComment(spec, principal); 210 return spec; 194 211 } 195 212 196 213 public static String getCommentTestContentStringForProfile(String commentName, String profileId) { 197 198 199 200 201 202 203 204 205 214 String comContent = ""; 215 comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; 216 comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; 217 comContent += " <comments>" + commentName + "</comments>\n"; 218 comContent += " <commentDate>" + Comment.createNewDate() + "</commentDate>\n"; 219 comContent += " <profileDescriptionId>" + profileId + "</profileDescriptionId>\n"; 220 comContent += " <userName>J. Unit</userName>\n"; 221 comContent += "</comment>\n"; 222 return comContent; 206 223 } 207 224 208 225 public static String getCommentTestContentStringForComponent(String commentName, String componentId) { 209 210 211 212 213 214 215 216 217 226 String comContent = ""; 227 comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; 228 comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; 229 comContent += " <comments>" + commentName + "</comments>\n"; 230 comContent += " <commentDate>" + Comment.createNewDate() + "</commentDate>\n"; 231 comContent += " <componentDescriptionId>" + componentId + "</componentDescriptionId>"; 232 comContent += " <userName>J. Unit</userName>\n"; 233 comContent += "</comment>\n"; 234 return comContent; 218 235 } 219 236 220 237 public static InputStream getTestCommentContent(String content, String descriptionId) { 221 222 223 224 225 238 if (descriptionId.contains("profile")) { 239 return new ByteArrayInputStream(getCommentTestContentStringForProfile(content, descriptionId).getBytes()); 240 } else { 241 return new ByteArrayInputStream(getCommentTestContentStringForComponent(content, descriptionId).getBytes()); 242 } 226 243 } 227 244 228 245 public static String getCommentTestContent(String commentId, String descriptionId) { 229 246 return getCommentTestContentStringForProfile(commentId, descriptionId); 230 247 } 231 248 232 249 public static InputStream getCommentTestContent() { 233 250 return getTestCommentContent("Actual", "clarin.eu:cr1:profile1"); 234 251 } 235 252 … … 239 256 */ 240 257 public static boolean hasComponent(String xsd, String name, String min, String max) { 241 242 243 258 Pattern pattern = Pattern.compile("<xs:element name=\"" + name + "\" minOccurs=\"" + min + "\" maxOccurs=\"" + max + "\">"); 259 Matcher matcher = pattern.matcher(xsd); 260 return matcher.find() && !matcher.find(); //find only one 244 261 } 245 262 }
Note: See TracChangeset
for help on using the changeset viewer.