Search in sources :

Example 1 with Aggregate

use of uk.gov.justice.domain.aggregate.Aggregate in project microservice_framework by CJSCommonPlatform.

the class DynamicAggregateTestClassGenerator method generatedTestAggregateClassOf.

public <T extends Aggregate> Class<T> generatedTestAggregateClassOf(long serialVersionUid, String basePackage, String fileNameToBeGenerated) throws IOException, ClassNotFoundException {
    final MethodSpec constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).build();
    final MethodSpec apply = MethodSpec.methodBuilder("apply").addModifiers(Modifier.PUBLIC).addCode("        return match(event).with(\n" + "                when(uk.gov.justice.domain.event.EventA.class).apply(x -> {\n" + "                            ++count;\n" + "                        }\n" + "                ));").returns(Object.class).addAnnotation(Override.class).addParameter(Object.class, "event").build();
    final MethodSpec applyObjectStream = MethodSpec.methodBuilder("apply").addModifiers(Modifier.PUBLIC).addCode("        return events\n" + "                    .map(this::apply)\n" + "                    .collect(toList())\n" + "                    .stream();").addAnnotation(Override.class).returns(ParameterizedTypeName.get(Stream.class, Object.class)).addParameter(ParameterizedTypeName.get(Stream.class, Object.class), "events").build();
    final FieldSpec.Builder fieldSpecSerialVersionId = FieldSpec.builder(TypeName.LONG.unbox(), "serialVersionUID", Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL);
    fieldSpecSerialVersionId.initializer(serialVersionUid + "L");
    final FieldSpec.Builder fieldSpecCount = FieldSpec.builder(TypeName.INT.unbox(), "count", Modifier.PRIVATE);
    final TypeSpec dynamicTestAggregate = TypeSpec.classBuilder(fileNameToBeGenerated).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(constructor).addField(fieldSpecSerialVersionId.build()).addField(fieldSpecCount.build()).addMethod(apply).addMethod(applyObjectStream).addSuperinterface(Aggregate.class).build();
    final ClassName eventSwitcherMatch = ClassName.get("uk.gov.justice.domain.aggregate", "matcher", "EventSwitcher");
    final ClassName eventSwitcherWhen = ClassName.get("uk.gov.justice.domain.aggregate", "matcher", "EventSwitcher");
    final ClassName toList = ClassName.get("java.util", "stream", "Collectors");
    final JavaFile javaFile = JavaFile.builder(basePackage, dynamicTestAggregate).addStaticImport(eventSwitcherMatch, "match").addStaticImport(eventSwitcherWhen, "when").addStaticImport(toList, "toList").build();
    final String path = DynamicAggregateTestClassGenerator.class.getClassLoader().getResource("").getPath();
    final File pathRoot = new File(path);
    javaFile.writeTo(pathRoot);
    javaFile.writeTo(System.out);
    final JavaCompilerUtil compiler = new JavaCompilerUtil(pathRoot, pathRoot);
    final Class<?> generatedClass = compiler.compiledClassOf(basePackage, fileNameToBeGenerated);
    return (Class<T>) generatedClass;
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) FieldSpec(com.squareup.javapoet.FieldSpec) JavaCompilerUtil(uk.gov.justice.services.test.utils.core.compiler.JavaCompilerUtil) ClassName(com.squareup.javapoet.ClassName) JavaFile(com.squareup.javapoet.JavaFile) Stream(java.util.stream.Stream) Aggregate(uk.gov.justice.domain.aggregate.Aggregate) File(java.io.File) JavaFile(com.squareup.javapoet.JavaFile) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 2 with Aggregate

use of uk.gov.justice.domain.aggregate.Aggregate in project microservice_framework by CJSCommonPlatform.

the class VersionedAggregateTest method shouldCreateInstanceOfVersionedAggregate.

@Test
public void shouldCreateInstanceOfVersionedAggregate() throws Exception {
    final Aggregate aggregate = mock(Aggregate.class);
    final VersionedAggregate<Aggregate> versionedAggregate = new VersionedAggregate<>(1L, aggregate);
    assertThat(versionedAggregate.getVersionId(), is(1L));
    assertThat(versionedAggregate.getAggregate(), is(aggregate));
}
Also used : Aggregate(uk.gov.justice.domain.aggregate.Aggregate) Test(org.junit.Test)

Aggregations

Aggregate (uk.gov.justice.domain.aggregate.Aggregate)2 ClassName (com.squareup.javapoet.ClassName)1 FieldSpec (com.squareup.javapoet.FieldSpec)1 JavaFile (com.squareup.javapoet.JavaFile)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 File (java.io.File)1 Stream (java.util.stream.Stream)1 Test (org.junit.Test)1 JavaCompilerUtil (uk.gov.justice.services.test.utils.core.compiler.JavaCompilerUtil)1