Changeset 1337
- Timestamp:
- 05/18/11 13:41:18 (13 years ago)
- Location:
- ComponentRegistry/trunk/ComponentRegistry/src
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/AbstractDescriptionDao.java
r1335 r1337 1 1 package clarin.cmdi.componentregistry.impl.database; 2 2 3 import java.text.ParseException; 3 4 import org.slf4j.LoggerFactory; 4 5 import org.slf4j.Logger; … … 12 13 import org.springframework.jdbc.core.simple.SimpleJdbcInsert; 13 14 import clarin.cmdi.componentregistry.model.AbstractDescription; 15 import java.sql.Timestamp; 16 import java.text.DateFormat; 17 import java.text.SimpleDateFormat; 18 import java.util.Date; 14 19 import org.springframework.dao.DataAccessException; 15 20 import org.springframework.jdbc.core.simple.ParameterizedSingleColumnRowMapper; … … 23 28 private final static Logger LOG = LoggerFactory.getLogger( 24 29 AbstractDescriptionDao.class); 30 private final static DateFormat ISO_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz"); 25 31 26 32 protected abstract String getTableName(); … … 79 85 params.put("name", description.getName()); 80 86 params.put("description", description.getDescription()); 87 params.put("creator_name", description.getCreatorName()); 88 params.put("group_name", description.getGroupName()); 89 params.put("domain_name", description.getDomainName()); 90 params.put("registration_date", extractTimestamp(description)); 81 91 return insertDescription.executeAndReturnKey(params); 92 } 93 94 private Timestamp extractTimestamp(AbstractDescription description) { 95 if (description.getRegistrationDate() != null) { 96 try { 97 Date date = AbstractDescription.getDate(description. 98 getRegistrationDate()); 99 return new Timestamp(date.getTime()); 100 } catch (ParseException ex) { 101 LOG.warn("Could not convert registration date " + description. 102 getRegistrationDate() + " to date", ex); 103 } catch (IllegalArgumentException ex) { 104 LOG.warn("Could not convert registration date " + description. 105 getRegistrationDate() + " to timestamp", ex); 106 } 107 } 108 return null; 82 109 } 83 110 … … 136 163 */ 137 164 public Number getDbId(String cmdId) { 138 String query = "SELECT " + COLUMN_ID + " FROM " + getTableName() + " WHERE " + getCMDIdColumn() + " = ?"; 165 // Check for is_deleted is important, because an id only has to be unique 166 // among non-deleted descriptions 167 String query = "SELECT " + COLUMN_ID + " FROM " + getTableName() + " WHERE is_deleted = false AND " + getCMDIdColumn() + " = ?"; 139 168 return getSimpleJdbcTemplate().queryForInt(query, cmdId); 140 169 } … … 213 242 private StringBuilder getDescriptionColumnList() { 214 243 StringBuilder sb = new StringBuilder(); 215 sb.append("name,description, ");244 sb.append("name,description,registration_date,creator_name,domain_name,group_name,"); 216 245 sb.append(getCMDIdColumn()); 217 246 return sb; … … 230 259 public T mapRow(ResultSet rs, int rowNumber) throws SQLException { 231 260 try { 261 Timestamp registrationDate = rs.getTimestamp("registration_date"); 262 232 263 AbstractDescription newDescription = (AbstractDescription) _class. 233 264 newInstance(); … … 235 266 newDescription.setDescription(rs.getString("description")); 236 267 newDescription.setId(rs.getString(getCMDIdColumn())); 268 newDescription.setRegistrationDate(registrationDate == null 269 ? null : ISO_DATE_FORMAT.format(registrationDate)); 270 newDescription.setCreatorName(rs.getString("creator_name")); 271 newDescription.setDomainName(rs.getString("domain_name")); 272 newDescription.setGroupName(rs.getString("group_name")); 237 273 return (T) newDescription; 238 274 } catch (InstantiationException ex) { -
ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImpl.java
r1335 r1337 355 355 if (isPublic() && !configuration.isAdminUser(principal)) { 356 356 try { 357 Date regDate = AbstractDescription.getDate(desc. 358 getRegistrationDate()); 357 Date regDate = AbstractDescription.getDate(desc.getRegistrationDate()); 359 358 Calendar calendar = Calendar.getInstance(); 360 359 calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); -
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/AbstractDescriptionDaoTest.java
r1332 r1337 3 3 import clarin.cmdi.componentregistry.model.AbstractDescription; 4 4 import clarin.cmdi.componentregistry.rest.RegistryTestHelper; 5 import java.text.DateFormat; 6 import java.util.Date; 5 7 import java.util.List; 8 import org.apache.commons.lang.time.DateFormatUtils; 9 import org.apache.commons.lang.time.DateUtils; 6 10 import org.junit.runner.RunWith; 7 11 import org.junit.Test; … … 35 39 @Test 36 40 public void testInsertComponent() throws Exception { 41 String regDate = AbstractDescription.createNewDate(); 42 37 43 AbstractDescription description = createNewDescription(); 38 description.setName(" Aap");44 description.setName("MyComponent"); 39 45 description.setDescription("MyDescription"); 46 description.setCreatorName("Aap"); 47 description.setGroupName("MyGroup"); 48 description.setDomainName("MyDomain"); 49 50 description.setRegistrationDate(regDate); 40 51 41 52 String testComponent = RegistryTestHelper.getComponentTestContentString(); … … 44 55 AbstractDescription descr = getDao().getById(newId); 45 56 assertNotNull(descr); 46 assertEquals(" Aap", descr.getName());57 assertEquals("MyComponent", descr.getName()); 47 58 assertEquals("MyDescription", descr.getDescription()); 59 assertEquals("Aap", descr.getCreatorName()); 60 assertEquals("MyGroup", descr.getGroupName()); 61 assertEquals("MyDomain", descr.getDomainName()); 62 assertEquals(AbstractDescription.getDate(regDate), AbstractDescription.getDate(descr.getRegistrationDate())); 48 63 assertEquals(testComponent, getDao().getContent(description.getId())); 49 64 } -
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDatabase.java
r1306 r1337 28 28 + " registration_date timestamp,"// with timezone," 29 29 + " creator_name character varying," 30 + " domain_name character varying);"); 30 + " domain_name character varying," 31 + " group_name character varying);"); 31 32 } 32 33 … … 43 44 + " registration_date timestamp,"// with timezone," 44 45 + " creator_name character varying," 45 + " domain_name character varying);"); 46 + " domain_name character varying," 47 + " group_name character varying);"); 46 48 } 47 49 -
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImplTest.java
r1331 r1337 12 12 import clarin.cmdi.componentregistry.model.UserMapping.User; 13 13 import clarin.cmdi.componentregistry.rest.DummyPrincipal; 14 import java.io.ByteArrayOutputStream; 14 15 import java.io.IOException; 15 import java.security.Principal; 16 import java.io.OutputStream; 17 import java.util.Calendar; 16 18 import javax.xml.bind.JAXBException; 19 import org.apache.commons.lang.time.DateFormatUtils; 17 20 18 21 import org.junit.Test; … … 22 25 import static org.junit.Assert.assertNull; 23 26 import static org.junit.Assert.assertNotNull; 27 import static org.junit.Assert.assertTrue; 24 28 import static org.junit.Assert.fail; 25 29 import org.springframework.beans.factory.annotation.Autowired; … … 174 178 } catch (UserUnauthorizedException e) { 175 179 } 176 180 177 181 assertEquals(1, register.getProfileDescriptions().size()); 178 182 assertNotNull(register.getMDProfile(description.getId())); … … 209 213 } 210 214 215 @Test 216 public void testDoNotDeleteOldPublicComponent() throws Exception { 217 Number userId = userDao.insertUser(createUser()); 218 ComponentRegistry registry = getComponentRegistryForUser(userId); 219 220 ComponentDescription description = ComponentDescription. 221 createNewDescription(); 222 description.setName("Aap"); 223 description.setCreatorName(USER_CREDS.getDisplayName()); 224 description.setUserId(userId.toString()); 225 description.setDescription("MyDescription"); 226 Calendar calendar = Calendar.getInstance(); 227 calendar.set(Calendar.YEAR, 1999); 228 description.setRegistrationDate(DateFormatUtils.formatUTC(calendar. 229 getTime(), DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT. 230 getPattern())); 231 CMDComponentSpec testComp = RegistryTestHelper.getTestComponent(); 232 233 registry.register(description, testComp); 234 registry.publish(description, testComp, USER_CREDS.getPrincipal()); 235 236 // Switch to public registry 237 registry = getComponentRegistryForUser(null); 238 239 try { 240 registry.deleteMDComponent(description.getId(), USER_CREDS. 241 getPrincipal(), false); 242 fail("Should have thrown exception"); 243 } catch (DeleteFailedException e) { 244 } 245 assertEquals(1, registry.getComponentDescriptions().size()); 246 registry.deleteMDComponent(description.getId(), PRINCIPAL_ADMIN, false); 247 assertEquals(0, registry.getComponentDescriptions().size()); 248 249 //TODO: convert below to dbImpl 250 registry = getComponentRegistryForUser(userId); // ComponentRegistryFactoryImpl.getInstance().getComponentRegistry(true, USER_CREDS); //user registry 251 registry.register(description, testComp); 252 assertEquals(1, registry.getComponentDescriptions().size()); 253 registry.deleteMDComponent(description.getId(), USER_CREDS.getPrincipal(), false); //user workspace can always delete 254 assertEquals(0, registry.getComponentDescriptions().size()); 255 } 256 257 @Test 258 public void testDoNotDeleteOldPublicProfile() throws Exception { 259 Number userId = userDao.insertUser(createUser()); 260 ComponentRegistry registry = getComponentRegistryForUser(userId); 261 262 ProfileDescription description = ProfileDescription.createNewDescription(); 263 description.setName("Aap"); 264 description.setCreatorName(USER_CREDS.getDisplayName()); 265 description.setUserId(userId.toString()); 266 description.setDescription("MyDescription"); 267 Calendar calendar = Calendar.getInstance(); 268 calendar.set(Calendar.YEAR, 1999); 269 description.setRegistrationDate(DateFormatUtils.formatUTC(calendar. 270 getTime(), DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT. 271 getPattern())); 272 CMDComponentSpec testComp = RegistryTestHelper.getTestProfile(); 273 274 registry.register(description, testComp); 275 registry.publish(description, testComp, USER_CREDS.getPrincipal()); 276 277 // Switch to public registry 278 registry = getComponentRegistryForUser(null); 279 try { 280 registry.deleteMDProfile(description.getId(), USER_CREDS. 281 getPrincipal()); 282 fail("Should have thrown exception"); 283 } catch (DeleteFailedException e) { 284 } 285 assertEquals(1, registry.getProfileDescriptions().size()); 286 registry.deleteMDProfile(description.getId(), PRINCIPAL_ADMIN); 287 assertEquals(0, registry.getProfileDescriptions().size()); 288 289 //TODO: convert below to dbImpl 290 // registry = ComponentRegistryFactoryImpl.getInstance().getComponentRegistry(true, USER_CREDS); //user registry 291 registry = getComponentRegistryForUser(userId); 292 registry.register(description, testComp); 293 assertEquals(1, registry.getProfileDescriptions().size()); 294 registry.deleteMDProfile(description.getId(), USER_CREDS.getPrincipal()); //user workspace can always delete 295 assertEquals(0, registry.getProfileDescriptions().size()); 296 } 297 211 298 private ComponentDescription createComponent(ComponentRegistry registry) throws IOException, DeleteFailedException, JAXBException { 212 299 ComponentDescription description = ComponentDescription. … … 226 313 } catch (UserUnauthorizedException e) { 227 314 } 228 315 229 316 assertEquals(1, registry.getComponentDescriptions().size()); 230 317 assertNotNull(registry.getMDComponent(description.getId())); … … 245 332 return user; 246 333 } 334 335 @Test 336 public void testGetProfileAsXsd() throws Exception { 337 ComponentRegistry register = getComponentRegistryForUser(null); 338 String profileContent = ""; 339 profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 340 profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; 341 profileContent += " <Header />\n"; 342 profileContent += " <CMD_Component name=\"Actor\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n"; 343 profileContent += " <CMD_Element name=\"Age\">\n"; 344 profileContent += " <ValueScheme>\n"; 345 profileContent += " <pattern>[0-9][0-9]</pattern>\n"; 346 profileContent += " </ValueScheme>\n"; 347 profileContent += " </CMD_Element>\n"; 348 profileContent += " </CMD_Component>\n"; 349 profileContent += "</CMD_ComponentSpec>\n"; 350 351 String id = "profile1"; 352 ProfileDescription description = RegistryTestHelper.addProfile(register, id, profileContent); 353 354 OutputStream output = new ByteArrayOutputStream(); 355 register.getMDProfileAsXsd(description.getId(), output); 356 String xsd = output.toString(); 357 assertTrue(xsd.endsWith("</xs:schema>")); 358 359 assertTrue(RegistryTestHelper.hasComponent(xsd, "Actor", "0", "unbounded")); 360 } 361 362 @Test 363 public void testGetNestedComponentAsXsd() throws Exception { 364 ComponentRegistry register = getComponentRegistryForUser(null); 365 366 String compId = "component1"; 367 String compContent = ""; 368 compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 369 compContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; 370 compContent += " <Header/>\n"; 371 compContent += " <CMD_Component name=\"Actor\" CardinalityMin=\"1\" CardinalityMax=\"10\">\n"; 372 compContent += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; 373 compContent += " </CMD_Component>\n"; 374 compContent += "</CMD_ComponentSpec>\n"; 375 376 ComponentDescription compDesc = RegistryTestHelper.addComponent(register, compId, compContent); 377 378 String profileContent = ""; 379 profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 380 profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; 381 profileContent += " <Header />\n"; 382 profileContent += " <CMD_Component ComponentId=\"" + compDesc.getId() 383 + "\" filename=\"component-test-file\" CardinalityMin=\"0\" CardinalityMax=\"5\">\n"; 384 profileContent += " </CMD_Component>\n"; 385 profileContent += "</CMD_ComponentSpec>\n"; 386 387 String id = "profile1"; 388 ProfileDescription description = RegistryTestHelper.addProfile(register, id, profileContent); 389 390 OutputStream output = new ByteArrayOutputStream(); 391 register.getMDProfileAsXsd(description.getId(), output); 392 String xsd = output.toString(); 393 394 assertTrue(xsd.endsWith("</xs:schema>")); 395 assertTrue(RegistryTestHelper.hasComponent(xsd, "Actor", "0", "5")); 396 } 397 398 @Test 399 public void testGetNestedComponentAsXsdComplex() throws Exception { 400 ComponentRegistry register = getComponentRegistryForUser(null); 401 402 String compContent = ""; 403 compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 404 compContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; 405 compContent += " <Header/>\n"; 406 compContent += " <CMD_Component name=\"XXX\" CardinalityMin=\"1\" CardinalityMax=\"10\">\n"; 407 compContent += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; 408 compContent += " </CMD_Component>\n"; 409 compContent += "</CMD_ComponentSpec>\n"; 410 ComponentDescription compDesc1 = RegistryTestHelper.addComponent(register, "component1", compContent); 411 412 compContent = ""; 413 compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 414 compContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; 415 compContent += " <Header/>\n"; 416 compContent += " <CMD_Component name=\"YYY\" CardinalityMin=\"1\" CardinalityMax=\"1\">\n"; 417 compContent += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; 418 compContent += " <CMD_Component ComponentId=\"" + compDesc1.getId() + "\" filename=\"component-test-file\">\n"; 419 compContent += " </CMD_Component>\n"; 420 compContent += " </CMD_Component>\n"; 421 compContent += "</CMD_ComponentSpec>\n"; 422 ComponentDescription compDesc2 = RegistryTestHelper.addComponent(register, "component2", compContent); 423 424 compContent = ""; 425 compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; 426 compContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; 427 compContent += " <Header/>\n"; 428 compContent += " <CMD_Component name=\"ZZZ\u00e9\" CardinalityMin=\"1\" CardinalityMax=\"unbounded\">\n"; 429 compContent += " <CMD_Component ComponentId=\"" + compDesc2.getId() 430 + "\" filename=\"component-test-file\" CardinalityMin=\"0\" CardinalityMax=\"2\">\n"; 431 compContent += " </CMD_Component>\n"; 432 compContent += " <CMD_Component ComponentId=\"" + compDesc1.getId() 433 + "\" filename=\"component-test-file\" CardinalityMin=\"0\" CardinalityMax=\"99\">\n"; 434 compContent += " </CMD_Component>\n"; 435 compContent += " </CMD_Component>\n"; 436 compContent += "</CMD_ComponentSpec>\n"; 437 ComponentDescription compDesc3 = RegistryTestHelper.addComponent(register, "component3", compContent); 438 439 ByteArrayOutputStream output = new ByteArrayOutputStream(); 440 register.getMDComponentAsXsd(compDesc3.getId(), output); 441 String xsd = output.toString("UTF-8"); 442 443 assertTrue(xsd.endsWith("</xs:schema>")); 444 // System.out.println(xsd); 445 446 assertTrue(RegistryTestHelper.hasComponent(xsd, "ZZZ\u00e9", "1", "unbounded")); 447 assertTrue(RegistryTestHelper.hasComponent(xsd, "YYY", "0", "2")); 448 assertTrue(RegistryTestHelper.hasComponent(xsd, "XXX", "1", "10")); 449 assertTrue(RegistryTestHelper.hasComponent(xsd, "XXX", "0", "99")); 450 } 247 451 } -
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/filesystem/ComponentRegistryImplTest.java
r1288 r1337 234 234 assertTrue(xsd.endsWith("</xs:schema>")); 235 235 236 assertTrue( hasComponent(xsd, "Actor", "0", "unbounded"));236 assertTrue(RegistryTestHelper.hasComponent(xsd, "Actor", "0", "unbounded")); 237 237 } 238 238 … … 270 270 271 271 assertTrue(xsd.endsWith("</xs:schema>")); 272 assertTrue( hasComponent(xsd, "Actor", "0", "5"));272 assertTrue(RegistryTestHelper.hasComponent(xsd, "Actor", "0", "5")); 273 273 } 274 274 … … 321 321 // System.out.println(xsd); 322 322 323 assertTrue( hasComponent(xsd, "ZZZ\u00e9", "1", "unbounded"));324 assertTrue( hasComponent(xsd, "YYY", "0", "2"));325 assertTrue( hasComponent(xsd, "XXX", "1", "10"));326 assertTrue( hasComponent(xsd, "XXX", "0", "99"));323 assertTrue(RegistryTestHelper.hasComponent(xsd, "ZZZ\u00e9", "1", "unbounded")); 324 assertTrue(RegistryTestHelper.hasComponent(xsd, "YYY", "0", "2")); 325 assertTrue(RegistryTestHelper.hasComponent(xsd, "XXX", "1", "10")); 326 assertTrue(RegistryTestHelper.hasComponent(xsd, "XXX", "0", "99")); 327 327 } 328 328 … … 520 520 assertEquals(2, result.size()); 521 521 } 522 523 /**524 * Testing a big xsd string is a bit hard, so doing a best effort by checking the xs:element which represent the nested components used525 * in a profile/component526 */527 private boolean hasComponent(String xsd, String name, String min, String max) {528 Pattern pattern = Pattern.compile("<xs:element name=\"" + name + "\" minOccurs=\"" + min + "\" maxOccurs=\"" + max + "\">");529 Matcher matcher = pattern.matcher(xsd);530 return matcher.find() && !matcher.find(); //find only one531 }532 522 } -
ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/RegistryTestHelper.java
r1298 r1337 17 17 import clarin.cmdi.componentregistry.model.ComponentDescription; 18 18 import clarin.cmdi.componentregistry.model.ProfileDescription; 19 import java.util.regex.Matcher; 20 import java.util.regex.Pattern; 19 21 20 22 /** … … 146 148 return MDMarshaller.unmarshal(CMDComponentSpec.class, getComponentTestContent(), MDMarshaller.getCMDComponentSchema()); 147 149 } 150 151 152 153 /** 154 * 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 155 * in a profile/component 156 */ 157 public static boolean hasComponent(String xsd, String name, String min, String max) { 158 Pattern pattern = Pattern.compile("<xs:element name=\"" + name + "\" minOccurs=\"" + min + "\" maxOccurs=\"" + max + "\">"); 159 Matcher matcher = pattern.matcher(xsd); 160 return matcher.find() && !matcher.find(); //find only one 161 } 148 162 }
Note: See TracChangeset
for help on using the changeset viewer.