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