Repository: gora Updated Branches: refs/heads/master cbed41d00 -> 52d583aff
http://git-wip-us.apache.org/repos/asf/gora/blob/89683c74/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/TestCassandraStore.java ---------------------------------------------------------------------- diff --git a/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/TestCassandraStore.java b/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/TestCassandraStore.java new file mode 100644 index 0000000..ce9e2df --- /dev/null +++ b/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/TestCassandraStore.java @@ -0,0 +1,178 @@ +/** + * 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.cassandra.store; + +import org.apache.gora.cassandra.GoraCassandraTestDriver; +import org.apache.gora.examples.WebPageDataCreator; +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.apache.gora.store.DataStoreTestUtil; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.Properties; + +import static org.apache.gora.examples.WebPageDataCreator.SORTED_URLS; +import static org.apache.gora.examples.WebPageDataCreator.URLS; +import static org.apache.gora.store.DataStoreTestUtil.assertEmptyResults; +import static org.apache.gora.store.DataStoreTestUtil.assertNumResults; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +/** + * + * Testing class for all standard gora-cassandra functionality. + * We extend DataStoreTestBase enabling us to run the entire base test + * suite for Gora. + * + * Test Avro Serialization for CassandraStore. + */ +public class TestCassandraStore extends DataStoreTestBase { + private static final Logger LOG = LoggerFactory.getLogger(TestCassandraStore.class); + private static Properties properties; + + static { + GoraCassandraTestDriver testDriver = new GoraCassandraTestDriver(); + setProperties(); + testDriver.setParameters(properties); + setTestDriver(testDriver); + } + + private static void setProperties() { + properties = new Properties(); + properties.setProperty(CassandraStoreParameters.CASSANDRA_SERVERS, "localhost"); + properties.setProperty(CassandraStoreParameters.PORT, "9042"); + properties.setProperty(CassandraStoreParameters.CASSANDRA_SERIALIZATION_TYPE, "avro"); + properties.setProperty(CassandraStoreParameters.PROTOCOL_VERSION, "3"); + properties.setProperty(CassandraStoreParameters.CLUSTER_NAME, "Test Cluster"); + properties.setProperty(CassandraStoreParameters.READ_CONSISTENCY_LEVEL,"ONE"); + properties.setProperty(CassandraStoreParameters.WRITE_CONSISTENCY_LEVEL,"ONE"); + properties.setProperty("gora.cassandrastore.mapping.file", "avro/gora-cassandra-mapping.xml"); + } + + @Before + public void setUp() throws Exception { + super.setUp(); + } + + @Ignore() + @Override + public void testGetPartitions() throws IOException { + } + + private void preConfiguration() { + if (webPageStore.schemaExists()) { + webPageStore.truncateSchema(); + } else { + webPageStore.createSchema(); + } + } + + @Test + public void testQuery() throws Exception { + preConfiguration(); + log.info("test method: testQuery"); + DataStoreTestUtil.testQueryWebPages(webPageStore); + } + + @Test + public void testQueryStartKey() throws Exception { + preConfiguration(); + log.info("test method: testQueryStartKey"); + DataStoreTestUtil.testQueryWebPageStartKey(webPageStore); + } + + @Test + public void testQueryEndKey() throws Exception { + preConfiguration(); + log.info("test method: testQueryEndKey"); + DataStoreTestUtil.testQueryWebPageEndKey(webPageStore); + } + + @Test + public void testQueryKeyRange() throws Exception { + preConfiguration(); + log.info("test method: testQueryKetRange"); + DataStoreTestUtil.testQueryWebPageKeyRange(webPageStore); + } + + @Test + public void testDelete() throws Exception { + preConfiguration(); + log.info("test method: testDelete"); + DataStoreTestUtil.testDelete(webPageStore); + } + + @Test + public void testDeleteByQuery() throws Exception { + preConfiguration(); + log.info("test method: testDeleteByQuery"); + DataStore store = webPageStore; + Query<String, WebPage> query; + //test 1 - delete all + WebPageDataCreator.createWebPageData(store); + + query = store.newQuery(); + + assertNumResults(store.newQuery(), URLS.length); + store.deleteByQuery(query); + store.flush(); + assertEmptyResults(store.newQuery()); + store.truncateSchema(); + } + + @Test + public void testDeleteByQueryFields() throws Exception { + preConfiguration(); + log.info("test method: testQueryByQueryFields"); + //test 5 - delete all with some fields + WebPageDataCreator.createWebPageData(webPageStore); + Query query = webPageStore.newQuery(); + query.setFields("outlinks", "parsedContent", "content"); + + for (String SORTED_URL : SORTED_URLS) { + query.setKey(SORTED_URL); + webPageStore.deleteByQuery(query); + WebPage page = webPageStore.get(SORTED_URL); + assertNotNull(page); + assertNotNull(page.getUrl()); + assertEquals(page.getUrl().toString(), SORTED_URL); + assertEquals("Map of Outlinks should have a size of '0' as the deleteByQuery " + + "not only removes the data but also the data structure.", 0, page.getOutlinks().size()); + assertEquals(0, page.getParsedContent().size()); + if (page.getContent() != null) { + LOG.info("url:" + page.getUrl().toString()); + LOG.info("limit:" + page.getContent().limit()); + } else { + assertNull(page.getContent()); + } + } + } + + @Ignore("Type 3 Union is not supported for Cassandra") + public void testGet3UnionField() { + } +} http://git-wip-us.apache.org/repos/asf/gora/blob/89683c74/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/TestCassandraStoreWithCassandraKey.java ---------------------------------------------------------------------- diff --git a/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/TestCassandraStoreWithCassandraKey.java b/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/TestCassandraStoreWithCassandraKey.java new file mode 100644 index 0000000..3ae3152 --- /dev/null +++ b/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/TestCassandraStoreWithCassandraKey.java @@ -0,0 +1,331 @@ +/* + * 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 + * + * 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.apache.gora.cassandra.store; + +import org.apache.avro.util.Utf8; +import org.apache.gora.cassandra.GoraCassandraTestDriver; +import org.apache.gora.cassandra.example.generated.AvroSerialization.CassandraKey; +import org.apache.gora.cassandra.example.generated.AvroSerialization.CassandraRecord; +import org.apache.gora.cassandra.query.CassandraQuery; +import org.apache.gora.query.Query; +import org.apache.gora.query.Result; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +/** + * This class tests Cassandra Store functionality with CassandraKey. + */ +public class TestCassandraStoreWithCassandraKey { + private static GoraCassandraTestDriver testDriver = new GoraCassandraTestDriver(); + private static CassandraStore<CassandraKey, CassandraRecord> cassandraRecordDataStore; + private static Properties parameter; + + @BeforeClass + public static void setUpClass() throws Exception { + setProperties(); + testDriver.setParameters(parameter); + testDriver.setUpClass(); + cassandraRecordDataStore = (CassandraStore<CassandraKey, CassandraRecord>) testDriver.createDataStore(CassandraKey.class, CassandraRecord.class); + } + + private static void setProperties() { + parameter = new Properties(); + parameter.setProperty(CassandraStoreParameters.CASSANDRA_SERVERS, "localhost"); + parameter.setProperty(CassandraStoreParameters.PORT, "9042"); + parameter.setProperty(CassandraStoreParameters.CASSANDRA_SERIALIZATION_TYPE, "avro"); + parameter.setProperty(CassandraStoreParameters.PROTOCOL_VERSION, "3"); + parameter.setProperty(CassandraStoreParameters.CLUSTER_NAME, "Test Cluster"); + parameter.setProperty("gora.cassandrastore.mapping.file", "compositeKey/gora-cassandra-mapping.xml"); + } + + @AfterClass + public static void tearDownClass() throws Exception { + testDriver.tearDownClass(); + } + + @After + public void tearDown() throws Exception { + testDriver.tearDown(); + } + + /** + * In this test case, schema exists method behavior of the data store is testing. + */ + @Test + public void testSchemaRelatedBehaviour() { + cassandraRecordDataStore.createSchema(); + Assert.assertTrue(cassandraRecordDataStore.schemaExists()); + cassandraRecordDataStore.deleteSchema(); + Assert.assertFalse(cassandraRecordDataStore.schemaExists()); + cassandraRecordDataStore.createSchema(); + Assert.assertTrue(cassandraRecordDataStore.schemaExists()); + } + + /** + * In this test case, get, put and delete methods behaviour of the data store is testing. + */ + @Test + public void testSimplePutGet() { + cassandraRecordDataStore.createSchema(); + CassandraRecord record = new CassandraRecord(); + record.setDataLong(719411002L); + record.setDataString(new Utf8("M.K.H. Gunasekara")); + record.setDataInt(144); + record.setDataBytes(ByteBuffer.wrap("No 144, Gunasekara Mawatha, Mattumgala, Ragama".getBytes(Charset.defaultCharset()))); + record.setDataDouble(3.14159d); + CassandraKey key = new CassandraKey(); + key.setTimestamp(2027L); + key.setUrl("www.apache.org"); + cassandraRecordDataStore.put(key, record); + CassandraRecord retrievedRecord = cassandraRecordDataStore.get(key); + Assert.assertEquals(record.getDataInt(), retrievedRecord.getDataInt()); + Assert.assertEquals(record.getDataString(), retrievedRecord.getDataString()); + Assert.assertEquals(record.getDataLong(), retrievedRecord.getDataLong()); + Assert.assertEquals(record.getDataBytes(), retrievedRecord.getDataBytes()); + Assert.assertEquals(record.getDataDouble(), retrievedRecord.getDataDouble()); + cassandraRecordDataStore.delete(key); + Assert.assertNull(cassandraRecordDataStore.get(key)); + } + + /** + * In this test case, execute and deleteByQuery methods behaviour of the data store is testing. + * + * @throws Exception + */ + @Test + public void testExecuteQuery() throws Exception { + Query<CassandraKey, CassandraRecord> query = cassandraRecordDataStore.newQuery(); + cassandraRecordDataStore.truncateSchema(); + CassandraKey key = new CassandraKey(); + key.setTimestamp(2027L); + key.setUrl("www.apache.org"); + query.setKey(key); + Result<CassandraKey, CassandraRecord> result = query.execute(); + Assert.assertFalse(result.next()); + CassandraRecord record = new CassandraRecord(); + record.setDataLong(719411002L); + record.setDataString(new Utf8("M.K.H. Gunasekara")); + record.setDataInt(144); + record.setDataBytes(ByteBuffer.wrap("No 144, Gunasekara Mawatha, Mattumgala, Ragama".getBytes(Charset.defaultCharset()))); + record.setDataDouble(3.14159d); + // test simple put and query with setKey + cassandraRecordDataStore.put(key, record); + CassandraRecord retrievedRecord = cassandraRecordDataStore.get(key); + Assert.assertEquals(record.getDataInt(), retrievedRecord.getDataInt()); + Assert.assertEquals(record.getDataString(), retrievedRecord.getDataString()); + Assert.assertEquals(record.getDataLong(), retrievedRecord.getDataLong()); + Assert.assertEquals(record.getDataBytes(), retrievedRecord.getDataBytes()); + Assert.assertEquals(record.getDataDouble(), retrievedRecord.getDataDouble()); + result = query.execute(); + Assert.assertTrue(result.next()); + // verify data + retrievedRecord = result.get(); + Assert.assertEquals(record.getDataInt(), retrievedRecord.getDataInt()); + Assert.assertEquals(record.getDataString(), retrievedRecord.getDataString()); + Assert.assertEquals(record.getDataLong(), retrievedRecord.getDataLong()); + Assert.assertEquals(record.getDataBytes(), retrievedRecord.getDataBytes()); + Assert.assertEquals(record.getDataDouble(), retrievedRecord.getDataDouble()); + // test delete by query + cassandraRecordDataStore.deleteByQuery(query); + result = query.execute(); + Assert.assertFalse(result.next()); + // test empty query + Query<CassandraKey, CassandraRecord> emptyQuery = cassandraRecordDataStore.newQuery(); + result = emptyQuery.execute(); + Assert.assertFalse(result.next()); + cassandraRecordDataStore.put(key, record); + result = query.execute(); + Assert.assertTrue(result.next()); + } + + @Test + public void testExecuteQueryWithRange() throws Exception { + // test Range with Query + cassandraRecordDataStore.truncateSchema(); + //insert data + CassandraRecord record1 = new CassandraRecord(); + CassandraRecord record2 = new CassandraRecord(); + CassandraRecord record3 = new CassandraRecord(); + CassandraRecord record4 = new CassandraRecord(); + record1.setDataLong(719411002L); + record1.setDataString(new Utf8("Madawa")); + record1.setDataInt(100); + record2.setDataLong(712778588L); + record2.setDataString(new Utf8("Kasun")); + record2.setDataInt(101); + record3.setDataLong(716069539L); + record3.setDataString(new Utf8("Charith")); + record3.setDataInt(102); + record4.setDataLong(112956051L); + record4.setDataString(new Utf8("Bhanuka")); + record4.setDataInt(103); + CassandraKey key1 = new CassandraKey(); + key1.setTimestamp(200L); + key1.setUrl("www.apache.org"); + CassandraKey key2 = new CassandraKey(); + key2.setTimestamp(205L); + key2.setUrl("www.apache.org"); + CassandraKey key3 = new CassandraKey(); + key3.setTimestamp(210L); + key3.setUrl("www.apache.org"); + CassandraKey key4 = new CassandraKey(); + key4.setTimestamp(215L); + key4.setUrl("www.apache.org"); + cassandraRecordDataStore.put(key1, record1); + cassandraRecordDataStore.put(key2, record2); + cassandraRecordDataStore.put(key3, record3); + cassandraRecordDataStore.put(key4, record4); + Query<CassandraKey, CassandraRecord> rangeQuery = cassandraRecordDataStore.newQuery(); + rangeQuery.setStartKey(key2); + rangeQuery.setEndKey(key2); + Result<CassandraKey, CassandraRecord> result = rangeQuery.execute(); + int i = 0; + while (result.next()) { + i++; + } + Assert.assertEquals(1, i); + + rangeQuery.setStartKey(key2); + rangeQuery.setEndKey(key3); + result = rangeQuery.execute(); + i = 0; + while (result.next()) { + i++; + } + Assert.assertEquals(2, i); + } + + @Test + public void testUpdateByQuery() { + cassandraRecordDataStore.truncateSchema(); + //insert data + CassandraRecord record1 = new CassandraRecord(); + CassandraRecord record2 = new CassandraRecord(); + CassandraRecord record3 = new CassandraRecord(); + CassandraRecord record4 = new CassandraRecord(); + record1.setDataLong(719411002L); + record1.setDataString(new Utf8("Madawa")); + record1.setDataInt(100); + record2.setDataLong(712778588L); + record2.setDataString(new Utf8("Kasun")); + record2.setDataInt(101); + record3.setDataLong(716069539L); + record3.setDataString(new Utf8("Charith")); + record3.setDataInt(102); + record4.setDataLong(112956051L); + record4.setDataString(new Utf8("Bhanuka")); + record4.setDataInt(103); + CassandraKey key1 = new CassandraKey(); + key1.setTimestamp(200L); + key1.setUrl("www.apache.org"); + CassandraKey key2 = new CassandraKey(); + key2.setTimestamp(205L); + key2.setUrl("www.apache.org"); + CassandraKey key3 = new CassandraKey(); + key3.setTimestamp(210L); + key3.setUrl("www.apache.org"); + CassandraKey key4 = new CassandraKey(); + key4.setTimestamp(215L); + key4.setUrl("www.apache.org"); + cassandraRecordDataStore.put(key1, record1); + cassandraRecordDataStore.put(key2, record2); + cassandraRecordDataStore.put(key3, record3); + cassandraRecordDataStore.put(key4, record4); + CassandraQuery<CassandraKey, CassandraRecord> query = new CassandraQuery<>(cassandraRecordDataStore); + query.setKey(key1); + query.addUpdateField("dataString", new Utf8("test123")); + cassandraRecordDataStore.updateByQuery(query); + CassandraRecord result = cassandraRecordDataStore.get(key1); + Assert.assertEquals(new Utf8("test123"), result.getDataString()); + } + + + @Test + public void testDataTypes() { + cassandraRecordDataStore.truncateSchema(); + CassandraRecord record = new CassandraRecord(); + record.setDataLong(719411002L); + record.setDataString(new Utf8("M.K.H. Gunasekara")); + record.setDataInt(144); + record.setDataBytes(ByteBuffer.wrap("No 144, Gunasekara Mawatha, Mattumgala, Ragama".getBytes(Charset.defaultCharset()))); + record.setDataDouble(3.14159d); + ArrayList<Double> doubles = new ArrayList<>(); + doubles.add(2.1D); + doubles.add(3.14D); + record.setArrayDouble(doubles); + ArrayList<Integer> integers = new ArrayList<>(); + integers.add(2); + integers.add(3); + record.setArrayInt(integers); + ArrayList<Long> longs = new ArrayList<>(); + longs.add(2L); + longs.add(3L); + record.setArrayLong(longs); + ArrayList<CharSequence> strings = new ArrayList<>(); + strings.add(new Utf8("Hello World")); + strings.add(new Utf8("Srilanka")); + record.setArrayString(strings); + HashMap<CharSequence, Double> map = new HashMap<>(); + map.put(new Utf8("Life"), 7.3D); + record.setMapDouble(map); + CassandraKey key = new CassandraKey(); + key.setTimestamp(2027L); + key.setUrl("www.apache.org"); + cassandraRecordDataStore.put(key, record); + CassandraRecord retrievedRecord = cassandraRecordDataStore.get(key); + Assert.assertEquals(record.getDataInt(), retrievedRecord.getDataInt()); + Assert.assertEquals(record.getDataString(), retrievedRecord.getDataString()); + Assert.assertEquals(record.getDataLong(), retrievedRecord.getDataLong()); + Assert.assertEquals(record.getDataBytes(), retrievedRecord.getDataBytes()); + Assert.assertEquals(record.getDataDouble(), retrievedRecord.getDataDouble()); + int i = 0; + for (Double obj : retrievedRecord.getArrayDouble()) { + Assert.assertEquals(doubles.get(i), obj); + i++; + } + i = 0; + for (Integer obj : retrievedRecord.getArrayInt()) { + Assert.assertEquals(integers.get(i), obj); + i++; + } + i = 0; + for (Long obj : retrievedRecord.getArrayLong()) { + Assert.assertEquals(longs.get(i), obj); + i++; + } + i = 0; + for (CharSequence obj : retrievedRecord.getArrayString()) { + Assert.assertEquals(strings.get(i), obj); + i++; + } + + for (Map.Entry entry : map.entrySet()) { + Assert.assertEquals(entry.getValue(), retrievedRecord.getMapDouble().get(entry.getKey())); + } + } +} http://git-wip-us.apache.org/repos/asf/gora/blob/89683c74/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/TestCassandraStoreWithNativeSerialization.java ---------------------------------------------------------------------- diff --git a/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/TestCassandraStoreWithNativeSerialization.java b/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/TestCassandraStoreWithNativeSerialization.java new file mode 100644 index 0000000..489732c --- /dev/null +++ b/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/TestCassandraStoreWithNativeSerialization.java @@ -0,0 +1,305 @@ +/* + * 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 + * + * 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.apache.gora.cassandra.store; + +import org.apache.gora.cassandra.GoraCassandraTestDriver; +import org.apache.gora.cassandra.example.generated.nativeSerialization.ComplexTypes; +import org.apache.gora.cassandra.example.generated.nativeSerialization.User; +import org.apache.gora.cassandra.query.CassandraQuery; +import org.apache.gora.query.Query; +import org.apache.gora.query.Result; +import org.apache.gora.store.DataStore; +import org.apache.gora.util.GoraException; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Properties; +import java.util.UUID; + +/** + * This class tests Cassandra Store functionality with Cassandra Native Serialization. + */ +public class TestCassandraStoreWithNativeSerialization { + private static GoraCassandraTestDriver testDriver = new GoraCassandraTestDriver(); + private static CassandraStore<UUID, User> userDataStore; + private static Properties parameter; + + @BeforeClass + public static void setUpClass() throws Exception { + setProperties(); + testDriver.setParameters(parameter); + testDriver.setUpClass(); + userDataStore = (CassandraStore<UUID, User>) testDriver.createDataStore(UUID.class, User.class); + } + + private static void setProperties() { + parameter = new Properties(); + parameter.setProperty(CassandraStoreParameters.CASSANDRA_SERVERS, "localhost"); + parameter.setProperty(CassandraStoreParameters.PORT, "9042"); + parameter.setProperty(CassandraStoreParameters.CASSANDRA_SERIALIZATION_TYPE, "native"); + parameter.setProperty(CassandraStoreParameters.PROTOCOL_VERSION, "3"); + parameter.setProperty(CassandraStoreParameters.CLUSTER_NAME, "Test Cluster"); + parameter.setProperty("gora.cassandrastore.mapping.file", "nativeSerialization/gora-cassandra-mapping.xml"); + } + + @AfterClass + public static void tearDownClass() throws Exception { + testDriver.tearDownClass(); + } + + @After + public void tearDown() throws Exception { + testDriver.tearDown(); + } + + /** + * In this test case, put and get behavior of the data store are testing. + */ + @Test + public void testSimplePutAndGet() { + UUID id = UUID.randomUUID(); + User user1 = new User(id, "madhawa", Date.from(Instant.now())); + // storing data; + userDataStore.put(id, user1); + // get data; + User olduser = userDataStore.get(id); + Assert.assertEquals(olduser.getName(), user1.getName()); + Assert.assertEquals(olduser.getDateOfBirth(), user1.getDateOfBirth()); + } + + /** + * In this test case, put and delete behavior of the data store are testing. + */ + @Test + public void testSimplePutDeleteAndGet() { + UUID id = UUID.randomUUID(); + User user1 = new User(id, "kasun", Date.from(Instant.now())); + // storing data; + userDataStore.put(id, user1); + // get data; + User olduser = userDataStore.get(user1.getUserId()); + Assert.assertEquals(olduser.getName(), user1.getName()); + Assert.assertEquals(olduser.getDateOfBirth(), user1.getDateOfBirth()); + // delete data; + userDataStore.delete(user1.getUserId()); + // get data + User deletedUser = userDataStore.get(id); + Assert.assertNull(deletedUser); + } + + /** + * In this test case, schema exists method behavior of the data store is testing. + */ + @Test() + public void testSchemaExists() { + userDataStore.deleteSchema(); + Assert.assertFalse(userDataStore.schemaExists()); + userDataStore.createSchema(); + Assert.assertTrue(userDataStore.schemaExists()); + } + + /** + * In this test case, schema exists method behavior of the data store is testing. + */ + @Test + public void testTruncateSchema() { + if (!userDataStore.schemaExists()) { + userDataStore.createSchema(); + } + UUID id = UUID.randomUUID(); + User user1 = new User(id, "Madhawa Kasun", Date.from(Instant.now())); + userDataStore.put(id, user1); + User olduser = userDataStore.get(id); + Assert.assertEquals(olduser.getName(), user1.getName()); + Assert.assertEquals(olduser.getDateOfBirth(), user1.getDateOfBirth()); + userDataStore.truncateSchema(); + olduser = userDataStore.get(id); + Assert.assertNull(olduser); + } + + /** + * In this test case, get with fields method behavior of the data store is testing. + */ + @Test + public void testGetWithFields() { + UUID id = UUID.randomUUID(); + User user1 = new User(id, "Madhawa Kasun Gunasekara", Date.from(Instant.now())); + userDataStore.put(id, user1); + // get data; + User olduser = userDataStore.get(user1.getUserId()); + Assert.assertEquals(olduser.getName(), user1.getName()); + Assert.assertEquals(olduser.getDateOfBirth(), user1.getDateOfBirth()); + User olduserWithFields = userDataStore.get(id, new String[]{"name"}); + Assert.assertNull(olduserWithFields.getDateOfBirth()); + } + + /** + * In this test case, get with fields method behavior of the data store is testing. + */ + @Test + public void testExecute() throws Exception { + userDataStore.truncateSchema(); + Map<UUID, User> users = new HashMap<>(); + UUID id1 = UUID.randomUUID(); + User user1 = new User(id1, "user1", Date.from(Instant.now())); + users.put(id1, user1); + userDataStore.put(id1, user1); + UUID id2 = UUID.randomUUID(); + User user2 = new User(id2, "user2", Date.from(Instant.now())); + users.put(id2, user2); + userDataStore.put(id2, user2); + UUID id3 = UUID.randomUUID(); + User user3 = new User(id3, "user3", Date.from(Instant.now())); + users.put(id3, user3); + userDataStore.put(id3, user3); + Query<UUID, User> query1 = userDataStore.newQuery(); + Result<UUID, User> result1 = userDataStore.execute(query1); + int i = 0; + Assert.assertEquals(result1.getProgress(), 0.0, 0.0); + while (result1.next()) { + // check objects values + Assert.assertEquals(result1.get().getName(), users.get(result1.getKey()).getName()); + Assert.assertEquals(result1.get().getDateOfBirth(), users.get(result1.getKey()).getDateOfBirth()); + Assert.assertEquals(result1.get().getUserId(), users.get(result1.getKey()).getUserId()); + i++; + } + Assert.assertEquals(result1.getProgress(), 1.0, 0.0); + Assert.assertEquals(3, i); + + // Check limit query + Query<UUID, User> query2 = userDataStore.newQuery(); + query2.setLimit(2); + Result<UUID, User> result2 = userDataStore.execute(query2); + i = 0; + while (result2.next()) { + Assert.assertEquals(result2.get().getName(), users.get(result2.getKey()).getName()); + Assert.assertEquals(result2.get().getDateOfBirth(), users.get(result2.getKey()).getDateOfBirth()); + Assert.assertEquals(result2.get().getUserId(), users.get(result2.getKey()).getUserId()); + i++; + } + Assert.assertEquals(2, i); + + // check key element + Query<UUID, User> query3 = userDataStore.newQuery(); + query3.setKey(id1); + + Result<UUID, User> result3 = userDataStore.execute(query3); + i = 0; + while (result3.next()) { + Assert.assertEquals(result3.get().getName(), users.get(result3.getKey()).getName()); + Assert.assertEquals(result3.get().getDateOfBirth(), users.get(result3.getKey()).getDateOfBirth()); + Assert.assertEquals(result3.get().getUserId(), users.get(result3.getKey()).getUserId()); + i++; + } + Assert.assertEquals(1, i); + } + + /** + * In this test case, delete by query method behavior of the data store is testing. + */ + @Test + public void testDeleteByQuery() throws Exception { + userDataStore.truncateSchema(); + UUID id1 = UUID.randomUUID(); + User user1 = new User(id1, "user1", Date.from(Instant.now())); + userDataStore.put(id1, user1); + UUID id2 = UUID.randomUUID(); + User user2 = new User(id2, "user2", Date.from(Instant.now())); + userDataStore.put(id2, user2); + Query<UUID, User> query1 = userDataStore.newQuery(); + query1.setKey(id1); + userDataStore.deleteByQuery(query1); + User user = userDataStore.get(id1); + Assert.assertNull(user); + + //test deleteByFields + Query<UUID, User> query2 = userDataStore.newQuery(); + query2.setKey(id2); + query2.setFields("name"); + userDataStore.deleteByQuery(query2); + User partialDeletedUser = userDataStore.get(id2); + Assert.assertNull(partialDeletedUser.getName()); + Assert.assertEquals(partialDeletedUser.getDateOfBirth(), user2.getDateOfBirth()); + } + + /** + * In this test case, update by quert method behavior of the data store is testing. + */ + @Test + public void testUpdateByQuery() { + userDataStore.truncateSchema(); + UUID id1 = UUID.randomUUID(); + User user1 = new User(id1, "user1", Date.from(Instant.now())); + userDataStore.put(id1, user1); + UUID id2 = UUID.randomUUID(); + User user2 = new User(id2, "user2", Date.from(Instant.now())); + userDataStore.put(id2, user2); + Query<UUID, User> query1 = userDataStore.newQuery(); + if (query1 instanceof CassandraQuery) { + ((CassandraQuery) query1).addUpdateField("name", "madhawa"); + } + query1.setKey(id1); + if (userDataStore instanceof CassandraStore) { + userDataStore.updateByQuery(query1); + } + User user = userDataStore.get(id1); + Assert.assertEquals(user.getName(), "madhawa"); + } + + @Test + public void testComplexTypes() throws GoraException { + DataStore<String, ComplexTypes> documentDataStore = testDriver.createDataStore(String.class, ComplexTypes.class); + ComplexTypes document = new ComplexTypes("document1"); + document.setIntArrayDataType(new int[]{1, 2, 3}); + document.setStringArrayDataType(new String[]{"madhawa", "kasun", "gunasekara", "pannipitiya", "srilanka"}); + document.setListDataType(new ArrayList<>(Arrays.asList("gora", "nutch", "tika", "opennlp", "olingo"))); + document.setSetDataType(new HashSet<>(Arrays.asList("important", "keeper"))); + HashMap<String, String> map = new HashMap<>(); + map.put("LK", "Colombo"); + document.setMapDataType(map); + documentDataStore.put("document1", document); + ComplexTypes retrievedDocuemnt = documentDataStore.get("document1"); + // verify list data + for (int i = 0; i < document.getListDataType().size(); i++) { + Assert.assertEquals(document.getListDataType().get(i), retrievedDocuemnt.getListDataType().get(i)); + } + // verify set data + for (int i = 0; i < document.getSetDataType().size(); i++) { + Assert.assertTrue(Arrays.equals(document.getSetDataType().toArray(), retrievedDocuemnt.getSetDataType().toArray())); + } + // verify array data + for (int i = 0; i < document.getIntArrayDataType().length; i++) { + Assert.assertTrue(Arrays.equals(document.getIntArrayDataType(), retrievedDocuemnt.getIntArrayDataType())); + } + for (int i = 0; i < document.getStringArrayDataType().length; i++) { + Assert.assertTrue(Arrays.equals(document.getStringArrayDataType(), retrievedDocuemnt.getStringArrayDataType())); + } + // verify map data + Assert.assertEquals(map.get("LK"), retrievedDocuemnt.getMapDataType().get("LK")); + } +} http://git-wip-us.apache.org/repos/asf/gora/blob/89683c74/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/TestNativeSerializationWithUDT.java ---------------------------------------------------------------------- diff --git a/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/TestNativeSerializationWithUDT.java b/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/TestNativeSerializationWithUDT.java new file mode 100644 index 0000000..f9b5df4 --- /dev/null +++ b/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/TestNativeSerializationWithUDT.java @@ -0,0 +1,87 @@ +/** + * 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.cassandra.store; + +import org.apache.gora.cassandra.GoraCassandraTestDriver; +import org.apache.gora.cassandra.example.generated.nativeSerialization.Customer; +import org.apache.gora.cassandra.example.generated.nativeSerialization.Document; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.Properties; + +/** + * This class contains the tests cases to test the behaviour of Native Serialization with UDT dataType. + */ +public class TestNativeSerializationWithUDT { + + private static GoraCassandraTestDriver testDriver = new GoraCassandraTestDriver(); + private static CassandraStore<String, Document> documentCassandraStore; + private static Properties parameter; + + @BeforeClass + public static void setUpClass() throws Exception { + setProperties(); + testDriver.setParameters(parameter); + testDriver.setUpClass(); + documentCassandraStore = (CassandraStore<String, Document>) testDriver.createDataStore(String.class, Document.class); + } + + private static void setProperties() { + parameter = new Properties(); + parameter.setProperty(CassandraStoreParameters.CASSANDRA_SERVERS, "localhost"); + parameter.setProperty(CassandraStoreParameters.PORT, "9042"); + parameter.setProperty(CassandraStoreParameters.CASSANDRA_SERIALIZATION_TYPE, "native"); + parameter.setProperty(CassandraStoreParameters.PROTOCOL_VERSION, "3"); + parameter.setProperty(CassandraStoreParameters.CLUSTER_NAME, "Test Cluster"); + parameter.setProperty("gora.cassandrastore.mapping.file", "nativeUDT/gora-cassandra-mapping.xml"); + } + + @AfterClass + public static void tearDownClass() throws Exception { + testDriver.tearDownClass(); + } + + @After + public void tearDown() throws Exception { + testDriver.tearDown(); + } + + /** + * This is for testGetNested() with UDT dataType with native serialization. + */ + @Test + public void testSimplePutAndGEt() { + documentCassandraStore.createSchema(); + Document document = new Document(); + document.setDefaultId("yawamu.com"); + Customer customer = new Customer(); + customer.setId("144"); + customer.setName("Madhawa"); + document.setCustomer(customer); + documentCassandraStore.put("yawamu.com", document); + Document retrievedDocument = documentCassandraStore.get("yawamu.com"); + Assert.assertEquals(customer.getId(), retrievedDocument.getCustomer().getId()); + Assert.assertEquals(customer.getName(), retrievedDocument.getCustomer().getName()); + } + +} http://git-wip-us.apache.org/repos/asf/gora/blob/89683c74/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/package-info.java ---------------------------------------------------------------------- diff --git a/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/package-info.java b/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/package-info.java new file mode 100644 index 0000000..6e1c6b8 --- /dev/null +++ b/gora-cassandra/src/test/java/org/apache/gora/cassandra/store/package-info.java @@ -0,0 +1,21 @@ +/* + * 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 + * + * 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. + */ +/** + * This package contains all the unit tests for basic CRUD operations + * functionality of the Cassandra dataStore. + */ +package org.apache.gora.cassandra.store; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/gora/blob/89683c74/gora-cassandra/src/test/java/org/apache/gora/cassandra/test/nativeSerialization/DateAsStringCodec.java ---------------------------------------------------------------------- diff --git a/gora-cassandra/src/test/java/org/apache/gora/cassandra/test/nativeSerialization/DateAsStringCodec.java b/gora-cassandra/src/test/java/org/apache/gora/cassandra/test/nativeSerialization/DateAsStringCodec.java new file mode 100644 index 0000000..b5ffedb --- /dev/null +++ b/gora-cassandra/src/test/java/org/apache/gora/cassandra/test/nativeSerialization/DateAsStringCodec.java @@ -0,0 +1,50 @@ +/* + * 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 + * + * 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.apache.gora.cassandra.test.nativeSerialization; + +import com.datastax.driver.core.TypeCodec; +import com.datastax.driver.extras.codecs.MappingCodec; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +/** + * Sample Class for Custom Codec + * {@link com.datastax.driver.extras.codecs.MappingCodec} + */ +public class DateAsStringCodec extends MappingCodec<String, Date> { + public DateAsStringCodec() { + super(TypeCodec.timestamp(), String.class); + } + + @Override + protected Date serialize(String value) { + try { + return new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH).parse(value); + } catch (ParseException e) { + throw new RuntimeException(e); + } + } + + @Override + protected String deserialize(Date value) { + return String.valueOf(value); + } +} http://git-wip-us.apache.org/repos/asf/gora/blob/89683c74/gora-tutorial/pom.xml ---------------------------------------------------------------------- diff --git a/gora-tutorial/pom.xml b/gora-tutorial/pom.xml index 270a2ba..99ba41d 100644 --- a/gora-tutorial/pom.xml +++ b/gora-tutorial/pom.xml @@ -115,7 +115,7 @@ <dependency> <groupId>org.apache.gora</groupId> - <artifactId>gora-cassandra-cql</artifactId> + <artifactId>gora-cassandra</artifactId> </dependency> <dependency> http://git-wip-us.apache.org/repos/asf/gora/blob/89683c74/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index ed1e157..b67d263 100644 --- a/pom.xml +++ b/pom.xml @@ -718,7 +718,7 @@ <module>gora-compiler-cli</module> <module>gora-core</module> <module>gora-accumulo</module> - <module>gora-cassandra-cql</module> + <module>gora-cassandra</module> <module>gora-goraci</module> <module>gora-hbase</module> <module>gora-infinispan</module> @@ -855,12 +855,12 @@ <dependency> <groupId>org.apache.gora</groupId> - <artifactId>gora-cassandra-cql</artifactId> + <artifactId>gora-cassandra</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.apache.gora</groupId> - <artifactId>gora-cassandra-cql</artifactId> + <artifactId>gora-cassandra</artifactId> <version>${project.version}</version> <type>test-jar</type> </dependency>
