|
| 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<Integer> 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 testWithValidArgumentsCase2() { |
| 48 | + subListExpressionFunction = createObjectUnderTest(); |
| 49 | + List<Integer> testList = List.of(1, 2, 3, 4); |
| 50 | + testEvent = createTestEvent(Map.of("list", testList)); |
| 51 | + assertThat(subListExpressionFunction.evaluate(List.of("/list", "\"1\"", "\"3\""), testEvent, testFunction), equalTo(List.of(2, 3))); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void testWithValidArgumentsCase3() { |
| 56 | + subListExpressionFunction = createObjectUnderTest(); |
| 57 | + List<Integer> testList = List.of(1, 2, 3, 4); |
| 58 | + testEvent = createTestEvent(Map.of("main", Map.of("list", testList))); |
| 59 | + assertThat(subListExpressionFunction.evaluate(List.of("/main/list", "\"1\"", "\"3\""), testEvent, testFunction), equalTo(List.of(2, 3))); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testWithValidArgumentsCase4() { |
| 64 | + subListExpressionFunction = createObjectUnderTest(); |
| 65 | + List<Integer> testList = List.of(1, 2, 3, 4); |
| 66 | + testEvent = createTestEvent(Map.of("main", Map.of("list", testList))); |
| 67 | + assertThat(subListExpressionFunction.evaluate(List.of("/main/list", "\"1\"", "\"-1\""), testEvent, testFunction), equalTo(List.of(2, 3, 4))); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void testWithValidArgumentsCase5() { |
| 72 | + subListExpressionFunction = createObjectUnderTest(); |
| 73 | + List<Integer> testList = List.of(1, 2, 3, 4); |
| 74 | + testEvent = createTestEvent(Map.of("list", testList)); |
| 75 | + assertThat(subListExpressionFunction.evaluate(List.of("/list", 1, 3), testEvent, testFunction), equalTo(List.of(2, 3))); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void testWithOutOfBoundArgumentsCase1() { |
| 80 | + subListExpressionFunction = createObjectUnderTest(); |
| 81 | + List<Integer> testList = List.of(1, 2, 3, 4, 5, 6); |
| 82 | + testEvent = createTestEvent(Map.of("list", testList)); |
| 83 | + Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", "\"-1\"", "\"4\""), testEvent, testFunction)); |
| 84 | + assertEquals("subList() start index should be between 0 and list length (inclusive)", exception.getMessage()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void testWithOutOfBoundArgumentsCase2() { |
| 89 | + subListExpressionFunction = createObjectUnderTest(); |
| 90 | + List<Integer> testList = List.of(1, 2, 3, 4, 5, 6); |
| 91 | + testEvent = createTestEvent(Map.of("list", testList)); |
| 92 | + Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", "\"10\"", "\"4\""), testEvent, testFunction)); |
| 93 | + assertEquals("subList() start index should be between 0 and list length (inclusive)", exception.getMessage()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void testWithOutOfBoundArgumentsCase3() { |
| 98 | + subListExpressionFunction = createObjectUnderTest(); |
| 99 | + List<Integer> testList = List.of(1, 2, 3, 4, 5, 6); |
| 100 | + testEvent = createTestEvent(Map.of("list", testList)); |
| 101 | + Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", "\"4\"", "\"2\""), testEvent, testFunction)); |
| 102 | + assertEquals("subList() start index should be less than or equal to end index", exception.getMessage()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void testWithOutOfBoundArgumentsCase4() { |
| 107 | + subListExpressionFunction = createObjectUnderTest(); |
| 108 | + List<Integer> testList = List.of(1, 2, 3, 4, 5, 6); |
| 109 | + testEvent = createTestEvent(Map.of("list", testList)); |
| 110 | + Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", "\"1\"", "\"10\""), testEvent, testFunction)); |
| 111 | + assertEquals("subList() end index should be between 0 and list length or -1 for list length (exclusive)", exception.getMessage()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void testWithOutOfBoundArgumentsCase5() { |
| 116 | + subListExpressionFunction = createObjectUnderTest(); |
| 117 | + List<Integer> testList = List.of(1, 2, 3, 4, 5, 6); |
| 118 | + testEvent = createTestEvent(Map.of("list", testList)); |
| 119 | + Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", "\"1\"", "\"-2\""), testEvent, testFunction)); |
| 120 | + assertEquals("subList() end index should be between 0 and list length or -1 for list length (exclusive)", exception.getMessage()); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void testWithInvalidArguments() { |
| 125 | + subListExpressionFunction = createObjectUnderTest(); |
| 126 | + List<Integer> testList = List.of(1, 2, 3, 4, 5, 6); |
| 127 | + testEvent = createTestEvent(Map.of("list", testList)); |
| 128 | + Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", "\"5\"", "\"2\""), testEvent, testFunction)); |
| 129 | + assertEquals("subList() start index should be less than or equal to end index", exception.getMessage()); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void testWithInvalidArgumentsCase2() { |
| 134 | + subListExpressionFunction = createObjectUnderTest(); |
| 135 | + List<Integer> testList = List.of(1, 2, 3, 4, 5, 6); |
| 136 | + testEvent = createTestEvent(Map.of("list", testList)); |
| 137 | + Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", "\"five\"", "\"two\""), testEvent, testFunction)); |
| 138 | + assertEquals("subList() takes 2nd and 3rd arguments as integers", exception.getMessage()); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + void testWithInvalidArgumentsCase3() { |
| 143 | + subListExpressionFunction = createObjectUnderTest(); |
| 144 | + List<Integer> testList = List.of(1, 2, 3, 4, 5, 6); |
| 145 | + testEvent = createTestEvent(Map.of("list", testList)); |
| 146 | + Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", "\"0\""), testEvent, testFunction)); |
| 147 | + assertEquals("subList() takes 3 arguments", exception.getMessage()); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + void testWithInvalidArgumentsCase4() { |
| 152 | + subListExpressionFunction = createObjectUnderTest(); |
| 153 | + List<Integer> testList = List.of(1, 2, 3, 4, 5, 6); |
| 154 | + testEvent = createTestEvent(Map.of("list", testList)); |
| 155 | + Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of(1, "\"0\"", "\"2\""), testEvent, testFunction)); |
| 156 | + assertEquals("subList() takes 1st argument as string type", exception.getMessage()); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + void testWithInvalidArgumentsCase5() { |
| 161 | + subListExpressionFunction = createObjectUnderTest(); |
| 162 | + testEvent = createTestEvent(Map.of("list", "testList")); |
| 163 | + Exception exception = assertThrows(RuntimeException.class, () -> subListExpressionFunction.evaluate(List.of("/list", "\"0\"", "\"2\""), testEvent, testFunction)); |
| 164 | + assertEquals("/list is not of list type", exception.getMessage()); |
| 165 | + } |
| 166 | +} |
0 commit comments