Exemplo n.º 1
0
  @Test
  public void testNamesWithDots() throws IOException {
    InputStream stream = TestUtils.getResourceStream(this.getClass(), "dotted-field-name.json");
    String input = IOUtils.toString(stream, Charset.forName("UTF-8"));

    // System.out.println( "INPUT=" + input );

    UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter("test-filter");
    UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent("application/json");
    // NOTE: The field names are rewritten first so the values rules need to match the rewritten
    // name.
    contentConfig.addApply("$.name<testField>", "test-rule");
    contentConfig.addApply("$.name<test_field>", "test-rule");
    contentConfig.addApply("$.name<test-field>", "test-rule");
    contentConfig.addApply("$['name<test.field>']", "test-rule");

    JsonFilterReader filter = new TestJsonFilterReader(new StringReader(input), contentConfig);
    String output = IOUtils.toString(filter);

    // System.out.println( "OUTPUT=" + output );

    JsonAssert.with(output)
        .assertThat("$['name<testField>']", is("value:test-rule<testField value>"));
    JsonAssert.with(output)
        .assertThat("$['name<test_field>']", is("value:test-rule<test_field value>"));
    JsonAssert.with(output)
        .assertThat("$['name<test-field>']", is("value:test-rule<test-field value>"));
    JsonAssert.with(output)
        .assertThat("$['name<test.field>']", is("value:test-rule<test.field value>"));
  }
Exemplo n.º 2
0
  @Test
  public void shouldBeSerializable() {
    Mapping mapping = new Mapping();
    mapping.setProcessorMethod("setChannelRecording");
    mapping.setProcessorMethodArguments(new LinkedList<>());
    mapping
        .getProcessorMethodArguments()
        .add(new MappingMethodArgument("channelId", Long.class.getName()));
    mapping
        .getProcessorMethodArguments()
        .add(new MappingMethodArgument("recording", Boolean.class.getName()));
    mapping.setTriggerValueMap(new HashMap<>());
    mapping.getTriggerValueMap().put("command", 1);
    mapping.getTriggerValueMap().put("channel", 2);
    mapping.getTriggerValueMap().put("data1", 3);
    mapping.getTriggerValueMap().put("data2", 4);
    mapping.setValueExpressionMap(new HashMap<>());
    mapping.getValueExpressionMap().put("channelId", "#{data1} * 10");
    mapping.getValueExpressionMap().put("recording", "#{data2} == 4");

    String json = new Gson().toJson(mapping);

    JsonAssert.with(json)
        .assertThat("processorMethod", is("setChannelRecording"))
        .assertThat("processorMethodArguments[0].name", is("channelId"))
        .assertThat("processorMethodArguments[0].type", is("java.lang.Long"))
        .assertThat("processorMethodArguments[1].name", is("recording"))
        .assertThat("processorMethodArguments[1].type", is("java.lang.Boolean"))
        .assertThat("triggerValueMap.command", is(1))
        .assertThat("triggerValueMap.channel", is(2))
        .assertThat("triggerValueMap.data1", is(3))
        .assertThat("triggerValueMap.data2", is(4))
        .assertThat("valueExpressionMap.channelId", is("#{data1} * 10"))
        .assertThat("valueExpressionMap.recording", is("#{data2} == 4"));
  }
Exemplo n.º 3
0
  @Test
  public void testRootArray() throws Exception {
    String inputJson = "[\"test-value-1\",\"test-value-2\",\"test-value-3\"]";
    StringReader inputReader = new StringReader(inputJson);
    JsonFilterReader filterReader = new TestJsonFilterReader(inputReader, null);
    String outputJson = new String(IOUtils.toCharArray(filterReader));
    // System.out.println( "JSON=" + outputJson );
    JsonAssert.with(outputJson).assertThat("$.[0]", is("value:null<test-value-1>"));
    JsonAssert.with(outputJson).assertThat("$.[1]", is("value:null<test-value-2>"));
    JsonAssert.with(outputJson).assertThat("$.[2]", is("value:null<test-value-3>"));

    inputJson = "[777,42]";
    inputReader = new StringReader(inputJson);
    filterReader = new TestJsonFilterReader(inputReader, null);
    outputJson = new String(IOUtils.toCharArray(filterReader));
    // System.out.println( "JSON=" + outputJson );
    JsonAssert.with(outputJson).assertThat("$.[0]", is(777));
    JsonAssert.with(outputJson).assertThat("$.[1]", is(42));
  }
Exemplo n.º 4
0
  @Test
  public void testSimple() throws IOException {
    String inputJson = "{ \"test-name\" : \"test-value\" }";
    StringReader inputReader = new StringReader(inputJson);
    JsonFilterReader filterReader = new TestJsonFilterReader(inputReader, null);
    String outputJson = new String(IOUtils.toCharArray(filterReader));
    // System.out.println( "JSON=" + outputJson );

    JsonAssert.with(outputJson).assertThat("name<test-name>", is("value:null<test-value>"));
  }
Exemplo n.º 5
0
  @Test
  public void testUnscopedStreaming() throws IOException {
    InputStream stream = TestUtils.getResourceStream(this.getClass(), "simple-values.json");
    String input = IOUtils.toString(stream, Charset.forName("UTF-8"));

    // System.out.println( "INPUT=" + input );

    UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter("filter=1");
    UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent("text/json");
    UrlRewriteFilterApplyDescriptor applyConfig =
        contentConfig.addApply("$['test-str']", "test-rule");

    JsonFilterReader filter = new TestJsonFilterReader(new StringReader(input), contentConfig);
    String output = IOUtils.toString(filter);

    // System.out.println( "OUTPUT=" + output );

    JsonAssert.with(output).assertThat("name<test-str>", is("value:null<text>"));
  }
Exemplo n.º 6
0
  @Test
  public void testBufferedApply() throws IOException {
    InputStream stream = TestUtils.getResourceStream(this.getClass(), "properties.json");
    String input = IOUtils.toString(stream, Charset.forName("UTF-8"));

    // System.out.println( "INPUT=" + input );

    UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter("filter-1");
    UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent("text/json");
    UrlRewriteFilterBufferDescriptor bufferConfig =
        contentConfig.addBuffer("$.name<properties>.*.name<property>");
    UrlRewriteFilterApplyDescriptor applyConfig =
        bufferConfig.addApply("$.name<property-value>", "test-rule");

    // UrlRewriteRulesDescriptorFactory.store( rulesConfig, "xml", new PrintWriter( System.out ) );

    JsonFilterReader filter = new TestJsonFilterReader(new StringReader(input), contentConfig);
    String output = IOUtils.toString(filter);

    // System.out.println( "OUTPUT=" + output );

    JsonAssert.with(output)
        .assertThat("name<properties>[0].name<property>.name<property-name>", is("test-name-1"));
    JsonAssert.with(output)
        .assertThat(
            "name<properties>[0].name<property>.name<property-value>",
            is("value:test-rule<test-value-1>"));
    JsonAssert.with(output)
        .assertThat("name<properties>[1].name<property>.name<property-name>", is("test-name-2"));
    JsonAssert.with(output)
        .assertThat(
            "name<properties>[1].name<property>.name<property-value>",
            is("value:test-rule<test-value-2>"));
    JsonAssert.with(output)
        .assertThat("name<properties>[2].name<property>.name<property-name>", is("test-name-3"));
    JsonAssert.with(output)
        .assertThat(
            "name<properties>[2].name<property>.name<property-value>",
            is("value:test-rule<test-value-3>"));
  }