Skip to content

Commit fc0b4c2

Browse files
committed
add geteventType expresion
Signed-off-by: Shenoy Pratik <sgguruda@amazon.com>
1 parent b422250 commit fc0b4c2

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package org.opensearch.dataprepper.expression;
7+
8+
import org.opensearch.dataprepper.model.event.Event;
9+
import javax.inject.Named;
10+
import java.util.function.Function;
11+
import java.util.List;
12+
13+
@Named
14+
public class GetEventTypeExpressionFunction implements ExpressionFunction {
15+
16+
@Override
17+
public String getFunctionName() {
18+
return "getEventType";
19+
}
20+
21+
@Override
22+
public Object evaluate(final List<Object> args, Event event, Function<Object, Object> convertLiteralType) {
23+
if (!args.isEmpty()) {
24+
throw new RuntimeException("getEventType() does not take any arguments");
25+
}
26+
return event.getMetadata().getEventType();
27+
}
28+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.opensearch.dataprepper.expression;
2+
3+
import org.opensearch.dataprepper.model.event.Event;
4+
import org.opensearch.dataprepper.model.event.JacksonEvent;
5+
import org.junit.jupiter.api.Test;
6+
import static org.hamcrest.CoreMatchers.equalTo;
7+
import static org.hamcrest.MatcherAssert.assertThat;
8+
import static org.junit.jupiter.api.Assertions.assertThrows;
9+
10+
import java.util.List;
11+
import java.util.Map;
12+
import java.util.function.Function;
13+
14+
class GetEventTypeExpressionFunctionTest {
15+
16+
private GetEventTypeExpressionFunction createObjectUnderTest() {
17+
return new GetEventTypeExpressionFunction();
18+
}
19+
20+
private Event createTestEvent(final String eventType) {
21+
return JacksonEvent.builder()
22+
.withEventType(eventType)
23+
.withData(Map.of())
24+
.build();
25+
}
26+
27+
@Test
28+
void testGetEventTypeReturnsCorrectType() {
29+
GetEventTypeExpressionFunction function = createObjectUnderTest();
30+
Event testEvent = createTestEvent("LOG");
31+
32+
Object result = function.evaluate(List.of(), testEvent, Function.identity());
33+
34+
assertThat(result, equalTo("LOG"));
35+
}
36+
37+
@Test
38+
void testGetEventTypeThrowsExceptionWhenArgumentsProvided() {
39+
GetEventTypeExpressionFunction function = createObjectUnderTest();
40+
Event testEvent = createTestEvent("LOG");
41+
42+
assertThrows(RuntimeException.class, () -> function.evaluate(List.of("arg1"), testEvent, Function.identity()));
43+
}
44+
45+
@Test
46+
void testGetEventTypeReturnsNullForNullEventType() {
47+
GetEventTypeExpressionFunction function = createObjectUnderTest();
48+
Event testEvent = createTestEvent(null);
49+
50+
Object result = function.evaluate(List.of(), testEvent, Function.identity());
51+
52+
assertThat(result, equalTo(null));
53+
}
54+
}

0 commit comments

Comments
 (0)