Skip to content

Commit

Permalink
Fix building and testing ElasticSearch plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsbasjes committed Jun 12, 2020
1 parent f6c8074 commit 99d2f87
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -29,6 +29,7 @@ bin
#IntelliJ stuff
.idea
*.iml
.attach_pid*

#test files
x
Expand Down
13 changes: 13 additions & 0 deletions pom.xml
Expand Up @@ -155,9 +155,16 @@

<dependencies>


<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -498,6 +505,11 @@
<artifactId>maven-surefire-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
Expand Down Expand Up @@ -803,6 +815,7 @@
<plugin><artifactId>maven-site-plugin</artifactId><version>3.8.2</version></plugin>
<plugin><artifactId>maven-source-plugin</artifactId><version>3.2.1</version></plugin>
<plugin><artifactId>maven-surefire-plugin</artifactId><version>3.0.0-M4</version></plugin>
<plugin><artifactId>maven-failsafe-plugin</artifactId><version>3.0.0-M4</version></plugin>
<plugin><artifactId>maven-war-plugin</artifactId><version>3.2.3</version></plugin>

<plugin>
Expand Down
6 changes: 3 additions & 3 deletions src/main/docs/README-Usage.md
Expand Up @@ -102,21 +102,21 @@ Essentially you can redirect all JCL logging into SLF4J and then into the actual
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.29</version>
<version>1.7.30</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.29</version>
<version>1.7.30</version>
</dependency>

or (if you really have to) can redirect all SLF4J into JCL by adding something like this

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jcl</artifactId>
<version>1.7.29</version>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
Expand Down
36 changes: 27 additions & 9 deletions udfs/elasticsearch/pom.xml
Expand Up @@ -45,12 +45,6 @@
<artifactId>yauaa</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>

<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
Expand All @@ -65,6 +59,10 @@
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>

Expand All @@ -90,6 +88,14 @@
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</exclusion>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
</exclusion>
</exclusions>
</dependency>

Expand All @@ -98,6 +104,12 @@
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
Expand All @@ -114,10 +126,16 @@
<scope>test</scope>
</dependency>


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>

Expand Down
Expand Up @@ -23,8 +23,6 @@
import org.elasticsearch.ingest.AbstractProcessor;
import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.Processor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -78,9 +76,6 @@ public YauaaProcessor(String tag,
}

this.uaa = builder.build();

Logger log = LoggerFactory.getLogger("PROCESSOR");
log.error("{}", uaa);
}

@Override
Expand Down
Expand Up @@ -18,9 +18,12 @@
package org.elasticsearch.plugin.ingest.yauaa;

import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.Processor;
import org.elasticsearch.ingest.RandomDocumentPicks;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -33,7 +36,8 @@
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;

public class YauaaProcessorTests {
@RunWith(com.carrotsearch.randomizedtesting.RandomizedRunner.class)
public class YauaaProcessorTest {

private static final String SOURCE_FIELD = "source_field";
private static final String TARGET_FIELD = "target_field";
Expand Down Expand Up @@ -139,4 +143,69 @@ private void assertHasNotKey(Map<String, String> results, String key) {
MatcherAssert.assertThat(results, not(hasKey(key)));
}

@SuppressWarnings("unchecked")
@Test
public void testIngestPlugin() throws Exception {
IngestYauaaPlugin plugin = new IngestYauaaPlugin();

Map<String, Processor.Factory> processors = plugin.getProcessors(null);

Processor.Factory yauaaFactory = processors.get("yauaa");

Map<String, Object> configuration = new HashMap<>();

configuration.put("field", SOURCE_FIELD);
configuration.put("target_field", TARGET_FIELD);
configuration.put("fieldNames", Arrays.asList("DeviceClass", "DeviceBrand", "DeviceName", "AgentNameVersionMajor", "FirstProductName"));
configuration.put("cacheSize", 10);
configuration.put("preheat", 10);
configuration.put("extraRules", "config:\n- matcher:\n extract:\n - 'FirstProductName : 1 :agent.(1)product.(1)name'\n");

Processor processor = yauaaFactory.create(processors, randomAlphaOfLength(10), configuration);

Map<String, Object> document = new HashMap<>();
document.put(SOURCE_FIELD,
"Mozilla/5.0 (Linux; Android 7.0; Nexus 6 Build/NBD90Z) " +
"AppleWebKit/537.36 (KHTML, like Gecko) " +
"Chrome/53.0.2785.124 Mobile Safari/537.36");
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);

Map<String, Object> data = processor.execute(ingestDocument).getSourceAndMetadata();

MatcherAssert.assertThat(data, hasKey(TARGET_FIELD));

Map<String, String> results = (Map<String, String>) data.get(TARGET_FIELD);

// The EXPLICITLY requested fields
assertHasKValue(results, "FirstProductName", "Mozilla");
assertHasKValue(results, "DeviceClass", "Phone");
assertHasKValue(results, "DeviceBrand", "Google");
assertHasKValue(results, "DeviceName", "Google Nexus 6");
assertHasKValue(results, "AgentNameVersionMajor", "Chrome 53");

// The IMPLICITLY requested fields (i.e. partials of the actually requested ones)
assertHasKValue(results, "AgentName", "Chrome");
assertHasKValue(results, "AgentVersion", "53.0.2785.124");
assertHasKValue(results, "AgentVersionMajor", "53");

// The NOT requested fields
assertHasNotKey(results, "OperatingSystemClass");
assertHasNotKey(results, "OperatingSystemName");
assertHasNotKey(results, "OperatingSystemNameVersion");
assertHasNotKey(results, "OperatingSystemNameVersionMajor");
assertHasNotKey(results, "OperatingSystemVersion");
assertHasNotKey(results, "OperatingSystemVersionBuild");
assertHasNotKey(results, "OperatingSystemVersionMajor");
assertHasNotKey(results, "LayoutEngineClass");
assertHasNotKey(results, "LayoutEngineName");
assertHasNotKey(results, "LayoutEngineNameVersion");
assertHasNotKey(results, "LayoutEngineNameVersionMajor");
assertHasNotKey(results, "LayoutEngineVersion");
assertHasNotKey(results, "LayoutEngineVersionMajor");
assertHasNotKey(results, "AgentClass");
assertHasNotKey(results, "AgentNameVersion");

LoggerFactory.getLogger("TestYauaaProcessor").info("Complete set of returned results:{}", results);
}

}
13 changes: 13 additions & 0 deletions udfs/pom.xml
Expand Up @@ -70,7 +70,20 @@
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-jcl</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
Expand Down

0 comments on commit 99d2f87

Please sign in to comment.