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;
}
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));
}
Aggregations