Changeset 1916


Ignore:
Timestamp:
04/27/12 13:28:30 (12 years ago)
Author:
twagoo
Message:

Added test for registering a large profile

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  
    11101110
    11111111    @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
    11121126    public void testRegisterComponentAsProfile() throws Exception {
    11131127        FormDataMultiPart form = new FormDataMultiPart();
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/RegistryTestHelper.java

    r1717 r1916  
    1919import clarin.cmdi.componentregistry.model.ComponentDescription;
    2020import clarin.cmdi.componentregistry.model.ProfileDescription;
     21import java.io.BufferedReader;
     22import java.io.InputStreamReader;
    2123
    2224/**
    2325 * Static helper methods to be used in tests
    24  * 
     26 *
    2527 */
    2628public final class RegistryTestHelper {
     
    3032
    3133    public static ComponentDescription addComponent(ComponentRegistry testRegistry, String id) throws ParseException, JAXBException {
    32         return addComponent(testRegistry, id, getComponentTestContent());
     34        return addComponent(testRegistry, id, getComponentTestContent());
    3335    }
    3436
    3537    public static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, String content) throws ParseException,
    36             JAXBException, UnsupportedEncodingException {
    37         return addComponent(testRegistry, id, new ByteArrayInputStream(content.getBytes("UTF-8")));
     38            JAXBException, UnsupportedEncodingException {
     39        return addComponent(testRegistry, id, new ByteArrayInputStream(content.getBytes("UTF-8")));
    3840    }
    3941
    4042    private static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, InputStream content) throws ParseException,
    41             JAXBException {
    42         ComponentDescription desc = ComponentDescription.createNewDescription();
    43         desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName());
    44         desc.setUserId(DummyPrincipal.DUMMY_CREDENTIALS.getPrincipalName());
    45         desc.setName(id);
    46         desc.setDescription("Test Description");
    47         desc.setId(ComponentRegistry.REGISTRY_ID + id);
    48         desc.setHref("link:" + ComponentRegistry.REGISTRY_ID + id);
    49         CMDComponentSpec spec = MDMarshaller.unmarshal(CMDComponentSpec.class, content, MDMarshaller.getCMDComponentSchema());
    50         testRegistry.register(desc, spec);
    51         return desc;
     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;
    5254    }
    5355
    5456    public static int updateComponent(ComponentRegistry testRegistry, ComponentDescription description, String content) throws JAXBException {
    55         return testRegistry.update(description, getComponentFromString(content), DummyPrincipal.DUMMY_CREDENTIALS.getPrincipal(), true);
     57        return testRegistry.update(description, getComponentFromString(content), DummyPrincipal.DUMMY_CREDENTIALS.getPrincipal(), true);
    5658    }
    5759
    5860    public static String getProfileTestContentString() {
    59         return getProfileTestContentString("Actor");
     61        return getProfileTestContentString("Actor");
    6062    }
    6163
    6264    private static String getProfileTestContentString(String name) {
    63         String profileContent = "";
    64         profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    65         profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
    66         profileContent += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
    67         profileContent += "    <Header />\n";
    68         profileContent += "    <CMD_Component name=\"" + name + "\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n";
    69         profileContent += "        <AttributeList>\n";
    70         profileContent += "            <Attribute>\n";
    71         profileContent += "                <Name>Name</Name>\n";
    72         profileContent += "                <Type>string</Type>\n";
    73         profileContent += "            </Attribute>\n";
    74         profileContent += "        </AttributeList>\n";
    75         profileContent += "        <CMD_Element name=\"Age\">\n";
    76         profileContent += "            <ValueScheme>\n";
    77         profileContent += "                <pattern>[23][0-9]</pattern>\n";
    78         profileContent += "            </ValueScheme>\n";
    79         profileContent += "        </CMD_Element>\n";
    80         profileContent += "    </CMD_Component>\n";
    81         profileContent += "</CMD_ComponentSpec>\n";
    82         return profileContent;
     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;
    8385    }
    8486
    8587    public static InputStream getTestProfileContent() {
    86         return getTestProfileContent("Actor");
     88        return getTestProfileContent("Actor");
    8789    }
    8890
    8991    public static InputStream getTestProfileContent(String name) {
    90         return new ByteArrayInputStream(getProfileTestContentString(name).getBytes());
     92        return new ByteArrayInputStream(getProfileTestContentString(name).getBytes());
    9193    }
    9294
    9395    public static ProfileDescription addProfile(ComponentRegistry testRegistry, String id) throws ParseException, JAXBException {
    94         return addProfile(testRegistry, id, RegistryTestHelper.getTestProfileContent());
     96        return addProfile(testRegistry, id, RegistryTestHelper.getTestProfileContent());
    9597    }
    9698
    9799    public static ProfileDescription addProfile(ComponentRegistry testRegistry, String id, String content) throws ParseException,
    98             JAXBException {
    99         return addProfile(testRegistry, id, new ByteArrayInputStream(content.getBytes()));
     100            JAXBException {
     101        return addProfile(testRegistry, id, new ByteArrayInputStream(content.getBytes()));
    100102    }
    101103
    102104    private static ProfileDescription addProfile(ComponentRegistry testRegistry, String id, InputStream content) throws ParseException,
    103             JAXBException {
    104         ProfileDescription desc = ProfileDescription.createNewDescription();
    105         desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName());
    106         desc.setUserId(DummyPrincipal.DUMMY_CREDENTIALS.getPrincipalName());
    107         desc.setName(id);
    108         desc.setDescription("Test Description");
    109         desc.setId(ComponentRegistry.REGISTRY_ID + id);
    110         desc.setHref("link:" + ComponentRegistry.REGISTRY_ID + id);
    111         CMDComponentSpec spec = MDMarshaller.unmarshal(CMDComponentSpec.class, content, MDMarshaller.getCMDComponentSchema());
    112         testRegistry.register(desc, spec);
    113         return desc;
     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;
    114116    }
    115117
    116118    public static CMDComponentSpec getTestProfile() throws JAXBException {
    117         return MDMarshaller.unmarshal(CMDComponentSpec.class, getTestProfileContent(), MDMarshaller.getCMDComponentSchema());
     119        return MDMarshaller.unmarshal(CMDComponentSpec.class, getTestProfileContent(), MDMarshaller.getCMDComponentSchema());
    118120    }
    119121
    120122    public static String getComponentTestContentString() {
    121         return getComponentTestContentString("Access");
     123        return getComponentTestContentString("Access");
    122124    }
    123125
    124126    public static InputStream getComponentTestContent() {
    125         return getComponentTestContent("Access");
     127        return getComponentTestContent("Access");
    126128    }
    127129
    128130    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        }
    151168    }
    152169
    153170    public static CMDComponentSpec getComponentFromString(String contentString) throws JAXBException {
    154         return MDMarshaller.unmarshal(CMDComponentSpec.class, getComponentContent(contentString), MDMarshaller.getCMDComponentSchema());
     171        return MDMarshaller.unmarshal(CMDComponentSpec.class, getComponentContent(contentString), MDMarshaller.getCMDComponentSchema());
    155172    }
    156173
    157174    public static InputStream getComponentContent(String content) {
    158         return new ByteArrayInputStream(content.getBytes());
     175        return new ByteArrayInputStream(content.getBytes());
    159176    }
    160177
    161178    public static InputStream getComponentTestContent(String componentName) {
    162         return getComponentContent(getComponentTestContentString(componentName));
     179        return getComponentContent(getComponentTestContentString(componentName));
    163180    }
    164181
    165182    public static CMDComponentSpec getTestComponent() throws JAXBException {
    166         return MDMarshaller.unmarshal(CMDComponentSpec.class, getComponentTestContent(), MDMarshaller.getCMDComponentSchema());
     183        return MDMarshaller.unmarshal(CMDComponentSpec.class, getComponentTestContent(), MDMarshaller.getCMDComponentSchema());
    167184    }
    168185
    169186    public static CMDComponentSpec getTestComponent(String name) throws JAXBException {
    170         return MDMarshaller.unmarshal(CMDComponentSpec.class, getComponentTestContent(name), MDMarshaller.getCMDComponentSchema());
     187        return MDMarshaller.unmarshal(CMDComponentSpec.class, getComponentTestContent(name), MDMarshaller.getCMDComponentSchema());
    171188    }
    172189
    173190    public static String getXml(CMDComponentSpec componentSpec) throws JAXBException, UnsupportedEncodingException {
    174         ByteArrayOutputStream os = new ByteArrayOutputStream();
    175         MDMarshaller.marshal(componentSpec, os);
    176         String xml = os.toString();
    177         try {
    178             os.close();
    179         } catch (IOException ex) {
    180         }
    181         return xml;
     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;
    182199    }
    183200
    184201    public static Comment addComment(ComponentRegistry testRegistry, String id, String descriptionId, String principal) throws ParseException, JAXBException, ComponentRegistryException {
    185         return addComment(testRegistry, RegistryTestHelper.getTestCommentContent(id, descriptionId), principal);
     202        return addComment(testRegistry, RegistryTestHelper.getTestCommentContent(id, descriptionId), principal);
    186203    }
    187204
    188205    private static Comment addComment(ComponentRegistry testRegistry, InputStream content, String principal) throws ParseException,
    189             JAXBException,
    190             ComponentRegistryException {
    191         Comment spec = MDMarshaller.unmarshal(Comment.class, content, null);
    192         testRegistry.registerComment(spec, principal);
    193         return spec;
     206            JAXBException,
     207            ComponentRegistryException {
     208        Comment spec = MDMarshaller.unmarshal(Comment.class, content, null);
     209        testRegistry.registerComment(spec, principal);
     210        return spec;
    194211    }
    195212
    196213    public static String getCommentTestContentStringForProfile(String commentName, String profileId) {
    197         String comContent = "";
    198         comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
    199         comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n";
    200         comContent += "    <comments>" + commentName + "</comments>\n";
    201         comContent += "    <commentDate>" + Comment.createNewDate() + "</commentDate>\n";
    202         comContent += "    <profileDescriptionId>" + profileId + "</profileDescriptionId>\n";
    203         comContent += "    <userName>J. Unit</userName>\n";
    204         comContent += "</comment>\n";
    205         return comContent;
     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;
    206223    }
    207224
    208225    public static String getCommentTestContentStringForComponent(String commentName, String componentId) {
    209         String comContent = "";
    210         comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
    211         comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n";
    212         comContent += "    <comments>" + commentName + "</comments>\n";
    213         comContent += "    <commentDate>" + Comment.createNewDate() + "</commentDate>\n";
    214         comContent += "     <componentDescriptionId>" + componentId + "</componentDescriptionId>";
    215         comContent += "    <userName>J. Unit</userName>\n";
    216         comContent += "</comment>\n";
    217         return comContent;
     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;
    218235    }
    219236
    220237    public static InputStream getTestCommentContent(String content, String descriptionId) {
    221         if (descriptionId.contains("profile")) {
    222             return new ByteArrayInputStream(getCommentTestContentStringForProfile(content, descriptionId).getBytes());
    223         } else {
    224             return new ByteArrayInputStream(getCommentTestContentStringForComponent(content, descriptionId).getBytes());
    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        }
    226243    }
    227244
    228245    public static String getCommentTestContent(String commentId, String descriptionId) {
    229         return getCommentTestContentStringForProfile(commentId, descriptionId);
     246        return getCommentTestContentStringForProfile(commentId, descriptionId);
    230247    }
    231248
    232249    public static InputStream getCommentTestContent() {
    233         return getTestCommentContent("Actual", "clarin.eu:cr1:profile1");
     250        return getTestCommentContent("Actual", "clarin.eu:cr1:profile1");
    234251    }
    235252
     
    239256     */
    240257    public static boolean hasComponent(String xsd, String name, String min, String max) {
    241         Pattern pattern = Pattern.compile("<xs:element name=\"" + name + "\" minOccurs=\"" + min + "\" maxOccurs=\"" + max + "\">");
    242         Matcher matcher = pattern.matcher(xsd);
    243         return matcher.find() && !matcher.find(); //find only one
     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
    244261    }
    245262}
Note: See TracChangeset for help on using the changeset viewer.