Search in sources :

Example 1 with ProcessorContext

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);
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ProcessorContext(com.datastax.oss.driver.internal.mapper.processor.ProcessorContext) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 2 with ProcessorContext

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;
}
Also used : ProcessorContext(com.datastax.oss.driver.internal.mapper.processor.ProcessorContext) ElementKind(javax.lang.model.element.ElementKind) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) Collectors(java.util.stream.Collectors) TypeKind(javax.lang.model.type.TypeKind) Entity(com.datastax.oss.driver.api.mapper.annotations.Entity) List(java.util.List) TypeMirror(javax.lang.model.type.TypeMirror) Map(java.util.Map) DeclaredType(javax.lang.model.type.DeclaredType) Annotation(java.lang.annotation.Annotation) TypeName(com.squareup.javapoet.TypeName) TypeVariable(javax.lang.model.type.TypeVariable) EntityDefinition(com.datastax.oss.driver.internal.mapper.processor.entity.EntityDefinition) Name(javax.lang.model.element.Name) TypeName(com.squareup.javapoet.TypeName)

Example 3 with ProcessorContext

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);
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ProcessorContext(com.datastax.oss.driver.internal.mapper.processor.ProcessorContext) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

ProcessorContext (com.datastax.oss.driver.internal.mapper.processor.ProcessorContext)3 MethodSpec (com.squareup.javapoet.MethodSpec)2 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)2 Test (org.junit.Test)2 Entity (com.datastax.oss.driver.api.mapper.annotations.Entity)1 EntityDefinition (com.datastax.oss.driver.internal.mapper.processor.entity.EntityDefinition)1 TypeName (com.squareup.javapoet.TypeName)1 Annotation (java.lang.annotation.Annotation)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Element (javax.lang.model.element.Element)1 ElementKind (javax.lang.model.element.ElementKind)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 Name (javax.lang.model.element.Name)1 TypeElement (javax.lang.model.element.TypeElement)1 VariableElement (javax.lang.model.element.VariableElement)1 DeclaredType (javax.lang.model.type.DeclaredType)1 TypeKind (javax.lang.model.type.TypeKind)1 TypeMirror (javax.lang.model.type.TypeMirror)1