Changeset 5556


Ignore:
Timestamp:
08/18/14 09:50:16 (10 years ago)
Author:
olhsha@mpi.nl
Message:

Unit test for getting profiles and components from groups. A little bug is fixed

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

Legend:

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

    r5553 r5556  
    4141     * @throws ComponentRegistryException
    4242     */
    43     List<ComponentDescription> getComponentDescriptions() throws ComponentRegistryException, UserUnauthorizedException;
     43    List<ComponentDescription> getComponentDescriptions() throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException;
    4444
    4545   
     
    5252     * @throws ComponentRegistryException
    5353     */
    54     List<ProfileDescription> getProfileDescriptions() throws ComponentRegistryException, UserUnauthorizedException;
     54    List<ProfileDescription> getProfileDescriptions() throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException;
    5555
    5656
     
    6060     * @throws ComponentRegistryException
    6161     */
    62     List<ProfileDescription> getProfileDescriptionsForMetadaEditor() throws ComponentRegistryException;
     62    List<ProfileDescription> getProfileDescriptionsForMetadaEditor() throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException;
    6363
    6464    /**
     
    6767    * @throws ComponentRegistryException
    6868    */
    69    List<ProfileDescription> getProfileDescriptionsForMetadaEditor(Number groupId) throws ComponentRegistryException, UserUnauthorizedException;
     69   List<ProfileDescription> getProfileDescriptionsForMetadaEditor(Number groupId) throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException;
    7070
    7171   ProfileDescription getProfileDescriptionAccessControlled(String id) throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException;
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/frontend/AdminHomePage.java

    r5549 r5556  
    2929import clarin.cmdi.componentregistry.ComponentRegistryException;
    3030import clarin.cmdi.componentregistry.ComponentRegistryFactory;
     31import clarin.cmdi.componentregistry.ItemNotFoundException;
    3132import clarin.cmdi.componentregistry.MDMarshaller;
    3233import clarin.cmdi.componentregistry.RegistrySpace;
     
    5354    private MDMarshaller marshaller;
    5455
    55     public AdminHomePage(final PageParameters parameters) throws ComponentRegistryException {
     56    public AdminHomePage(final PageParameters parameters) throws ComponentRegistryException, ItemNotFoundException {
    5657        super(parameters);
    5758        adminRegistry.setComponentRegistryFactory(componentRegistryFactory);
     
    181182    }
    182183
    183     private void reloadTreeModel(CMDItemInfo info) throws UserUnauthorizedException {
     184    private void reloadTreeModel(CMDItemInfo info) throws UserUnauthorizedException, ItemNotFoundException {
    184185        try {
    185186            tree.setModelObject(createDBTreeModel());
     
    254255    }
    255256
    256     private TreeModel createDBTreeModel() throws ComponentRegistryException, UserUnauthorizedException {
     257    private TreeModel createDBTreeModel() throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException {
    257258        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(new DisplayDataNode("ComponentRegistry", false));
    258259        DefaultMutableTreeNode publicNode = new DefaultMutableTreeNode(new DisplayDataNode("Public", false));
     
    270271    }
    271272
    272     private void add(DefaultMutableTreeNode parent, ComponentRegistry registry) throws ComponentRegistryException, UserUnauthorizedException {
     273    private void add(DefaultMutableTreeNode parent, ComponentRegistry registry) throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException {
    273274        DefaultMutableTreeNode componentsNode = new DefaultMutableTreeNode(new DisplayDataNode("Components", false));
    274275        parent.add(componentsNode);
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/frontend/StatisticsPage.java

    r5549 r5556  
    1414import clarin.cmdi.componentregistry.ComponentRegistryException;
    1515import clarin.cmdi.componentregistry.ComponentRegistryFactory;
     16import clarin.cmdi.componentregistry.ItemNotFoundException;
    1617import clarin.cmdi.componentregistry.RegistrySpace;
    1718import clarin.cmdi.componentregistry.UserUnauthorizedException;
     
    4243    }
    4344
    44     public StatisticsPage(final PageParameters pageParameters) throws IOException, ComponentRegistryException, UserUnauthorizedException {
     45    public StatisticsPage(final PageParameters pageParameters) throws IOException, ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException {
    4546        super(pageParameters);
    4647        ComponentRegistry registry = componentRegistryFactory.getComponentRegistry(RegistrySpace.PUBLISHED, null, null, null);
     
    4950    }
    5051
    51     private void displayStatistics(ComponentRegistry registry) throws ComponentRegistryException, UserUnauthorizedException {
     52    private void displayStatistics(ComponentRegistry registry) throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException {
    5253        List<ProfileDescription> profileList = registry.getProfileDescriptions();
    5354        RepeatingView repeating = new RepeatingView("repeating");
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImpl.java

    r5553 r5556  
    155155        }
    156156    }
    157    
    158     @Override
    159     public Number makeGroupMember(String principalName, String groupName) throws  UserUnauthorizedException, ItemNotFoundException{
     157
     158    @Override
     159    public Number makeGroupMember(String principalName, String groupName) throws UserUnauthorizedException, ItemNotFoundException {
    160160        RegistryUser regOwner = userDao.getPrincipalNameById(registryOwner.getId());
    161         if  (groupService.isUserOwnerOfGroup(groupName, regOwner.getPrincipalName()) ||
    162                 configuration.isAdminUser(regOwner.getPrincipalName())) {
    163         return groupService.makeMember(principalName, groupName);
     161        if (groupService.isUserOwnerOfGroup(groupName, regOwner.getPrincipalName())
     162                || configuration.isAdminUser(regOwner.getPrincipalName())) {
     163            return groupService.makeMember(principalName, groupName);
    164164        } else {
    165165            throw new UserUnauthorizedException("The registry owner is not the admin or not the owner of the group and cannot add users to the group");
    166166        }
    167167    }
    168    
     168
    169169//    @Override
    170170//    public long removeGroupMember(String principalName, String groupName) throws  UserUnauthorizedException, ItemNotFoundException{
     
    177177//        }
    178178//    }
    179    
    180    
    181     @Override
    182     public List<ProfileDescription> getProfileDescriptions() throws ComponentRegistryException, UserUnauthorizedException {
     179    @Override
     180    public List<ProfileDescription> getProfileDescriptions() throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException {
    183181        try {
    184182            switch (registrySpace) {
     
    234232
    235233    @Override
    236     public List<ComponentDescription> getComponentDescriptions() throws ComponentRegistryException, UserUnauthorizedException {
     234    public List<ComponentDescription> getComponentDescriptions() throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException {
    237235        try {
    238236            switch (registrySpace) {
     
    861859
    862860    private List<ComponentDescription> getComponentDescriptionsInGroup(Number groupId)
    863             throws ComponentRegistryException, UserUnauthorizedException {
     861            throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException {
    864862
    865863        String principalName = userDao.getPrincipalNameById(registryOwner.getId()).getPrincipalName();
    866 
    867         if (!groupService.userGroupMember(principalName, groupId.toString())) {
     864        String groupName = groupService.getGroupNameById(groupId.longValue());
     865        if (!groupService.userGroupMember(principalName, groupId.toString())
     866                && !groupService.isUserOwnerOfGroup(groupName, principalName)) {
    868867            throw new UserUnauthorizedException("The user \'" + principalName + "\' does not have access to components of the group " + groupId);
    869868        }
     
    880879    }
    881880
    882     private List<ProfileDescription> getProfileDescriptionsInGroup(Number groupId) throws ComponentRegistryException, UserUnauthorizedException {
     881    private List<ProfileDescription> getProfileDescriptionsInGroup(Number groupId) throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException {
    883882
    884883        String principalName = userDao.getPrincipalNameById(registryOwner.getId()).getPrincipalName();
    885 
    886         if (!groupService.userGroupMember(principalName, groupId.toString())) {
     884        String groupName = groupService.getGroupNameById(groupId.longValue());
     885        if (!groupService.userGroupMember(principalName, groupId.toString())
     886                && !groupService.isUserOwnerOfGroup(groupName, principalName)) {
    887887            throw new UserUnauthorizedException("The user \'" + principalName + "\' does not have access to profiles of the group " + groupId);
    888888        }
     
    900900
    901901    @Override
    902     public List<ProfileDescription> getProfileDescriptionsForMetadaEditor(Number groupId) throws UserUnauthorizedException,
    903             ComponentRegistryException {
     902    public List<ProfileDescription> getProfileDescriptionsForMetadaEditor(Number groupId) throws ComponentRegistryException, UserUnauthorizedException, ItemNotFoundException {
    904903        return this.getProfileDescriptionsInGroup(groupId.longValue());
    905904    }
     
    984983        }
    985984    }
    986    
    987    
    988985}
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java

    r5553 r5556  
    200200            return new ArrayList<ComponentDescription>();
    201201
     202       
     203       
     204        } catch (ItemNotFoundException e) {
     205            response.sendError(Status.NOT_FOUND.getStatusCode(), e.toString());
     206            return new ArrayList<ComponentDescription>();
    202207        }
    203208
     
    235240        } catch (UserUnauthorizedException e) {
    236241            response.sendError(Status.FORBIDDEN.getStatusCode(), e.toString());
     242            return new ArrayList<ProfileDescription>();
     243       
     244       
     245        } catch (ItemNotFoundException e) {
     246            response.sendError(Status.NOT_FOUND.getStatusCode(), e.toString());
    237247            return new ArrayList<ProfileDescription>();
    238248        }
     
    15581568            return new Rss();
    15591569        }
     1570        catch (ItemNotFoundException e) {
     1571            response.sendError(Status.NOT_FOUND.getStatusCode(), e.toString());
     1572            return new Rss();
     1573        }
    15601574        // obsolete, add group Id
    15611575        final RssCreatorDescriptions instance = new RssCreatorDescriptions(!registrySpace.equalsIgnoreCase("published"), getApplicationBaseURI(), "components",
     
    15961610        } catch (UserUnauthorizedException e) {
    15971611            response.sendError(Status.FORBIDDEN.getStatusCode(), e.toString());
     1612            return new Rss();
     1613       
     1614        } catch (ItemNotFoundException e) {
     1615            response.sendError(Status.NOT_FOUND.getStatusCode(), e.toString());
    15981616            return new Rss();
    15991617        }
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/RegistryTestHelper.java

    r5553 r5556  
    4949        return addComponent(testRegistry, id, getComponentTestContent(), isPublic);
    5050    }
    51 
     51   
     52   
    5253    public static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, String content, boolean isPublic) throws ParseException,
    5354            JAXBException, UnsupportedEncodingException {
     
    6061        desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName());
    6162        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());
     67        desc.setPublic(isPublic);
     68        CMDComponentSpec spec = marshaller.unmarshal(CMDComponentSpec.class, content, marshaller.getCMDComponentSchema());
     69        testRegistry.register(desc, spec);
     70        return desc;
     71    }
     72   
     73    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");
     81        desc.setUserId("AnotherPrincipal");
     82        desc.setDbUserId(2);
    6283        desc.setName(id);
    6384        desc.setDescription("Test Description");
     
    121142        desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName());
    122143        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);
     148        desc.setPublic(isPublic);
     149        CMDComponentSpec spec = marshaller.unmarshal(CMDComponentSpec.class, content, marshaller.getCMDComponentSchema());
     150        testRegistry.register(desc, spec);
     151        return desc;
     152    }
     153   
     154    public static ProfileDescription addProfileAnotherPrincipal(ComponentRegistry testRegistry, String id, boolean isPublic) throws ParseException, JAXBException, ItemNotFoundException {
     155        return addProfileAnotherPrincipal(testRegistry, id, RegistryTestHelper.getTestProfileContent(), isPublic);
     156    }
     157   
     158    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");
     163        desc.setDbUserId(2);
    123164        desc.setName(id);
    124165        desc.setDescription("Test Description");
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/RestGroupServiceTest.java

    r5554 r5556  
    88import clarin.cmdi.componentregistry.ComponentRegistryFactory;
    99import clarin.cmdi.componentregistry.ItemNotFoundException;
     10import clarin.cmdi.componentregistry.components.CMDComponentSpec;
    1011import clarin.cmdi.componentregistry.impl.database.ComponentRegistryTestDatabase;
    1112import clarin.cmdi.componentregistry.impl.database.GroupService;
     
    184185        MakeGroupC();
    185186       
    186         RegistryTestHelper.addProfile(baseRegistry, "Cprofile-1", false);
    187         RegistryTestHelper.addComponent(baseRegistry, "Ccomponent-1", false);
    188         RegistryTestHelper.addComponent(baseRegistry, "Ccomponent-2", false);
     187        RegistryTestHelper.addProfileAnotherPrincipal(baseRegistry, "Cprofile-1", false);
     188        RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-1", false);
     189        RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-2", false);
    189190       
    190191        Ownership ownership = new Ownership();
     
    497498    }
    498499   
     500    @Test
     501    public void testGetGroupProfilesAndComponents() throws Exception {
     502
     503        System.out.println("test getGroupProfiles");
     504
     505        fillUpGroupA();
     506        fillUpGroupB();       
     507        fillUpGroupC();
     508
     509       
     510        List<ProfileDescription> response = this.getAuthenticatedResource(getResource()
     511                .path("/registry/profiles").queryParam("registrySpace", "group").queryParam("groupid", "1")).accept(MediaType.APPLICATION_XML)
     512                .get(PROFILE_LIST_GENERICTYPE);
     513        assertEquals(1, response.size());
     514       
     515        List<ComponentDescription> responseC = this.getAuthenticatedResource(getResource()
     516                .path("/registry/components").queryParam("registrySpace", "group").queryParam("groupid", "1")).accept(MediaType.APPLICATION_XML)
     517                .get(COMPONENT_LIST_GENERICTYPE);
     518        assertEquals(2, responseC.size());
     519       
     520        response = this.getAuthenticatedResource(getResource()
     521                .path("/registry/profiles").queryParam("registrySpace", "group").queryParam("groupid", "2")).accept(MediaType.APPLICATION_XML)
     522                .get(PROFILE_LIST_GENERICTYPE);
     523        assertEquals(1, response.size());
     524       
     525        responseC = this.getAuthenticatedResource(getResource()
     526                .path("/registry/components").queryParam("registrySpace", "group").queryParam("groupid", "2")).accept(MediaType.APPLICATION_XML)
     527                .get(COMPONENT_LIST_GENERICTYPE);
     528        assertEquals(2, responseC.size());
     529       
     530        ClientResponse clientResponse = this.getAuthenticatedResource(getResource()
     531                .path("/registry/components").queryParam("registrySpace", "group").queryParam("groupid", "3")).accept(MediaType.APPLICATION_XML)
     532                .get(ClientResponse.class);
     533       
     534        assertEquals(403, clientResponse.getStatus());
     535       
     536       
     537        CMDComponentSpec component = this.getAuthenticatedResource(getResource()
     538                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"profile-1"))
     539                .accept(MediaType.APPLICATION_JSON).get(CMDComponentSpec.class);
     540        assertNotNull(component);
     541        assertEquals("Actor", component.getCMDComponent().getName());
     542   
     543        component = this.getAuthenticatedResource(getResource()
     544                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"component-1"))
     545                .accept(MediaType.APPLICATION_JSON).get(CMDComponentSpec.class);
     546        assertNotNull(component);
     547        assertEquals("Access", component.getCMDComponent().getName());
     548   
     549        component = this.getAuthenticatedResource(getResource()
     550                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Bprofile-1"))
     551                .accept(MediaType.APPLICATION_JSON).get(CMDComponentSpec.class);
     552        assertNotNull(component);
     553        assertEquals("Actor", component.getCMDComponent().getName());
     554   
     555        component = this.getAuthenticatedResource(getResource()
     556                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1"))
     557                .accept(MediaType.APPLICATION_JSON).get(CMDComponentSpec.class);
     558        assertNotNull(component);
     559        assertEquals("Access", component.getCMDComponent().getName());
     560   
     561        clientResponse = this.getAuthenticatedResource(getResource()
     562                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1"))
     563                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
     564        assertEquals(403, clientResponse.getStatus());
     565       
     566         clientResponse = this.getAuthenticatedResource(getResource()
     567                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1"))
     568                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
     569        assertEquals(403, clientResponse.getStatus());
     570       
     571    }
    499572
    500573}
Note: See TracChangeset for help on using the changeset viewer.