Changeset 5560


Ignore:
Timestamp:
08/18/14 14:22:07 (10 years ago)
Author:
olhsha@mpi.nl
Message:

Unit test for getting comments of profiles and components from groups.

Location:
ComponentRegistry/trunk/ComponentRegistry/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImpl.java

    r5556 r5560  
    278278    public List<Comment> getCommentsInProfile(String profileId) throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException {
    279279        try {
    280             //Olha was here
    281             //if (componentDao.isInRegistry(profileId, getUserId())) {
    282280            if (this.canCurrentUserAccessDescription(profileId)) {
    283281                final List<Comment> commentsFromProfile = commentsDao.getCommentsFromItem(profileId);
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java

    r5556 r5560  
    524524        LOG.debug("Comments of component with id {} are requested.", commentId);
    525525        try {
    526 
    527             return this.getBaseRegistry().getSpecifiedCommentInComponent(componentId, commentId);
     526            Comment result = this.getBaseRegistry().getSpecifiedCommentInComponent(componentId, commentId);
     527            return result;
    528528        } catch (ComponentRegistryException e) {
    529529            response.sendError(Status.INTERNAL_SERVER_ERROR.getStatusCode(), e.getMessage());
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/RegistryTestHelper.java

    r5556 r5560  
    2323import clarin.cmdi.componentregistry.model.ComponentDescription;
    2424import clarin.cmdi.componentregistry.model.ProfileDescription;
     25import clarin.cmdi.componentregistry.persistence.jpa.CommentsDao;
    2526
    2627import java.io.BufferedReader;
     
    2829import java.io.FileOutputStream;
    2930import java.io.InputStreamReader;
     31import java.util.Date;
    3032
    3133import org.springframework.beans.factory.annotation.Autowired;
     
    3840
    3941    private static MDMarshaller marshaller;
     42   
     43   
     44           
    4045
    4146    @Autowired
    4247    public void setMarshaller(MDMarshaller marshaller) {
    43         RegistryTestHelper.marshaller = marshaller;
    44     }
    45    
    46    
     48        RegistryTestHelper.marshaller = marshaller;
     49    }
    4750
    4851    public static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, boolean isPublic) throws ParseException, JAXBException {
    49         return addComponent(testRegistry, id, getComponentTestContent(), isPublic);
    50     }
    51    
    52    
     52        return addComponent(testRegistry, id, getComponentTestContent(), isPublic);
     53    }
     54
    5355    public static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, String content, boolean isPublic) throws ParseException,
    54             JAXBException, UnsupportedEncodingException {
    55         return addComponent(testRegistry, id, new ByteArrayInputStream(content.getBytes("UTF-8")), isPublic);
     56            JAXBException, UnsupportedEncodingException {
     57        return addComponent(testRegistry, id, new ByteArrayInputStream(content.getBytes("UTF-8")), isPublic);
    5658    }
    5759
    5860    private static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, InputStream content, boolean isPublic) throws ParseException,
    59             JAXBException {
    60         ComponentDescription desc = ComponentDescription.createNewDescription();
    61         desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName());
     61            JAXBException {
     62        ComponentDescription desc = ComponentDescription.createNewDescription();
     63        desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName());
    6264        desc.setUserId(DummyPrincipal.DUMMY_PRINCIPAL.getName());
    63         desc.setName(id);
    64         desc.setDescription("Test Description");
    65         desc.setId(ComponentDescription.COMPONENT_PREFIX + id);
    66         desc.setHref("link:" + desc.getId());
     65        desc.setName(id);
     66        desc.setDescription("Test Description");
     67        desc.setId(ComponentDescription.COMPONENT_PREFIX + id);
     68        desc.setHref("link:" + desc.getId());
    6769        desc.setPublic(isPublic);
    68         CMDComponentSpec spec = marshaller.unmarshal(CMDComponentSpec.class, content, marshaller.getCMDComponentSchema());
    69         testRegistry.register(desc, spec);
    70         return desc;
    71     }
    72    
     70        CMDComponentSpec spec = marshaller.unmarshal(CMDComponentSpec.class, content, marshaller.getCMDComponentSchema());
     71        testRegistry.register(desc, spec);
     72        return desc;
     73    }
     74
    7375    public static ComponentDescription addComponentAnotherPrincipal(ComponentRegistry testRegistry, String id, boolean isPublic) throws ParseException, JAXBException {
    74         return addComponentAnotherPrincipal(testRegistry, id, getComponentTestContent(), isPublic);
    75     }
    76 
    77      private static ComponentDescription addComponentAnotherPrincipal(ComponentRegistry testRegistry, String id, InputStream content, boolean isPublic) throws ParseException,
    78             JAXBException {
    79         ComponentDescription desc = ComponentDescription.createNewDescription();
    80         desc.setCreatorName("AnotherPrincipal");
     76        return addComponentAnotherPrincipal(testRegistry, id, getComponentTestContent(), isPublic);
     77    }
     78
     79    private static ComponentDescription addComponentAnotherPrincipal(ComponentRegistry testRegistry, String id, InputStream content, boolean isPublic) throws ParseException,
     80            JAXBException {
     81        ComponentDescription desc = ComponentDescription.createNewDescription();
     82        desc.setCreatorName("AnotherPrincipal");
    8183        desc.setUserId("AnotherPrincipal");
    8284        desc.setDbUserId(2);
    83         desc.setName(id);
    84         desc.setDescription("Test Description");
    85         desc.setId(ComponentDescription.COMPONENT_PREFIX + id);
    86         desc.setHref("link:" + desc.getId());
     85        desc.setName(id);
     86        desc.setDescription("Test Description");
     87        desc.setId(ComponentDescription.COMPONENT_PREFIX + id);
     88        desc.setHref("link:" + desc.getId());
    8789        desc.setPublic(isPublic);
    88         CMDComponentSpec spec = marshaller.unmarshal(CMDComponentSpec.class, content, marshaller.getCMDComponentSchema());
    89         testRegistry.register(desc, spec);
    90         return desc;
     90        CMDComponentSpec spec = marshaller.unmarshal(CMDComponentSpec.class, content, marshaller.getCMDComponentSchema());
     91        testRegistry.register(desc, spec);
     92        return desc;
    9193    }
    9294
    9395    public static String getProfileTestContentString() {
    94         return getProfileTestContentString("Actor");
     96        return getProfileTestContentString("Actor");
    9597    }
    9698
    9799    private static String getProfileTestContentString(String name) {
    98         String profileContent = "";
    99         profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    100         profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
    101         profileContent += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
    102         profileContent += "    <Header />\n";
    103         profileContent += "    <CMD_Component name=\"" + name + "\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n";
    104         profileContent += "        <AttributeList>\n";
    105         profileContent += "            <Attribute>\n";
    106         profileContent += "                <Name>Name</Name>\n";
    107         profileContent += "                <Type>string</Type>\n";
    108         profileContent += "            </Attribute>\n";
    109         profileContent += "        </AttributeList>\n";
    110         profileContent += "        <CMD_Element name=\"Age\">\n";
    111         profileContent += "            <ValueScheme>\n";
    112         profileContent += "                <pattern>[23][0-9]</pattern>\n";
    113         profileContent += "            </ValueScheme>\n";
    114         profileContent += "        </CMD_Element>\n";
    115         profileContent += "    </CMD_Component>\n";
    116         profileContent += "</CMD_ComponentSpec>\n";
    117         return profileContent;
     100        String profileContent = "";
     101        profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
     102        profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
     103        profileContent += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
     104        profileContent += "    <Header />\n";
     105        profileContent += "    <CMD_Component name=\"" + name + "\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n";
     106        profileContent += "        <AttributeList>\n";
     107        profileContent += "            <Attribute>\n";
     108        profileContent += "                <Name>Name</Name>\n";
     109        profileContent += "                <Type>string</Type>\n";
     110        profileContent += "            </Attribute>\n";
     111        profileContent += "        </AttributeList>\n";
     112        profileContent += "        <CMD_Element name=\"Age\">\n";
     113        profileContent += "            <ValueScheme>\n";
     114        profileContent += "                <pattern>[23][0-9]</pattern>\n";
     115        profileContent += "            </ValueScheme>\n";
     116        profileContent += "        </CMD_Element>\n";
     117        profileContent += "    </CMD_Component>\n";
     118        profileContent += "</CMD_ComponentSpec>\n";
     119        return profileContent;
    118120    }
    119121
    120122    public static InputStream getTestProfileContent() {
    121         return getTestProfileContent("Actor");
     123        return getTestProfileContent("Actor");
    122124    }
    123125
    124126    public static InputStream getTestProfileContent(String name) {
    125         return new ByteArrayInputStream(getProfileTestContentString(name).getBytes());
     127        return new ByteArrayInputStream(getProfileTestContentString(name).getBytes());
    126128    }
    127129
    128130    public static ProfileDescription addProfile(ComponentRegistry testRegistry, String id, boolean isPublic) throws ParseException, JAXBException, ItemNotFoundException {
    129         return addProfile(testRegistry, id, RegistryTestHelper.getTestProfileContent(), isPublic);
     131        return addProfile(testRegistry, id, RegistryTestHelper.getTestProfileContent(), isPublic);
    130132    }
    131133
    132134    public static ProfileDescription addProfile(ComponentRegistry testRegistry, String id, String content, boolean isPublic) throws ParseException,
    133             JAXBException, ItemNotFoundException {
    134         return addProfile(testRegistry, id, new ByteArrayInputStream(content.getBytes()), isPublic);
    135     }
    136    
    137    
     135            JAXBException, ItemNotFoundException {
     136        return addProfile(testRegistry, id, new ByteArrayInputStream(content.getBytes()), isPublic);
     137    }
    138138
    139139    private static ProfileDescription addProfile(ComponentRegistry testRegistry, String id, InputStream content, boolean isPublic) throws ParseException,
    140             JAXBException, ItemNotFoundException {
    141         ProfileDescription desc = ProfileDescription.createNewDescription();
    142         desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName());
    143         desc.setUserId(DummyPrincipal.DUMMY_CREDENTIALS.getPrincipalName());
    144         desc.setName(id);
    145         desc.setDescription("Test Description");
    146         desc.setId(ProfileDescription.PROFILE_PREFIX + id);
    147         desc.setHref("link:" + ProfileDescription.PROFILE_PREFIX + id);
     140            JAXBException, ItemNotFoundException {
     141        ProfileDescription desc = ProfileDescription.createNewDescription();
     142        desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName());
     143        desc.setUserId(DummyPrincipal.DUMMY_CREDENTIALS.getPrincipalName());
     144        desc.setName(id);
     145        desc.setDescription("Test Description");
     146        desc.setId(ProfileDescription.PROFILE_PREFIX + id);
     147        desc.setHref("link:" + ProfileDescription.PROFILE_PREFIX + id);
    148148        desc.setPublic(isPublic);
    149         CMDComponentSpec spec = marshaller.unmarshal(CMDComponentSpec.class, content, marshaller.getCMDComponentSchema());
    150         testRegistry.register(desc, spec);
    151         return desc;
    152     }
    153    
     149        CMDComponentSpec spec = marshaller.unmarshal(CMDComponentSpec.class, content, marshaller.getCMDComponentSchema());
     150        testRegistry.register(desc, spec);
     151        return desc;
     152    }
     153
    154154    public static ProfileDescription addProfileAnotherPrincipal(ComponentRegistry testRegistry, String id, boolean isPublic) throws ParseException, JAXBException, ItemNotFoundException {
    155         return addProfileAnotherPrincipal(testRegistry, id, RegistryTestHelper.getTestProfileContent(), isPublic);
    156     }
    157    
     155        return addProfileAnotherPrincipal(testRegistry, id, RegistryTestHelper.getTestProfileContent(), isPublic);
     156    }
     157
    158158    private static ProfileDescription addProfileAnotherPrincipal(ComponentRegistry testRegistry, String id, InputStream content, boolean isPublic) throws ParseException,
    159             JAXBException, ItemNotFoundException {
    160         ProfileDescription desc = ProfileDescription.createNewDescription();
    161         desc.setCreatorName("AnotherPrincipal");
    162         desc.setUserId("AnotherPrincipal");
     159            JAXBException, ItemNotFoundException {
     160        ProfileDescription desc = ProfileDescription.createNewDescription();
     161        desc.setCreatorName("AnotherPrincipal");
     162        desc.setUserId("AnotherPrincipal");
    163163        desc.setDbUserId(2);
    164         desc.setName(id);
    165         desc.setDescription("Test Description");
    166         desc.setId(ProfileDescription.PROFILE_PREFIX + id);
    167         desc.setHref("link:" + ProfileDescription.PROFILE_PREFIX + id);
     164        desc.setName(id);
     165        desc.setDescription("Test Description");
     166        desc.setId(ProfileDescription.PROFILE_PREFIX + id);
     167        desc.setHref("link:" + ProfileDescription.PROFILE_PREFIX + id);
    168168        desc.setPublic(isPublic);
    169         CMDComponentSpec spec = marshaller.unmarshal(CMDComponentSpec.class, content, marshaller.getCMDComponentSchema());
    170         testRegistry.register(desc, spec);
    171         return desc;
     169        CMDComponentSpec spec = marshaller.unmarshal(CMDComponentSpec.class, content, marshaller.getCMDComponentSchema());
     170        testRegistry.register(desc, spec);
     171        return desc;
    172172    }
    173173
    174174    public static CMDComponentSpec getTestProfile() throws JAXBException {
    175         return marshaller.unmarshal(CMDComponentSpec.class, getTestProfileContent(), marshaller.getCMDComponentSchema());
     175        return marshaller.unmarshal(CMDComponentSpec.class, getTestProfileContent(), marshaller.getCMDComponentSchema());
    176176    }
    177177
    178178    public static String getComponentTestContentString() {
    179         return getComponentTestContentString("Access");
     179        return getComponentTestContentString("Access");
    180180    }
    181181
    182182    public static InputStream getComponentTestContent() {
    183         return getComponentTestContentAsStream("Access");
     183        return getComponentTestContentAsStream("Access");
    184184    }
    185185
    186186    public static String getComponentTestContentString(String componentName) {
    187         String compContent = "";
    188         compContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    189         compContent += "\n";
    190         compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
    191         compContent += "    xsi:noNamespaceSchemaLocation=\"../../general-component-schema.xsd\">\n";
    192         compContent += "    \n";
    193         compContent += "    <Header/>\n";
    194         compContent += "    \n";
    195         compContent += "    <CMD_Component name=\"" + componentName + "\" CardinalityMin=\"1\" CardinalityMax=\"1\">\n";
    196         compContent += "        <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n";
    197         compContent += "        <CMD_Element name=\"Date\">\n";
    198         compContent += "            <ValueScheme>\n";
    199         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";
    200         compContent += "                <pattern>(1|2)\\d{3}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])</pattern>                \n";
    201         compContent += "            </ValueScheme>\n";
    202         compContent += "        </CMD_Element>\n";
    203         compContent += "        <CMD_Element name=\"Owner\" ValueScheme=\"string\" />\n";
    204         compContent += "        <CMD_Element name=\"Publisher\" ValueScheme=\"string\" />\n";
    205         compContent += "    </CMD_Component>\n";
    206         compContent += "\n";
    207         compContent += "</CMD_ComponentSpec>\n";
    208         return compContent;
     187        String compContent = "";
     188        compContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
     189        compContent += "\n";
     190        compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
     191        compContent += "    xsi:noNamespaceSchemaLocation=\"../../general-component-schema.xsd\">\n";
     192        compContent += "    \n";
     193        compContent += "    <Header/>\n";
     194        compContent += "    \n";
     195        compContent += "    <CMD_Component name=\"" + componentName + "\" CardinalityMin=\"1\" CardinalityMax=\"1\">\n";
     196        compContent += "        <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n";
     197        compContent += "        <CMD_Element name=\"Date\">\n";
     198        compContent += "            <ValueScheme>\n";
     199        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";
     200        compContent += "                <pattern>(1|2)\\d{3}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])</pattern>                \n";
     201        compContent += "            </ValueScheme>\n";
     202        compContent += "        </CMD_Element>\n";
     203        compContent += "        <CMD_Element name=\"Owner\" ValueScheme=\"string\" />\n";
     204        compContent += "        <CMD_Element name=\"Publisher\" ValueScheme=\"string\" />\n";
     205        compContent += "    </CMD_Component>\n";
     206        compContent += "\n";
     207        compContent += "</CMD_ComponentSpec>\n";
     208        return compContent;
    209209    }
    210210
    211211    public static String getStringFromStream(InputStream largeProfileStream) throws IOException {
    212         try {
    213             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(largeProfileStream));
    214             StringBuilder profileStringBuilder = new StringBuilder();
    215             String line;
    216             while (null != (line = bufferedReader.readLine())) {
    217                 profileStringBuilder.append(line);
    218             }
    219             return profileStringBuilder.toString();
    220         } finally {
    221             largeProfileStream.close();
    222         }
     212        try {
     213            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(largeProfileStream));
     214            StringBuilder profileStringBuilder = new StringBuilder();
     215            String line;
     216            while (null != (line = bufferedReader.readLine())) {
     217                profileStringBuilder.append(line);
     218            }
     219            return profileStringBuilder.toString();
     220        } finally {
     221            largeProfileStream.close();
     222        }
    223223    }
    224224    ///////////////////////////////////////////////////////
    225225
    226226    public static String getLargeProfileContent() throws IOException {
    227         InputStream largeProfileStream = RegistryTestHelper.class.getResourceAsStream("/xml/largeProfile.xml");
    228         return getStringFromStream(largeProfileStream);
     227        InputStream largeProfileStream = RegistryTestHelper.class.getResourceAsStream("/xml/largeProfile.xml");
     228        return getStringFromStream(largeProfileStream);
    229229    }
    230230
    231231    //////////////////////
    232232    public static CMDComponentSpec getComponentFromString(String contentString) throws JAXBException {
    233         return marshaller.unmarshal(CMDComponentSpec.class, getComponentContentAsStream(contentString), marshaller.getCMDComponentSchema());
     233        return marshaller.unmarshal(CMDComponentSpec.class, getComponentContentAsStream(contentString), marshaller.getCMDComponentSchema());
    234234    }
    235235
    236236    public static InputStream getComponentContentAsStream(String content) {
    237         return new ByteArrayInputStream(content.getBytes());
     237        return new ByteArrayInputStream(content.getBytes());
    238238    }
    239239
    240240    public static InputStream getComponentTestContentAsStream(String componentName) {
    241         return getComponentContentAsStream(getComponentTestContentString(componentName));
     241        return getComponentContentAsStream(getComponentTestContentString(componentName));
    242242    }
    243243
    244244    public static CMDComponentSpec getTestComponent() throws JAXBException {
    245         return marshaller.unmarshal(CMDComponentSpec.class, getComponentTestContent(), marshaller.getCMDComponentSchema());
     245        return marshaller.unmarshal(CMDComponentSpec.class, getComponentTestContent(), marshaller.getCMDComponentSchema());
    246246    }
    247247
    248248    public static CMDComponentSpec getTestComponent(String name) throws JAXBException {
    249         return marshaller.unmarshal(CMDComponentSpec.class, getComponentTestContentAsStream(name), marshaller.getCMDComponentSchema());
     249        return marshaller.unmarshal(CMDComponentSpec.class, getComponentTestContentAsStream(name), marshaller.getCMDComponentSchema());
    250250    }
    251251
    252252    public static String getXml(CMDComponentSpec componentSpec) throws JAXBException, UnsupportedEncodingException {
    253         ByteArrayOutputStream os = new ByteArrayOutputStream();
    254         marshaller.marshal(componentSpec, os);
    255         String xml = os.toString();
    256         try {
    257             os.close();
    258         } catch (IOException ex) {
    259         }
    260         return xml;
    261     }
    262 
    263     public static Comment addComment(ComponentRegistry testRegistry, String id, String descriptionId, String principal) throws ParseException, JAXBException, ComponentRegistryException, ItemNotFoundException,UserUnauthorizedException {
    264         return addComment(testRegistry, RegistryTestHelper.getTestCommentContent(id, descriptionId), principal);
     253        ByteArrayOutputStream os = new ByteArrayOutputStream();
     254        marshaller.marshal(componentSpec, os);
     255        String xml = os.toString();
     256        try {
     257            os.close();
     258        } catch (IOException ex) {
     259        }
     260        return xml;
     261    }
     262
     263    public static Comment addComment(ComponentRegistry testRegistry, String id, String descriptionId, String principal) throws ParseException, JAXBException, ComponentRegistryException, ItemNotFoundException, UserUnauthorizedException {
     264        return addComment(testRegistry, RegistryTestHelper.getTestCommentContent(id, descriptionId), principal);
    265265    }
    266266
    267267    private static Comment addComment(ComponentRegistry testRegistry, InputStream content, String principal) throws ParseException,
    268             JAXBException,
    269             ComponentRegistryException, ItemNotFoundException,UserUnauthorizedException {
    270         Comment spec = marshaller.unmarshal(Comment.class, content, null);
    271         testRegistry.registerComment(spec, principal);
    272         return spec;
     268            JAXBException,
     269            ComponentRegistryException, ItemNotFoundException, UserUnauthorizedException {
     270        Comment spec = marshaller.unmarshal(Comment.class, content, null);
     271        testRegistry.registerComment(spec, principal);
     272        return spec;
     273    }
     274   
     275    public Comment addCommentBypassAuthorisation(CommentsDao commentsDao, String id, String descriptionId, String principal) throws ParseException, JAXBException, ComponentRegistryException, ItemNotFoundException, UserUnauthorizedException {
     276        return addCommentBypassAuthorisation(commentsDao, RegistryTestHelper.getTestCommentContent(id, descriptionId), principal);
     277    }
     278
     279
     280    private Comment addCommentBypassAuthorisation(CommentsDao commentsDao, InputStream content, String principal) throws ParseException,
     281            JAXBException,
     282            ComponentRegistryException, ItemNotFoundException, UserUnauthorizedException {
     283        Comment comment = marshaller.unmarshal(Comment.class, content, null);
     284        comment.setCommentDate(new Date());
     285        comment.setUserId(2);
     286        commentsDao.saveAndFlush(comment);
     287        return comment;
    273288    }
    274289
    275290    public static String getCommentTestContentStringForProfile(String commentName, String profileId) {
    276         String comContent = "";
    277         comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
    278         comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n";
    279         comContent += "    <comments>" + commentName + "</comments>\n";
    280         comContent += "    <commentDate>" + DatesHelper.createNewDate() + "</commentDate>\n";
    281         comContent += "    <componentId>" + profileId + "</componentId>\n";
    282         comContent += "    <userName>J. Unit</userName>\n";
    283         comContent += "</comment>\n";
    284         return comContent;
     291        String comContent = "";
     292        comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
     293        comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n";
     294        comContent += "    <comments>" + commentName + "</comments>\n";
     295        comContent += "    <commentDate>" + DatesHelper.createNewDate() + "</commentDate>\n";
     296        comContent += "    <componentId>" + profileId + "</componentId>\n";
     297        comContent += "    <userName>JUnit@test.com</userName>\n";
     298        comContent += "</comment>\n";
     299        return comContent;
     300    }
     301   
     302     public static String getanotherPrincipalCommentTestContentStringForProfile(String commentName, String profileId) {
     303        String comContent = "";
     304        comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
     305        comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n";
     306        comContent += "    <comments>" + commentName + "</comments>\n";
     307        comContent += "    <commentDate>" + DatesHelper.createNewDate() + "</commentDate>\n";
     308        comContent += "    <componentId>" + profileId + "</componentId>\n";
     309        comContent += "    <userName>anotherPrincipal</userName>\n";
     310        comContent += "</comment>\n";
     311        return comContent;
    285312    }
    286313
    287314    public static String getCommentTestContentStringForComponent(String commentName, String componentId) {
    288         String comContent = "";
    289         comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
    290         comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n";
    291         comContent += "    <comments>" + commentName + "</comments>\n";
    292         comContent += "    <commentDate>" + DatesHelper.createNewDate() + "</commentDate>\n";
    293         comContent += "     <componentId>" + componentId + "</componentId>";
    294         comContent += "    <userName>J. Unit</userName>\n";
    295         comContent += "</comment>\n";
    296         return comContent;
     315        String comContent = "";
     316        comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
     317        comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n";
     318        comContent += "    <comments>" + commentName + "</comments>\n";
     319        comContent += "    <commentDate>" + DatesHelper.createNewDate() + "</commentDate>\n";
     320        comContent += "     <componentId>" + componentId + "</componentId>";
     321        comContent += "    <userName>JUnit@test.com</userName>\n";
     322        comContent += "</comment>\n";
     323        return comContent;
     324    }
     325   
     326     public static String getAnotherPrincipalCommentTestContentStringForComponent(String commentName, String componentId) {
     327        String comContent = "";
     328        comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
     329        comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n";
     330        comContent += "    <comments>" + commentName + "</comments>\n";
     331        comContent += "    <commentDate>" + DatesHelper.createNewDate() + "</commentDate>\n";
     332        comContent += "     <componentId>" + componentId + "</componentId>";
     333        comContent += "    <userName>anotherPrincipal</userName>\n";
     334        comContent += "</comment>\n";
     335        return comContent;
    297336    }
    298337
    299338    public static InputStream getTestCommentContent(String content, String descriptionId) {
    300         if (descriptionId.contains("profile")) {
    301             return new ByteArrayInputStream(getCommentTestContentStringForProfile(content, descriptionId).getBytes());
    302         } else {
    303             return new ByteArrayInputStream(getCommentTestContentStringForComponent(content, descriptionId).getBytes());
    304         }
     339        if (descriptionId.contains("profile")) {
     340            return new ByteArrayInputStream(getCommentTestContentStringForProfile(content, descriptionId).getBytes());
     341        } else {
     342            return new ByteArrayInputStream(getCommentTestContentStringForComponent(content, descriptionId).getBytes());
     343        }
    305344    }
    306345
    307346    public static String getCommentTestContent(String commentId, String descriptionId) {
    308         return getCommentTestContentStringForProfile(commentId, descriptionId);
     347        return getCommentTestContentStringForProfile(commentId, descriptionId);
    309348    }
    310349
    311350    public static InputStream getCommentTestContent() {
    312         return getTestCommentContent("Actual", ProfileDescription.PROFILE_PREFIX+"profile1");
    313     }
    314 
    315     /**
    316      * 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
    317      * in a profile/component
     351        return getTestCommentContent("Actual", ProfileDescription.PROFILE_PREFIX + "profile1");
     352    }
     353
     354    /**
     355     * Testing a big xsd string is a bit hard, so doing a best effort by
     356     * checking the xs:element which represent the nested components used in a
     357     * profile/component
    318358     */
    319359    public static boolean hasComponent(String xsd, String name, String min, String max) {
    320         Pattern pattern = Pattern.compile("<xs:element name=\"" + name + "\" minOccurs=\"" + min + "\" maxOccurs=\"" + max + "\">");
    321         Matcher matcher = pattern.matcher(xsd);
    322         return matcher.find() && !matcher.find(); //find only one
     360        Pattern pattern = Pattern.compile("<xs:element name=\"" + name + "\" minOccurs=\"" + min + "\" maxOccurs=\"" + max + "\">");
     361        Matcher matcher = pattern.matcher(xsd);
     362        return matcher.find() && !matcher.find(); //find only one
    323363    }
    324364
    325365    /**
    326366     *
    327      * @param bytes is an array of bytes to be written in the file filename (from scratch!)
    328      * @param filename is the name of the file where the array "bytes" is to be written to
     367     * @param bytes is an array of bytes to be written in the file filename
     368     * (from scratch!)
     369     * @param filename is the name of the file where the array "bytes" is to be
     370     * written to
    329371     * @throws IOException
    330372     * @throws JAXBException
     
    332374    public static void writeBytesToFile(byte[] bytes, String filename) throws IOException, JAXBException {
    333375
    334         File file = new File(filename);
    335         FileOutputStream fop = new FileOutputStream(file);
    336 
    337         fop.write(bytes);
    338 
    339         fop.flush();
    340         fop.close();
     376        File file = new File(filename);
     377        FileOutputStream fop = new FileOutputStream(file);
     378
     379        fop.write(bytes);
     380
     381        fop.flush();
     382        fop.close();
    341383
    342384
     
    345387    /**
    346388     *
    347      * @param str is a string which is to be written into the filename (from scratch!)
     389     * @param str is a string which is to be written into the filename (from
     390     * scratch!)
    348391     * @param filename is a filename where the string is to be written to
    349392     * @throws IOException
     
    352395    public static void writeStringToFile(String str, String filename) throws IOException, JAXBException {
    353396
    354         writeBytesToFile(str.getBytes(), filename);
     397        writeBytesToFile(str.getBytes(), filename);
    355398
    356399
     
    359402    /**
    360403     *
    361      * @param os is an output stream which is to be written into the filename (from scratch!)
     404     * @param os is an output stream which is to be written into the filename
     405     * (from scratch!)
    362406     * @param filename is a filename where the stream is to be written to
    363407     * @throws IOException
     
    366410    public static void writeStreamToFile(ByteArrayOutputStream os, String filename) throws IOException, JAXBException {
    367411
    368         writeBytesToFile(os.toByteArray(), filename);
     412        writeBytesToFile(os.toByteArray(), filename);
    369413
    370414
     
    373417    /**
    374418     *
    375      * @param cdesc is a component which is to be written into the filename (from scratch!)
     419     * @param cdesc is a component which is to be written into the filename
     420     * (from scratch!)
    376421     * @param filename is a filename where the component is to be written to
    377422     * @throws IOException
     
    381426
    382427
    383         ByteArrayOutputStream os = new ByteArrayOutputStream();
    384         marshaller.marshal(cdesc, os);
    385 
    386         writeStreamToFile(os, filename);
     428        ByteArrayOutputStream os = new ByteArrayOutputStream();
     429        marshaller.marshal(cdesc, os);
     430
     431        writeStreamToFile(os, filename);
    387432
    388433    }
     
    391436     * opens a temporary sub-directory dirName in /target/
    392437     *
    393      * @param dirName is the name of the temporary subdirectory which is to be opened
     438     * @param dirName is the name of the temporary subdirectory which is to be
     439     * opened
    394440     * @return the absolute part for this directory
    395441     */
    396442    public static String openTestDir(String dirName) {
    397443
    398         File testDir = new File("target/" + dirName);
    399 
    400 
    401         testDir.mkdir();
    402 
    403         System.out.println(dirName);
    404         //String retval = new File(testDir, dirName).getAbsolutePath();
    405         String retval = new File(testDir, "/").getAbsolutePath();
    406 
    407         return (retval);
     444        File testDir = new File("target/" + dirName);
     445
     446
     447        testDir.mkdir();
     448
     449        System.out.println(dirName);
     450        //String retval = new File(testDir, dirName).getAbsolutePath();
     451        String retval = new File(testDir, "/").getAbsolutePath();
     452
     453        return (retval);
    408454
    409455    }
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/RestGroupServiceTest.java

    r5556 r5560  
    1717import clarin.cmdi.componentregistry.model.ProfileDescription;
    1818import clarin.cmdi.componentregistry.model.RegistryUser;
     19import clarin.cmdi.componentregistry.persistence.jpa.CommentsDao;
    1920import clarin.cmdi.componentregistry.persistence.jpa.UserDao;
    2021import com.sun.jersey.api.client.ClientResponse;
     
    2223import java.util.List;
    2324import javax.ws.rs.core.MediaType;
    24 import javax.ws.rs.core.MultivaluedMap;
    2525import javax.xml.bind.JAXBException;
    2626import org.junit.Before;
     
    4646    @Autowired
    4747    private UserDao userDao;
     48    @Autowired
     49    private CommentsDao commentsDao;
    4850   
    4951    private ComponentRegistry baseRegistry;
     
    507509        fillUpGroupC();
    508510
     511        // lists
    509512       
    510513        List<ProfileDescription> response = this.getAuthenticatedResource(getResource()
     
    534537        assertEquals(403, clientResponse.getStatus());
    535538       
     539        // particular components and profiles
    536540       
    537541        CMDComponentSpec component = this.getAuthenticatedResource(getResource()
     
    570574       
    571575    }
     576   
     577   @Test
     578    public void testGetGroupComments() throws Exception {
     579
     580        System.out.println("test getGroupComments");
     581
     582        fillUpGroupA();
     583        fillUpGroupB();       
     584        fillUpGroupC();
     585
     586       
     587        RegistryTestHelper.addComment(baseRegistry, "COMMENTc1",  ComponentDescription.COMPONENT_PREFIX + "component-1",
     588                "JUnit@test.com");
     589        RegistryTestHelper.addComment(baseRegistry, "COMMENTp1",  ProfileDescription.PROFILE_PREFIX + "profile-1",
     590                "JUnit@test.com");
     591        RegistryTestHelper.addComment(baseRegistry, "COMMENTBc1",  ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1",
     592                "anotherPrincipal");
     593        RegistryTestHelper.addComment(baseRegistry, "COMMENTBp1",  ProfileDescription.PROFILE_PREFIX + "Bprofile-1",
     594                "anotherPrincipal");
     595       (new RegistryTestHelper()).addCommentBypassAuthorisation(commentsDao, "COMMENTCc1",  ComponentDescription.COMPONENT_PREFIX + "Ccomponent-1",
     596                "anotherPrincipal");
     597       (new RegistryTestHelper()).addCommentBypassAuthorisation(commentsDao, "COMMENTCp1",  ProfileDescription.PROFILE_PREFIX + "Cprofile-1","anotherPrincipal");
     598         
     599         
     600                // lists
     601       
     602        List<Comment> response = this.getAuthenticatedResource(getResource()
     603                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "component-1" + "/comments/"))
     604                .accept(MediaType.APPLICATION_XML)
     605                .get(COMMENT_LIST_GENERICTYPE);       
     606        assertEquals(1, response.size());
     607       
     608        response = this.getAuthenticatedResource(getResource()
     609                .path("/registry/components/" + ProfileDescription.PROFILE_PREFIX + "profile-1" + "/comments/"))
     610                .accept(MediaType.APPLICATION_XML)
     611                .get(COMMENT_LIST_GENERICTYPE);       
     612        assertEquals(1, response.size());
     613       
     614        response = this.getAuthenticatedResource(getResource()
     615                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1" + "/comments/"))
     616                .accept(MediaType.APPLICATION_XML)
     617                .get(COMMENT_LIST_GENERICTYPE);       
     618        assertEquals(1, response.size());
     619       
     620        response = this.getAuthenticatedResource(getResource()
     621                .path("/registry/components/" + ProfileDescription.PROFILE_PREFIX + "Bprofile-1" + "/comments/"))
     622                .accept(MediaType.APPLICATION_XML)
     623                .get(COMMENT_LIST_GENERICTYPE);       
     624        assertEquals(1, response.size());
     625       
     626       
     627   
     628        ClientResponse clientResponse = this.getAuthenticatedResource(getResource()
     629                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1"))
     630                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
     631        assertEquals(403, clientResponse.getStatus());
     632       
     633         clientResponse = this.getAuthenticatedResource(getResource()
     634                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1"))
     635                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
     636        assertEquals(403, clientResponse.getStatus());
     637       
     638        // particular comments
     639       
     640       Comment responseComment = this.getAuthenticatedResource(getResource()
     641                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "component-1" + "/comments/1"))
     642                .accept(MediaType.APPLICATION_XML)
     643                .get(Comment.class);       
     644        assertNotNull(responseComment);
     645     
     646        assertEquals(1, Long.parseLong(responseComment.getId()));
     647       
     648        responseComment = this.getAuthenticatedResource(getResource()
     649                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "profile-1" + "/comments/2"))
     650                .accept(MediaType.APPLICATION_XML)
     651                .get(Comment.class);       
     652        assertNotNull(responseComment);
     653       
     654        assertEquals(2, Long.parseLong(responseComment.getId()));
     655       
     656       responseComment = this.getAuthenticatedResource(getResource()
     657                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1" + "/comments/3"))
     658                .accept(MediaType.APPLICATION_XML)
     659                .get(Comment.class);       
     660        assertNotNull(responseComment);
     661        assertEquals(3, Long.parseLong(responseComment.getId()));
     662       
     663        responseComment = this.getAuthenticatedResource(getResource()
     664                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "Bprofile-1" + "/comments/4"))
     665                .accept(MediaType.APPLICATION_XML)
     666                .get(Comment.class);       
     667        assertNotNull(responseComment);
     668        assertEquals(4, Long.parseLong(responseComment.getId()));
     669       
     670        clientResponse = this.getAuthenticatedResource(getResource()
     671                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1/comments/6"))
     672                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
     673        assertEquals(403, clientResponse.getStatus());
     674       
     675         clientResponse = this.getAuthenticatedResource(getResource()
     676                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1/comments/5"))
     677                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
     678        assertEquals(403, clientResponse.getStatus());
     679       
     680    }
    572681
    573682}
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/SanboxTest.java

    r5552 r5560  
    77import clarin.cmdi.componentregistry.ComponentRegistry;
    88import clarin.cmdi.componentregistry.ComponentRegistryFactory;
    9 import clarin.cmdi.componentregistry.impl.database.ComponentRegistryBeanFactory;
     9import clarin.cmdi.componentregistry.ItemNotFoundException;
    1010import clarin.cmdi.componentregistry.impl.database.ComponentRegistryTestDatabase;
     11import clarin.cmdi.componentregistry.impl.database.GroupService;
    1112import clarin.cmdi.componentregistry.model.Comment;
    1213import clarin.cmdi.componentregistry.model.ComponentDescription;
     14import clarin.cmdi.componentregistry.model.Ownership;
    1315import clarin.cmdi.componentregistry.model.ProfileDescription;
     16import clarin.cmdi.componentregistry.model.RegistryUser;
     17import clarin.cmdi.componentregistry.persistence.jpa.CommentsDao;
     18import clarin.cmdi.componentregistry.persistence.jpa.UserDao;
    1419import com.sun.jersey.api.client.ClientResponse;
    15 import com.sun.jersey.multipart.FormDataMultiPart;
     20import java.text.ParseException;
    1621import java.util.List;
    17 import javax.ws.rs.WebApplicationException;
    1822import javax.ws.rs.core.MediaType;
     23import javax.xml.bind.JAXBException;
    1924import org.junit.Before;
    20 import org.junit.Test;
    2125import org.springframework.beans.factory.annotation.Autowired;
    2226import org.springframework.jdbc.core.JdbcTemplate;
    2327
    2428import static org.junit.Assert.*;
     29import org.junit.Test;
    2530
    2631/**
     
    2934 */
    3035public class SanboxTest extends ComponentRegistryRestServiceTestCase {
    31 
     36   
    3237    @Autowired
    3338    private ComponentRegistryFactory componentRegistryFactory;
    34     @Autowired
    35     private ComponentRegistryBeanFactory componentRegistryBeanFactory;
     39   
    3640    @Autowired
    3741    private JdbcTemplate jdbcTemplate;
     42    @Autowired
     43    private GroupService groupService;
     44    @Autowired
     45    private UserDao userDao;
     46     @Autowired
     47    private CommentsDao commentsDao;
    3848    private ComponentRegistry baseRegistry;
    3949
     
    4555    }
    4656
    47     private String expectedUserId(String principal) {
    48         return getUserDao().getByPrincipalName(principal).getId().toString();
    49     }
     57   
    5058    private ComponentDescription component1;
    5159    private ComponentDescription component2;
     
    94102                "JUnit@test.com");
    95103    }
    96 
     104   
     105     protected void createAntherUserRecord() {
     106        RegistryUser user = new RegistryUser();
     107        user.setName("Another database test user");
     108        user.setPrincipalName("anotherPrincipal");
     109        userDao.save(user);
     110    }
     111     
     112    private void MakeGroupA(){
     113        groupService.createNewGroup("group A", DummyPrincipal.DUMMY_PRINCIPAL.getName());
     114    }
     115   
     116     private void fillUpGroupA() throws ParseException, JAXBException, ItemNotFoundException{
     117         
     118        MakeGroupA();
     119       
     120        RegistryTestHelper.addProfile(baseRegistry, "profile-1", false);
     121        RegistryTestHelper.addComponent(baseRegistry, "component-1", false);
     122        RegistryTestHelper.addComponent(baseRegistry, "component-2", false);
     123       
     124        Ownership ownership = new Ownership();
     125        ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"profile-1");
     126        ownership.setGroupId(1);
     127        ownership.setUserId(0);
     128        groupService.addOwnership(ownership);
     129       
     130        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-1");
     131        ownership.setGroupId(1);
     132        ownership.setUserId(0);
     133        groupService.addOwnership(ownership);
     134       
     135        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-2");
     136        ownership.setGroupId(1);
     137        ownership.setUserId(0);
     138        groupService.addOwnership(ownership);
     139       
     140    }
     141     
     142     
     143   
     144    private void MakeGroupB() throws ItemNotFoundException{
     145        createAntherUserRecord();
     146        groupService.createNewGroup("group B", "anotherPrincipal");
     147        groupService.makeMember(DummyPrincipal.DUMMY_PRINCIPAL.getName(), "group B");
     148    }
     149   
     150     private void MakeGroupC() throws ItemNotFoundException{
     151        groupService.createNewGroup("group C", "anotherPrincipal");
     152    }
     153   
     154     private void fillUpGroupB() throws ParseException, JAXBException, ItemNotFoundException{
     155         
     156        MakeGroupB();
     157       
     158        RegistryTestHelper.addProfile(baseRegistry, "Bprofile-1", false);
     159        RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-1", false);
     160        RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-2", false);
     161       
     162        Ownership ownership = new Ownership();
     163        ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Bprofile-1");
     164        ownership.setGroupId(2);
     165        ownership.setUserId(0);
     166        groupService.addOwnership(ownership);
     167       
     168        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1");
     169        ownership.setGroupId(2);
     170        ownership.setUserId(0);
     171        groupService.addOwnership(ownership);
     172       
     173        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-2");
     174        ownership.setGroupId(2);
     175        ownership.setUserId(0);
     176        groupService.addOwnership(ownership);
     177       
     178    }
     179     
     180     private void fillUpGroupC() throws ParseException, JAXBException, ItemNotFoundException{
     181         
     182        MakeGroupC();
     183       
     184        RegistryTestHelper.addProfileAnotherPrincipal(baseRegistry, "Cprofile-1", false);
     185        RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-1", false);
     186        RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-2", false);
     187       
     188        Ownership ownership = new Ownership();
     189        ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Cprofile-1");
     190        ownership.setGroupId(3);
     191        ownership.setUserId(0);
     192        groupService.addOwnership(ownership);
     193       
     194        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1");
     195        ownership.setGroupId(3);
     196        ownership.setUserId(0);
     197        groupService.addOwnership(ownership);
     198       
     199        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-2");
     200        ownership.setGroupId(3);
     201        ownership.setUserId(0);
     202        groupService.addOwnership(ownership);
     203       
     204    }
     205   
     206 
     207   
    97208    @Test
    98     public void testDeleteCommentFromPublicProfile() throws Exception {
    99 
    100         System.out.println("testDeleteCommentFromPublicProfile");
    101 
    102         fillUpPublicItems();
    103 
    104         List<Comment> comments = this.getAuthenticatedResource(getResource().path(
    105                 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
    106                 + "profile1/comments")).get(COMMENT_LIST_GENERICTYPE);
    107         assertEquals(2, comments.size());
    108         Comment aComment = this.getAuthenticatedResource(getResource().path(
    109                 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
    110                 + "profile1/comments/" + profile1Comment1.getId())).get(Comment.class);
    111         assertNotNull(aComment);
    112 
    113         // Try to delete from other profile
    114         ClientResponse response = getAuthenticatedResource(
    115                 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
    116                 + "profile2/comments/9999").delete(ClientResponse.class);
    117         assertEquals(404, response.getStatus());
    118         // Delete from correct profile
    119         response = getAuthenticatedResource(
    120                 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
    121                 + "profile1/comments/" + profile1Comment1.getId()).delete(ClientResponse.class);
    122         assertEquals(200, response.getStatus());
    123 
    124         comments = this.getAuthenticatedResource(getResource().path(
    125                 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
    126                 + "profile1/comments/")).get(COMMENT_LIST_GENERICTYPE);
    127         assertEquals(1, comments.size());
    128 
    129         response = getAuthenticatedResource(
    130                 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
    131                 + "profile1/comments/" + profile1Comment2.getId()).delete(ClientResponse.class);
    132         assertEquals(200, response.getStatus());
    133 
    134         comments = this.getAuthenticatedResource(getResource().path(
    135                 "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
    136                 + "profile1/comments")).get(COMMENT_LIST_GENERICTYPE);
    137         assertEquals(0, comments.size());
    138     }
    139 
    140     private FormDataMultiPart createFormData(Object content) {
    141         return createFormData(content, "My Test");
    142     }
    143 
    144     private FormDataMultiPart createFormData(Object content, String description) {
    145         FormDataMultiPart form = new FormDataMultiPart();
    146         form.field(IComponentRegistryRestService.DATA_FORM_FIELD, content,
    147                 MediaType.APPLICATION_OCTET_STREAM_TYPE);
    148         form.field(IComponentRegistryRestService.NAME_FORM_FIELD, "Test1");
    149         form.field(IComponentRegistryRestService.DESCRIPTION_FORM_FIELD,
    150                 description);
    151         form.field(IComponentRegistryRestService.DOMAIN_FORM_FIELD, "My domain");
    152         form.field(IComponentRegistryRestService.GROUP_FORM_FIELD, "TestGroup");
    153         return form;
    154     }
     209    public void testGetGroupComments() throws Exception {
     210
     211        System.out.println("test getGroupComments");
     212
     213        fillUpGroupA();
     214        fillUpGroupB();       
     215        fillUpGroupC();
     216
     217       
     218        RegistryTestHelper.addComment(baseRegistry, "COMMENTc1",  ComponentDescription.COMPONENT_PREFIX + "component-1",
     219                "JUnit@test.com");
     220        RegistryTestHelper.addComment(baseRegistry, "COMMENTp1",  ProfileDescription.PROFILE_PREFIX + "profile-1",
     221                "JUnit@test.com");
     222        RegistryTestHelper.addComment(baseRegistry, "COMMENTBc1",  ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1",
     223                "anotherPrincipal");
     224        RegistryTestHelper.addComment(baseRegistry, "COMMENTBp1",  ProfileDescription.PROFILE_PREFIX + "Bprofile-1",
     225                "anotherPrincipal");
     226       (new RegistryTestHelper()).addCommentBypassAuthorisation(commentsDao, "COMMENTCc1",  ComponentDescription.COMPONENT_PREFIX + "Ccomponent-1",
     227                "anotherPrincipal");
     228       (new RegistryTestHelper()).addCommentBypassAuthorisation(commentsDao, "COMMENTCp1",  ProfileDescription.PROFILE_PREFIX + "Cprofile-1","anotherPrincipal");
     229         
     230         
     231                // lists
     232       
     233        List<Comment> response = this.getAuthenticatedResource(getResource()
     234                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "component-1" + "/comments/"))
     235                .accept(MediaType.APPLICATION_XML)
     236                .get(COMMENT_LIST_GENERICTYPE);       
     237        assertEquals(1, response.size());
     238       
     239        response = this.getAuthenticatedResource(getResource()
     240                .path("/registry/components/" + ProfileDescription.PROFILE_PREFIX + "profile-1" + "/comments/"))
     241                .accept(MediaType.APPLICATION_XML)
     242                .get(COMMENT_LIST_GENERICTYPE);       
     243        assertEquals(1, response.size());
     244       
     245        response = this.getAuthenticatedResource(getResource()
     246                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1" + "/comments/"))
     247                .accept(MediaType.APPLICATION_XML)
     248                .get(COMMENT_LIST_GENERICTYPE);       
     249        assertEquals(1, response.size());
     250       
     251        response = this.getAuthenticatedResource(getResource()
     252                .path("/registry/components/" + ProfileDescription.PROFILE_PREFIX + "Bprofile-1" + "/comments/"))
     253                .accept(MediaType.APPLICATION_XML)
     254                .get(COMMENT_LIST_GENERICTYPE);       
     255        assertEquals(1, response.size());
     256       
     257       
     258   
     259        ClientResponse clientResponse = this.getAuthenticatedResource(getResource()
     260                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1"))
     261                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
     262        assertEquals(403, clientResponse.getStatus());
     263       
     264         clientResponse = this.getAuthenticatedResource(getResource()
     265                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1"))
     266                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
     267        assertEquals(403, clientResponse.getStatus());
     268       
     269        // particular comments
     270       
     271       Comment responseComment = this.getAuthenticatedResource(getResource()
     272                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "component-1" + "/comments/1"))
     273                .accept(MediaType.APPLICATION_XML)
     274                .get(Comment.class);       
     275        assertNotNull(responseComment);
     276     
     277        assertEquals(1, Long.parseLong(responseComment.getId()));
     278       
     279        responseComment = this.getAuthenticatedResource(getResource()
     280                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "profile-1" + "/comments/2"))
     281                .accept(MediaType.APPLICATION_XML)
     282                .get(Comment.class);       
     283        assertNotNull(responseComment);
     284       
     285        assertEquals(2, Long.parseLong(responseComment.getId()));
     286       
     287       responseComment = this.getAuthenticatedResource(getResource()
     288                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1" + "/comments/3"))
     289                .accept(MediaType.APPLICATION_XML)
     290                .get(Comment.class);       
     291        assertNotNull(responseComment);
     292        assertEquals(3, Long.parseLong(responseComment.getId()));
     293       
     294        responseComment = this.getAuthenticatedResource(getResource()
     295                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "Bprofile-1" + "/comments/4"))
     296                .accept(MediaType.APPLICATION_XML)
     297                .get(Comment.class);       
     298        assertNotNull(responseComment);
     299        assertEquals(4, Long.parseLong(responseComment.getId()));
     300       
     301        clientResponse = this.getAuthenticatedResource(getResource()
     302                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1/comments/6"))
     303                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
     304        assertEquals(403, clientResponse.getStatus());
     305       
     306         clientResponse = this.getAuthenticatedResource(getResource()
     307                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1/comments/5"))
     308                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
     309        assertEquals(403, clientResponse.getStatus());
     310       
     311    }
     312
    155313}
Note: See TracChangeset for help on using the changeset viewer.