use of uk.gov.gchq.gaffer.serialisation.implementation.MapSerialiser in project Gaffer by gchq.
the class SchemaTest method testLoadingSchemaFromJson.
@Test
public void testLoadingSchemaFromJson() {
// Edge definitions
SchemaElementDefinition edgeDefinition = schema.getEdge(TestGroups.EDGE);
assertNotNull(edgeDefinition);
assertEquals(EDGE_DESCRIPTION, edgeDefinition.getDescription());
final Map<String, String> propertyMap = edgeDefinition.getPropertyMap();
assertThat(propertyMap).hasSize(3);
assertEquals("prop.string", propertyMap.get(TestPropertyNames.PROP_2));
assertEquals("prop.date", propertyMap.get(TestPropertyNames.DATE));
assertEquals("timestamp", propertyMap.get(TestPropertyNames.TIMESTAMP));
assertEquals(Sets.newLinkedHashSet(Collections.singletonList(TestPropertyNames.DATE)), edgeDefinition.getGroupBy());
// Check validator
ElementFilter validator = edgeDefinition.getValidator();
List<TupleAdaptedPredicate<String, ?>> valContexts = validator.getComponents();
int index = 0;
TupleAdaptedPredicate<String, ?> tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof IsA);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(IdentifierType.SOURCE.name(), tuplePredicate.getSelection()[0]);
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof IsA);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(IdentifierType.DESTINATION.name(), tuplePredicate.getSelection()[0]);
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof IsA);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(IdentifierType.DIRECTED.name(), tuplePredicate.getSelection()[0]);
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof ExampleFilterFunction);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(IdentifierType.DIRECTED.name(), tuplePredicate.getSelection()[0]);
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof IsA);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(TestPropertyNames.PROP_2, tuplePredicate.getSelection()[0]);
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof ExampleFilterFunction);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(TestPropertyNames.PROP_2, tuplePredicate.getSelection()[0]);
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof IsA);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(TestPropertyNames.DATE, tuplePredicate.getSelection()[0]);
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof IsA);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(TestPropertyNames.TIMESTAMP, tuplePredicate.getSelection()[0]);
assertEquals(index, valContexts.size());
TypeDefinition type = edgeDefinition.getPropertyTypeDef(TestPropertyNames.DATE);
assertEquals(Date.class, type.getClazz());
assertEquals(DATE_TYPE_DESCRIPTION, type.getDescription());
assertNull(type.getSerialiser());
assertTrue(type.getAggregateFunction() instanceof ExampleAggregateFunction);
// Entity definitions
SchemaElementDefinition entityDefinition = schema.getEntity(TestGroups.ENTITY);
assertNotNull(entityDefinition);
assertEquals(ENTITY_DESCRIPTION, entityDefinition.getDescription());
assertTrue(entityDefinition.containsProperty(TestPropertyNames.PROP_1));
type = entityDefinition.getPropertyTypeDef(TestPropertyNames.PROP_1);
assertEquals(0, entityDefinition.getGroupBy().size());
assertEquals(STRING_TYPE_DESCRIPTION, type.getDescription());
assertEquals(String.class, type.getClazz());
assertNull(type.getSerialiser());
assertTrue(type.getAggregateFunction() instanceof ExampleAggregateFunction);
validator = entityDefinition.getValidator();
valContexts = validator.getComponents();
index = 0;
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof IsXMoreThanY);
assertThat(tuplePredicate.getSelection()).hasSize(2);
assertEquals(TestPropertyNames.PROP_1, tuplePredicate.getSelection()[0]);
assertEquals(TestPropertyNames.VISIBILITY, tuplePredicate.getSelection()[1]);
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof IsA);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(IdentifierType.VERTEX.name(), tuplePredicate.getSelection()[0]);
tuplePredicate = valContexts.get(index++);
assertTrue(tuplePredicate.getPredicate() instanceof IsA);
assertThat(tuplePredicate.getSelection()).hasSize(1);
assertEquals(TestPropertyNames.PROP_1, tuplePredicate.getSelection()[0]);
final ElementAggregator aggregator = edgeDefinition.getFullAggregator();
final List<TupleAdaptedBinaryOperator<String, ?>> aggContexts = aggregator.getComponents();
assertThat(aggContexts).hasSize(3);
TupleAdaptedBinaryOperator<String, ?> aggContext = aggContexts.get(0);
assertTrue(aggContext.getBinaryOperator() instanceof ExampleAggregateFunction);
assertThat(aggContext.getSelection()).hasSize(1);
assertEquals(TestPropertyNames.PROP_2, aggContext.getSelection()[0]);
aggContext = aggContexts.get(1);
assertTrue(aggContext.getBinaryOperator() instanceof ExampleAggregateFunction);
assertThat(aggContext.getSelection()).hasSize(1);
assertEquals(TestPropertyNames.DATE, aggContext.getSelection()[0]);
TypeDefinition mapTypeDef = schema.getType(TestTypes.PROP_MAP);
assertEquals(LinkedHashMap.class, mapTypeDef.getClazz());
assertEquals(MAP_TYPE_DESCRIPTION, mapTypeDef.getDescription());
Serialiser serialiser = mapTypeDef.getSerialiser();
assertEquals(MapSerialiser.class, serialiser.getClass());
MapSerialiser mapSerialiser = (MapSerialiser) serialiser;
assertEquals(StringSerialiser.class, mapSerialiser.getKeySerialiser().getClass());
assertEquals(RawLongSerialiser.class, mapSerialiser.getValueSerialiser().getClass());
assertNull(mapSerialiser.getMapClass());
}
use of uk.gov.gchq.gaffer.serialisation.implementation.MapSerialiser in project Gaffer by gchq.
the class SchemaTest method createSchema.
private Schema createSchema() {
MapSerialiser mapSerialiser = new MapSerialiser();
mapSerialiser.setKeySerialiser(new StringSerialiser());
mapSerialiser.setValueSerialiser(new RawLongSerialiser());
mapSerialiser.setMapClass(LinkedHashMap.class);
return new Schema.Builder().edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder().source(TestTypes.ID_STRING).destination(TestTypes.ID_STRING).property(TestPropertyNames.PROP_1, TestTypes.PROP_STRING).property(TestPropertyNames.PROP_2, TestTypes.PROP_INTEGER).property(TestPropertyNames.TIMESTAMP, TestTypes.TIMESTAMP).groupBy(TestPropertyNames.PROP_1).description(EDGE_DESCRIPTION).validator(new ElementFilter.Builder().select(TestPropertyNames.PROP_1).execute(new ExampleFilterFunction()).build()).build()).entity(TestGroups.ENTITY, new SchemaEntityDefinition.Builder().vertex(TestTypes.ID_STRING).property(TestPropertyNames.PROP_1, TestTypes.PROP_STRING).property(TestPropertyNames.PROP_2, TestTypes.PROP_INTEGER).property(TestPropertyNames.TIMESTAMP, TestTypes.TIMESTAMP).groupBy(TestPropertyNames.PROP_1).description(EDGE_DESCRIPTION).validator(new ElementFilter.Builder().select(TestPropertyNames.PROP_1).execute(new ExampleFilterFunction()).build()).build()).type(TestTypes.ID_STRING, new TypeDefinition.Builder().clazz(String.class).description(STRING_TYPE_DESCRIPTION).build()).type(TestTypes.PROP_MAP, new TypeDefinition.Builder().description(MAP_TYPE_DESCRIPTION).clazz(LinkedHashMap.class).serialiser(mapSerialiser).build()).type(TestTypes.PROP_STRING, new TypeDefinition.Builder().clazz(String.class).description(STRING_TYPE_DESCRIPTION).build()).type(TestTypes.PROP_INTEGER, new TypeDefinition.Builder().clazz(Integer.class).description(INTEGER_TYPE_DESCRIPTION).build()).type(TestTypes.TIMESTAMP, new TypeDefinition.Builder().clazz(Long.class).description(TIMESTAMP_TYPE_DESCRIPTION).build()).visibilityProperty(TestPropertyNames.VISIBILITY).timestampProperty(TestPropertyNames.TIMESTAMP).config("key", "value").build();
}
use of uk.gov.gchq.gaffer.serialisation.implementation.MapSerialiser in project Gaffer by gchq.
the class CustomMapTest method shouldJSONSerialiseStringMap.
@Test
public void shouldJSONSerialiseStringMap() throws IOException {
// given
final MapSerialiser mapSerialiser = new MapSerialiser();
mapSerialiser.setValueSerialiser(new StringSerialiser());
mapSerialiser.setKeySerialiser(new StringSerialiser());
final CustomMap<String, HashMap> expectedMap = new CustomMap<>(new StringSerialiser(), mapSerialiser);
final HashMap<String, String> innerMap1 = new HashMap<>();
innerMap1.put("innerKeyOne", "innerValue1");
final HashMap<String, String> innerMap2 = new HashMap<>();
innerMap2.put("innerKeyTwo", "innerValue2");
expectedMap.put("innerOne", innerMap1);
expectedMap.put("innerTwo", innerMap2);
final String expectedJson = jsonFromFile("custom-map03.json");
// when
final byte[] serialise = JSONSerialiser.serialise(expectedMap, true);
final CustomMap jsonMap = JSONSerialiser.deserialise(expectedJson, CustomMap.class);
final CustomMap deserialiseMap = JSONSerialiser.deserialise(serialise, CustomMap.class);
// then
assertEquals(jsonMap, deserialiseMap, "The expected map from Json doesn't match");
assertEquals(expectedMap, deserialiseMap, "The expected map doesn't match");
}
Aggregations