Example usage for com.fasterxml.jackson.databind.node ArrayNode ArrayNode

List of usage examples for com.fasterxml.jackson.databind.node ArrayNode ArrayNode

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.node ArrayNode ArrayNode.

Prototype

public ArrayNode(JsonNodeFactory paramJsonNodeFactory) 

Source Link

Usage

From source file:com.redhat.lightblue.metadata.parser.JSONMetadataParserTest.java

License:asdf

@Test
public void addStringToArray() {
    String value = "bar";
    ArrayNode array = new ArrayNode(factory);

    Assert.assertEquals(0, array.size());

    parser.addStringToArray(array, value);

    Assert.assertEquals(1, array.size());
    Assert.assertEquals(value, array.get(0).textValue());
}

From source file:com.redhat.lightblue.metadata.parser.JSONMetadataParserTest.java

License:asdf

@Test
public void addObjectToArray() {
    JsonNode value = new TextNode("asdf");
    ArrayNode array = new ArrayNode(factory);

    Assert.assertEquals(0, array.size());

    parser.addObjectToArray(array, value);

    Assert.assertEquals(1, array.size());
    Assert.assertEquals(value, array.get(0));
}

From source file:org.apache.usergrid.client.EntityTestCase.java

@Test
public void testEntityAppendInArray() {
    String collectionName = "testEntityProperties" + System.currentTimeMillis();
    String entityName = "testEntity1";

    UsergridEntity entity = new UsergridEntity(collectionName, entityName);
    entity.save();//from w ww . j  a v a  2  s  .  c  o  m

    ArrayList<Object> lenArr = new ArrayList<>();
    lenArr.add(1);
    lenArr.add(2);
    lenArr.add(3);
    lenArr.add(4);
    entity.insert("lenArray", lenArr);
    entity.save();

    lenArr = new ArrayList<>();
    lenArr.add(6);
    lenArr.add(7);
    entity.append("lenArray", lenArr);
    entity.save();

    UsergridEntity eLookUp = Usergrid.GET(collectionName, entityName).first();
    assertNotNull("The entity returned is not null.", eLookUp);

    ArrayNode toCompare = new ArrayNode(JsonNodeFactory.instance);
    toCompare.add(1).add(2).add(3).add(4).add(6).add(7);
    assertEquals("The two arrays should be equal.", eLookUp.getJsonNodeProperty("lenArray"), toCompare);
}

From source file:org.apache.usergrid.client.EntityTestCase.java

@Test
public void testEntityPrependInArray() {
    String collectionName = "testEntityProperties" + System.currentTimeMillis();
    String entityName = "testEntity1";

    UsergridEntity entity = new UsergridEntity(collectionName, entityName);
    entity.save();//from   w  w w.  j  ava  2 s. c  o m

    ArrayList<Object> lenArr = new ArrayList<>();
    lenArr.add(1);
    lenArr.add(2);
    lenArr.add(3);
    lenArr.add(4);
    entity.putProperty("lenArray", lenArr);
    entity.save();

    lenArr = new ArrayList<>();
    lenArr.add(6);
    lenArr.add(7);

    entity.insert("lenArray", lenArr, 0);
    entity.save();
    UsergridEntity eLookUp = Usergrid.GET(collectionName, entityName).first();
    assertNotNull("The entity returned is not null.", eLookUp);

    ArrayNode toCompare = new ArrayNode(JsonNodeFactory.instance);
    toCompare.add(6).add(7).add(1).add(2).add(3).add(4);
    assertEquals("The two arrays should be equal.", eLookUp.getJsonNodeProperty("lenArray"), toCompare);
}

From source file:org.apache.usergrid.client.EntityTestCase.java

@Test
public void testEntityPopInArray() {
    String collectionName = "testEntityProperties" + System.currentTimeMillis();
    String entityName = "testEntity1";

    UsergridEntity entity = new UsergridEntity(collectionName, entityName);
    entity.save();//from  ww  w  .j  a  v a2 s.c o m

    ArrayList<Object> lenArr = new ArrayList<>();
    lenArr.add(1);
    lenArr.add(2);
    lenArr.add(3);
    entity.putProperty("lenArray", lenArr);
    entity.save();

    // should remove the last value of an existing array
    entity.pop("lenArray");
    entity.save();

    UsergridEntity eLookUp = Usergrid.GET(collectionName, entityName).first();
    assertNotNull("The entity returned is not null.", eLookUp);

    ArrayNode toCompare = new ArrayNode(JsonNodeFactory.instance);
    toCompare.add(1).add(2);
    assertEquals("The two arrays should be equal.", eLookUp.getJsonNodeProperty("lenArray"), toCompare);

    // value should remain unchanged if it is not an array
    entity.putProperty("foo", "test1");
    entity.save();

    entity.pop("foo");
    entity.save();

    eLookUp = Usergrid.GET(collectionName, entityName).first();
    assertNotNull("The entity returned is not null.", eLookUp);
    assertEquals("foo should equal test1.", eLookUp.getStringProperty("foo"), "test1");

    //should gracefully handle empty arrays
    ArrayList<Object> lenArr2 = new ArrayList<>();
    entity.putProperty("foo", lenArr2);
    entity.save();
    entity.pop("foo");

    eLookUp = Usergrid.GET(collectionName, entityName).first();
    assertNotNull("The entity returned is not null.", eLookUp);

    toCompare = new ArrayNode(JsonNodeFactory.instance);
    assertEquals("The two arrays should be equal.", eLookUp.getJsonNodeProperty("foo"), toCompare);
}

From source file:org.apache.usergrid.client.EntityTestCase.java

@Test
public void testEntityShiftInArray() {
    String collectionName = "testEntityProperties" + System.currentTimeMillis();
    String entityName = "testEntity1";

    //should remove the last value of an existing array
    UsergridEntity entity = new UsergridEntity(collectionName, entityName);
    entity.save();//from  w  w  w  .  ja va2  s . co  m

    ArrayList<Object> lenArr = new ArrayList<>();
    lenArr.add(1);
    lenArr.add(2);
    lenArr.add(3);
    entity.putProperty("lenArray", lenArr);
    entity.save();

    entity.shift("lenArray");
    entity.save();

    UsergridEntity eLookUp = Usergrid.GET(collectionName, entityName).first();
    assertNotNull("The entity returned is not null.", eLookUp);

    ArrayNode toCompare = new ArrayNode(JsonNodeFactory.instance);
    toCompare.add(2).add(3);
    assertEquals("The two arrays should be equal.", eLookUp.getJsonNodeProperty("lenArray"), toCompare);

    //value should remain unchanged if it is not an array
    entity.putProperty("foo", "test1");
    entity.shift("foo");
    entity.save();

    eLookUp = Usergrid.GET(collectionName, entityName).first();
    assertNotNull("The entity returned is not null.", eLookUp);
    assertEquals("The entity returned is not null.", eLookUp.getStringProperty("foo"), "test1");

    //should gracefully handle empty arrays
    ArrayList<Object> lenArr2 = new ArrayList<>();
    entity.putProperty("foo", lenArr2);
    entity.shift("foo");
    entity.save();

    eLookUp = Usergrid.GET(collectionName, entityName).first();
    assertNotNull("The entity returned is not null.", eLookUp);
    assertEquals("The two arrays should be equal.", eLookUp.getJsonNodeProperty("foo"),
            new ArrayNode(JsonNodeFactory.instance));
}

From source file:org.apache.usergrid.client.EntityTestCase.java

@Test
public void testEntityInsertInArray() {
    String collectionName = "testEntityProperties" + System.currentTimeMillis();
    String entityName = "testEntity1";

    //should set properties for a given object, overwriting properties that exist and creating those that don\'t
    UsergridEntity entity = new UsergridEntity(collectionName, entityName);
    entity.save();/*w  w w  .jav a  2s .c om*/

    ArrayList<Object> lenArr = new ArrayList<>();
    lenArr.add(1);
    lenArr.add(2);
    lenArr.add(3);
    lenArr.add(4);
    entity.putProperty("lenArray", lenArr);
    entity.save();

    ArrayList<Object> lenArr2 = new ArrayList<>();
    lenArr2.add(6);
    lenArr2.add(7);

    entity.insert("lenArray", lenArr2, 6);
    entity.save();

    UsergridEntity eLookUp = Usergrid.GET(collectionName, entityName).first();
    assertNotNull("The entity returned is not null.", eLookUp);

    ArrayNode toCompare = new ArrayNode(JsonNodeFactory.instance);
    toCompare.add(1).add(2).add(3).add(4).add(6).add(7);
    assertEquals("The two arrays should be equal.", eLookUp.getJsonNodeProperty("lenArray"), toCompare);

    //should merge an array of values into an existing array at the specified index
    lenArr = new ArrayList<>();
    lenArr.add(1);
    lenArr.add(2);
    lenArr.add(3);
    lenArr.add(4);

    entity.putProperty("lenArray", lenArr);
    entity.save();

    lenArr2 = new ArrayList<>();
    lenArr2.add(5);
    lenArr2.add(6);
    lenArr2.add(7);
    lenArr2.add(8);

    entity.insert("lenArray", lenArr2, 2);
    entity.save();

    eLookUp = Usergrid.GET(collectionName, entityName).first();
    assertNotNull("The entity returned is not null.", eLookUp);

    toCompare = new ArrayNode(JsonNodeFactory.instance);
    toCompare.add(1).add(2).add(5).add(6).add(7).add(8).add(3).add(4);
    assertEquals("The two arrays should be equal.", eLookUp.getJsonNodeProperty("lenArray"), toCompare);

    //should convert an existing value into an array when inserting a second value
    entity.putProperty("foo", "test");
    entity.insert("foo", "test1", 1);
    entity.save();

    eLookUp = Usergrid.GET(collectionName, entityName).first();
    assertNotNull("The entity returned is not null.", eLookUp);

    toCompare = new ArrayNode(JsonNodeFactory.instance);
    toCompare.add("test").add("test1");
    assertEquals("The two arrays should be equal.", eLookUp.getJsonNodeProperty("foo"), toCompare);

    //should create a new array when a property does not exist
    entity.insert("foo1", "test2", 1);
    entity.save();

    eLookUp = Usergrid.GET(collectionName, entityName).first();
    assertNotNull("The entity returned is not null.", eLookUp);

    toCompare = new ArrayNode(JsonNodeFactory.instance);
    toCompare.add("test2");
    assertEquals("The two arrays should be equal.", eLookUp.getJsonNodeProperty("foo1"), toCompare);

    //should gracefully handle index out of positive range
    entity.putProperty("ArrayIndex", "test1");
    entity.insert("ArrayIndex", "test2", 1000);
    entity.save();

    eLookUp = Usergrid.GET(collectionName, entityName).first();
    assertNotNull("The entity returned is not null.", eLookUp);

    toCompare = new ArrayNode(JsonNodeFactory.instance);
    toCompare.add("test1").add("test2");
    assertEquals("The two arrays should be equal.", eLookUp.getJsonNodeProperty("ArrayIndex"), toCompare);

    //should gracefully handle index out of negative range
    entity.insert("ArrayIndex", "test3", -1000);
    entity.save();

    eLookUp = Usergrid.GET(collectionName, entityName).first();
    assertNotNull("The entity returned is not null.", eLookUp);

    toCompare = new ArrayNode(JsonNodeFactory.instance);
    toCompare.add("test3").add("test1").add("test2");
    assertEquals("The two arrays should be equal.", eLookUp.getJsonNodeProperty("ArrayIndex"), toCompare);
}

From source file:org.jetbrains.webdemo.database.MySqlConnector.java

public ArrayNode getProjectHeaders(UserInfo userInfo, String projectType) throws DatabaseOperationException {
    PreparedStatement st = null;//from   ww w .  ja  va2 s  .  co  m
    ResultSet rs = null;
    try (Connection connection = dataSource.getConnection()) {
        st = connection.prepareStatement("SELECT projects.public_id, projects.name FROM projects JOIN "
                + "users ON projects.owner_id = users.id WHERE "
                + "(users.client_id = ? AND users.provider = ? AND projects.type = ?)");
        st.setString(1, userInfo.getId());
        st.setString(2, userInfo.getType());
        st.setString(3, projectType);

        rs = st.executeQuery();

        ArrayNode projects = new ArrayNode(JsonNodeFactory.instance);
        while (rs.next()) {
            ObjectNode object = new ObjectNode(JsonNodeFactory.instance);
            object.put("name", unEscape(rs.getString("name")));
            object.put("publicId", rs.getString("public_id"));
            object.put("modified", false);
            projects.add(object);
        }

        return projects;
    } catch (Throwable e) {
        ErrorWriter.ERROR_WRITER.writeExceptionToExceptionAnalyzer(e,
                SessionInfo.TypeOfRequest.WORK_WITH_DATABASE.name(), "unknown",
                userInfo.getId() + " " + userInfo.getType() + " " + userInfo.getName());
        throw new DatabaseOperationException("Unknown error while loading list of your programs");
    } finally {
        closeStatementAndResultSet(st, rs);
    }
}

From source file:com.joyent.manta.client.multipart.ServerSideMultipartManager.java

/**
 * Creates the JSON request body used to commit all of the parts of a multipart
 * upload request.//from   w  w  w . j  a v  a 2 s  .c o m
 *
 * @param parts stream of tuples - this is a terminal operation that will close the stream
 * @return byte array containing JSON data
 */
static ImmutablePair<byte[], Integer> createCommitRequestBody(
        final Stream<? extends MantaMultipartUploadTuple> parts) {
    final JsonNodeFactory nodeFactory = MantaObjectMapper.NODE_FACTORY_INSTANCE;
    final ObjectNode objectNode = new ObjectNode(nodeFactory);

    final ArrayNode partsArrayNode = new ArrayNode(nodeFactory);
    objectNode.set("parts", partsArrayNode);

    try (Stream<? extends MantaMultipartUploadTuple> sorted = parts.sorted()) {
        sorted.forEach(tuple -> partsArrayNode.add(tuple.getEtag()));
    }

    Validate.isTrue(partsArrayNode.size() > 0, "Can't commit multipart upload with no parts");

    try {
        return ImmutablePair.of(MantaObjectMapper.INSTANCE.writeValueAsBytes(objectNode),
                partsArrayNode.size());
    } catch (IOException e) {
        String msg = "Error serializing JSON for commit MPU request body";
        throw new MantaMultipartException(msg, e);
    }
}

From source file:com.redhat.lightblue.metadata.mongo.MongoMetadata.java

@Override
public Response getAccess(String entityName, String version) {
    List<String> entityNames = new ArrayList<>();
    // accessMap: <role, <operation, List<path>>>
    Map<String, Map<String, List<String>>> accessMap = new HashMap<>();

    if (null != entityName && !entityName.isEmpty()) {
        entityNames.add(entityName);// www. j a v  a 2  s  . c o  m
    } else {
        // force version to be null
        version = null;
        entityNames.addAll(Arrays.asList(getEntityNames()));
    }

    // initialize response, assume will be completely successful
    Response response = new Response(JsonNodeFactory.instance);
    response.setStatus(OperationStatus.COMPLETE);

    // for each name get metadata
    for (String name : entityNames) {
        EntityMetadata metadata;
        try {
            metadata = getEntityMetadata(name, version);
        } catch (Exception e) {
            response.setStatus(OperationStatus.PARTIAL);
            // construct error data
            ObjectNode obj = new ObjectNode(JsonNodeFactory.instance);
            obj.put(LITERAL_NAME, name);
            if (null != version) {
                obj.put("version", version);
            }
            List<Error> errors = new ArrayList<>();
            errors.add(Error.get("ERR_NO_METADATA",
                    "Could not get metadata for given input. Error message: " + e.getMessage()));
            DataError error = new DataError(obj, errors);
            response.getDataErrors().add(error);
            // skip to next entity name
            continue;
        }

        EntityAccess ea = metadata.getAccess();
        Map<FieldAccess, Path> fa = new HashMap<>();
        FieldCursor fc = metadata.getFieldCursor();
        // collect field access
        while (fc.next()) {
            FieldTreeNode ftn = fc.getCurrentNode();
            if (ftn instanceof Field) {
                // add access if there is anything to extract later
                Field f = (Field) ftn;
                if (!f.getAccess().getFind().isEmpty() || !f.getAccess().getInsert().isEmpty()
                        || !f.getAccess().getUpdate().isEmpty()) {
                    fa.put(f.getAccess(), f.getFullPath());
                }
            }
        }

        // key is role, value is all associated paths.
        // accessMap: <role, <operation, List<path>>>
        // collect entity access
        helperAddRoles(ea.getDelete().getRoles(), "delete", name, accessMap);
        helperAddRoles(ea.getFind().getRoles(), "find", name, accessMap);
        helperAddRoles(ea.getInsert().getRoles(), "insert", name, accessMap);
        helperAddRoles(ea.getUpdate().getRoles(), "update", name, accessMap);

        // collect field access
        for (Map.Entry<FieldAccess, Path> entry : fa.entrySet()) {
            FieldAccess access = entry.getKey();
            String pathString = name + "." + entry.getValue().toString();
            helperAddRoles(access.getFind().getRoles(), "find", pathString, accessMap);
            helperAddRoles(access.getInsert().getRoles(), "insert", pathString, accessMap);
            helperAddRoles(access.getUpdate().getRoles(), "update", pathString, accessMap);
        }
    }

    // finally, populate response with valid output
    if (!accessMap.isEmpty()) {
        ArrayNode root = new ArrayNode(JsonNodeFactory.instance);
        response.setEntityData(root);
        for (Map.Entry<String, Map<String, List<String>>> entry : accessMap.entrySet()) {
            String role = entry.getKey();
            Map<String, List<String>> opPathMap = entry.getValue();

            ObjectNode roleJson = new ObjectNode(JsonNodeFactory.instance);
            root.add(roleJson);

            roleJson.put("role", role);

            for (Map.Entry<String, List<String>> operationMap : opPathMap.entrySet()) {
                String operation = operationMap.getKey();
                List<String> paths = opPathMap.get(operation);
                ArrayNode pathNode = new ArrayNode(JsonNodeFactory.instance);
                for (String path : paths) {
                    pathNode.add(path);
                }
                roleJson.put(operation, pathNode);
            }
        }
    } else {
        // nothing successful! set status to error
        response.setStatus(OperationStatus.ERROR);
    }

    return response;
}