/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ package org.opensearch.securityanalytics.mapper.action.mapping; import org.opensearch.cluster.metadata.MappingMetadata; import org.opensearch.common.io.stream.BytesStreamOutput; import org.opensearch.common.io.stream.StreamInput; import org.opensearch.index.mapper.MapperService; import org.opensearch.securityanalytics.action.GetIndexMappingsResponse; import org.opensearch.test.OpenSearchTestCase; import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Objects; public class GetIndexMappingsResponseTests extends OpenSearchTestCase { public void testStreamInOut() throws IOException { BytesStreamOutput out = new BytesStreamOutput(); Map mappings = new HashMap<>(); mappings.put("my_index", createMappingsForIndex()); GetIndexMappingsResponse response = new GetIndexMappingsResponse(mappings); response.writeTo(out); StreamInput sin = StreamInput.wrap(out.bytes().toBytesRef().bytes); GetIndexMappingsResponse newRes = new GetIndexMappingsResponse(sin); assertTrue(newRes.getMappings().containsKey("my_index")); } private static MappingMetadata createMappingsForIndex() { int typeCount = rarely() ? 0 : 1; MappingMetadata mmd = new MappingMetadata(MapperService.SINGLE_MAPPING_NAME, Collections.emptyMap()); for (int i = 0; i < typeCount; i++) { if (rarely() == false) { // rarely have no fields Map mappings = new HashMap<>(); mappings.put("field-" + i, randomFieldMapping()); if (randomBoolean()) { mappings.put("field2-" + i, randomFieldMapping()); } String typeName = MapperService.SINGLE_MAPPING_NAME; mmd = new MappingMetadata(typeName, mappings); } } return mmd; } private static Map randomFieldMapping() { Map mappings = new HashMap<>(); if (randomBoolean()) { mappings.put("type", randomBoolean() ? "text" : "keyword"); mappings.put("index", "analyzed"); mappings.put("analyzer", "english"); } else if (randomBoolean()) { mappings.put("type", randomFrom("integer", "float", "long", "double")); mappings.put("index", Objects.toString(randomBoolean())); } else if (randomBoolean()) { mappings.put("type", "object"); mappings.put("dynamic", "strict"); Map properties = new HashMap<>(); Map props1 = new HashMap<>(); props1.put("type", randomFrom("text", "keyword")); props1.put("analyzer", "keyword"); properties.put("subtext", props1); Map props2 = new HashMap<>(); props2.put("type", "object"); Map prop2properties = new HashMap<>(); Map props3 = new HashMap<>(); props3.put("type", "integer"); props3.put("index", "false"); prop2properties.put("subsubfield", props3); props2.put("properties", prop2properties); mappings.put("properties", properties); } else { mappings.put("type", "keyword"); } return mappings; } }