Changeset 6919 for ComponentRegistry/branches
- Timestamp:
- 01/19/16 13:16:54 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/branches/ComponentRegistry-2.0/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/MDValidatorTest.java
r5549 r6919 17 17 18 18 import org.junit.Before; 19 import org.junit.Ignore;20 19 import org.junit.Test; 21 20 import org.springframework.beans.factory.annotation.Autowired; 22 21 23 24 22 /** 25 * 23 * 26 24 * @author george.georgovassilis@mpi.nl 27 25 * … … 30 28 31 29 @Autowired 32 private ComponentRegistryFactory componentRegistryFactory; 30 private ComponentRegistryFactory componentRegistryFactory; 33 31 @Autowired 34 32 private ComponentRegistry testRegistry; 35 33 36 34 private RegistryUser user; 37 38 35 39 36 @Before 40 37 @Override … … 43 40 user = componentRegistryFactory.getOrCreateUser(DummyPrincipal.DUMMY_CREDENTIALS); 44 41 } 45 46 47 48 @Test 49 public void testValidateSucces() throws UserUnauthorizedException{ 50 MDValidator validator = this.getValidProfileValidator(); 42 43 @Test 44 public void testValidateSucces() throws UserUnauthorizedException { 45 MDValidator validator = this.getValidProfileValidator(); 51 46 boolean result = validator.validate(); 52 assertTrue(result); 53 } 54 55 @Test 56 public void testValidateIllegalComponentAttributeName() throws UserUnauthorizedException{ 57 String profileContent = ""; 58 profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 59 profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 60 profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; 61 profileContent += " <Header />\n"; 62 profileContent += " <CMD_Component name=\"Actor\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n"; 63 profileContent += " <AttributeList>\n"; 64 profileContent += " <Attribute>\n"; 65 profileContent += " <Name>myattribute</Name>\n"; // this should be 66 // allowed 67 profileContent += " <Type>string</Type>\n"; 68 profileContent += " </Attribute>\n"; 69 profileContent += " <Attribute>\n"; 70 profileContent += " <Name>ref</Name>\n"; // this should NOT be 71 // allowed 72 profileContent += " <Type>string</Type>\n"; 73 profileContent += " </Attribute>\n"; 74 profileContent += " <Attribute>\n"; 75 profileContent += " <Name>ComponentId</Name>\n"; // neither should 76 // this 77 profileContent += " <Type>string</Type>\n"; 78 profileContent += " </Attribute>\n"; 79 profileContent += " </AttributeList>\n"; 80 profileContent += " <CMD_Element name=\"Age\">\n"; 81 profileContent += " <AttributeList>\n"; 82 profileContent += " <Attribute>\n"; 83 profileContent += " <Name>ref</Name>\n"; // allowed here, only 84 // forbidden on components 85 profileContent += " <Type>string</Type>\n"; 86 profileContent += " </Attribute>\n"; 87 profileContent += " <Attribute>\n"; 88 profileContent += " <Name>ComponentId</Name>\n"; // allowed here, only 89 // forbidden on 90 // components 91 profileContent += " <Type>string</Type>\n"; 92 profileContent += " </Attribute>\n"; 93 profileContent += " </AttributeList>\n"; 94 profileContent += " <ValueScheme>\n"; 95 profileContent += " <pattern>[23][0-9]</pattern>\n"; 96 profileContent += " </ValueScheme>\n"; 97 profileContent += " </CMD_Element>\n"; 98 profileContent += " </CMD_Component>\n"; 99 profileContent += "</CMD_ComponentSpec>\n"; 100 InputStream input = new ByteArrayInputStream(profileContent.getBytes()); 101 102 ProfileDescription desc = ProfileDescription.createNewDescription(); 103 104 MDValidator validator = new MDValidator(input, desc, testRegistry, marshaller); 105 assertFalse(validator.validate()); 106 assertEquals(4, validator.getErrorMessages().size()); 107 assertTrue(validator.getErrorMessages().get(0) 108 .startsWith(MDValidator.PARSE_ERROR)); 109 assertTrue(validator.getErrorMessages().get(1) 110 .startsWith(MDValidator.PARSE_ERROR)); 47 assertTrue(result); 48 } 49 50 @Test 51 public void testValidateIllegalComponentAttributeName() throws UserUnauthorizedException { 52 String profileContent = ""; 53 profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 54 profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 55 profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; 56 profileContent += " <Header />\n"; 57 profileContent += " <CMD_Component name=\"Actor\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n"; 58 profileContent += " <AttributeList>\n"; 59 profileContent += " <Attribute>\n"; 60 profileContent += " <Name>myattribute</Name>\n"; // this should be 61 // allowed 62 profileContent += " <Type>string</Type>\n"; 63 profileContent += " </Attribute>\n"; 64 profileContent += " <Attribute>\n"; 65 profileContent += " <Name>ref</Name>\n"; // this should NOT be 66 // allowed 67 profileContent += " <Type>string</Type>\n"; 68 profileContent += " </Attribute>\n"; 69 profileContent += " <Attribute>\n"; 70 profileContent += " <Name>ComponentId</Name>\n"; // neither should 71 // this 72 profileContent += " <Type>string</Type>\n"; 73 profileContent += " </Attribute>\n"; 74 profileContent += " </AttributeList>\n"; 75 profileContent += " <CMD_Element name=\"Age\">\n"; 76 profileContent += " <AttributeList>\n"; 77 profileContent += " <Attribute>\n"; 78 profileContent += " <Name>ref</Name>\n"; // allowed here, only 79 // forbidden on components 80 profileContent += " <Type>string</Type>\n"; 81 profileContent += " </Attribute>\n"; 82 profileContent += " <Attribute>\n"; 83 profileContent += " <Name>ComponentId</Name>\n"; // allowed here, only 84 // forbidden on 85 // components 86 profileContent += " <Type>string</Type>\n"; 87 profileContent += " </Attribute>\n"; 88 profileContent += " </AttributeList>\n"; 89 profileContent += " <ValueScheme>\n"; 90 profileContent += " <pattern>[23][0-9]</pattern>\n"; 91 profileContent += " </ValueScheme>\n"; 92 profileContent += " </CMD_Element>\n"; 93 profileContent += " </CMD_Component>\n"; 94 profileContent += "</CMD_ComponentSpec>\n"; 95 InputStream input = new ByteArrayInputStream(profileContent.getBytes()); 96 97 ProfileDescription desc = ProfileDescription.createNewDescription(); 98 99 MDValidator validator = new MDValidator(input, desc, testRegistry, marshaller); 100 assertFalse(validator.validate()); 101 assertEquals("Messages expected for 2 failed tests:\n" 102 + "- Actor/@ref ('ref' attribute not allowed on component)\n" 103 + "- Actor/@ComponentId ('ComponentId' attribute not allowed on component)", 104 2, validator.getErrorMessages().size()); 105 assertTrue(validator.getErrorMessages().get(0) 106 .startsWith(MDValidator.PARSE_ERROR)); 107 assertTrue(validator.getErrorMessages().get(1) 108 .startsWith(MDValidator.PARSE_ERROR)); 111 109 } 112 110 113 111 @Test 114 112 public void testValidateNoComponentId() throws Exception { 115 116 117 118 119 120 121 122 123 124 113 String profileContent = ""; 114 profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 115 profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 116 profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; 117 profileContent += " <Header />\n"; 118 profileContent += " <CMD_Component filename=\"component-actor.xml\"/>\n"; 119 profileContent += "</CMD_ComponentSpec>\n"; 120 InputStream input = new ByteArrayInputStream(profileContent.getBytes()); 121 122 ProfileDescription desc = ProfileDescription.createNewDescription(); 125 123 desc.setPublic(true); 126 127 128 129 130 131 124 MDValidator validator = new MDValidator(input, desc, testRegistry, marshaller); 125 assertFalse(validator.validate()); 126 assertTrue(validator 127 .getErrorMessages() 128 .get(0) 129 .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR)); 132 130 // 133 131 } 134 132 135 @Test 133 @Test 136 134 public void testValidateComponentIdNotRegistered() throws Exception { 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 135 String id1 = "component1"; 136 String id2 = "component2"; 137 138 String profileContent = ""; 139 profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; 140 profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 141 profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">"; 142 profileContent += " <Header />"; 143 profileContent += " <CMD_Component name=\"Test\">"; 144 profileContent += " <CMD_Component ComponentId=\"" 145 + ComponentDescription.COMPONENT_PREFIX + id1 + "\"/>"; // id not 146 // registered 147 profileContent += " <CMD_Component ComponentId=\"" 148 + ComponentDescription.COMPONENT_PREFIX + id2 + "\"/>"; // id not 149 // registered 150 profileContent += " </CMD_Component>"; 151 profileContent += "</CMD_ComponentSpec>"; 152 153 // Ids not registered. will return 2 errors: one per each id. 154 ProfileDescription desc = ProfileDescription.createNewDescription(); 157 155 desc.setPublic(true); 158 MDValidator validator = new MDValidator(new ByteArrayInputStream( 159 profileContent.getBytes()), desc, testRegistry, marshaller); 160 assertFalse(validator.validate()); 161 assertEquals(1, validator.getErrorMessages().size()); 162 assertTrue(validator 163 .getErrorMessages() 164 .get(0) 165 .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR)); 166 167 168 // id1 will be added as public and therefore only id2 is not registered 169 RegistryTestHelper.addComponent(testRegistry, id1, true); 170 validator = new MDValidator(new ByteArrayInputStream( 171 profileContent.getBytes()), desc, testRegistry 172 , marshaller); 173 assertFalse(validator.validate()); 174 assertEquals(1, validator.getErrorMessages().size()); 175 assertTrue(validator 176 .getErrorMessages() 177 .get(0) 178 .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR)); 179 180 // id2 is added as public, no more errors shoud be return 181 RegistryTestHelper.addComponent(testRegistry, id2, true); 182 validator = new MDValidator(new ByteArrayInputStream( 183 profileContent.getBytes()), desc, testRegistry, marshaller); 184 assertTrue("component is registered should be valid now", 185 validator.validate()); 186 assertEquals(0, validator.getErrorMessages().size()); 156 MDValidator validator = new MDValidator(new ByteArrayInputStream( 157 profileContent.getBytes()), desc, testRegistry, marshaller); 158 assertFalse(validator.validate()); 159 assertEquals(1, validator.getErrorMessages().size()); 160 assertTrue(validator 161 .getErrorMessages() 162 .get(0) 163 .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR)); 164 165 // id1 will be added as public and therefore only id2 is not registered 166 RegistryTestHelper.addComponent(testRegistry, id1, true); 167 validator = new MDValidator(new ByteArrayInputStream( 168 profileContent.getBytes()), desc, testRegistry, marshaller); 169 assertFalse(validator.validate()); 170 assertEquals(1, validator.getErrorMessages().size()); 171 assertTrue(validator 172 .getErrorMessages() 173 .get(0) 174 .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR)); 175 176 // id2 is added as public, no more errors shoud be return 177 RegistryTestHelper.addComponent(testRegistry, id2, true); 178 validator = new MDValidator(new ByteArrayInputStream( 179 profileContent.getBytes()), desc, testRegistry, marshaller); 180 assertTrue("component is registered should be valid now", 181 validator.validate()); 182 assertEquals(0, validator.getErrorMessages().size()); 187 183 } 188 184 189 185 @Test 190 186 public void testValidateBaseAndUserRegistry() throws Exception { 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 187 String id1 = "component1"; 188 String id2 = "component2"; 189 190 String profileContent = ""; 191 profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; 192 profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""; 193 profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">"; 194 profileContent += " <Header />"; 195 profileContent += " <CMD_Component name=\"Test\">"; 196 profileContent += " <CMD_Component ComponentId=\"" 197 + ComponentDescription.COMPONENT_PREFIX + id1 + "\"/>"; // id not 198 // registered 199 profileContent += " <CMD_Component ComponentId=\"" 200 + ComponentDescription.COMPONENT_PREFIX + id2 + "\"/>"; // id not 201 // registered 202 profileContent += " </CMD_Component>"; 203 profileContent += "</CMD_ComponentSpec>"; 208 204 209 205 // Public Registry 210 ProfileDescription desc = ProfileDescription.createNewDescription(); 211 MDValidator validator = new MDValidator(new ByteArrayInputStream( 212 profileContent.getBytes()), desc, testRegistry, marshaller); 213 assertFalse(validator.validate()); 214 assertEquals(1, validator.getErrorMessages().size()); // the exception is thrown and propagated on the first non-registered component 215 assertTrue(validator.getErrorMessages().get(0) 216 .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR)); 217 218 206 ProfileDescription desc = ProfileDescription.createNewDescription(); 207 MDValidator validator = new MDValidator(new ByteArrayInputStream( 208 profileContent.getBytes()), desc, testRegistry, marshaller); 209 assertFalse(validator.validate()); 210 assertEquals(1, validator.getErrorMessages().size()); // the exception is thrown and propagated on the first non-registered component 211 assertTrue(validator.getErrorMessages().get(0) 212 .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR)); 213 219 214 // registering publically the first component 220 RegistryTestHelper.addComponent(testRegistry, id1, true); 221 validator = new MDValidator(new ByteArrayInputStream( 222 profileContent.getBytes()), desc, testRegistry, marshaller); 223 assertFalse(validator.validate()); 224 assertEquals(1, validator.getErrorMessages().size()); // the exception is thrown on the second non-registered component 225 assertTrue(validator.getErrorMessages().get(0) 226 .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR)); 227 228 229 230 RegistryTestHelper.addComponent(testRegistry, id2, false); 231 validator = new MDValidator(new ByteArrayInputStream( 232 profileContent.getBytes()), desc, testRegistry, marshaller); 233 assertFalse(validator.validate()); 234 assertEquals(1, validator.getErrorMessages().size()); 235 assertTrue(validator 236 .getErrorMessages() 237 .get(0) 238 .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_IN_APPROPRIATE_SPACE_ERROR)); 239 240 215 RegistryTestHelper.addComponent(testRegistry, id1, true); 216 validator = new MDValidator(new ByteArrayInputStream( 217 profileContent.getBytes()), desc, testRegistry, marshaller); 218 assertFalse(validator.validate()); 219 assertEquals(1, validator.getErrorMessages().size()); // the exception is thrown on the second non-registered component 220 assertTrue(validator.getErrorMessages().get(0) 221 .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR)); 222 223 RegistryTestHelper.addComponent(testRegistry, id2, false); 224 validator = new MDValidator(new ByteArrayInputStream( 225 profileContent.getBytes()), desc, testRegistry, marshaller); 226 assertFalse(validator.validate()); 227 assertEquals(1, validator.getErrorMessages().size()); 228 assertTrue(validator 229 .getErrorMessages() 230 .get(0) 231 .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_IN_APPROPRIATE_SPACE_ERROR)); 232 241 233 // make it user registry 242 234 testRegistry.setRegistryOwner(new OwnerUser(user.getId())); 243 testRegistry.setRegistrySpace(RegistrySpace.PRIVATE); 244 245 246 validator = new MDValidator(new ByteArrayInputStream( 247 profileContent.getBytes()), desc, testRegistry, marshaller); 248 Boolean result =validator.validate(); 249 assertTrue(result); 250 assertEquals(0, validator.getErrorMessages().size()); 235 testRegistry.setRegistrySpace(RegistrySpace.PRIVATE); 236 237 validator = new MDValidator(new ByteArrayInputStream( 238 profileContent.getBytes()), desc, testRegistry, marshaller); 239 Boolean result = validator.validate(); 240 assertTrue(result); 241 assertEquals(0, validator.getErrorMessages().size()); 251 242 } 252 243 253 244 @Test 254 245 public void testValidateNestedComponents() throws Exception { 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 new ByteArrayInputStream(content.getBytes()), desc,testRegistry, marshaller);283 284 246 String id1 = "component1"; 247 248 String content = ""; 249 content += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 250 content += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 251 content += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; 252 content += " <Header />\n"; 253 content += " <CMD_Component name=\"Actor\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n"; 254 content += " <CMD_Element name=\"Name\" ValueScheme=\"string\" />\n"; 255 content += " <CMD_Component ComponentId=\"" 256 + ComponentDescription.COMPONENT_PREFIX + id1 + "\"/>\n"; // id not 257 // registered 258 content += " </CMD_Component>\n"; 259 content += "</CMD_ComponentSpec>\n"; 260 261 ComponentDescription desc = ComponentDescription.createNewDescription(); 262 MDValidator validator = new MDValidator(new ByteArrayInputStream( 263 content.getBytes()), desc, testRegistry, marshaller); 264 assertFalse(validator.validate()); 265 assertEquals(1, validator.getErrorMessages().size()); 266 assertTrue(validator 267 .getErrorMessages() 268 .get(0) 269 .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR)); 270 271 RegistryTestHelper.addComponent(testRegistry, id1, true); 272 validator = new MDValidator( 273 new ByteArrayInputStream(content.getBytes()), desc, testRegistry, marshaller); 274 assertTrue(validator.validate()); 275 assertEquals(0, validator.getErrorMessages().size()); 285 276 } 286 277 … … 290 281 @Test 291 282 public void testGetCMDComponentSpec() throws Exception { 292 293 294 295 296 MDValidator validator = new MDValidator(input, desc, testRegistry,marshaller);297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 283 String profileContent = getValidProfileString(); 284 InputStream input = new ByteArrayInputStream(profileContent.getBytes()); 285 286 ProfileDescription desc = ProfileDescription.createNewDescription(); 287 MDValidator validator = new MDValidator(input, desc, testRegistry, marshaller); 288 289 // Spec is created during validation, before it should be null 290 assertNull(validator.getCMDComponentSpec()); 291 validator.validate(); 292 293 // Get spec created during validation 294 final CMDComponentSpec cmdComponentSpec = validator 295 .getCMDComponentSpec(); 296 assertNotNull(cmdComponentSpec); 297 298 // Spec content should match XML 299 assertTrue(cmdComponentSpec.isIsProfile()); 300 assertEquals("Actor", cmdComponentSpec.getCMDComponent().getName()); 301 302 // Spec copy should be a freshly unmarshalled copy 303 final CMDComponentSpec specCopy = validator.getCopyOfCMDComponentSpec(); 304 assertNotSame(cmdComponentSpec, specCopy); 305 306 // Content should still match XML 307 assertTrue(specCopy.isIsProfile()); 308 assertEquals("Actor", specCopy.getCMDComponent().getName()); 318 309 } 319 310 320 311 private MDValidator getValidProfileValidator() { 321 322 323 312 final String profileContent = getValidProfileString(); 313 InputStream input = new ByteArrayInputStream(profileContent.getBytes()); 314 ProfileDescription desc = ProfileDescription.createNewDescription(); 324 315 desc.setPublic(true); 325 316 desc.setName("test_name"); 326 327 317 MDValidator validator = new MDValidator(input, desc, testRegistry, marshaller); 318 return validator; 328 319 } 329 320 330 321 private String getValidProfileString() { 331 332 333 334 335 336 337 338 339 340 341 342 343 344 322 String profileContent = ""; 323 profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 324 profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 325 profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; 326 profileContent += " <Header />\n"; 327 profileContent += " <CMD_Component name=\"Actor\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n"; 328 profileContent += " <CMD_Element name=\"Age\">\n"; 329 profileContent += " <ValueScheme>\n"; 330 profileContent += " <pattern>[23][0-9]</pattern>\n"; 331 profileContent += " </ValueScheme>\n"; 332 profileContent += " </CMD_Element>\n"; 333 profileContent += " </CMD_Component>\n"; 334 profileContent += "</CMD_ComponentSpec>\n"; 335 return profileContent; 345 336 } 346 337 }
Note: See TracChangeset
for help on using the changeset viewer.