Repository: metron
Updated Branches:
  refs/heads/master ac0e05f01 -> 353bc8b3b


METRON-632: Added validation of "shew.enrichmentType" and "shew.keyColumns" 
closes apache/incubator-metron#732


Project: http://git-wip-us.apache.org/repos/asf/metron/repo
Commit: http://git-wip-us.apache.org/repos/asf/metron/commit/353bc8b3
Tree: http://git-wip-us.apache.org/repos/asf/metron/tree/353bc8b3
Diff: http://git-wip-us.apache.org/repos/asf/metron/diff/353bc8b3

Branch: refs/heads/master
Commit: 353bc8b3be70c5b3da10ae98445521c31ae16568
Parents: ac0e05f
Author: zezutom <zezulato...@gmail.com>
Authored: Mon Oct 2 09:50:20 2017 -0400
Committer: cstella <ceste...@gmail.com>
Committed: Mon Oct 2 09:50:20 2017 -0400

----------------------------------------------------------------------
 .../writer/SimpleHbaseEnrichmentWriter.java     |  44 ++++++-
 .../SimpleHBaseEnrichmentWriterTest.java        | 118 +++++++++++++++++++
 2 files changed, 161 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/metron/blob/353bc8b3/metron-platform/metron-enrichment/src/main/java/org/apache/metron/enrichment/writer/SimpleHbaseEnrichmentWriter.java
----------------------------------------------------------------------
diff --git 
a/metron-platform/metron-enrichment/src/main/java/org/apache/metron/enrichment/writer/SimpleHbaseEnrichmentWriter.java
 
b/metron-platform/metron-enrichment/src/main/java/org/apache/metron/enrichment/writer/SimpleHbaseEnrichmentWriter.java
index ad3b6d7..d0e9f67 100644
--- 
a/metron-platform/metron-enrichment/src/main/java/org/apache/metron/enrichment/writer/SimpleHbaseEnrichmentWriter.java
+++ 
b/metron-platform/metron-enrichment/src/main/java/org/apache/metron/enrichment/writer/SimpleHbaseEnrichmentWriter.java
@@ -124,6 +124,8 @@ public class SimpleHbaseEnrichmentWriter extends 
AbstractWriter implements BulkM
 
   @Override
   public void configure(String sensorName, WriterConfiguration configuration) {
+    validateEnrichmentType(sensorName, configuration);
+    validateKeyColumns(sensorName, configuration);
     String hbaseProviderImpl = 
Configurations.HBASE_PROVIDER.getAndConvert(configuration.getSensorConfig(sensorName),String.class);
     if(hbaseProviderImpl != null) {
       provider = ReflectionUtils.createInstance(hbaseProviderImpl);
@@ -134,6 +136,39 @@ public class SimpleHbaseEnrichmentWriter extends 
AbstractWriter implements BulkM
     LOG.debug("Sensor: '{}': {Provider: '{}', Converter: '{}'}", sensorName, 
getClassName(provider), getClassName(converter));
   }
 
+
+  private void validateEnrichmentType(String sensorName, WriterConfiguration 
configuration) {
+    Map<String, Object> sensorConfig = 
configuration.getSensorConfig(sensorName);
+    Object enrichmentTypeObj = 
Configurations.ENRICHMENT_TYPE.get(sensorConfig);
+    if (enrichmentTypeObj == null) {
+      throw new IllegalArgumentException(String.format("%s must be provided", 
Configurations.ENRICHMENT_TYPE.getKey()));
+    }
+
+    if (!(enrichmentTypeObj instanceof String)) {
+      throw new IllegalArgumentException(String.format("%s must be a string", 
Configurations.ENRICHMENT_TYPE.getKey()));
+    }
+
+    String enrichmentType = enrichmentTypeObj.toString();
+    if (enrichmentType.trim().isEmpty()) {
+      throw new IllegalArgumentException(String.format("%s must not be an 
empty string",
+              Configurations.ENRICHMENT_TYPE.getKey()));
+    }
+  }
+
+  private void validateKeyColumns(String sensorName, WriterConfiguration 
configuration) {
+    Map<String, Object> sensorConfig = 
configuration.getSensorConfig(sensorName);
+    Object keyColumnsObj = Configurations.KEY_COLUMNS.get(sensorConfig);
+
+    try {
+      List<String> keyColumns = getColumns(keyColumnsObj, true);
+      if (keyColumns == null || keyColumns.isEmpty()) {
+        throw new IllegalArgumentException(String.format("%s must be 
provided", Configurations.KEY_COLUMNS.getKey()));
+      }
+    } catch (RuntimeException ex) {
+      throw new IllegalArgumentException(ex.getMessage(), ex);
+    }
+  }
+
   private String getClassName(Object object) {
     return object == null ? "" : object.getClass().getName();
   }
@@ -196,7 +231,14 @@ public class SimpleHbaseEnrichmentWriter extends 
AbstractWriter implements BulkM
     else if (o instanceof List) {
       List<String> keyCols = new ArrayList<>();
       for(Object key : (List)o) {
-        keyCols.add(key.toString());
+        if (key == null) {
+          throw new IllegalArgumentException("Column name must not be null");
+        }
+        String columnName = key.toString();
+        if (columnName.trim().isEmpty()) {
+          throw new IllegalArgumentException("Column name must not be empty");
+        }
+        keyCols.add(columnName);
       }
       LOG.debug("Key columns: '{}'", String.join(",", keyCols));
       return keyCols;

http://git-wip-us.apache.org/repos/asf/metron/blob/353bc8b3/metron-platform/metron-parsers/src/test/java/org/apache/metron/writers/SimpleHBaseEnrichmentWriterTest.java
----------------------------------------------------------------------
diff --git 
a/metron-platform/metron-parsers/src/test/java/org/apache/metron/writers/SimpleHBaseEnrichmentWriterTest.java
 
b/metron-platform/metron-parsers/src/test/java/org/apache/metron/writers/SimpleHBaseEnrichmentWriterTest.java
index 15c84da..b1404e2 100644
--- 
a/metron-platform/metron-parsers/src/test/java/org/apache/metron/writers/SimpleHBaseEnrichmentWriterTest.java
+++ 
b/metron-platform/metron-parsers/src/test/java/org/apache/metron/writers/SimpleHBaseEnrichmentWriterTest.java
@@ -40,6 +40,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Arrays;
 
 public class SimpleHBaseEnrichmentWriterTest {
   private static final String SENSOR_TYPE= "dummy";
@@ -140,6 +141,123 @@ public class SimpleHBaseEnrichmentWriterTest {
     Assert.assertNull(values.get(0).getValue().getMetadata().get("foo"));
     Assert.assertEquals(2, values.get(0).getValue().getMetadata().size());
   }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testConfigValidation_missing_enrichment_type() {
+    final String sensorType = "dummy";
+    SimpleHbaseEnrichmentWriter writer = new SimpleHbaseEnrichmentWriter();
+
+    WriterConfiguration configuration = createConfig(1,
+            new HashMap<String, Object>() {{
+              
put(SimpleHbaseEnrichmentWriter.Configurations.KEY_COLUMNS.getKey(), "ip");
+            }}
+    );
+    try {
+      writer.configure(sensorType, configuration);
+    } catch (IllegalArgumentException ex) {
+      Assert.assertEquals(String.format("%s must be provided",
+              
SimpleHbaseEnrichmentWriter.Configurations.ENRICHMENT_TYPE.getKey()), 
ex.getMessage());
+      throw ex;
+    }
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testConfigValidation_enrichment_type_is_not_a_string() {
+    final String sensorType = "dummy";
+    SimpleHbaseEnrichmentWriter writer = new SimpleHbaseEnrichmentWriter();
+
+    WriterConfiguration configuration = createConfig(1,
+            new HashMap<String, Object>() {{
+              
put(SimpleHbaseEnrichmentWriter.Configurations.KEY_COLUMNS.getKey(), "ip");
+              
put(SimpleHbaseEnrichmentWriter.Configurations.ENRICHMENT_TYPE.getKey(), 10);
+            }}
+    );
+    try {
+      writer.configure(sensorType, configuration);
+    } catch (IllegalArgumentException ex) {
+      Assert.assertEquals(String.format("%s must be a string",
+              
SimpleHbaseEnrichmentWriter.Configurations.ENRICHMENT_TYPE.getKey()), 
ex.getMessage());
+      throw ex;
+    }
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testConfigValidation_enrichment_type_is_empty() {
+    final String sensorType = "dummy";
+    SimpleHbaseEnrichmentWriter writer = new SimpleHbaseEnrichmentWriter();
+
+    WriterConfiguration configuration = createConfig(1,
+            new HashMap<String, Object>() {{
+              
put(SimpleHbaseEnrichmentWriter.Configurations.KEY_COLUMNS.getKey(), "ip");
+              
put(SimpleHbaseEnrichmentWriter.Configurations.ENRICHMENT_TYPE.getKey(), "  ");
+            }}
+    );
+    try {
+      writer.configure(sensorType, configuration);
+    } catch (IllegalArgumentException ex) {
+        Assert.assertEquals(String.format("%s must not be an empty string",
+                
SimpleHbaseEnrichmentWriter.Configurations.ENRICHMENT_TYPE.getKey()), 
ex.getMessage());
+      throw ex;
+    }
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testConfigValidation_missing_key_columns() {
+    final String sensorType = "dummy";
+    SimpleHbaseEnrichmentWriter writer = new SimpleHbaseEnrichmentWriter();
+
+    WriterConfiguration configuration = createConfig(1,
+            new HashMap<String, Object>() {{
+              
put(SimpleHbaseEnrichmentWriter.Configurations.ENRICHMENT_TYPE.getKey(), 
ENRICHMENT_TYPE);
+            }}
+    );
+    try {
+      writer.configure(sensorType, configuration);
+    } catch (IllegalArgumentException ex) {
+        Assert.assertEquals(String.format("%s must be provided",
+                
SimpleHbaseEnrichmentWriter.Configurations.KEY_COLUMNS.getKey()), 
ex.getMessage());
+      throw ex;
+    }
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testConfigValidation_key_columns_contain_an_empty_value() {
+    final String sensorType = "dummy";
+    SimpleHbaseEnrichmentWriter writer = new SimpleHbaseEnrichmentWriter();
+
+    WriterConfiguration configuration = createConfig(1,
+            new HashMap<String, Object>() {{
+              
put(SimpleHbaseEnrichmentWriter.Configurations.ENRICHMENT_TYPE.getKey(), 
ENRICHMENT_TYPE);
+              
put(SimpleHbaseEnrichmentWriter.Configurations.KEY_COLUMNS.getKey(), 
Arrays.asList("ip", "  "));
+            }}
+    );
+    try {
+      writer.configure(sensorType, configuration);
+    } catch (IllegalArgumentException ex) {
+        Assert.assertEquals("Column name must not be empty", ex.getMessage());
+      throw ex;
+    }
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void testConfigValidation_key_columns_contain_a_null_value() {
+    final String sensorType = "dummy";
+    SimpleHbaseEnrichmentWriter writer = new SimpleHbaseEnrichmentWriter();
+
+    WriterConfiguration configuration = createConfig(1,
+            new HashMap<String, Object>() {{
+              
put(SimpleHbaseEnrichmentWriter.Configurations.ENRICHMENT_TYPE.getKey(), 
ENRICHMENT_TYPE);
+              
put(SimpleHbaseEnrichmentWriter.Configurations.KEY_COLUMNS.getKey(), 
Arrays.asList("ip", null));
+            }}
+    );
+    try {
+      writer.configure(sensorType, configuration);
+    } catch (IllegalArgumentException ex) {
+        Assert.assertEquals("Column name must not be null", ex.getMessage());
+      throw ex;
+    }
+  }
+
   public static List<LookupKV<EnrichmentKey, EnrichmentValue>> getValues() 
throws IOException {
     MockHTable table = (MockHTable) 
MockHBaseTableProvider.getFromCache(TABLE_NAME);
     Assert.assertNotNull(table);

Reply via email to