use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor in project knox by apache.
the class JsonFilterReaderTest method testBuffered.
// @Test
// public void testJsonPathObject() throws IOException {
// InputStream stream = TestUtils.getResourceStream( this.getClass(), "complex.json" );
// String input = IOUtils.toString( stream, Charset.forName( "UTF-8" ) );
//
// Object o;
//
// o = JsonPath.read( "$", input, JsonNode.class );
// assertThat( o, instanceOf( ObjectNode.class ) );
// assertThat( o.toString(), startsWith( "{" ) );
// System.out.println( o.getClass() + "=" + o );
//
// o = JsonPath.read( "$['test-str']", input, JsonNode.class );
// System.out.println( o.getClass() + "=" + o );
//
// o = JsonPath.read( "$['test-obj-multi']", input, JsonNode.class );
// System.out.println( o.getClass() + "=" + o );
//
// o = JsonPath.read( "$['val']", (JsonNode)o, JsonNode.class );
// System.out.println( o.getClass() + "=" + o );
//
// JsonPath p = JsonPath.compile( "$['test-obj-multi']['val']" );
// o = JsonPath.read( "$['test-obj-multi']['val']", input, JsonNode.class );
// JsonNode pp = ((JsonNode)o).findParent("val");
// System.out.println( "$['test-obj-multi']['val']=" + o.getClass() + "=" + o );
//
// }
//
// @Test
// public void testJsonPathArray() throws IOException {
// InputStream stream = TestUtils.getResourceStream( this.getClass(), "array.json" );
// String input = IOUtils.toString( stream, Charset.forName( "UTF-8" ) );
//
// Object o;
//
// o = JsonPath.read( "$", input, JsonNode.class );
// System.out.println( o.getClass() + "=" + o );
//
// o = JsonPath.read( "$[0]", input, JsonNode.class );
// System.out.println( o.getClass() + "=" + o );
//
// o = JsonPath.read( "$[*]", input, JsonNode.class );
// System.out.println( "$[*]=" + o.getClass() + "=" + o );
//
// o = JsonPath.read( "$['obj1-fld1']", (JsonNode)o, JsonNode.class );
// System.out.println( o.getClass() + "=" + o );
//
// o = JsonPath.read( "$[0]['obj1-fld1']", input, JsonNode.class );
// System.out.println( o.getClass() + "=" + o );
//
// }
@Test
public void testBuffered() 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");
UrlRewriteFilterBufferDescriptor bufferConfig = contentConfig.addBuffer("$");
UrlRewriteFilterApplyDescriptor applyConfig = bufferConfig.addApply("$['name<test-str>']", "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<test-str>", is("value:test-rule<text>"));
}
use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor in project knox by apache.
the class JsonFilterReaderTest method testBufferedMultiApply.
@Test
public void testBufferedMultiApply() 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>");
UrlRewriteFilterApplyDescriptor applyConfig = bufferConfig.addApply("$.*.name<property>.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>"));
}
use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor in project knox by apache.
the class JsonFilterReaderTest method testUnscopedStreaming.
@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>"));
}
use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor in project knox by apache.
the class JsonFilterReaderTest method testInvalidConfigShouldThrowException.
@Test
public void testInvalidConfigShouldThrowException() throws Exception {
String input = "{\"test-name\":\"test-value\"}";
// System.out.println( "INPUT=" + input );
UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create();
UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter("filter-1");
UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent("*/json");
contentConfig.addApply("/root/@url", "test-rule");
try {
JsonFilterReader filter = new TestJsonFilterReader(new StringReader(input), contentConfig);
IOUtils.toString(filter);
fail("Should have thrown an IllegalArgumentException.");
} catch (IOException e) {
fail("Should have thrown an IllegalArgumentException.");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), containsString("/root/@url"));
}
}
use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor in project knox by apache.
the class JsonFilterReaderTest method testNamesWithDots.
@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>"));
}
Aggregations