|
| 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