Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
move the event plugin to it's own module
Browse files Browse the repository at this point in the history
  • Loading branch information
amckenzie committed Sep 11, 2017
1 parent 0a6342e commit fd3089c
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 62 deletions.
5 changes: 5 additions & 0 deletions integration-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.gov.justice.generators</groupId>
<artifactId>pojo-event-annotation-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.everit.json</groupId>
<artifactId>org.everit.json.schema</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import uk.gov.justice.domain.annotation.Event;
import uk.gov.justice.generation.pojo.integration.utils.GeneratorUtil;
import uk.gov.justice.generation.pojo.integration.utils.OutputDirectories;

Expand Down Expand Up @@ -39,8 +38,6 @@ public void shouldGenerateAnEmptyClassWithAdditionalPropertiesIfNoAdditionalProp

assertThat(emptySchemaClass.getDeclaredMethod("getAdditionalProperties"), is(notNullValue()));
assertThat(emptySchemaClass.getDeclaredMethod("setAdditionalProperty", String.class, Object.class), is(notNullValue()));

assertThat(emptySchemaClass.getAnnotation(Event.class), is(notNullValue()));
}

@Test
Expand All @@ -56,8 +53,6 @@ public void shouldGenerateAnEmptyClassWithAdditionalPropertiesIfoAdditionalPrope
emptySchemaClass.getDeclaredField("additionalProperties").getType().getName();
emptySchemaClass.getDeclaredMethod("getAdditionalProperties");
emptySchemaClass.getDeclaredMethod("setAdditionalProperty", String.class, Object.class);

assertThat(emptySchemaClass.getAnnotation(Event.class), is(notNullValue()));
}

@Test
Expand Down Expand Up @@ -89,8 +84,6 @@ public void shouldGenerateAnEmptyClassWithAdditionalPropertiesIfoAdditionalPrope
fail();
} catch (final NoSuchMethodException ignored) {
}

assertThat(emptySchemaClass.getAnnotation(Event.class), is(notNullValue()));
}

private List<Class<?>> setupAndGenerate(final String fileName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import uk.gov.justice.generation.io.files.loader.SchemaLoader;
import uk.gov.justice.generation.pojo.core.GenerationContext;
import uk.gov.justice.generation.pojo.core.NameGenerator;
import uk.gov.justice.generation.pojo.generators.ClassGeneratable;
import uk.gov.justice.generation.pojo.generators.ClassNameFactory;
import uk.gov.justice.generation.pojo.generators.JavaGeneratorFactory;
import uk.gov.justice.generation.pojo.generators.TypeNameProvider;
Expand Down Expand Up @@ -87,15 +88,22 @@ public List<Class<?>> generateAndCompileJavaSource(final File jsonSchemaFile,
return javaGeneratorFactory
.createClassGeneratorsFor(definitionBuilderVisitor.getDefinitions(), pluginProvider, pluginContext, generationContext)
.stream()
.map(classGeneratable -> {
sourceWriter.write(classGeneratable, generationContext);
return classCompiler.compile(classGeneratable, generationContext, outputDirectories.getClassesOutputDirectory().toFile());
})
.map(classGeneratable -> writeAndCompile(
outputDirectories,
generationContext,
sourceWriter,
classCompiler,
classGeneratable))
.collect(toList());
}

public void validate(final File schemaFile, final String json) {
final Schema schema = schemaLoader.loadFrom(schemaFile);
schema.validate(new JSONObject(json));
}

private Class<?> writeAndCompile(final OutputDirectories outputDirectories, final GenerationContext generationContext, final SourceWriter sourceWriter, final ClassCompiler classCompiler, final ClassGeneratable classGeneratable) {
sourceWriter.write(classGeneratable, generationContext);
return classCompiler.compile(classGeneratable, generationContext, outputDirectories.getClassesOutputDirectory().toFile());
}
}
35 changes: 35 additions & 0 deletions pojo-event-annotation-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>jsonschema-pojo-generator</artifactId>
<groupId>uk.gov.justice.generators</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>pojo-event-annotation-plugin</artifactId>

<dependencies>
<dependency>
<groupId>uk.gov.justice.generators</groupId>
<artifactId>pojo-generation-core</artifactId>
<version>${project.version}</version>
</dependency>


<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static java.util.Arrays.asList;

import uk.gov.justice.generation.pojo.generators.plugin.classgenerator.ClassGeneratorPlugin;
import uk.gov.justice.generation.pojo.generators.plugin.classgenerator.EventAnnotationPlugin;
import uk.gov.justice.generation.pojo.generators.plugin.classgenerator.FieldAndMethodPlugin;
import uk.gov.justice.generation.pojo.generators.plugin.classgenerator.SerializablePlugin;
import uk.gov.justice.generation.pojo.generators.plugin.classgenerator.builder.BuilderGeneratorFactory;
Expand All @@ -20,7 +19,6 @@ public class DefaultPluginProvider implements PluginProvider {
@Override
public List<ClassGeneratorPlugin> pluginClassGenerators() {
return asList(
new EventAnnotationPlugin(),
new SerializablePlugin(),
new FieldAndMethodPlugin(),
new BuilderPlugin(new BuilderGeneratorFactory()));
Expand All @@ -33,4 +31,6 @@ public List<TypeNamePlugin> typeNamePlugins() {
new UuidTypeNamePlugin(),
new ZonedDateTimeTypeNamePlugin());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import static java.nio.file.Paths.get;
import static org.apache.commons.io.FileUtils.cleanDirectory;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
Expand All @@ -13,14 +11,10 @@
import uk.gov.justice.maven.generator.io.files.parser.core.GeneratorConfig;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -58,35 +52,6 @@ public void shouldConvertSchemaFileToJavaPojo() throws Exception {
assertThat(files, notNullValue());
assertThat(files.length, is(1));
assertThat(files[0].toPath().toString(), is("target/test-generation/uk/gov/justice/generation/pojo/PersonSchema.java"));

final String javaSource = loadFileAsString(files[0].toPath());

assertThat(javaSource, not(containsString("@Event(\"example.events.person-event\")")));
}

@Test
public void shouldConvertSchemaFileToJavaPojoWithEventAnnotation() throws Exception {
final File schemaFile = get("src/test/resources/schemas/example.events.person-event.json").toFile();
final GeneratorConfig generatorConfig = mock(GeneratorConfig.class);

when(generatorConfig.getOutputDirectory()).thenReturn(sourceOutputDirectory.toPath());
when(generatorConfig.getBasePackageName()).thenReturn("uk.gov.justice.generation.event");

final SchemaPojoGenerator schemaPojoGenerator = new SchemaPojoGenerator();

schemaPojoGenerator.run(schemaFile, generatorConfig);

final File directory = Paths.get("target/test-generation/uk/gov/justice/generation/event").toFile();
assertThat(directory.exists(), is(true));

final File[] files = directory.listFiles();
assertThat(files, notNullValue());
assertThat(files.length, is(1));
assertThat(files[0].toPath().toString(), is("target/test-generation/uk/gov/justice/generation/event/PersonEvent.java"));

final String javaSource = loadFileAsString(files[0].toPath());

assertThat(javaSource, containsString("@Event(\"example.events.person-event\")"));
}

@Test
Expand All @@ -111,15 +76,5 @@ public void shouldUsePluginProviderSetInGeneratorConfigAndNotUseEventAnnotationP
assertThat(files, notNullValue());
assertThat(files.length, is(1));
assertThat(files[0].toPath().toString(), is("target/test-generation/uk/gov/justice/generation/event/PersonEvent.java"));

final String javaSource = loadFileAsString(files[0].toPath());

assertThat(javaSource, not(containsString("@Event(\"example.events.person-event\")")));
}

private String loadFileAsString(final Path path) throws IOException {
try (final FileReader reader = new FileReader(path.toFile())) {
return IOUtils.toString(reader);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import static org.junit.Assert.assertThat;

import uk.gov.justice.generation.pojo.generators.plugin.classgenerator.ClassGeneratorPlugin;
import uk.gov.justice.generation.pojo.generators.plugin.classgenerator.EventAnnotationPlugin;
import uk.gov.justice.generation.pojo.generators.plugin.classgenerator.FieldAndMethodPlugin;
import uk.gov.justice.generation.pojo.generators.plugin.classgenerator.SerializablePlugin;
import uk.gov.justice.generation.pojo.generators.plugin.classgenerator.builder.BuilderPlugin;
Expand All @@ -26,9 +25,8 @@ public class DefaultPluginProviderTest {
public void shouldProvideDefaultListOfPluginClassGenerators() throws Exception {
final List<ClassGeneratorPlugin> pluginClassGenerators = new DefaultPluginProvider().pluginClassGenerators();

assertThat(pluginClassGenerators.size(), is(4));
assertThat(pluginClassGenerators.size(), is(3));
assertThat(pluginClassGenerators, hasItems(
instanceOf(EventAnnotationPlugin.class),
instanceOf(SerializablePlugin.class),
instanceOf(FieldAndMethodPlugin.class),
instanceOf(BuilderPlugin.class)
Expand Down
7 changes: 6 additions & 1 deletion pojo-plugin-test-provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<artifactId>pojo-generation-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>uk.gov.justice.generators</groupId>
<artifactId>pojo-event-annotation-plugin</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand All @@ -25,4 +30,4 @@
<scope>test</scope>
</dependency>
</dependencies>
</project>
</project>
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<module>integration-test</module>
<module>pojo-plugin-it</module>
<module>pojo-plugin-test-provider</module>
<module>pojo-event-annotation-plugin</module>
</modules>

<properties>
Expand Down

0 comments on commit fd3089c

Please sign in to comment.