com.bazaarvoice.jolt.JsonUtils Java Examples

The following examples show how to use com.bazaarvoice.jolt.JsonUtils. 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 nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransformInputWithCustomTransformationWithJar() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
    final String customJarPath = "src/test/resources/TestJoltTransformJson/TestCustomJoltTransform.jar";
    final String spec = new String(Files.readAllBytes(Paths.get("src/test/resources/TestJoltTransformJson/chainrSpec.json")));
    runner.setProperty(JoltTransformJSON.JOLT_SPEC, spec);
    runner.setProperty(JoltTransformJSON.CUSTOM_CLASS,"TestCustomJoltTransform");
    runner.setProperty(JoltTransformJSON.MODULES,customJarPath);
    runner.setProperty(JoltTransformJSON.JOLT_TRANSFORM,JoltTransformJSON.CUSTOMR);
    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/chainrOutput.json")));
    assertTrue(DIFFY.diff(compareJson, transformedJson).isEmpty());
}
 
Example #2
Source File: DeepCopyTest.java    From jolt with Apache License 2.0 6 votes vote down vote up
@Test
public void deepCopyTest() throws Exception {

    Object input = JsonUtils.classpathToObject( "/json/deepcopy/original.json" );

    Map<String, Object> fiddle = (Map<String, Object>) DeepCopy.simpleDeepCopy( input );

    JoltTestUtil.runDiffy( "Verify that the DeepCopy did in fact make a copy.", input, fiddle );

    // The test is to make a deep copy, then manipulate the copy, and verify that the original did not change  ;)
    // copy and fiddle
    List array = (List) fiddle.get( "array" );
    array.add( "c" );
    array.set( 1, 3 );
    Map<String,Object> subMap = (Map<String,Object>) fiddle.get( "map" );
    subMap.put("c", "c");
    subMap.put("b", 3 );

    // Verify that the input to the copy was unmodified
    Object unmodified = JsonUtils.classpathToObject( "/json/deepcopy/original.json" );
    JoltTestUtil.runDiffy( "Verify that the deepcopy was actually deep / input is unmodified", unmodified, input );

    // Verify we made the modifications we wanted to.
    Object expectedModified = JsonUtils.classpathToObject( "/json/deepcopy/modifed.json" );
    JoltTestUtil.runDiffy( "Verify fiddled post deepcopy object looks correct / was modifed.", expectedModified, fiddle );
}
 
Example #3
Source File: TestJoltTransformJSON.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransformInputWithDefaultrExpressionLanguage() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
    runner.setValidateExpressionUsage(false);
    final String spec = new String(Files.readAllBytes(Paths.get("src/test/resources/TestJoltTransformJson/defaultrELSpec.json")));
    runner.setProperty(JoltTransformJSON.JOLT_SPEC, spec);
    runner.setProperty(JoltTransformJSON.JOLT_TRANSFORM, JoltTransformJSON.DEFAULTR);
    runner.setVariable("quota","5");
    runner.enqueue(JSON_INPUT);
    runner.run();
    runner.assertAllFlowFilesTransferred(JoltTransformJSON.REL_SUCCESS);
    final MockFlowFile transformed = runner.getFlowFilesForRelationship(JoltTransformJSON.REL_SUCCESS).get(0);
    Object transformedJson = JsonUtils.jsonToObject(new ByteArrayInputStream(transformed.toByteArray()));
    Object compareJson = JsonUtils.jsonToObject(Files.newInputStream(Paths.get("src/test/resources/TestJoltTransformJson/defaultrELOutput.json")));
    assertTrue(DIFFY.diff(compareJson, transformedJson).isEmpty());
}
 
Example #4
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 #5
Source File: TestJoltTransformJSON.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransformInputWithCustomTransformationWithDir() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
    final String customJarPath = "src/test/resources/TestJoltTransformJson";
    final String spec = new String(Files.readAllBytes(Paths.get("src/test/resources/TestJoltTransformJson/chainrSpec.json")));
    runner.setProperty(JoltTransformJSON.JOLT_SPEC, spec);
    runner.setProperty(JoltTransformJSON.CUSTOM_CLASS,"TestCustomJoltTransform");
    runner.setProperty(JoltTransformJSON.MODULES,customJarPath);
    runner.setProperty(JoltTransformJSON.JOLT_TRANSFORM,JoltTransformJSON.CUSTOMR);
    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/chainrOutput.json")));
    assertTrue(DIFFY.diff(compareJson, transformedJson).isEmpty());
}
 
Example #6
Source File: GuicedChainrTest.java    From jolt with Apache License 2.0 6 votes vote down vote up
@Test( expectedExceptions = SpecException.class )
public void itBlowsUpForMissingProviderStockTransform() throws IOException
{
    String testPath = "/json/chainr/guice_spec.json";
    Map<String, Object> testUnit = JsonUtils.classpathToMap( testPath );

    Object spec = testUnit.get( "spec" );

    Module parentModule = new AbstractModule() {
        @Override
        protected void configure() {
        }

        @Provides
        public GuiceSpecDrivenTransform.GuiceConfig getConfigD() {
            return new GuiceSpecDrivenTransform.GuiceConfig( "dd" );
        }
    };

    Chainr.fromSpec( spec, new GuiceChainrInstantiator( parentModule ) );
}
 
Example #7
Source File: TestJoltTransformJSON.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testJoltSpecEL() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
    final String spec = "${joltSpec}";
    runner.setProperty(JoltTransformJSON.JOLT_SPEC, spec);
    runner.setProperty(JoltTransformJSON.JOLT_TRANSFORM,JoltTransformJSON.DEFAULTR);
    final Map<String, String> attributes = Collections.singletonMap("joltSpec",
            "{\"RatingRange\":5,\"rating\":{\"*\":{\"MaxLabel\":\"High\",\"MinLabel\":\"Low\",\"DisplayType\":\"NORMAL\"}}}");
    runner.enqueue(JSON_INPUT, attributes);
    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/defaultrOutput.json")));
    assertTrue(DIFFY.diff(compareJson, transformedJson).isEmpty());
}
 
Example #8
Source File: TestJoltTransformJSON.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransformInputWithShiftr() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
    runner.setValidateExpressionUsage(false);
    final String spec = new String(Files.readAllBytes(Paths.get("src/test/resources/TestJoltTransformJson/shiftrSpec.json")));
    runner.setProperty(JoltTransformJSON.JOLT_SPEC, spec);
    runner.setProperty(JoltTransformJSON.JOLT_TRANSFORM, JoltTransformJSON.SHIFTR);
    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/shiftrOutput.json")));
    assertTrue(DIFFY.diff(compareJson, transformedJson).isEmpty());
}
 
Example #9
Source File: TestJoltTransformJSON.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransformInputWithChainr() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
    runner.setValidateExpressionUsage(false);
    final String spec = new String(Files.readAllBytes(Paths.get("src/test/resources/TestJoltTransformJson/chainrSpec.json")));
    runner.setProperty(JoltTransformJSON.JOLT_SPEC, spec);
    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/chainrOutput.json")));
    assertTrue(DIFFY.diff(compareJson, transformedJson).isEmpty());
}
 
Example #10
Source File: TransformJSONResource.java    From nifi with Apache License 2.0 6 votes vote down vote up
protected Object getSpecificationJsonObject(JoltSpecificationDTO specificationDTO, boolean evaluateAttributes){

        if (!StringUtils.isEmpty(specificationDTO.getSpecification()) ){

            final String specification;

            if(evaluateAttributes) {
                PreparedQuery preparedQuery = Query.prepare(specificationDTO.getSpecification());
                Map<String, String> attributes = specificationDTO.getExpressionLanguageAttributes() == null ? Collections.emptyMap() : specificationDTO.getExpressionLanguageAttributes();
                specification = preparedQuery.evaluateExpressions(new StandardEvaluationContext(attributes), null);
            }else{
                specification = specificationDTO.getSpecification().replaceAll("\\$\\{","\\\\\\\\\\$\\{");
            }
            return JsonUtils.jsonToObject(specification, DEFAULT_CHARSET);

        }else{
            return null;
        }
    }
 
Example #11
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 #12
Source File: TestTransformJSONResource.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecuteWithValidCustomSpec() {
    final Diffy diffy = new Diffy();
    JoltSpecificationDTO joltSpecificationDTO = new JoltSpecificationDTO("jolt-transform-custom","[{ \"operation\": \"default\", \"spec\":{ \"custom-id\" :4 }}]");
    String inputJson = "{\"rating\":{\"quality\":2,\"count\":1}}";
    joltSpecificationDTO.setInput(inputJson);
    joltSpecificationDTO.setCustomClass("TestCustomJoltTransform");
    joltSpecificationDTO.setModules("src/test/resources/TestTransformJSONResource/TestCustomJoltTransform.jar");
    String responseString = client().target(getBaseUri())
            .path("/standard/transformjson/execute")
            .request()
            .post(Entity.json(joltSpecificationDTO), String.class);

    Object transformedJson = JsonUtils.jsonToObject(responseString);
    Object compareJson = JsonUtils.jsonToObject("{\"rating\":{\"quality\":2,\"count\":1}, \"custom-id\": 4}");
    assertNotNull(transformedJson);
    assertTrue(diffy.diff(compareJson, transformedJson).isEmpty());
}
 
Example #13
Source File: TestTransformJSONResource.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecuteWithValidExpressionLanguageSpec() {
    final Diffy diffy = new Diffy();
    JoltSpecificationDTO joltSpecificationDTO = new JoltSpecificationDTO("jolt-transform-shift","{ \"rating\" : {\"quality\": \"${qual_var}\"} }");
    String inputJson = "{\"rating\":{\"quality\":2,\"count\":1}}";
    joltSpecificationDTO.setInput(inputJson);
    Map<String,String> attributes = new HashMap<String,String>();
    attributes.put("qual_var","qa");
    joltSpecificationDTO.setExpressionLanguageAttributes(attributes);
    String responseString = client().target(getBaseUri())
            .path("/standard/transformjson/execute")
            .request()
            .post(Entity.json(joltSpecificationDTO), String.class);

    Object transformedJson = JsonUtils.jsonToObject(responseString);
    Object compareJson = JsonUtils.jsonToObject( "{\"qa\":2}}");
    assertNotNull(transformedJson);
    assertTrue(diffy.diff(compareJson, transformedJson).isEmpty());
}
 
Example #14
Source File: TestProcessorResource.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetProperties() {

    final NiFiWebConfigurationContext niFiWebConfigurationContext = mock(NiFiWebConfigurationContext.class);
    final Map<String,String> properties = new HashMap<>();
    properties.put("jolt-transform","jolt-transform-chain");
    final ComponentDetails componentDetails = new ComponentDetails.Builder().properties(properties).build();

    Mockito.when(servletContext.getAttribute(Mockito.anyString())).thenReturn(niFiWebConfigurationContext);
    Mockito.when(niFiWebConfigurationContext.updateComponent(any(NiFiWebConfigurationRequestContext.class), AdditionalMatchers.or(any(String.class), isNull()),
            any(Map.class))).thenReturn(componentDetails);

    Response response = client().target(getBaseUri())
            .path("/standard/processor/properties")
            .queryParam("processorId","1")
            .queryParam("clientId","1")
            .queryParam("revisionId","1")
            .request()
            .put(Entity.json(JsonUtils.toJsonString(properties)));

    assertNotNull(response);
    JsonNode jsonNode = response.readEntity(JsonNode.class);
    assertNotNull(jsonNode);
    assertTrue(jsonNode.get("properties").get("jolt-transform").asText().equals("jolt-transform-chain"));
}
 
Example #15
Source File: GuicedChainrContextTest.java    From jolt with Apache License 2.0 6 votes vote down vote up
@DataProvider
public Iterator<Object[]> getCases() throws IOException {

    String testPath = "/json/chainr/guice_spec_with_context.json";
    Map<String, Object> testSuite = JsonUtils.classpathToMap( testPath );

    Object spec = testSuite.get( "spec" );
    List<Map> tests = (List<Map>) testSuite.get( "tests" );

    List<Object[]> accum = Lists.newLinkedList();

    for ( Map testCase : tests ) {

        String testCaseName = (String) testCase.get( "testCaseName" );
        Object input = testCase.get( "input" );
        Map<String,Object> context = (Map<String,Object>) testCase.get( "context" );
        Object expected = testCase.get( "expected" );

        accum.add( new Object[] { testCaseName, spec, input, context, expected } );
    }

    return accum.iterator();
}
 
Example #16
Source File: TestJoltTransformJSON.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransformInputWithCustomTransformationWithJar() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
    runner.setValidateExpressionUsage(false);
    final String customJarPath = "src/test/resources/TestJoltTransformJson/TestCustomJoltTransform.jar";
    final String spec = new String(Files.readAllBytes(Paths.get("src/test/resources/TestJoltTransformJson/chainrSpec.json")));
    runner.setProperty(JoltTransformJSON.JOLT_SPEC, spec);
    runner.setProperty(JoltTransformJSON.CUSTOM_CLASS,"TestCustomJoltTransform");
    runner.setProperty(JoltTransformJSON.MODULES,customJarPath);
    runner.setProperty(JoltTransformJSON.JOLT_TRANSFORM,JoltTransformJSON.CUSTOMR);
    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/chainrOutput.json")));
    assertTrue(DIFFY.diff(compareJson, transformedJson).isEmpty());
}
 
Example #17
Source File: TestJoltTransformJSON.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransformInputCustomTransformationIgnored() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
    final String customJarPath = "src/test/resources/TestJoltTransformJson/TestCustomJoltTransform.jar";
    final String spec = new String(Files.readAllBytes(Paths.get("src/test/resources/TestJoltTransformJson/defaultrSpec.json")));
    runner.setProperty(JoltTransformJSON.JOLT_SPEC, spec);
    runner.setProperty(JoltTransformJSON.CUSTOM_CLASS,"TestCustomJoltTransform");
    runner.setProperty(JoltTransformJSON.MODULES,customJarPath);
    runner.setProperty(JoltTransformJSON.JOLT_TRANSFORM,JoltTransformJSON.DEFAULTR);
    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/defaultrOutput.json")));
    assertTrue(DIFFY.diff(compareJson, transformedJson).isEmpty());
}
 
Example #18
Source File: KeyOrderingTest.java    From jolt with Apache License 2.0 6 votes vote down vote up
@DataProvider
public Object[][] shiftrKeyOrderingTestCases() throws IOException {
    return new Object[][] {
        {
            "Simple * and &",
            JsonUtils.jsonToMap( "{ \"*\" : { \"a\" : \"b\" }, \"&\" : { \"a\" : \"b\" } }" ),
            Arrays.asList( "&(0,0)", "*" )
        },
        {
            "2* and 2&",
            JsonUtils.jsonToMap( "{ \"rating-*\" : { \"a\" : \"b\" }, \"rating-range-*\" : { \"a\" : \"b\" }, \"&\" : { \"a\" : \"b\" }, \"tuna-&(0)\" : { \"a\" : \"b\" } }" ),
            Arrays.asList( "tuna-&(0,0)", "&(0,0)", "rating-range-*", "rating-*" )
        },
        {
            "2& alpha-number based fallback",
            JsonUtils.jsonToMap( "{ \"&\" : { \"a\" : \"b\" }, \"&(0,1)\" : { \"a\" : \"b\" } }" ),
            Arrays.asList( "&(0,0)", "&(0,1)" )
        },
        {
            "2* and 2& alpha fallback",
            JsonUtils.jsonToMap( "{ \"aaaa-*\" : { \"a\" : \"b\" }, \"bbbb-*\" : { \"a\" : \"b\" }, \"aaaa-&\" : { \"a\" : \"b\" }, \"bbbb-&(0)\" : { \"a\" : \"b\" } }" ),
            Arrays.asList( "aaaa-&(0,0)", "bbbb-&(0,0)", "aaaa-*", "bbbb-*" )
        }
    };
}
 
Example #19
Source File: TestProcessorResource.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetProperties() throws JSONException{

    final NiFiWebConfigurationContext niFiWebConfigurationContext = mock(NiFiWebConfigurationContext.class);
    final Map<String,String> properties = new HashMap<>();
    properties.put("jolt-transform","jolt-transform-chain");
    final ComponentDetails componentDetails = new ComponentDetails.Builder().properties(properties).build();

    Mockito.when(servletContext.getAttribute(Mockito.anyString())).thenReturn(niFiWebConfigurationContext);
    Mockito.when(niFiWebConfigurationContext.updateComponent(any(NiFiWebConfigurationRequestContext.class),any(String.class),any(Map.class))).thenReturn(componentDetails);

    ClientResponse response = client().resource(getBaseURI()).path("/standard/processor/properties")
                                                           .queryParam("processorId","1")
                                                           .queryParam("clientId","1")
                                                           .queryParam("revisionId","1")
                                                           .type(MediaType.APPLICATION_JSON_TYPE)
                                                           .put(ClientResponse.class,JsonUtils.toJsonString(properties));
    assertNotNull(response);
    JSONObject jsonObject = response.getEntity(JSONObject.class);
    assertNotNull(jsonObject);
    assertTrue(jsonObject.getJSONObject("properties").get("jolt-transform").equals("jolt-transform-chain"));
}
 
Example #20
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 #21
Source File: GuicedChainrTest.java    From jolt with Apache License 2.0 6 votes vote down vote up
@Test( expectedExceptions = SpecException.class )
public void itBlowsUpForMissingProviderSpecTransform() throws IOException
{
    String testPath = "/json/chainr/guice_spec.json";
    Map<String, Object> testUnit = JsonUtils.classpathToMap( testPath );

    Object spec = testUnit.get( "spec" );

    Module parentModule = new AbstractModule() {
        @Override
        protected void configure() {
        }

        @Provides
        public GuiceTransform.GuiceConfig getConfigC() {
            return new GuiceTransform.GuiceConfig( "c", "cc" );
        }
    };

    Chainr.fromSpec( spec, new GuiceChainrInstantiator( parentModule ) );
}
 
Example #22
Source File: TestJoltTransformJSON.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransformInputCustomTransformationIgnored() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
    runner.setValidateExpressionUsage(false);
    final String customJarPath = "src/test/resources/TestJoltTransformJson/TestCustomJoltTransform.jar";
    final String spec = new String(Files.readAllBytes(Paths.get("src/test/resources/TestJoltTransformJson/defaultrSpec.json")));
    runner.setProperty(JoltTransformJSON.JOLT_SPEC, spec);
    runner.setProperty(JoltTransformJSON.CUSTOM_CLASS,"TestCustomJoltTransform");
    runner.setProperty(JoltTransformJSON.MODULES,customJarPath);
    runner.setProperty(JoltTransformJSON.JOLT_TRANSFORM,JoltTransformJSON.DEFAULTR);
    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/defaultrOutput.json")));
    assertTrue(DIFFY.diff(compareJson, transformedJson).isEmpty());
}
 
Example #23
Source File: TransformJSONResource.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@POST
@Produces({MediaType.APPLICATION_JSON})
@Path("/execute")
public Response executeSpec(JoltSpecificationDTO specificationDTO) {

    try {
        JoltTransform transform = getTransformation(specificationDTO,true);
        Object inputJson = JsonUtils.jsonToObject(specificationDTO.getInput());
        return Response.ok(JsonUtils.toJsonString(TransformUtils.transform(transform,inputJson))).build();

    }catch(final Exception e){
        logger.error("Execute Specification Failed - " + e.toString());
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }

}
 
Example #24
Source File: JoltUtilsSquashTest.java    From jolt with Apache License 2.0 6 votes vote down vote up
@Test
public void squashNullsInAMapTest() {
    Map<String,Object> actual = new HashMap<>();
    actual.put( "a", 1 );
    actual.put( "b", null );
    actual.put( "c", "C" );

    Map<String,Object>  expectedMap = new HashMap<>();
    expectedMap.put( "a",  1  );
    expectedMap.put( "c", "C" );

    Objects.squashNulls( actual );

    Diffy.Result result = diffy.diff( expectedMap, actual );
    if (!result.isEmpty()) {
        Assert.fail( "Failed.\nhere is a diff:\nexpected: " + JsonUtils.toJsonString( result.expected ) + "\n  actual: " + JsonUtils.toJsonString( result.actual ) );
    }
}
 
Example #25
Source File: JoltTransformRecord.java    From nifi with Apache License 2.0 5 votes vote down vote up
private JoltTransform createTransform(final ProcessContext context, final String specString) throws Exception {
    final Object specJson;
    if (context.getProperty(JOLT_SPEC).isSet() && !SORTR.getValue().equals(context.getProperty(JOLT_TRANSFORM).getValue())) {
        specJson = JsonUtils.jsonToObject(specString, DEFAULT_CHARSET);
    } else {
        specJson = null;
    }

    if (CUSTOMR.getValue().equals(context.getProperty(JOLT_TRANSFORM).getValue())) {
        return TransformFactory.getCustomTransform(Thread.currentThread().getContextClassLoader(), context.getProperty(CUSTOM_CLASS).getValue(), specJson);
    } else {
        return TransformFactory.getTransform(Thread.currentThread().getContextClassLoader(), context.getProperty(JOLT_TRANSFORM).getValue(), specJson);
    }
}
 
Example #26
Source File: WebOptionsManagerImpl.java    From teamcity-web-parameters with MIT License 5 votes vote down vote up
@NotNull
private InputStream transform(@NotNull InputStream content, @NotNull RequestConfiguration configuration) {
    if (StringUtil.isNotEmpty(configuration.getTransform())) {
        List chainrSpecJSON = JsonUtils.jsonToList(configuration.getTransform());
        Chainr chainr = Chainr.fromSpec(chainrSpecJSON);
        Object inputJSON = JsonUtils.jsonToObject(content);
        Object transformedOutput = chainr.transform(inputJSON);

        return new ByteArrayInputStream(JsonUtils.toJsonString(transformedOutput).getBytes(StandardCharsets.UTF_8));
    } else {
        return content;
    }
}
 
Example #27
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 #28
Source File: TestJoltTransformJSON.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testTransformInputWithModifierDefine() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
    final String spec = new String(Files.readAllBytes(Paths.get("src/test/resources/TestJoltTransformJson/modifierDefineSpec.json")));
    runner.setProperty(JoltTransformJSON.JOLT_SPEC, spec);
    runner.setProperty(JoltTransformJSON.JOLT_TRANSFORM, JoltTransformJSON.MODIFIER_DEFAULTR);
    runner.enqueue(JSON_INPUT);
    runner.run();
    runner.assertAllFlowFilesTransferred(JoltTransformJSON.REL_SUCCESS);
    final MockFlowFile transformed = runner.getFlowFilesForRelationship(JoltTransformJSON.REL_SUCCESS).get(0);
    Object transformedJson = JsonUtils.jsonToObject(new ByteArrayInputStream(transformed.toByteArray()));
    Object compareJson = JsonUtils.jsonToObject(Files.newInputStream(Paths.get("src/test/resources/TestJoltTransformJson/modifierDefineOutput.json")));
    assertTrue(DIFFY.diff(compareJson, transformedJson).isEmpty());
}
 
Example #29
Source File: TestJoltTransformJSON.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testTransformInputWithCardinality() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
    final String spec = new String(Files.readAllBytes(Paths.get("src/test/resources/TestJoltTransformJson/cardrSpec.json")));
    runner.setProperty(JoltTransformJSON.JOLT_SPEC, spec);
    runner.setProperty(JoltTransformJSON.JOLT_TRANSFORM, JoltTransformJSON.CARDINALITY);
    runner.enqueue(JSON_INPUT);
    runner.run();
    runner.assertAllFlowFilesTransferred(JoltTransformJSON.REL_SUCCESS);
    final MockFlowFile transformed = runner.getFlowFilesForRelationship(JoltTransformJSON.REL_SUCCESS).get(0);
    Object transformedJson = JsonUtils.jsonToObject(new ByteArrayInputStream(transformed.toByteArray()));
    Object compareJson = JsonUtils.jsonToObject(Files.newInputStream(Paths.get("src/test/resources/TestJoltTransformJson/cardrOutput.json")));
    assertTrue(DIFFY.diff(compareJson, transformedJson).isEmpty());
}
 
Example #30
Source File: JoltUtilsSquashTest.java    From jolt with Apache License 2.0 5 votes vote down vote up
@Test
public void squashNullsInAListTest() {
    List actual = new ArrayList();
    actual.addAll( Arrays.asList( "a", null, 1, null, "b", 2) );

    List expectedList = Arrays.asList( "a", 1, "b", 2);

    Objects.squashNulls( actual );

    Diffy.Result result = diffy.diff( expectedList, actual );
    if (!result.isEmpty()) {
        Assert.fail( "Failed.\nhere is a diff:\nexpected: " + JsonUtils.toJsonString( result.expected ) + "\n  actual: " + JsonUtils.toJsonString( result.actual ) );
    }
}