@Test public void testJSONToPOJO() { Ad pojo = new Ad(); pojo.adId = 123; pojo.campaignId = 234876274; pojo.description = "sports"; pojo.sizes = Lists.newArrayList("200x350", "600x800"); pojo.startDate = new DateTime() .withDate(2016, 1, 1) .withMillisOfDay(0) .withZoneRetainFields(DateTimeZone.UTC) .toDate(); pojo.endDate = new DateTime() .withDate(2016, 2, 1) .withMillisOfDay(0) .withZoneRetainFields(DateTimeZone.UTC) .toDate(); operator.in.put(pojo); Assert.assertEquals(1, validDataSink.collectedTuples.size()); Assert.assertEquals(0, invalidDataSink.collectedTuples.size()); String expectedJSONString = "{\"adId\":123,\"campaignId\":234876274,\"sizes\":[\"200x350\",\"600x800\"],\"startDate\":\"Fri, 1 Jan 2016 00:00:00\",\"endDate\":\"01-Feb-2016\",\"desc\":\"sports\"}"; Assert.assertEquals(expectedJSONString, validDataSink.collectedTuples.get(0)); Assert.assertEquals(1, operator.getIncomingTuplesCount()); Assert.assertEquals(1, operator.getEmittedObjectCount()); Assert.assertEquals(0, operator.getErrorTupleCount()); }
@Override protected void finished(Description description) { operator.endWindow(); operator.teardown(); deleteDirectory(); super.finished(description); }
@Test public void testJSONToPOJONullPOJO() { operator.in.put(null); Assert.assertEquals(0, validDataSink.collectedTuples.size()); Assert.assertEquals(1, invalidDataSink.collectedTuples.size()); Assert.assertEquals(1, operator.getIncomingTuplesCount()); Assert.assertEquals(0, operator.getEmittedObjectCount()); Assert.assertEquals(1, operator.getErrorTupleCount()); }
@Override public String getStringValue() { if (this.object != null) { return JsonFormatter.format(StringEscapeUtils.unescapeJavaScript(this.object.toString())); } else if (this.array != null) { return JsonFormatter.format(StringEscapeUtils.unescapeJavaScript(this.array.toString())); } else { return null; } }
@Test public void testJSONToPOJOEmptyPOJO() { Ad pojo = new Ad(); operator.in.put(pojo); Assert.assertEquals(1, validDataSink.collectedTuples.size()); Assert.assertEquals(0, invalidDataSink.collectedTuples.size()); String expectedJSONString = "{\"adId\":0,\"campaignId\":0,\"sizes\":null,\"startDate\":null,\"endDate\":null,\"desc\":null}"; Assert.assertEquals(expectedJSONString, validDataSink.collectedTuples.get(0)); Assert.assertEquals(1, operator.getIncomingTuplesCount()); Assert.assertEquals(1, operator.getEmittedObjectCount()); Assert.assertEquals(0, operator.getErrorTupleCount()); }
@Override protected void starting(Description descriptor) { super.starting(descriptor); deleteDirectory(); operator = new JsonFormatter(); validDataSink = new CollectorTestSink<Object>(); invalidDataSink = new CollectorTestSink<String>(); TestUtils.setSink(operator.out, validDataSink); TestUtils.setSink(operator.err, invalidDataSink); operator.setup(null); operator.beginWindow(0); }
@Test public void testJSONToPOJONullFields() { Ad pojo = new Ad(); pojo.adId = 123; pojo.campaignId = 234876274; pojo.description = "sports"; pojo.sizes = null; pojo.startDate = null; pojo.endDate = null; operator.in.put(pojo); Assert.assertEquals(1, validDataSink.collectedTuples.size()); Assert.assertEquals(0, invalidDataSink.collectedTuples.size()); String expectedJSONString = "{\"adId\":123,\"campaignId\":234876274,\"sizes\":null,\"startDate\":null,\"endDate\":null,\"desc\":\"sports\"}"; Assert.assertEquals(expectedJSONString, validDataSink.collectedTuples.get(0)); Assert.assertEquals(1, operator.getIncomingTuplesCount()); Assert.assertEquals(1, operator.getEmittedObjectCount()); Assert.assertEquals(0, operator.getErrorTupleCount()); }
@Test public void testJSONToPOJONoFieldPOJO() { operator.endWindow(); operator.teardown(); operator.setup(null); operator.beginWindow(1); TestPojo o = new TestPojo(); operator.in.put(o); Assert.assertEquals(0, validDataSink.collectedTuples.size()); Assert.assertEquals(1, invalidDataSink.collectedTuples.size()); Assert.assertEquals(o, invalidDataSink.collectedTuples.get(0)); Assert.assertEquals(1, operator.getIncomingTuplesCount()); Assert.assertEquals(0, operator.getEmittedObjectCount()); Assert.assertEquals(1, operator.getErrorTupleCount()); }