|
| 1 | +package com.openelements.content; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import com.openelements.spring.base.services.search.MeilisearchProperties; |
| 6 | +import java.time.Duration; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.concurrent.atomic.AtomicInteger; |
| 11 | +import java.util.stream.IntStream; |
| 12 | +import java.util.stream.Stream; |
| 13 | +import org.junit.jupiter.api.DisplayName; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.springframework.util.unit.DataSize; |
| 16 | + |
| 17 | +/** |
| 18 | + * Unit tests for {@link ContentBootstrapStep} using a real {@link ContentIndexer} driven by a |
| 19 | + * test-double strategy, so document streaming is exercised without a running Meilisearch. |
| 20 | + * |
| 21 | + * <p>Readiness toggling and batching are the library {@code MeilisearchBootstrapRunner}'s |
| 22 | + * responsibility and require a live instance; they are not reproduced here. |
| 23 | + */ |
| 24 | +@DisplayName("Content bootstrap step") |
| 25 | +class ContentBootstrapStepTest { |
| 26 | + |
| 27 | + private final MeilisearchProperties meilisearch = |
| 28 | + new MeilisearchProperties("http://localhost:7700", "", "content_", Duration.ofSeconds(10)); |
| 29 | + private final StubStrategy strategy = new StubStrategy(); |
| 30 | + private final ContentIndexer indexer = |
| 31 | + new ContentIndexer(new SourceStrategyRegistry(List.of(strategy)), new NoOpStore()); |
| 32 | + |
| 33 | + private static ContentSource source(String id, boolean enabled) { |
| 34 | + return new ContentSource( |
| 35 | + id, SourceType.WEBSITE, "https://ex.com", List.of(), List.of("/**"), List.of(), "article", List.of(), enabled); |
| 36 | + } |
| 37 | + |
| 38 | + private static ContentSourceProperties properties(ContentSource... sources) { |
| 39 | + return new ContentSourceProperties( |
| 40 | + true, null, "UA", 2.0, Duration.ofSeconds(10), DataSize.ofMegabytes(5), List.of(sources)); |
| 41 | + } |
| 42 | + |
| 43 | + private ContentBootstrapStep step(ContentSourceProperties properties) { |
| 44 | + return new ContentBootstrapStep(properties, indexer, meilisearch); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + @DisplayName("indexUid targets the prefixed content index") |
| 49 | + void indexUidTargetsContentIndex() { |
| 50 | + assertThat(step(properties()).indexUid()).isEqualTo("content_content"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + @DisplayName("only enabled sources contribute documents") |
| 55 | + void onlyEnabledSourcesContribute() { |
| 56 | + ContentBootstrapStep step = step(properties(source("a", true), source("b", true), source("c", false))); |
| 57 | + |
| 58 | + List<String> sources = step.documents().map(doc -> (String) doc.get("source")).toList(); |
| 59 | + |
| 60 | + assertThat(sources).containsExactlyInAnyOrder("a", "b"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + @DisplayName("the document stream is lazy — nothing is fetched until it is consumed") |
| 65 | + void streamIsLazy() { |
| 66 | + strategy.itemsPerSource = 3; |
| 67 | + ContentBootstrapStep step = step(properties(source("a", true))); |
| 68 | + |
| 69 | + Stream<Map<String, Object>> documents = step.documents(); |
| 70 | + assertThat(strategy.fetchCount.get()).isZero(); // building the stream fetches nothing |
| 71 | + |
| 72 | + List<Map<String, Object>> materialized = documents.toList(); |
| 73 | + assertThat(materialized).hasSize(3); |
| 74 | + assertThat(strategy.fetchCount.get()).isEqualTo(3); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + @DisplayName("a source that fails while streaming does not block the others") |
| 79 | + void failingSourceDoesNotBlockOthers() { |
| 80 | + strategy.throwingSourceIds.add("bad"); |
| 81 | + ContentBootstrapStep step = step(properties(source("a", true), source("bad", true))); |
| 82 | + |
| 83 | + List<String> sources = step.documents().map(doc -> (String) doc.get("source")).toList(); |
| 84 | + |
| 85 | + assertThat(sources).containsExactly("a"); |
| 86 | + } |
| 87 | + |
| 88 | + /** Test-double strategy: one INDEX document per discovered item; can throw for chosen sources. */ |
| 89 | + private static final class StubStrategy implements ContentSourceStrategy { |
| 90 | + private final List<String> throwingSourceIds = new ArrayList<>(); |
| 91 | + private final AtomicInteger fetchCount = new AtomicInteger(); |
| 92 | + private int itemsPerSource = 1; |
| 93 | + |
| 94 | + @Override |
| 95 | + public SourceType type() { |
| 96 | + return SourceType.WEBSITE; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public List<DiscoveredItem> discover(ContentSource source) { |
| 101 | + if (throwingSourceIds.contains(source.id())) { |
| 102 | + throw new IllegalStateException("boom " + source.id()); |
| 103 | + } |
| 104 | + return IntStream.range(0, itemsPerSource) |
| 105 | + .mapToObj(i -> new DiscoveredItem("https://ex.com/" + source.id() + "/" + i, "L")) |
| 106 | + .toList(); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public FetchOutcome fetch(ContentSource source, DiscoveredItem item) { |
| 111 | + fetchCount.incrementAndGet(); |
| 112 | + ContentDocument document = new ContentDocument( |
| 113 | + ContentDocument.id(source.id(), item.url()), source.id(), "en", item.url(), |
| 114 | + "T", "E", "body", "a", List.of(), "2026-01-01", item.lastmod(), null); |
| 115 | + return FetchOutcome.index(document); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** streamAllDocuments never touches the store, so a no-op suffices. */ |
| 120 | + private static final class NoOpStore implements ContentIndexStore { |
| 121 | + @Override |
| 122 | + public Map<String, StoredDocument> loadState(String source) { |
| 123 | + return Map.of(); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public int upsert(List<Map<String, Object>> documents) { |
| 128 | + return documents.size(); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void delete(String id) { |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments