Search in sources :

Example 1 with SiteUpdate

use of org.alfresco.rest.api.model.SiteUpdate in project alfresco-remote-api by Alfresco.

the class TestSites method testUpdateSite.

@Test
public void testUpdateSite() throws PublicApiException {
    Sites sitesProxy = publicApiClient.sites();
    publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));
    // +ve test: simple happy path with successful update.
    {
        SiteImpl siteToCreate = new SiteImpl("Initial Title", SiteVisibility.PRIVATE.toString());
        siteToCreate.setRole(SiteRole.SiteManager);
        siteToCreate.setDescription("This is the initial description for the site");
        Site site = sitesProxy.createSite(siteToCreate, 201);
        // Testing with the actual JSON reveals more than testing with rich POJOs.
        // It shows us exactly what the input is and is easy to compare to the Open API spec.
        String updateJSON = "{\n" + "  \"title\": \"Updated Title\",\n" + "  \"description\": \"This is an updated description for the site\",\n" + "  \"visibility\": \"PUBLIC\"\n" + "}";
        // Update the site details
        HttpResponse response = sitesProxy.update("sites", site.getSiteId(), null, null, updateJSON, "Failed to update site " + site.getSiteId());
        Site updatedSite = SiteImpl.parseSite((JSONObject) response.getJsonResponse().get("entry"));
        Site expectedUpdate = new SiteImpl(null, "initial-title", site.getGuid(), "Updated Title", "This is an updated description for the site", SiteVisibility.PUBLIC.toString(), null, SiteRole.SiteManager);
        // Check the updated site is as expected.
        expectedUpdate.expected(updatedSite);
        // Double check by do a fresh get of the details, since the details returned by updateSite()
        // aren't necessarily retrieved from the DB.
        Site fresh = sitesProxy.getSite(site.getSiteId(), 200);
        expectedUpdate.expected(fresh);
        removeSiteQuietly(site.getSiteId());
    }
    // +ve test: remove the description.
    // This is a workaround for being unable to explicitly null the field (see REPO-1409)
    {
        Site site = sitesProxy.createSite(new SiteImpl(null, "NoDescriptionTest", "Initial description", SiteVisibility.PUBLIC.toString()), 201);
        Site updatedSite = sitesProxy.updateSite(site.getSiteId(), new SiteUpdate("title", "", SiteVisibility.PUBLIC), 200);
        // Note the null value for description, as the framework
        // treats empty strings and null as equivalent in responses, and does not return
        // those fields (as per API guidelines)
        Site expectedUpdate = new SiteImpl(null, "nodescriptiontest", site.getGuid(), "title", null, SiteVisibility.PUBLIC.toString(), null, SiteRole.SiteManager);
        // Check the return from updateSite(...)
        expectedUpdate.expected(updatedSite);
        // TODO improve expected (and/or AssertUtil) and affected test cases
        Assert.assertNull(updatedSite.getDescription());
        // Double check a fresh retrieval matches.
        Site fresh = sitesProxy.getSite(site.getSiteId(), 200);
        expectedUpdate.expected(fresh);
        // TODO improve expected (and/or AssertUtil) and affected test cases
        Assert.assertNull(fresh.getDescription());
        removeSiteQuietly(site.getSiteId());
    }
    // +ve test: partial updates must be allowed
    {
        Site site = sitesProxy.createSite(new SiteImpl("partial-updates-test", "title-v1", "description-v1", SiteVisibility.PUBLIC.toString()), 201);
        // Check we can update just the title
        Site updatedSite = sitesProxy.updateSite(site.getSiteId(), new SiteUpdate("title-v2", null, null), 200);
        Site expectedUpdate = new SiteImpl(null, "partial-updates-test", site.getGuid(), "title-v2", "description-v1", SiteVisibility.PUBLIC.toString(), null, SiteRole.SiteManager);
        expectedUpdate.expected(updatedSite);
        // Check we can update just the description
        updatedSite = sitesProxy.updateSite(site.getSiteId(), new SiteUpdate(null, "description-v2", null), 200);
        expectedUpdate = new SiteImpl(null, "partial-updates-test", site.getGuid(), "title-v2", "description-v2", SiteVisibility.PUBLIC.toString(), null, SiteRole.SiteManager);
        expectedUpdate.expected(updatedSite);
        // Check we can update just the visibility
        updatedSite = sitesProxy.updateSite(site.getSiteId(), new SiteUpdate(null, null, SiteVisibility.PRIVATE), 200);
        expectedUpdate = new SiteImpl(null, "partial-updates-test", site.getGuid(), "title-v2", "description-v2", SiteVisibility.PRIVATE.toString(), null, SiteRole.SiteManager);
        expectedUpdate.expected(updatedSite);
        removeSiteQuietly(site.getSiteId());
    }
    // -ve test: 400, bad requests
    {
        Site site = sitesProxy.createSite(new SiteImpl("Site Update Bad Request Tests", SiteVisibility.PRIVATE.toString()), 201);
        // long title
        sitesProxy.updateSite(site.getSiteId(), new SiteUpdate(new String(new char[257]).replace('\0', 'a'), "description", SiteVisibility.PUBLIC), 400);
        // long description
        sitesProxy.updateSite(site.getSiteId(), new SiteUpdate("title", new String(new char[513]).replace('\0', 'a'), SiteVisibility.PUBLIC), 400);
        // Invalid visibility
        sitesProxy.update("sites", site.getSiteId(), null, null, "{\n" + "  \"title\": \"Updated Title\",\n" + "  \"description\": \"This is an updated description for the site\",\n" + "  \"visibility\": \"INVALID_VISIBILITY\"\n" + "}", null, "Expected 400 response when updating " + site.getSiteId(), 400);
        // Invalid fields
        // Check that id, guid and role are not silently ignored. This is until REPO-110
        // is implemented, since we currently have to bind to Site rather than SiteUpdate in
        // SiteEntityResource.update
        sitesProxy.update("sites", site.getSiteId(), null, null, "{\n" + "  \"id\": \"a-new-id\"," + "  \"title\": \"Updated Title\"\n" + "}", null, "Expected 400 response when updating " + site.getSiteId(), 400);
        sitesProxy.update("sites", site.getSiteId(), null, null, "{\n" + "  \"guid\": \"76ba60c1-f05b-406a-86a4-4eeb1bb49aaa\"" + "}", null, "Expected 400 response when updating " + site.getSiteId(), 400);
        sitesProxy.update("sites", site.getSiteId(), null, null, "{\n" + "  \"role\": \"SiteConsumer\"" + "}", null, "Expected 400 response when updating " + site.getSiteId(), 400);
        sitesProxy.update("sites", site.getSiteId(), null, null, "{\n" + "  \"preset\": \"sitePreset\"" + "}", null, "Expected 400 response when updating " + site.getSiteId(), 400);
        // Details should not have changed.
        Site fresh = sitesProxy.getSite(site.getSiteId(), 200);
        site.expected(fresh);
        removeSiteQuietly(site.getSiteId());
    }
    // -ve test: 404 for non-existent site
    {
        // Attempt to update site details using an invalid site ID.
        sitesProxy.updateSite(// Non-existent site.
        UUID.randomUUID().toString(), new SiteUpdate("Updated title", "Updated description", SiteVisibility.PUBLIC), HttpStatus.SC_NOT_FOUND);
    }
    // -ve test: 401 authorization failure
    {
        Site site = sitesProxy.createSite(new SiteImpl("SiteUpdate401", SiteVisibility.PRIVATE.toString()), 201);
        // Invalid auth details
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), GUID.generate(), "password"));
        // Update the site details
        sitesProxy.updateSite(site.getSiteId(), new SiteUpdate("Updated Title", "Updated description", SiteVisibility.PUBLIC), HttpStatus.SC_UNAUTHORIZED);
        // Valid authentication again
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));
        // Check the details haven't changed.
        Site fresh = sitesProxy.getSite(site.getSiteId(), 200);
        site.expected(fresh);
        removeSiteQuietly(site.getSiteId());
    }
    // -ve test: 403 for incorrect permissions
    {
        Site site = sitesProxy.createSite(new SiteImpl("SiteUpdate403", SiteVisibility.PUBLIC.toString()), 201);
        // Update as person2
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2Id));
        // Update the site details
        sitesProxy.updateSite(site.getSiteId(), new SiteUpdate("Updated Title", "Updated description", SiteVisibility.PUBLIC), HttpStatus.SC_FORBIDDEN);
        // Check details haven't changed as original creator.
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));
        Site fresh = sitesProxy.getSite(site.getSiteId(), 200);
        site.expected(fresh);
        removeSiteQuietly(site.getSiteId());
    }
}
Also used : TestSite(org.alfresco.rest.api.tests.RepoService.TestSite) JSONObject(org.json.simple.JSONObject) SiteUpdate(org.alfresco.rest.api.model.SiteUpdate) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) RequestContext(org.alfresco.rest.api.tests.client.RequestContext) Sites(org.alfresco.rest.api.tests.client.PublicApiClient.Sites) Test(org.junit.Test)

Example 2 with SiteUpdate

use of org.alfresco.rest.api.model.SiteUpdate in project records-management by Alfresco.

the class RMSitesImplUnitTest method updateRMSite.

@Test
public void updateRMSite() throws Exception {
    String siteId = RM_SITE_ID;
    SiteInfo mockedSiteInfo = mock(SiteInfo.class);
    NodeRef siteNodeRef = AlfMock.generateNodeRef(mockedNodeService);
    // mock SiteInfo
    when(mockedSiteInfo.getShortName()).thenReturn(siteId);
    when(mockedSiteInfo.getNodeRef()).thenReturn(siteNodeRef);
    when(mockedSiteInfo.getDescription()).thenReturn(RM_SITE_DESCRIPTION).thenReturn(RM_SITE_DESCRIPTION_AFTER_UPDATE);
    when(mockedSiteInfo.getTitle()).thenReturn(RM_SITE_TITLE).thenReturn(RM_SITE_TITLE_AFTER_UPDATE);
    when(mockedSiteInfo.getVisibility()).thenReturn(SiteVisibility.PUBLIC);
    when(mockedNodeService.getType(siteNodeRef)).thenReturn(RecordsManagementModel.TYPE_RM_SITE);
    when(mockedSiteService.getSite(siteId)).thenReturn(mockedSiteInfo);
    when(mockedSiteService.getMembersRole(eq(siteId), any(String.class))).thenReturn(RM_SITE_MANAGER_ROLE);
    // mock UpdateSite
    SiteUpdate mockedSiteUpdate = mock(SiteUpdate.class);
    when(mockedSiteUpdate.getDescription()).thenReturn(RM_SITE_DESCRIPTION_AFTER_UPDATE);
    when(mockedSiteUpdate.getTitle()).thenReturn(RM_SITE_TITLE_AFTER_UPDATE);
    when(mockedSiteUpdate.getVisibility()).thenReturn(null);
    when(mockedSiteUpdate.wasSet(Site.TITLE)).thenReturn(true);
    when(mockedSiteUpdate.wasSet(Site.DESCRIPTION)).thenReturn(true);
    // mock Parameters
    Parameters mockedParameters = mock(Parameters.class);
    // call updateRMSite method
    RMSite updatedRMSite = rmSitesImpl.updateRMSite(siteId, mockedSiteUpdate, mockedParameters);
    // check if the new title is set to siteInfo
    ArgumentCaptor<String> titleCaptor = ArgumentCaptor.forClass(String.class);
    verify(mockedSiteInfo, times(1)).setTitle(titleCaptor.capture());
    assertEquals(RM_SITE_TITLE_AFTER_UPDATE, titleCaptor.getValue());
    // check that new description is set to siteInfo
    ArgumentCaptor<String> descriptionCaptor = ArgumentCaptor.forClass(String.class);
    verify(mockedSiteInfo, times(1)).setDescription(descriptionCaptor.capture());
    assertEquals(RM_SITE_DESCRIPTION_AFTER_UPDATE, descriptionCaptor.getValue());
    // check that site visibility is not changed
    verify(mockedSiteInfo, never()).setVisibility(any(SiteVisibility.class));
    // check that updateSite is called
    verify(mockedSiteService, times(1)).updateSite(any(SiteInfo.class));
    // verify returned values for RM site are the right ones
    assertEquals(RMSiteCompliance.STANDARD, updatedRMSite.getCompliance());
    assertEquals(RM_SITE_MANAGER_ROLE, updatedRMSite.getRole());
    assertEquals(siteId, updatedRMSite.getId());
    assertEquals(siteNodeRef.getId(), updatedRMSite.getGuid());
    assertEquals(RM_SITE_DESCRIPTION_AFTER_UPDATE, updatedRMSite.getDescription());
    assertEquals(RM_SITE_TITLE_AFTER_UPDATE, updatedRMSite.getTitle());
    assertEquals(SiteVisibility.PUBLIC, updatedRMSite.getVisibility());
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Parameters(org.alfresco.rest.framework.resource.parameters.Parameters) RMSite(org.alfresco.rm.rest.api.model.RMSite) SiteUpdate(org.alfresco.rest.api.model.SiteUpdate) SiteVisibility(org.alfresco.service.cmr.site.SiteVisibility) BaseUnitTest(org.alfresco.module.org_alfresco_module_rm.test.util.BaseUnitTest) Test(org.junit.Test)

Aggregations

SiteUpdate (org.alfresco.rest.api.model.SiteUpdate)2 Test (org.junit.Test)2 BaseUnitTest (org.alfresco.module.org_alfresco_module_rm.test.util.BaseUnitTest)1 TestSite (org.alfresco.rest.api.tests.RepoService.TestSite)1 HttpResponse (org.alfresco.rest.api.tests.client.HttpResponse)1 Sites (org.alfresco.rest.api.tests.client.PublicApiClient.Sites)1 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)1 Parameters (org.alfresco.rest.framework.resource.parameters.Parameters)1 RMSite (org.alfresco.rm.rest.api.model.RMSite)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 SiteInfo (org.alfresco.service.cmr.site.SiteInfo)1 SiteVisibility (org.alfresco.service.cmr.site.SiteVisibility)1 JSONObject (org.json.simple.JSONObject)1