[ 
https://issues.apache.org/jira/browse/APEXMALHAR-2015?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15201417#comment-15201417
 ] 

ASF GitHub Bot commented on APEXMALHAR-2015:
--------------------------------------------

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

    
https://github.com/apache/incubator-apex-malhar/pull/217#discussion_r56650591
  
    --- Diff: 
library/src/test/java/com/datatorrent/lib/projection/ProjectionTest.java ---
    @@ -0,0 +1,264 @@
    +/**
    + * 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.lib.projection;
    +
    +import java.lang.reflect.Field;
    +
    +import org.junit.AfterClass;
    +import org.junit.Assert;
    +import org.junit.BeforeClass;
    +import org.junit.Test;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +/**
    + * Tests for ProjectionOperator
    + */
    +
    +public class ProjectionTest
    +{
    +  private static final Logger logger = 
LoggerFactory.getLogger(ProjectionTest.class);
    +
    +  public static class DummyPOJO
    +  {
    +    private long projected;
    +    private long remainder;
    +
    +    public long getProjected()
    +    {
    +      return projected;
    +    }
    +
    +    public void setProjected(long _projected)
    +    {
    +      projected = _projected;
    +    }
    +
    +    public long getRemainder()
    +    {
    +      return remainder;
    +    }
    +
    +    public void setRemainder(long _remainder)
    +    {
    +      remainder = _remainder;
    +    }
    +  }
    +
    +  public static class ProjectedPOJO
    +  {
    +    private long projected;
    +
    +    public long getProjected()
    +    {
    +      return projected;
    +    }
    +
    +    public void setProjected(long _projected)
    +    {
    +      projected = _projected;
    +    }
    +  }
    +
    +  public static class RemainderPOJO
    +  {
    +    private long remainder;
    +
    +    public long getRemainder()
    +    {
    +      return remainder;
    +    }
    +
    +    public void setRemainder(long _remainder)
    +    {
    +      remainder = _remainder;
    +    }
    +  }
    +
    +  private static DummyProjection projection;
    +  private static DummyPOJO data;
    +
    +  @Test
    +  public void testProjectionRemainder()
    +  {
    +    logger.debug("start round 0");
    +    projection.beginWindow(0);
    +
    +    data.setProjected(1234);
    +    data.setRemainder(6789);
    +
    +    Object p = null;
    +    try {
    +      p = projection.getProjectedObject(data);
    +    } catch (IllegalAccessException e) {
    +      Assert.assertTrue(e instanceof IllegalAccessException);
    +    }
    +    logger.debug("projected class {}", p.getClass());
    +
    +    for (Field f: p.getClass().getDeclaredFields()) {
    +      f.setAccessible(true);
    +      try {
    +        logger.debug("projected field: {} type: {} val: {}", f.getName(), 
f.getType(), f.get(p));
    +      } catch (IllegalAccessException e) {
    +        logger.info("could not access value of field: {} type: {}", 
f.getName(), f.getType());
    +      }
    +    }
    +
    +    Object r = null;
    +    try {
    +      r = projection.getRemainderObject(data);
    +    } catch (IllegalAccessException e) {
    +      Assert.assertTrue(e instanceof IllegalAccessException);
    +    }
    +    logger.debug("remainder class {}", r.getClass());
    +
    +    for (Field f: r.getClass().getDeclaredFields()) {
    +      f.setAccessible(true);
    +      try {
    +        logger.debug("projected field: {} type: {} val: {}", f.getName(), 
f.getType(), f.get(r));
    +      } catch (IllegalAccessException e) {
    +        logger.info("could not access value of field: {} type: {}", 
f.getName(), f.getType());
    +      }
    +    }
    +
    +    try {
    +      Assert.assertEquals("projected field value", 1234, 
p.getClass().getDeclaredField("projected").get(p));
    +      Assert.assertEquals("remainder field value", 6789, 
r.getClass().getDeclaredField("remainder").get(r));
    +    } catch (NoSuchFieldException e) {
    +      Assert.assertTrue(e instanceof NoSuchFieldException);
    +    } catch (IllegalAccessException e) {
    +      Assert.assertTrue(e instanceof IllegalAccessException);
    +    }
    +
    +    projection.endWindow();
    +    logger.debug("end round 0");
    +  }
    +
    +  @Test
    +  public void testProjected()
    +  {
    +    logger.debug("start round 0");
    +    projection.beginWindow(0);
    +
    +    data.setProjected(2345);
    +    data.setRemainder(5678);
    +
    +    Object p = null;
    +    try {
    +      p = projection.getProjectedObject(data);
    +    } catch (IllegalAccessException e) {
    +      Assert.assertTrue(e instanceof IllegalAccessException);
    +    }
    +    logger.debug("projected class {}", p.getClass());
    +
    +    for (Field f: p.getClass().getDeclaredFields()) {
    +      f.setAccessible(true);
    +      try {
    +        logger.debug("projected field: {} type: {} val: {}", f.getName(), 
f.getType(), f.get(p));
    +      } catch (IllegalAccessException e) {
    +        logger.info("could not access value of field: {} type: {}", 
f.getName(), f.getType());
    +      }
    +    }
    +
    +    try {
    +      Assert.assertEquals("projected field value", 2345, 
p.getClass().getDeclaredField("projected").get(p));
    +    } catch (NoSuchFieldException e) {
    +      Assert.assertTrue(e instanceof NoSuchFieldException);
    +    } catch (IllegalAccessException e) {
    +      Assert.assertTrue(e instanceof IllegalAccessException);
    +    }
    +
    +    projection.endWindow();
    +    logger.debug("end round 0");
    +  }
    +
    +  @Test
    +  public void testRemainder()
    +  {
    +    logger.debug("start round 0");
    +    projection.beginWindow(0);
    +
    +    data.setProjected(9876);
    +    data.setRemainder(4321);
    +
    +    Object r = null;
    +    try {
    +      r = projection.getRemainderObject(data);
    +    } catch (IllegalAccessException e) {
    +      Assert.assertTrue(e instanceof IllegalAccessException);
    +    }
    +    logger.debug("remainder class {}", r.getClass());
    +
    +    for (Field f: r.getClass().getDeclaredFields()) {
    +      f.setAccessible(true);
    +      try {
    +        logger.debug("projected field: {} type: {} val: {}", f.getName(), 
f.getType(), f.get(r));
    +      } catch (IllegalAccessException e) {
    +        logger.info("could not access value of field: {} type: {}", 
f.getName(), f.getType());
    +      }
    +    }
    +
    +    try {
    +      Assert.assertEquals("remainder field value", 4321, 
r.getClass().getDeclaredField("remainder").get(r));
    +    } catch (NoSuchFieldException e) {
    +      Assert.assertTrue(e instanceof NoSuchFieldException);
    +    } catch (IllegalAccessException e) {
    +      Assert.assertTrue(e instanceof IllegalAccessException);
    +    }
    +
    +    projection.endWindow();
    +    logger.debug("end round 0");
    +  }
    +
    +  @Test
    +  public void testProjection()
    +  {
    +    logger.debug("start round 0");
    +    projection.beginWindow(0);
    +
    +    projection.in.process(data);
    +    Assert.assertEquals("projected tuples", 1, projection.projectedTuples);
    --- End diff --
    
    Can we add some more scenarios for this test.


> Projection Operator
> -------------------
>
>                 Key: APEXMALHAR-2015
>                 URL: https://issues.apache.org/jira/browse/APEXMALHAR-2015
>             Project: Apache Apex Malhar
>          Issue Type: New Feature
>            Reporter: Pradeep A Dalvi
>
> Projection Operator will allow apex users to project (select/drop) certain 
> fields from the incoming tuples. This operation might be done unconditionally 
> or based on certain condition.
> Use case:
> -------------
> Not all fields of tuples are of interest for the downstream operators. In 
> such cases, one may want project selective fields to downstream. Also one may 
> want to drop few fields, instead of selecting many.
> In certain scenarios, one may want to project certain fields based on given 
> condition or expression.
> Functionality:
> -----------------
> 1. Projection operator shall receive POJO as input tuple and emit 2 POJOs on 
> separate output ports i.e. selected and dropped. Selected output port shall 
> emit POJO with selected fields and dropped output shall emit POJO of dropped 
> fields.
> 2. Operator needs select or drop fields as input params. This shall be 
> specified using comma separated list of fields.
> 3. Operator shall emit POJO only on connected output ports. In another words, 
> if dropped output port is not connected, it shall not even try to generate 
> POJOs with dropped fields.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to