|
16 | 16 |
|
17 | 17 | package org.springframework.ai.document;
|
18 | 18 |
|
| 19 | +import java.util.HashMap; |
19 | 20 | import java.util.Map;
|
20 | 21 |
|
21 | 22 | import org.junit.jupiter.api.Test;
|
| 23 | +import org.springframework.ai.document.id.IdGenerator; |
22 | 24 |
|
23 | 25 | import static org.assertj.core.api.Assertions.assertThat;
|
| 26 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; |
| 27 | +import static org.mockito.Mockito.mock; |
| 28 | +import static org.mockito.Mockito.when; |
24 | 29 |
|
25 | 30 | /**
|
26 | 31 | * @author Christian Tzolov
|
@@ -62,4 +67,196 @@ void defaultConfigTextFormatter() {
|
62 | 67 | .isEqualTo(defaultConfigFormatter.format(this.document, MetadataMode.ALL));
|
63 | 68 | }
|
64 | 69 |
|
| 70 | +@Test |
| 71 | +void shouldThrowWhenIdIsNull() { |
| 72 | +assertThatThrownBy(() -> new Document(null, "text", new HashMap<>())) |
| 73 | +.isInstanceOf(IllegalArgumentException.class) |
| 74 | +.hasMessageContaining("id cannot be null or empty"); |
| 75 | +} |
| 76 | + |
| 77 | +@Test |
| 78 | +void shouldThrowWhenIdIsEmpty() { |
| 79 | +assertThatThrownBy(() -> new Document("", "text", new HashMap<>())).isInstanceOf(IllegalArgumentException.class) |
| 80 | +.hasMessageContaining("id cannot be null or empty"); |
| 81 | +} |
| 82 | + |
| 83 | +@Test |
| 84 | +void shouldThrowWhenMetadataIsNull() { |
| 85 | +assertThatThrownBy(() -> new Document("Sample text", null)).isInstanceOf(IllegalArgumentException.class) |
| 86 | +.hasMessageContaining("metadata cannot be null"); |
| 87 | +} |
| 88 | + |
| 89 | +@Test |
| 90 | +void shouldThrowWhenMetadataHasNullKey() { |
| 91 | +Map<String, Object> metadata = new HashMap<>(); |
| 92 | +metadata.put(null, "value"); |
| 93 | + |
| 94 | +assertThatThrownBy(() -> new Document("Sample text", metadata)).isInstanceOf(IllegalArgumentException.class) |
| 95 | +.hasMessageContaining("metadata cannot have null keys"); |
| 96 | +} |
| 97 | + |
| 98 | +@Test |
| 99 | +void shouldThrowWhenMetadataHasNullValue() { |
| 100 | +Map<String, Object> metadata = new HashMap<>(); |
| 101 | +metadata.put("key", null); |
| 102 | + |
| 103 | +assertThatThrownBy(() -> new Document("Sample text", metadata)).isInstanceOf(IllegalArgumentException.class) |
| 104 | +.hasMessageContaining("metadata cannot have null values"); |
| 105 | +} |
| 106 | + |
| 107 | +@Test |
| 108 | +void shouldThrowWhenNeitherTextNorMediaAreSet() { |
| 109 | +assertThatThrownBy(() -> Document.builder().id("test-id").metadata("key", "value").build()) |
| 110 | +.isInstanceOf(IllegalArgumentException.class) |
| 111 | +.hasMessageContaining("exactly one of text or media must be specified"); |
| 112 | +} |
| 113 | + |
| 114 | +@Test |
| 115 | +void builderWithCustomIdGenerator() { |
| 116 | +IdGenerator mockGenerator = mock(IdGenerator.class); |
| 117 | +when(mockGenerator.generateId("test text", Map.of("key", "value"))).thenReturn("generated-id"); |
| 118 | + |
| 119 | +Document document = Document.builder() |
| 120 | +.idGenerator(mockGenerator) |
| 121 | +.text("test text") |
| 122 | +.metadata("key", "value") |
| 123 | +.build(); |
| 124 | + |
| 125 | +assertThat(document.getId()).isEqualTo("generated-id"); |
| 126 | +} |
| 127 | + |
| 128 | +@Test |
| 129 | +void builderShouldThrowWhenIdGeneratorIsNull() { |
| 130 | +assertThatThrownBy(() -> Document.builder().idGenerator(null)).isInstanceOf(IllegalArgumentException.class) |
| 131 | +.hasMessageContaining("idGenerator cannot be null"); |
| 132 | +} |
| 133 | + |
| 134 | +@Test |
| 135 | +void builderShouldThrowWhenMetadataKeyIsNull() { |
| 136 | +assertThatThrownBy(() -> Document.builder().metadata(null, "value")) |
| 137 | +.isInstanceOf(IllegalArgumentException.class) |
| 138 | +.hasMessageContaining("metadata key cannot be null"); |
| 139 | +} |
| 140 | + |
| 141 | +@Test |
| 142 | +void builderShouldThrowWhenMetadataValueIsNull() { |
| 143 | +assertThatThrownBy(() -> Document.builder().metadata("key", null)).isInstanceOf(IllegalArgumentException.class) |
| 144 | +.hasMessageContaining("metadata value cannot be null"); |
| 145 | +} |
| 146 | + |
| 147 | +@Test |
| 148 | +void setCustomContentFormatter() { |
| 149 | +Document document = new Document("Sample text", Map.of()); |
| 150 | +ContentFormatter customFormatter = mock(ContentFormatter.class); |
| 151 | +when(customFormatter.format(document, MetadataMode.ALL)).thenReturn("Custom formatted content"); |
| 152 | + |
| 153 | +document.setContentFormatter(customFormatter); |
| 154 | + |
| 155 | +assertThat(document.getContentFormatter()).isEqualTo(customFormatter); |
| 156 | +assertThat(document.getFormattedContent()).isEqualTo("Custom formatted content"); |
| 157 | +} |
| 158 | + |
| 159 | +@Test |
| 160 | +void shouldThrowWhenFormatterIsNull() { |
| 161 | +Document document = new Document("Sample text", Map.of()); |
| 162 | + |
| 163 | +assertThatThrownBy(() -> document.getFormattedContent(null, MetadataMode.ALL)) |
| 164 | +.isInstanceOf(IllegalArgumentException.class) |
| 165 | +.hasMessageContaining("formatter must not be null"); |
| 166 | +} |
| 167 | + |
| 168 | +@Test |
| 169 | +void shouldThrowWhenMetadataModeIsNull() { |
| 170 | +Document document = new Document("Sample text", Map.of()); |
| 171 | + |
| 172 | +assertThatThrownBy(() -> document.getFormattedContent(null)).isInstanceOf(IllegalArgumentException.class) |
| 173 | +.hasMessageContaining("Metadata mode must not be null"); |
| 174 | +} |
| 175 | + |
| 176 | +@Test |
| 177 | +void mutateTextDocument() { |
| 178 | +Document original = new Document("id", "original text", Map.of("key", "value")); |
| 179 | + |
| 180 | +Document mutated = original.mutate().text("modified text").metadata("newKey", "newValue").score(0.9).build(); |
| 181 | + |
| 182 | +assertThat(mutated.getId()).isEqualTo("id"); |
| 183 | +assertThat(mutated.getText()).isEqualTo("modified text"); |
| 184 | +assertThat(mutated.getMetadata()).containsEntry("newKey", "newValue"); |
| 185 | +assertThat(mutated.getScore()).isEqualTo(0.9); |
| 186 | + |
| 187 | +// Original should be unchanged |
| 188 | +assertThat(original.getText()).isEqualTo("original text"); |
| 189 | +assertThat(original.getScore()).isNull(); |
| 190 | +} |
| 191 | + |
| 192 | +@Test |
| 193 | +void equalDocuments() { |
| 194 | +Map<String, Object> metadata = Map.of("key", "value"); |
| 195 | +Document doc1 = new Document("id", "text", metadata); |
| 196 | +Document doc2 = new Document("id", "text", metadata); |
| 197 | + |
| 198 | +assertThat(doc1).isEqualTo(doc2); |
| 199 | +assertThat(doc1.hashCode()).isEqualTo(doc2.hashCode()); |
| 200 | +} |
| 201 | + |
| 202 | +@Test |
| 203 | +void differentIds() { |
| 204 | +Map<String, Object> metadata = Map.of("key", "value"); |
| 205 | +Document doc1 = new Document("id1", "text", metadata); |
| 206 | +Document doc2 = new Document("id2", "text", metadata); |
| 207 | + |
| 208 | +assertThat(doc1).isNotEqualTo(doc2); |
| 209 | +} |
| 210 | + |
| 211 | +@Test |
| 212 | +void differentText() { |
| 213 | +Map<String, Object> metadata = Map.of("key", "value"); |
| 214 | +Document doc1 = new Document("id", "text1", metadata); |
| 215 | +Document doc2 = new Document("id", "text2", metadata); |
| 216 | + |
| 217 | +assertThat(doc1).isNotEqualTo(doc2); |
| 218 | +} |
| 219 | + |
| 220 | +@Test |
| 221 | +void isTextReturnsTrueForTextDocument() { |
| 222 | +Document document = new Document("Sample text", Map.of()); |
| 223 | +assertThat(document.isText()).isTrue(); |
| 224 | +assertThat(document.getText()).isNotNull(); |
| 225 | +assertThat(document.getMedia()).isNull(); |
| 226 | +} |
| 227 | + |
| 228 | +@Test |
| 229 | +void scoreHandling() { |
| 230 | +Document document = Document.builder().text("test").score(0.85).build(); |
| 231 | + |
| 232 | +assertThat(document.getScore()).isEqualTo(0.85); |
| 233 | + |
| 234 | +Document documentWithoutScore = new Document("test"); |
| 235 | +assertThat(documentWithoutScore.getScore()).isNull(); |
| 236 | +} |
| 237 | + |
| 238 | +@Test |
| 239 | +void metadataImmutability() { |
| 240 | +Map<String, Object> originalMetadata = new HashMap<>(); |
| 241 | +originalMetadata.put("key", "value"); |
| 242 | + |
| 243 | +Document document = new Document("test", originalMetadata); |
| 244 | + |
| 245 | +// Modify original map |
| 246 | +originalMetadata.put("newKey", "newValue"); |
| 247 | + |
| 248 | +// Document's metadata should not be affected |
| 249 | +assertThat(document.getMetadata()).hasSize(1); |
| 250 | +assertThat(document.getMetadata()).containsEntry("key", "value"); |
| 251 | +assertThat(document.getMetadata()).doesNotContainKey("newKey"); |
| 252 | +} |
| 253 | + |
| 254 | +@Test |
| 255 | +void builderWithMetadataMap() { |
| 256 | +Map<String, Object> metadata = Map.of("key1", "value1", "key2", 1); |
| 257 | +Document document = Document.builder().text("test").metadata(metadata).build(); |
| 258 | + |
| 259 | +assertThat(document.getMetadata()).containsExactlyInAnyOrderEntriesOf(metadata); |
| 260 | +} |
| 261 | + |
65 | 262 | }
|
0 commit comments