|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.inference.common; |
| 9 | + |
| 10 | +import org.elasticsearch.test.ESTestCase; |
| 11 | + |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +import static org.hamcrest.Matchers.is; |
| 16 | + |
| 17 | +public class MapPathExtractorTests extends ESTestCase { |
| 18 | + public void testExtract_RetrievesListOfLists() { |
| 19 | + Map<String, Object> input = Map.of( |
| 20 | + "result", |
| 21 | + Map.of("embeddings", List.of(Map.of("index", 0, "embedding", List.of(1, 2)), Map.of("index", 1, "embedding", List.of(3, 4)))) |
| 22 | + ); |
| 23 | + |
| 24 | + assertThat(MapPathExtractor.extract(input, "$.result.embeddings[*].embedding"), is(List.of(List.of(1, 2), List.of(3, 4)))); |
| 25 | + } |
| 26 | + |
| 27 | + public void testExtract_IteratesListOfMapsToListOfStrings() { |
| 28 | + Map<String, Object> input = Map.of( |
| 29 | + "result", |
| 30 | + List.of(Map.of("key", List.of("value1", "value2")), Map.of("key", List.of("value3", "value4"))) |
| 31 | + ); |
| 32 | + |
| 33 | + assertThat( |
| 34 | + MapPathExtractor.extract(input, "$.result[*].key[*]"), |
| 35 | + is(List.of(List.of("value1", "value2"), List.of("value3", "value4"))) |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + public void testExtract_IteratesListOfMapsToListOfMapsOfStringToDoubles() { |
| 40 | + Map<String, Object> input = Map.of( |
| 41 | + "result", |
| 42 | + List.of( |
| 43 | + Map.of("key", List.of(Map.of("a", 1.1d), Map.of("a", 2.2d))), |
| 44 | + Map.of("key", List.of(Map.of("a", 3.3d), Map.of("a", 4.4d))) |
| 45 | + ) |
| 46 | + ); |
| 47 | + |
| 48 | + assertThat(MapPathExtractor.extract(input, "$.result[*].key[*].a"), is(List.of(List.of(1.1d, 2.2d), List.of(3.3d, 4.4d)))); |
| 49 | + } |
| 50 | + |
| 51 | + public void testExtract_ReturnsNullForEmptyList() { |
| 52 | + Map<String, Object> input = Map.of(); |
| 53 | + |
| 54 | + assertNull(MapPathExtractor.extract(input, "$.awesome")); |
| 55 | + } |
| 56 | + |
| 57 | + public void testExtract_ReturnsNull_WhenTheInputMapIsNull() { |
| 58 | + assertNull(MapPathExtractor.extract(null, "$.result")); |
| 59 | + } |
| 60 | + |
| 61 | + public void testExtract_ReturnsNull_WhenPathIsNull() { |
| 62 | + assertNull(MapPathExtractor.extract(Map.of("key", "value"), null)); |
| 63 | + } |
| 64 | + |
| 65 | + public void testExtract_ReturnsNull_WhenPathIsWhiteSpace() { |
| 66 | + assertNull(MapPathExtractor.extract(Map.of("key", "value"), " ")); |
| 67 | + } |
| 68 | + |
| 69 | + public void testExtract_ThrowsException_WhenPathDoesNotStartWithDollarSign() { |
| 70 | + var exception = expectThrows(IllegalArgumentException.class, () -> MapPathExtractor.extract(Map.of("key", "value"), ".key")); |
| 71 | + assertThat(exception.getMessage(), is("Path [.key] must start with a dollar sign ($)")); |
| 72 | + } |
| 73 | + |
| 74 | + public void testExtract_ThrowsException_WhenCannotFindField() { |
| 75 | + Map<String, Object> input = Map.of("result", "key"); |
| 76 | + |
| 77 | + var exception = expectThrows(IllegalArgumentException.class, () -> MapPathExtractor.extract(input, "$.awesome")); |
| 78 | + assertThat(exception.getMessage(), is("Unable to find field [awesome] in map")); |
| 79 | + } |
| 80 | + |
| 81 | + public void testExtract_ThrowsAnException_WhenThePathIsInvalid() { |
| 82 | + Map<String, Object> input = Map.of("result", "key"); |
| 83 | + |
| 84 | + var exception = expectThrows(IllegalArgumentException.class, () -> MapPathExtractor.extract(input, "$awesome")); |
| 85 | + assertThat(exception.getMessage(), is("Invalid path received [awesome], unable to extract a field name.")); |
| 86 | + } |
| 87 | + |
| 88 | + public void testExtract_ThrowsException_WhenMissingArraySyntax() { |
| 89 | + Map<String, Object> input = Map.of( |
| 90 | + "result", |
| 91 | + Map.of("embeddings", List.of(Map.of("index", 0, "embedding", List.of(1, 2)), Map.of("index", 1, "embedding", List.of(3, 4)))) |
| 92 | + ); |
| 93 | + |
| 94 | + var exception = expectThrows( |
| 95 | + IllegalArgumentException.class, |
| 96 | + // embeddings is missing [*] to indicate that it is an array |
| 97 | + () -> MapPathExtractor.extract(input, "$.result.embeddings.embedding") |
| 98 | + ); |
| 99 | + assertThat( |
| 100 | + exception.getMessage(), |
| 101 | + is( |
| 102 | + "Current path [.embedding] matched the dot field pattern but the current object " |
| 103 | + + "is not a map, found invalid type [List12] instead." |
| 104 | + ) |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + public void testExtract_ThrowsException_WhenHasArraySyntaxButIsAMap() { |
| 109 | + Map<String, Object> input = Map.of( |
| 110 | + "result", |
| 111 | + Map.of("embeddings", List.of(Map.of("index", 0, "embedding", List.of(1, 2)), Map.of("index", 1, "embedding", List.of(3, 4)))) |
| 112 | + ); |
| 113 | + |
| 114 | + var exception = expectThrows( |
| 115 | + IllegalArgumentException.class, |
| 116 | + // result is not an array |
| 117 | + () -> MapPathExtractor.extract(input, "$.result[*].embeddings[*].embedding") |
| 118 | + ); |
| 119 | + assertThat( |
| 120 | + exception.getMessage(), |
| 121 | + is( |
| 122 | + "Current path [[*].embeddings[*].embedding] matched the array field pattern but the current " |
| 123 | + + "object is not a list, found invalid type [Map1] instead." |
| 124 | + ) |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + public void testExtract_ReturnsAnEmptyList_WhenItIsEmpty() { |
| 129 | + Map<String, Object> input = Map.of("result", List.of()); |
| 130 | + |
| 131 | + assertThat(MapPathExtractor.extract(input, "$.result"), is(List.of())); |
| 132 | + } |
| 133 | + |
| 134 | + public void testExtract_ReturnsAnEmptyList_WhenItIsEmpty_PathIncludesArray() { |
| 135 | + Map<String, Object> input = Map.of("result", List.of()); |
| 136 | + |
| 137 | + assertThat(MapPathExtractor.extract(input, "$.result[*]"), is(List.of())); |
| 138 | + } |
| 139 | + |
| 140 | + public void testDotFieldPattern() { |
| 141 | + { |
| 142 | + var matcher = MapPathExtractor.dotFieldPattern.matcher(".abc.123"); |
| 143 | + assertTrue(matcher.matches()); |
| 144 | + assertThat(matcher.group(1), is("abc")); |
| 145 | + assertThat(matcher.group(2), is(".123")); |
| 146 | + } |
| 147 | + { |
| 148 | + var matcher = MapPathExtractor.dotFieldPattern.matcher(".abc[*].123"); |
| 149 | + assertTrue(matcher.matches()); |
| 150 | + assertThat(matcher.group(1), is("abc")); |
| 151 | + assertThat(matcher.group(2), is("[*].123")); |
| 152 | + } |
| 153 | + { |
| 154 | + var matcher = MapPathExtractor.dotFieldPattern.matcher(".abc[.123"); |
| 155 | + assertTrue(matcher.matches()); |
| 156 | + assertThat(matcher.group(1), is("abc")); |
| 157 | + assertThat(matcher.group(2), is("[.123")); |
| 158 | + } |
| 159 | + { |
| 160 | + var matcher = MapPathExtractor.dotFieldPattern.matcher(".abc"); |
| 161 | + assertTrue(matcher.matches()); |
| 162 | + assertThat(matcher.group(1), is("abc")); |
| 163 | + assertThat(matcher.group(2), is("")); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + public void testArrayWildcardPattern() { |
| 168 | + { |
| 169 | + var matcher = MapPathExtractor.arrayWildcardPattern.matcher("[*].abc.123"); |
| 170 | + assertTrue(matcher.matches()); |
| 171 | + assertThat(matcher.group(1), is(".abc.123")); |
| 172 | + } |
| 173 | + { |
| 174 | + var matcher = MapPathExtractor.arrayWildcardPattern.matcher("[*]"); |
| 175 | + assertTrue(matcher.matches()); |
| 176 | + assertThat(matcher.group(1), is("")); |
| 177 | + } |
| 178 | + { |
| 179 | + var matcher = MapPathExtractor.arrayWildcardPattern.matcher("[1].abc"); |
| 180 | + assertFalse(matcher.matches()); |
| 181 | + } |
| 182 | + { |
| 183 | + var matcher = MapPathExtractor.arrayWildcardPattern.matcher("[].abc"); |
| 184 | + assertFalse(matcher.matches()); |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments