Ignore:
Timestamp:
08/12/14 14:59:10 (10 years ago)
Author:
olhsha@mpi.nl
Message:

Fixed exception that have appeared while unit-testing

File:
1 edited

Legend:

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

    r5549 r5550  
    1212import clarin.cmdi.componentregistry.model.ComponentDescription;
    1313import clarin.cmdi.componentregistry.model.ProfileDescription;
    14 import clarin.cmdi.componentregistry.model.RegisterResponse;
    1514import com.sun.jersey.api.client.ClientResponse;
    1615import com.sun.jersey.multipart.FormDataMultiPart;
     16import java.util.List;
     17import javax.ws.rs.WebApplicationException;
    1718import javax.ws.rs.core.MediaType;
    1819import org.junit.Before;
     
    2122import org.springframework.jdbc.core.JdbcTemplate;
    2223
    23 import org.springframework.util.Assert;
    2424import static org.junit.Assert.*;
     25
    2526/**
    2627 *
    2728 * @author olhsha
    2829 */
    29 public class SanboxTest extends
    30         ComponentRegistryRestServiceTestCase {
     30public class SanboxTest extends ComponentRegistryRestServiceTestCase {
    3131
    3232    @Autowired
     
    4040    @Before
    4141    public void init() {
    42         ComponentRegistryTestDatabase.resetAndCreateAllTables(jdbcTemplate);
    43         createUserRecord();
    44         baseRegistry = componentRegistryFactory.getBaseRegistry(DummyPrincipal.DUMMY_CREDENTIALS);
     42        ComponentRegistryTestDatabase.resetAndCreateAllTables(jdbcTemplate);
     43        createUserRecord();
     44        baseRegistry = componentRegistryFactory.getBaseRegistry(DummyPrincipal.DUMMY_CREDENTIALS);
    4545    }
    4646
    4747    private String expectedUserId(String principal) {
    48         return getUserDao().getByPrincipalName(principal).getId().toString();
     48        return getUserDao().getByPrincipalName(principal).getId().toString();
    4949    }
    50 
    5150    private ComponentDescription component1;
    5251    private ComponentDescription component2;
    5352    private ProfileDescription profile1;
    5453    private ProfileDescription profile2;
    55    
    5654    private ComponentDescription component3;
    5755    private ProfileDescription profile3;
    58    
    5956    private Comment profile1Comment1;
    6057    private Comment profile1Comment2;
    6158    private Comment component1Comment3;
    6259    private Comment component1Comment4;
    63    
    6460    private Comment profile3Comment5;
    6561    private Comment component3Comment7;
    6662
    6763    private void fillUpPublicItems() throws Exception {
    68        
    69         profile1 = RegistryTestHelper.addProfile(baseRegistry, "profile2", true);
    70         profile2 = RegistryTestHelper.addProfile(baseRegistry, "profile1", true);
    71         component1 = RegistryTestHelper.addComponent(baseRegistry,
    72                 "component2", true);
    73         component2 = RegistryTestHelper.addComponent(baseRegistry,
    74                 "component1", true);
    75         profile1Comment2 = RegistryTestHelper.addComment(baseRegistry, "comment2",
    76                 ProfileDescription.PROFILE_PREFIX + "profile1",
    77                 "JUnit@test.com");
    78         profile1Comment1 = RegistryTestHelper.addComment(baseRegistry, "comment1",
    79                 ProfileDescription.PROFILE_PREFIX + "profile1",
    80                 "JUnit@test.com");
    81         component1Comment3 = RegistryTestHelper.addComment(baseRegistry, "comment3",
    82                 ComponentDescription.COMPONENT_PREFIX + "component1",
    83                 "JUnit@test.com");
    84         component1Comment4 = RegistryTestHelper.addComment(baseRegistry, "comment4",
    85                 ComponentDescription.COMPONENT_PREFIX + "component1",
    86                 "JUnit@test.com");
    87     }
    88    
    89     private void fillUpPrivateItems() throws Exception {
    90         profile3 = RegistryTestHelper.addProfile(baseRegistry, "profile3", false);
    91         component3 = RegistryTestHelper.addComponent(baseRegistry,
    92                 "component3", false);
    93         profile3Comment5 = RegistryTestHelper.addComment(baseRegistry, "comment5",
    94                 ProfileDescription.PROFILE_PREFIX + "profile3",
    95                 "JUnit@test.com");
    96         component3Comment7 = RegistryTestHelper.addComment(baseRegistry, "comment7",
    97                 ComponentDescription.COMPONENT_PREFIX + "component3",
    98                 "JUnit@test.com");
    99     }
    100    
    101        @Test
    102     public void testCreatePrivateComponentWithRecursion() throws Exception {
    103        
    104         fillUpPrivateItems();
    105         // Create new componet
    106         FormDataMultiPart form = createFormData(RegistryTestHelper
    107                 .getComponentTestContent());
    108         ClientResponse cResponse = getAuthenticatedResource(getResource().path("/registry/components")).type(
    109                 MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
    110         assertEquals(ClientResponse.Status.OK.getStatusCode(),
    111                 cResponse.getStatus());
    112         RegisterResponse response = cResponse.getEntity(RegisterResponse.class);
    113         assertTrue(response.isRegistered());
    114         ComponentDescription desc = (ComponentDescription) response
    115                 .getDescription();
    11664
    117         // Re-define with self-recursion
    118         String compContent = "";
    119         compContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    120         compContent += "\n";
    121         compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
    122         compContent += "    xsi:noNamespaceSchemaLocation=\"../../general-component-schema.xsd\">\n";
    123         compContent += "    \n";
    124         compContent += "    <Header/>\n";
    125         compContent += "    \n";
    126         compContent += "    <CMD_Component name=\"Nested\" CardinalityMin=\"1\" CardinalityMax=\"1\">\n";
    127         compContent += "        <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n";
    128         compContent += "        <CMD_Component ComponentId=\""
    129                 + desc.getId()
    130                 + "\" name=\"Recursive\" CardinalityMin=\"1\" CardinalityMax=\"1\" />\n";
    131         compContent += "    </CMD_Component>\n";
    132         compContent += "\n";
    133         compContent += "</CMD_ComponentSpec>\n";
    134 
    135         // Update component
    136         form = createFormData(
    137                 RegistryTestHelper.getComponentContentAsStream(compContent),
    138                 "UPDATE DESCRIPTION!");
    139         cResponse = getAuthenticatedResource(getResource().path(
    140                         "/registry/components/" + desc.getId() + "/update")).type(
    141                 MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
    142         assertEquals(ClientResponse.Status.OK.getStatusCode(),
    143                 cResponse.getStatus());
    144         response = cResponse.getEntity(RegisterResponse.class);
    145         assertFalse("Recursive definition should fail", response.isRegistered());
    146         assertEquals("There should be an error message for the recursion", 1,
    147                 response.getErrors().size());
    148         assertTrue(
    149                 "There error message should specify the point of recursion",
    150                 response.getErrors().get(0)
    151                         .contains("already contains " + desc.getId()));
    152 
     65        profile1 = RegistryTestHelper.addProfile(baseRegistry, "profile2", true);
     66        profile2 = RegistryTestHelper.addProfile(baseRegistry, "profile1", true);
     67        component1 = RegistryTestHelper.addComponent(baseRegistry,
     68                "component2", true);
     69        component2 = RegistryTestHelper.addComponent(baseRegistry,
     70                "component1", true);
     71        profile1Comment2 = RegistryTestHelper.addComment(baseRegistry, "comment2",
     72                ProfileDescription.PROFILE_PREFIX + "profile1",
     73                "JUnit@test.com");
     74        profile1Comment1 = RegistryTestHelper.addComment(baseRegistry, "comment1",
     75                ProfileDescription.PROFILE_PREFIX + "profile1",
     76                "JUnit@test.com");
     77        component1Comment3 = RegistryTestHelper.addComment(baseRegistry, "comment3",
     78                ComponentDescription.COMPONENT_PREFIX + "component1",
     79                "JUnit@test.com");
     80        component1Comment4 = RegistryTestHelper.addComment(baseRegistry, "comment4",
     81                ComponentDescription.COMPONENT_PREFIX + "component1",
     82                "JUnit@test.com");
    15383    }
    15484
    155      private FormDataMultiPart createFormData(Object content) {
    156         return createFormData(content, "My Test");
     85    private void fillUpPrivateItems() throws Exception {
     86        profile3 = RegistryTestHelper.addProfile(baseRegistry, "profile3", false);
     87        component3 = RegistryTestHelper.addComponent(baseRegistry,
     88                "component3", false);
     89        profile3Comment5 = RegistryTestHelper.addComment(baseRegistry, "comment5",
     90                ProfileDescription.PROFILE_PREFIX + "profile3",
     91                "JUnit@test.com");
     92        component3Comment7 = RegistryTestHelper.addComment(baseRegistry, "comment7",
     93                ComponentDescription.COMPONENT_PREFIX + "component3",
     94                "JUnit@test.com");
     95    }
     96
     97    @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(500, 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");
    157142    }
    158143
    159144    private FormDataMultiPart createFormData(Object content, String description) {
    160         FormDataMultiPart form = new FormDataMultiPart();
    161         form.field(IComponentRegistryRestService.DATA_FORM_FIELD, content,
    162                 MediaType.APPLICATION_OCTET_STREAM_TYPE);
    163         form.field(IComponentRegistryRestService.NAME_FORM_FIELD, "Test1");
    164         form.field(IComponentRegistryRestService.DESCRIPTION_FORM_FIELD,
    165                 description);
    166         form.field(IComponentRegistryRestService.DOMAIN_FORM_FIELD, "My domain");
    167         form.field(IComponentRegistryRestService.GROUP_FORM_FIELD, "TestGroup");
    168         return form;
     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;
    169154    }
    170155}
Note: See TracChangeset for help on using the changeset viewer.