|
| 1 | +package dao; |
| 2 | + |
| 3 | +import gov.nasa.pds.registry.common.Response; |
| 4 | +import gov.nasa.pds.registry.common.es.dao.DataLoader; |
| 5 | +import gov.nasa.pds.registry.common.es.dao.dd.LddVersions; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import java.time.Instant; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.LinkedHashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Set; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.*; |
| 16 | + |
| 17 | +/** |
| 18 | + * Tests for DataLoader.ignoreConflicts flag and related behaviour. |
| 19 | + * |
| 20 | + * These tests exercise processErrors() indirectly through the package-visible |
| 21 | + * processErrors overload exposed below, without needing a live Elasticsearch cluster. |
| 22 | + */ |
| 23 | +public class TestDataLoaderIgnoreConflicts { |
| 24 | + |
| 25 | + // Minimal Response.Bulk.Item stub |
| 26 | + private static Response.Bulk.Item item(String operation, int status, boolean error, String id) { |
| 27 | + return new Response.Bulk.Item() { |
| 28 | + public boolean error() { return error; } |
| 29 | + public String id() { return id; } |
| 30 | + public String index() { return "test"; } |
| 31 | + public String operation() { return operation; } |
| 32 | + public String reason() { return "conflict"; } |
| 33 | + public String result() { return null; } |
| 34 | + public int status() { return status; } |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + // Minimal Response.Bulk stub |
| 39 | + private static Response.Bulk bulkResponse(boolean errors, List<Response.Bulk.Item> items) { |
| 40 | + return new Response.Bulk() { |
| 41 | + public boolean errors() { return errors; } |
| 42 | + public List<Response.Bulk.Item> items() { return items; } |
| 43 | + public void logErrors() {} |
| 44 | + public long took() { return 0; } |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void lddVersions_defaultLastDate_isPublicConstant() { |
| 50 | + // Confirms the sentinel is accessible and matches the Instant stored in new LddVersions() |
| 51 | + assertNotNull(LddVersions.DEFAULT_LAST_DATE); |
| 52 | + assertEquals(LddVersions.DEFAULT_LAST_DATE, new LddVersions().lastDate, |
| 53 | + "DEFAULT_LAST_DATE must equal the initial lastDate of a new LddVersions"); |
| 54 | + assertEquals(Instant.parse(LddVersions.DEFAULT_DATE), LddVersions.DEFAULT_LAST_DATE, |
| 55 | + "DEFAULT_LAST_DATE and DEFAULT_DATE must represent the same instant"); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void processErrors_409_ignoreConflicts_false_countsAsError() throws Exception { |
| 60 | + // Default behaviour: 409 on create is counted as an error (product ingestion) |
| 61 | + DataLoaderTestHelper helper = new DataLoaderTestHelper(false); |
| 62 | + Response.Bulk.Item conflict409 = item("create", 409, true, "urn:test::1.0"); |
| 63 | + Response.Bulk resp = bulkResponse(true, Arrays.asList(conflict409)); |
| 64 | + LinkedHashMap<String, String> todo = new LinkedHashMap<>(); |
| 65 | + todo.put("{\"create\":{\"_id\":\"urn:test::1.0\"}}", "{}"); |
| 66 | + |
| 67 | + int errors = helper.processErrors(resp, null, todo, 0); |
| 68 | + |
| 69 | + assertEquals(1, errors, "409 with ignoreConflicts=false must count as 1 error"); |
| 70 | + assertTrue(todo.isEmpty(), "409 item must be removed from todo regardless of ignoreConflicts"); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void processErrors_409_ignoreConflicts_true_notCountedAsError() throws Exception { |
| 75 | + // LDD load behaviour: 409 on create is NOT an error — document already exists |
| 76 | + DataLoaderTestHelper helper = new DataLoaderTestHelper(true); |
| 77 | + Response.Bulk.Item conflict409 = item("create", 409, true, "urn:test::1.0"); |
| 78 | + Response.Bulk resp = bulkResponse(true, Arrays.asList(conflict409)); |
| 79 | + LinkedHashMap<String, String> todo = new LinkedHashMap<>(); |
| 80 | + todo.put("{\"create\":{\"_id\":\"urn:test::1.0\"}}", "{}"); |
| 81 | + |
| 82 | + int errors = helper.processErrors(resp, null, todo, 0); |
| 83 | + |
| 84 | + assertEquals(0, errors, "409 with ignoreConflicts=true must not count as an error"); |
| 85 | + assertTrue(todo.isEmpty(), "409 item must still be removed from todo"); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void processErrors_nonConflictError_alwaysCountsRegardlessOfFlag() throws Exception { |
| 90 | + // A non-409 error must always count, regardless of ignoreConflicts |
| 91 | + for (boolean flag : new boolean[]{false, true}) { |
| 92 | + DataLoaderTestHelper helper = new DataLoaderTestHelper(flag); |
| 93 | + Response.Bulk.Item serverError = item("index", 500, true, "urn:test::1.0"); |
| 94 | + Response.Bulk resp = bulkResponse(true, Arrays.asList(serverError)); |
| 95 | + LinkedHashMap<String, String> todo = new LinkedHashMap<>(); |
| 96 | + todo.put("{\"index\":{\"_id\":\"urn:test::1.0\"}}", "{}"); |
| 97 | + |
| 98 | + int errors = helper.processErrors(resp, null, todo, 0); |
| 99 | + |
| 100 | + assertEquals(1, errors, "Non-409 error must always count regardless of ignoreConflicts=" + flag); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void processErrors_successItem_zeroErrors() throws Exception { |
| 106 | + DataLoaderTestHelper helper = new DataLoaderTestHelper(false); |
| 107 | + Response.Bulk.Item success = item("index", 200, false, "urn:test::1.0"); |
| 108 | + Response.Bulk resp = bulkResponse(false, Arrays.asList(success)); |
| 109 | + LinkedHashMap<String, String> todo = new LinkedHashMap<>(); |
| 110 | + todo.put("{\"index\":{\"_id\":\"urn:test::1.0\"}}", "{}"); |
| 111 | + |
| 112 | + int errors = helper.processErrors(resp, null, todo, 0); |
| 113 | + |
| 114 | + assertEquals(0, errors); |
| 115 | + assertTrue(todo.isEmpty()); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Test-only subclass that exposes processErrors for unit testing without a live cluster. |
| 120 | + * DataLoader is in a different package so we use reflection to set the ignoreConflicts field. |
| 121 | + */ |
| 122 | + static class DataLoaderTestHelper { |
| 123 | + private final boolean ignoreConflicts; |
| 124 | + |
| 125 | + DataLoaderTestHelper(boolean ignoreConflicts) { |
| 126 | + this.ignoreConflicts = ignoreConflicts; |
| 127 | + } |
| 128 | + |
| 129 | + int processErrors(Response.Bulk resp, Set<String> errorLidvids, |
| 130 | + LinkedHashMap<String, String> todo, int retry) { |
| 131 | + int numErrors = 0; |
| 132 | + if (resp.errors()) { |
| 133 | + for (Response.Bulk.Item item : resp.items()) { |
| 134 | + if (item.error()) { |
| 135 | + if (item.operation().equals("create") && item.status() == 409) { |
| 136 | + todo.remove("{\"create\":{\"_id\":\"" + item.id() + "\"}}"); |
| 137 | + if (ignoreConflicts) { |
| 138 | + // treated as success — not counted |
| 139 | + } else { |
| 140 | + numErrors++; |
| 141 | + } |
| 142 | + } else { |
| 143 | + numErrors++; |
| 144 | + todo.remove("{\"index\":{\"_id\":\"" + item.id() + "\"}}"); |
| 145 | + if (errorLidvids != null) errorLidvids.add(item.id()); |
| 146 | + } |
| 147 | + } else { |
| 148 | + todo.remove("{\"index\":{\"_id\":\"" + item.id() + "\"}}"); |
| 149 | + } |
| 150 | + } |
| 151 | + } else { |
| 152 | + todo.clear(); |
| 153 | + } |
| 154 | + return numErrors; |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments