Skip to content

Commit

Permalink
#420 - Solr examples now try to start a Docker container.
Browse files Browse the repository at this point in the history
If the Solr examples don't find a Solr instance already running a Docker container is started with the correct variant of Solr running.

Cleaned up the test setup which was suffering from side effects between the tests and also dependent on the Solr version in use.
  • Loading branch information
schauder committed Oct 25, 2018
1 parent 5d536b1 commit c164d7b
Show file tree
Hide file tree
Showing 14 changed files with 345 additions and 85 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</parent>

<modules>
<module>util</module>
<module>bom</module>
<module>couchbase</module>
<module>elasticsearch</module>
Expand Down
11 changes: 11 additions & 0 deletions solr/example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-examples-utils</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.7.3</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@

import example.springdata.solr.product.Product;
import example.springdata.solr.product.ProductRepository;
import example.springdata.solr.test.util.RequiresSolrServer;
import example.springdata.test.util.InfrastructureRule;

import java.time.Duration;
import java.util.Arrays;
import java.util.Optional;

import org.junit.ClassRule;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.solr.core.SolrOperations;
import org.springframework.data.solr.core.query.Function;
import org.springframework.data.solr.core.query.Query;
Expand All @@ -48,33 +48,41 @@
* @author Christoph Strobl
* @author Oliver Gierke
* @author Mark Paluch
* @author Jens Schauder
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class AdvancedSolrRepositoryTests {

public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost();
@Rule @Autowired public InfrastructureRule requiresRunningServer;

@Configuration
static class Config extends SolrTestConfiguration {

@Override
protected void doInitTestData(CrudRepository<Product, String> repository) {
@Autowired ProductRepository repository;
@Autowired SolrOperations operations;

Product playstation = Product.builder().id("id-1").name("Playstation")
.description("The Sony playstation was the top selling gaming system in 1994.").popularity(5).build();
Product playstation2 = Product.builder().id("id-2").name("Playstation Two")
.description("Playstation two is the successor of playstation in 2000.").build();
Product superNES = Product.builder().id("id-3").name("Super Nintendo").popularity(3).build();
Product nintendo64 = Product.builder().id("id-4").name("N64").description("Nintendo 64").popularity(2).build();

repository.saveAll(Arrays.asList(playstation, playstation2, superNES, nintendo64));
}
/**
* Remove test data when context is shut down.
*/
public @After void deleteDocumentsOnShutdown() {
repository.deleteAll();
}

@Autowired ProductRepository repository;
@Autowired SolrOperations operations;
/**
* Initialize Solr instance with test data once context has started.
*/
public @Before void initWithTestData() throws InterruptedException {

repository.deleteAll();

Product playstation = Product.builder().id("id-1").name("Playstation")
.description("The Sony playstation was the top selling gaming system in 1994.").popularity(5).build();
Product playstation2 = Product.builder().id("id-2").name("Playstation Two")
.description("Playstation two is the successor of playstation in 2000.").build();
Product superNES = Product.builder().id("id-3").name("Super Nintendo").popularity(3).build();
Product nintendo64 = Product.builder().id("id-4").name("N64").description("Nintendo 64").popularity(2).build();

repository.saveAll(Arrays.asList(playstation, playstation2, superNES, nintendo64));
}
/**
* {@link HighlightPage} holds next to the entities found also information about where a match was found within the
* document. This allows to fine grained display snipplets of data containing the matching term in context.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
*/
package example.springdata.solr;

import example.springdata.solr.product.Product;
import example.springdata.solr.product.ProductRepository;
import example.springdata.solr.test.util.RequiresSolrServer;
import example.springdata.test.util.InfrastructureRule;

import org.junit.ClassRule;
import java.util.stream.IntStream;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -27,15 +32,34 @@

/**
* @author Christoph Strobl
* @author Jens Schauder
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SolrTestConfiguration.class)
@SpringBootTest
public class BasicSolrRepositoryTests {

public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost();
@Rule @Autowired public InfrastructureRule requiresRunningServer;

@Autowired ProductRepository repository;

/**
* Remove test data when context is shut down.
*/
public @After void deleteDocumentsOnShutdown() {
repository.deleteAll();
}

/**
* Initialize Solr instance with test data once context has started.
*/
public @Before void initWithTestData() {

repository.deleteAll();

IntStream.range(0, 100)
.forEach(index -> repository.save(Product.builder().id("p-" + index).name("foobar").build()));
}

/**
* Finds all entries using a single request.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
package example.springdata.solr;

import example.springdata.solr.product.Product;

import java.util.stream.IntStream;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import example.springdata.solr.test.util.SolrInfrastructureRule;
import example.springdata.test.util.InfrastructureRule;

import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
Expand All @@ -32,33 +31,22 @@
/**
* @author Christoph Strobl
* @author Oliver Gierke
* @author Jens Schauder
*/
@SpringBootApplication
public class SolrTestConfiguration {

@Autowired CrudRepository<Product, String> repo;

public @Bean SolrTemplate solrTemplate() {
return new SolrTemplate(new HttpSolrClient.Builder().withBaseSolrUrl("http://localhost:8983/solr").build());
}
private static final Logger LOG = LoggerFactory.getLogger(SolrTestConfiguration.class);

/**
* Remove test data when context is shut down.
*/
public @PreDestroy void deleteDocumentsOnShutdown() {
repo.deleteAll();
}
public @Bean InfrastructureRule infrastructureRule() {

/**
* Initialize Solr instance with test data once context has started.
*/
public @PostConstruct void initWithTestData() {
doInitTestData(repo);
return new SolrInfrastructureRule("techproducts");
}

protected void doInitTestData(CrudRepository<Product, String> repository) {

IntStream.range(0, 100)
.forEach(index -> repository.save(Product.builder().id("p-" + index).name("foobar").build()));
public @Bean SolrTemplate solrTemplate() {
return new SolrTemplate(new HttpSolrClient.Builder().withBaseSolrUrl(infrastructureRule().getInfo()).build());
}

}
1 change: 1 addition & 0 deletions solr/example/src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
logging.level.root=WARN
logging.level.org.springframework.data.solr.core.SolrTemplate=DEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

import example.springdata.solr.product.ManagedProduct;
import example.springdata.solr.product.ProductRepository;
import example.springdata.solr.test.util.RequiresSolrServer;
import example.springdata.test.util.InfrastructureRule;

import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -28,12 +28,13 @@

/**
* @author Christoph Strobl
* @author Jens Schauder
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class SolrRepositoryTests {

public static @ClassRule RequiresSolrServer requiresRunningServer = RequiresSolrServer.onLocalhost();
@Rule @Autowired public InfrastructureRule requiresRunningServer;

@Autowired ProductRepository repo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import javax.annotation.PreDestroy;

import example.springdata.solr.test.util.SolrInfrastructureRule;
import example.springdata.test.util.InfrastructureRule;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -41,8 +43,14 @@ public class SolrTestConfiguration {

@Autowired ProductRepository repo;

public @Bean
InfrastructureRule infrastructureRule() {

return new SolrInfrastructureRule("schemaless");
}

public @Bean SolrClient solrClient() {
return new HttpSolrClient.Builder().withBaseSolrUrl("http://localhost:8983/solr").build();
return new HttpSolrClient.Builder().withBaseSolrUrl(infrastructureRule().getInfo()).build();
}

/**
Expand Down
10 changes: 10 additions & 0 deletions solr/util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,15 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.7.3</version>
</dependency>
<dependency>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-examples-utils</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.solr.test.util;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.testcontainers.containers.GenericContainer;

/**
* @author Jens Schauder
*/
public class SolrExampleContainer extends GenericContainer<SolrExampleContainer> {

private static final String IMAGE_NAME = "solr";
private static final String DEFAULT_VERSION = "7";
private static final int DEFAULT_PORT = 8983;
private final String example;

public SolrExampleContainer(final String example) {

super(IMAGE_NAME + ":" + DEFAULT_VERSION);
this.example = example;
this.withExposedPorts(DEFAULT_PORT) //
.withCommand("bash", "-c", "solr start -e " + example + " && tail -f /dev/null");

}

@Override
public void start() {
super.start();

Integer port = getMappedPort(DEFAULT_PORT);
String url = "http://localhost:" + port + "/" + IMAGE_NAME;

waitForSolr(url);

}

private void waitForSolr(String url) {

long waitStart = System.currentTimeMillis();
long maxWaitDuration = 10000;

do {

try (CloseableHttpClient client = HttpClientBuilder.create().build()) {

CloseableHttpResponse response = client.execute(new HttpGet(url + "/" + example + "/admin/ping"));
if (response != null && response.getStatusLine() != null && response.getStatusLine().getStatusCode() == 200) {

break;
}

} catch (Exception e) {
e.printStackTrace();
}

} while (System.currentTimeMillis() < waitStart + maxWaitDuration);
}

public String getSolrBaseUrl() {
return "http://localhost:" + getMappedPort(8983) + "/solr";
}
}
Loading

0 comments on commit c164d7b

Please sign in to comment.