From 7828612f15dda3879dbdc6df7f84067da4e106c4 Mon Sep 17 00:00:00 2001 From: "mohsin.husen" Date: Thu, 6 Feb 2014 19:48:45 +0000 Subject: [PATCH] DATAES-43 : Add Test cases for CDI Repository --- pom.xml | 6 + .../data/elasticsearch/Product.java | 178 ++++++++++++++++++ .../cdi/CdiProductRepository.java | 29 +++ .../repositories/cdi/CdiRepositoryClient.java | 36 ++++ .../repositories/cdi/CdiRepositoryTests.java | 81 ++++++++ .../cdi/ElasticsearchTemplateProducer.java | 74 ++++++++ src/test/resources/META-INF/beans.xml | 5 + 7 files changed, 409 insertions(+) create mode 100644 src/test/java/org/springframework/data/elasticsearch/Product.java create mode 100644 src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiProductRepository.java create mode 100644 src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiRepositoryClient.java create mode 100644 src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiRepositoryTests.java create mode 100644 src/test/java/org/springframework/data/elasticsearch/repositories/cdi/ElasticsearchTemplateProducer.java create mode 100644 src/test/resources/META-INF/beans.xml diff --git a/pom.xml b/pom.xml index 75ae98657..38986db65 100644 --- a/pom.xml +++ b/pom.xml @@ -126,6 +126,12 @@ ${webbeans} test + + javax.servlet + servlet-api + 3.0-alpha-1 + test + diff --git a/src/test/java/org/springframework/data/elasticsearch/Product.java b/src/test/java/org/springframework/data/elasticsearch/Product.java new file mode 100644 index 000000000..de1141f00 --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/Product.java @@ -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 title; + + private String name; + + private String description; + + private String text; + + private List 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 getTitle() { + return title; + } + + public void setTitle(List title) { + this.title = title; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public List getCategories() { + return categories; + } + + public void setCategories(List 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; + } + +} diff --git a/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiProductRepository.java b/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiProductRepository.java new file mode 100644 index 000000000..faea2aae8 --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiProductRepository.java @@ -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 findOne(String id); + +} diff --git a/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiRepositoryClient.java b/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiRepositoryClient.java new file mode 100644 index 000000000..7dbc6e22c --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiRepositoryClient.java @@ -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; + } + +} diff --git a/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiRepositoryTests.java b/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiRepositoryTests.java new file mode 100644 index 000000000..7f2770500 --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/CdiRepositoryTests.java @@ -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); + } + +} diff --git a/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/ElasticsearchTemplateProducer.java b/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/ElasticsearchTemplateProducer.java new file mode 100644 index 000000000..a7448d31e --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/repositories/cdi/ElasticsearchTemplateProducer.java @@ -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); + } + + } + +} diff --git a/src/test/resources/META-INF/beans.xml b/src/test/resources/META-INF/beans.xml new file mode 100644 index 000000000..7b5940532 --- /dev/null +++ b/src/test/resources/META-INF/beans.xml @@ -0,0 +1,5 @@ + + +