DATAES-43 : Add Test cases for CDI Repository

This commit is contained in:
mohsin.husen 2014-02-06 19:48:45 +00:00
parent 47a654e263
commit 7828612f15
7 changed files with 409 additions and 0 deletions

View File

@ -126,6 +126,12 @@
<version>${webbeans}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -0,0 +1,178 @@
/*
* 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.elasticsearch;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import java.util.Date;
import java.util.List;
/**
* @author Mohsin Husen
*/
@Document(indexName = "test-product-index", type = "test-product-type", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
public class Product {
@Id
private String id;
private List<String> title;
private String name;
private String description;
private String text;
private List<String> categories;
private Float weight;
private Float price;
private Integer popularity;
private boolean available;
private String location;
private Date lastModified;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<String> getTitle() {
return title;
}
public void setTitle(List<String> title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<String> getCategories() {
return categories;
}
public void setCategories(List<String> categories) {
this.categories = categories;
}
public Float getWeight() {
return weight;
}
public void setWeight(Float weight) {
this.weight = weight;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public Integer getPopularity() {
return popularity;
}
public void setPopularity(Integer popularity) {
this.popularity = popularity;
}
public boolean isAvailable() {
return available;
}
public void setAvailable(boolean available) {
this.available = available;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Date getLastModified() {
return lastModified;
}
public void setLastModified(Date lastModified) {
this.lastModified = lastModified;
}
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Product other = (Product) obj;
if (id == null) {
if (other.id != null) {
return false;
}
} else if (!id.equals(other.id)) {
return false;
}
return true;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}

View File

@ -0,0 +1,29 @@
/*
* 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.elasticsearch.repositories.cdi;
import org.springframework.data.elasticsearch.Product;
import org.springframework.data.repository.CrudRepository;
/**
* @author Mohsin Husen
*/
public interface CdiProductRepository extends CrudRepository<Product, String> {
Product findOne(String id);
}

View File

@ -0,0 +1,36 @@
/*
* 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.elasticsearch.repositories.cdi;
import javax.inject.Inject;
/**
* @author Mohsin Husen
*/
class CdiRepositoryClient {
private CdiProductRepository repository;
public CdiProductRepository getRepository() {
return repository;
}
@Inject
public void setRepository(CdiProductRepository repository) {
this.repository = repository;
}
}

View File

@ -0,0 +1,81 @@
/*
* 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.elasticsearch.repositories.cdi;
import org.apache.webbeans.cditest.CdiTestContainer;
import org.apache.webbeans.cditest.CdiTestContainerLoader;
import org.junit.*;
import org.springframework.data.elasticsearch.Product;
import static org.junit.Assert.*;
/**
* @author Mohsin Husen
*/
public class CdiRepositoryTests {
private static CdiTestContainer cdiContainer;
private CdiProductRepository repository;
@BeforeClass
public static void init() throws Exception {
cdiContainer = CdiTestContainerLoader.getCdiContainer();
cdiContainer.startApplicationScope();
cdiContainer.bootContainer();
}
@AfterClass
public static void shutdown() throws Exception {
cdiContainer.stopContexts();
cdiContainer.shutdownContainer();
}
@Before
public void setUp() {
CdiRepositoryClient client = cdiContainer.getInstance(CdiRepositoryClient.class);
repository = client.getRepository();
}
@Test
public void testCdiRepository() {
assertNotNull(repository);
Product bean = new Product();
bean.setId("id-1");
bean.setName("cidContainerTest-1");
repository.save(bean);
assertTrue(repository.exists(bean.getId()));
Product retrieved = repository.findOne(bean.getId());
assertNotNull(retrieved);
assertEquals(bean.getId(), retrieved.getId());
assertEquals(bean.getName(), retrieved.getName());
assertEquals(1, repository.count());
assertTrue(repository.exists(bean.getId()));
repository.delete(bean);
assertEquals(0, repository.count());
retrieved = repository.findOne(bean.getId());
assertNull(retrieved);
}
}

View File

@ -0,0 +1,74 @@
/*
* 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.elasticsearch.repositories.cdi;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
import org.xml.sax.SAXException;
import javax.annotation.PreDestroy;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
/**
* @author Mohsin Husen
*/
@ApplicationScoped
class ElasticsearchTemplateProducer {
@Produces
public ElasticsearchOperations createSolrTemplate() throws IOException, ParserConfigurationException, SAXException {
ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder().put("http.enabled","false" );
NodeClient client = (NodeClient) nodeBuilder().settings(settings).clusterName("testCluster").local(true).node()
.client();
return new ElasticsearchTemplate(client);
}
@PreDestroy
public void shutdown() {
// remove everything to avoid conflicts with other tests in case server not shut down properly
deleteAll();
}
private void deleteAll() {
ElasticsearchOperations template;
try {
template = createSolrTemplate();
DeleteQuery deleteQuery = new DeleteQuery();
deleteQuery.setQuery(QueryBuilders.matchAllQuery());
deleteQuery.setIndex("test-product-index");
deleteQuery.setType("test-product-type");
template.delete(deleteQuery);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>