Skip to content

Commit

Permalink
Reworked test enities to a SimpleTable and a TortureTable to include …
Browse files Browse the repository at this point in the history
…all data types. Includes bugfixes found while testing TortureTable.
  • Loading branch information
tdl-jturner committed Aug 19, 2019
1 parent fa1fe9c commit e680688
Show file tree
Hide file tree
Showing 26 changed files with 611 additions and 250 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ else if(enclosedElement.getAnnotation(com.tmobile.opensource.casquatch.annotatio
}
else if(enclosedElement.getAnnotation(com.tmobile.opensource.casquatch.annotation.UDT.class)!=null) {
udtFields.put(enclosedElement.getSimpleName().toString(),type);
if (!imports.contains("com.datastax.oss.driver.api.core.data.UdtValue")) imports.add("com.datastax.oss.driver.api.core.data.UdtValue");
}
else {
nonKeyFields.put(enclosedElement.getSimpleName().toString(),type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ public class ${naming.classToStatementFactory(naming.classToSimpleClass(class))}
if(!options.getIgnoreNonPrimaryKeys()) {
<#list nonKeyFields as field,type>
if(${naming.classToVar(naming.classToSimpleClass(class))}.${naming.javaVariableToJavaGet(field)}()!=null) {
select=select.whereColumn("${naming.javaVariableToCql(field)}").isEqualTo(bindMarker());
if(!allowQueryByType(${type}.class)) {
log.warn("Ignorning column ${field}. ${type} is not a supported query column type");
}
else {
select=select.whereColumn("${naming.javaVariableToCql(field)}").isEqualTo(bindMarker());
}
}
</#list>
<#list udtFields as field,type>
Expand Down Expand Up @@ -104,19 +109,11 @@ public class ${naming.classToStatementFactory(naming.classToSimpleClass(class))}
@Override
protected Delete deleteObject(${naming.classToSimpleClass(class)} ${naming.classToVar(naming.classToSimpleClass(class))}, QueryOptions options) {
Delete delete=null;

<#list keyFields as field,type>
if(${naming.classToVar(naming.classToSimpleClass(class))}.${naming.javaVariableToJavaGet(field)}()!=null) {
delete=(delete==null?deleteStart:delete).whereColumn("${naming.javaVariableToCql(field)}").isEqualTo(bindMarker());
}
</#list>
if(!options.getIgnoreNonPrimaryKeys()) {
<#list nonKeyFields as field,type>
if(${naming.classToVar(naming.classToSimpleClass(class))}.${naming.javaVariableToJavaGet(field)}()!=null) {
delete=(delete==null?deleteStart:delete).whereColumn("${naming.javaVariableToCql(field)}").isEqualTo(bindMarker());
}
</#list>
}
return delete;
}

Expand All @@ -129,21 +126,18 @@ public class ${naming.classToStatementFactory(naming.classToSimpleClass(class))}
protected BoundStatementBuilder bindObject(BoundStatementBuilder boundStatementBuilder, ${naming.classToSimpleClass(class)} ${naming.classToVar(naming.classToSimpleClass(class))}, QueryOptions options) {
<#list keyFields as field,type>
if(${naming.classToVar(naming.classToSimpleClass(class))}.${naming.javaVariableToJavaGet(field)}()!=null || options.getPersistNulls()) {
boundStatementBuilder = boundStatementBuilder.set("${naming.javaVariableToCql(field)}", ${naming.classToVar(naming.classToSimpleClass(class))}.${naming.javaVariableToJavaGet(field)}(), ${type}.class);
boundStatementBuilder = bindIfMarked(boundStatementBuilder,"${naming.javaVariableToCql(field)}", ${naming.classToVar(naming.classToSimpleClass(class))}.${naming.javaVariableToJavaGet(field)}(), ${type}.class);
}
</#list>
if(!options.getIgnoreNonPrimaryKeys()) {
<#list nonKeyFields as field,type>
if(${naming.classToVar(naming.classToSimpleClass(class))}.${naming.javaVariableToJavaGet(field)}()!=null || options.getPersistNulls()) {
boundStatementBuilder = boundStatementBuilder.set("${naming.javaVariableToCql(field)}", ${naming.classToVar(naming.classToSimpleClass(class))}.${naming.javaVariableToJavaGet(field)}(), ${type}.class);
boundStatementBuilder = bindIfMarked(boundStatementBuilder,"${naming.javaVariableToCql(field)}", ${naming.classToVar(naming.classToSimpleClass(class))}.${naming.javaVariableToJavaGet(field)}(), ${type}.class);
}
</#list>
<#list udtFields as field,type>
if(${naming.classToVar(naming.classToSimpleClass(class))}.${naming.javaVariableToJavaGet(field)}()!=null || options.getPersistNulls()) {
try {
boundStatementBuilder = boundStatementBuilder.setUdtValue("${naming.javaVariableToCql(field)}", ${naming.classToVar(naming.classToTypeFactory(naming.classToSimpleClass(type)))}.toUdtValue(${naming.classToVar(naming.classToSimpleClass(class))}.${naming.javaVariableToJavaGet(field)}()));
}
catch (java.lang.IllegalArgumentException e) {}
boundStatementBuilder=bindIfMarked(boundStatementBuilder,"${naming.javaVariableToCql(field)}",${naming.classToVar(naming.classToTypeFactory(naming.classToSimpleClass(type)))}.toUdtValue(${naming.classToVar(naming.classToSimpleClass(class))}.${naming.javaVariableToJavaGet(field)}()), UdtValue.class);
}
</#list>
}
Expand All @@ -158,20 +152,26 @@ public class ${naming.classToStatementFactory(naming.classToSimpleClass(class))}
${naming.classToVar(naming.classToSimpleClass(class))}.${naming.javaVariableToJavaSet(field)}(source.get("${naming.javaVariableToCql(field)}",${naming.classToSimpleClass(type)}.class));
}
catch (java.lang.IllegalArgumentException e) {
log.trace("Failed to map {}","${naming.javaVariableToCql(field)}",e);
}
</#list>
<#list nonKeyFields as field,type>
try {
${naming.classToVar(naming.classToSimpleClass(class))}.${naming.javaVariableToJavaSet(field)}(source.get("${naming.javaVariableToCql(field)}",${naming.classToSimpleClass(type)}.class));
}
catch (java.lang.IllegalArgumentException e) {
log.trace("Failed to map {}","${naming.javaVariableToCql(field)}",e);
}
</#list>
<#list udtFields as field,type>
try {
${naming.classToVar(naming.classToSimpleClass(class))}.${naming.javaVariableToJavaSet(field)}(${naming.classToVar(naming.classToTypeFactory(naming.classToSimpleClass(type)))}.fromUdtValue(source.getUdtValue("${field}")));
${naming.classToVar(naming.classToSimpleClass(class))}.${naming.javaVariableToJavaSet(field)}(${naming.classToVar(naming.classToTypeFactory(naming.classToSimpleClass(type)))}.fromUdtValue(source.getUdtValue("${naming.javaVariableToCql(field)}")));
}
catch (java.lang.IllegalArgumentException e) {
log.trace("Failed to map {}","${naming.javaVariableToCql(field)}",e);
}
catch (java.lang.NullPointerException e) {
log.trace("Failed to map {}","${naming.javaVariableToCql(field)}",e);
}
</#list>
return ${naming.classToVar(naming.classToSimpleClass(class))};
Expand Down
101 changes: 101 additions & 0 deletions casquatch-driver-tests/genmodels.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/bin/sh
#
# Helper script to spin up docker and regenerate test models
#

VERSION=2.0-SNAPSHOT
KEYSPACE=junittest
INSTANCE=casquatch-driver-tests
OUTPUT_FOLDER=src/test/java/com/tmobile/opensource/casquatch/tests/
GENERATOR=../casquatch-generator/target/casquatch-generator-$VERSION.jar
PACKAGE=com.tmobile.opensource.casquatch.tests
PORT=9044

echo "---------------------------------------------"
echo "Starting Docker (30 second pause to startup)"
echo "---------------------------------------------"
docker kill $INSTANCE
docker run --rm -p $PORT:9042 -d --name $INSTANCE -d cassandra:latest
sleep 30

echo "---------------------------------------------"
echo "Installing CQL"
echo "---------------------------------------------"
docker exec -i $INSTANCE cqlsh <<EOF
CREATE KEYSPACE $KEYSPACE WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true;
CREATE TABLE $KEYSPACE.simple_table (
key_one int,
key_two int,
col_one text,
col_two text,
solr_query text,
PRIMARY KEY (key_one, key_two)
) WITH CLUSTERING ORDER BY (key_two ASC);
CREATE TYPE $KEYSPACE.udt ( val1 text, val2 text );
CREATE TABLE $KEYSPACE.torture_table (
id uuid PRIMARY KEY,
col_ascii ascii,
col_bigint bigint,
col_blob blob,
col_boolean boolean,
col_date date,
col_decimal decimal,
col_double double,
col_float float,
col_inet inet,
col_int int,
col_list list<text>,
col_map map<text, text>,
col_set set<text>,
col_smallint smallint,
col_text text,
col_time time,
col_timestamp timestamp,
col_timeuuid timeuuid,
col_tinyint tinyint,
col_uuid uuid,
col_varchar text,
col_varint varint,
col_udt frozen<udt>
);
CREATE TABLE $KEYSPACE.counter_table (
id uuid PRIMARY KEY,
col_counter counter
);
CREATE TABLE $KEYSPACE.tuple_table (
id uuid PRIMARY KEY,
col_tuple tuple<text,text,text>
);
EOF

echo "---------------------------------------------"
echo "Generate Models"
echo "---------------------------------------------"
java -Dcasquatch.basic.contact-points.0=localhost:$PORT \
-Dcasquatch.basic.load-balancing-policy.local-datacenter=datacenter1 \
-Dcasquatch.basic.session-keyspace=$KEYSPACE \
-Dcasquatch.generator.outputFolder=$OUTPUT_FOLDER \
-Dcasquatch.generator.overwrite=true \
-Dcasquatch.generator.file=true \
-Dcasquatch.generator.packageName=$PACKAGE \
-DCASQUATCH_LOG_LEVEL=TRACE \
-jar $GENERATOR

echo "---------------------------------------------"
echo "Modify Output"
echo "---------------------------------------------"
rm $OUTPUT_FOLDER/*_EmbeddedTests.java
sed -i .bak 's/^\@CasquatchEntity\(\)$/\@CasquatchEntity\(generateTests = true\)/' $OUTPUT_FOLDER/*.java
rm $OUTPUT_FOLDER/*.java.bak


echo "---------------------------------------------"
echo "Kill Docker"
echo "---------------------------------------------"
docker kill $INSTANCE
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

package com.tmobile.opensource.casquatch;

import com.tmobile.opensource.casquatch.annotation.CasquatchIgnore;
import com.tmobile.opensource.casquatch.annotation.ClusteringColumn;
import com.tmobile.opensource.casquatch.annotation.PartitionKey;
import com.tmobile.opensource.casquatch.tests.podam.CasquatchPodamFactoryImpl;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
Expand Down Expand Up @@ -127,12 +128,9 @@ protected void waitForSolrIndex(E obj){
List<String> solrQuery = new ArrayList<>();
try {
for (Field field : this.entityClass.getDeclaredFields()) {
if (!field.isAnnotationPresent(CasquatchIgnore.class)) {
solrQuery.add(
CasquatchNamingConvention.javaVariableToCql(field.getName()) +
":" +
this.entityClass.getMethod(CasquatchNamingConvention.javaVariableToJavaGet(field.getName())).invoke(obj)
);
if(field.isAnnotationPresent(PartitionKey.class) || field.isAnnotationPresent(ClusteringColumn.class)) {
Object fieldValue=this.entityClass.getMethod(CasquatchNamingConvention.javaVariableToJavaGet(field.getName())).invoke(obj);
solrQuery.add(String.format("%s:%s", CasquatchNamingConvention.javaVariableToCql(field.getName()), fieldValue));
}
}
}
Expand Down Expand Up @@ -342,6 +340,8 @@ public void testSaveASyncWithQueryOptions() {
public void testGetCache() {
E obj = prepObject();
E cachedObject = getDatabaseCache().get(obj);
log.trace("Created {}",obj);
log.trace("Returned {}",cachedObject);
assert(obj.equals(cachedObject));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public BigDecimal getType(DataProviderStrategy strategy,
AttributeMetadata attributeMetadata,
Map<String, Type> genericTypesArgumentsMap) {

return new BigDecimal(Integer.MAX_VALUE/100);
return new BigDecimal(strategy.getTypeValue(attributeMetadata,genericTypesArgumentsMap,Integer.class)/100);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2018 T-Mobile US, 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.tmobile.opensource.casquatch.tests.podam;

import uk.co.jemos.podam.api.AttributeMetadata;
import uk.co.jemos.podam.api.DataProviderStrategy;
import uk.co.jemos.podam.typeManufacturers.AbstractTypeManufacturer;

import java.lang.reflect.Type;
import java.math.BigInteger;
import java.util.Map;

/**
* Implements BigInteger generation
*/
class BigIntegerStrategy extends AbstractTypeManufacturer<BigInteger> {

/**
* Required interface to implement BigInteger generation.
* @param strategy passed strategy
* @param attributeMetadata passed attribute metadata
* @param genericTypesArgumentsMap passed map
* @return generated BigInteger
*/
@Override
public BigInteger getType(DataProviderStrategy strategy,
AttributeMetadata attributeMetadata,
Map<String, Type> genericTypesArgumentsMap) {

return BigInteger.valueOf(strategy.getTypeValue(attributeMetadata,genericTypesArgumentsMap,Integer.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import uk.co.jemos.podam.api.AttributeMetadata;
import uk.co.jemos.podam.api.DataProviderStrategy;
import uk.co.jemos.podam.api.PodamUtils;
import uk.co.jemos.podam.common.PodamConstants;
import uk.co.jemos.podam.typeManufacturers.AbstractTypeManufacturer;

import java.lang.reflect.Type;
Expand All @@ -43,12 +41,6 @@ public ByteBuffer getType(DataProviderStrategy strategy,
AttributeMetadata attributeMetadata,
Map<String, Type> genericTypesArgumentsMap) {

StringBuilder buff = new StringBuilder();

while (buff.length() < PodamConstants.STR_DEFAULT_LENGTH) {
buff.append(PodamUtils.getNiceCharacter());
}

return ByteBuffer.wrap(buff.toString().getBytes());
return ByteBuffer.wrap(strategy.getTypeValue(attributeMetadata,genericTypesArgumentsMap,String.class).getBytes());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
import uk.co.jemos.podam.api.PodamFactoryImpl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.UUID;

/**
Expand All @@ -31,8 +36,13 @@ public class CasquatchPodamFactoryImpl extends PodamFactoryImpl {
*/
public CasquatchPodamFactoryImpl() {
super();
this.getStrategy().addOrReplaceTypeManufacturer(UUID.class, new UUIDStrategy());
this.getStrategy().addOrReplaceTypeManufacturer(ByteBuffer.class, new ByteBufferStrategy());
this.getStrategy().addOrReplaceTypeManufacturer(BigDecimal.class, new BigDecimalStrategy());
this.getStrategy().addOrReplaceTypeManufacturer(BigInteger.class, new BigIntegerStrategy());
this.getStrategy().addOrReplaceTypeManufacturer(ByteBuffer.class, new ByteBufferStrategy());
this.getStrategy().addOrReplaceTypeManufacturer(InetAddress.class, new InetAddressStrategy());
this.getStrategy().addOrReplaceTypeManufacturer(Instant.class, new InstantStrategy());
this.getStrategy().addOrReplaceTypeManufacturer(LocalDate.class, new LocalDateStrategy());
this.getStrategy().addOrReplaceTypeManufacturer(LocalTime.class, new LocalTimeStrategy());
this.getStrategy().addOrReplaceTypeManufacturer(UUID.class, new UUIDStrategy());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2018 T-Mobile US, 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.tmobile.opensource.casquatch.tests.podam;

import uk.co.jemos.podam.api.AttributeMetadata;
import uk.co.jemos.podam.api.DataProviderStrategy;
import uk.co.jemos.podam.typeManufacturers.AbstractTypeManufacturer;

import java.lang.reflect.Type;
import java.net.InetAddress;
import java.util.Map;

/**
* Implements InetAddress generation
*/
class InetAddressStrategy extends AbstractTypeManufacturer<InetAddress> {

/**
* Required interface to implement InetAddress generation.
* @param strategy passed strategy
* @param attributeMetadata passed attribute metadata
* @param genericTypesArgumentsMap passed map
* @return generated InetAddress
*/
@Override
public InetAddress getType(DataProviderStrategy strategy,
AttributeMetadata attributeMetadata,
Map<String, Type> genericTypesArgumentsMap) {

return InetAddress.getLoopbackAddress();
}
}
Loading

0 comments on commit e680688

Please sign in to comment.