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

Commit

Permalink
Add type mapping for string format field
Browse files Browse the repository at this point in the history
  • Loading branch information
mapingo committed Oct 3, 2017
1 parent 8ceae31 commit ccd5d13
Show file tree
Hide file tree
Showing 46 changed files with 995 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void setup() throws Exception {
}

@Test
public void shouldGenerateAnPojoFromAnSchemaDocumentWithAnArrayOfItems() throws Exception {
public void shouldGeneratePojoFromSchemaDocumentWithArrayOfItems() throws Exception {

final File jsonSchemaFile = new File("src/test/resources/schemas/examples/array.json");
final String packageName = "uk.gov.justice.pojo.arrays";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void setup() throws Exception {
}

@Test
public void shouldGenerateAnPojoFromAnSchemaDocumentWithACombinedSchema() throws Exception {
public void shouldGeneratePojoFromSchemaDocumentWithCombinedSchema() throws Exception {

final String packageName = "uk.gov.justice.pojo.combined.schema";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void setup() throws Exception {

@SuppressWarnings("ConstantConditions")
@Test
public void shouldGenerateAnPojoFromAnSchemaDocumentWithAnArrayOfItems() throws Exception {
public void shouldGeneratePojoFromSchemaDocumentWithArrayOfItems() throws Exception {

final String packageName = "uk.gov.justice.pojo.vanilla";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void setup() throws Exception {
}

@Test
public void shouldGenerateAClassWithAMapForAdditionalPropertiesIfAdditionalPropertiesIsTrue() throws Exception {
public void shouldGenerateClassWithMapForAdditionalPropertiesIfAdditionalPropertiesIsTrue() throws Exception {

final String packageName = "uk.gov.justice.pojo.additional.properties";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void setup() throws Exception {
}

@Test
public void shouldCreateAnObjectWithHashcodeAndEqualsMethods() throws Exception {
public void shouldCreateObjectWithHashcodeAndEqualsMethods() throws Exception {

final String packageName = "uk.gov.justice.pojo.equals";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package uk.gov.justice.generation.pojo.integration.examples.plugins;

import static com.jayway.jsonassert.JsonAssert.with;
import static org.hamcrest.CoreMatchers.is;
import static uk.gov.justice.generation.pojo.integration.utils.PojoGeneratorPropertiesBuilder.pojoGeneratorPropertiesBuilder;
import static uk.gov.justice.generation.pojo.plugin.typemodifying.FormatCustomReturnTypePlugin.formatCustomReturnTypePlugin;

import uk.gov.justice.generation.pojo.core.PojoGeneratorProperties;
import uk.gov.justice.generation.pojo.integration.utils.ClassInstantiator;
import uk.gov.justice.generation.pojo.integration.utils.GeneratorUtil;
import uk.gov.justice.generation.pojo.integration.utils.OutputDirectories;
import uk.gov.justice.services.common.converter.ZonedDateTimes;
import uk.gov.justice.services.common.converter.jackson.ObjectMapperProducer;

import java.io.File;
import java.time.ZonedDateTime;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;

public class FormatCustomReturnTypePluginIT {

private final ObjectMapper objectMapper = new ObjectMapperProducer().objectMapper();
private final GeneratorUtil generatorUtil = new GeneratorUtil();
private final ClassInstantiator classInstantiator = new ClassInstantiator();
private final OutputDirectories outputDirectories = new OutputDirectories();

private static final File JSON_SCHEMA_FILE = new File("src/test/resources/schemas/examples/plugins/format-custom-return-types-plugin.json");

@Before
public void setup() throws Exception {
outputDirectories.makeDirectories("./target/test-generation/examples/plugins/format-custom-return-types-plugin");
}

@Test
public void shouldGenerateJavaClassSourceCode() throws Exception {

final String packageName = "uk.gov.justice.pojo.formatcustomreturntypes";

final PojoGeneratorProperties generatorProperties = pojoGeneratorPropertiesBuilder()
.withRootClassName("EmployeeWithFormatCustomReturnType")
.addFormatTypeMappingOf("date-time", "java.time.ZonedDateTime")
.build();

final List<Class<?>> classes = generatorUtil
.withGeneratorProperties(generatorProperties)
.withTypeModifyingPlugin(formatCustomReturnTypePlugin())
.generateAndCompileJavaSource(
JSON_SCHEMA_FILE,
packageName,
outputDirectories);

final String firstName = "firstName";
final String lastName = "lastName";
final ZonedDateTime startDate = ZonedDateTimes.fromString("2016-03-18T00:46:54.700Z");

final Class<?> employeeClass = classes.get(0);

final Object employee = classInstantiator.newInstance(
employeeClass,
firstName,
lastName,
startDate);

final String employeeJson = objectMapper.writeValueAsString(employee);

with(employeeJson)
.assertThat("$.firstName", is(firstName))
.assertThat("$.lastName", is(lastName))
.assertThat("$.startDate", is("2016-03-18T00:46:54.700Z"))
;

generatorUtil.validate(JSON_SCHEMA_FILE, employeeJson);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void setup() throws Exception {
}

@Test
public void shouldGenerateAClassWithAMapForAdditionalPropertiesIfAdditionalPropertiesIsTrue() throws Exception {
public void shouldGenerateClassWithMapForAdditionalPropertiesIfAdditionalPropertiesIsTrue() throws Exception {
final String packageName = "uk.gov.justice.pojo.builder";


Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package uk.gov.justice.generation.pojo.integration.examples.plugins;

import static com.google.common.collect.ImmutableMap.of;
import static com.jayway.jsonassert.JsonAssert.with;
import static java.util.UUID.randomUUID;
import static org.hamcrest.CoreMatchers.is;
import static uk.gov.justice.generation.pojo.integration.utils.PojoGeneratorPropertiesBuilder.pojoGeneratorPropertiesBuilder;
import static uk.gov.justice.generation.pojo.plugin.typemodifying.CustomReturnTypePlugin.newCustomReturnTypePlugin;
import static uk.gov.justice.generation.pojo.plugin.typemodifying.ReferenceCustomReturnTypePlugin.customReturnTypePlugin;

import uk.gov.justice.generation.pojo.core.PojoGeneratorProperties;
import uk.gov.justice.generation.pojo.integration.utils.ClassInstantiator;
Expand All @@ -23,11 +22,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class CustomReturnTypePluginIT {
public class ReferenceCustomReturnTypePluginIT {

private final ObjectMapper objectMapper = new ObjectMapperProducer().objectMapper();
private final GeneratorUtil generatorUtil = new GeneratorUtil();
Expand All @@ -48,16 +44,14 @@ public void shouldGenerateJavaClassSourceCode() throws Exception {

final PojoGeneratorProperties generatorProperties = pojoGeneratorPropertiesBuilder()
.withRootClassName("EmployeeWithCustomReturnTypes")
.withTypeMappings(of(
"uuid", "java.util.UUID",
"bigInteger", "java.math.BigInteger",
"date", "java.time.ZonedDateTime"
))
.addReferenceTypeMappingOf("uuid", "java.util.UUID")
.addReferenceTypeMappingOf("bigInteger", "java.math.BigInteger")
.addReferenceTypeMappingOf("date", "java.time.ZonedDateTime")
.build();

final List<Class<?>> classes = generatorUtil
.withGeneratorProperties(generatorProperties)
.withTypeModifyingPlugin(newCustomReturnTypePlugin())
.withTypeModifyingPlugin(customReturnTypePlugin())
.generateAndCompileJavaSource(
JSON_SCHEMA_FILE,
packageName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void setup() throws Exception {
}

@Test
public void shouldGenerateAnEmptyClassWithAdditionalPropertiesIfNoAdditionalPropertiesSpecified() throws Exception {
public void shouldGenerateEmptyClassWithAdditionalPropertiesIfNoAdditionalPropertiesSpecified() throws Exception {

final File jsonSchemaFile = new File("src/test/resources/schemas/tests/empty-schema.json");

Expand All @@ -51,7 +51,7 @@ public void shouldGenerateAnEmptyClassWithAdditionalPropertiesIfNoAdditionalProp
}

@Test
public void shouldGenerateAnEmptyClassWithAdditionalPropertiesIfoAdditionalPropertiesIsSetToTrue() throws Exception {
public void shouldGenerateEmptyClassWithAdditionalPropertiesIfAdditionalPropertiesIsSetToTrue() throws Exception {

final File jsonSchemaFile = new File("src/test/resources/schemas/tests/empty-schema-with-additional-properties-true.json");

Expand All @@ -75,7 +75,7 @@ public void shouldGenerateAnEmptyClassWithAdditionalPropertiesIfoAdditionalPrope
}

@Test
public void shouldGenerateAnEmptyClassWithAdditionalPropertiesIfoAdditionalPropertiesIsSetToFalse() throws Exception {
public void shouldGenerateEmptyClassWithAdditionalPropertiesIfAdditionalPropertiesIsSetToFalse() throws Exception {

final File jsonSchemaFile = new File("src/test/resources/schemas/tests/empty-schema-with-additional-properties-false.json");
final String packageName = "uk.gov.justice.pojo.empty.schemas";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void setup() throws Exception {
}

@Test
public void shouldGeneratePojosFromANullSchema() throws Exception {
public void shouldGeneratePojosFromNullSchema() throws Exception {
final File jsonSchemaFile = new File("src/test/resources/schemas/tests/null-schema.json");
final String packageName = "uk.gov.justice.pojo.nullable.schema";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class SchemaValidationIT {
private final SchemaValidatorVisitor schemaValidatorVisitor = new SchemaValidatorVisitor(new Validator());

@Test
public void shouldFailValidationIfAnEnumContainsDiversTypesOfValues() throws Exception {
public void shouldFailValidationIfEnumContainsDifferentTypesOfValues() throws Exception {
final File jsonSchemaFile = new File("src/test/resources/invalid-schemas/invalid-enum.json");
final Schema schema = schemaLoader.loadFrom(jsonSchemaFile);

Expand All @@ -38,7 +38,7 @@ public void shouldFailValidationIfAnEnumContainsDiversTypesOfValues() throws Exc
}

@Test
public void shouldFailValidationIfAnArrayContainsDiversTypesOfValues() throws Exception {
public void shouldFailValidationIfArrayContainsDifferentTypesOfValues() throws Exception {
final File jsonSchemaFile = new File("src/test/resources/invalid-schemas/invalid-array.json");
final Schema schema = schemaLoader.loadFrom(jsonSchemaFile);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import uk.gov.justice.generation.pojo.visitor.DefinitionBuilderVisitor;
import uk.gov.justice.generation.pojo.visitor.DefinitionFactory;
import uk.gov.justice.generation.pojo.visitor.ReferenceValueParser;
import uk.gov.justice.generation.pojo.visitor.StringFormatValueParser;
import uk.gov.justice.generation.pojo.write.SourceWriter;

import java.io.File;
Expand All @@ -37,7 +38,7 @@

public class GeneratorUtil {

private final DefinitionFactory definitionFactory = new DefaultDefinitionFactory(new ReferenceValueParser());
private final DefinitionFactory definitionFactory = new DefaultDefinitionFactory(new ReferenceValueParser(), new StringFormatValueParser());
private final DefinitionBuilderVisitor definitionBuilderVisitor = new DefinitionBuilderVisitor(definitionFactory);
private final VisitableFactory visitableFactory = new VisitableFactory();
private final SchemaLoader schemaLoader = new SchemaLoader();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package uk.gov.justice.generation.pojo.integration.utils;

import static org.junit.Assert.assertTrue;
import static uk.gov.justice.generation.pojo.integration.utils.TypeMappingFactory.typeMappingOf;

import uk.gov.justice.generation.pojo.core.PojoGeneratorProperties;
import uk.gov.justice.generation.pojo.core.TypeMapping;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class PojoGeneratorPropertiesBuilder {

private boolean excludeDefaultPlugins;
private List<String> plugins;
private String rootClassName;
private Map<String, String> typeMappings;
private List<TypeMapping> typeMappings = new ArrayList<>();

public static PojoGeneratorPropertiesBuilder pojoGeneratorPropertiesBuilder() {
return new PojoGeneratorPropertiesBuilder();
Expand All @@ -36,8 +38,13 @@ public PojoGeneratorPropertiesBuilder withRootClassName(final String rootClassNa
return this;
}

public PojoGeneratorPropertiesBuilder withTypeMappings(final Map<String, String> typeMappings) {
this.typeMappings = typeMappings;
public PojoGeneratorPropertiesBuilder addReferenceTypeMappingOf(final String name, final String implementation) throws IllegalAccessException {
typeMappings.add(typeMappingOf("reference", name, implementation));
return this;
}

public PojoGeneratorPropertiesBuilder addFormatTypeMappingOf(final String name, final String implementation) throws IllegalAccessException {
typeMappings.add(typeMappingOf("format", name, implementation));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package uk.gov.justice.generation.pojo.integration.utils;

import static uk.gov.justice.generation.pojo.integration.utils.PojoGeneratorPropertiesBuilder.setField;

import uk.gov.justice.generation.pojo.core.TypeMapping;

public class TypeMappingFactory {

private static String type;
private static String name;
private static String implementation;

public static TypeMapping typeMappingOf(final String type, final String name, final String implementation) throws IllegalAccessException {
final TypeMapping typeMapping = new TypeMapping();

setField(typeMapping, "type", type);
setField(typeMapping, "name", name);
setField(typeMapping, "implementation", implementation);

return typeMapping;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"startDate": {
"type": "string",
"format": "date-time"
}
},
"additionalProperties": false,
"required": [
"firstName",
"lastName",
"startDate"
]
}
5 changes: 5 additions & 0 deletions pojo-generator-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.trajano.commons</groupId>
<artifactId>commons-testing</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import uk.gov.justice.generation.pojo.visitable.acceptor.DefaultAcceptorService;
import uk.gov.justice.generation.pojo.visitor.DefaultDefinitionFactory;
import uk.gov.justice.generation.pojo.visitor.DefinitionBuilderVisitor;
import uk.gov.justice.generation.pojo.visitor.DefinitionFactory;
import uk.gov.justice.generation.pojo.visitor.ReferenceValueParser;
import uk.gov.justice.generation.pojo.visitor.StringFormatValueParser;
import uk.gov.justice.generation.pojo.write.JavaSourceFileProvider;
import uk.gov.justice.generation.pojo.write.NonDuplicatingSourceWriter;
import uk.gov.justice.generation.pojo.write.SourceWriter;
Expand All @@ -21,7 +23,8 @@ public class SchemaPojoGeneratorFactory implements GeneratorFactory<File> {

private final SchemaLoader schemaLoader = new SchemaLoader();
private final VisitableFactory visitableFactory = new VisitableFactory();
private final DefinitionBuilderVisitor definitionBuilderVisitor = new DefinitionBuilderVisitor(new DefaultDefinitionFactory(new ReferenceValueParser()));
private final DefinitionFactory definitionFactory = new DefaultDefinitionFactory(new ReferenceValueParser(), new StringFormatValueParser());
private final DefinitionBuilderVisitor definitionBuilderVisitor = new DefinitionBuilderVisitor(definitionFactory);
private final AcceptorService acceptorService = new DefaultAcceptorService(visitableFactory);

private final JavaFileSimpleNameLister javaFileSimpleNameLister = new JavaFileSimpleNameLister();
Expand Down
Loading

0 comments on commit ccd5d13

Please sign in to comment.