Example usage for org.apache.commons.httpclient HttpStatus SC_NOT_FOUND

List of usage examples for org.apache.commons.httpclient HttpStatus SC_NOT_FOUND

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpStatus SC_NOT_FOUND.

Prototype

int SC_NOT_FOUND

To view the source code for org.apache.commons.httpclient HttpStatus SC_NOT_FOUND.

Click Source Link

Document

<tt>404 Not Found</tt> (HTTP/1.0 - RFC 1945)

Usage

From source file:org.alfresco.dataprep.SitePagesService.java

/**
 * Delete comment from an item//from   w ww .ja  va  2s.c  o  m
 * @param userName String user name
 * @param password String password
 * @param siteName String site name
 * @param page Page page
 * @param itemTitle String item title
 * @param comment comment to delete
 * @return true if deleted
 */
private boolean deleteComment(final String userName, final String password, final String siteName,
        final Page page, final String itemTitle, final String comment) {
    String nodeRef = getCommentNodeRef(userName, password, siteName, page, itemTitle, comment);
    AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
    String url = client.getApiUrl() + "comment/node/" + nodeRef.replaceFirst(":/", "");
    HttpDelete delete = new HttpDelete(url);
    HttpResponse response = client.executeRequest(userName, password, delete);
    switch (response.getStatusLine().getStatusCode()) {
    case HttpStatus.SC_OK:
        return true;
    case HttpStatus.SC_NOT_FOUND:
        throw new RuntimeException("Comment doesn't exists " + comment);
    default:
        logger.error("Unable to delete comment: " + response.toString());
        break;
    }
    return false;
}

From source file:org.alfresco.integrations.google.docs.webscripts.DiscardContent.java

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    getGoogleDocsServiceSubsystem();//from  w  ww  .jav  a2s.  co  m

    Map<String, Object> model = new HashMap<String, Object>();

    Map<String, Serializable> map = parseContent(req);
    final NodeRef nodeRef = (NodeRef) map.get(JSON_KEY_NODEREF);

    if (nodeService.hasAspect(nodeRef, GoogleDocsModel.ASPECT_EDITING_IN_GOOGLE)) {
        try {
            boolean deleted = false;

            if (!Boolean.valueOf(map.get(JSON_KEY_OVERRIDE).toString())) {
                SiteInfo siteInfo = siteService.getSite(nodeRef);
                if (siteInfo == null
                        || siteService.isMember(siteInfo.getShortName(), AuthenticationUtil.getRunAsUser())) {

                    if (googledocsService.hasConcurrentEditors(nodeRef)) {
                        throw new WebScriptException(HttpStatus.SC_CONFLICT,
                                "Node: " + nodeRef.toString() + " has concurrent editors.");
                    }
                } else {
                    throw new AccessDeniedException(
                            "Access Denied.  You do not have the appropriate permissions to perform this operation.");
                }
            }

            deleted = delete(nodeRef);

            model.put(MODEL_SUCCESS, deleted);

        } catch (InvalidNodeRefException ine) {
            throw new WebScriptException(HttpStatus.SC_NOT_FOUND, ine.getMessage());
        } catch (IOException ioe) {
            throw new WebScriptException(HttpStatus.SC_INTERNAL_SERVER_ERROR, ioe.getMessage());
        } catch (GoogleDocsAuthenticationException gdae) {
            throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdae.getMessage());
        } catch (GoogleDocsRefreshTokenException gdrte) {
            throw new WebScriptException(HttpStatus.SC_BAD_GATEWAY, gdrte.getMessage());
        } catch (GoogleDocsServiceException gdse) {
            if (gdse.getPassedStatusCode() > -1) {
                throw new WebScriptException(gdse.getPassedStatusCode(), gdse.getMessage());
            } else {
                throw new WebScriptException(gdse.getMessage());
            }
        } catch (AccessDeniedException ade) {
            // This code will make changes after the rollback has occurred to clean up the node: remove the lock and the Google
            // Docs aspect. If it has the temporary aspect it will also remove the node from Alfresco
            AlfrescoTransactionSupport.bindListener(new TransactionListenerAdapter() {
                public void afterRollback() {
                    transactionService.getRetryingTransactionHelper()
                            .doInTransaction(new RetryingTransactionCallback<Object>() {
                                public Object execute() throws Throwable {
                                    DriveFile driveFile = googledocsService.getDriveFile(nodeRef);
                                    googledocsService.unlockNode(nodeRef);
                                    boolean deleted = googledocsService.deleteContent(nodeRef, driveFile);

                                    if (deleted) {
                                        AuthenticationUtil
                                                .runAsSystem(new AuthenticationUtil.RunAsWork<Object>() {
                                                    public Object doWork() throws Exception {
                                                        if (nodeService.hasAspect(nodeRef,
                                                                ContentModel.ASPECT_TEMPORARY)) {
                                                            nodeService.deleteNode(nodeRef);
                                                        }

                                                        return null;
                                                    }
                                                });
                                    }

                                    return null;
                                }
                            }, false, true);
                }
            });

            throw new WebScriptException(HttpStatus.SC_FORBIDDEN, ade.getMessage(), ade);
        } catch (Exception e) {
            throw new WebScriptException(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage(), e);
        }
    } else {
        throw new WebScriptException(HttpStatus.SC_NOT_ACCEPTABLE,
                "Missing Google Docs Aspect on " + nodeRef.toString());
    }

    return model;
}

From source file:org.alfresco.repo.jive.rest.GetCommunitiesWebScript.java

/**
 * @see org.springframework.extensions.webscripts.DeclarativeWebScript#executeImpl(org.springframework.extensions.webscripts.WebScriptRequest, org.springframework.extensions.webscripts.Status, org.springframework.extensions.webscripts.Cache)
 *//*from   w  ww  . ja  v  a  2 s . co m*/
@Override
protected Map<String, Object> executeImpl(final WebScriptRequest req, final Status status, final Cache cache) {
    if (log.isTraceEnabled())
        log.trace("GetCommunitiesWebScript.executeImpl called");

    final Map<String, Object> result = new HashMap<String, Object>();
    final String communityIdAsStr = parseCommunityId(req.getExtensionPath());
    List<JiveCommunity> subCommunities = null;

    cache.setNeverCache(true);

    if (communityIdAsStr != null && communityIdAsStr.trim().length() > 0) {
        // We got a community Id in the request, so retrieve its subcommunities
        long communityId = -1;

        try {
            communityId = Long.parseLong(communityIdAsStr);
        } catch (final NumberFormatException nfe) {
            throw new WebScriptException(HttpStatus.SC_BAD_REQUEST,
                    "Could not parse community Id from value '" + communityIdAsStr + "'.", nfe);
        }

        try {
            if (log.isDebugEnabled())
                log.debug("Retrieving sub-communities of Jive community " + communityId);
            result.put("root", false);
            subCommunities = jiveService.getSubCommunities(communityId);
        } catch (final InvalidCommunityException ice) {
            throw new WebScriptException(HttpStatus.SC_NOT_FOUND, ice.getMessage(), ice);
        } catch (final ServiceUnavailableException sue) {
            throw new WebScriptException(HttpStatus.SC_SERVICE_UNAVAILABLE, sue.getMessage(), sue);
        }
    } else {
        try {
            // No community id provided, so retrieve the root-level communities
            if (log.isDebugEnabled())
                log.debug("Retrieving root-level communities from Jive.");
            result.put("root", true);
            subCommunities = jiveService.getCommunities();
        } catch (final ServiceUnavailableException sue) {
            throw new WebScriptException(HttpStatus.SC_SERVICE_UNAVAILABLE, sue.getMessage(), sue);
        }
    }

    result.put("subCommunities", subCommunities);

    return (result);
}

From source file:org.alfresco.repo.jive.rest.SocializeDocumentsWebScript.java

/**
 * @see org.springframework.extensions.webscripts.DeclarativeWebScript#executeImpl(org.springframework.extensions.webscripts.WebScriptRequest, org.springframework.extensions.webscripts.Status, org.springframework.extensions.webscripts.Cache)
 *///from w  w  w.j ava 2 s .c  o m
@Override
protected Map<String, Object> executeImpl(final WebScriptRequest req, final Status status, final Cache cache) {
    if (log.isTraceEnabled())
        log.trace("SocializeDocumentsWebScript.executeImpl called");

    Map<String, Object> result = null;
    List<NodeRef> nodeRefs = parseNodeRefs(req);
    long communityId = parseCommunityId(req);

    cache.setNeverCache(true);

    try {
        if (log.isDebugEnabled())
            log.debug("Socializing documents " + Arrays.toString(nodeRefs.toArray()) + " to Jive community "
                    + communityId);
        jiveService.socializeDocuments(nodeRefs, communityId);
    } catch (final FileNotFoundException fnfe) {
        throw new WebScriptException(HttpStatus.SC_NOT_FOUND, fnfe.getMessage(), fnfe);
    } catch (final NotAFileException nafe) {
        throw new WebScriptException(HttpStatus.SC_BAD_REQUEST, nafe.getMessage(), nafe);
    } catch (final JiveServiceException jse) {
        throw new WebScriptException(HttpStatus.SC_BAD_REQUEST, jse.getMessage(), jse);
    } catch (final ServiceUnavailableException sue) {
        throw new WebScriptException(HttpStatus.SC_SERVICE_UNAVAILABLE, sue.getMessage(), sue);
    } catch (final DocumentSizeException dse) {
        throw new WebScriptException(HttpStatus.SC_CONFLICT, dse.getMessage(), dse);
    }

    return (result);
}

From source file:org.alfresco.rest.api.tests.ModulePackagesApiTest.java

@Test
public void testSingleModulePackage() throws Exception {
    setRequestContext(nonAdminUserName);

    HttpResponse response = getSingle(MODULEPACKAGES, "NonSENSE_NOTFOUND", HttpStatus.SC_NOT_FOUND);
    assertNotNull(response);//from   ww  w  .j a v  a 2 s  . c o  m

    response = getSingle(MODULEPACKAGES, "alfresco-simple-module", HttpStatus.SC_OK);
    assertNotNull(response);

    ModulePackage simpleModule = parseRestApiEntry(response.getJsonResponse(), ModulePackage.class);
    assertNotNull(simpleModule);
    assertTrue("Simple module must be the correct version",
            "1.0.0-SNAPSHOT".equals(simpleModule.getVersion().toString()));
}

From source file:org.alfresco.rest.api.tests.ModulePackagesApiTest.java

@Test
public void testErrorUrls() throws Exception {
    setRequestContext(null);/*from  w  w w  .  j ava2 s . c om*/

    Map<String, String> params = createParams(null, null);

    //Call an endpoint that doesn't exist
    HttpResponse response = publicApiClient.get(getScope(),
            MODULEPACKAGES + "/fred/blogs/king/kong/got/if/wrong", null, null, null, params);
    assertNotNull(response);
    assertEquals(HttpStatus.SC_NOT_FOUND, response.getStatusCode());
    assertEquals("no-cache", response.getHeaders().get("Cache-Control"));
    assertEquals("application/json;charset=UTF-8", response.getHeaders().get("Content-Type"));

    PublicApiClient.ExpectedErrorResponse errorResponse = RestApiUtil
            .parseErrorResponse(response.getJsonResponse());
    assertNotNull(errorResponse);
    assertNotNull(errorResponse.getErrorKey());
    assertNotNull(errorResponse.getBriefSummary());
}

From source file:org.alfresco.rest.api.tests.TestActivities.java

@Test
public void testPersonActivities() throws Exception {
    People peopleProxy = publicApiClient.people();

    //Test with default tenant domain. see ALF-20448
    {/* ww  w  . j  av  a 2s .c om*/
        List<Activity> expectedActivities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {
            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> activities = repoService.getActivities(person5.getId(), null, false, true);
                return activities;
            }
        }, person5.getId(), defaultNetwork.getId());

        for (Activity activity : expectedActivities) {
            if (activity.getSiteId() == null) {
                fail("SiteId should present in user-joined activity.");
            }
        }

        {
            int skipCount = 0;
            int maxItems = expectedActivities.size();
            Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);

            Map<String, String> params = createParams(paging, null);
            params.put("who", String.valueOf(ActivityWho.me));
            publicApiClient.setRequestContext(new RequestContext(defaultNetwork.getId(), person5.getId()));
            ListResponse<Activity> response = peopleProxy.getActivities(person5.getId(), params);
            checkList(expectedActivities.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                    paging.getExpectedPaging(), response);
        }

    }

    // Test Case cloud-2204
    // Test case cloud-1500
    // Test Case cloud-2216
    // paging

    // Test Case cloud-1500
    {
        List<Activity> expectedActivities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {
            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> activities = repoService.getActivities(person1.getId(), null, false, true);
                return activities;
            }
        }, person1.getId(), network1.getId());

        {
            int skipCount = 0;
            int maxItems = 2;
            Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);

            Map<String, String> params = createParams(paging, null);
            params.put("who", String.valueOf(ActivityWho.me));
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            ListResponse<Activity> response = peopleProxy.getActivities(person1.getId(), params);
            checkList(expectedActivities.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                    paging.getExpectedPaging(), response);
        }

        {
            int skipCount = 2;
            int maxItems = expectedActivities.size() - 2;
            assertTrue(maxItems > 0);
            Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);

            Map<String, String> params = createParams(paging, null);
            params.put("who", String.valueOf(ActivityWho.me));
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            ListResponse<Activity> response = peopleProxy.getActivities(person1.getId(), params);
            checkList(expectedActivities.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                    paging.getExpectedPaging(), response);
        }

        // "-me-" user
        {
            int skipCount = 0;
            int maxItems = 2;
            Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);

            Map<String, String> params = createParams(paging, null);
            params.put("who", String.valueOf(ActivityWho.me));
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            ListResponse<Activity> response = peopleProxy
                    .getActivities(org.alfresco.rest.api.People.DEFAULT_USER, params);
            checkList(expectedActivities.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                    paging.getExpectedPaging(), response);
        }
    }

    // unknown user - 404
    try {
        List<Activity> expectedActivities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {
            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> activities = repoService.getActivities(person1.getId(), null, false, true);
                return activities;
            }
        }, person1.getId(), network1.getId());

        int skipCount = 0;
        int maxItems = 2;
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);
        peopleProxy.getActivities(GUID.generate(), createParams(paging, null));
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }

    // unknown site - 404
    try {
        List<Activity> expectedActivities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {
            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> activities = repoService.getActivities(person1.getId(), null, false, true);
                return activities;
            }
        }, person1.getId(), network1.getId());

        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);
        Map<String, String> params = createParams(paging, null);
        params.put("siteId", GUID.generate());
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        peopleProxy.getActivities(GUID.generate(), params);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }

    // user from another network - 404
    try {
        List<Activity> expectedActivities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {
            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> activities = repoService.getActivities(person1.getId(), null, false, true);
                return activities;
            }
        }, person1.getId(), network1.getId());

        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person3.getId()));
        peopleProxy.getActivities(person1.getId(), createParams(paging, null));
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
    }

    // another user from the same network - 403
    try {
        List<Activity> expectedActivities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {
            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> activities = repoService.getActivities(person1.getId(), null, false, true);
                return activities;
            }
        }, person1.getId(), network1.getId());

        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2.getId()));
        peopleProxy.getActivities(person1.getId(), createParams(paging, null));
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_FORBIDDEN, e.getHttpResponse().getStatusCode());
    }

    try {
        List<Activity> activities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {
            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> activities = repoService.getActivities(person1.getId(), null, false, true);
                return activities;
            }
        }, person1.getId(), network1.getId());
        assertTrue(activities.size() > 0);
        Activity activity = activities.get(0);

        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        peopleProxy.remove("people", person1.getId(), "activities", String.valueOf(activity.getId()),
                "Unable to DELETE a person activity");
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }

    // Test Case cloud-1500
    // other user activities
    {
        List<Activity> expectedActivities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {
            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> expectedActivities = repoService.getActivities(person1.getId(), null, true,
                        false);
                return expectedActivities;
            }
        }, person1.getId(), network1.getId());

        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);

        Map<String, String> params = createParams(paging, null);
        params.put("who", String.valueOf(ActivityWho.others));
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        ListResponse<Activity> response = peopleProxy.getActivities(person1.getId(), params);
        checkList(expectedActivities.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                paging.getExpectedPaging(), response);
    }

    // all activities with siteId exclusion
    {
        List<Activity> expectedActivities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {
            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> expectedActivities = repoService.getActivities(person1.getId(),
                        testSite.getSiteId(), false, false);
                return expectedActivities;
            }
        }, person1.getId(), network1.getId());

        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems, expectedActivities.size(), null);

        Map<String, String> params = createParams(paging, null);
        params.put("siteId", testSite.getSiteId());
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        ListResponse<Activity> response = peopleProxy.getActivities(person1.getId(), params);
        checkList(expectedActivities.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                paging.getExpectedPaging(), response);
    }

    // all activities with siteId exclusion, unknown site id
    try {
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems);

        Map<String, String> params = createParams(paging, null);
        params.put("siteId", GUID.generate());
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        peopleProxy.getActivities(person1.getId(), params);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }

    // unknown person id
    try {
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems);

        Map<String, String> params = createParams(paging, null);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        peopleProxy.getActivities(GUID.generate(), params);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }

    // invalid who parameter
    try {
        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems);

        Map<String, String> params = createParams(paging, null);
        params.put("who", GUID.generate());
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
        peopleProxy.getActivities(person1.getId(), params);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
    }

    // Test Case cloud-1970
    // Not allowed methods
    //      try
    //      {
    //         publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
    //         peopleProxy.create("people", person1.getId(), "activities", null, null, "Unable to POST to person activities");
    //         fail("");
    //      }
    //      catch(PublicApiException e)
    //      {
    //         assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    //      }
    //      
    //      try
    //      {
    //         publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
    //         peopleProxy.update("people", person1.getId(), "activities", null, null, "Unable to PUT person activities");
    //         fail("");
    //      }
    //      catch(PublicApiException e)
    //      {
    //         assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    //      }
    //
    //      try
    //      {
    //         publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
    //         peopleProxy.remove("people", person1.getId(), "activities", null, "Unable to DELETE person activities");
    //         fail("");
    //      }
    //      catch(PublicApiException e)
    //      {
    //         assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    //      }
    //
    //      try
    //      {
    //         List<Activity> activities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>()
    //         {
    //            @Override
    //            public List<Activity> doWork() throws Exception
    //            {
    //               List<Activity> activities = repoService.getActivities(person1.getId(), null, false, true);
    //               return activities;
    //            }
    //         }, person1.getId(), network1.getId());
    //         assertTrue(activities.size() > 0);
    //         Activity activity = activities.get(0);
    //
    //         publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
    //         peopleProxy.create("people", person1.getId(), "activities", String.valueOf(activity.getId()), null, "Unable to POST to a person activity");
    //         fail("");
    //      }
    //      catch(PublicApiException e)
    //      {
    //         assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    //      }
    //
    //      try
    //      {
    //         List<Activity> activities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>()
    //         {
    //            @Override
    //            public List<Activity> doWork() throws Exception
    //            {
    //               List<Activity> activities = repoService.getActivities(person1.getId(), null, false, true);
    //               return activities;
    //            }
    //         }, person1.getId(), network1.getId());
    //         assertTrue(activities.size() > 0);
    //         Activity activity = activities.get(0);
    //
    //         publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
    //         peopleProxy.update("people", person1.getId(), "activities", String.valueOf(activity.getId()), null, "Unable to PUT a person activity");
    //         fail("");
    //      }
    //      catch(PublicApiException e)
    //      {
    //         assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    //      }

    // Test Case cloud-1970
    // not allowed methods
    {
        List<Activity> activities = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<Activity>>() {
            @Override
            public List<Activity> doWork() throws Exception {
                List<Activity> activities = repoService.getActivities(person1.getId(), null, false, false);
                return activities;
            }
        }, person1.getId(), network1.getId());
        assertTrue(activities.size() > 0);
        Activity activity = activities.get(0);

        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            peopleProxy.create("people", person1.getId(), "activities", null, null,
                    "Unable to POST to activities");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }

        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            peopleProxy.create("people", person1.getId(), "activities", String.valueOf(activity.getId()), null,
                    "Unable to POST to an activity");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }

        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            peopleProxy.update("people", person1.getId(), "activities", null, null, "Unable to PUT activities");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }

        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            peopleProxy.update("people", person1.getId(), "activities", String.valueOf(activity.getId()), null,
                    "Unable to PUT an activity");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }

        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            peopleProxy.remove("people", person1.getId(), "activities", null, "Unable to DELETE activities");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }

        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1.getId()));
            peopleProxy.remove("people", person1.getId(), "activities", String.valueOf(activity.getId()),
                    "Unable to DELETE an activity");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
    }
}

From source file:org.alfresco.rest.api.tests.TestFavourites.java

@Test
public void testInvalidRequests() throws Exception {
    try {// w w w . jav a2 s.  c  o  m
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

        Favourite favourite = makeSiteFavourite(person1PublicSites.get(0));
        Favourite ret = favouritesProxy.createFavourite(person11Id, favourite);
        favourite.expected(ret);
        fail();
    } catch (PublicApiException e) {
        // Note: un-authorized comes back as 404
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }

    // cloud-2468
    // invalid type
    // NOTE: The test below has swapped to attempt to favorite a comment rather than a
    //       a wiki page as the WikiService has moved to the Share Services AMP in 5.1

    try {
        log("cloud-2468");

        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

        final NodeRef document = personDocs.get(0);
        final NodeRef comment = TenantUtil.runAsUserTenant(new TenantRunAsWork<NodeRef>() {
            @Override
            public NodeRef doWork() throws Exception {
                NodeRef comment = repoService.createComment(document, new Comment("Title", "Content"));
                return comment;
            }
        }, person10Id, network1.getId());

        final String guid = comment.getId();
        JSONAble commentJSON = new JSONAble() {
            @SuppressWarnings("unchecked")
            @Override
            public JSONObject toJSON() {
                JSONObject json = new JSONObject();
                json.put("guid", guid);
                return json;
            }
        };

        FavouritesTarget target = new InvalidFavouriteTarget("comment", commentJSON, guid);
        Favourite favourite = new Favourite(target);

        favouritesProxy.createFavourite(person10Id, favourite);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
    }

    try {
        log("cloud-2468");

        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

        Site site = person1PublicSites.get(0);
        FavouritesTarget target = new InvalidFavouriteTarget(GUID.generate(), site, site.getGuid());
        Favourite favourite = new Favourite(target);

        favouritesProxy.createFavourite(person10Id, favourite);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
    }

    // type = file, target is a site
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

        String siteGuid = person1PublicSites.get(0).getGuid();
        FavouriteDocument document = new FavouriteDocument(siteGuid);
        Favourite favourite = makeFileFavourite(document.getGuid());
        Favourite ret = favouritesProxy.createFavourite(person10Id, favourite);
        favourite.expected(ret);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }

    // type = folder, target is a site
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

        String siteGuid = person1PublicSites.get(0).getGuid();
        FavouriteFolder folder = new FavouriteFolder(siteGuid);
        Favourite favourite = makeFolderFavourite(folder.getGuid());
        Favourite ret = favouritesProxy.createFavourite(person10Id, favourite);
        favourite.expected(ret);

        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }

    // type = folder, target is a file
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

        FavouriteFolder folder = new FavouriteFolder(person1PublicDocs.get(0).getId());
        Favourite favourite = makeFolderFavourite(folder.getGuid());
        Favourite ret = favouritesProxy.createFavourite(person10Id, favourite);
        favourite.expected(ret);

        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }

    // type = file, target is a folder
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

        FavouriteDocument document = new FavouriteDocument(person1PublicFolders.get(0).getId());
        Favourite favourite = makeFileFavourite(document.getGuid());
        Favourite ret = favouritesProxy.createFavourite(person10Id, favourite);
        favourite.expected(ret);

        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }

    // make sure that a user can't favourite on behalf of another user
    // 2471
    {
        log("cloud-2471");

        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            FavouriteDocument document = new FavouriteDocument(person1PublicDocs.get(0).getId());
            Favourite favourite = makeFileFavourite(document.getGuid());
            favouritesProxy.createFavourite(person11Id, favourite);

            fail();
        } catch (PublicApiException e) {
            // Note: un-authorized comes back as 404
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }

        // person1 should have no favourites
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11Id));
        ListResponse<Favourite> response = favouritesProxy.getFavourites(person11Id, createParams(null, null));
        assertEquals(0, response.getList().size());
    }

    // invalid/non-existent user
    // 2469
    try {
        log("cloud-2469");

        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

        Favourite favourite = makeSiteFavourite(personSites.get(0));
        Favourite ret = favouritesProxy.createFavourite(GUID.generate(), favourite);
        favourite.expected(ret);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }

    // make sure that a user can't see other user's favourites.
    // 2465
    try {
        log("cloud-2465");

        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
        favouritesProxy.getFavourites(person11Id, null);
        fail();
    } catch (PublicApiException e) {
        // Note: un-authorized comes back as 404
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }

    // 2464, unknown user
    try {
        log("cloud-2464");

        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
        favouritesProxy.getFavourites(GUID.generate(), null);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }

    // non-existent entity for a given type
    // 2480
    {
        log("cloud-2480");

        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            SiteImpl site = new SiteImpl();
            site.setGuid(GUID.generate());
            Favourite favourite = makeSiteFavourite((Site) site);
            favouritesProxy.createFavourite(person10Id, favourite);

            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }

        ListResponse<Favourite> response = favouritesProxy.getFavourites(person10Id, createParams(null, null));
        assertEquals(0, response.getList().size());
    }

    {
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            FavouriteDocument document = new FavouriteDocument(GUID.generate());
            Favourite favourite = makeFileFavourite(document.getGuid());
            favouritesProxy.createFavourite(person10Id, favourite);

            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }

        ListResponse<Favourite> response = favouritesProxy.getFavourites(person10Id, createParams(null, null));
        assertEquals(0, response.getList().size());
    }

    {
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            FavouriteFolder folder = new FavouriteFolder(GUID.generate());
            Favourite favourite = makeFolderFavourite(folder.getGuid());
            favouritesProxy.createFavourite(person10Id, favourite);

            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }

        ListResponse<Favourite> response = favouritesProxy.getFavourites(person10Id, createParams(null, null));
        assertEquals(0, response.getList().size());
    }

    // 2470
    // incorrect type for a given favourite target
    {
        log("cloud-2470");

        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            Site site = person1PublicSites.get(0);
            FavouritesTarget target = new InvalidFavouriteTarget("folder", site, site.getGuid());
            Favourite favourite = new Favourite(target);

            favouritesProxy.createFavourite(person10Id, favourite);

            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
        }

        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            Site site = person1PublicSites.get(0);
            FavouritesTarget target = new InvalidFavouriteTarget("file", site, site.getGuid());
            Favourite favourite = new Favourite(target);

            favouritesProxy.createFavourite(person10Id, favourite);

            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
        }

        FavouriteDocument document = new FavouriteDocument(person1PublicDocs.get(0).getId());

        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            FavouritesTarget target = new InvalidFavouriteTarget("site", document, document.getGuid());
            Favourite favourite = new Favourite(target);

            favouritesProxy.createFavourite(person10Id, favourite);

            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }

        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            FavouritesTarget target = new InvalidFavouriteTarget("folder", document, document.getGuid());
            Favourite favourite = new Favourite(target);

            favouritesProxy.createFavourite(person10Id, favourite);

            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }

        FavouriteFolder folder = new FavouriteFolder(person1PublicFolders.get(0).getId());

        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            FavouritesTarget target = new InvalidFavouriteTarget("site", folder, folder.getGuid());
            Favourite favourite = new Favourite(target);

            favouritesProxy.createFavourite(person10Id, favourite);

            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }

        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            FavouritesTarget target = new InvalidFavouriteTarget("file", folder, folder.getGuid());
            Favourite favourite = new Favourite(target);

            favouritesProxy.createFavourite(person10Id, favourite);

            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }

        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

        // none of these POSTs should have resulted in favourites being created...
        ListResponse<Favourite> response = favouritesProxy.getFavourites(person10Id, createParams(null, null));
        assertEquals(0, response.getList().size());
    }

    // invalid methods
    {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

        try {
            Favourite favourite = new Favourite(null);
            favouritesProxy.update("people", "-me-", "favorites", null, favourite.toJSON().toString(),
                    "Unable to PUT favourites");
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
    }
}

From source file:org.alfresco.rest.api.tests.TestFavourites.java

@Test
public void testValidRequests() throws Exception {
    List<Favourite> expectedFavourites = new ArrayList<Favourite>();

    {/*  w w  w .  j  a va 2  s .co  m*/
        // add some favourites
        // 2467

        log("cloud-2467");

        Favourite siteFavourite1 = makeSiteFavourite(person1PublicSites.get(0));

        FavouriteDocument document = repoService.getDocument(network1.getId(), person1PublicDocs.get(0));
        Favourite fileFavourite1 = makeFileFavourite(document.getGuid());

        FavouriteFolder folder = repoService.getFolder(network1.getId(), person1PublicFolders.get(0));
        Favourite folderFavourite1 = makeFolderFavourite(folder.getGuid());

        Favourite siteFavourite2 = makeSiteFavourite(person1PublicSites.get(1));

        document = repoService.getDocument(network1.getId(), person1PublicDocs.get(1));
        Favourite fileFavourite2 = makeFileFavourite(document.getGuid());

        folder = repoService.getFolder(network1.getId(), person1PublicFolders.get(1));
        Favourite folderFavourite2 = makeFolderFavourite(folder.getGuid());

        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            Favourite ret = favouritesProxy.createFavourite(person10Id, siteFavourite1);
            expectedFavourites.add(ret);
            siteFavourite1.expected(ret);
        }

        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            Favourite ret = favouritesProxy.createFavourite(person10Id, fileFavourite1);
            expectedFavourites.add(ret);
            fileFavourite1.expected(ret);
        }

        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            Favourite ret = favouritesProxy.createFavourite(person10Id, folderFavourite1);
            expectedFavourites.add(ret);
            folderFavourite1.expected(ret);
        }

        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            Favourite ret = favouritesProxy.createFavourite(person10Id, siteFavourite2);
            expectedFavourites.add(ret);
            siteFavourite2.expected(ret);
        }

        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            Favourite ret = favouritesProxy.createFavourite(person10Id, fileFavourite2);
            expectedFavourites.add(ret);
            fileFavourite2.expected(ret);
        }

        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            Favourite ret = favouritesProxy.createFavourite(person10Id, folderFavourite2);
            expectedFavourites.add(ret);
            folderFavourite2.expected(ret);
        }

        // already a favourite - 201
        {
            log("cloud-2472");

            {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

                Favourite ret = favouritesProxy.createFavourite(person10Id, siteFavourite1);
                siteFavourite1.expected(ret);
            }

            {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

                Favourite ret = favouritesProxy.createFavourite(person10Id, folderFavourite1);
                folderFavourite1.expected(ret);
            }

            {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

                Favourite ret = favouritesProxy.createFavourite(person10Id, fileFavourite1);
                fileFavourite1.expected(ret);
            }
        }

        {
            // cloud-2498
            // cloud-2499
            // create and list favourites across networks

            List<Favourite> person21ExpectedFavourites = new ArrayList<Favourite>();

            log("cloud-2498");
            log("cloud-2499");

            {
                // favourite a site in another network

                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person21Id));

                Favourite favourite = makeSiteFavourite(person1PrivateSites.get(0));
                try {
                    favouritesProxy.createFavourite("-me-", favourite);
                    fail();
                } catch (PublicApiException e) {
                    assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
                }

                int skipCount = 0;
                int maxItems = 10;
                Paging paging = getPaging(skipCount, maxItems, person21ExpectedFavourites.size(),
                        person21ExpectedFavourites.size());
                try {
                    favouritesProxy.getFavourites("-me-", createParams(paging, null));
                    fail();
                } catch (PublicApiException e) {
                    assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
                }
            }

            // favourite a document in another network
            {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person21Id));

                FavouriteDocument document1 = new FavouriteDocument(person1PrivateDocs.get(0).getId());
                Favourite favourite = makeFileFavourite(document1.getGuid());
                try {
                    favouritesProxy.createFavourite("-me-", favourite);
                    fail();
                } catch (PublicApiException e) {
                    assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
                }

                sort(person21ExpectedFavourites, FavouritesService.DEFAULT_SORT_PROPS);

                int skipCount = 0;
                int maxItems = 10;
                Paging paging = getPaging(skipCount, maxItems, person21ExpectedFavourites.size(),
                        person21ExpectedFavourites.size());
                try {
                    favouritesProxy.getFavourites("-me-", createParams(paging, null));
                    fail();
                } catch (PublicApiException e) {
                    assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
                }
            }

            // favourite a folder in another network
            {
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person21Id));

                FavouriteFolder folder1 = new FavouriteFolder(person1PrivateFolders.get(0).getId());
                Favourite favourite = makeFolderFavourite(folder1.getGuid());
                try {
                    favouritesProxy.createFavourite("-me-", favourite);
                    fail();
                } catch (PublicApiException e) {
                    assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
                }

                sort(person21ExpectedFavourites, FavouritesService.DEFAULT_SORT_PROPS);

                int skipCount = 0;
                int maxItems = 10;
                Paging paging = getPaging(skipCount, maxItems, person21ExpectedFavourites.size(),
                        person21ExpectedFavourites.size());
                try {
                    favouritesProxy.getFavourites("-me-", createParams(paging, null));
                    fail();
                } catch (PublicApiException e) {
                    assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
                }
            }
        }
    }

    // GET favourites
    // test paging and sorting
    {
        // cloud-2458
        // cloud-2462
        // cloud-2461
        {
            log("cloud-2458");
            log("cloud-2461");
            log("cloud-2462");

            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            List<Favourite> expected = new ArrayList<Favourite>(expectedFavourites);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);

            int skipCount = 0;
            int maxItems = 2;
            Paging paging = getPaging(skipCount, maxItems, expectedFavourites.size(),
                    expectedFavourites.size());
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id,
                    createParams(paging, null));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                    paging.getExpectedPaging(), resp);
        }

        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            List<Favourite> expected = new ArrayList<Favourite>(expectedFavourites);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);

            int skipCount = 2;
            int maxItems = 4;
            Paging paging = getPaging(skipCount, maxItems, expectedFavourites.size(),
                    expectedFavourites.size());
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id,
                    createParams(paging, null));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                    paging.getExpectedPaging(), resp);
        }

        // 2466
        // GET favourites for "-me-"
        {
            log("cloud-2466");

            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            List<Favourite> expected = new ArrayList<Favourite>(expectedFavourites);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);

            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expectedFavourites.size(),
                    expectedFavourites.size());
            ListResponse<Favourite> resp = favouritesProxy.getFavourites("-me-", createParams(paging, null));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                    paging.getExpectedPaging(), resp);
        }
    }

    // 2459
    {
        log("cloud-2459");

        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11Id));

        int skipCount = 0;
        int maxItems = Integer.MAX_VALUE;
        Paging paging = getPaging(skipCount, maxItems, 0, 0);
        ListResponse<Favourite> resp = favouritesProxy.getFavourites(person11Id, createParams(paging, null));
        List<Favourite> empty = Collections.emptyList();
        checkList(empty, paging.getExpectedPaging(), resp);
    }

    // cloud-2460: filtering by target type
    {
        log("cloud-2460");

        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            Set<Type> types = new HashSet<Type>(Arrays.asList(Type.FILE));
            List<Favourite> expected = filter(expectedFavourites, types);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);

            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expected.size(), expected.size());
            Map<String, String> params = Collections.singletonMap("where", "(EXISTS(target/file))");
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id,
                    createParams(paging, params));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                    paging.getExpectedPaging(), resp);
        }

        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            Set<Type> types = new HashSet<Type>(Arrays.asList(Type.FOLDER));
            List<Favourite> expected = filter(expectedFavourites, types);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);

            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expected.size(), expected.size());
            Map<String, String> params = Collections.singletonMap("where", "(EXISTS(target/folder))");
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id,
                    createParams(paging, params));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                    paging.getExpectedPaging(), resp);
        }

        // target/file
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            Set<Type> types = new HashSet<Type>(Arrays.asList(Type.FILE));
            List<Favourite> expected = filter(expectedFavourites, types);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);

            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expected.size(), expected.size());
            Map<String, String> params = Collections.singletonMap("where", "(EXISTS(target/file))");
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id,
                    createParams(paging, params));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                    paging.getExpectedPaging(), resp);
        }

        // target/folder
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            Set<Type> types = new HashSet<Type>(Arrays.asList(Type.FOLDER));
            List<Favourite> expected = filter(expectedFavourites, types);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);

            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expected.size(), expected.size());
            Map<String, String> params = Collections.singletonMap("where", "(EXISTS(target/folder))");
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id,
                    createParams(paging, params));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                    paging.getExpectedPaging(), resp);
        }

        // target/site
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            Set<Type> types = new HashSet<Type>(Arrays.asList(Type.SITE));
            List<Favourite> expected = filter(expectedFavourites, types);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);

            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expected.size(), expected.size());
            Map<String, String> params = Collections.singletonMap("where", "(EXISTS(target/site))");
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id,
                    createParams(paging, params));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                    paging.getExpectedPaging(), resp);
        }

        // target/folder OR target/file.
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            Set<Type> types = new HashSet<Type>(Arrays.asList(Type.FOLDER, Type.FILE));
            List<Favourite> expected = filter(expectedFavourites, types);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);

            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expected.size(), expected.size());
            Map<String, String> params = Collections.singletonMap("where",
                    "(EXISTS(target/file) OR EXISTS(target/folder))");
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id,
                    createParams(paging, params));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                    paging.getExpectedPaging(), resp);
        }

        // target/site OR target/file.
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            Set<Type> types = new HashSet<Type>(Arrays.asList(Type.SITE, Type.FILE));
            List<Favourite> expected = filter(expectedFavourites, types);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);

            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expected.size(), expected.size());
            Map<String, String> params = Collections.singletonMap("where",
                    "(EXISTS(target/file) OR EXISTS(target/site))");
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id,
                    createParams(paging, params));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                    paging.getExpectedPaging(), resp);
        }

        // target/site OR target/folder.
        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

            Set<Type> types = new HashSet<Type>(Arrays.asList(Type.SITE, Type.FOLDER));
            List<Favourite> expected = filter(expectedFavourites, types);
            sort(expected, FavouritesService.DEFAULT_SORT_PROPS);

            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expected.size(), expected.size());
            Map<String, String> params = Collections.singletonMap("where",
                    "(EXISTS(target/site) OR EXISTS(target/folder))");
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person10Id,
                    createParams(paging, params));
            checkList(expected.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                    paging.getExpectedPaging(), resp);
        }
    }

    // GET a favourite
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

        favouritesProxy.getFavourite(person10Id, GUID.generate());

        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }

    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

        Favourite favourite = expectedFavourites.get(0);

        favouritesProxy.getFavourite(GUID.generate(), favourite.getTargetGuid());

        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }

    {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

        Favourite favourite = expectedFavourites.get(0);

        Favourite resp = favouritesProxy.getFavourite(person10Id, favourite.getTargetGuid());
        favourite.expected(resp);
    }

    // cloud-2479, PUT case
    {
        log("cloud-2479.1");
        updateFavourite(network1.getId(), person10Id, person10Id, TARGET_TYPE.site);

        log("cloud-2479.2");
        updateFavourite(network1.getId(), person10Id, person10Id, TARGET_TYPE.file);

        log("cloud-2479.3");
        updateFavourite(network1.getId(), person10Id, person10Id, TARGET_TYPE.folder);
    }

    try {
        // cloud-2474
        // non-existent personId
        log("cloud-2474");

        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

        favouritesProxy.removeFavourite(GUID.generate(), GUID.generate());

        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }

    // cloud-2475
    // try delete a non-existent favourite for a node that exists
    {
        log("cloud-2475");

        NodeRef doc = TenantUtil.runAsUserTenant(new TenantRunAsWork<NodeRef>() {
            @Override
            public NodeRef doWork() throws Exception {
                NodeRef containerNodeRef = person1PublicSites.get(0).getContainerNodeRef("documentLibrary");
                NodeRef doc = repoService.createDocument(containerNodeRef, GUID.generate(), "");
                return doc;
            }
        }, person11Id, network1.getId());

        String favouriteId = doc.getId();

        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));

        ListResponse<Favourite> before = getFavourites(network1.getId(), person10Id, person10Id, 0,
                Integer.MAX_VALUE, null, null, null);
        List<Favourite> beforeList = before.getList();
        assertTrue(beforeList.size() > 0);

        try {
            favouritesProxy.removeFavourite(person10Id, favouriteId);
            fail("Should be a 404");
        } catch (PublicApiException e) {
            // expected
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }

        // check no favourites have been removed
        ListResponse<Favourite> after = getFavourites(network1.getId(), person10Id, person10Id, 0,
                Integer.MAX_VALUE, null, null, null);
        assertEquals(beforeList.size(), after.getList().size());
    }

    // cloud-2473, DELETE case
    {
        log("cloud-2473.1");
        deleteFavourite(network1.getId(), person10Id, person10Id, TARGET_TYPE.site);

        log("cloud-2473.2");
        deleteFavourite(network1.getId(), person10Id, person10Id, TARGET_TYPE.file);

        log("cloud-2473.3");
        Favourite favourite = deleteFavourite(network1.getId(), person10Id, person10Id, TARGET_TYPE.folder);

        // try to delete non-existent favourite
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person10Id));
            favouritesProxy.removeFavourite(person10Id, favourite.getTargetGuid());

            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }
    }

    // cloud-2476
    // try delete another user's favourite
    {
        log("cloud-2476");

        // make sure there are favourites to delete
        //         publicApiClient.setRequestContext(new RequestContext(network1.getId(), personId));
        //         SiteFavouriteTarget target = new SiteFavouriteTarget(person1Sites.get(0));
        //         Favourite favourite = new Favourite(target);
        //         favouritesProxy.createFavourite(personId, favourite);

        ListResponse<Favourite> before = getFavourites(network1.getId(), person10Id, person10Id, 0,
                Integer.MAX_VALUE, null, null, null);
        assertTrue(before.getList().size() > 0);
        Favourite favourite = before.getList().get(0);

        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11Id));
            favouritesProxy.removeFavourite(person10Id, favourite.getTargetGuid());

            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }

        ListResponse<Favourite> after = getFavourites(network1.getId(), person10Id, person10Id, 0,
                Integer.MAX_VALUE, null, null, null);
        assertEquals(before.getList().size(), after.getList().size());
    }
}

From source file:org.alfresco.rest.api.tests.TestFavourites.java

@Test
public void testPUBLICAPI141() throws Exception {
    final TestSite publicSite = person1PublicSites.get(0); // person1's public site
    final TestSite publicSite1 = person1PublicSites.get(1); // person1's public site
    final TestSite privateSite = person1PrivateSites.get(0); // person1's private site
    final NodeRef folderNodeRef = person1PublicFolders.get(0); // person1's folder
    final NodeRef nodeRef = person1PublicDocs.get(1); // a file in the folder
    final List<Favourite> expectedFavourites = new ArrayList<Favourite>();

    {// w  ww  . j  a  v a  2  s  .  co  m
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person12Id));

        // invite to another user's public site
        SiteMembershipRequest siteMembershipRequest = new SiteMembershipRequest();
        siteMembershipRequest.setId(publicSite.getSiteId());
        siteMembershipRequest.setMessage("Please can I join your site?");
        siteMembershipRequestsProxy.createSiteMembershipRequest(person12Id, siteMembershipRequest);

        // favourite other users site, folder and file
        Favourite folderFavourite = makeFolderFavourite(folderNodeRef.getId());
        favouritesProxy.createFavourite(person12Id, folderFavourite);

        Favourite fileFavourite = makeFileFavourite(nodeRef.getId());
        favouritesProxy.createFavourite(person12Id, fileFavourite);

        final Favourite siteFavourite = makeSiteFavourite(publicSite);
        favouritesProxy.createFavourite(person12Id, siteFavourite);
        expectedFavourites.add(siteFavourite);

        final Favourite siteFavourite1 = makeSiteFavourite(publicSite1);
        favouritesProxy.createFavourite(person12Id, siteFavourite1);
        expectedFavourites.add(siteFavourite1);

        sort(expectedFavourites, FavouritesService.DEFAULT_SORT_PROPS);

        // move the folder and file to person1's private site
        TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {
            @Override
            public Void doWork() throws Exception {
                NodeRef documentLibraryNodeRef = privateSite.getContainerNodeRef("documentLibrary");
                repoService.moveNode(folderNodeRef, documentLibraryNodeRef);

                return null;
            }
        }, person11Id, network1.getId());

        try {
            favouritesProxy.getFavourite(person12Id, folderFavourite.getTargetGuid());
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }

        try {
            favouritesProxy.getFavourite(person12Id, fileFavourite.getTargetGuid());
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }

        {
            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expectedFavourites.size(),
                    expectedFavourites.size());
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person12Id,
                    createParams(paging, null));
            checkList(sublist(expectedFavourites, skipCount, skipCount + paging.getExpectedPaging().getCount()),
                    paging.getExpectedPaging(), resp);
        }

        // make the public sites private
        TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {
            @Override
            public Void doWork() throws Exception {
                publicSite.setSiteVisibility(SiteVisibility.PRIVATE);
                publicSite1.setSiteVisibility(SiteVisibility.PRIVATE);

                return null;
            }
        }, person11Id, network1.getId());
        expectedFavourites.remove(siteFavourite1);

        // Given that person2Id is still a member of 'publicSite', they should still have access and therefore
        // it should show up in their favourites. But person2Id is not a member of 'publicSite1', they should 
        // not have access and therefore it should not show up in their favourites.
        {
            Favourite actual = favouritesProxy.getFavourite(person12Id, siteFavourite.getTargetGuid());
            siteFavourite.expected(actual);

            try {
                favouritesProxy.getFavourite(person12Id, siteFavourite1.getTargetGuid());
            } catch (PublicApiException e) {
                assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
            }

            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            Paging paging = getPaging(skipCount, maxItems, expectedFavourites.size(),
                    expectedFavourites.size());
            ListResponse<Favourite> resp = favouritesProxy.getFavourites(person12Id,
                    createParams(paging, null));
            checkList(sublist(expectedFavourites, skipCount, skipCount + paging.getExpectedPaging().getCount()),
                    paging.getExpectedPaging(), resp);
        }
    }
}