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

Commit

Permalink
Add root set on ClassDefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
mapingo committed Sep 18, 2017
1 parent 50a4f29 commit e505a5d
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package uk.gov.justice.generation.pojo.plugin.classmodifying;

import static uk.gov.justice.generation.pojo.dom.DefinitionType.ROOT;

import uk.gov.justice.domain.annotation.Event;
import uk.gov.justice.generation.pojo.dom.ClassDefinition;

Expand All @@ -22,7 +20,7 @@ public TypeSpec.Builder generateWith(final TypeSpec.Builder classBuilder,
final ClassDefinition classDefinition,
final PluginContext pluginContext) {

if (ROOT.equals(classDefinition.type())) {
if (classDefinition.isRoot()) {

final String eventName = removeFileExtensionFrom(pluginContext.getSourceFilename());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static uk.gov.justice.generation.pojo.dom.DefinitionType.CLASS;
import static uk.gov.justice.generation.pojo.dom.DefinitionType.ROOT;

import uk.gov.justice.domain.annotation.Event;
import uk.gov.justice.generation.pojo.dom.ClassDefinition;
Expand All @@ -27,7 +26,8 @@ public class AddEventAnnotationToClassPluginTest {

@Test
public void shouldAddEventAnnotationToTypeSpecIfRoot() throws Exception {
final ClassDefinition classDefinition = new ClassDefinition(ROOT, "address");
final ClassDefinition classDefinition = new ClassDefinition(CLASS, "address");
classDefinition.setRoot(true);

when(pluginContext.getSourceFilename()).thenReturn("example.events.address.json");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public class ClassDefinition extends FieldDefinition {
private final List<Definition> fieldDefinitions = new ArrayList<>();

private boolean allowAdditionalProperties = false;
private boolean root = false;

/**
* Creates a ClassDefinition
* Creates a ClassDefinition that is not the root class.
*
* @param type The {@link DefinitionType} of the class. Can be a
* {@link DefinitionType#CLASS}
* {@link DefinitionType#ENUM}
* {@link DefinitionType#ROOT} or
* {@link DefinitionType#ENUM} or
* {@link DefinitionType#COMBINED}
* @param fieldName The name of the field that will be used in the generated POJO
*/
Expand All @@ -44,7 +44,7 @@ public void addFieldDefinition(final Definition fieldDefinition) {
* NB: The list of fields is sorted alphabetically by thier field names. This
* results in all fields in both the constructor and the order of fields in the class
* will be alphabetical.
*
*
* @return The list of all fields defined for this class
*/
public List<Definition> getFieldDefinitions() {
Expand Down Expand Up @@ -78,6 +78,24 @@ public void setAllowAdditionalProperties(final boolean allowAdditionalProperties
this.allowAdditionalProperties = allowAdditionalProperties;
}

/**
* Determines whether this is the root definition.
*
* @return true if the root
*/
public boolean isRoot() {
return root;
}

/**
* Sets whether this is the root defintion.
*
* @param root true if the root
*/
public void setRoot(final boolean root) {
this.root = root;
}

private void sortDefinitionsByFieldNameFirst() {
fieldDefinitions.sort(comparing(Definition::getFieldName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ public enum DefinitionType {
*/
REFERENCE,

/**
* This refers to the containing root object of the schema and will be generated as a class
*/
ROOT,

/**
* This property was defined as an string in the json schema:
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public TypeName createTypeNameFrom(final Definition definition) {
case CLASS:
case ENUM:
case COMBINED:
case ROOT:
default:
typeName = typeNameProvider.typeNameForClass(definition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static uk.gov.justice.generation.pojo.dom.DefinitionType.CLASS;
import static uk.gov.justice.generation.pojo.dom.DefinitionType.INTEGER;
import static uk.gov.justice.generation.pojo.dom.DefinitionType.NUMBER;
import static uk.gov.justice.generation.pojo.dom.DefinitionType.ROOT;
import static uk.gov.justice.generation.pojo.dom.DefinitionType.STRING;

import uk.gov.justice.generation.pojo.core.UnsupportedSchemaException;
Expand Down Expand Up @@ -49,8 +48,10 @@ public DefaultDefinitionFactory(final ReferenceValueParser referenceValueParser)
}

@Override
public Definition constructRootClassDefinition(final String fieldName) {
return new ClassDefinition(ROOT, fieldName);
public Definition constructRootDefinitionFor(final String fieldName, final Schema schema) {
final ClassDefinition definition = new ClassDefinition(CLASS, fieldName);
definition.setRoot(true);
return definition;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void enter(final String fieldName, final ObjectSchema schema) {
final ClassDefinition definition;

if (definitions.isEmpty()) {
definition = (ClassDefinition) definitionFactory.constructRootClassDefinition(fieldName);
definition = (ClassDefinition) definitionFactory.constructRootDefinitionFor(fieldName, schema);
} else {
definition = (ClassDefinition) definitionFactory.constructDefinitionFor(fieldName, schema);
}
Expand Down Expand Up @@ -232,7 +232,7 @@ public void visit(final String fieldName, final NullSchema schema) {
public void visit(final String fieldName, final EmptySchema schema) {
final ClassDefinition definition;
if (definitions.isEmpty()) {
definition = (ClassDefinition) definitionFactory.constructRootClassDefinition(fieldName);
definition = (ClassDefinition) definitionFactory.constructRootDefinitionFor(fieldName, schema);
} else {
definition = (ClassDefinition) definitionFactory.constructDefinitionFor(fieldName, schema);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface DefinitionFactory {
* @param fieldName the field name to use for the ROOT definition
* @return the {@link Definition}
*/
Definition constructRootClassDefinition(final String fieldName);
Definition constructRootDefinitionFor(final String fieldName, final Schema schema);

/**
* Constructs a {@link Definition} for the given field name and {@link Schema}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,32 @@ public void shouldAllowAdditionalProperties() throws Exception {

assertThat(classDefinition.allowAdditionalProperties(), is(true));
}

@Test
public void shouldBeTheRootClass() throws Exception {
final ClassDefinition classDefinition = new ClassDefinition(CLASS, "test");

classDefinition.setRoot(true);

assertThat(classDefinition, is(instanceOf(FieldDefinition.class)));
assertThat(classDefinition.isRoot(), is(true));
}

@Test
public void shouldNotBeTheRootClass() throws Exception {
final ClassDefinition classDefinition = new ClassDefinition(CLASS, "test");

classDefinition.setRoot(false);

assertThat(classDefinition, is(instanceOf(FieldDefinition.class)));
assertThat(classDefinition.isRoot(), is(false));
}

@Test
public void shouldDefaultToNotBeTheRootClass() throws Exception {
final ClassDefinition classDefinition = new ClassDefinition(CLASS, "test");

assertThat(classDefinition, is(instanceOf(FieldDefinition.class)));
assertThat(classDefinition.isRoot(), is(false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import static uk.gov.justice.generation.pojo.dom.DefinitionType.INTEGER;
import static uk.gov.justice.generation.pojo.dom.DefinitionType.NUMBER;
import static uk.gov.justice.generation.pojo.dom.DefinitionType.REFERENCE;
import static uk.gov.justice.generation.pojo.dom.DefinitionType.ROOT;
import static uk.gov.justice.generation.pojo.dom.DefinitionType.STRING;

import uk.gov.justice.generation.pojo.dom.ClassDefinition;
Expand Down Expand Up @@ -181,23 +180,6 @@ public void shouldCreateTheCorrectTypeNameForCombinedSchemas() throws Exception
verify(typeNamePluginProcessor).processTypeNamePlugins(originalClassName, combinedDefinition);
}

@Test
public void shouldCreateTheCorrectTypeNameForRootSchemas() throws Exception {

final ClassDefinition rootDefinition = mock(ClassDefinition.class);
final ClassName originalClassName = get(TestClass.class);
final TypeName modifiedTypeName = mock(TypeName.class);

when(rootDefinition.type()).thenReturn(ROOT);
when(typeNameProvider.typeNameForClass(rootDefinition)).thenReturn(originalClassName);
when(typeNamePluginProcessor.processTypeNamePlugins(originalClassName, rootDefinition)).thenReturn(modifiedTypeName);

assertThat(classNameFactory.createTypeNameFrom(rootDefinition), is(modifiedTypeName));

verify(typeNameProvider).typeNameForClass(rootDefinition);
verify(typeNamePluginProcessor).processTypeNamePlugins(originalClassName, rootDefinition);
}

@Test
public void shouldCreateTheCorrectTypeNameForReference() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import static uk.gov.justice.generation.pojo.dom.DefinitionType.INTEGER;
import static uk.gov.justice.generation.pojo.dom.DefinitionType.NUMBER;
import static uk.gov.justice.generation.pojo.dom.DefinitionType.REFERENCE;
import static uk.gov.justice.generation.pojo.dom.DefinitionType.ROOT;
import static uk.gov.justice.generation.pojo.dom.DefinitionType.STRING;

import uk.gov.justice.generation.pojo.core.UnsupportedSchemaException;
Expand Down Expand Up @@ -63,11 +62,14 @@ public class DefaultDefinitionFactoryTest {
@Test
public void shouldConstructRootClassDefinition() throws Exception {
final String fieldName = "fieldName";
final Definition definition = defaultDefinitionFactory.constructRootClassDefinition(fieldName);
final ObjectSchema objectSchema = ObjectSchema.builder().build();

final Definition definition = defaultDefinitionFactory.constructRootDefinitionFor(fieldName, objectSchema);

assertThat(definition, is(instanceOf(ClassDefinition.class)));
assertThat(definition.type(), is(ROOT));
assertThat(definition.type(), is(CLASS));
assertThat(definition.getFieldName(), is(fieldName));
assertThat(((ClassDefinition) definition).isRoot(), is(true));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void shouldGenerateMultiLevelClassDefinitions() throws Exception {
final FieldDefinition innerStringDefinition = mock(FieldDefinition.class);
final FieldDefinition outerStringDefinition = mock(FieldDefinition.class);

when(definitionFactory.constructRootClassDefinition(outerFieldName)).thenReturn(outerDefinition);
when(definitionFactory.constructRootDefinitionFor(outerFieldName, outerObjectSchema)).thenReturn(outerDefinition);
when(definitionFactory.constructDefinitionFor(innerFieldName, innerObjectSchema)).thenReturn(innerDefinition);
when(definitionFactory.constructDefinitionFor(outerStringFieldName, outerStringSchema)).thenReturn(outerStringDefinition);
when(definitionFactory.constructDefinitionFor(innerStringField, innerStringSchema)).thenReturn(innerStringDefinition);
Expand Down Expand Up @@ -107,7 +107,7 @@ public void shouldGenerateClassDefinitionWithBooleanSchemaProperty() throws Exce
final ClassDefinition outerDefinition = mock(ClassDefinition.class);
final FieldDefinition booleanDefinition = mock(FieldDefinition.class);

when(definitionFactory.constructRootClassDefinition(outerFieldName)).thenReturn(outerDefinition);
when(definitionFactory.constructRootDefinitionFor(outerFieldName, objectSchema)).thenReturn(outerDefinition);
when(definitionFactory.constructDefinitionFor(booleanFieldName, booleanSchema)).thenReturn(booleanDefinition);

final DefinitionBuilderVisitor definitionBuilderVisitor = new DefinitionBuilderVisitor(definitionFactory);
Expand Down Expand Up @@ -137,7 +137,7 @@ public void shouldGenerateClassDefinitionWithEnumSchemaProperty() throws Excepti
final ClassDefinition outerDefinition = mock(ClassDefinition.class);
final EnumDefinition enumDefinition = mock(EnumDefinition.class);

when(definitionFactory.constructRootClassDefinition(outerFieldName)).thenReturn(outerDefinition);
when(definitionFactory.constructRootDefinitionFor(outerFieldName, objectSchema)).thenReturn(outerDefinition);
when(definitionFactory.constructDefinitionFor(enumFieldName, enumSchema)).thenReturn(enumDefinition);

final DefinitionBuilderVisitor definitionBuilderVisitor = new DefinitionBuilderVisitor(definitionFactory);
Expand Down Expand Up @@ -170,7 +170,7 @@ public void shouldGenerateClassDefinitionWithNumberSchemaProperty() throws Excep
final FieldDefinition numberDefinition = mock(FieldDefinition.class);
final FieldDefinition integerDefinition = mock(FieldDefinition.class);

when(definitionFactory.constructRootClassDefinition(outerFieldName)).thenReturn(outerDefinition);
when(definitionFactory.constructRootDefinitionFor(outerFieldName, objectSchema)).thenReturn(outerDefinition);
when(definitionFactory.constructDefinitionFor(numberFieldName, numberProperty)).thenReturn(numberDefinition);
when(definitionFactory.constructDefinitionFor(integerFieldName, integerProperty)).thenReturn(integerDefinition);

Expand Down Expand Up @@ -209,7 +209,7 @@ public void shouldGenerateClassDefinitionWithArraySchemaProperty() throws Except
final ClassDefinition arrayDefinition = mock(ClassDefinition.class);
final ClassDefinition arrayObjectDefinition = mock(ClassDefinition.class);

when(definitionFactory.constructRootClassDefinition(outerFieldName)).thenReturn(outerDefinition);
when(definitionFactory.constructRootDefinitionFor(outerFieldName, objectSchema)).thenReturn(outerDefinition);
when(definitionFactory.constructDefinitionFor(arrayFieldName, arraySchema)).thenReturn(arrayDefinition);
when(definitionFactory.constructDefinitionFor(arrayObjectFieldName, arrayObjectSchema)).thenReturn(arrayObjectDefinition);

Expand Down Expand Up @@ -250,7 +250,7 @@ public void shouldGenerateClassDefinitionWithReferenceSchemaProperty() throws Ex
final ReferenceDefinition referenceDefinition = mock(ReferenceDefinition.class);
final ClassDefinition referredDefinition = mock(ClassDefinition.class);

when(definitionFactory.constructRootClassDefinition(outerFieldName)).thenReturn(outerDefinition);
when(definitionFactory.constructRootDefinitionFor(outerFieldName, objectSchema)).thenReturn(outerDefinition);
when(definitionFactory.constructDefinitionFor(referenceFieldName, referenceSchema)).thenReturn(referenceDefinition);
when(definitionFactory.constructDefinitionFor(referredFieldName, referredSchema)).thenReturn(referredDefinition);

Expand Down Expand Up @@ -286,7 +286,7 @@ public void shouldGenerateClassDefinitionWithStringSchemaProperty() throws Excep
final ClassDefinition outerDefinition = mock(ClassDefinition.class);
final FieldDefinition stringDefinition = mock(FieldDefinition.class);

when(definitionFactory.constructRootClassDefinition(outerFieldName)).thenReturn(outerDefinition);
when(definitionFactory.constructRootDefinitionFor(outerFieldName, objectSchema)).thenReturn(outerDefinition);
when(definitionFactory.constructDefinitionFor(stringFieldName, stringSchema)).thenReturn(stringDefinition);

final DefinitionBuilderVisitor definitionBuilderVisitor = new DefinitionBuilderVisitor(definitionFactory);
Expand Down Expand Up @@ -446,7 +446,7 @@ public void shouldGenerateRootClassDefinitionWithRootEmptySchemaProperty() throw

final ClassDefinition emptyClassDefinition = mock(ClassDefinition.class);

when(definitionFactory.constructRootClassDefinition(emptyFieldName)).thenReturn(emptyClassDefinition);
when(definitionFactory.constructRootDefinitionFor(emptyFieldName, rootEmptySchema)).thenReturn(emptyClassDefinition);

final DefinitionBuilderVisitor definitionBuilderVisitor = new DefinitionBuilderVisitor(definitionFactory);

Expand Down Expand Up @@ -475,7 +475,7 @@ public void shouldGenerateClassDefinitionWithNonRootEmptySchemaProperty() throws
final ClassDefinition rootClassDefinition = mock(ClassDefinition.class);
final ClassDefinition emptyClassDefinition = mock(ClassDefinition.class);

when(definitionFactory.constructRootClassDefinition(rootObjectFieldName)).thenReturn(rootClassDefinition);
when(definitionFactory.constructRootDefinitionFor(rootObjectFieldName, objectSchema)).thenReturn(rootClassDefinition);
when(definitionFactory.constructDefinitionFor(emptyFieldName, emptySchema)).thenReturn(emptyClassDefinition);

final DefinitionBuilderVisitor definitionBuilderVisitor = new DefinitionBuilderVisitor(definitionFactory);
Expand Down

0 comments on commit e505a5d

Please sign in to comment.