use of com.datastax.oss.driver.internal.mapper.processor.ProcessorContext in project java-driver by datastax.
the class DaoUpdateMethodGeneratorTest method should_process_timestamp.
@Test
@UseDataProvider("usingTimestampProvider")
public void should_process_timestamp(String timestamp, CodeBlock expected) {
// given
ProcessorContext processorContext = mock(ProcessorContext.class);
DaoUpdateMethodGenerator daoUpdateMethodGenerator = new DaoUpdateMethodGenerator(null, null, null, null, processorContext);
MethodSpec.Builder builder = MethodSpec.constructorBuilder();
// when
daoUpdateMethodGenerator.maybeAddTimestamp(timestamp, builder);
// then
assertThat(builder.build().code).isEqualTo(expected);
}
use of com.datastax.oss.driver.internal.mapper.processor.ProcessorContext in project java-driver by datastax.
the class EntityUtils method areParametersValid.
/**
* Validates that the given parameters are valid for an {@link EntityDefinition}, meaning that
* there are at least enough parameters provided to match the number of partition key columns and
* that parameter types match the primary key types.
*
* <p>If it is determined that the parameters are not valid, false is returned and an error
* message is emitted on the given method element.
*/
public static boolean areParametersValid(TypeElement entityElement, EntityDefinition entityDefinition, List<? extends VariableElement> parameters, Class<? extends Annotation> annotationClass, ProcessorContext context, ExecutableElement methodElement, TypeElement processedType, String exceptionCondition) {
if (exceptionCondition == null || exceptionCondition.isEmpty()) {
exceptionCondition = "";
} else {
exceptionCondition = " that " + exceptionCondition;
}
List<TypeName> primaryKeyTypes = entityDefinition.getPrimaryKey().stream().map(d -> d.getType().asTypeName()).collect(Collectors.toList());
List<TypeName> partitionKeyTypes = entityDefinition.getPartitionKey().stream().map(d -> d.getType().asTypeName()).collect(Collectors.toList());
List<TypeName> parameterTypes = parameters.stream().map(p -> TypeName.get(p.asType())).collect(Collectors.toList());
// if parameters are provided, we must have at least enough to match partition key.
if (parameterTypes.size() < partitionKeyTypes.size()) {
context.getMessager().error(methodElement, "Invalid parameter list: %s methods%s " + "must at least specify partition key components " + "(expected partition key of %s: %s)", annotationClass.getSimpleName(), exceptionCondition, entityElement.getSimpleName(), partitionKeyTypes);
return false;
}
if (parameterTypes.size() > primaryKeyTypes.size()) {
context.getMessager().error(methodElement, "Invalid parameter list: %s methods%s " + "must match the primary key components in the exact order " + "(expected primary key of %s: %s). Too many parameters provided", annotationClass.getSimpleName(), exceptionCondition, entityElement.getSimpleName(), primaryKeyTypes);
return false;
}
// validate that each parameter type matches the primary key type
for (int parameterIndex = 0; parameterIndex < parameterTypes.size(); parameterIndex++) {
TypeName parameterType = parameterTypes.get(parameterIndex);
TypeName primaryKeyParameterType = primaryKeyTypes.get(parameterIndex);
if (!parameterType.equals(primaryKeyParameterType)) {
context.getMessager().error(methodElement, "Invalid parameter list: %s methods%s " + "must match the primary key components in the exact order " + "(expected primary key of %s: %s). Mismatch at index %d: %s should be %s", annotationClass.getSimpleName(), exceptionCondition, entityElement.getSimpleName(), primaryKeyTypes, parameterIndex, parameterType, primaryKeyParameterType);
return false;
}
}
return true;
}
use of com.datastax.oss.driver.internal.mapper.processor.ProcessorContext in project java-driver by datastax.
the class DaoUpdateMethodGeneratorTest method should_process_ttl.
@Test
@UseDataProvider("usingTtlProvider")
public void should_process_ttl(String ttl, CodeBlock expected) {
// given
ProcessorContext processorContext = mock(ProcessorContext.class);
DaoUpdateMethodGenerator daoUpdateMethodGenerator = new DaoUpdateMethodGenerator(null, null, null, null, processorContext);
MethodSpec.Builder builder = MethodSpec.constructorBuilder();
// when
daoUpdateMethodGenerator.maybeAddTtl(ttl, builder);
// then
assertThat(builder.build().code).isEqualTo(expected);
}
Aggregations