Java Code Examples for com.fasterxml.jackson.databind.ObjectMapper#getNodeFactory()

The following examples show how to use com.fasterxml.jackson.databind.ObjectMapper#getNodeFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestLoadSchema.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testLoadingJson() {
    ObjectMapper objectMapper =  new ObjectMapper();
    ObjectNode json = new ObjectNode(objectMapper.getNodeFactory());
    json.put("username", "john");
    Vertex v1 = this.sqlgGraph.addVertex(T.label, "Person", "doc", json);
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.close();
    try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
        Vertex vv = sqlgGraph1.traversal().V(v1.id()).next();
        Assert.assertTrue(vv.property("doc").isPresent());
        Map<String, PropertyType> propertyTypeMap = sqlgGraph1.getTopology().getAllTables().get(SchemaTable.of(
                sqlgGraph1.getSqlDialect().getPublicSchema(), "V_Person").toString());
        Assert.assertTrue(propertyTypeMap.containsKey("doc"));
        sqlgGraph1.tx().rollback();
    }
}
 
Example 2
Source File: TestBatchStreamVertex.java    From sqlg with MIT License 6 votes vote down vote up
@Test(expected = SqlgExceptions.InvalidPropertyTypeException.class)
public void testStreamJsonAsArray() {
    ObjectMapper objectMapper =  new ObjectMapper();
    ObjectNode json1 = new ObjectNode(objectMapper.getNodeFactory());
    json1.put("username", "john1");
    ObjectNode json2 = new ObjectNode(objectMapper.getNodeFactory());
    json2.put("username", "john2");

    JsonNode[] jsonNodes = new JsonNode[]{json1};
    this.sqlgGraph.tx().streamingBatchModeOn();
    for (int i = 0; i < 10; i++) {
        this.sqlgGraph.streamVertex(T.label, "Person", "docs", jsonNodes);
    }
    this.sqlgGraph.tx().commit();
    List<Vertex> vertices = this.sqlgGraph.traversal().V().hasLabel("Person").toList();
    Assert.assertEquals(10, vertices.size());
    JsonNode[] value = vertices.get(0).value("docs");
    Assert.assertArrayEquals(jsonNodes, value);
}
 
Example 3
Source File: TestJson.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testJsonArraysForArrayNode() {
    Assume.assumeTrue(this.sqlgGraph.getSqlDialect().supportsJsonArrayValues());
    ObjectMapper objectMapper =  new ObjectMapper();
    ArrayNode jsonArray1 = new ArrayNode(objectMapper.getNodeFactory());
    ObjectNode john = new ObjectNode(objectMapper.getNodeFactory());
    john.put("username", "john");
    ObjectNode pete = new ObjectNode(objectMapper.getNodeFactory());
    pete.put("username", "pete");
    jsonArray1.add(john);
    jsonArray1.add(pete);

    ArrayNode jsonArray2 = new ArrayNode(objectMapper.getNodeFactory());
    ObjectNode john2 = new ObjectNode(objectMapper.getNodeFactory());
    john2.put("username", "john2");
    ObjectNode pete2 = new ObjectNode(objectMapper.getNodeFactory());
    pete2.put("username", "pete2");
    jsonArray2.add(john2);
    jsonArray2.add(pete2);

    ArrayNode[] arrayNodes = new ArrayNode[]{jsonArray1, jsonArray2};
    Vertex v1 = this.sqlgGraph.addVertex(T.label, "Person", "docs", arrayNodes);
    this.sqlgGraph.tx().commit();
    JsonNode[] value = this.sqlgGraph.traversal().V(v1.id()).next().value("docs");
    Assert.assertArrayEquals(arrayNodes, value);
}
 
Example 4
Source File: TestBatchStreamVertex.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testStreamJson() throws InterruptedException {
    ObjectMapper objectMapper =  new ObjectMapper();
    ObjectNode json = new ObjectNode(objectMapper.getNodeFactory());
    json.put("username", "john");
    this.sqlgGraph.tx().streamingBatchModeOn();
    for (int i = 0; i < 10; i++) {
        this.sqlgGraph.streamVertex(T.label, "Person", "doc", json);
    }
    this.sqlgGraph.tx().commit();
    testStreamJson_assert(this.sqlgGraph, json);
    if (this.sqlgGraph1 != null) {
        Thread.sleep(SLEEP_TIME);
        testStreamJson_assert(this.sqlgGraph1, json);
    }
}
 
Example 5
Source File: TestBatchJson.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void batchUpdateJsonWithNulls() throws InterruptedException {
    ObjectMapper objectMapper =  new ObjectMapper();
    ObjectNode json = new ObjectNode(objectMapper.getNodeFactory());
    json.put("username", "john");
    this.sqlgGraph.tx().normalBatchModeOn();
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "Person", "doc1", json);
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "Person", "doc2", json);
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "Person", "doc3", json);
    this.sqlgGraph.tx().commit();

    ObjectNode jsonAgain = new ObjectNode(objectMapper.getNodeFactory());
    jsonAgain.put("surname", "zzz");
    this.sqlgGraph.tx().normalBatchModeOn();
    a1.property("doc1", jsonAgain);
    a2.property("doc2", jsonAgain);
    a3.property("doc3", jsonAgain);
    this.sqlgGraph.tx().commit();

    batchUpdateJsonWithNulls_assert(this.sqlgGraph, a1, a2, a3, jsonAgain);
    if (this.sqlgGraph1 != null) {
        Thread.sleep(SLEEP_TIME);
        batchUpdateJsonWithNulls_assert(this.sqlgGraph1, a1, a2, a3, jsonAgain);
    }
}
 
Example 6
Source File: TestBatchJson.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void batchJson() throws InterruptedException {
    ObjectMapper objectMapper =  new ObjectMapper();
    ObjectNode json = new ObjectNode(objectMapper.getNodeFactory());
    json.put("username", "john");
    this.sqlgGraph.tx().normalBatchModeOn();
    for (int i = 0; i < 10; i++) {
        this.sqlgGraph.addVertex(T.label, "Person", "doc", json);
    }
    this.sqlgGraph.tx().commit();
    batchJson_assert(this.sqlgGraph, json);
    if (this.sqlgGraph1 != null) {
        Thread.sleep(SLEEP_TIME);
        batchJson_assert(this.sqlgGraph, json);
    }
}
 
Example 7
Source File: TestBatchJson.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testJson() throws InterruptedException {
    ObjectMapper objectMapper =  new ObjectMapper();
    ObjectNode json = new ObjectNode(objectMapper.getNodeFactory());
    json.put("username", "john");
    this.sqlgGraph.tx().batchMode(BatchManager.BatchModeType.NORMAL);
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "doc", json);
    this.sqlgGraph.tx().commit();
    testJson_assert(this.sqlgGraph, json, a1);
    if (this.sqlgGraph1 != null) {
        Thread.sleep(SLEEP_TIME);
        testJson_assert(this.sqlgGraph1, json, a1);
    }
}
 
Example 8
Source File: AggregateQueryBuilder.java    From immutables with Apache License 2.0 5 votes vote down vote up
AggregateQueryBuilder(Query query, ObjectMapper mapper, Mapping mapping, PathNaming pathNaming, Predicate<Path> idPredicate) {
  this.query = Objects.requireNonNull(query, "query");
  Preconditions.checkArgument(query.hasAggregations(), "no aggregations for query %s", query);
  this.mapping = mapping;
  this.pathNaming = pathNaming;
  List<Expression> toName = new ArrayList<>();
  toName.addAll(query.projections());
  toName.addAll(query.collations().stream().map(Collation::expression).collect(Collectors.toList()));
  toName.addAll(query.groupBy());
  naming = UniqueCachedNaming.of(toName);
  this.mapper = mapper;
  this.nodeFactory = mapper.getNodeFactory();
  this.idPredicate = idPredicate;
}
 
Example 9
Source File: RemoveModulesYMLAction.java    From walkmod-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void doAction(JsonNode node) throws Exception {
	if (node.has("modules")) {
		JsonNode aux = node.get("modules");
		ObjectMapper mapper = provider.getObjectMapper();
		if (aux.isArray()) {
			ArrayNode modulesList = (ArrayNode) node.get("modules");
			Iterator<JsonNode> it = modulesList.iterator();
			ArrayNode newModulesList = new ArrayNode(mapper.getNodeFactory());
			while (it.hasNext()) {
				JsonNode next = it.next();
				if (next.isTextual()) {
					String text = next.asText();
					if (!modules.contains(text)) {
						newModulesList.add(text);
					}
				}
			}
			ObjectNode oNode = (ObjectNode) node;
			if (newModulesList.size() > 0) {
				oNode.set("modules", newModulesList);
			} else {
				oNode.remove("modules");
			}
			provider.write(node);
		}
	}
}
 
Example 10
Source File: RemoveModulesYMLAction.java    From walkmod-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void doAction(JsonNode node) throws Exception {
	if (node.has("modules")) {
		JsonNode aux = node.get("modules");
		ObjectMapper mapper = provider.getObjectMapper();
		if (aux.isArray()) {
			ArrayNode modulesList = (ArrayNode) node.get("modules");
			Iterator<JsonNode> it = modulesList.iterator();
			ArrayNode newModulesList = new ArrayNode(mapper.getNodeFactory());
			while (it.hasNext()) {
				JsonNode next = it.next();
				if (next.isTextual()) {
					String text = next.asText();
					if (!modules.contains(text)) {
						newModulesList.add(text);
					}
				}
			}
			ObjectNode oNode = (ObjectNode) node;
			if (newModulesList.size() > 0) {
				oNode.set("modules", newModulesList);
			} else {
				oNode.remove("modules");
			}
			provider.write(node);
		}
	}
}
 
Example 11
Source File: TestBatchStreamEdge.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testStreamJson() throws InterruptedException {
    this.sqlgGraph.tx().streamingBatchModeOn();
    LinkedHashMap<String, Object> keyValues = new LinkedHashMap<>();
    keyValues.put("name", "halo");
    keyValues.put("surname", "halo");
    for (int i = 0; i < 10; i++) {
        keyValues.put("age", i);
        this.sqlgGraph.streamVertex("Man", keyValues);
    }
    this.sqlgGraph.tx().flush();
    for (int i = 0; i < 10; i++) {
        keyValues.put("age", i);
        this.sqlgGraph.streamVertex("Female", keyValues);
    }
    this.sqlgGraph.tx().flush();
    int count = 0;
    List<Vertex> men = this.sqlgGraph.traversal().V().hasLabel("Man").toList();
    List<Vertex> females = this.sqlgGraph.traversal().V().hasLabel("Female").toList();
    LinkedHashMap<String, Object> edgeKeyValues = new LinkedHashMap<>();

    ObjectMapper objectMapper =  new ObjectMapper();
    ObjectNode json = new ObjectNode(objectMapper.getNodeFactory());
    json.put("username", "john");

    edgeKeyValues.put("doc", json);
    for (Vertex man : men) {
        SqlgVertex female = (SqlgVertex) females.get(count++);
        ((SqlgVertex)man).streamEdge("married", female, edgeKeyValues);
    }
    this.sqlgGraph.tx().commit();
    testStreamJson_assert(this.sqlgGraph, json);
    if (this.sqlgGraph1 != null) {
        Thread.sleep(SLEEP_TIME);
        testStreamJson_assert(this.sqlgGraph1, json);
    }
}
 
Example 12
Source File: TestJson.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testJsonArraysForObjectNodes() {
    Assume.assumeTrue(this.sqlgGraph.getSqlDialect().supportsJsonArrayValues());
    ObjectMapper objectMapper =  new ObjectMapper();
    ObjectNode json1 = new ObjectNode(objectMapper.getNodeFactory());
    json1.put("username", "john1");
    ObjectNode json2 = new ObjectNode(objectMapper.getNodeFactory());
    json2.put("username", "john2");
    ObjectNode[] objectNodes = new ObjectNode[]{json1, json2};

    Vertex v1 = this.sqlgGraph.addVertex(T.label, "Person", "docs", objectNodes);
    this.sqlgGraph.tx().commit();
    JsonNode[] value = this.sqlgGraph.traversal().V(v1.id()).next().value("docs");
    Assert.assertArrayEquals(objectNodes, value);
}
 
Example 13
Source File: TestJson.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testJsonArray() {
    ObjectMapper objectMapper =  new ObjectMapper();
    ArrayNode jsonArray = new ArrayNode(objectMapper.getNodeFactory());
    ObjectNode john = new ObjectNode(objectMapper.getNodeFactory());
    john.put("username", "john");
    ObjectNode pete = new ObjectNode(objectMapper.getNodeFactory());
    pete.put("username", "pete");
    jsonArray.add(john);
    jsonArray.add(pete);
    Vertex v1 = this.sqlgGraph.addVertex(T.label, "Person", "users", jsonArray);
    this.sqlgGraph.tx().commit();
    JsonNode value = this.sqlgGraph.traversal().V(v1.id()).next().value("users");
    Assert.assertEquals(jsonArray, value);
}
 
Example 14
Source File: TestJson.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testJson() {
    ObjectMapper objectMapper =  new ObjectMapper();
    ObjectNode json = new ObjectNode(objectMapper.getNodeFactory());
    json.put("username", "john");
    Vertex v1 = this.sqlgGraph.addVertex(T.label, "Person", "doc", json);
    this.sqlgGraph.tx().commit();
    JsonNode value = this.sqlgGraph.traversal().V(v1.id()).next().value("doc");
    Assert.assertEquals(json, value);
}
 
Example 15
Source File: TopicOperatorReplicationIT.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Test
public void testKafkaTopicModifiedChangedReplication() throws Exception {
    // create the topicResource
    String topicName = "test-kafkatopic-modified-with-changed-replication";
    String resourceName = createTopic(topicName, asList(1));

    // now change the topicResource
    KafkaTopic changedTopic = new KafkaTopicBuilder(operation().inNamespace(NAMESPACE).withName(resourceName).get())
            .editOrNewSpec().withReplicas(2).endSpec().build();
    operation().inNamespace(NAMESPACE).withName(resourceName).patch(changedTopic);
    assertStatusNotReady(topicName,
            "Changing 'spec.replicas' is not supported. " +
                    "This KafkaTopic's 'spec.replicas' should be reverted to 1 and then " +
                    "the replication should be changed directly in Kafka.");

    // Now do the revert
    changedTopic = new KafkaTopicBuilder(operation().inNamespace(NAMESPACE).withName(resourceName).get())
            .editOrNewSpec().withReplicas(1).endSpec().build();
    operation().inNamespace(NAMESPACE).withName(resourceName).patch(changedTopic);
    assertStatusReady(topicName);

    File file = File.createTempFile(getClass().getSimpleName(), ".json");
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode root = new ObjectNode(mapper.getNodeFactory());
    root.put("version", 1)
        .putArray("partitions")
            .addObject()
                .put("topic", topicName)
                .put("partition", 0)
                .putArray("replicas")
                    .add(1)
                    .add(2);
    mapper.writeValue(file, root);
    LOGGER.info("Creating 2nd replica: {}", mapper.writeValueAsString(root));

    // Now change it in Kafka
    doReassignmentCommand(
            //"--boostrap-server", kafkaCluster.brokerList(),
            "--zookeeper", "localhost:" + kafkaCluster.zkPort(),
            "--reassignment-json-file", file.getAbsolutePath(),
            "--execute");

    LOGGER.info("Waiting for reassignment completion");
    waitFor(() -> {
        String output = doReassignmentCommand(
                //"--boostrap-server", kafkaCluster.brokerList(),
                "--zookeeper", "localhost:" + kafkaCluster.zkPort(),
                "--reassignment-json-file", file.getAbsolutePath(),
                "--verify");
        LOGGER.info(output);

        if (output.contains("Reassignment of partition test-kafkatopic-modified-with-changed-replication-0 is still in progress")) {
            return false;
        } else {
            assertThat("Reassignment is no longer in progress, but wasn't successful: " + output,
                    output.contains("Reassignment of partition test-kafkatopic-modified-with-changed-replication-0 completed successfully"), is(true));
            return true;
        }
    }, "reassignment completion");

    // wait for reconciliation and that now replicas=2.
    waitFor(() -> {
        KafkaTopic kafkaTopic = Crds.topicOperation(kubeClient).inNamespace(NAMESPACE).withName(resourceName).get();
        LOGGER.info(kafkaTopic == null ? "Null topic" : kafkaTopic.toString());
        return kafkaTopic.getSpec().getReplicas() == 2;
    }, "KafkaTopic.spec.replicas=2");

    // And check that the status is ready
    assertStatusReady(topicName);
}
 
Example 16
Source File: JacksonProvider.java    From JsonSurfer with MIT License 4 votes vote down vote up
public JacksonProvider(ObjectMapper om) {
    this.om = om;
    this.factory = om.getNodeFactory();
}
 
Example 17
Source File: CrdGenerator.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
CrdGenerator(ObjectMapper mapper, Map<String, String> labels) {
    this.mapper = mapper;
    this.nf = mapper.getNodeFactory();
    this.labels = labels;
}