This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add type mapping for string format field
- Loading branch information
Showing
46 changed files
with
995 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
.../justice/generation/pojo/integration/examples/plugins/FormatCustomReturnTypePluginIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...st/src/test/java/uk/gov/justice/generation/pojo/integration/utils/TypeMappingFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
...n-test/src/test/resources/schemas/examples/plugins/format-custom-return-types-plugin.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.