Skip to content

Commit 9aca2dc

Browse files
committed
SubList function (#5529)
* SubList function Signed-off-by: Sai charan raj Gudala <s.charancherry22@gmail.com>
1 parent f136f48 commit 9aca2dc

2 files changed

Lines changed: 187 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.opensearch.dataprepper.expression;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.function.Function;
6+
7+
import org.opensearch.dataprepper.model.event.Event;
8+
9+
import javax.inject.Named;
10+
11+
@Named
12+
public class SubListExpressionFunction implements ExpressionFunction {
13+
14+
@Override
15+
public String getFunctionName() {
16+
return "subList";
17+
}
18+
19+
@Override
20+
public Object evaluate(List<Object> args, Event event, Function<Object, Object> convertLiteralType) {
21+
if (args.size() != 3) {
22+
throw new IllegalArgumentException("subList() takes 3 arguments");
23+
}
24+
if (!(args.get(0) instanceof String)) {
25+
throw new IllegalArgumentException("subList() takes 1st argument as string type");
26+
}
27+
if (!(args.get(1) instanceof Integer) || !(args.get(2) instanceof Integer)) {
28+
throw new IllegalArgumentException("subList() takes 2nd and 3rd arguments as type integer");
29+
}
30+
String key = (String)args.get(0);
31+
final Object value = event.get(key, Object.class);
32+
if (value == null) return null;
33+
if (!(value instanceof List)) {
34+
throw new RuntimeException(key + " is not of list type");
35+
}
36+
List<?> sourceList = (List<?>)value;
37+
int startIndex = (Integer)args.get(1), endIndex = (Integer)args.get(2);
38+
if (endIndex == -1) endIndex = sourceList.size();
39+
40+
if (startIndex < 0 || startIndex >= sourceList.size()) {
41+
throw new RuntimeException("subList() start index should be between 0 and list length (inclusive)");
42+
}
43+
44+
if (endIndex < 0 || endIndex > sourceList.size()) {
45+
throw new RuntimeException("subList() end index should be between 0 and list length or -1 for list length (exclusive)");
46+
}
47+
if (startIndex > endIndex) {
48+
throw new RuntimeException("subList() start index should be less or equal to end index");
49+
}
50+
return new ArrayList<>(sourceList.subList(startIndex, endIndex));
51+
}
52+
53+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package org.opensearch.dataprepper.expression;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.opensearch.dataprepper.model.event.Event;
5+
import org.opensearch.dataprepper.model.event.JacksonEvent;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.mockito.Mockito.mock;
9+
import static org.hamcrest.CoreMatchers.equalTo;
10+
import static org.hamcrest.MatcherAssert.assertThat;
11+
import static org.junit.jupiter.api.Assertions.assertThrows;
12+
13+
import java.util.List;
14+
import java.util.Map;
15+
import java.util.function.Function;
16+
17+
public class SubListExpressionFunctionTest {
18+
private SubListExpressionFunction subListExpressionFunction;
19+
private Event testEvent;
20+
private Function<Object, Object> testFunction;
21+
22+
private Event createTestEvent(final Object data) {
23+
return JacksonEvent.builder().withEventType("event").withData(data).build();
24+
}
25+
26+
27+
public SubListExpressionFunction createObjectUnderTest() {
28+
testFunction = mock(Function.class);
29+
return new SubListExpressionFunction();
30+
}
31+
32+
@Test
33+
void testFunctionName() {
34+
subListExpressionFunction = createObjectUnderTest();
35+
assertEquals("subList", subListExpressionFunction.getFunctionName());
36+
}
37+
38+
@Test
39+
void testWithValidArguments() {
40+
subListExpressionFunction = createObjectUnderTest();
41+
List testList = List.of(1, 2, 3, 4);
42+
testEvent = createTestEvent(Map.of("list", testList));
43+
assertThat(subListExpressionFunction.evaluate(List.of("/list", 1, 3), testEvent, testFunction), equalTo(List.of(2, 3)));
44+
}
45+
46+
@Test
47+
void testWithOutOfBoundArgumentsCase1() {
48+
subListExpressionFunction = createObjectUnderTest();
49+
List testList = List.of(1, 2, 3, 4, 5, 6);
50+
testEvent = createTestEvent(Map.of("list", testList));
51+
Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", -1, 4), testEvent, testFunction));
52+
assertEquals("subList() start index should be between 0 and list length (inclusive)", exception.getMessage());
53+
}
54+
55+
@Test
56+
void testWithOutOfBoundArgumentsCase2() {
57+
subListExpressionFunction = createObjectUnderTest();
58+
List testList = List.of(1, 2, 3, 4, 5, 6);
59+
testEvent = createTestEvent(Map.of("list", testList));
60+
Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", 10, 4), testEvent, testFunction));
61+
assertEquals("subList() start index should be between 0 and list length (inclusive)", exception.getMessage());
62+
}
63+
64+
@Test
65+
void testWithOutOfBoundArgumentsCase3() {
66+
subListExpressionFunction = createObjectUnderTest();
67+
List testList = List.of(1, 2, 3, 4, 5, 6);
68+
testEvent = createTestEvent(Map.of("list", testList));
69+
Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", 4, 2), testEvent, testFunction));
70+
assertEquals("subList() start index should be less or equal to end index", exception.getMessage());
71+
}
72+
73+
@Test
74+
void testWithOutOfBoundArgumentsCase4() {
75+
subListExpressionFunction = createObjectUnderTest();
76+
List testList = List.of(1, 2, 3, 4, 5, 6);
77+
testEvent = createTestEvent(Map.of("list", testList));
78+
Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", 1, 10), testEvent, testFunction));
79+
assertEquals("subList() end index should be between 0 and list length or -1 for list length (exclusive)", exception.getMessage());
80+
}
81+
82+
@Test
83+
void testWithOutOfBoundArgumentsCase5() {
84+
subListExpressionFunction = createObjectUnderTest();
85+
List testList = List.of(1, 2, 3, 4, 5, 6);
86+
testEvent = createTestEvent(Map.of("list", testList));
87+
Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", 1, -2), testEvent, testFunction));
88+
assertEquals("subList() end index should be between 0 and list length or -1 for list length (exclusive)", exception.getMessage());
89+
}
90+
91+
@Test
92+
void testWithInvalidArguments() {
93+
subListExpressionFunction = createObjectUnderTest();
94+
List testList = List.of(1, 2, 3, 4, 5, 6);
95+
testEvent = createTestEvent(Map.of("list", testList));
96+
Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", 5, 2), testEvent, testFunction));
97+
assertEquals("subList() start index should be less or equal to end index", exception.getMessage());
98+
}
99+
100+
@Test
101+
void testWithInvalidArgumentsCase2() {
102+
subListExpressionFunction = createObjectUnderTest();
103+
List testList = List.of(1, 2, 3, 4, 5, 6);
104+
testEvent = createTestEvent(Map.of("list", testList));
105+
Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", "five", "two"), testEvent, testFunction));
106+
assertEquals("subList() takes 2nd and 3rd arguments as type integer", exception.getMessage());
107+
}
108+
109+
@Test
110+
void testWithInvalidArgumentsCase3() {
111+
subListExpressionFunction = createObjectUnderTest();
112+
List testList = List.of(1, 2, 3, 4, 5, 6);
113+
testEvent = createTestEvent(Map.of("list", testList));
114+
Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", 0), testEvent, testFunction));
115+
assertEquals("subList() takes 3 arguments", exception.getMessage());
116+
}
117+
118+
@Test
119+
void testWithInvalidArgumentsCase4() {
120+
subListExpressionFunction = createObjectUnderTest();
121+
List testList = List.of(1, 2, 3, 4, 5, 6);
122+
testEvent = createTestEvent(Map.of("list", testList));
123+
Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of(1, 0, 2), testEvent, testFunction));
124+
assertEquals("subList() takes 1st argument as string type", exception.getMessage());
125+
}
126+
127+
@Test
128+
void testWithInvalidArgumentsCase5() {
129+
subListExpressionFunction = createObjectUnderTest();
130+
testEvent = createTestEvent(Map.of("list", "testList"));
131+
Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", 0, 2), testEvent, testFunction));
132+
assertEquals("/list is not of list type", exception.getMessage());
133+
}
134+
}

0 commit comments

Comments
 (0)