|
| 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", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.index.mapper; |
| 11 | + |
| 12 | +import org.elasticsearch.common.Strings; |
| 13 | +import org.elasticsearch.test.ESTestCase; |
| 14 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 15 | +import org.elasticsearch.xcontent.XContentType; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.stream.Collectors; |
| 21 | + |
| 22 | +public class StringStoredFieldFieldLoaderTests extends ESTestCase { |
| 23 | + |
| 24 | + public void testLoadStoredFieldAndReset() throws IOException { |
| 25 | + var sfl = new StringStoredFieldFieldLoader("foo", "foo") { |
| 26 | + @Override |
| 27 | + protected void write(XContentBuilder b, Object value) throws IOException { |
| 28 | + b.value((String) value); |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + var storedFieldLoaders = sfl.storedFieldLoaders().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 33 | + storedFieldLoaders.get("foo").load(List.of("one")); |
| 34 | + |
| 35 | + var result = XContentBuilder.builder(XContentType.JSON.xContent()); |
| 36 | + result.startObject(); |
| 37 | + sfl.write(result); |
| 38 | + result.endObject(); |
| 39 | + |
| 40 | + assertEquals(""" |
| 41 | + {"foo":"one"}""", Strings.toString(result)); |
| 42 | + |
| 43 | + var empty = XContentBuilder.builder(XContentType.JSON.xContent()); |
| 44 | + empty.startObject(); |
| 45 | + // reset() should have been called after previous write |
| 46 | + sfl.write(empty); |
| 47 | + empty.endObject(); |
| 48 | + |
| 49 | + assertEquals("{}", Strings.toString(empty)); |
| 50 | + } |
| 51 | + |
| 52 | +} |
0 commit comments