Example usage for org.json.simple JSONArray toJSONString

List of usage examples for org.json.simple JSONArray toJSONString

Introduction

In this page you can find the example usage for org.json.simple JSONArray toJSONString.

Prototype

public static String toJSONString(List list) 

Source Link

Usage

From source file:io.personium.common.es.impl.InternalEsClient.java

/**
 * ????.//from www .  ja va 2s  .c  o  m
 * ?????????TransportSerializationException????????
 * @param index ??
 * @param type ??
 * @param routingId routingId
 * @param queryList ??
 * @return ??
 */
public ActionFuture<MultiSearchResponse> asyncMultiSearch(String index, String type, String routingId,
        List<Map<String, Object>> queryList) {
    MultiSearchRequest mrequest = new MultiSearchRequest();
    if (queryList == null || queryList.size() == 0) {
        throw new EsMultiSearchQueryParseException();
    }
    for (Map<String, Object> query : queryList) {
        SearchRequest req = new SearchRequest(index).searchType(SearchType.DEFAULT);
        if (type != null) {
            req.types(type);
        }
        // ????????
        if (query != null) {
            req.source(query);
        }
        if (routingFlag) {
            req = req.routing(routingId);
        }
        mrequest.add(req);
    }

    ActionFuture<MultiSearchResponse> ret = esTransportClient.multiSearch(mrequest);
    this.fireEvent(Event.afterRequest, index, type, null, JSONArray.toJSONString(queryList), "MultiSearch");
    return ret;
}

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

@Test
public void testNodeRatings() throws Exception {
    Iterator<TestNetwork> networksIt = getTestFixture().getNetworksIt();
    assertTrue(networksIt.hasNext());/*from   w w w .  j  a v  a  2  s  .  c  o m*/
    final TestNetwork network1 = networksIt.next();
    assertTrue(networksIt.hasNext());
    final TestNetwork network2 = networksIt.next();

    final List<TestPerson> people = new ArrayList<TestPerson>(3);

    // create users
    TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>() {
        @Override
        public Void doWork() throws Exception {
            TestPerson person = network1.createUser();
            people.add(person);
            person = network1.createUser();
            people.add(person);
            return null;
        }
    }, network1.getId());

    TenantUtil.runAsSystemTenant(new TenantRunAsWork<Void>() {
        @Override
        public Void doWork() throws Exception {
            TestPerson person = network2.createUser();
            people.add(person);
            return null;
        }
    }, network2.getId());

    final TestPerson person11 = people.get(0);
    final TestPerson person12 = people.get(1);
    final TestPerson person21 = people.get(2);

    final List<NodeRef> nodes = new ArrayList<NodeRef>();
    final List<TestSite> sites = new ArrayList<TestSite>();

    // Create site
    TenantUtil.runAsUserTenant(new TenantRunAsWork<Void>() {
        @Override
        public Void doWork() throws Exception {
            TestSite site = network1.createSite(SiteVisibility.PRIVATE);
            sites.add(site);

            NodeRef nodeRef = repoService.createDocument(site.getContainerNodeRef("documentLibrary"),
                    "Test Doc 1", "Test Content");
            nodes.add(nodeRef);

            nodeRef = repoService.createDocument(site.getContainerNodeRef("documentLibrary"), "Test Doc 2",
                    "Test Content");
            nodes.add(nodeRef);

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

    final NodeRef nodeRef1 = nodes.get(0);

    Comments commentsProxy = publicApiClient.comments();
    People peopleProxy = publicApiClient.people();
    Nodes nodesProxy = publicApiClient.nodes();
    DateFormat format = PublicApiDateFormat.getDateFormat();

    // Test Case cloud-1976
    // Create node ratings
    // try to add a rating to a comment
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));

        Comment comment = new Comment("Test Comment", "Test Comment");
        Comment newComment = commentsProxy.createNodeComment(nodeRef1.getId(), comment);
        NodeRating rating = new NodeRating("likes", true);
        nodesProxy.createNodeRating(newComment.getId(), rating);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }

    // invalid node id
    try {
        NodeRating rating = new NodeRating("likes", true);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        nodesProxy.createNodeRating(GUID.generate(), rating);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
    }

    // try to add a rating to a tag
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));

        Tag tag = new Tag("testTag");
        Tag newTag = nodesProxy.createNodeTag(nodeRef1.getId(), tag);
        NodeRating rating = new NodeRating("likes", true);
        nodesProxy.createNodeRating(newTag.getId(), rating);
        fail();
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
    }

    // invalid rating scheme
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        nodesProxy.createNodeRating(nodeRef1.getId(),
                new NodeRating("missingRatingScheme", Double.valueOf(1.0f)));
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
    }

    // invalid rating
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        nodesProxy.createNodeRating(nodeRef1.getId(), new NodeRating("likes", Double.valueOf(2.0f)));
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
    }

    // invalid rating
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        nodesProxy.createNodeRating(nodeRef1.getId(), new NodeRating("fiveStar", true));
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
    }

    // invalid rating - can't rate own content for fiveStar
    try {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        nodesProxy.createNodeRating(nodeRef1.getId(), new NodeRating("fiveStar", 5));
        fail("");
    } catch (PublicApiException e) {
        assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
    }

    // valid ratings
    {
        NodeRating rating = new NodeRating("likes", true);

        Date time = new Date();

        // rate by multiple users in more than 1 network
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        NodeRating ret = nodesProxy.createNodeRating(nodeRef1.getId(), rating);
        assertEquals(rating.getMyRating(), ret.getMyRating());
        assertTrue(format.parse(ret.getRatedAt()).after(time));
        assertEquals(rating.getId(), ret.getId());
        assertEquals(new NodeRating.Aggregate(1, null), ret.getAggregate());

        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person12.getId()));
        ret = nodesProxy.createNodeRating(nodeRef1.getId(), rating);
        assertEquals(rating.getMyRating(), ret.getMyRating());
        assertTrue(format.parse(ret.getRatedAt()).after(time));
        assertEquals(rating.getId(), ret.getId());
        assertEquals(new NodeRating.Aggregate(2, null), ret.getAggregate());

        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person12.getId()));
        ret = nodesProxy.createNodeRating(nodeRef1.getId(), rating);
        assertEquals(rating.getMyRating(), ret.getMyRating());
        assertTrue(format.parse(ret.getRatedAt()).after(time));
        assertEquals(rating.getId(), ret.getId());
        assertEquals(new NodeRating.Aggregate(2, null), ret.getAggregate());

        // different network - unauthorized
        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person21.getId()));
            nodesProxy.createNodeRating(nodeRef1.getId(), rating);
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
        }

        // Test Case cloud-2209
        // Test Case cloud-2220
        // Test Case cloud-1520
        // check that the node ratings are there, test paging

        {
            // person11

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

            List<NodeRating> expectedRatings = repoService.getNodeRatings(person11.getId(), network1.getId(),
                    nodeRef1);

            {
                int skipCount = 0;
                int maxItems = 1;
                Paging paging = getPaging(skipCount, maxItems, expectedRatings.size(), expectedRatings.size());
                ListResponse<NodeRating> resp = nodesProxy.getNodeRatings(nodeRef1.getId(),
                        createParams(paging, null));
                checkList(expectedRatings.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                        paging.getExpectedPaging(), resp);
            }

            {
                int skipCount = 1;
                int maxItems = Integer.MAX_VALUE;
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
                Paging paging = getPaging(skipCount, maxItems, expectedRatings.size(), expectedRatings.size());
                ListResponse<NodeRating> resp = nodesProxy.getNodeRatings(nodeRef1.getId(),
                        createParams(paging, null));
                checkList(expectedRatings.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                        paging.getExpectedPaging(), resp);
            }

            {
                int skipCount = 1;
                int maxItems = expectedRatings.size();
                publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
                Paging paging = getPaging(skipCount, maxItems, expectedRatings.size(), expectedRatings.size());
                ListResponse<NodeRating> resp = nodesProxy.getNodeRatings(nodeRef1.getId(),
                        createParams(paging, null));
                checkList(expectedRatings.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                        paging.getExpectedPaging(), resp);
            }
        }

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

            // person12
            List<NodeRating> expectedRatings = repoService.getNodeRatings(person12.getId(), network1.getId(),
                    nodeRef1);

            {
                int skipCount = 0;
                int maxItems = 1;
                Paging paging = getPaging(skipCount, maxItems, expectedRatings.size(), expectedRatings.size());
                ListResponse<NodeRating> resp = nodesProxy.getNodeRatings(nodeRef1.getId(),
                        createParams(paging, null));
                checkList(expectedRatings.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                        paging.getExpectedPaging(), resp);
            }

            {
                int skipCount = 1;
                int maxItems = Integer.MAX_VALUE;
                Paging paging = getPaging(skipCount, maxItems, expectedRatings.size(), expectedRatings.size());
                ListResponse<NodeRating> resp = nodesProxy.getNodeRatings(nodeRef1.getId(),
                        createParams(paging, null));
                checkList(expectedRatings.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                        paging.getExpectedPaging(), resp);
            }
        }

        {
            // person21

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

            List<NodeRating> expectedRatings = Collections.emptyList();

            try {
                int skipCount = 0;
                int maxItems = 1;

                Paging paging = getPaging(skipCount, maxItems, expectedRatings.size(), expectedRatings.size());
                nodesProxy.getNodeRatings(nodeRef1.getId(), createParams(paging, null));
                fail();
            } catch (PublicApiException e) {
                assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
            }
        }

        // invalid node id
        try {
            int skipCount = 1;
            int maxItems = Integer.MAX_VALUE;
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            Paging paging = getPaging(skipCount, maxItems);
            nodesProxy.getNodeRatings(GUID.generate(), createParams(paging, null));
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_NOT_FOUND, e.getHttpResponse().getStatusCode());
        }

        // check activities have been raised for the created ratings
        repoService.generateFeed();

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

            Paging paging = getPaging(0, Integer.MAX_VALUE);
            ListResponse<Activity> activities = peopleProxy.getActivities(person11.getId(),
                    createParams(paging, null));

            boolean found = false;
            for (Activity activity : activities.getList()) {
                String activityType = activity.getActivityType();
                if (activityType.equals(ActivityType.FILE_LIKED)) {
                    Map<String, Object> summary = activity.getSummary();
                    assertNotNull(summary);
                    String objectId = (String) summary.get("objectId");
                    assertNotNull(objectId);
                    if (nodeRef1.getId().equals(objectId)) {
                        found = true;
                        break;
                    }
                }
            }

            assertTrue(found);
        }
    }

    {
        // remove node rating

        NodeRating rating = new NodeRating("likes", null);

        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person21.getId()));
            nodesProxy.removeNodeRating(nodeRef1.getId(), rating);
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_UNAUTHORIZED, e.getHttpResponse().getStatusCode());
        }

        {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person12.getId()));
            nodesProxy.removeNodeRating(nodeRef1.getId(), rating);
        }

        // check list
        {
            List<NodeRating> ratings = repoService.getNodeRatings(person11.getId(), network1.getId(), nodeRef1);

            int skipCount = 0;
            int maxItems = Integer.MAX_VALUE;
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            Paging paging = getPaging(skipCount, maxItems, ratings.size(), ratings.size());
            ListResponse<NodeRating> resp = nodesProxy.getNodeRatings(nodeRef1.getId(),
                    createParams(paging, null));
            checkList(ratings.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                    paging.getExpectedPaging(), resp);
        }
    }

    // get a node rating
    // 1977
    {
        NodeRating rating = new NodeRating("likes", true);
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));

        NodeRating expected = nodesProxy.createNodeRating(nodeRef1.getId(), rating);
        NodeRating actual = nodesProxy.getNodeRating(nodeRef1.getId(), "likes");
        expected.expected(actual);
    }

    {
        // update node rating
        NodeRating rating = new NodeRating("fiveStar", 2);

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

        // create initial rating
        NodeRating createdRating = nodesProxy.createNodeRating(nodeRef1.getId(), rating);
        NodeRating updateRating = new NodeRating(createdRating.getId(), 5);

        // update - not supported
        try {
            nodesProxy.updateNodeRating(nodeRef1.getId(), updateRating);
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
    }

    // Test Case cloud-1977
    // invalid methods
    {
        try {
            // -ve test - cannot create multiple ratings in single POST call (unsupported)
            List<NodeRating> ratings = new ArrayList<>(2);
            ratings.add(new NodeRating("likes", true));
            ratings.add(new NodeRating("likes", false));

            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            nodesProxy.create("nodes", nodeRef1.getId(), "ratings", null, JSONArray.toJSONString(ratings),
                    "Unable to POST to multiple ratings");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }

        // get an arbitrary rating
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
        ListResponse<NodeRating> resp = nodesProxy.getNodeRatings(nodeRef1.getId(),
                createParams(getPaging(0, Integer.MAX_VALUE), null));
        List<NodeRating> nodeRatings = resp.getList();
        assertTrue(nodeRatings.size() > 0);

        try {
            NodeRating rating = new NodeRating("likes", true);

            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            nodesProxy.create("nodes", nodeRef1.getId(), "ratings", "likes", rating.toJSON().toString(),
                    "Unable to POST to a node rating");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }

        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            nodesProxy.update("nodes", nodeRef1.getId(), "ratings", null, null, "Unable to PUT node ratings");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }

        try {
            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            nodesProxy.remove("nodes", nodeRef1.getId(), "ratings", null, "Unable to DELETE node ratings");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }

        try {
            NodeRating rating = nodeRatings.get(0);

            publicApiClient.setRequestContext(new RequestContext(network1.getId(), person11.getId()));
            nodesProxy.update("nodes", nodeRef1.getId(), "ratings", rating.getId(), null,
                    "Unable to PUT a node rating");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
    }
}

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

@Test
public void testSites() throws Exception {
    Sites sitesProxy = publicApiClient.sites();

    // create & get sites (as person 2)
    {//from   w  w w.  j ava  2 s  .c  o m
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person2Id));

        String siteTitle = "site 1 " + System.currentTimeMillis();
        Site site = new SiteImpl(siteTitle, SiteVisibility.PRIVATE.toString());
        site1 = sitesProxy.createSite(site);

        Site ret = sitesProxy.getSite(site1.getSiteId());
        site1.expected(ret);

        siteTitle = "site 2 " + System.currentTimeMillis();
        site = new SiteImpl(siteTitle, SiteVisibility.PUBLIC.toString());
        site2 = sitesProxy.createSite(site);

        ret = sitesProxy.getSite(site2.getSiteId());
        site2.expected(ret);

        siteTitle = "site 3 " + System.currentTimeMillis();
        site = new SiteImpl(siteTitle, SiteVisibility.MODERATED.toString());
        site3 = sitesProxy.createSite(site);

        ret = sitesProxy.getSite(site3.getSiteId());
        site3.expected(ret);
    }

    List<TestSite> expectedSites = TenantUtil.runAsUserTenant(new TenantRunAsWork<List<TestSite>>() {
        @Override
        public List<TestSite> doWork() throws Exception {
            List<TestSite> sites = network1.getSites(person1Id);
            return sites;
        }
    }, person1Id, network1.getId());

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

        int skipCount = 0;
        int maxItems = 2;
        Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), expectedSites.size());
        ListResponse<Site> resp = sitesProxy.getSites(createParams(paging, null));
        checkList(expectedSites.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                paging.getExpectedPaging(), resp);
    }

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

        int skipCount = 2;
        int maxItems = Integer.MAX_VALUE;
        Paging paging = getPaging(skipCount, maxItems, expectedSites.size(), expectedSites.size());
        ListResponse<Site> resp = sitesProxy.getSites(createParams(paging, null));
        checkList(expectedSites.subList(skipCount, skipCount + paging.getExpectedPaging().getCount()),
                paging.getExpectedPaging(), resp);
    }

    // test create and delete site
    {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));

        String siteTitle = "my site !*#$ 123";
        String siteDescription = "my site description";

        SiteImpl site = new SiteImpl(siteTitle, SiteVisibility.PRIVATE.toString());
        site.setDescription(siteDescription);

        Site ret = sitesProxy.createSite(site);
        String siteId = ret.getSiteId();

        String expectedSiteId = "my-site-123";
        Site siteExp = new SiteImpl(null, expectedSiteId, ret.getGuid(), siteTitle, siteDescription,
                SiteVisibility.PRIVATE.toString(), null, SiteRole.SiteManager);
        siteExp.expected(ret);

        ret = sitesProxy.getSite(siteId);
        siteExp.expected(ret);

        sitesProxy.removeSite(siteId);

        // -ve test - ie. cannot get site after it has been deleted
        sitesProxy.getSite(siteId, 404);
    }

    // test create + permanent delete + create
    {

        String siteId = "bbb";
        String siteTitle = "BBB site";

        Site site = new SiteImpl(null, siteId, null, siteTitle, null, SiteVisibility.PUBLIC.toString(), null,
                null);

        sitesProxy.createSite(site);

        // permanent site delete (bypass trashcan/archive)
        sitesProxy.removeSite(siteId, true, 204);

        sitesProxy.createSite(site);
    }

    // test create using site id = "true" (RA-1101)
    {

        String siteId = "true";
        String siteTitle = "string";
        String siteDescription = "string";

        Site site = new SiteImpl(null, siteId, null, siteTitle, siteDescription,
                SiteVisibility.PUBLIC.toString(), null, null);

        sitesProxy.createSite(site);
    }

    // -ve tests
    {
        // invalid auth
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), GUID.generate(), "password"));
        sitesProxy.getSite(site1.getSiteId(), 401);

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

        // -ve - cannot view or delete a private site
        sitesProxy.getSite(site1.getSiteId(), 404);
        sitesProxy.removeSite(site1.getSiteId(), false, 404);

        // -ve - test cannot delete a public site (but can view it)
        sitesProxy.getSite(site2.getSiteId(), 200);
        sitesProxy.removeSite(site2.getSiteId(), false, 403);

        // -ve - try to get unknown site
        sitesProxy.getSite(GUID.generate(), 404);

        SiteImpl site = new SiteImpl("my site 123", "invalidsitevisibility");
        sitesProxy.createSite(site, 400);

        site = new SiteImpl(null, "invalid site id", null, "my site 123", null,
                SiteVisibility.PRIVATE.toString(), null, null);
        sitesProxy.createSite(site, 400);

        site = new SiteImpl(null, "invalidsiteid*", null, "my site 123", null,
                SiteVisibility.PRIVATE.toString(), null, null);
        sitesProxy.createSite(site, 400);

        site = new SiteImpl();
        site.setSiteId(new String(new char[72]).replace('\0', 'a'));
        site.setTitle(new String(new char[256]).replace('\0', 'a'));
        site.setDescription(new String(new char[512]).replace('\0', 'a'));
        site.setVisibility(SiteVisibility.PUBLIC.toString());
        sitesProxy.createSite(site, 201);

        // -ve - site id too long
        site = new SiteImpl();
        site.setSiteId(new String(new char[73]).replace('\0', 'a'));
        site.setTitle("ok");
        site.setDescription("ok");
        site.setVisibility(SiteVisibility.PUBLIC.toString());
        sitesProxy.createSite(site, 400);

        // -ve - site title too long
        site = new SiteImpl();
        site.setSiteId("ok");
        site.setTitle(new String(new char[257]).replace('\0', 'a'));
        site.setDescription("ok");
        site.setVisibility(SiteVisibility.PUBLIC.toString());
        sitesProxy.createSite(site, 400);

        // -ve - site description too long
        site = new SiteImpl();
        site.setSiteId("ok");
        site.setTitle("ok");
        site.setDescription(new String(new char[513]).replace('\0', 'a'));
        site.setVisibility(SiteVisibility.PUBLIC.toString());
        sitesProxy.createSite(site, 400);

        // site already exists (409)
        String siteTitle = "my site 456";
        site = new SiteImpl(siteTitle, SiteVisibility.PRIVATE.toString());
        String siteId = sitesProxy.createSite(site, 201).getSiteId();
        sitesProxy.createSite(site, 409);
        sitesProxy.removeSite(siteId); // cleanup

        sitesProxy.removeSite(GUID.generate(), false, 404);
    }

    // -ve - cannot create site with same site id as an existing site (even if it is in the trashcan/archive)
    {
        String siteId = "aaa";
        String siteTitle = "AAA site";

        Site site = new SiteImpl(null, siteId, null, siteTitle, null, SiteVisibility.PUBLIC.toString(), null,
                null);

        String siteNodeId = sitesProxy.createSite(site).getGuid();

        // -ve - duplicate site id
        sitesProxy.createSite(site, 409);

        sitesProxy.removeSite(siteId);

        // -ve - duplicate site id (even if site is in trashcan)
        sitesProxy.createSite(site, 409);

        // now purge the site
        sitesProxy.remove("deleted-nodes", siteNodeId, null, null, "Cannot purge site");

        sitesProxy.createSite(site);
    }

    // -ve - minor: error code if updating via nodes api (REPO-512)
    {
        String siteId = "zzz";
        String siteTitle = "ZZZ site";

        Site site = new SiteImpl(null, siteId, null, siteTitle, null, SiteVisibility.PRIVATE.toString(), null,
                null);
        String siteNodeId = sitesProxy.createSite(site).getGuid();

        // try to update to invalid site visibility
        JSONObject prop = new JSONObject();
        prop.put("st:siteVisibility", "INVALID");
        JSONObject properties = new JSONObject();
        properties.put("properties", new JSONObject(prop));
        try {
            sitesProxy.update("nodes", siteNodeId, null, null, properties.toJSONString(), null);
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_BAD_REQUEST, e.getHttpResponse().getStatusCode());
        }

        sitesProxy.removeSite(siteId); // cleanup
    }

    // -ve test - cannot create multiple sites in single POST call (unsupported)
    {
        List<Site> sites = new ArrayList<>(2);
        sites.add(new SiteImpl(null, "siteA1", null, "siteA1", null, SiteVisibility.PRIVATE.toString(), null,
                null));
        sites.add(new SiteImpl(null, "siteB1", null, "siteB1", null, SiteVisibility.PRIVATE.toString(), null,
                null));

        sitesProxy.create("sites", null, null, null, JSONArray.toJSONString(sites), null, 405);
    }

    // -ve tests - belts-and-braces for unsupported methods
    {
        publicApiClient.setRequestContext(new RequestContext(network1.getId(), person1Id));

        // -ve - cannot call POST method on /sites/siteId
        try {
            sitesProxy.create("sites", "site", null, null, null, "Unable to POST to a site");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }

        // -ve - cannot call DELETE method on /sites
        try {
            sitesProxy.remove("sites", null, null, null, "Unable to DELETE sites");
            fail();
        } catch (PublicApiException e) {
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, e.getHttpResponse().getStatusCode());
        }
    }

    // Test Case cloud-1478
    // Test Case cloud-1479
    // user invited to network and user invited to site
    // user invited to network and user not invited to site
}

From source file:org.apache.eagle.jpm.analyzer.meta.impl.orm.JobMetaEntityToRelation.java

@Override
public void accept(PreparedStatement statement, JobMetaEntity entity) throws SQLException {
    int parameterIndex = 1;
    if (entity.getUuid() != null && StringUtils.isNotBlank(entity.getUuid())) {
        statement.setString(parameterIndex, entity.getUuid());
        parameterIndex++;// w w  w.  ja  va  2  s . c om
    }
    if (entity.getConfiguration() != null) {
        statement.setString(parameterIndex, JSONObject.toJSONString(entity.getConfiguration()));
        parameterIndex++;
    }
    if (entity.getEvaluators() != null) {
        statement.setString(parameterIndex, JSONArray.toJSONString(entity.getEvaluators()));
        parameterIndex++;
    }
    if (entity.getCreatedTime() > 0) {
        statement.setLong(parameterIndex, entity.getCreatedTime());
        parameterIndex++;
    }
    if (entity.getModifiedTime() > 0) {
        statement.setLong(parameterIndex, entity.getModifiedTime());
        parameterIndex++;
    }
    if (StringUtils.isNotBlank(entity.getSiteId())) {
        statement.setString(parameterIndex, entity.getSiteId());
        parameterIndex++;
    }
    if (StringUtils.isNotBlank(entity.getJobDefId())) {
        statement.setString(parameterIndex, entity.getJobDefId());
        parameterIndex++;
    }
}

From source file:org.apache.hadoop.fs.http.server.FSOperations.java

/**
 * Converts xAttr names to a JSON object.
 *
 * @param names file xAttr names./*  w  w w  .ja v a2 s .  c  o  m*/
 *
 * @return The JSON representation of the xAttr names.
 * @throws IOException 
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private static Map xAttrNamesToJSON(List<String> names) throws IOException {
    Map jsonMap = new LinkedHashMap();
    jsonMap.put(HttpFSFileSystem.XATTRNAMES_JSON, JSONArray.toJSONString(names));
    return jsonMap;
}

From source file:org.clustermc.lib.utils.UUIDFetcher.java

public Map<String, UUID> call() throws Exception {
    Map<String, UUID> uuidMap = new HashMap<>();
    int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
    for (int i = 0; i < requests; i++) {
        HttpURLConnection connection = createConnection();
        String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size())));
        writeBody(connection, body);/*from   w w  w .  j  a  v  a 2s. c om*/
        JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
        for (Object profile : array) {
            JSONObject jsonProfile = (JSONObject) profile;
            String id = (String) jsonProfile.get("id");
            String name = (String) jsonProfile.get("name");
            UUID uuid = UUIDFetcher.getUUID(id);
            uuidMap.put(name.toLowerCase(), uuid);
        }
        if (rateLimiting && i != requests - 1) {
            Thread.sleep(100L);
        }
    }
    return uuidMap;
}

From source file:org.marinemc.util.mojang.UUIDHandler.java

private UUID getMojangUUID(final String name) throws Throwable {
    final HttpURLConnection connection = (HttpURLConnection) new URL(
            "https://api.mojang.com/profiles/minecraft").openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setUseCaches(false);/*from w  ww.j  a  va 2 s .c  om*/
    connection.setDoInput(true);
    connection.setDoOutput(true);
    final String request = JSONArray.toJSONString(Arrays.asList(name));
    {
        final OutputStream stream = connection.getOutputStream();
        stream.write(request.getBytes());
        stream.flush();
        stream.close();
    }
    final JSONArray array = (JSONArray) parser.parse(new InputStreamReader(connection.getInputStream()));
    UUID uuid = null;
    for (final Object o : array)
        uuid = UUID.fromString(StringUtils.fixUUID(((JSONObject) o).get("id").toString()));
    connection.disconnect();
    if (uuid == null)
        throw new NullPointerException("Couldn't fetch the uuid");
    return uuid;
}