Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.Criteria;
Expand All @@ -43,6 +46,7 @@
* @author Artur Konczak
* @author Oliver Gierke
* @author Christoph Strobl
* @author omniCoder77
* @author Prakhar Gupta
* @author Peter-Josef Meisch
*/
Expand Down Expand Up @@ -73,6 +77,8 @@ public ClientConfiguration clientConfiguration() {
}

@Autowired ElasticsearchOperations operations;
@Autowired
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this injecting the Template object? We have already ElasticsearchOperations at hand.

ElasticsearchTemplate template;

@Test
void textSearch() throws ParseException {
Expand Down Expand Up @@ -103,4 +109,29 @@ void geoSpatialSearch() {

assertThat(result).hasSize(2);
}
}

@Test
void shouldUpdateConferenceKeywords() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use spaces and not tabs.

var conferenceName = "JDD14 - Cracow";
var newKeyword = "java-ee";

var initialQuery = new CriteriaQuery(new Criteria("name").is(conferenceName));
SearchHit<Conference> searchHit = template.searchOne(initialQuery, Conference.class);
assertThat(searchHit).isNotNull();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asserting an intermediate state quickly creates the impression that we're testing something that shouldn't be tested. When setting up a state, we assume it is correct and expect the given state, otherwise, the entire arrangement is just waste.


Conference conference = searchHit.getContent();
String conferenceId = searchHit.getId();

int originalKeywordsCount = conference.getKeywords().size();
assertThat(conference.getKeywords()).doesNotContain(newKeyword);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, the need for asserting initial state hints at a potential problem with creating the state and distracts from the thing we actually want to explain here.


conference.getKeywords().add(newKeyword);

template.save(conference);

Conference updatedConference = template.get(conferenceId, Conference.class);
assertThat(updatedConference).isNotNull();
assertThat(updatedConference.getKeywords()).contains(newKeyword);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contains and hasSize can be combined.

assertThat(updatedConference.getKeywords()).hasSize(originalKeywordsCount + 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static org.assertj.core.api.Assertions.*;

import org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchTemplate;
import reactor.test.StepVerifier;

import java.text.ParseException;
Expand All @@ -43,6 +44,7 @@
*
* @author Christoph Strobl
* @author Prakhar Gupta
* @author omniCoder77
* @author Peter-Josef Meisch
*/
@SpringBootTest(
Expand Down Expand Up @@ -74,6 +76,8 @@ public ClientConfiguration clientConfiguration() {
}

@Autowired ReactiveElasticsearchOperations operations;
@Autowired
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this injecting the Template object? We have already ReactiveElasticsearchOperations at hand.

ReactiveElasticsearchTemplate template;

@Test
void textSearch() {
Expand All @@ -100,4 +104,39 @@ private void verify(SearchHit<Conference> hit, String expectedWord, String expec
fail("o_O", e);
}
}
}

@Test
void shouldUpdateConferenceKeywordsReactive() {
var conferenceName = "JDD14 - Cracow";
var newKeyword = "java-ee";

var initialQuery = new CriteriaQuery(new Criteria("name").is(conferenceName));

StepVerifier.create(
template
.search(initialQuery, Conference.class)
.next()
.flatMap(searchHit -> {
assert searchHit != null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert?

Conference conference = searchHit.getContent();
String conferenceId = searchHit.getId();

int originalKeywordsCount = conference.getKeywords().size();
assert !conference.getKeywords().contains(newKeyword);

conference.getKeywords().add(newKeyword);

return template.save(conference)
.then(template.get(conferenceId, Conference.class))
.map(updatedConference -> {
assert updatedConference != null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we use AssertJ here?

assert updatedConference.getKeywords().contains(newKeyword);
assert updatedConference.getKeywords().size() == originalKeywordsCount + 1;
return updatedConference;
});
})
).expectNextCount(1)
.verifyComplete();
}

}