#514 - Fix test setup of Spring Data for Apache Solr examples.

Guard test execution of Spring Data for Apache Solr examples with collection requirement. We now make sure to register required Beans just once and check if a specific collection is available via Apache Solr. Move off deprecated API in Solr example.
This commit is contained in:
Christoph Strobl
2019-03-12 14:25:02 +01:00
committed by Oliver Drotbohm
parent 557851105c
commit b6c86c67c8
4 changed files with 26 additions and 8 deletions

View File

@@ -28,6 +28,8 @@ import java.time.Duration;
import java.util.Arrays;
import java.util.Optional;
import javax.annotation.PostConstruct;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -50,15 +52,15 @@ import org.springframework.test.context.junit4.SpringRunner;
* @author Mark Paluch
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@SpringBootTest(classes = SolrTestConfiguration.class)
public class AdvancedSolrRepositoryTests {
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost();
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost().withCollection("techproducts");
@Configuration
static class Config extends SolrTestConfiguration {
static class Config {
@Override
@PostConstruct
protected void doInitTestData(CrudRepository<Product, String> repository) {
Product playstation = Product.builder().id("id-1").name("Playstation")
@@ -82,7 +84,7 @@ public class AdvancedSolrRepositoryTests {
@Test
public void annotationBasedHighlighting() {
HighlightPage<Product> products = repository.findByDescriptionStartingWith("play", new PageRequest(0, 10));
HighlightPage<Product> products = repository.findByDescriptionStartingWith("play", PageRequest.of(0, 10));
products.getHighlighted().forEach(entry -> entry.getHighlights().forEach(highligh -> System.out
.println(entry.getEntity().getId() + " | " + highligh.getField() + ":\t" + highligh.getSnipplets())));

View File

@@ -32,7 +32,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = SolrTestConfiguration.class)
public class BasicSolrRepositoryTests {
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost();
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost().withCollection("techproducts");
@Autowired ProductRepository repository;

View File

@@ -33,7 +33,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
public class SolrRepositoryTests {
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost();
public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost().withCollection("gettingstarted");
@Autowired ProductRepository repo;

View File

@@ -27,6 +27,8 @@ import org.junit.AssumptionViolatedException;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
/**
* {@link TestRule} implementation using {@link CloseableHttpClient} to check if Solr is running by sending
@@ -39,15 +41,26 @@ public class RequiresSolrServer implements TestRule {
private static final String PING_PATH = "/admin/info/system";
private final String baseUrl;
private final @Nullable String collection;
private RequiresSolrServer(String baseUrl) {
this(baseUrl, null);
}
private RequiresSolrServer(String baseUrl, @Nullable String collection) {
this.baseUrl = baseUrl;
this.collection = collection;
}
public static RequiresSolrServer onLocalhost() {
return new RequiresSolrServer("http://localhost:8983/solr");
}
public RequiresSolrServer withCollection(String collection) {
return new RequiresSolrServer(baseUrl, collection);
}
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@@ -64,7 +77,10 @@ public class RequiresSolrServer implements TestRule {
private void checkServerRunning() {
try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
CloseableHttpResponse response = client.execute(new HttpGet(baseUrl + PING_PATH));
String url = StringUtils.hasText(collection) ? baseUrl + "/" + collection + "/select?q=*:*" : baseUrl + PING_PATH;
CloseableHttpResponse response = client.execute(new HttpGet(url));
if (response != null && response.getStatusLine() != null) {
Assume.assumeThat(response.getStatusLine().getStatusCode(), Is.is(200));
}