metron-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] justinleet commented on a change in pull request #1299: METRON-1929: Build GET_ASN Stellar function
Date Tue, 08 Jan 2019 17:36:15 GMT
justinleet commented on a change in pull request #1299: METRON-1929: Build GET_ASN Stellar
function
URL: https://github.com/apache/metron/pull/1299#discussion_r246087229
 
 

 ##########
 File path: metron-platform/metron-enrichment/src/test/java/org/apache/metron/enrichment/stellar/AsnEnrichmentFunctionsTest.java
 ##########
 @@ -0,0 +1,161 @@
+/*
+ * 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 org.apache.metron.enrichment.stellar;
+
+import com.google.common.collect.ImmutableMap;
+import java.io.File;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.metron.enrichment.adapters.maxmind.asn.AsnDatabase;
+import org.apache.metron.stellar.common.StellarProcessor;
+import org.apache.metron.stellar.dsl.Context;
+import org.apache.metron.stellar.dsl.DefaultVariableResolver;
+import org.apache.metron.stellar.dsl.ParseException;
+import org.apache.metron.stellar.dsl.StellarFunctions;
+import org.apache.metron.test.utils.UnitTestHelper;
+import org.json.simple.JSONObject;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class AsnEnrichmentFunctionsTest {
+
+  private static Context context;
+  private static File asnHdfsFile;
+
+  private static JSONObject expectedMessage = new JSONObject();
+  private static JSONObject expectedSubsetMessage = new JSONObject();
+
+  @BeforeClass
+  @SuppressWarnings("unchecked")
+  public static void setupOnce() {
+    // Construct this explicitly here, otherwise it'll be a Long instead of Integer.
+    expectedMessage.put("autonomous_system_organization", "Google LLC");
+    expectedMessage.put("autonomous_system_number", 15169);
+    expectedMessage.put("network", "8.8.4.0");
+
+    expectedSubsetMessage.put("autonomous_system_organization", "Google LLC");
+    expectedSubsetMessage.put("autonomous_system_number", 15169);
+
+    String baseDir = UnitTestHelper.findDir("GeoLite");
+    asnHdfsFile = new File(new File(baseDir), "GeoLite2-ASN.tar.gz");
+  }
+
+  @Before
+  public void setup() throws Exception {
+    context = new Context.Builder().with(Context.Capabilities.GLOBAL_CONFIG,
+        () -> ImmutableMap.of(AsnDatabase.ASN_HDFS_FILE, asnHdfsFile.getAbsolutePath())
+    ).build();
+  }
+
+  public Object run(String rule, Map<String, Object> variables) {
+    StellarProcessor processor = new StellarProcessor();
+    Assert.assertTrue(rule + " not valid.", processor.validate(rule, context));
+    return processor.parse(rule,
+        new DefaultVariableResolver(variables::get, variables::containsKey),
+        StellarFunctions.FUNCTION_RESOLVER(), context);
+  }
+
+  @Test
+  public void testMissingDb() {
+    context = new Context.Builder().with(Context.Capabilities.GLOBAL_CONFIG,
+        () -> ImmutableMap.of(AsnDatabase.ASN_HDFS_FILE, "./fakefile.mmdb")
+    ).build();
+    String stellar = "ASN_GET()";
+    try {
+      run(stellar, ImmutableMap.of());
+    } catch (Exception expected) {
+      Assert.assertTrue(expected.getMessage().contains("File fakefile.mmdb does not exist"));
+    }
+  }
+
+  @Test
+  public void testMissingDbDuringUpdate() {
+    String stellar = "ASN_GET()";
+    Object result = run(stellar, ImmutableMap.of());
+    Assert.assertNull("Null IP should return null", result);
+    try {
+      AsnDatabase.INSTANCE.updateIfNecessary(
+          Collections.singletonMap(AsnDatabase.ASN_HDFS_FILE, "./fakefile.mmdb"));
+    } catch (IllegalStateException e) {
+      // ignore it, the file doesn't exist
+    }
+    // Should still continue to query the old database, instead of dying.
+    result = run(stellar, ImmutableMap.of());
+    Assert.assertNull("Null IP should return null", result);
+  }
+
+  @Test
+  public void testGetNull() {
+    String stellar = "ASN_GET()";
 
 Review comment:
   Added tests to reflect this.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message