Skip to content

Commit 61e0e9f

Browse files
committed
test: add SearchDocumentResponseBuilder unit tests
1 parent d74552c commit 61e0e9f

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch.client.elc;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
21+
import org.assertj.core.api.SoftAssertions;
22+
import org.json.JSONException;
23+
import org.junit.jupiter.api.Test;
24+
import org.springframework.data.elasticsearch.core.document.SearchDocumentResponse;
25+
26+
import com.google.common.collect.ImmutableList;
27+
import com.google.common.collect.ImmutableMap;
28+
29+
import co.elastic.clients.elasticsearch.core.search.HitsMetadata;
30+
import co.elastic.clients.elasticsearch.core.search.Suggestion;
31+
import co.elastic.clients.elasticsearch.core.search.TotalHitsRelation;
32+
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
33+
34+
/**
35+
* Tests for the factory class to create {@link SearchDocumentResponse} instances.
36+
*
37+
* @author Sébastien Comeau
38+
* @since 5.2
39+
*/
40+
class SearchDocumentResponseBuilderUnitTests {
41+
42+
private JacksonJsonpMapper jsonpMapper = new JacksonJsonpMapper();
43+
44+
@Test // GH-2681
45+
void shouldGetPhraseSuggestion() throws JSONException {
46+
// arrange
47+
final var hitsMetadata = new HitsMetadata.Builder<EntityAsMap>()
48+
.total(total -> total
49+
.value(0)
50+
.relation(TotalHitsRelation.Eq))
51+
.hits(new ArrayList<>())
52+
.build();
53+
54+
final var suggestionTest = new Suggestion.Builder<EntityAsMap>()
55+
.phrase(phrase -> phrase
56+
.text("National")
57+
.offset(0)
58+
.length(8)
59+
.options(option -> option
60+
.text("nations")
61+
.highlighted("highlighted-nations")
62+
.score(0.11480146)
63+
.collateMatch(false))
64+
.options(option -> option
65+
.text("national")
66+
.highlighted("highlighted-national")
67+
.score( 0.08063514)
68+
.collateMatch(false)))
69+
.build();
70+
71+
final var sortProperties = ImmutableMap.<String, List<Suggestion<EntityAsMap>>>builder()
72+
.put("suggestionTest", ImmutableList.of(suggestionTest))
73+
.build();
74+
75+
// act
76+
final var actual = SearchDocumentResponseBuilder.from(hitsMetadata, null, null, null, sortProperties, null, jsonpMapper);
77+
78+
// assert
79+
SoftAssertions softly = new SoftAssertions();
80+
81+
softly.assertThat(actual).isNotNull();
82+
softly.assertThat(actual.getSuggest()).isNotNull();
83+
softly.assertThat(actual.getSuggest().getSuggestions()).isNotNull().hasSize(1);
84+
85+
final var actualSuggestion = actual.getSuggest().getSuggestions().get(0);
86+
softly.assertThat(actualSuggestion.getName()).isEqualTo("suggestionTest");
87+
softly.assertThat(actualSuggestion.getEntries()).isNotNull().hasSize(1);
88+
89+
final var actualEntry = actualSuggestion.getEntries().get(0);
90+
softly.assertThat(actualEntry).isNotNull();
91+
softly.assertThat(actualEntry.getText()).isEqualTo("National");
92+
softly.assertThat(actualEntry.getOffset()).isEqualTo(0);
93+
softly.assertThat(actualEntry.getLength()).isEqualTo(8);
94+
softly.assertThat(actualEntry.getOptions()).isNotNull().hasSize(2);
95+
96+
final var actualOption1 = actualEntry.getOptions().get(0);
97+
softly.assertThat(actualOption1.getText()).isEqualTo("nations");
98+
softly.assertThat(actualOption1.getHighlighted()).isEqualTo("highlighted-nations");
99+
softly.assertThat(actualOption1.getScore()).isEqualTo(0.11480146);
100+
softly.assertThat(actualOption1.getCollateMatch()).isEqualTo(false);
101+
102+
final var actualOption2 = actualEntry.getOptions().get(1);
103+
softly.assertThat(actualOption2.getText()).isEqualTo("national");
104+
softly.assertThat(actualOption2.getHighlighted()).isEqualTo("highlighted-national");
105+
softly.assertThat(actualOption2.getScore()).isEqualTo(0.08063514);
106+
softly.assertThat(actualOption2.getCollateMatch()).isEqualTo(false);
107+
108+
softly.assertAll();
109+
}
110+
}

0 commit comments

Comments
 (0)