Java Code Examples for com.bazaarvoice.jolt.JsonUtils#toJsonString()

The following examples show how to use com.bazaarvoice.jolt.JsonUtils#toJsonString() . 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: TestJoltTransformJSON.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransformInputWithSortr() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(JoltTransformJSON.JOLT_TRANSFORM, JoltTransformJSON.SORTR);
    runner.enqueue(JSON_INPUT);
    runner.run();
    runner.assertAllFlowFilesTransferred(JoltTransformJSON.REL_SUCCESS);
    final MockFlowFile transformed = runner.getFlowFilesForRelationship(JoltTransformJSON.REL_SUCCESS).get(0);
    transformed.assertAttributeExists(CoreAttributes.MIME_TYPE.key());
    transformed.assertAttributeEquals(CoreAttributes.MIME_TYPE.key(),"application/json");
    Object transformedJson = JsonUtils.jsonToObject(new ByteArrayInputStream(transformed.toByteArray()));
    Object compareJson = JsonUtils.jsonToObject(Files.newInputStream(Paths.get("src/test/resources/TestJoltTransformJson/sortrOutput.json")));
    String transformedJsonString = JsonUtils.toJsonString(transformedJson);
    String compareJsonString = JsonUtils.toJsonString(compareJson);
    assertTrue(compareJsonString.equals(transformedJsonString));
}
 
Example 2
Source File: TestJoltTransformJSON.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransformInputWithSortrPopulatedSpec() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(JoltTransformJSON.JOLT_TRANSFORM, JoltTransformJSON.SORTR);
    runner.setProperty(JoltTransformJSON.JOLT_SPEC, "abcd");
    runner.enqueue(JSON_INPUT);
    runner.run();
    runner.assertAllFlowFilesTransferred(JoltTransformJSON.REL_SUCCESS);
    final MockFlowFile transformed = runner.getFlowFilesForRelationship(JoltTransformJSON.REL_SUCCESS).get(0);
    transformed.assertAttributeExists(CoreAttributes.MIME_TYPE.key());
    transformed.assertAttributeEquals(CoreAttributes.MIME_TYPE.key(),"application/json");
    Object transformedJson = JsonUtils.jsonToObject(new ByteArrayInputStream(transformed.toByteArray()));
    Object compareJson = JsonUtils.jsonToObject(Files.newInputStream(Paths.get("src/test/resources/TestJoltTransformJson/sortrOutput.json")));
    String transformedJsonString = JsonUtils.toJsonString(transformedJson);
    String compareJsonString = JsonUtils.toJsonString(compareJson);
    assertTrue(compareJsonString.equals(transformedJsonString));
}
 
Example 3
Source File: TestJoltTransformJSON.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransformInputWithSortrPopulatedSpec() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
    runner.setProperty(JoltTransformJSON.JOLT_TRANSFORM, JoltTransformJSON.SORTR);
    runner.setProperty(JoltTransformJSON.JOLT_SPEC, "abcd");
    runner.enqueue(JSON_INPUT);
    runner.run();
    runner.assertAllFlowFilesTransferred(JoltTransformJSON.REL_SUCCESS);
    final MockFlowFile transformed = runner.getFlowFilesForRelationship(JoltTransformJSON.REL_SUCCESS).get(0);
    transformed.assertAttributeExists(CoreAttributes.MIME_TYPE.key());
    transformed.assertAttributeEquals(CoreAttributes.MIME_TYPE.key(),"application/json");
    Object transformedJson = JsonUtils.jsonToObject(new ByteArrayInputStream(transformed.toByteArray()));
    Object compareJson = JsonUtils.jsonToObject(Files.newInputStream(Paths.get("src/test/resources/TestJoltTransformJson/sortrOutput.json")));
    String transformedJsonString = JsonUtils.toJsonString(transformedJson);
    String compareJsonString = JsonUtils.toJsonString(compareJson);
    assertTrue(compareJsonString.equals(transformedJsonString));
}
 
Example 4
Source File: TestJoltTransformJSON.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransformInputWithSortr() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
    runner.setProperty(JoltTransformJSON.JOLT_TRANSFORM, JoltTransformJSON.SORTR);
    runner.enqueue(JSON_INPUT);
    runner.run();
    runner.assertAllFlowFilesTransferred(JoltTransformJSON.REL_SUCCESS);
    final MockFlowFile transformed = runner.getFlowFilesForRelationship(JoltTransformJSON.REL_SUCCESS).get(0);
    transformed.assertAttributeExists(CoreAttributes.MIME_TYPE.key());
    transformed.assertAttributeEquals(CoreAttributes.MIME_TYPE.key(),"application/json");
    Object transformedJson = JsonUtils.jsonToObject(new ByteArrayInputStream(transformed.toByteArray()));
    Object compareJson = JsonUtils.jsonToObject(Files.newInputStream(Paths.get("src/test/resources/TestJoltTransformJson/sortrOutput.json")));
    String transformedJsonString = JsonUtils.toJsonString(transformedJson);
    String compareJsonString = JsonUtils.toJsonString(compareJson);
    assertTrue(compareJsonString.equals(transformedJsonString));
}
 
Example 5
Source File: ConfigRepoDocumentMother.java    From gocd with Apache License 2.0 5 votes vote down vote up
String versionOneComprehensiveWithNoLocking() {
    Map<String, Object> map = getJSONFor("/v1_comprehensive.json");
    store(map, "1", "target_version");
    remove(map, "pipelines", 0, "enable_pipeline_locking");
    remove(map, "pipelines", 1, "enable_pipeline_locking");
    return JsonUtils.toJsonString(map);
}
 
Example 6
Source File: JoltMapper.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
public Collection<DynamicProperty> map(String source) {
    //Default value is equal to the input json value (in case empty jolt specs)
    String jsonProperties = source;
    ArrayList transformed;
    if (jsonProperties != null && jsonProperties.charAt(0) == '[') {
        transformed = (ArrayList) chainr.transform(JsonUtils.jsonToList(source));
    } else {
        transformed = (ArrayList) chainr.transform(JsonUtils.jsonToMap(source));
    }
    jsonProperties = JsonUtils.toJsonString(transformed);

    //Now ensure current json properties is well formatted.
//    if (validateJson(jsonProperties)) {

    List<Object> items = JsonUtils.jsonToList(jsonProperties);
    Object collect = items.stream()
            .map(item -> {
                Map<String, String> mapItem = (Map<String, String>) item;
                Object key = mapItem.get("key");
                if (key instanceof Number) {
                    return new DynamicProperty(key.toString(), mapItem.get("value"));
                } else {
                    return new DynamicProperty((String) key, mapItem.get("value"));
                }
            })
            .collect(Collectors.toList());

    return (Collection<DynamicProperty>) collect;
}
 
Example 7
Source File: JoltMapper.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
public Collection<DynamicProperty> map(String source) {
    //Default value is equal to the input json value (in case empty jolt specs)
    String jsonProperties = source;
    ArrayList transformed;
    if (jsonProperties != null && jsonProperties.charAt(0) == '[') {
        transformed = (ArrayList) chainr.transform(JsonUtils.jsonToList(source));
    } else {
        transformed = (ArrayList) chainr.transform(JsonUtils.jsonToMap(source));
    }
    jsonProperties = JsonUtils.toJsonString(transformed);

    //Now ensure current json properties is well formatted.
//    if (validateJson(jsonProperties)) {

    List<Object> items = JsonUtils.jsonToList(jsonProperties);
    Object collect = items.stream()
            .map(item -> {
                Map<String, String> mapItem = (Map<String, String>) item;
                Object key = mapItem.get("key");
                if (key instanceof Number) {
                    return new DynamicProperty(key.toString(), mapItem.get("value"));
                } else {
                    return new DynamicProperty((String) key, mapItem.get("value"));
                }
            })
            .collect(Collectors.toList());

    return (Collection<DynamicProperty>) collect;
}
 
Example 8
Source File: ConfigRepoDocumentMother.java    From gocd with Apache License 2.0 4 votes vote down vote up
String v5Simple() {
    return JsonUtils.toJsonString(getJSONFor("/v5_simple.json"));
}
 
Example 9
Source File: ConfigRepoDocumentMother.java    From gocd with Apache License 2.0 4 votes vote down vote up
String v6Pipeline() {
    return JsonUtils.toJsonString(getJSONFor("/v6_pipeline.json"));
}
 
Example 10
Source File: ConfigRepoDocumentMother.java    From gocd with Apache License 2.0 4 votes vote down vote up
String versionOneWithLockingSetTo(boolean enablePipelineLockingValue) {
    Map<String, Object> map = getJSONFor("/v1_simple.json");
    store(map, "1", "target_version");
    store(map, enablePipelineLockingValue, "pipelines", 0, "enable_pipeline_locking");
    return JsonUtils.toJsonString(map);
}
 
Example 11
Source File: ConfigRepoDocumentMother.java    From gocd with Apache License 2.0 4 votes vote down vote up
String v8Pipeline() {
    return JsonUtils.toJsonString(getJSONFor("/v8_pipeline.json"));
}
 
Example 12
Source File: ConfigRepoDocumentMother.java    From gocd with Apache License 2.0 4 votes vote down vote up
String versionTwoComprehensive() {
    return JsonUtils.toJsonString(getJSONFor("/v2_comprehensive.json"));
}
 
Example 13
Source File: ConfigRepoDocumentMother.java    From gocd with Apache License 2.0 4 votes vote down vote up
String v7Pipeline() {
    return JsonUtils.toJsonString(getJSONFor("/v7_pipeline.json"));
}
 
Example 14
Source File: ConfigRepoDocumentMother.java    From gocd with Apache License 2.0 4 votes vote down vote up
String v2WithFetchExternalArtifactTask() {
    return JsonUtils.toJsonString(getJSONFor("/v2_with_fetch_external_artifact_task.json"));
}
 
Example 15
Source File: ConfigRepoDocumentMother.java    From gocd with Apache License 2.0 4 votes vote down vote up
String v8PipelineWithDependencyMaterial() {
    return JsonUtils.toJsonString(getJSONFor("/v8_pipeline_with_dependency_material.json"));
}
 
Example 16
Source File: ConfigRepoDocumentMother.java    From gocd with Apache License 2.0 4 votes vote down vote up
String v9Pipeline() {
    return JsonUtils.toJsonString(getJSONFor("/v9_pipeline.json"));
}
 
Example 17
Source File: ConfigRepoDocumentMother.java    From gocd with Apache License 2.0 4 votes vote down vote up
String v3WithFetchExternalArtifactTask() {
    return JsonUtils.toJsonString(getJSONFor("/v3_with_fetch_external_artifact_task.json"));
}
 
Example 18
Source File: ConfigRepoDocumentMother.java    From gocd with Apache License 2.0 4 votes vote down vote up
String v3ComprehensiveWithDisplayOrderWeightsOf10AndNull() {
    return JsonUtils.toJsonString(getJSONFor("/v3_comprehensive_with_display_order_weight_which_was_introduced_in_v4_for_one_pipeline.json"));
}
 
Example 19
Source File: ConfigRepoDocumentMother.java    From gocd with Apache License 2.0 4 votes vote down vote up
String v4ComprehensiveWithDisplayOrderWeightOfMinusOneForBothPipelines() {
    return JsonUtils.toJsonString(getJSONFor("/v4_comprehensive_with_display_order_weight_of_minus_one_for_both_pipelines.json"));
}
 
Example 20
Source File: ConfigRepoDocumentMother.java    From gocd with Apache License 2.0 4 votes vote down vote up
String v4ComprehensiveWithDisplayOrderWeightsOf10AndMinusOne() {
    return JsonUtils.toJsonString(getJSONFor("/v4_comprehensive_with_display_order_weights_of_10_and_minus_one.json"));
}