apex-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From shubham-pathak22 <...@git.apache.org>
Subject [GitHub] incubator-apex-malhar pull request: MLHR-1962 json parser enhancem...
Date Thu, 14 Jan 2016 04:50:50 GMT
Github user shubham-pathak22 commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/163#discussion_r49687536
  
    --- Diff: contrib/src/test/java/com/datatorrent/contrib/parser/JsonParserTest.java ---
    @@ -0,0 +1,356 @@
    +/**
    + * 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();
    +    }
    +  }
    +
    +  @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.in.process(tuple);
    +    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 testValidInputWithoutSchema() throws JSONException
    +  {
    +    parser.setJsonSchema(null);
    +    parser.setup(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.in.process(tuple);
    +    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.in.process(tuple);
    +    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.in.process(tuple);
    +    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.in.process(tuple);
    +    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.in.process(tuple);
    +    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.in.process(tuple);
    +    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()
    +  {
    +    Assert.assertEquals(0, parser.parsedOutputCount);
    +    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.in.process(tuple);
    +    Assert.assertEquals(1, parser.parsedOutputCount);
    +  }
    +
    +  @Test
    +  public void TestParserInvalidInputMetricVerification()
    +  {
    +    Assert.assertEquals(0, parser.parsedOutputCount);
    +    String tuple = "{" + "\"id\": 2" + "}";
    +    parser.in.process(tuple);
    +    Assert.assertEquals(0, parser.parsedOutputCount);
    +  }
    --- End diff --
    
    Sure @gauravgopi123 . Because beingWindow and endWindow did not have any functionality
except resetting autometric which actually defaults to 0, i did not explicitly call them.
But i agree with you, the test must reflect function calls in the same order as they are called
in application. I have added it now. 


---
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.
---

Mime
View raw message