Changeset 1337


Ignore:
Timestamp:
05/18/11 13:41:18 (13 years ago)
Author:
twagoo
Message:

All columns in AbstractDescriptionDao?
Tests reflect this. Also added tests for testDoNotDeleteOld...

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  
    11package clarin.cmdi.componentregistry.impl.database;
    22
     3import java.text.ParseException;
    34import org.slf4j.LoggerFactory;
    45import org.slf4j.Logger;
     
    1213import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
    1314import clarin.cmdi.componentregistry.model.AbstractDescription;
     15import java.sql.Timestamp;
     16import java.text.DateFormat;
     17import java.text.SimpleDateFormat;
     18import java.util.Date;
    1419import org.springframework.dao.DataAccessException;
    1520import org.springframework.jdbc.core.simple.ParameterizedSingleColumnRowMapper;
     
    2328    private final static Logger LOG = LoggerFactory.getLogger(
    2429            AbstractDescriptionDao.class);
     30    private final static DateFormat ISO_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
    2531
    2632    protected abstract String getTableName();
     
    7985        params.put("name", description.getName());
    8086        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));
    8191        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;
    82109    }
    83110
     
    136163     */
    137164    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() + " = ?";
    139168        return getSimpleJdbcTemplate().queryForInt(query, cmdId);
    140169    }
     
    213242    private StringBuilder getDescriptionColumnList() {
    214243        StringBuilder sb = new StringBuilder();
    215         sb.append("name,description,");
     244        sb.append("name,description,registration_date,creator_name,domain_name,group_name,");
    216245        sb.append(getCMDIdColumn());
    217246        return sb;
     
    230259        public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
    231260            try {
     261                Timestamp registrationDate = rs.getTimestamp("registration_date");
     262
    232263                AbstractDescription newDescription = (AbstractDescription) _class.
    233264                        newInstance();
     
    235266                newDescription.setDescription(rs.getString("description"));
    236267                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"));
    237273                return (T) newDescription;
    238274            } catch (InstantiationException ex) {
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImpl.java

    r1335 r1337  
    355355        if (isPublic() && !configuration.isAdminUser(principal)) {
    356356            try {
    357                 Date regDate = AbstractDescription.getDate(desc.
    358                         getRegistrationDate());
     357                Date regDate = AbstractDescription.getDate(desc.getRegistrationDate());
    359358                Calendar calendar = Calendar.getInstance();
    360359                calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/AbstractDescriptionDaoTest.java

    r1332 r1337  
    33import clarin.cmdi.componentregistry.model.AbstractDescription;
    44import clarin.cmdi.componentregistry.rest.RegistryTestHelper;
     5import java.text.DateFormat;
     6import java.util.Date;
    57import java.util.List;
     8import org.apache.commons.lang.time.DateFormatUtils;
     9import org.apache.commons.lang.time.DateUtils;
    610import org.junit.runner.RunWith;
    711import org.junit.Test;
     
    3539    @Test
    3640    public void testInsertComponent() throws Exception {
     41        String regDate = AbstractDescription.createNewDate();
     42
    3743        AbstractDescription description = createNewDescription();
    38         description.setName("Aap");
     44        description.setName("MyComponent");
    3945        description.setDescription("MyDescription");
     46        description.setCreatorName("Aap");
     47        description.setGroupName("MyGroup");
     48        description.setDomainName("MyDomain");
     49
     50        description.setRegistrationDate(regDate);
    4051
    4152        String testComponent = RegistryTestHelper.getComponentTestContentString();
     
    4455        AbstractDescription descr = getDao().getById(newId);
    4556        assertNotNull(descr);
    46         assertEquals("Aap", descr.getName());
     57        assertEquals("MyComponent", descr.getName());
    4758        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()));
    4863        assertEquals(testComponent, getDao().getContent(description.getId()));
    4964    }
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDatabase.java

    r1306 r1337  
    2828                + "  registration_date timestamp,"// with timezone,"
    2929                + "  creator_name character varying,"
    30                 + "  domain_name character varying);");
     30                + "  domain_name character varying,"
     31                + "  group_name character varying);");
    3132    }
    3233
     
    4344                + "  registration_date timestamp,"// with timezone,"
    4445                + "  creator_name character varying,"
    45                 + "  domain_name character varying);");
     46                + "  domain_name character varying,"
     47                + "  group_name character varying);");
    4648    }
    4749
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImplTest.java

    r1331 r1337  
    1212import clarin.cmdi.componentregistry.model.UserMapping.User;
    1313import clarin.cmdi.componentregistry.rest.DummyPrincipal;
     14import java.io.ByteArrayOutputStream;
    1415import java.io.IOException;
    15 import java.security.Principal;
     16import java.io.OutputStream;
     17import java.util.Calendar;
    1618import javax.xml.bind.JAXBException;
     19import org.apache.commons.lang.time.DateFormatUtils;
    1720
    1821import org.junit.Test;
     
    2225import static org.junit.Assert.assertNull;
    2326import static org.junit.Assert.assertNotNull;
     27import static org.junit.Assert.assertTrue;
    2428import static org.junit.Assert.fail;
    2529import org.springframework.beans.factory.annotation.Autowired;
     
    174178        } catch (UserUnauthorizedException e) {
    175179        }
    176        
     180
    177181        assertEquals(1, register.getProfileDescriptions().size());
    178182        assertNotNull(register.getMDProfile(description.getId()));
     
    209213    }
    210214
     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
    211298    private ComponentDescription createComponent(ComponentRegistry registry) throws IOException, DeleteFailedException, JAXBException {
    212299        ComponentDescription description = ComponentDescription.
     
    226313        } catch (UserUnauthorizedException e) {
    227314        }
    228        
     315
    229316        assertEquals(1, registry.getComponentDescriptions().size());
    230317        assertNotNull(registry.getMDComponent(description.getId()));
     
    245332        return user;
    246333    }
     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    }
    247451}
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/filesystem/ComponentRegistryImplTest.java

    r1288 r1337  
    234234        assertTrue(xsd.endsWith("</xs:schema>"));
    235235
    236         assertTrue(hasComponent(xsd, "Actor", "0", "unbounded"));
     236        assertTrue(RegistryTestHelper.hasComponent(xsd, "Actor", "0", "unbounded"));
    237237    }
    238238
     
    270270
    271271        assertTrue(xsd.endsWith("</xs:schema>"));
    272         assertTrue(hasComponent(xsd, "Actor", "0", "5"));
     272        assertTrue(RegistryTestHelper.hasComponent(xsd, "Actor", "0", "5"));
    273273    }
    274274
     
    321321        // System.out.println(xsd);
    322322
    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"));
    327327    }
    328328
     
    520520        assertEquals(2, result.size());
    521521    }
    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 used
    525      * in a profile/component
    526      */
    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 one
    531     }
    532522}
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/RegistryTestHelper.java

    r1298 r1337  
    1717import clarin.cmdi.componentregistry.model.ComponentDescription;
    1818import clarin.cmdi.componentregistry.model.ProfileDescription;
     19import java.util.regex.Matcher;
     20import java.util.regex.Pattern;
    1921
    2022/**
     
    146148        return MDMarshaller.unmarshal(CMDComponentSpec.class, getComponentTestContent(), MDMarshaller.getCMDComponentSchema());
    147149    }
     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    }
    148162}
Note: See TracChangeset for help on using the changeset viewer.