Skip to content

Commit 6702e5e

Browse files
committed
Add unit tests.
Signed-off-by: Youssef Aouichaoui <[email protected]>
1 parent 869ffcd commit 6702e5e

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

src/test/java/org/springframework/data/elasticsearch/core/index/MappingBuilderUnitTests.java

+41
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,38 @@ void shouldUseCustomMappedNameMultiField() throws JSONException {
12961296
assertEquals(expected, mapping, true);
12971297
}
12981298

1299+
@Test // #2952
1300+
void shouldMapNullityParameters() throws JSONException {
1301+
// Given
1302+
String expected = """
1303+
{
1304+
"properties": {
1305+
"_class": {
1306+
"type": "keyword",
1307+
"index": false,
1308+
"doc_values": false
1309+
},
1310+
"empty-field": {
1311+
"type": "keyword",
1312+
"null_value": "EMPTY",
1313+
"fields": {
1314+
"suffix": {
1315+
"type": "keyword",
1316+
"null_value": "EMPTY_TEXT"
1317+
}
1318+
}
1319+
}
1320+
}
1321+
}
1322+
""";
1323+
1324+
// When
1325+
String result = getMappingBuilder().buildPropertyMapping(MultiFieldWithNullEmptyParameters.class);
1326+
1327+
// Then
1328+
assertEquals(expected, result, true);
1329+
}
1330+
12991331
// region entities
13001332

13011333
@Document(indexName = "ignore-above-index")
@@ -2570,5 +2602,14 @@ private static class MultiFieldMappedNameEntity {
25702602
@MultiField(mainField = @Field(type = FieldType.Text, mappedTypeName = "match_only_text"), otherFields = { @InnerField(suffix = "lower_case",
25712603
type = FieldType.Keyword, normalizer = "lower_case_normalizer", mappedTypeName = "constant_keyword") }) private String description;
25722604
}
2605+
2606+
@SuppressWarnings("unused")
2607+
private static class MultiFieldWithNullEmptyParameters {
2608+
@Nullable
2609+
@MultiField(
2610+
mainField = @Field(name = "empty-field", type = FieldType.Keyword, nullValue = "EMPTY", storeNullValue = true),
2611+
otherFields = {
2612+
@InnerField(suffix = "suffix", type = Keyword, nullValue = "EMPTY_TEXT") }) private List<String> emptyField;
2613+
}
25732614
// endregion
25742615
}

src/test/java/org/springframework/data/elasticsearch/core/index/ReactiveMappingBuilderUnitTests.java

+47
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
import static org.skyscreamer.jsonassert.JSONAssert.*;
1919
import static org.springframework.data.elasticsearch.annotations.FieldType.*;
2020

21+
import org.springframework.data.elasticsearch.annotations.FieldType;
22+
import org.springframework.data.elasticsearch.annotations.InnerField;
23+
import org.springframework.data.elasticsearch.annotations.MultiField;
2124
import reactor.core.publisher.Mono;
2225
import reactor.core.scheduler.Schedulers;
2326

2427
import java.time.Instant;
28+
import java.util.List;
2529

2630
import org.json.JSONException;
2731
import org.junit.jupiter.api.DisplayName;
@@ -78,6 +82,40 @@ void shouldWriteRuntimeFields() throws JSONException {
7882

7983
assertEquals(expected, mapping, true);
8084
}
85+
86+
@Test // #2952
87+
void shouldMapNullityParameters() throws JSONException {
88+
// Given
89+
ReactiveMappingBuilder mappingBuilder = getReactiveMappingBuilder();
90+
String expected = """
91+
{
92+
"properties": {
93+
"_class": {
94+
"type": "keyword",
95+
"index": false,
96+
"doc_values": false
97+
},
98+
"empty-field": {
99+
"type": "keyword",
100+
"null_value": "EMPTY",
101+
"fields": {
102+
"suffix": {
103+
"type": "keyword",
104+
"null_value": "EMPTY_TEXT"
105+
}
106+
}
107+
}
108+
}
109+
}
110+
""";
111+
112+
// When
113+
String result = Mono.defer(() -> mappingBuilder.buildReactivePropertyMapping(MultiFieldWithNullEmptyParameters.class))
114+
.subscribeOn(Schedulers.parallel()).block();
115+
116+
// Then
117+
assertEquals(expected, result, true);
118+
}
81119

82120
// region entities
83121
@Document(indexName = "runtime-fields")
@@ -88,5 +126,14 @@ private static class RuntimeFieldEntity {
88126
@Field(type = Date, format = DateFormat.epoch_millis, name = "@timestamp")
89127
@Nullable private Instant timestamp;
90128
}
129+
130+
@SuppressWarnings("unused")
131+
private static class MultiFieldWithNullEmptyParameters {
132+
@Nullable
133+
@MultiField(
134+
mainField = @Field(name = "empty-field", type = FieldType.Keyword, nullValue = "EMPTY", storeNullValue = true),
135+
otherFields = {
136+
@InnerField(suffix = "suffix", type = Keyword, nullValue = "EMPTY_TEXT") }) private List<String> emptyField;
137+
}
91138
// endregion
92139
}

0 commit comments

Comments
 (0)