SchemaOptimiser { public Schema optimise(final Schema schema, final boolean isStoreOrdered) { if (null != schema && null != schema.getTypes()) { return new Schema.Builder() .merge(schema) .types(getOptimisedTypes(schema, isStoreOrdered)) .vertexSerialiser(getDefaultVertexSerialiser(schema, isStoreOrdered)) .build(); } return schema; } SchemaOptimiser();  SchemaOptimiser(final SerialisationFactory serialisationFactory); Schema optimise(final Schema schema, final boolean isStoreOrdered);  }
@Test public void shouldRemoveUnusedTypes() { final SchemaOptimiser optimiser = new SchemaOptimiser(); final boolean isOrdered = true; final Schema optimisedSchema = optimiser.optimise(schema, isOrdered); assertEquals(2, optimisedSchema.getTypes().size()); assertEquals(stringType, schema.getType("string")); assertEquals(intType, schema.getType("int")); }
@Test public void shouldAddDefaultSerialisers() { final SerialisationFactory serialisationFactory = mock(SerialisationFactory.class); final SchemaOptimiser optimiser = new SchemaOptimiser(serialisationFactory); final boolean isOrdered = true; final StringSerialiser stringSerialiser = mock(StringSerialiser.class); final OrderedIntegerSerialiser intSerialiser = mock(OrderedIntegerSerialiser.class); final JavaSerialiser javaSerialiser = mock(JavaSerialiser.class); given(serialisationFactory.getSerialiser(String.class, true, true)).willReturn(stringSerialiser); given(serialisationFactory.getSerialiser(Integer.class, false, false)).willReturn(intSerialiser); given(serialisationFactory.getSerialiser(Serializable.class, true, true)).willReturn(javaSerialiser); given(javaSerialiser.canHandle(Mockito.any(Class.class))).willReturn(true); schema = new Schema.Builder() .merge(schema) .type("obj", Serializable.class) .entity(TestGroups.ENTITY_2, new SchemaEntityDefinition.Builder() .vertex("obj") .build()) .build(); final Schema optimisedSchema = optimiser.optimise(schema, isOrdered); assertSame(stringSerialiser, optimisedSchema.getType("string").getSerialiser()); assertSame(intSerialiser, optimisedSchema.getType("int").getSerialiser()); assertSame(javaSerialiser, optimisedSchema.getVertexSerialiser()); verify(serialisationFactory, never()).getSerialiser(String.class, false, true); verify(serialisationFactory, never()).getSerialiser(Serializable.class, false, true); }
@Test public void shouldThrowExceptionIfDefaultVertexSerialiserCouldNotBeFound() { final SchemaOptimiser optimiser = new SchemaOptimiser(); final boolean isOrdered = true; schema = new Schema.Builder() .merge(schema) .type("obj", Object.class) .entity(TestGroups.ENTITY_2, new SchemaEntityDefinition.Builder() .vertex("obj") .build()) .build(); try { optimiser.optimise(schema, isOrdered); fail("Exception expected"); } catch (final IllegalArgumentException e) { assertNotNull(e.getMessage()); } }