Ignore:
Timestamp:
08/11/14 16:07:55 (10 years ago)
Author:
olhsha@mpi.nl
Message:

Added group service. Tested via the tomcat on loclahots (test URI and postman), old unit tests are adjusted and work well. Todo: retest on localhost tomcat, look at run-time exceptions, add new unit tests, adjust front-end

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/MDValidatorTest.java

    r4550 r5549  
    44import clarin.cmdi.componentregistry.ComponentRegistry;
    55import clarin.cmdi.componentregistry.ComponentRegistryFactory;
    6 import clarin.cmdi.componentregistry.ComponentStatus;
    7 import clarin.cmdi.componentregistry.MDMarshaller;
     6import clarin.cmdi.componentregistry.OwnerUser;
     7import clarin.cmdi.componentregistry.RegistrySpace;
     8import clarin.cmdi.componentregistry.UserUnauthorizedException;
    89import clarin.cmdi.componentregistry.components.CMDComponentSpec;
    9 import clarin.cmdi.componentregistry.impl.database.ComponentRegistryTestDatabase;
    1010import clarin.cmdi.componentregistry.model.ComponentDescription;
    1111import clarin.cmdi.componentregistry.model.ProfileDescription;
    12 
     12import clarin.cmdi.componentregistry.model.RegistryUser;
    1313import java.io.ByteArrayInputStream;
    1414import java.io.InputStream;
     
    1717
    1818import org.junit.Before;
     19import org.junit.Ignore;
    1920import org.junit.Test;
    2021import org.springframework.beans.factory.annotation.Autowired;
    21 import org.springframework.jdbc.core.JdbcTemplate;
    22 
    23 import static org.junit.Assert.*;
     22
    2423
    2524/**
     
    3130
    3231    @Autowired
    33     private ComponentRegistryFactory componentRegistryFactory;
    34     private ComponentRegistry publicRegistry;
    35 
     32    private ComponentRegistryFactory componentRegistryFactory;
     33    @Autowired
     34    private ComponentRegistry testRegistry;
     35   
     36    private RegistryUser user;
     37   
     38   
    3639    @Before
    37     public void setUp() throws TransformerException {
    38         publicRegistry = componentRegistryFactory.getPublicRegistry();
    39     }
    40 
    41     @Test
    42     public void testValidateSucces() {
    43         MDValidator validator = getValidProfileValidator();
    44         assertTrue(validator.validate());
    45     }
    46 
    47     @Test
    48     public void testValidateIllegalComponentAttributeName() {
     40    @Override
     41    public void setUp() throws TransformerException, UserUnauthorizedException {
     42        testRegistry = componentRegistryFactory.getPublicRegistry();
     43        user = componentRegistryFactory.getOrCreateUser(DummyPrincipal.DUMMY_CREDENTIALS);
     44    }
     45   
     46   
     47
     48    @Test
     49    public void testValidateSucces() throws UserUnauthorizedException{
     50        MDValidator validator = this.getValidProfileValidator();
     51        boolean result = validator.validate();
     52        assertTrue(result);
     53    }
     54
     55    @Test
     56    public void testValidateIllegalComponentAttributeName() throws UserUnauthorizedException{
    4957        String profileContent = "";
    5058        profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
     
    93101
    94102        ProfileDescription desc = ProfileDescription.createNewDescription();
    95         MDValidator validator = new MDValidator(input, desc, publicRegistry,
    96                 null, publicRegistry, marshaller);
     103       
     104        MDValidator validator = new MDValidator(input, desc, testRegistry, marshaller);
    97105        assertFalse(validator.validate());
    98106        assertEquals(4, validator.getErrorMessages().size());
     
    115123
    116124        ProfileDescription desc = ProfileDescription.createNewDescription();
    117         MDValidator validator = new MDValidator(input, desc, publicRegistry,
    118                 null, publicRegistry, marshaller);
    119         assertFalse(validator.validate());
    120         assertTrue(validator
    121                 .getErrorMessages()
    122                 .get(0)
    123                 .startsWith(MDValidator.COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR));
    124     }
    125 
    126     @Test
     125        desc.setPublic(true);
     126        MDValidator validator = new MDValidator(input, desc, testRegistry, marshaller);
     127        assertFalse(validator.validate());
     128        assertTrue(validator
     129                .getErrorMessages()
     130                .get(0)
     131                .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR));
     132        //
     133    }
     134
     135    @Test 
    127136    public void testValidateComponentIdNotRegistered() throws Exception {
    128137        String id1 = "component1";
     
    144153        profileContent += "</CMD_ComponentSpec>";
    145154
    146         // Ids not registered will return two errors. One for each id
    147         ProfileDescription desc = ProfileDescription.createNewDescription();
     155        // Ids not registered. will return 2 errors: one per each id.
     156        ProfileDescription desc = ProfileDescription.createNewDescription();
     157        desc.setPublic(true);
    148158        MDValidator validator = new MDValidator(new ByteArrayInputStream(
    149                 profileContent.getBytes()), desc, publicRegistry, null,
    150                 publicRegistry, marshaller);
    151         assertFalse(validator.validate());
    152         assertEquals(2, validator.getErrorMessages().size());
    153         assertTrue(validator
    154                 .getErrorMessages()
    155                 .get(0)
    156                 .startsWith(MDValidator.COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR));
    157         assertTrue(validator
    158                 .getErrorMessages()
    159                 .get(1)
    160                 .startsWith(MDValidator.COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR));
    161 
    162         // id1 will be added and therefore only id2 is not registered
    163         RegistryTestHelper.addComponent(publicRegistry, id1);
     159                profileContent.getBytes()), desc, testRegistry, marshaller);
     160        assertFalse(validator.validate());
     161        assertEquals(1, validator.getErrorMessages().size());
     162        assertTrue(validator
     163                .getErrorMessages()
     164                .get(0)
     165                .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR));
     166       
     167
     168        // id1 will be added as public and therefore only id2 is not registered
     169        RegistryTestHelper.addComponent(testRegistry, id1, true);
    164170        validator = new MDValidator(new ByteArrayInputStream(
    165                 profileContent.getBytes()), desc, publicRegistry, null,
    166                 publicRegistry, marshaller);
     171                profileContent.getBytes()), desc, testRegistry
     172                , marshaller);
    167173        assertFalse(validator.validate());
    168174        assertEquals(1, validator.getErrorMessages().size());
     
    170176                .getErrorMessages()
    171177                .get(0)
    172                 .startsWith(MDValidator.COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR));
    173 
    174         // id2 is added, no more errors shoud be return
    175         RegistryTestHelper.addComponent(publicRegistry, id2);
     178                .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR));
     179
     180        // id2 is added as public, no more errors shoud be return
     181        RegistryTestHelper.addComponent(testRegistry, id2, true);
    176182        validator = new MDValidator(new ByteArrayInputStream(
    177                 profileContent.getBytes()), desc, publicRegistry, null,
    178                 publicRegistry, marshaller);
     183                profileContent.getBytes()), desc, testRegistry, marshaller);
    179184        assertTrue("component is registered should be valid now",
    180185                validator.validate());
     
    183188
    184189    @Test
    185     public void testValidateUserRegistry() throws Exception {
     190    public void testValidateBaseAndUserRegistry() throws Exception {
    186191        String id1 = "component1";
    187192        String id2 = "component2";
    188         ComponentRegistry userRegistry = componentRegistryFactory
    189                 .getComponentRegistry(ComponentStatus.PRIVATE, null,
    190                         DummyPrincipal.DUMMY_CREDENTIALS);
    191193
    192194        String profileContent = "";
     
    205207        profileContent += "</CMD_ComponentSpec>";
    206208
     209        // Public Registry
    207210        ProfileDescription desc = ProfileDescription.createNewDescription();
    208211        MDValidator validator = new MDValidator(new ByteArrayInputStream(
    209                 profileContent.getBytes()), desc, userRegistry, userRegistry,
    210                 publicRegistry, marshaller);
    211         assertFalse(validator.validate());
    212         assertEquals(2, validator.getErrorMessages().size());
     212                profileContent.getBytes()), desc, testRegistry, marshaller);
     213        assertFalse(validator.validate());
     214        assertEquals(1, validator.getErrorMessages().size()); // the exception is thrown and propagated on the first non-registered component
    213215        assertTrue(validator.getErrorMessages().get(0)
    214216                .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR));
    215         assertTrue(validator.getErrorMessages().get(1)
    216                 .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR));
    217 
    218         RegistryTestHelper.addComponent(userRegistry, id1);
    219         RegistryTestHelper.addComponent(publicRegistry, id2);
     217       
     218       
     219        // registering publically the first component
     220        RegistryTestHelper.addComponent(testRegistry, id1, true);
     221        validator = new MDValidator(new ByteArrayInputStream(
     222                profileContent.getBytes()), desc, testRegistry, marshaller);
     223        assertFalse(validator.validate());
     224        assertEquals(1, validator.getErrorMessages().size()); // the exception is thrown on the second non-registered component
     225        assertTrue(validator.getErrorMessages().get(0)
     226                .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR));
     227       
     228       
     229       
     230        RegistryTestHelper.addComponent(testRegistry, id2, false);
    220231        validator = new MDValidator(new ByteArrayInputStream(
    221                 profileContent.getBytes()), desc, publicRegistry, null,
    222                 publicRegistry, marshaller);
     232                profileContent.getBytes()), desc, testRegistry, marshaller);
    223233        assertFalse(validator.validate());
    224234        assertEquals(1, validator.getErrorMessages().size());
     
    226236                .getErrorMessages()
    227237                .get(0)
    228                 .startsWith(MDValidator.COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR));
    229 
     238                .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_IN_APPROPRIATE_SPACE_ERROR));
     239       
     240       
     241        // make it user registry
     242        testRegistry.setRegistryOwner(new OwnerUser(user.getId()));
     243        testRegistry.setRegistrySpace(RegistrySpace.PRIVATE);       
     244       
     245       
    230246        validator = new MDValidator(new ByteArrayInputStream(
    231                 profileContent.getBytes()), desc, userRegistry, userRegistry,
    232                 publicRegistry, marshaller);
    233         assertTrue(validator.validate());
     247                profileContent.getBytes()), desc, testRegistry,  marshaller);
     248        Boolean result =validator.validate();
     249        assertTrue(result);
    234250        assertEquals(0, validator.getErrorMessages().size());
    235251    }
     
    254270        ComponentDescription desc = ComponentDescription.createNewDescription();
    255271        MDValidator validator = new MDValidator(new ByteArrayInputStream(
    256                 content.getBytes()), desc, publicRegistry, null,
    257                 publicRegistry, marshaller);
     272                content.getBytes()), desc, testRegistry, marshaller);
    258273        assertFalse(validator.validate());
    259274        assertEquals(1, validator.getErrorMessages().size());
     
    261276                .getErrorMessages()
    262277                .get(0)
    263                 .startsWith(MDValidator.COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR));
    264 
    265         RegistryTestHelper.addComponent(publicRegistry, id1);
     278                .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR));
     279
     280        RegistryTestHelper.addComponent(testRegistry, id1, true);
    266281        validator = new MDValidator(
    267                 new ByteArrayInputStream(content.getBytes()), desc,
    268                 publicRegistry, null, publicRegistry, marshaller);
     282                new ByteArrayInputStream(content.getBytes()), desc,testRegistry, marshaller);
    269283        assertTrue(validator.validate());
    270284        assertEquals(0, validator.getErrorMessages().size());
     
    280294
    281295        ProfileDescription desc = ProfileDescription.createNewDescription();
    282         MDValidator validator = new MDValidator(input, desc, publicRegistry,
    283                 null, publicRegistry, marshaller);
     296        MDValidator validator = new MDValidator(input, desc, testRegistry,marshaller);
    284297
    285298        // Spec is created during validation, before it should be null
     
    309322        InputStream input = new ByteArrayInputStream(profileContent.getBytes());
    310323        ProfileDescription desc = ProfileDescription.createNewDescription();
    311         MDValidator validator = new MDValidator(input, desc, publicRegistry,
    312                 null, publicRegistry, marshaller);
     324        desc.setPublic(true);
     325        desc.setName("test_name");
     326        MDValidator validator = new MDValidator(input, desc, testRegistry, marshaller);
    313327        return validator;
    314328    }
Note: See TracChangeset for help on using the changeset viewer.