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

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

Introduction

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

Prototype

public TextNode(String paramString) 

Source Link

Usage

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:managers.nodes.AVMManager.java

private Promise<JsonNode> getValue(JsonNode parentAVM, JsonNode feature) {
    final String parentUUID = parentAVM.get("uuid").asText();
    final String ruleUUID = parentAVM.get("ruleUUID").asText();
    if (feature.get("type").asText().equals("complex")) {
        ObjectNode props = ((ObjectNode) feature).deepCopy().retain("name");
        Promise<Feature> feat = Feature.nodes.get(props);
        return feat.flatMap(new Function<Feature, Promise<JsonNode>>() {
            public Promise<JsonNode> apply(Feature feat) {
                String subUUID = UUIDGenerator.from(parentUUID + feat.getUUID());
                ObjectNode substructure = Json.newObject();
                substructure.put("uuid", subUUID);
                substructure.put("ruleUUID", ruleUUID);
                return toJSON(substructure);
            }/*from   w  w w  .j  a  v a2s . co m*/
        });
    }
    Feature f = new Feature(feature.get("name").asText());
    ObjectNode properties = Json.newObject();
    properties.put("rule", ruleUUID);
    properties.put("avm", parentUUID);
    Promise<JsonNode> node = Has.relationships.endNode(f, properties);
    return node.map(new Function<JsonNode, JsonNode>() {
        public JsonNode apply(JsonNode node) {
            String name = node.get("name").asText();
            return new TextNode(name);
        }
    });
}

From source file:org.apache.olingo.fit.utils.AbstractJSONUtilities.java

@Override
public InputStream addEditLink(final InputStream content, final String title, final String href)
        throws Exception {
    final ObjectMapper mapper = new ObjectMapper();
    final ObjectNode srcNode = (ObjectNode) mapper.readTree(content);
    IOUtils.closeQuietly(content);//w  ww.  ja v a 2 s. c  o m

    srcNode.set(JSON_EDITLINK_NAME, new TextNode(href));
    return IOUtils.toInputStream(srcNode.toString());
}

From source file:org.apache.olingo.fit.utils.AbstractJSONUtilities.java

@Override
public InputStream replaceProperty(final InputStream src, final InputStream replacement,
        final List<String> path, final boolean justValue) throws Exception {
    final ObjectMapper mapper = new ObjectMapper();
    final ObjectNode srcNode = (ObjectNode) mapper.readTree(src);
    IOUtils.closeQuietly(src);//from   w w w  .ja v a2  s . c  o m

    final JsonNode replacementNode;
    if (justValue) {
        replacementNode = new TextNode(IOUtils.toString(replacement));
    } else {
        replacementNode = (ObjectNode) mapper.readTree(replacement);
    }
    IOUtils.closeQuietly(replacement);

    JsonNode node = srcNode;
    for (int i = 0; i < path.size() - 1; i++) {
        node = node.get(path.get(i));
        if (node == null) {
            throw new NotFoundException();
        }
    }

    ((ObjectNode) node).set(path.get(path.size() - 1), replacementNode);

    return IOUtils.toInputStream(srcNode.toString());
}

From source file:com.ikanow.aleph2.graph.titan.utils.TestTitanGraphBuildingUtils.java

@Test
public void test_jsonNodeToObject() throws JsonProcessingException, IOException {
    assertEquals(3L, ((Number) TitanGraphBuildingUtils.jsonNodeToObject(_mapper.readTree("3"))).longValue());
    assertEquals(3.1,/*from www . ja  v a2s  .  c o m*/
            ((Double) TitanGraphBuildingUtils.jsonNodeToObject(_mapper.readTree("3.1"))).doubleValue(),
            0.00000001);
    assertEquals(true,
            ((Boolean) TitanGraphBuildingUtils.jsonNodeToObject(_mapper.readTree("true"))).booleanValue());
    assertEquals("alex", TitanGraphBuildingUtils.jsonNodeToObject(new TextNode("alex")));
}

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.json.JsonFilterReader.java

protected String filterStreamValue(Level node) {
    String value;/* ww  w  .  jav a2s  . c om*/
    if (node.isArray()) {
        value = node.node.get(0).asText();
    } else {
        value = node.node.get(node.field).asText();
    }
    String rule = null;
    UrlRewriteFilterGroupDescriptor scope = node.scopeConfig;
    //TODO: Scan the top level apply rules for the first match.
    if (scope != null) {
        for (UrlRewriteFilterPathDescriptor selector : scope.getSelectors()) {
            JsonPath.Expression path = (JsonPath.Expression) selector.compiledPath(JPATH_COMPILER);
            List<JsonPath.Match> matches = path.evaluate(node.scopeNode);
            if (matches != null && matches.size() > 0) {
                JsonPath.Match match = matches.get(0);
                if (match.getNode().isTextual()) {
                    if (selector instanceof UrlRewriteFilterApplyDescriptor) {
                        UrlRewriteFilterApplyDescriptor apply = (UrlRewriteFilterApplyDescriptor) selector;
                        rule = apply.rule();
                        break;
                    }
                }
            }
        }
    }
    try {
        value = filterValueString(node.field, value, rule);
        if (node.isArray()) {
            ((ArrayNode) node.node).set(0, new TextNode(value));
        } else {
            ((ObjectNode) node.node).put(node.field, value);
        }
    } catch (Exception e) {
        LOG.failedToFilterValue(value, rule, e);
    }
    return value;
}

From source file:com.ikanow.aleph2.graph.titan.utils.TestTitanGraphBuildingUtils.java

@Test
public void test_convertToObject() {

    // Graph schema
    final GraphSchemaBean graph_schema = BeanTemplateUtils.build(GraphSchemaBean.class)
            .with(GraphSchemaBean::deduplication_fields,
                    Arrays.asList(GraphAnnotationBean.name, GraphAnnotationBean.type))
            .done().get();//from   w ww  .  j av a2s.  c  om

    final ObjectNode graphson = _mapper.createObjectNode().put("name", "alex");

    assertEquals(graphson, TitanGraphBuildingUtils.convertToObject(graphson, graph_schema));
    assertEquals(graphson.toString(),
            TitanGraphBuildingUtils.convertToObject(new TextNode("alex"), graph_schema).toString());
}

From source file:com.ikanow.aleph2.management_db.mongodb.services.TestIkanowV1SyncService_LibraryJars.java

@Test
public void test_compareSharesToLibraryBeans_get()
        throws JsonProcessingException, IOException, ParseException, InterruptedException, ExecutionException {
    @SuppressWarnings("unchecked")
    ICrudService<JsonNode> v1_share_db = this._service_context.getCoreManagementDbService()
            .getUnderlyingPlatformDriver(ICrudService.class, Optional.of("social.share")).get();

    v1_share_db.deleteDatastore().get();

    IManagementCrudService<SharedLibraryBean> library_db = this._service_context.getCoreManagementDbService()
            .getSharedLibraryStore();/*from   w  ww  .ja va  2  s .c om*/

    library_db.deleteDatastore().get();

    // Create 2 V1 sources

    final ObjectMapper mapper = BeanTemplateUtils.configureMapper(Optional.empty());

    final JsonNode v1_share_1 = mapper
            .readTree(this.getClass().getResourceAsStream("test_v1_sync_sample_share.json"));
    final JsonNode v1_share_2 = mapper
            .readTree(this.getClass().getResourceAsStream("test_v1_sync_sample_share.json"));
    final JsonNode v1_share_3 = mapper
            .readTree(this.getClass().getResourceAsStream("test_v1_sync_sample_share.json"));

    ((ObjectNode) v1_share_2).set("_id", new TextNode("655d44e3347d336b3e8c4cbe"));
    ((ObjectNode) v1_share_2).set("title", new TextNode("/app/aleph2/library/misc/library2.jar"));

    ((ObjectNode) v1_share_3).set("_id", new TextNode("755d44e3347d336b3e8c4cbe"));
    ((ObjectNode) v1_share_3).set("title", new TextNode("/app/aleph2/library/misc/library3.jar"));

    assertEquals(0L, (long) v1_share_db.countObjects().get());
    v1_share_db.storeObjects(Arrays.asList(v1_share_1, v1_share_2, v1_share_3)).get();
    assertEquals(3L, (long) v1_share_db.countObjects().get());

    // Create 2 buckets

    final SharedLibraryBean share1 = IkanowV1SyncService_LibraryJars.getLibraryBeanFromV1Share(v1_share_1);
    final SharedLibraryBean share2 = IkanowV1SyncService_LibraryJars.getLibraryBeanFromV1Share(v1_share_2);

    assertEquals(0L, (long) library_db.countObjects().get());
    library_db.storeObjects(Arrays.asList(share1, share2)).get();
    assertEquals(2L, (long) library_db.countObjects().get());

    // Run the function under test

    final Tuple2<Map<String, String>, Map<String, Date>> f_res = IkanowV1SyncService_LibraryJars
            .compareJarsToLibaryBeans_get(library_db, v1_share_db).get();

    assertEquals(
            "{755d44e3347d336b3e8c4cbe=May 21, 2015 02:37:24 AM UTC, 555d44e3347d336b3e8c4cbe=May 21, 2015 02:37:24 AM UTC, 655d44e3347d336b3e8c4cbe=May 21, 2015 02:37:24 AM UTC}",
            f_res._1().toString());

    assertEquals(2, f_res._2().size());
    //(times are sys dependent here so just check the keys)      
    assertEquals(true, f_res._2().containsKey("555d44e3347d336b3e8c4cbe"));
    assertEquals(true, f_res._2().containsKey("655d44e3347d336b3e8c4cbe"));
}

From source file:com.ikanow.aleph2.management_db.mongodb.services.TestIkanowV1SyncService_Buckets.java

@Test
public void test_compareSourcesToBuckets_get()
        throws JsonProcessingException, IOException, ParseException, InterruptedException, ExecutionException {
    _logger.info("Starting test_compareSourcesToBuckets_get");

    @SuppressWarnings("unchecked")
    ICrudService<JsonNode> v1_source_db = this._service_context.getCoreManagementDbService()
            .getUnderlyingPlatformDriver(ICrudService.class, Optional.of("ingest.source")).get();

    v1_source_db.deleteDatastore().get();

    IManagementCrudService<DataBucketBean> bucket_db = this._service_context.getCoreManagementDbService()
            .getDataBucketStore();//from w  ww .j  a  v  a  2  s.  c o m

    bucket_db.deleteDatastore().get();

    // Create 2 V1 sources

    final ObjectMapper mapper = BeanTemplateUtils.configureMapper(Optional.empty());

    final JsonNode v1_source_1 = mapper
            .readTree(this.getClass().getResourceAsStream("test_v1_sync_sample_source.json"));
    final JsonNode v1_source_2 = mapper
            .readTree(this.getClass().getResourceAsStream("test_v1_sync_sample_source.json"));
    final JsonNode v1_source_3 = mapper
            .readTree(this.getClass().getResourceAsStream("test_v1_sync_sample_source.json"));

    ((ObjectNode) v1_source_2).set("_id", null);
    ((ObjectNode) v1_source_2).set("key", new TextNode("aleph...bucket.Template_V2_data_bucket.2"));

    ((ObjectNode) v1_source_3).set("_id", null);
    ((ObjectNode) v1_source_3).set("key", new TextNode("aleph...bucket.Template_V2_data_bucket.3"));
    ((ObjectNode) v1_source_3).set("isApproved", BooleanNode.FALSE);

    assertEquals(0L, (long) v1_source_db.countObjects().get());
    v1_source_db.storeObjects(Arrays.asList(v1_source_1, v1_source_2, v1_source_3)).get();
    assertEquals(3L, (long) v1_source_db.countObjects().get());

    // Create 2 buckets

    final DataBucketBean bucket1 = IkanowV1SyncService_Buckets.getBucketFromV1Source(v1_source_1);
    final DataBucketBean bucket2 = IkanowV1SyncService_Buckets.getBucketFromV1Source(v1_source_2);

    assertEquals(0L, (long) bucket_db.countObjects().get());
    bucket_db.storeObjects(Arrays.asList(bucket1, bucket2)).get();
    assertEquals(2L, (long) bucket_db.countObjects().get());

    // Run the function under test

    final Tuple2<Map<String, String>, Map<String, Date>> f_res = IkanowV1SyncService_Buckets
            .compareSourcesToBuckets_get(bucket_db, v1_source_db).get();

    assertEquals(
            "{aleph...bucket.Template_V2_data_bucket.=May 25, 2015 01:52:01 PM UTC, aleph...bucket.Template_V2_data_bucket.3=, aleph...bucket.Template_V2_data_bucket.2=May 25, 2015 01:52:01 PM UTC}",
            f_res._1().toString());

    assertEquals(2, f_res._2().size());
    //(times are sys dependent here so just check the keys)
    assertEquals(true, f_res._2().containsKey("aleph...bucket.Template_V2_data_bucket."));
    assertEquals(true, f_res._2().containsKey("aleph...bucket.Template_V2_data_bucket.2"));
}

From source file:com.ikanow.aleph2.analytics.services.TestDeduplicationService.java

@Test
public void test_extractKeyOrKeys() {

    // extractKeyFields / extractKeyField, single element:
    {/* w  w w  .ja va 2s  . co  m*/
        final ObjectNode test = _mapper.createObjectNode();
        final ObjectNode test_nest = _mapper.createObjectNode();
        test.put("field1", "test1");
        test.put("field2", "test2");
        test.set("nested1", test_nest);
        test_nest.put("nested_field", "nested1");

        // Empty things:

        assertEquals(Optional.empty(), DeduplicationService.extractKeyField(test, "test_notpresent"));
        assertEquals(Optional.empty(), DeduplicationService.extractKeyField(test, "nested1.test_notpresent"));
        assertEquals(Optional.empty(), DeduplicationService.extractKeyField(test, "nested2.nested1"));

        assertEquals(Optional.empty(), DeduplicationService.extractKeyFields(test,
                Arrays.asList("test_notpresent", "test_notpresent2")));
        assertEquals(Optional.empty(),
                DeduplicationService.extractKeyFields(test, Arrays.asList("nested1.test_notpresent")));
        assertEquals(Optional.empty(),
                DeduplicationService.extractKeyFields(test, Arrays.asList("nested2.nested1")));

        // values:

        // single field
        assertEquals("test1", DeduplicationService.extractKeyField(test, "field1").get().asText());
        assertEquals("nested1",
                DeduplicationService.extractKeyField(test, "nested1.nested_field").get().asText());

        // multi-field
        final String expected = "{\"field1\":\"test1\",\"nested1.nested_field\":\"nested1\"}";
        assertEquals(expected, DeduplicationService
                .extractKeyFields(test, Arrays.asList("field1", "nested1.nested_field")).get().toString());
        assertEquals(expected,
                DeduplicationService
                        .extractKeyFields(test, Arrays.asList("field1", "nested1.nested_field", "field3")).get()
                        .toString());
    }
    // Similar but with a stream of objects
    {
        final ObjectNode test1 = _mapper.createObjectNode();
        test1.put("field1", "test1");
        final ObjectNode test2 = _mapper.createObjectNode();
        test2.put("field2", "test2");

        final List<Tuple2<Long, IBatchRecord>> batch = Arrays.<JsonNode>asList(test1, test2).stream()
                .map(j -> Tuples._2T(0L, (IBatchRecord) new BatchRecordUtils.JsonBatchRecord(j)))
                .collect(Collectors.toList());

        assertEquals(Arrays.asList(new TextNode("test1")),
                DeduplicationService.extractKeyField(batch.stream(), "field1").stream().map(t2 -> t2._1())
                        .collect(Collectors.toList()));
        assertEquals(Arrays.asList("{\"field1\":\"test1\"}"),
                DeduplicationService.extractKeyField(batch.stream(), "field1").stream()
                        .map(t2 -> t2._2()._2().getJson().toString()).collect(Collectors.toList()));
        assertEquals(Arrays.asList("{\"field1\":\"test1\"}"),
                DeduplicationService.extractKeyFields(batch.stream(), Arrays.asList("field1")).stream()
                        .map(t2 -> t2._1().toString()).collect(Collectors.toList()));
        assertEquals(Arrays.asList("{\"field1\":\"test1\"}"),
                DeduplicationService.extractKeyFields(batch.stream(), Arrays.asList("field1")).stream()
                        .map(t2 -> t2._2()._2().getJson().toString()).collect(Collectors.toList()));

        //(keep the old results since i keep changing my mind!)
        //         assertEquals(Arrays.asList(new TextNode("test1"), _mapper.createObjectNode()), DeduplicationService.extractKeyField(batch.stream(), "field1").stream().map(t2 -> t2._1()).collect(Collectors.toList()));
        //         assertEquals(Arrays.asList("{\"field1\":\"test1\"}", "{\"field2\":\"test2\"}"), DeduplicationService.extractKeyField(batch.stream(), "field1").stream().map(t2 -> t2._2()._2().getJson().toString()).collect(Collectors.toList()));
        //         assertEquals(Arrays.asList("{\"field1\":\"test1\"}", "{}"), DeduplicationService.extractKeyFields(batch.stream(), Arrays.asList("field1")).stream().map(t2 -> t2._1().toString()).collect(Collectors.toList()));
        //         assertEquals(Arrays.asList("{\"field1\":\"test1\"}", "{\"field2\":\"test2\"}"), DeduplicationService.extractKeyFields(batch.stream(), Arrays.asList("field1")).stream().map(t2 -> t2._2()._2().getJson().toString()).collect(Collectors.toList()));         
    }
    // Another utility function related to these
    {
        final ObjectNode test1 = _mapper.createObjectNode();
        test1.put("field1", "test1");

        assertEquals(new TextNode("test1"),
                DeduplicationService.getKeyFieldsAgain(test1, Either.left("field1")).get());
        assertEquals("{\"field1\":\"test1\"}", DeduplicationService
                .getKeyFieldsAgain(test1, Either.right(Arrays.asList("field1"))).get().toString());
    }

}