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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.