DATAREST-387 - Added integration tests for Spring Data Solr repositories.

Added configuration for setting up an EmbeddedSolrServer instance within a temporary directory. We use a temporary test folder created by JUnit where we copy the required configuration to. The directory will also hold all index data and will be deleted afterwards.

Original pull request: #156.
This commit is contained in:
Christoph Strobl
2014-09-16 12:09:00 +02:00
committed by Oliver Gierke
parent 0347bda251
commit 38d1b81a4d
11 changed files with 486 additions and 5 deletions

View File

@@ -32,6 +32,7 @@
<springdata.mongodb>1.7.0.BUILD-SNAPSHOT</springdata.mongodb>
<springdata.neo4j>3.3.0.BUILD-SNAPSHOT</springdata.neo4j>
<springdata.gemfire>1.6.0.BUILD-SNAPSHOT</springdata.gemfire>
<springdata.solr>1.4.0.BUILD-SNAPSHOT</springdata.solr>
<hibernate.version>4.3.5.Final</hibernate.version>
@@ -109,6 +110,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>${springdata.solr}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>

View File

@@ -110,6 +110,23 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-core</artifactId>
<version>4.7.2</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<artifactId>jdk.tools</artifactId>
<groupId>jdk.tools</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>

View File

@@ -57,6 +57,7 @@ import com.jayway.jsonpath.JsonPath;
*
* @author Oliver Gierke
* @author Greg Turnquist
* @author Christoph Strobl
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@@ -205,10 +206,18 @@ public abstract class AbstractWebIntegrationTests {
protected String assertJsonPathEquals(String path, String expected, MockHttpServletResponse response)
throws Exception {
String jsonQueryResults = assertHasJsonPathValue(path, response);
assertThat(jsonQueryResults, is(expected));
Object jsonQueryResults = assertHasJsonPathValue(path, response);
return jsonQueryResults;
String jsonString = "";
if (jsonQueryResults instanceof JSONArray) {
jsonString = ((JSONArray) jsonQueryResults).toJSONString();
} else {
jsonString = jsonQueryResults != null ? jsonQueryResults.toString() : null;
}
assertThat(jsonString, is(expected));
return jsonString;
}
protected ResultMatcher doesNotHaveLinkWithRel(final String rel) {

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2014 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 org.springframework.data.rest.webmvc.solr;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.springframework.data.annotation.Id;
import org.springframework.data.solr.core.mapping.Indexed;
import org.springframework.data.solr.core.mapping.SolrDocument;
import org.springframework.util.ObjectUtils;
/**
* @author Christoph Strobl
*/
@SolrDocument(solrCoreName = "collection1")
public class Product {
private @Id String id;
private @Indexed String name;
private @Indexed(name = "cat") List<String> categories;
public Product() {}
public Product(String id, String name, String... categories) {
this.id = id;
this.name = name;
this.categories = ObjectUtils.isEmpty(categories) ? Collections.<String> emptyList() : Arrays.asList(categories);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getCategories() {
return categories;
}
public void setCategories(List<String> categories) {
this.categories = categories;
}
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2014 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 org.springframework.data.rest.webmvc.solr;
import org.springframework.data.repository.PagingAndSortingRepository;
/**
* @author Christoph Strobl
*/
public interface ProductRepository extends PagingAndSortingRepository<Product, String> {}

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2014 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 org.springframework.data.rest.webmvc.solr;
import static org.springframework.data.rest.webmvc.util.TestUtils.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.server.SolrServerFactory;
import org.springframework.data.solr.server.support.EmbeddedSolrServerFactory;
import org.springframework.util.FileCopyUtils;
import org.xml.sax.SAXException;
/**
* @author Christoph Strobl
*/
@Configuration
public class SolrInfrastructureConfig {
private static final String CORE_PROPERTIES = "name=collection1";
private static final Resource SOLR_CONFIG = new ClassPathResource("solrconfig.xml", SolrInfrastructureConfig.class);
private static final Resource SOLR_SCHEMA = new ClassPathResource("schema.xml", SolrInfrastructureConfig.class);
@Bean
public SolrServerFactory solrServerFactory(final String solrHomeDir) throws ParserConfigurationException,
IOException, SAXException {
prepareConfiguration(solrHomeDir);
return new EmbeddedSolrServerFactory(solrHomeDir);
}
@Bean
public SolrTemplate solrTemplate(SolrServerFactory factory) {
return new SolrTemplate(factory);
}
private static void prepareConfiguration(final String solrHomeDir) throws IOException {
Map<String, String> configParams = new HashMap<String, String>();
configParams.put("${data.dir}", solrHomeDir);
configParams.put("${lucene.version}", "4.7");
Resource solrConfig = filterResource(SOLR_CONFIG, configParams);
Resource solrSchema = SOLR_SCHEMA;
File confDir = new File(new File(solrHomeDir, "collection1"), "conf");
confDir.mkdirs();
FileCopyUtils.copy(solrSchema.getInputStream(), new FileOutputStream(createFile(confDir, "schema.xml")));
FileCopyUtils.copy(solrConfig.getInputStream(), new FileOutputStream(createFile(confDir, "solrconfig.xml")));
FileCopyUtils.copy(CORE_PROPERTIES.getBytes(),
new FileOutputStream(createFile(new File(solrHomeDir), "config.properties")));
}
private static File createFile(File parent, String child) throws IOException {
File file = new File(parent, child);
if (!file.exists()) {
file.createNewFile();
}
return file;
}
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2014 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 org.springframework.data.rest.webmvc.solr;
import org.junit.ClassRule;
import org.junit.rules.TemporaryFolder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.solr.repository.config.EnableSolrRepositories;
import org.springframework.test.context.ContextConfiguration;
/**
* @author Christoph Strobl
*/
@ContextConfiguration
public class SolrTestBase {
public static @ClassRule TemporaryFolder TEMP_FOLDER = new TemporaryFolder();
@Configuration
@EnableSolrRepositories
@Import(SolrInfrastructureConfig.class)
static class MyConf {
@Bean
String solrHomeDir() {
return TEMP_FOLDER.getRoot().getAbsolutePath();
}
}
}

View File

@@ -0,0 +1,154 @@
/*
* Copyright 2014 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 org.springframework.data.rest.webmvc.solr;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import java.util.Arrays;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.rest.webmvc.AbstractWebIntegrationTests;
import org.springframework.data.solr.repository.config.EnableSolrRepositories;
import org.springframework.hateoas.Link;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* @author Christoph Strobl
*/
@ContextConfiguration(classes = { SolrWebTests.MyConf.class })
public class SolrWebTests extends AbstractWebIntegrationTests {
public static @ClassRule TemporaryFolder TEMP_FOLDER = new TemporaryFolder();
private static final Product PLAYSTATION = new Product("1", "playstation", "electronic", "game", "media");
private static final Product GAMEBOY = new Product("2", "gameboy", "electronic");
private static final Product AMIGA500 = new Product("3", "amiga500", "ancient");
private static final ObjectMapper MAPPER = new ObjectMapper();
@Configuration
@EnableSolrRepositories
@Import(value = { SolrInfrastructureConfig.class })
static class MyConf {
@Bean
String solrHomeDir() {
return TEMP_FOLDER.getRoot().getAbsolutePath();
}
}
@Autowired ProductRepository repo;
@Before
public void setUp() {
super.setUp();
repo.save(Arrays.asList(PLAYSTATION, GAMEBOY, AMIGA500));
}
@After
public void tearDown() {
repo.deleteAll();
}
/**
* @see DATAREST-387
*/
@Test
public void allowsPaginationThroughData() throws Exception {
MockHttpServletResponse response = client.request("/products?page=0&size=1");
Link nextLink = client.assertHasLinkWithRel(Link.REL_NEXT, response);
assertDoesNotHaveLinkWithRel(Link.REL_PREVIOUS, response);
response = client.request(nextLink);
client.assertHasLinkWithRel(Link.REL_PREVIOUS, response);
nextLink = client.assertHasLinkWithRel(Link.REL_NEXT, response);
response = client.request(nextLink);
client.assertHasLinkWithRel(Link.REL_PREVIOUS, response);
assertDoesNotHaveLinkWithRel(Link.REL_NEXT, response);
}
/**
* @see DATAREST-387
*/
@Test
public void allowsRetrievingDataById() throws Exception {
requestAndCompare(PLAYSTATION);
}
/**
* @see DATAREST-387
*/
@Test
public void createsEntitesCorrectly() throws Exception {
Product product = new Product("4", "iWatch", "trends", "scary");
mvc.perform(
put("/products/{id}", 4).content(MAPPER.writeValueAsString(product)).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated()).andReturn().getResponse();
assertJsonDocumentMatches(product);
}
/**
* @see DATAREST-387
*/
@Test
public void deletesEntitiesCorrectly() throws Exception {
deleteAndVerify(new Link("/products/1"));
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.webmvc.AbstractWebIntegrationTests#expectedRootLinkRels()
*/
@Override
protected Iterable<String> expectedRootLinkRels() {
return Arrays.asList("products");
}
private void assertJsonDocumentMatches(Product reference) throws Exception {
requestAndCompare(reference);
}
private MockHttpServletResponse requestAndCompare(Product reference) throws Exception {
MockHttpServletResponse response = client.request("/products/" + reference.getId());
assertJsonPathEquals("name", reference.getName(), response);
assertJsonPathEquals("categories", MAPPER.writeValueAsString(reference.getCategories()), response);
return response;
}
}

View File

@@ -16,27 +16,37 @@
package org.springframework.data.rest.webmvc.util;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Scanner;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.data.rest.webmvc.jpa.JpaWebTests;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
/**
* Test helper methods.
*
* @author Oliver Gierke
* @author Christoph Strobl
*/
public class TestUtils {
private static final Charset UTF8 = Charset.forName("UTF-8");
public static String readFileFromClasspath(String name) throws Exception {
ClassPathResource file = new ClassPathResource(name, JpaWebTests.class);
StringBuilder builder = new StringBuilder();
Scanner scanner = new Scanner(file.getFile(), "UTF-8");
Scanner scanner = new Scanner(file.getFile(), UTF8.name());
try {
@@ -59,6 +69,31 @@ public class TestUtils {
*/
public static InputStream asStream(String source) {
Assert.notNull(source, "Source string must not be null!");
return new ByteArrayInputStream(source.getBytes(Charset.forName("UTF-8")));
return new ByteArrayInputStream(source.getBytes(UTF8));
}
/**
* Filters the given {@link Resource} by replacing values within.
*
* @param source must not be {@literal null}.
* @param replacements
* @return {@link Resource} with replaced values.
* @throws IOException
*/
public static Resource filterResource(Resource source, Map<String, ?> replacements) throws IOException {
Assert.notNull(source, "Cannot filter 'null' resource");
if (CollectionUtils.isEmpty(replacements)) {
return source;
}
String temp = StreamUtils.copyToString(source.getInputStream(), UTF8);
for (Map.Entry<String, ?> entry : replacements.entrySet()) {
temp = StringUtils.replace(temp, entry.getKey(), entry.getValue() != null ? entry.getValue().toString() : "");
}
return new ByteArrayResource(temp.getBytes(UTF8));
}
}

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<schema name="minimal" version="1.1">
<types>
<fieldType name="string" class="solr.StrField" />
</types>
<fields>
<field name="id" type="string" indexed="true" stored="true"
required="true" />
<field name="name" type="string" indexed="true" stored="true" />
<field name="cat" type="string" indexed="true" stored="true" multiValued="true" />
</fields>
<uniqueKey>id</uniqueKey>
</schema>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<config>
<dataDir>${data.dir}</dataDir>
<directoryFactory name="DirectoryFactory"
class="solr.NRTCachingDirectoryFactory" />
<luceneMatchVersion>${lucene.version}</luceneMatchVersion>
<updateHandler class="solr.DirectUpdateHandler2">
<commitWithin>
<softCommit>${solr.commitwithin.softcommit:true}</softCommit>
</commitWithin>
</updateHandler>
<requestHandler name="/select" class="solr.SearchHandler">
<lst name="defaults">
<str name="echoParams">explicit</str>
<str name="indent">true</str>
<str name="df">text</str>
</lst>
</requestHandler>
<requestHandler name="/admin/"
class="org.apache.solr.handler.admin.AdminHandlers" />
<requestHandler name="/update" class="solr.UpdateRequestHandler" />
</config>