GORA-437 gora couchdb datastore tests
Project: http://git-wip-us.apache.org/repos/asf/gora/repo Commit: http://git-wip-us.apache.org/repos/asf/gora/commit/788807ee Tree: http://git-wip-us.apache.org/repos/asf/gora/tree/788807ee Diff: http://git-wip-us.apache.org/repos/asf/gora/diff/788807ee Branch: refs/heads/master Commit: 788807eeb0335b163ab20c769983a91602273f22 Parents: 51e4a34 Author: cihad guzel <cguz...@gmail.com> Authored: Sat Aug 20 23:28:10 2016 +0300 Committer: cihad guzel <cguz...@gmail.com> Committed: Sun Aug 21 22:56:38 2016 +0300 ---------------------------------------------------------------------- .../java/org/apache/gora/GoraTestDriver.java | 9 +- gora-couchdb/pom.xml | 6 + .../gora/couchdb/store/TestCouchDBStore.java | 156 +++++++++++++++++++ 3 files changed, 167 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/gora/blob/788807ee/gora-core/src/test/java/org/apache/gora/GoraTestDriver.java ---------------------------------------------------------------------- diff --git a/gora-core/src/test/java/org/apache/gora/GoraTestDriver.java b/gora-core/src/test/java/org/apache/gora/GoraTestDriver.java index 02006d9..5c2d071 100644 --- a/gora-core/src/test/java/org/apache/gora/GoraTestDriver.java +++ b/gora-core/src/test/java/org/apache/gora/GoraTestDriver.java @@ -99,12 +99,13 @@ public class GoraTestDriver { @SuppressWarnings("unchecked") public<K, T extends Persistent> DataStore<K,T> createDataStore(Class<K> keyClass, Class<T> persistentClass) throws GoraException { - setProperties(DataStoreFactory.createProps()); - DataStore<K,T> dataStore = DataStoreFactory.createDataStore( - (Class<? extends DataStore<K,T>>)dataStoreClass, keyClass, persistentClass, conf); + final Properties properties = DataStoreFactory.createProps(); + final DataStore<K, T> dataStore = DataStoreFactory.createDataStore( + (Class<? extends DataStore<K, T>>) dataStoreClass, keyClass, persistentClass, conf, properties); + dataStores.add(dataStore); - log.info("Datastore for "+persistentClass+" was added."); + log.info("Datastore for {} was added.", persistentClass); return dataStore; } http://git-wip-us.apache.org/repos/asf/gora/blob/788807ee/gora-couchdb/pom.xml ---------------------------------------------------------------------- diff --git a/gora-couchdb/pom.xml b/gora-couchdb/pom.xml index 69307a6..ab050cc 100644 --- a/gora-couchdb/pom.xml +++ b/gora-couchdb/pom.xml @@ -223,6 +223,12 @@ <scope>test</scope> </dependency> + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>testcontainers</artifactId> + <version>1.1.0</version> + </dependency> + <!-- END of Testing Dependencies --> </dependencies> http://git-wip-us.apache.org/repos/asf/gora/blob/788807ee/gora-couchdb/src/test/java/org/apache/gora/couchdb/store/TestCouchDBStore.java ---------------------------------------------------------------------- diff --git a/gora-couchdb/src/test/java/org/apache/gora/couchdb/store/TestCouchDBStore.java b/gora-couchdb/src/test/java/org/apache/gora/couchdb/store/TestCouchDBStore.java new file mode 100644 index 0000000..1c8fb9d --- /dev/null +++ b/gora-couchdb/src/test/java/org/apache/gora/couchdb/store/TestCouchDBStore.java @@ -0,0 +1,156 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.apache.gora.couchdb.store; + +import org.apache.avro.util.Utf8; +import org.apache.gora.couchdb.GoraCouchDBTestDriver; +import org.apache.gora.couchdb.query.CouchDBResult; +import org.apache.gora.examples.WebPageDataCreator; +import org.apache.gora.examples.generated.Employee; +import org.apache.gora.examples.generated.WebPage; +import org.apache.gora.query.Query; +import org.apache.gora.store.DataStore; +import org.apache.gora.store.DataStoreTestBase; +import org.junit.ClassRule; +import org.junit.Ignore; +import org.junit.Test; +import org.testcontainers.containers.GenericContainer; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.charset.Charset; + +import static org.junit.Assert.*; + +/** + * Tests extending {@link DataStoreTestBase} + * which run the base JUnit test suite for Gora. + */ +public class TestCouchDBStore extends DataStoreTestBase { + + private static final String DOCKER_CONTAINER_NAME = "klaemo/couchdb:1.6.1"; + /** + * JUnit integration testing with Docker and Testcontainers + */ + @ClassRule + public static GenericContainer CouchDB_CONTAINER = new GenericContainer(DOCKER_CONTAINER_NAME); + + static { + try { + setTestDriver(new GoraCouchDBTestDriver(CouchDB_CONTAINER)); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public void setUp() throws Exception { + super.setUp(); + } + + @SuppressWarnings("unchecked") + @Override + protected DataStore<String, Employee> createEmployeeDataStore() throws IOException { + throw new UnsupportedOperationException(); + } + + @SuppressWarnings("unchecked") + @Override + protected DataStore<String, WebPage> createWebPageDataStore() throws IOException { + throw new UnsupportedOperationException(); + } + + @Test + public void testPutAndGet() { + WebPage page = webPageStore.newPersistent(); + + // Write webpage data + page.setUrl(new Utf8("http://example.com")); + byte[] contentBytes = "example content in example.com".getBytes(Charset.defaultCharset()); + ByteBuffer buff = ByteBuffer.wrap(contentBytes); + page.setContent(buff); + webPageStore.put("com.example/http", page); + webPageStore.flush(); + + WebPage storedPage = webPageStore.get("com.example/http"); + + assertNotNull(storedPage); + assertEquals(page.getUrl(), storedPage.getUrl()); + } + + @Test + public void testCreateAndDeleteSchema() throws IOException { + WebPage page = webPageStore.newPersistent(); + + // Write webpage data + page.setUrl(new Utf8("http://example.com")); + webPageStore.put("com.example/http", page); + webPageStore.flush(); + + assertEquals("WebPage isn't created.", page.getUrl(), webPageStore.get("com.example/http").getUrl()); + + webPageStore.deleteSchema(); + + assertNull(webPageStore.get("com.example/http")); + } + + @Test + public void testGetSchemaName() throws IOException { + assertEquals("WebPage", webPageStore.getSchemaName()); + assertEquals("Employee", employeeStore.getSchemaName()); + } + + @Test + public void testExecute() throws IOException { + WebPageDataCreator.createWebPageData(webPageStore); + + final Query<String, WebPage> query = webPageStore.newQuery(); + + int limit = 5; + query.setLimit(limit); + CouchDBResult<String, WebPage> result = (CouchDBResult<String, WebPage>) webPageStore.execute(query); + assertEquals(limit, result.getResultData().size()); + + limit = 10; + query.setLimit(limit); + result = (CouchDBResult<String, WebPage>) webPageStore.execute(query); + assertEquals(limit, result.getResultData().size()); + + } + + /** + * By design, you cannot update a CouchDB document blindly, you can only attempt to update a specific revision of a document. FIXME + */ + @Test + @Ignore + public void testUpdate() throws Exception { + //By design, you cannot update a CouchDB document blindly, you can only attempt to update a specific revision of a document. FIXME + } + + @Ignore("CouchDBStore doesn't support 3 types union field yet") + @Override + public void testGet3UnionField() throws Exception { + // CouchDBStore doesn't support 3 types union field yet + } + + @Ignore("Skip until GORA-66 is fixed: need better semantic for end/start keys") + @Override + public void testDeleteByQueryFields() throws IOException { + // Skip until GORA-66 is fixed: need better semantic for end/start keys + } + +} \ No newline at end of file