/*
* Copyright (C) 2012-2016 DuyHai DOAN
*
* 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 info.archinnov.achilles.it.bugs;
import static info.archinnov.achilles.embedded.CassandraEmbeddedConfigParameters.DEFAULT_CASSANDRA_EMBEDDED_KEYSPACE_NAME;
import static org.assertj.core.api.Assertions.assertThat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import org.apache.commons.lang3.RandomUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import com.datastax.driver.core.ConsistencyLevel;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.google.common.collect.ImmutableMap;
import info.archinnov.achilles.generated.ManagerFactory;
import info.archinnov.achilles.generated.ManagerFactoryBuilder;
import info.archinnov.achilles.generated.manager.SimpleEntity_Manager;
import info.archinnov.achilles.internals.entities.SimpleEntity;
import info.archinnov.achilles.junit.AchillesTestResource;
import info.archinnov.achilles.junit.AchillesTestResourceBuilder;
import info.archinnov.achilles.script.ScriptExecutor;
@RunWith(MockitoJUnitRunner.class)
public class TestDSLUpdateWithTTLAndTimestamp {
@Rule
public AchillesTestResource<ManagerFactory> resource = AchillesTestResourceBuilder
.forJunit()
.entityClassesToTruncate(SimpleEntity.class)
.truncateBeforeAndAfterTest()
.withScript("create_keyspace.cql")
.build((cluster, statementsCache) -> ManagerFactoryBuilder
.builder(cluster)
.withManagedEntityClasses(SimpleEntity.class)
.doForceSchemaCreation(true)
.withStatementsCache(statementsCache)
.withDefaultKeyspaceName(DEFAULT_CASSANDRA_EMBEDDED_KEYSPACE_NAME)
.build());
private Session session = resource.getNativeSession();
private ScriptExecutor scriptExecutor = resource.getScriptExecutor();
private SimpleEntity_Manager manager = resource.getManagerFactory().forSimpleEntity();
@Test
public void should_dsl_update_with_ttl() throws Exception {
//Given
final long id = RandomUtils.nextLong(0L, Long.MAX_VALUE);
final Date date = buildDateKey();
scriptExecutor.executeScriptTemplate("SimpleEntity/insert_single_row.cql", ImmutableMap.of("id", id, "table", "simple"));
manager
.dsl()
.update()
.fromBaseTable()
.value().Set("new value")
.consistencyList().Set(Arrays.asList(ConsistencyLevel.ALL))
.where()
.id().Eq(id)
.date().Eq(date)
.usingTimeToLive(1)
.usingTimestamp(new Date().getTime())
.execute();
//When
Thread.sleep(2000);
Row row = session.execute("SELECT * FROM simple WHERE id = " + id + " AND date = '2015-10-01 00:00:00.000+0000'").one();
//Then
assertThat(row).isNotNull();
assertThat(row.getList("consistencylist", String.class)).hasSize(2).containsExactly("QUORUM", "LOCAL_ONE");
assertThat(row.getString("value")).isEqualTo("0 AM");
}
private Date buildDateKey() throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
return dateFormat.parse("2015-10-01 00:00:00 GMT");
}
}