static column test implementation

This commit is contained in:
Albert Shift 2015-04-17 21:10:09 -07:00
parent 3f127e3b9c
commit 3353d10976
6 changed files with 228 additions and 10 deletions

View file

@ -47,8 +47,10 @@ public final class Casser {
return Objects.requireNonNull(session, "session is not initialized");
}
protected static void register(CasserSession newSession) {
session = newSession;
protected static synchronized void register(CasserSession newSession) {
if (session == null) {
session = newSession;
}
}
public static CasserSettings settings() {

View file

@ -15,12 +15,13 @@
*/
package com.noorq.casser.core;
import java.util.Arrays;
import com.datastax.driver.core.querybuilder.Clause;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.noorq.casser.core.reflect.CasserPropertyNode;
import com.noorq.casser.mapping.value.ColumnValuePreparer;
import com.noorq.casser.support.CasserMappingException;
import com.noorq.casser.support.Requires;
public final class Postulate<V> {
@ -28,7 +29,6 @@ public final class Postulate<V> {
private final V[] values;
protected Postulate(Operator op, V[] values) {
Requires.nonNullArray(values);
this.operator = op;
this.values = values;
}
@ -78,7 +78,22 @@ public final class Postulate<V> {
public String toString() {
if (operator == Operator.IN) {
return "in(" + values + ")";
if (values == null) {
return "in()";
}
int len = values.length;
StringBuilder b = new StringBuilder();
b.append("in(");
for (int i = 0; i != len; i++) {
if (b.length() > 3) {
b.append(", ");
}
b.append(String.valueOf(values[i]));
}
return b.append(')').toString();
}
return operator.getName() + values[0];

View file

@ -166,10 +166,7 @@ public final class SessionInitializer extends AbstractSessionOperations {
return this;
}
public synchronized void register() {
if (Casser.session() != null) {
return;
}
public void register() {
Casser.register(get());
}

View file

@ -339,7 +339,7 @@ public final class UpdateOperation extends AbstractFilterOperation<ResultSet, Up
public BuiltStatement buildStatement() {
if (entity == null) {
throw new CasserMappingException("unknown entity");
throw new CasserMappingException("empty update operation");
}
Update update = QueryBuilder.update(entity.getName().toCql());

View file

@ -0,0 +1,46 @@
/*
* 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.simple;
import java.util.Date;
import com.noorq.casser.mapping.annotation.ClusteringColumn;
import com.noorq.casser.mapping.annotation.Column;
import com.noorq.casser.mapping.annotation.PartitionKey;
import com.noorq.casser.mapping.annotation.StaticColumn;
import com.noorq.casser.mapping.annotation.Table;
import com.noorq.casser.mapping.annotation.Types;
@Table
public interface Message {
@PartitionKey
int id();
@ClusteringColumn
@Types.Timeuuid
Date timestamp();
@StaticColumn(forceQuote=true)
String from();
@Column(forceQuote=true)
String to();
@Column
String message();
}

View file

@ -0,0 +1,158 @@
/*
* 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.simple;
import static com.noorq.casser.core.Query.eq;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import com.noorq.casser.core.Casser;
import com.noorq.casser.test.integration.build.AbstractEmbeddedCassandraTest;
public class StaticColumnTest extends AbstractEmbeddedCassandraTest {
static Message message = Casser.dsl(Message.class);
@BeforeClass
public static void beforeTest() {
Casser.init(getSession()).showCql().add(Message.class).autoCreateDrop().register();
}
@Test
public void testPrint() {
System.out.println(message);
}
private static class MessageImpl implements Message {
int id;
Date timestamp;
String from;
String to;
String msg;
@Override
public int id() {
return id;
}
@Override
public Date timestamp() {
return timestamp;
}
@Override
public String from() {
return from;
}
@Override
public String to() {
return to;
}
@Override
public String message() {
return msg;
}
}
@Test
public void testCRUID() {
MessageImpl msg = new MessageImpl();
msg.id = 123;
msg.timestamp = new Date();
msg.from = "Alex";
msg.to = "Bob";
msg.msg = "hi";
// CREATE
Casser.session().insert(msg).sync();
msg.id = 123;
msg.to = "Craig";
Casser.session().insert(msg).sync();
// READ
List<Message> actual = Casser.session().select(Message.class)
.where(message::id, eq(123)).sync()
.collect(Collectors.toList());
Assert.assertEquals(2, actual.size());
Message toCraig = actual.stream().filter(m -> m.to().equals("Craig")).findFirst().get();
assertMessages(msg, toCraig);
// UPDATE
Casser.session().update().set(message::from, "Albert")
.where(message::id, eq(123))
.onlyIf(message::from, eq("Alex"))
.sync();
long cnt = Casser.session().select(message::from)
.where(message::id, eq(123)).sync()
.filter(t -> t._1.equals("Albert"))
.count();
Assert.assertEquals(2, cnt);
// INSERT
Casser.session().update().set(message::from, null)
.where(message::id, eq(123))
.sync();
Casser.session().select(message::from)
.where(message::id, eq(123))
.sync()
.map(t -> t._1)
.forEach(Assert::assertNull);
Casser.session().update().set(message::from, "Alex")
.where(message::id, eq(123))
.onlyIf(message::from, eq(null)).sync();
// DELETE
Casser.session().delete().where(message::id, eq(123)).sync();
cnt = Casser.session().count().where(message::id, eq(123)).sync();
Assert.assertEquals(0, cnt);
}
private void assertMessages(Message expected, Message actual) {
Assert.assertEquals(expected.id(), actual.id());
Assert.assertEquals(expected.from(), actual.from());
Assert.assertEquals(expected.timestamp(), actual.timestamp());
Assert.assertEquals(expected.to(), actual.to());
Assert.assertEquals(expected.message(), actual.message());
}
}