From f7de3257c288dbfc431f34037836fa70552fd29a Mon Sep 17 00:00:00 2001 From: Albert Shift Date: Mon, 20 Apr 2015 22:50:40 -0700 Subject: [PATCH] UDT tests decomposition --- .../core/udtcollection/UDTCollectionTest.java | 248 +----------------- .../core/udtcollection/UDTListTest.java | 168 ++++++++++++ .../core/udtcollection/UDTSetTest.java | 125 +++++++++ 3 files changed, 296 insertions(+), 245 deletions(-) create mode 100644 src/test/java/com/noorq/casser/test/integration/core/udtcollection/UDTListTest.java create mode 100644 src/test/java/com/noorq/casser/test/integration/core/udtcollection/UDTSetTest.java diff --git a/src/test/java/com/noorq/casser/test/integration/core/udtcollection/UDTCollectionTest.java b/src/test/java/com/noorq/casser/test/integration/core/udtcollection/UDTCollectionTest.java index c79e781..b391ab4 100644 --- a/src/test/java/com/noorq/casser/test/integration/core/udtcollection/UDTCollectionTest.java +++ b/src/test/java/com/noorq/casser/test/integration/core/udtcollection/UDTCollectionTest.java @@ -15,15 +15,6 @@ */ package com.noorq.casser.test.integration.core.udtcollection; -import static com.noorq.casser.core.Query.eq; -import static com.noorq.casser.core.Query.getIdx; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -31,7 +22,7 @@ import com.noorq.casser.core.Casser; import com.noorq.casser.core.CasserSession; import com.noorq.casser.test.integration.build.AbstractEmbeddedCassandraTest; -public class UDTCollectionTest extends AbstractEmbeddedCassandraTest { +public abstract class UDTCollectionTest extends AbstractEmbeddedCassandraTest { static Book book = Casser.dsl(Book.class); @@ -47,240 +38,7 @@ public class UDTCollectionTest extends AbstractEmbeddedCassandraTest { System.out.println(book); } - @Test - public void testSetCRUID() { - - int id = 555; - - // CREATE - - Set reviewers = new HashSet(); - reviewers.add(new AuthorImpl("Alex", "San Jose")); - reviewers.add(new AuthorImpl("Bob", "San Francisco")); - - session.insert() - .value(book::id, id) - .value(book::reviewers, reviewers) - .sync(); - - // READ - - Book actual = session.select(Book.class).where(book::id, eq(id)).sync().findFirst().get(); - Assert.assertEquals(id, actual.id()); - assertEqualSets(reviewers, actual.reviewers()); - - // UPDATE - - Set expected = new HashSet(); - expected.add(new AuthorImpl("Craig", "Los Altos")); - - session.update().set(book::reviewers, expected).where(book::id, eq(id)).sync(); - - Set actualSet = session.select(book::reviewers).where(book::id, eq(id)).sync().findFirst().get()._1; - assertEqualSets(expected, actualSet); - - // add operation - - expected.add(new AuthorImpl("Add", "AddCity")); - session.update().add(book::reviewers, new AuthorImpl("Add", "AddCity")) - .where(book::id, eq(id)).sync(); - - actualSet = session.select(book::reviewers).where(book::id, eq(id)).sync().findFirst().get()._1; - assertEqualSets(expected, actualSet); - - // addAll operation - expected.addAll(reviewers); - session.update().addAll(book::reviewers, reviewers).where(book::id, eq(id)).sync(); - - actualSet = session.select(book::reviewers).where(book::id, eq(id)).sync().findFirst().get()._1; - assertEqualSets(expected, actualSet); - - // DELETE - - // remove single value - - Author a = expected.stream().filter(p -> p.name().equals("Add")).findFirst().get(); - expected.remove(a); - - session.update().remove(book::reviewers, a).where(book::id, eq(id)).sync(); - - actualSet = session.select(book::reviewers).where(book::id, eq(id)).sync().findFirst().get()._1; - assertEqualSets(expected, actualSet); - - // remove values - - expected.remove(expected.stream().filter(p -> p.name().equals("Alex")).findFirst().get()); - expected.remove(expected.stream().filter(p -> p.name().equals("Bob")).findFirst().get()); - session.update().removeAll(book::reviewers, reviewers).where(book::id, eq(id)).sync(); - - actualSet = session.select(book::reviewers).where(book::id, eq(id)).sync().findFirst().get()._1; - assertEqualSets(expected, actualSet); - - // remove full list - - session.update().set(book::reviewers, null).where(book::id, eq(id)).sync(); - - actualSet = session.select(book::reviewers).where(book::id, eq(id)).sync().findFirst().get()._1; - Assert.assertNull(actualSet); - - // remove object - - session.delete().where(book::id, eq(id)).sync(); - Long cnt = session.count().where(book::id, eq(id)).sync(); - Assert.assertEquals(Long.valueOf(0), cnt); - - - } - - private void assertEqualSets(Set expected, Set actual) { - Assert.assertEquals(expected.size(), actual.size()); - - for (Author e : expected) { - Author a = actual.stream().filter(p -> p.name().equals(e.name())).findFirst().get(); - Assert.assertEquals(e.city(), a.city()); - } - - } - - - @Test - public void testListCRUID() { - - int id = 777; - - List authors = new ArrayList(); - authors.add(new AuthorImpl("Alex", "San Jose")); - authors.add(new AuthorImpl("Bob", "San Francisco")); - - // CREATE - - session.insert() - .value(book::id, id) - .value(book::authors, authors) - .sync(); - - // READ - - // read full object - - Book actual = session.select(Book.class).where(book::id, eq(id)).sync().findFirst().get(); - Assert.assertEquals(id, actual.id()); - assertEqualLists(authors, actual.authors()); - Assert.assertNull(actual.reviewers()); - Assert.assertNull(actual.contents()); - - // read full list - - List actualList = session.select(book::authors).where(book::id, eq(id)).sync().findFirst().get()._1; - assertEqualLists(authors, actualList); - - // read single value by index - - String cql = session.select(getIdx(book::authors, 1)) - .where(book::id, eq(id)).cql(); - - System.out.println("Still not supporting cql = " + cql); - - // UPDATE - - List expected = new ArrayList(); - expected.add(new AuthorImpl("Unknown", "City 17")); - - session.update().set(book::authors, expected).where(book::id, eq(id)).sync(); - - actual = session.select(Book.class).where(book::id, eq(id)).sync().findFirst().get(); - Assert.assertEquals(id, actual.id()); - assertEqualLists(expected, actual.authors()); - - // INSERT - - // prepend operation - - expected.add(0, new AuthorImpl("Prepend", "PrependCity")); - session.update().prepend(book::authors, new AuthorImpl("Prepend", "PrependCity")).where(book::id, eq(id)).sync(); - - actualList = session.select(book::authors).where(book::id, eq(id)).sync().findFirst().get()._1; - assertEqualLists(expected, actualList); - - // append operation - - expected.add(new AuthorImpl("Append", "AppendCity")); - session.update().append(book::authors, new AuthorImpl("Append", "AppendCity")).where(book::id, eq(id)).sync(); - - actualList = session.select(book::authors).where(book::id, eq(id)).sync().findFirst().get()._1; - assertEqualLists(expected, actualList); - - // prependAll operation - expected.addAll(0, authors); - session.update().prependAll(book::authors, authors).where(book::id, eq(id)).sync(); - - actualList = session.select(book::authors).where(book::id, eq(id)).sync().findFirst().get()._1; - assertEqualLists(expected, actualList); - - // appendAll operation - expected.addAll(authors); - session.update().appendAll(book::authors, authors).where(book::id, eq(id)).sync(); - - actualList = session.select(book::authors).where(book::id, eq(id)).sync().findFirst().get()._1; - assertEqualLists(expected, actualList); - - // set by Index - - Author inserted = new AuthorImpl("Insert", "InsertCity"); - expected.set(5, inserted); - session.update().setIdx(book::authors, 5, inserted).where(book::id, eq(id)).sync(); - - actualList = session.select(book::authors).where(book::id, eq(id)).sync().findFirst().get()._1; - assertEqualLists(expected, actualList); - - // DELETE - - // remove single value - - expected.remove(inserted); - session.update().discard(book::authors, inserted).where(book::id, eq(id)).sync(); - - actualList = session.select(book::authors).where(book::id, eq(id)).sync().findFirst().get()._1; - assertEqualLists(expected, actualList); - - // remove values - - expected.removeAll(authors); - session.update().discardAll(book::authors, authors).where(book::id, eq(id)).sync(); - - actualList = session.select(book::authors).where(book::id, eq(id)).sync().findFirst().get()._1; - assertEqualLists(expected, actualList); - - // remove full list - - session.update().set(book::authors, null).where(book::id, eq(id)).sync(); - - actualList = session.select(book::authors).where(book::id, eq(id)).sync().findFirst().get()._1; - Assert.assertNull(actualList); - - // remove object - - session.delete().where(book::id, eq(id)).sync(); - Long cnt = session.count().where(book::id, eq(id)).sync(); - Assert.assertEquals(Long.valueOf(0), cnt); - - } - - private void assertEqualLists(List expected, List actual) { - Assert.assertEquals(expected.size(), actual.size()); - - int size = expected.size(); - - for (int i = 0; i != size; ++i) { - Author e = expected.get(i); - Author a = actual.get(i); - Assert.assertEquals(e.name(), a.name()); - Assert.assertEquals(e.city(), a.city()); - } - - } - - private static final class AuthorImpl implements Author { + public static final class AuthorImpl implements Author { String name; String city; @@ -338,7 +96,7 @@ public class UDTCollectionTest extends AbstractEmbeddedCassandraTest { } - private static final class SectionImpl implements Section { + public static final class SectionImpl implements Section { String title; int page; diff --git a/src/test/java/com/noorq/casser/test/integration/core/udtcollection/UDTListTest.java b/src/test/java/com/noorq/casser/test/integration/core/udtcollection/UDTListTest.java new file mode 100644 index 0000000..d7706e9 --- /dev/null +++ b/src/test/java/com/noorq/casser/test/integration/core/udtcollection/UDTListTest.java @@ -0,0 +1,168 @@ +/* + * Copyright (C) 2015 Noorq, Inc. + * + * 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 com.noorq.casser.test.integration.core.udtcollection; + +import static com.noorq.casser.core.Query.eq; +import static com.noorq.casser.core.Query.getIdx; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; + +public class UDTListTest extends UDTCollectionTest { + + @Test + public void testListCRUID() { + + int id = 777; + + List authors = new ArrayList(); + authors.add(new AuthorImpl("Alex", "San Jose")); + authors.add(new AuthorImpl("Bob", "San Francisco")); + + // CREATE + + session.insert() + .value(book::id, id) + .value(book::authors, authors) + .sync(); + + // READ + + // read full object + + Book actual = session.select(Book.class).where(book::id, eq(id)).sync().findFirst().get(); + Assert.assertEquals(id, actual.id()); + assertEqualLists(authors, actual.authors()); + Assert.assertNull(actual.reviewers()); + Assert.assertNull(actual.contents()); + + // read full list + + List actualList = session.select(book::authors).where(book::id, eq(id)).sync().findFirst().get()._1; + assertEqualLists(authors, actualList); + + // read single value by index + + String cql = session.select(getIdx(book::authors, 1)) + .where(book::id, eq(id)).cql(); + + System.out.println("Still not supporting cql = " + cql); + + // UPDATE + + List expected = new ArrayList(); + expected.add(new AuthorImpl("Unknown", "City 17")); + + session.update().set(book::authors, expected).where(book::id, eq(id)).sync(); + + actual = session.select(Book.class).where(book::id, eq(id)).sync().findFirst().get(); + Assert.assertEquals(id, actual.id()); + assertEqualLists(expected, actual.authors()); + + // INSERT + + // prepend operation + + expected.add(0, new AuthorImpl("Prepend", "PrependCity")); + session.update().prepend(book::authors, new AuthorImpl("Prepend", "PrependCity")).where(book::id, eq(id)).sync(); + + actualList = session.select(book::authors).where(book::id, eq(id)).sync().findFirst().get()._1; + assertEqualLists(expected, actualList); + + // append operation + + expected.add(new AuthorImpl("Append", "AppendCity")); + session.update().append(book::authors, new AuthorImpl("Append", "AppendCity")).where(book::id, eq(id)).sync(); + + actualList = session.select(book::authors).where(book::id, eq(id)).sync().findFirst().get()._1; + assertEqualLists(expected, actualList); + + // prependAll operation + expected.addAll(0, authors); + session.update().prependAll(book::authors, authors).where(book::id, eq(id)).sync(); + + actualList = session.select(book::authors).where(book::id, eq(id)).sync().findFirst().get()._1; + assertEqualLists(expected, actualList); + + // appendAll operation + expected.addAll(authors); + session.update().appendAll(book::authors, authors).where(book::id, eq(id)).sync(); + + actualList = session.select(book::authors).where(book::id, eq(id)).sync().findFirst().get()._1; + assertEqualLists(expected, actualList); + + // set by Index + + Author inserted = new AuthorImpl("Insert", "InsertCity"); + expected.set(5, inserted); + session.update().setIdx(book::authors, 5, inserted).where(book::id, eq(id)).sync(); + + actualList = session.select(book::authors).where(book::id, eq(id)).sync().findFirst().get()._1; + assertEqualLists(expected, actualList); + + // DELETE + + // remove single value + + expected.remove(inserted); + session.update().discard(book::authors, inserted).where(book::id, eq(id)).sync(); + + actualList = session.select(book::authors).where(book::id, eq(id)).sync().findFirst().get()._1; + assertEqualLists(expected, actualList); + + // remove values + + expected.removeAll(authors); + session.update().discardAll(book::authors, authors).where(book::id, eq(id)).sync(); + + actualList = session.select(book::authors).where(book::id, eq(id)).sync().findFirst().get()._1; + assertEqualLists(expected, actualList); + + // remove full list + + session.update().set(book::authors, null).where(book::id, eq(id)).sync(); + + actualList = session.select(book::authors).where(book::id, eq(id)).sync().findFirst().get()._1; + Assert.assertNull(actualList); + + // remove object + + session.delete().where(book::id, eq(id)).sync(); + Long cnt = session.count().where(book::id, eq(id)).sync(); + Assert.assertEquals(Long.valueOf(0), cnt); + + } + + private void assertEqualLists(List expected, List actual) { + Assert.assertEquals(expected.size(), actual.size()); + + int size = expected.size(); + + for (int i = 0; i != size; ++i) { + Author e = expected.get(i); + Author a = actual.get(i); + Assert.assertEquals(e.name(), a.name()); + Assert.assertEquals(e.city(), a.city()); + } + + } + +} + + diff --git a/src/test/java/com/noorq/casser/test/integration/core/udtcollection/UDTSetTest.java b/src/test/java/com/noorq/casser/test/integration/core/udtcollection/UDTSetTest.java new file mode 100644 index 0000000..c814320 --- /dev/null +++ b/src/test/java/com/noorq/casser/test/integration/core/udtcollection/UDTSetTest.java @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2015 Noorq, Inc. + * + * 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 com.noorq.casser.test.integration.core.udtcollection; + +import static com.noorq.casser.core.Query.eq; + +import java.util.HashSet; +import java.util.Set; + +import org.junit.Assert; +import org.junit.Test; + +public class UDTSetTest extends UDTCollectionTest { + + @Test + public void testSetCRUID() { + + int id = 555; + + // CREATE + + Set reviewers = new HashSet(); + reviewers.add(new AuthorImpl("Alex", "San Jose")); + reviewers.add(new AuthorImpl("Bob", "San Francisco")); + + session.insert() + .value(book::id, id) + .value(book::reviewers, reviewers) + .sync(); + + // READ + + Book actual = session.select(Book.class).where(book::id, eq(id)).sync().findFirst().get(); + Assert.assertEquals(id, actual.id()); + assertEqualSets(reviewers, actual.reviewers()); + + // UPDATE + + Set expected = new HashSet(); + expected.add(new AuthorImpl("Craig", "Los Altos")); + + session.update().set(book::reviewers, expected).where(book::id, eq(id)).sync(); + + Set actualSet = session.select(book::reviewers).where(book::id, eq(id)).sync().findFirst().get()._1; + assertEqualSets(expected, actualSet); + + // add operation + + expected.add(new AuthorImpl("Add", "AddCity")); + session.update().add(book::reviewers, new AuthorImpl("Add", "AddCity")) + .where(book::id, eq(id)).sync(); + + actualSet = session.select(book::reviewers).where(book::id, eq(id)).sync().findFirst().get()._1; + assertEqualSets(expected, actualSet); + + // addAll operation + expected.addAll(reviewers); + session.update().addAll(book::reviewers, reviewers).where(book::id, eq(id)).sync(); + + actualSet = session.select(book::reviewers).where(book::id, eq(id)).sync().findFirst().get()._1; + assertEqualSets(expected, actualSet); + + // DELETE + + // remove single value + + Author a = expected.stream().filter(p -> p.name().equals("Add")).findFirst().get(); + expected.remove(a); + + session.update().remove(book::reviewers, a).where(book::id, eq(id)).sync(); + + actualSet = session.select(book::reviewers).where(book::id, eq(id)).sync().findFirst().get()._1; + assertEqualSets(expected, actualSet); + + // remove values + + expected.remove(expected.stream().filter(p -> p.name().equals("Alex")).findFirst().get()); + expected.remove(expected.stream().filter(p -> p.name().equals("Bob")).findFirst().get()); + session.update().removeAll(book::reviewers, reviewers).where(book::id, eq(id)).sync(); + + actualSet = session.select(book::reviewers).where(book::id, eq(id)).sync().findFirst().get()._1; + assertEqualSets(expected, actualSet); + + // remove full list + + session.update().set(book::reviewers, null).where(book::id, eq(id)).sync(); + + actualSet = session.select(book::reviewers).where(book::id, eq(id)).sync().findFirst().get()._1; + Assert.assertNull(actualSet); + + // remove object + + session.delete().where(book::id, eq(id)).sync(); + Long cnt = session.count().where(book::id, eq(id)).sync(); + Assert.assertEquals(Long.valueOf(0), cnt); + + + } + + private void assertEqualSets(Set expected, Set actual) { + Assert.assertEquals(expected.size(), actual.size()); + + for (Author e : expected) { + Author a = actual.stream().filter(p -> p.name().equals(e.name())).findFirst().get(); + Assert.assertEquals(e.city(), a.city()); + } + + } + +} + +