|
| 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.ingest.common; |
| 11 | + |
| 12 | +import org.elasticsearch.ingest.IngestDocument; |
| 13 | +import org.elasticsearch.ingest.Processor; |
| 14 | +import org.elasticsearch.test.ESTestCase; |
| 15 | + |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +import static org.hamcrest.Matchers.containsString; |
| 20 | +import static org.hamcrest.Matchers.equalTo; |
| 21 | +import static org.hamcrest.Matchers.is; |
| 22 | + |
| 23 | +public class RemediateProcessorTests extends ESTestCase { |
| 24 | + |
| 25 | + private static Processor createRemediateProcessor() { |
| 26 | + return new RemediateProcessor(randomAlphaOfLength(8), null); |
| 27 | + } |
| 28 | + |
| 29 | + private static IngestDocument createFailureDoc( |
| 30 | + String currentIndex, |
| 31 | + String originalIndex, |
| 32 | + String routing, |
| 33 | + Map<String, Object> originalSource |
| 34 | + ) { |
| 35 | + Map<String, Object> sourceAndMetadata = new HashMap<>(); |
| 36 | + // current metadata |
| 37 | + sourceAndMetadata.put("_index", currentIndex); |
| 38 | + sourceAndMetadata.put("_id", "test-id"); |
| 39 | + if (randomBoolean()) { |
| 40 | + sourceAndMetadata.put("_version", 1); |
| 41 | + } |
| 42 | + |
| 43 | + // failure wrapper |
| 44 | + Map<String, Object> documentWrapper = new HashMap<>(); |
| 45 | + documentWrapper.put("index", originalIndex); |
| 46 | + if (routing != null) { |
| 47 | + documentWrapper.put("routing", routing); |
| 48 | + } |
| 49 | + documentWrapper.put("source", originalSource); |
| 50 | + |
| 51 | + sourceAndMetadata.put("error", "simulated failure"); |
| 52 | + sourceAndMetadata.put("document", documentWrapper); |
| 53 | + |
| 54 | + // no special ingest-metadata |
| 55 | + Map<String, Object> ingestMetadata = new HashMap<>(); |
| 56 | + |
| 57 | + return new IngestDocument(sourceAndMetadata, ingestMetadata); |
| 58 | + } |
| 59 | + |
| 60 | + public void testRemediate_basic() throws Exception { |
| 61 | + Processor processor = createRemediateProcessor(); |
| 62 | + |
| 63 | + Map<String, Object> originalSource = new HashMap<>(); |
| 64 | + originalSource.put("a", 1); |
| 65 | + originalSource.put("b", "x"); |
| 66 | + |
| 67 | + IngestDocument doc = createFailureDoc("failure-index", "orig-index", null, originalSource); |
| 68 | + |
| 69 | + processor.execute(doc); |
| 70 | + |
| 71 | + // metadata index restored |
| 72 | + assertThat(doc.getFieldValue("_index", String.class), equalTo("orig-index")); |
| 73 | + // routing is not set when not provided |
| 74 | + assertThat(doc.hasField("_routing", true), is(false)); |
| 75 | + |
| 76 | + // source restored at root |
| 77 | + assertThat(doc.getFieldValue("a", Integer.class), equalTo(1)); |
| 78 | + assertThat(doc.getFieldValue("b", String.class), equalTo("x")); |
| 79 | + |
| 80 | + // failure scaffolding removed |
| 81 | + assertThat(doc.hasField("error", true), is(false)); |
| 82 | + assertThat(doc.hasField("document", true), is(false)); |
| 83 | + } |
| 84 | + |
| 85 | + public void testRemediate_withRouting() throws Exception { |
| 86 | + Processor processor = createRemediateProcessor(); |
| 87 | + |
| 88 | + Map<String, Object> originalSource = new HashMap<>(); |
| 89 | + originalSource.put("nested", Map.of("k", "v")); |
| 90 | + |
| 91 | + IngestDocument doc = createFailureDoc("dlq-index", "original-idx", "orig-route", originalSource); |
| 92 | + |
| 93 | + processor.execute(doc); |
| 94 | + |
| 95 | + assertThat(doc.getFieldValue("_index", String.class), equalTo("original-idx")); |
| 96 | + assertThat(doc.getFieldValue("_routing", String.class), equalTo("orig-route")); |
| 97 | + assertThat(doc.getFieldValue("nested.k", String.class), equalTo("v")); |
| 98 | + assertThat(doc.hasField("error", true), is(false)); |
| 99 | + assertThat(doc.hasField("document", true), is(false)); |
| 100 | + } |
| 101 | + |
| 102 | + public void testRemediate_missingDocument_throws() { |
| 103 | + Processor processor = createRemediateProcessor(); |
| 104 | + |
| 105 | + Map<String, Object> sourceAndMetadata = new HashMap<>(); |
| 106 | + sourceAndMetadata.put("_index", "failure-index"); |
| 107 | + sourceAndMetadata.put("error", "simulated failure"); |
| 108 | + // no "document" field |
| 109 | + |
| 110 | + IngestDocument doc = new IngestDocument(sourceAndMetadata, new HashMap<>()); |
| 111 | + |
| 112 | + Exception e = expectThrows(IllegalArgumentException.class, () -> processor.execute(doc)); |
| 113 | + assertThat(e.getMessage(), containsString("document")); |
| 114 | + } |
| 115 | + |
| 116 | + public void testRemediate_missingSourceInsideDocument_throws() { |
| 117 | + Processor processor = createRemediateProcessor(); |
| 118 | + |
| 119 | + Map<String, Object> sourceAndMetadata = new HashMap<>(); |
| 120 | + sourceAndMetadata.put("_index", "failure-index"); |
| 121 | + sourceAndMetadata.put("error", "simulated failure"); |
| 122 | + sourceAndMetadata.put("document", Map.of("index", "orig-index")); // missing "source" |
| 123 | + |
| 124 | + IngestDocument doc = new IngestDocument(sourceAndMetadata, new HashMap<>()); |
| 125 | + |
| 126 | + Exception e = expectThrows(IllegalArgumentException.class, () -> processor.execute(doc)); |
| 127 | + assertThat(e.getMessage(), containsString("source")); |
| 128 | + } |
| 129 | +} |
0 commit comments