JoltTransformer { public static Future<JsonObject> transform(String jsonInput, JoltSpec spec) { Future<JsonObject> future = Future.future(); try { Chainr chainr = spec.getChainr(); Object inputJSON = JsonUtils.jsonToObject(jsonInput); Object transformedOutput = chainr.transform(inputJSON); future.complete(new JsonObject(JsonUtils.toJsonString(transformedOutput))); } catch (Exception ex){ future.fail(ex); } return future; }  static Future<JsonObject> transform(String jsonInput, JoltSpec spec);  }
@Test public void testTransformInputNull(TestContext context) { JoltTransformer.transform(null, identitySpec).setHandler(transform -> { context.assertFalse(transform.succeeded()); context.assertNull(transform.result()); context.assertEquals(NullPointerException.class, transform.cause().getClass()); }); }
@Test public void testTransformInputEmptyString(TestContext context) { JoltTransformer.transform("", identitySpec).setHandler(transform -> { context.assertFalse(transform.succeeded()); context.assertNull(transform.result()); context.assertEquals(JsonUnmarshalException.class, transform.cause().getClass()); }); }
@Test public void testTransformInputNotJsonString(TestContext context) { JoltTransformer.transform("abcd", identitySpec).setHandler(transform -> { context.assertFalse(transform.succeeded()); context.assertNull(transform.result()); context.assertEquals(JsonUnmarshalException.class, transform.cause().getClass()); }); }
@Test public void testTransformInputEmptyJsonObject(TestContext context) { String input = "{}"; context.assertEquals(new JsonObject(input), JoltTransformer.transform(input, identitySpec).result(), "the output is expected to be equal to the input"); }
@Test public void testTransformInputSpecNull(TestContext context) { JoltTransformer.transform("{}", null).setHandler(transform -> { context.assertFalse(transform.succeeded()); context.assertNull(transform.result()); context.assertEquals(NullPointerException.class, transform.cause().getClass()); }); }
@Test public void testTransformInputSpecInvalid(TestContext context) { JoltTransformer.transform("{}", new JoltSpec(null)).setHandler(transform -> { context.assertFalse(transform.succeeded()); context.assertNull(transform.result()); context.assertEquals(NullPointerException.class, transform.cause().getClass()); }); }
@Test public void testTransformIdentityFunction(TestContext context) { String input = "{\n" + " \"rating\": {\n" + " \"primary\": {\n" + " \"value\": 3\n" + " }\n" + " }\n" + "}"; context.assertEquals(new JsonObject(input), JoltTransformer.transform(input, identitySpec).result(), "the output is expected to be equal to the input"); }
@Test public void testTransformCopyToArray(TestContext context) { String input = "{\n" + " \"rating\": {\n" + " \"primary\": {\n" + " \"value\": 3\n" + " }\n" + " }\n" + "}"; JsonObject output = buildRecordsOutput(new JsonObject(input)); context.assertEquals(output, JoltTransformer.transform(input, copyToArraySpec).result()); String anotherInput = "{\"test\": 234, \"foo\": \"bar\"}"; JsonObject anotherOutput = buildRecordsOutput(new JsonObject(anotherInput)); context.assertEquals(anotherOutput, JoltTransformer.transform(anotherInput, copyToArraySpec).result()); JsonObject complexOutput = buildRecordsOutput(new JsonObject(COMPLEX_INPUT_JSON)); context.assertEquals(complexOutput, JoltTransformer.transform(COMPLEX_INPUT_JSON, copyToArraySpec).result()); }
@Test public void testTransformWithMetadata(TestContext context) { JoltTransformer.transform(TRANSFORM_WITH_METADATA_INPUT_JSON, transformWithMetadataSpec).setHandler(transform -> { context.assertTrue(transform.succeeded()); context.assertNotNull(transform.result()); context.assertEquals(new JsonObject(TRANSFORM_WITH_METADATA_EXPECTED_OUTPUT_JSON), transform.result()); }); }