test collections and implement collection operations

This commit is contained in:
Albert Shift 2015-04-17 14:26:30 -07:00
parent 27a6bd1e9d
commit f627eb9741
15 changed files with 1076 additions and 17 deletions

View file

@ -43,7 +43,7 @@
<cassandra-unit.version>2.0.2.2</cassandra-unit.version> <cassandra-unit.version>2.0.2.2</cassandra-unit.version>
<cassandra-driver-core.version>2.1.5</cassandra-driver-core.version> <cassandra-driver-core.version>2.1.5</cassandra-driver-core.version>
<cassandra>2.1.2</cassandra> <cassandra>2.1.4</cassandra>
<guava.version>16.0.1</guava.version> <guava.version>16.0.1</guava.version>
<hamcrest>1.3</hamcrest> <hamcrest>1.3</hamcrest>

View file

@ -17,6 +17,7 @@ package com.noorq.casser.core;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import com.datastax.driver.core.querybuilder.BindMarker; import com.datastax.driver.core.querybuilder.BindMarker;
import com.datastax.driver.core.querybuilder.QueryBuilder; import com.datastax.driver.core.querybuilder.QueryBuilder;
@ -72,12 +73,31 @@ public final class Query {
return new Postulate<V>(Operator.IN, vals); return new Postulate<V>(Operator.IN, vals);
} }
public static <K,V> Getter<V> get(Getter<List<V>> listGetter, int index) { public static <K,V> Getter<V> getIdx(Getter<List<V>> listGetter, int index) {
return null; Objects.requireNonNull(listGetter, "listGetter is null");
return new Getter<V>() {
@Override
public V get() {
return listGetter.get().get(index);
}
};
} }
public static <K, V> Getter<V> get(Getter<Map<K, V>> mapGetter, K k) { public static <K, V> Getter<V> get(Getter<Map<K, V>> mapGetter, K k) {
return null; Objects.requireNonNull(mapGetter, "mapGetter is null");
Objects.requireNonNull(k, "key is null");
return new Getter<V>() {
@Override
public V get() {
return mapGetter.get().get(k);
}
};
} }

View file

@ -309,7 +309,6 @@ public final class UpdateOperation extends AbstractFilterOperation<ResultSet, Up
Objects.requireNonNull(mapGetter, "mapGetter is empty"); Objects.requireNonNull(mapGetter, "mapGetter is empty");
Objects.requireNonNull(key, "key is empty"); Objects.requireNonNull(key, "key is empty");
Objects.requireNonNull(value, "value is empty");
CasserPropertyNode p = MappingUtil.resolveMappingProperty(mapGetter); CasserPropertyNode p = MappingUtil.resolveMappingProperty(mapGetter);
//Object valueObj = sessionOps.getValuePreparer().prepareColumnValue(value, p.getProperty()); //Object valueObj = sessionOps.getValuePreparer().prepareColumnValue(value, p.getProperty());

View file

@ -0,0 +1,101 @@
/*
* 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.core.reflect;
import java.lang.reflect.Method;
import java.util.Optional;
import java.util.function.Function;
import com.noorq.casser.core.SessionRepository;
import com.noorq.casser.mapping.CasserEntity;
import com.noorq.casser.mapping.CasserProperty;
import com.noorq.casser.mapping.ColumnType;
import com.noorq.casser.mapping.IdentityName;
import com.noorq.casser.mapping.OrderingDirection;
import com.noorq.casser.mapping.type.AbstractDataType;
import com.noorq.casser.support.CasserMappingException;
public final class CasserNamedProperty implements CasserProperty {
private final String name;
public CasserNamedProperty(String name) {
this.name = name;
}
@Override
public CasserEntity getEntity() {
throw new CasserMappingException("will never called");
}
@Override
public String getPropertyName() {
return name;
}
@Override
public Method getGetterMethod() {
throw new CasserMappingException("will never called");
}
@Override
public IdentityName getColumnName() {
return IdentityName.of(name, false);
}
@Override
public Optional<IdentityName> getIndexName() {
return Optional.empty();
}
@Override
public Class<?> getJavaType() {
throw new CasserMappingException("will never called");
}
@Override
public AbstractDataType getDataType() {
throw new CasserMappingException("will never called");
}
@Override
public ColumnType getColumnType() {
return ColumnType.COLUMN;
}
@Override
public int getOrdinal() {
return 0;
}
@Override
public OrderingDirection getOrdering() {
return OrderingDirection.ASC;
}
@Override
public Optional<Function<Object, Object>> getReadConverter(
SessionRepository repository) {
return Optional.empty();
}
@Override
public Optional<Function<Object, Object>> getWriteConverter(
SessionRepository repository) {
return Optional.empty();
}
}

View file

@ -37,13 +37,29 @@ public final class CasserPropertyNode implements Iterable<CasserProperty> {
public String getColumnName() { public String getColumnName() {
if (next.isPresent()) { if (next.isPresent()) {
List<String> columnNames = new ArrayList<String>(); List<String> columnNames = new ArrayList<String>();
for (CasserProperty p : this) { for (CasserProperty p : this) {
columnNames.add(p.getColumnName().toCql(true)); columnNames.add(p.getColumnName().toCql(true));
} }
Collections.reverse(columnNames); Collections.reverse(columnNames);
if (prop instanceof CasserNamedProperty) {
int size = columnNames.size();
StringBuilder str = new StringBuilder();
for (int i = 0; i != size -1; ++i) {
if (str.length() != 0) {
str.append(".");
}
str.append(columnNames.get(i));
}
str.append("[").append(columnNames.get(size-1)).append("]");
return str.toString();
}
else {
return columnNames.stream().collect(Collectors.joining(".")); return columnNames.stream().collect(Collectors.joining("."));
} }
}
else { else {
return prop.getColumnName().toCql(); return prop.getColumnName().toCql();
} }

View file

@ -21,6 +21,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.TupleType; import com.datastax.driver.core.TupleType;
import com.datastax.driver.core.TupleValue; import com.datastax.driver.core.TupleValue;
import com.datastax.driver.core.UDTValue; import com.datastax.driver.core.UDTValue;
@ -114,8 +115,11 @@ public class DslInvocationHandler<E> implements InvocationHandler {
if (type instanceof DTDataType) { if (type instanceof DTDataType) {
DTDataType dataType = (DTDataType) type; DTDataType dataType = (DTDataType) type;
DataType dt = dataType.getDataType();
if (dataType.getDataType() instanceof TupleType) { switch(dt.getName()) {
case TUPLE:
Object childDsl = tupleMap.get(method); Object childDsl = tupleMap.get(method);
@ -123,7 +127,23 @@ public class DslInvocationHandler<E> implements InvocationHandler {
return childDsl; return childDsl;
} }
break;
case SET:
return new SetDsl(new CasserPropertyNode(prop, parent));
case LIST:
return new ListDsl(new CasserPropertyNode(prop, parent));
case MAP:
return new MapDsl(new CasserPropertyNode(prop, parent));
default:
break;
} }
} }
throw new DslPropertyException(new CasserPropertyNode(prop, parent)); throw new DslPropertyException(new CasserPropertyNode(prop, parent));

View file

@ -0,0 +1,184 @@
/*
* 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.core.reflect;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Optional;
import com.noorq.casser.mapping.CasserProperty;
import com.noorq.casser.support.CasserMappingException;
import com.noorq.casser.support.DslPropertyException;
public final class ListDsl<V> implements List<V> {
private final CasserPropertyNode parent;
public ListDsl(CasserPropertyNode parent) {
this.parent = parent;
}
public CasserPropertyNode getParent() {
return parent;
}
@Override
public V get(int index) {
CasserProperty prop = new CasserNamedProperty(Integer.toString(index));
throw new DslPropertyException(new CasserPropertyNode(prop, Optional.of(parent)));
}
@Override
public int size() {
throwShouldNeverCall();
return 0;
}
@Override
public boolean isEmpty() {
throwShouldNeverCall();
return false;
}
@Override
public boolean contains(Object o) {
throwShouldNeverCall();
return false;
}
@Override
public Iterator<V> iterator() {
throwShouldNeverCall();
return null;
}
@Override
public Object[] toArray() {
throwShouldNeverCall();
return null;
}
@Override
public <T> T[] toArray(T[] a) {
throwShouldNeverCall();
return null;
}
@Override
public boolean add(V e) {
throwShouldNeverCall();
return false;
}
@Override
public boolean remove(Object o) {
throwShouldNeverCall();
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
throwShouldNeverCall();
return false;
}
@Override
public boolean addAll(Collection<? extends V> c) {
throwShouldNeverCall();
return false;
}
@Override
public boolean addAll(int index, Collection<? extends V> c) {
throwShouldNeverCall();
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
throwShouldNeverCall();
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
throwShouldNeverCall();
return false;
}
@Override
public void clear() {
throwShouldNeverCall();
}
@Override
public V set(int index, V element) {
throwShouldNeverCall();
return null;
}
@Override
public void add(int index, V element) {
throwShouldNeverCall();
}
@Override
public V remove(int index) {
throwShouldNeverCall();
return null;
}
@Override
public int indexOf(Object o) {
throwShouldNeverCall();
return 0;
}
@Override
public int lastIndexOf(Object o) {
throwShouldNeverCall();
return 0;
}
@Override
public ListIterator<V> listIterator() {
throwShouldNeverCall();
return null;
}
@Override
public ListIterator<V> listIterator(int index) {
throwShouldNeverCall();
return null;
}
@Override
public List<V> subList(int fromIndex, int toIndex) {
throwShouldNeverCall();
return null;
}
private void throwShouldNeverCall() {
throw new CasserMappingException("should be never called");
}
@Override
public String toString() {
return "ListDsl";
}
}

View file

@ -0,0 +1,118 @@
/*
* 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.core.reflect;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import com.noorq.casser.mapping.CasserProperty;
import com.noorq.casser.support.CasserMappingException;
import com.noorq.casser.support.DslPropertyException;
public final class MapDsl<K, V> implements Map<K, V> {
private final CasserPropertyNode parent;
public MapDsl(CasserPropertyNode parent) {
this.parent = parent;
}
public CasserPropertyNode getParent() {
return parent;
}
@Override
public V get(Object key) {
CasserProperty prop = new CasserNamedProperty(key.toString());
throw new DslPropertyException(new CasserPropertyNode(prop, Optional.of(parent)));
}
@Override
public int size() {
throwShouldNeverCall();
return 0;
}
@Override
public boolean isEmpty() {
throwShouldNeverCall();
return false;
}
@Override
public boolean containsKey(Object key) {
throwShouldNeverCall();
return false;
}
@Override
public boolean containsValue(Object value) {
throwShouldNeverCall();
return false;
}
@Override
public V put(K key, V value) {
throwShouldNeverCall();
return null;
}
@Override
public V remove(Object key) {
throwShouldNeverCall();
return null;
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {
throwShouldNeverCall();
}
@Override
public void clear() {
throwShouldNeverCall();
}
@Override
public Set<K> keySet() {
throwShouldNeverCall();
return null;
}
@Override
public Collection<V> values() {
throwShouldNeverCall();
return null;
}
@Override
public Set<java.util.Map.Entry<K, V>> entrySet() {
throwShouldNeverCall();
return null;
}
private void throwShouldNeverCall() {
throw new CasserMappingException("should be never called");
}
@Override
public String toString() {
return "MapDsl";
}
}

View file

@ -0,0 +1,121 @@
/*
* 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.core.reflect;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import com.noorq.casser.support.CasserMappingException;
public final class SetDsl<V> implements Set<V> {
private final CasserPropertyNode parent;
public SetDsl(CasserPropertyNode parent) {
this.parent = parent;
}
public CasserPropertyNode getParent() {
return parent;
}
@Override
public int size() {
throwShouldNeverCall();
return 0;
}
@Override
public boolean isEmpty() {
throwShouldNeverCall();
return false;
}
@Override
public boolean contains(Object o) {
throwShouldNeverCall();
return false;
}
@Override
public Iterator<V> iterator() {
throwShouldNeverCall();
return null;
}
@Override
public Object[] toArray() {
throwShouldNeverCall();
return null;
}
@Override
public <T> T[] toArray(T[] a) {
throwShouldNeverCall();
return null;
}
@Override
public boolean add(V e) {
throwShouldNeverCall();
return false;
}
@Override
public boolean remove(Object o) {
throwShouldNeverCall();
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
throwShouldNeverCall();
return false;
}
@Override
public boolean addAll(Collection<? extends V> c) {
throwShouldNeverCall();
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
throwShouldNeverCall();
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
throwShouldNeverCall();
return false;
}
@Override
public void clear() {
throwShouldNeverCall();
}
private void throwShouldNeverCall() {
throw new CasserMappingException("should be never called");
}
@Override
public String toString() {
return "SetDsl";
}
}

View file

@ -22,7 +22,10 @@ import com.noorq.casser.core.Casser;
import com.noorq.casser.core.Getter; import com.noorq.casser.core.Getter;
import com.noorq.casser.core.reflect.CasserPropertyNode; import com.noorq.casser.core.reflect.CasserPropertyNode;
import com.noorq.casser.core.reflect.DslExportable; import com.noorq.casser.core.reflect.DslExportable;
import com.noorq.casser.core.reflect.ListDsl;
import com.noorq.casser.core.reflect.MapDsl;
import com.noorq.casser.core.reflect.MapExportable; import com.noorq.casser.core.reflect.MapExportable;
import com.noorq.casser.core.reflect.SetDsl;
import com.noorq.casser.mapping.annotation.Index; import com.noorq.casser.mapping.annotation.Index;
import com.noorq.casser.mapping.annotation.Table; import com.noorq.casser.mapping.annotation.Table;
import com.noorq.casser.mapping.annotation.Tuple; import com.noorq.casser.mapping.annotation.Tuple;
@ -195,6 +198,21 @@ public final class MappingUtil {
return e.getParentDslCasserPropertyNode(); return e.getParentDslCasserPropertyNode();
} }
else if (childDsl instanceof MapDsl) {
MapDsl mapDsl = (MapDsl) childDsl;
return mapDsl.getParent();
}
else if (childDsl instanceof ListDsl) {
ListDsl listDsl = (ListDsl) childDsl;
return listDsl.getParent();
}
else if (childDsl instanceof SetDsl) {
SetDsl setDsl = (SetDsl) childDsl;
return setDsl.getParent();
}
throw new CasserMappingException( throw new CasserMappingException(
"getter must reference to the dsl object " + getter); "getter must reference to the dsl object " + getter);

View file

@ -65,9 +65,15 @@ public abstract class AbstractEmbeddedCassandraTest {
KeyspaceMetadata kmd = cluster.getMetadata().getKeyspace(keyspace); KeyspaceMetadata kmd = cluster.getMetadata().getKeyspace(keyspace);
if (kmd == null) { if (kmd == null) {
session = cluster.connect(); session = cluster.connect();
session.execute("CREATE KEYSPACE " + keyspace
+ " WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1};"); String cql = "CREATE KEYSPACE " + keyspace
session.execute("USE " + keyspace + ";"); + " WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1};";
System.out.println(cql + "\n");
session.execute(cql);
cql = "USE " + keyspace + ";";
System.out.println(cql + "\n");
session.execute(cql);
} else { } else {
session = cluster.connect(keyspace); session = cluster.connect(keyspace);
} }

View file

@ -15,10 +15,19 @@
*/ */
package com.noorq.casser.test.integration.core.collection; package com.noorq.casser.test.integration.core.collection;
import static com.noorq.casser.core.Query.eq;
import static com.noorq.casser.core.Query.get;
import static com.noorq.casser.core.Query.getIdx;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -64,6 +73,328 @@ public class CollectionTest extends AbstractEmbeddedCassandraTest {
} }
@Test @Test
public void testPrint() {
System.out.println(customer);
}
@Test
public void testSetCRUID() {
UUID id = UUID.randomUUID();
Set<String> aliases = new HashSet<String>();
aliases.add("Alex");
aliases.add("Albert");
// CREATE
csession.insert()
.value(customer::id, id)
.value(customer::aliases, aliases)
.sync();
// READ
// read full object
Customer actual = csession.select(Customer.class).where(customer::id, eq(id)).sync().findFirst().get();
Assert.assertEquals(id, actual.id());
Assert.assertEquals(aliases, actual.aliases());
Assert.assertNull(actual.names());
Assert.assertNull(actual.properties());
// read full set
Set<String> actualSet = csession.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertEquals(aliases, actualSet);
// UPDATE
Set<String> expected = new HashSet<String>();
expected.add("unknown");
csession.update().set(customer::aliases, expected).where(customer::id, eq(id)).sync();
actual = csession.select(Customer.class).where(customer::id, eq(id)).sync().findFirst().get();
Assert.assertEquals(id, actual.id());
Assert.assertEquals(expected, actual.aliases());
// INSERT
// add operation
expected.add("add");
csession.update().add(customer::aliases, "add").where(customer::id, eq(id)).sync();
actualSet = csession.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertEquals(expected, actualSet);
// addAll operation
expected.addAll(aliases);
csession.update().addAll(customer::aliases, aliases).where(customer::id, eq(id)).sync();
actualSet = csession.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertEquals(expected, actualSet);
// DELETE
// remove single value
expected.remove("add");
csession.update().remove(customer::aliases, "add").where(customer::id, eq(id)).sync();
actualSet = csession.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertEquals(expected, actualSet);
// remove values
expected.removeAll(aliases);
csession.update().removeAll(customer::aliases, aliases).where(customer::id, eq(id)).sync();
actualSet = csession.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertEquals(expected, actualSet);
// remove full list
csession.update().set(customer::aliases, null).where(customer::id, eq(id)).sync();
actualSet = csession.select(customer::aliases).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertNull(actualSet);
// remove object
csession.delete().where(customer::id, eq(id)).sync();
Long cnt = csession.count().where(customer::id, eq(id)).sync();
Assert.assertEquals(Long.valueOf(0), cnt);
}
@Test
public void testListCRUID() {
UUID id = UUID.randomUUID();
List<String> names = new ArrayList<String>();
names.add("Alex");
names.add("Albert");
// CREATE
csession.insert()
.value(customer::id, id)
.value(customer::names, names)
.sync();
// READ
// read full object
Customer actual = csession.select(Customer.class).where(customer::id, eq(id)).sync().findFirst().get();
Assert.assertEquals(id, actual.id());
Assert.assertEquals(names, actual.names());
Assert.assertNull(actual.aliases());
Assert.assertNull(actual.properties());
// read full list
List<String> actualList = csession.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertEquals(names, actualList);
// read single value by index
String cql = csession.select(getIdx(customer::names, 1))
.where(customer::id, eq(id)).cql();
System.out.println("Still not supporting cql = " + cql);
// UPDATE
List<String> expected = new ArrayList<String>();
expected.add("unknown");
csession.update().set(customer::names, expected).where(customer::id, eq(id)).sync();
actual = csession.select(Customer.class).where(customer::id, eq(id)).sync().findFirst().get();
Assert.assertEquals(id, actual.id());
Assert.assertEquals(expected, actual.names());
// INSERT
// prepend operation
expected.add(0, "prepend");
csession.update().prepend(customer::names, "prepend").where(customer::id, eq(id)).sync();
actualList = csession.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertEquals(expected, actualList);
// append operation
expected.add("append");
csession.update().append(customer::names, "append").where(customer::id, eq(id)).sync();
actualList = csession.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertEquals(expected, actualList);
// prependAll operation
expected.addAll(0, names);
csession.update().prependAll(customer::names, names).where(customer::id, eq(id)).sync();
actualList = csession.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertEquals(expected, actualList);
// appendAll operation
expected.addAll(names);
csession.update().appendAll(customer::names, names).where(customer::id, eq(id)).sync();
actualList = csession.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertEquals(expected, actualList);
// set by Index
expected.set(5, "inserted");
csession.update().setIdx(customer::names, 5, "inserted").where(customer::id, eq(id)).sync();
actualList = csession.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertEquals(expected, actualList);
// DELETE
// remove single value
expected.remove("inserted");
csession.update().discard(customer::names, "inserted").where(customer::id, eq(id)).sync();
actualList = csession.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertEquals(expected, actualList);
// remove values
expected.removeAll(names);
csession.update().discardAll(customer::names, names).where(customer::id, eq(id)).sync();
actualList = csession.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertEquals(expected, actualList);
// remove full list
csession.update().set(customer::names, null).where(customer::id, eq(id)).sync();
actualList = csession.select(customer::names).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertNull(actualList);
// remove object
csession.delete().where(customer::id, eq(id)).sync();
Long cnt = csession.count().where(customer::id, eq(id)).sync();
Assert.assertEquals(Long.valueOf(0), cnt);
}
@Test
public void testMapCRUID() {
UUID id = UUID.randomUUID();
Map<String, String> props = new HashMap<String, String>();
props.put("key1", "value1");
props.put("key2", "value2");
// CREATE
csession.insert()
.value(customer::id, id)
.value(customer::properties, props)
.sync();
// READ
// read full object
Customer actual = csession.select(Customer.class).where(customer::id, eq(id)).sync().findFirst().get();
Assert.assertEquals(id, actual.id());
Assert.assertEquals(props, actual.properties());
Assert.assertNull(actual.aliases());
Assert.assertNull(actual.names());
// read full map
Map<String, String> actualMap = csession.select(customer::properties).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertEquals(props, actualMap);
// read single key-value in map
String cql = csession.select(get(customer::properties, "key1"))
.where(customer::id, eq(id)).cql();
System.out.println("Still not supporting cql = " + cql);
// UPDATE
Map<String, String> expected = new HashMap<String, String>();
expected.put("k1", "v1");
expected.put("k2", "v2");
csession.update().set(customer::properties, expected).where(customer::id, eq(id)).sync();
actual = csession.select(Customer.class).where(customer::id, eq(id)).sync().findFirst().get();
Assert.assertEquals(id, actual.id());
Assert.assertEquals(expected, actual.properties());
// INSERT
// put operation
expected.put("k3", "v3");
csession.update().put(customer::properties, "k3", "v3").where(customer::id, eq(id)).sync();
actualMap = csession.select(customer::properties).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertEquals(expected, actualMap);
// putAll operation
expected.putAll(props);
csession.update().putAll(customer::properties, props).where(customer::id, eq(id)).sync();
actualMap = csession.select(customer::properties).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertEquals(expected, actualMap);
// put existing
expected.put("k3", "v33");
csession.update().put(customer::properties, "k3", "v33").where(customer::id, eq(id)).sync();
actualMap = csession.select(customer::properties).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertEquals(expected, actualMap);
// DELETE
// remove single key
expected.remove("k3");
csession.update().put(customer::properties, "k3", null).where(customer::id, eq(id)).sync();
actualMap = csession.select(customer::properties).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertEquals(expected, actualMap);
// remove full map
csession.update().set(customer::properties, null).where(customer::id, eq(id)).sync();
actualMap = csession.select(customer::properties).where(customer::id, eq(id)).sync().findFirst().get()._1;
Assert.assertNull(actualMap);
// remove object
csession.delete().where(customer::id, eq(id)).sync();
Long cnt = csession.count().where(customer::id, eq(id)).sync();
Assert.assertEquals(Long.valueOf(0), cnt);
}
//@Test
public void test() { public void test() {
System.out.println(customer); System.out.println(customer);

View file

@ -21,7 +21,6 @@ import java.util.Set;
import java.util.UUID; import java.util.UUID;
import com.datastax.driver.core.DataType.Name; import com.datastax.driver.core.DataType.Name;
import com.noorq.casser.mapping.annotation.Column;
import com.noorq.casser.mapping.annotation.PartitionKey; import com.noorq.casser.mapping.annotation.PartitionKey;
import com.noorq.casser.mapping.annotation.Table; import com.noorq.casser.mapping.annotation.Table;
import com.noorq.casser.mapping.annotation.Types; import com.noorq.casser.mapping.annotation.Types;
@ -29,19 +28,16 @@ import com.noorq.casser.mapping.annotation.Types;
@Table @Table
public interface Customer { public interface Customer {
@PartitionKey(ordinal=0) @PartitionKey
UUID id(); UUID id();
@Types.Set(Name.TEXT) @Types.Set(Name.TEXT)
@Column(ordinal=1)
Set<String> aliases(); Set<String> aliases();
@Types.List(Name.TEXT) @Types.List(Name.TEXT)
@Column(ordinal=2) List<String> names();
List<String> name();
@Types.Map(key=Name.TEXT, value=Name.TEXT) @Types.Map(key=Name.TEXT, value=Name.TEXT)
@Column(ordinal=3)
Map<String, String> properties(); Map<String, String> properties();
} }

View file

@ -0,0 +1,42 @@
/*
* 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.unit.core.dsl;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.datastax.driver.core.DataType.Name;
import com.noorq.casser.mapping.annotation.PartitionKey;
import com.noorq.casser.mapping.annotation.Table;
import com.noorq.casser.mapping.annotation.Types;
@Table
public interface AccountWithCollections {
@PartitionKey
long id();
@Types.Set(Name.TEXT)
Set<String> aliases();
@Types.List(Name.TEXT)
List<String> name();
@Types.Map(key=Name.TEXT, value=Name.TEXT)
Map<String, String> properties();
}

View file

@ -0,0 +1,87 @@
/*
* 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.unit.core.dsl;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import com.noorq.casser.core.Casser;
import com.noorq.casser.core.Getter;
import com.noorq.casser.core.Query;
import com.noorq.casser.core.reflect.CasserPropertyNode;
import com.noorq.casser.support.DslPropertyException;
public class CollectionsDlsTest {
static AccountWithCollections account;
@BeforeClass
public static void beforeTests() {
account = Casser.dsl(AccountWithCollections.class);
}
@Test
public void testPrint() {
System.out.println(account);
}
@Test
public void testMapGet() {
String columnName = null;
Getter<String> getter = Query.get(account::properties, "key1");
try {
getter.get();
}
catch(DslPropertyException e) {
CasserPropertyNode node = e.getPropertyNode();
columnName = node.getColumnName();
}
Assert.assertEquals("\"properties\"[\"key1\"]", columnName);
}
@Test
public void testListGet() {
String columnName = null;
Getter<String> getter = Query.getIdx(account::name, 2);
try {
getter.get();
}
catch(DslPropertyException e) {
CasserPropertyNode node = e.getPropertyNode();
columnName = node.getColumnName();
}
Assert.assertEquals("\"name\"[\"2\"]", columnName);
}
}