|
11 | 11 | import org.junit.jupiter.api.Test; |
12 | 12 | import org.junit.jupiter.params.ParameterizedTest; |
13 | 13 | import org.junit.jupiter.params.provider.ValueSource; |
| 14 | +import org.opensearch.dataprepper.model.codec.OutputCodec; |
14 | 15 | import org.opensearch.dataprepper.model.event.Event; |
15 | 16 | import org.opensearch.dataprepper.model.log.JacksonLog; |
16 | 17 | import org.opensearch.dataprepper.model.sink.OutputCodecContext; |
@@ -148,6 +149,120 @@ void writeEvent_with_exclude_keys(final int numberOfRecords) throws IOException |
148 | 149 | assertThat(index, equalTo(numberOfRecords)); |
149 | 150 | } |
150 | 151 |
|
| 152 | + |
| 153 | + @ParameterizedTest |
| 154 | + @ValueSource(ints = {1, 2, 10, 100}) |
| 155 | + void writer_happy_case(final int numberOfRecords) throws IOException { |
| 156 | + JsonOutputCodec jsonOutputCodec = createObjectUnderTest(); |
| 157 | + outputStream = new ByteArrayOutputStream(); |
| 158 | + OutputCodecContext codecContext = new OutputCodecContext(); |
| 159 | + OutputCodec.Writer objectUnderTest = jsonOutputCodec.createWriter(outputStream, null, codecContext); |
| 160 | + |
| 161 | + final List<Map<String, Object>> expectedData = generateRecords(numberOfRecords); |
| 162 | + for (int index = 0; index < numberOfRecords; index++) { |
| 163 | + final Event event = convertToEvent(expectedData.get(index)); |
| 164 | + objectUnderTest.writeEvent(event); |
| 165 | + } |
| 166 | + objectUnderTest.complete(); |
| 167 | + |
| 168 | + int index = 0; |
| 169 | + ObjectMapper mapper = new ObjectMapper(); |
| 170 | + JsonNode jsonNode = mapper.readTree(outputStream.toByteArray()); |
| 171 | + assertThat(jsonNode.getNodeType(), equalTo(JsonNodeType.OBJECT)); |
| 172 | + Map.Entry<String, JsonNode> nextField = jsonNode.fields().next(); |
| 173 | + assertThat(nextField, notNullValue()); |
| 174 | + assertThat(nextField.getKey(), equalTo(JsonOutputCodecConfig.DEFAULT_KEY_NAME)); |
| 175 | + jsonNode = nextField.getValue(); |
| 176 | + assertThat(jsonNode, notNullValue()); |
| 177 | + assertThat(jsonNode.getNodeType(), equalTo(JsonNodeType.ARRAY)); |
| 178 | + for (JsonNode actualElement : jsonNode) { |
| 179 | + Map<String, Object> expectedMap = expectedData.get(index); |
| 180 | + Set<String> keys = expectedMap.keySet(); |
| 181 | + Map<String, Object> actualMap = new HashMap<>(); |
| 182 | + for (String key : keys) { |
| 183 | + actualMap.put(key, getValue(actualElement.get(key))); |
| 184 | + } |
| 185 | + assertThat(actualMap, equalTo(expectedMap)); |
| 186 | + index++; |
| 187 | + } |
| 188 | + |
| 189 | + assertThat(index, equalTo(numberOfRecords)); |
| 190 | + } |
| 191 | + |
| 192 | + @ParameterizedTest |
| 193 | + @ValueSource(ints = {1, 2, 10, 100}) |
| 194 | + void writer_writeEvent_with_include_keys(final int numberOfRecords) throws IOException { |
| 195 | + JsonOutputCodec jsonOutputCodec = createObjectUnderTest(); |
| 196 | + outputStream = new ByteArrayOutputStream(); |
| 197 | + OutputCodecContext codecContext = new OutputCodecContext(null, List.of("name"), null); |
| 198 | + OutputCodec.Writer objectUnderTest = jsonOutputCodec.createWriter(outputStream, null, codecContext); |
| 199 | + |
| 200 | + final List<Map<String, Object>> expectedData = generateRecords(numberOfRecords); |
| 201 | + for (int index = 0; index < numberOfRecords; index++) { |
| 202 | + final Event event = convertToEvent(expectedData.get(index)); |
| 203 | + objectUnderTest.writeEvent(event); |
| 204 | + } |
| 205 | + objectUnderTest.complete(); |
| 206 | + |
| 207 | + int index = 0; |
| 208 | + ObjectMapper mapper = new ObjectMapper(); |
| 209 | + JsonNode jsonNode = mapper.readTree(outputStream.toByteArray()); |
| 210 | + assertThat(jsonNode.getNodeType(), equalTo(JsonNodeType.OBJECT)); |
| 211 | + Map.Entry<String, JsonNode> nextField = jsonNode.fields().next(); |
| 212 | + assertThat(nextField, notNullValue()); |
| 213 | + assertThat(nextField.getKey(), equalTo(JsonOutputCodecConfig.DEFAULT_KEY_NAME)); |
| 214 | + jsonNode = nextField.getValue(); |
| 215 | + assertThat(jsonNode, notNullValue()); |
| 216 | + assertThat(jsonNode.getNodeType(), equalTo(JsonNodeType.ARRAY)); |
| 217 | + for (JsonNode actualElement : jsonNode) { |
| 218 | + Map<String, Object> expectedMap = expectedData.get(index); |
| 219 | + assertThat(actualElement.has("age"), equalTo(false)); |
| 220 | + assertThat(actualElement.has("name"), equalTo(true)); |
| 221 | + assertThat(actualElement.get("name").getNodeType(), equalTo(JsonNodeType.STRING)); |
| 222 | + assertThat(actualElement.get("name").asText(), equalTo(expectedMap.get("name"))); |
| 223 | + index++; |
| 224 | + } |
| 225 | + |
| 226 | + assertThat(index, equalTo(numberOfRecords)); |
| 227 | + } |
| 228 | + |
| 229 | + @ParameterizedTest |
| 230 | + @ValueSource(ints = {1, 2, 10, 100}) |
| 231 | + void writer_writeEvent_with_exclude_keys(final int numberOfRecords) throws IOException { |
| 232 | + JsonOutputCodec jsonOutputCodec = createObjectUnderTest(); |
| 233 | + outputStream = new ByteArrayOutputStream(); |
| 234 | + OutputCodecContext codecContext = new OutputCodecContext(null, null, List.of("age")); |
| 235 | + OutputCodec.Writer objectUnderTest = jsonOutputCodec.createWriter(outputStream, null, codecContext); |
| 236 | + |
| 237 | + final List<Map<String, Object>> expectedData = generateRecords(numberOfRecords); |
| 238 | + for (int index = 0; index < numberOfRecords; index++) { |
| 239 | + final Event event = convertToEvent(expectedData.get(index)); |
| 240 | + objectUnderTest.writeEvent(event); |
| 241 | + } |
| 242 | + objectUnderTest.complete(); |
| 243 | + |
| 244 | + int index = 0; |
| 245 | + ObjectMapper mapper = new ObjectMapper(); |
| 246 | + JsonNode jsonNode = mapper.readTree(outputStream.toByteArray()); |
| 247 | + assertThat(jsonNode.getNodeType(), equalTo(JsonNodeType.OBJECT)); |
| 248 | + Map.Entry<String, JsonNode> nextField = jsonNode.fields().next(); |
| 249 | + assertThat(nextField, notNullValue()); |
| 250 | + assertThat(nextField.getKey(), equalTo(JsonOutputCodecConfig.DEFAULT_KEY_NAME)); |
| 251 | + jsonNode = nextField.getValue(); |
| 252 | + assertThat(jsonNode, notNullValue()); |
| 253 | + assertThat(jsonNode.getNodeType(), equalTo(JsonNodeType.ARRAY)); |
| 254 | + for (JsonNode actualElement : jsonNode) { |
| 255 | + Map<String, Object> expectedMap = expectedData.get(index); |
| 256 | + assertThat(actualElement.has("age"), equalTo(false)); |
| 257 | + assertThat(actualElement.has("name"), equalTo(true)); |
| 258 | + assertThat(actualElement.get("name").getNodeType(), equalTo(JsonNodeType.STRING)); |
| 259 | + assertThat(actualElement.get("name").asText(), equalTo(expectedMap.get("name"))); |
| 260 | + index++; |
| 261 | + } |
| 262 | + |
| 263 | + assertThat(index, equalTo(numberOfRecords)); |
| 264 | + } |
| 265 | + |
151 | 266 | @Test |
152 | 267 | void testGetEstimatedSize() throws Exception { |
153 | 268 | int numberOfRecords = 1; |
|
0 commit comments