You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apex.apache.org by chinmaykolhatkar <gi...@git.apache.org> on 2016/02/03 08:59:54 UTC

[GitHub] incubator-apex-malhar pull request: MLHR-1962 json parser enhancem...

Github user chinmaykolhatkar commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/163#discussion_r51689008
  
    --- Diff: contrib/src/test/java/com/datatorrent/contrib/parser/JsonParserTest.java ---
    @@ -0,0 +1,414 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *   http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package com.datatorrent.contrib.parser;
    +
    +import java.util.Date;
    +import java.util.List;
    +import java.util.Map;
    +
    +import org.codehaus.jettison.json.JSONException;
    +import org.codehaus.jettison.json.JSONObject;
    +import org.junit.Assert;
    +import org.junit.Rule;
    +import org.junit.Test;
    +import org.junit.rules.TestWatcher;
    +import org.junit.runner.Description;
    +
    +import com.fasterxml.jackson.annotation.JsonFormat;
    +
    +import com.datatorrent.lib.appdata.schemas.SchemaUtils;
    +import com.datatorrent.lib.testbench.CollectorTestSink;
    +import com.datatorrent.lib.util.KeyValPair;
    +
    +public class JsonParserTest
    +{
    +  private static final String filename = "json-parser-schema.json";
    +  CollectorTestSink<Object> error = new CollectorTestSink<Object>();
    +  CollectorTestSink<Object> objectPort = new CollectorTestSink<Object>();
    +  CollectorTestSink<Object> pojoPort = new CollectorTestSink<Object>();
    +  JsonParser parser = new JsonParser();
    +
    +  @Rule
    +  public Watcher watcher = new Watcher();
    +
    +  public class Watcher extends TestWatcher
    +  {
    +    @Override
    +    protected void starting(Description description)
    +    {
    +      super.starting(description);
    +      parser.err.setSink(error);
    +      parser.parsedOutput.setSink(objectPort);
    +      parser.out.setSink(pojoPort);
    +      parser.setClazz(Product.class);
    +      parser.setJsonSchema(SchemaUtils.jarResourceFileToString(filename));
    +      parser.setup(null);
    +    }
    +
    +    @Override
    +    protected void finished(Description description)
    +    {
    +      super.finished(description);
    +      error.clear();
    +      objectPort.clear();
    +      pojoPort.clear();
    +      parser.teardown();
    +    }
    +  }
    +
    +  @Test
    +  public void testValidInput() throws JSONException
    +  {
    +    String tuple = "{" + "\"id\": 2," + "\"name\": \"An ice sculpture\"," + "\"price\": 1,"
    +        + "\"tags\": [\"cold\", \"ice\"]," + "\"dimensions\": {" + "\"length\": 7.0," + "\"width\" : 8.0,"
    +        + "\"height\": 9.5" + "}," + "\"warehouseLocation\": {" + "\"latitude\": -78.75," + "\"longitude\": 20.4"
    +        + "}," + "\"dateOfManufacture\": \"2013/09/29\"," + "\"dateOfExpiry\": \"2013\"" + "}";
    +    parser.beginWindow(0);
    +    parser.in.process(tuple.getBytes());
    +    parser.endWindow();
    +    Assert.assertEquals(1, objectPort.collectedTuples.size());
    +    Assert.assertEquals(1, pojoPort.collectedTuples.size());
    +    Assert.assertEquals(0, error.collectedTuples.size());
    +    Object obj = pojoPort.collectedTuples.get(0);
    +    Assert.assertNotNull(obj);
    +    Assert.assertEquals(Product.class, obj.getClass());
    +    Product pojo = (Product)obj;
    +    JSONObject jsonObject = (JSONObject)objectPort.collectedTuples.get(0);
    +    Assert.assertEquals(2, jsonObject.getInt("id"));
    +    Assert.assertEquals(1, jsonObject.getInt("price"));
    +    Assert.assertEquals("An ice sculpture", jsonObject.get("name"));
    +    Assert.assertEquals(7.0, jsonObject.getJSONObject("dimensions").getDouble("length"), 0);
    +    Assert.assertEquals(2, pojo.getId());
    +    Assert.assertEquals(1, pojo.getPrice());
    +    Assert.assertEquals("An ice sculpture", pojo.getName());
    +    Assert.assertEquals(7.0, (double)pojo.getDimensions().get("length"), 0);
    +  }
    +
    +  @Test
    +  public void testEmptyInput() throws JSONException
    +  {
    +    parser.setSchema(null);
    +    String tuple = "";
    +    parser.beginWindow(0);
    +    parser.in.process(tuple.getBytes());
    +    parser.endWindow();
    +    Assert.assertEquals(0, objectPort.collectedTuples.size());
    +    Assert.assertEquals(0, pojoPort.collectedTuples.size());
    +    Assert.assertEquals(1, error.collectedTuples.size());
    +  }
    +
    +  @Test
    +  public void testValidInputWithoutSchema() throws JSONException
    +  {
    +    parser.setSchema(null);
    +    String tuple = "{" + "\"id\": 2," + "\"name\": \"An ice sculpture\"," + "\"price\": 1,"
    +        + "\"tags\": [\"cold\", \"ice\"]," + "\"dimensions\": {" + "\"length\": 7.0," + "\"width\" : 8.0,"
    +        + "\"height\": 9.5" + "}," + "\"warehouseLocation\": {" + "\"latitude\": -78.75," + "\"longitude\": 20.4"
    +        + "}," + "\"dateOfManufacture\": \"2013/09/29\"," + "\"dateOfExpiry\": \"2013\"" + "}";
    +    parser.beginWindow(0);
    +    parser.in.process(tuple.getBytes());
    +    parser.endWindow();
    +    Assert.assertEquals(1, objectPort.collectedTuples.size());
    +    Assert.assertEquals(1, pojoPort.collectedTuples.size());
    +    Assert.assertEquals(0, error.collectedTuples.size());
    +    Object obj = pojoPort.collectedTuples.get(0);
    +    Assert.assertNotNull(obj);
    +    Assert.assertEquals(Product.class, obj.getClass());
    +    Product pojo = (Product)obj;
    +    JSONObject jsonObject = (JSONObject)objectPort.collectedTuples.get(0);
    +    Assert.assertEquals(2, jsonObject.getInt("id"));
    +    Assert.assertEquals(1, jsonObject.getInt("price"));
    +    Assert.assertEquals("An ice sculpture", jsonObject.get("name"));
    +    Assert.assertEquals(7.0, jsonObject.getJSONObject("dimensions").getDouble("length"), 0);
    +    Assert.assertEquals(2, pojo.getId());
    +    Assert.assertEquals(1, pojo.getPrice());
    +    Assert.assertEquals("An ice sculpture", pojo.getName());
    +    Assert.assertEquals(7.0, (double)pojo.getDimensions().get("length"), 0);
    +  }
    +
    +  @Test
    +  public void testInvalidPrice() throws JSONException
    +  {
    +    String tuple = "{" + "\"id\": 2," + "\"name\": \"An ice sculpture\"," + "\"price\": -1,"
    +        + "\"tags\": [\"cold\", \"ice\"]," + "\"dimensions\": {" + "\"length\": 7.0," + "\"width\" : 8.0,"
    +        + "\"height\": 9.5" + "}," + "\"warehouseLocation\": {" + "\"latitude\": -78.75," + "\"longitude\": 20.4"
    +        + "}," + "\"dateOfManufacture\": \"2013/09/29\"," + "\"dateOfExpiry\": \"2013\"" + "}";
    +    parser.beginWindow(0);
    +    parser.in.process(tuple.getBytes());
    +    parser.endWindow();
    +    Assert.assertEquals(0, objectPort.collectedTuples.size());
    +    Assert.assertEquals(0, pojoPort.collectedTuples.size());
    +    Assert.assertEquals(1, error.collectedTuples.size());
    +    KeyValPair<String, String> errorKeyValPair = (KeyValPair<String, String>)error.collectedTuples.get(0);
    +    Assert.assertEquals(tuple, errorKeyValPair.getKey());
    +    Assert.assertEquals("\"/price\":\"number is lower than the required minimum\"", errorKeyValPair.getValue());
    +  }
    +
    +  @Test
    +  public void testMultipleViolations() throws JSONException
    +  {
    +    String tuple = "{" + "\"id\": 2," + "\"name\": \"An ice sculpture\"," + "\"price\": -1,"
    +        + "\"tags\": [\"cold\", \"ice\"]," + "\"dimensions\": {" + "\"width\" : 8.0," + "\"height\": 9.5" + "},"
    +        + "\"warehouseLocation\": {" + "\"latitude\": -78.75," + "\"longitude\": 20.4" + "},"
    +        + "\"dateOfManufacture\": \"2013/09/29\"," + "\"dateOfExpiry\": \"2013\"" + "}";
    +    parser.beginWindow(0);
    +    parser.in.process(tuple.getBytes());
    +    parser.endWindow();
    +    Assert.assertEquals(0, objectPort.collectedTuples.size());
    +    Assert.assertEquals(0, pojoPort.collectedTuples.size());
    +    Assert.assertEquals(1, error.collectedTuples.size());
    +    KeyValPair<String, String> errorKeyValPair = (KeyValPair<String, String>)error.collectedTuples.get(0);
    +    Assert.assertEquals(tuple, errorKeyValPair.getKey());
    +    Assert.assertEquals(
    +        "\"/dimensions\":\"missing required property(ies)\",\"/price\":\"number is lower than the required minimum\"",
    +        errorKeyValPair.getValue());
    +  }
    +
    +  @Test
    +  public void testJsonSyntaxError() throws JSONException
    +  {
    +    String tuple = "{" + "\"id\": 2," + "\"name\": \"An ice sculpture\"," + "\"price\": -1,"
    +        + "\"tags\": [\"cold\", \"ice\"]," + "\"dimensions\": {" + "\"width\" : 8.0," + "\"height\": 9.5" + "}"
    +        + "\"warehouseLocation\": {" + "\"latitude\": -78.75," + "\"longitude\": 20.4" + "},"
    +        + "\"dateOfManufacture\": \"2013/09/29\"," + "\"dateOfExpiry\": \"2013\"" + "}";
    +    parser.beginWindow(0);
    +    parser.in.process(tuple.getBytes());
    +    parser.endWindow();
    +    Assert.assertEquals(0, objectPort.collectedTuples.size());
    +    Assert.assertEquals(0, pojoPort.collectedTuples.size());
    +    Assert.assertEquals(1, error.collectedTuples.size());
    +    KeyValPair<String, String> errorKeyValPair = (KeyValPair<String, String>)error.collectedTuples.get(0);
    +    Assert.assertEquals(tuple, errorKeyValPair.getKey());
    +  }
    +
    +  @Test
    +  public void testValidInputPojoPortNotConnected() throws JSONException
    +  {
    +    parser.out.setSink(null);
    +    String tuple = "{" + "\"id\": 2," + "\"name\": \"An ice sculpture\"," + "\"price\": 1,"
    +        + "\"tags\": [\"cold\", \"ice\"]," + "\"dimensions\": {" + "\"length\": 7.0," + "\"width\" : 8.0,"
    +        + "\"height\": 9.5" + "}," + "\"warehouseLocation\": {" + "\"latitude\": -78.75," + "\"longitude\": 20.4"
    +        + "}," + "\"dateOfManufacture\": \"2013/09/29\"," + "\"dateOfExpiry\": \"2013\"" + "}";
    +    parser.beginWindow(0);
    +    parser.in.process(tuple.getBytes());
    +    parser.endWindow();
    +    Assert.assertEquals(1, objectPort.collectedTuples.size());
    +    Assert.assertEquals(0, pojoPort.collectedTuples.size());
    +    Assert.assertEquals(0, error.collectedTuples.size());
    +    JSONObject jsonObject = (JSONObject)objectPort.collectedTuples.get(0);
    +    Assert.assertEquals(2, jsonObject.getInt("id"));
    +    Assert.assertEquals(1, jsonObject.getInt("price"));
    +    Assert.assertEquals("An ice sculpture", jsonObject.get("name"));
    +    Assert.assertEquals(7.0, jsonObject.getJSONObject("dimensions").getDouble("length"), 0);
    +  }
    +
    +  @Test
    +  public void testValidInputParsedOutputPortNotConnected() throws JSONException
    +  {
    +    parser.parsedOutput.setSink(null);
    +    String tuple = "{" + "\"id\": 2," + "\"name\": \"An ice sculpture\"," + "\"price\": 1,"
    +        + "\"tags\": [\"cold\", \"ice\"]," + "\"dimensions\": {" + "\"length\": 7.0," + "\"width\" : 8.0,"
    +        + "\"height\": 9.5" + "}," + "\"warehouseLocation\": {" + "\"latitude\": -78.75," + "\"longitude\": 20.4"
    +        + "}," + "\"dateOfManufacture\": \"2013/09/29\"," + "\"dateOfExpiry\": \"2013\"" + "}";
    +    parser.beginWindow(0);
    +    parser.in.process(tuple.getBytes());
    +    parser.endWindow();
    +    Assert.assertEquals(0, objectPort.collectedTuples.size());
    +    Assert.assertEquals(1, pojoPort.collectedTuples.size());
    +    Assert.assertEquals(0, error.collectedTuples.size());
    +    Object obj = pojoPort.collectedTuples.get(0);
    +    Assert.assertNotNull(obj);
    +    Assert.assertEquals(Product.class, obj.getClass());
    +    Product pojo = (Product)obj;
    +    Assert.assertEquals(2, pojo.getId());
    +    Assert.assertEquals(1, pojo.getPrice());
    +    Assert.assertEquals("An ice sculpture", pojo.getName());
    +    Assert.assertEquals(7.0, (double)pojo.getDimensions().get("length"), 0);
    +  }
    +
    +  @Test
    +  public void TestParserValidInputMetricVerification()
    --- End diff --
    
    Please keep the same format for method name. All the unit tests should start from test* and not Test*


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---