Search in sources :

Example 1 with MapSerialiser

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());
}
Also used : TupleAdaptedPredicate(uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate) RawLongSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.raw.RawLongSerialiser) MapSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.MapSerialiser) Serialiser(uk.gov.gchq.gaffer.serialisation.Serialiser) StringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.StringSerialiser) JavaSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.JavaSerialiser) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) StringToStringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.tostring.StringToStringSerialiser) MapSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.MapSerialiser) IsA(uk.gov.gchq.koryphe.impl.predicate.IsA) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ExampleAggregateFunction(uk.gov.gchq.gaffer.function.ExampleAggregateFunction) TupleAdaptedBinaryOperator(uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator) ExampleFilterFunction(uk.gov.gchq.gaffer.function.ExampleFilterFunction) IsXMoreThanY(uk.gov.gchq.koryphe.impl.predicate.IsXMoreThanY) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator) Test(org.junit.jupiter.api.Test)

Example 2 with MapSerialiser

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();
}
Also used : StringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.StringSerialiser) StringToStringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.tostring.StringToStringSerialiser) MapSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.MapSerialiser) RawLongSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.raw.RawLongSerialiser) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ExampleFilterFunction(uk.gov.gchq.gaffer.function.ExampleFilterFunction)

Example 3 with MapSerialiser

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");
}
Also used : StringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.StringSerialiser) TreeSetStringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.TreeSetStringSerialiser) MapSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.MapSerialiser) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Aggregations

MapSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.MapSerialiser)3 StringSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.StringSerialiser)3 Test (org.junit.jupiter.api.Test)2 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)2 ExampleFilterFunction (uk.gov.gchq.gaffer.function.ExampleFilterFunction)2 RawLongSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.raw.RawLongSerialiser)2 StringToStringSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.tostring.StringToStringSerialiser)2 HashMap (java.util.HashMap)1 ElementAggregator (uk.gov.gchq.gaffer.data.element.function.ElementAggregator)1 ExampleAggregateFunction (uk.gov.gchq.gaffer.function.ExampleAggregateFunction)1 Serialiser (uk.gov.gchq.gaffer.serialisation.Serialiser)1 ToBytesSerialiser (uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser)1 JavaSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.JavaSerialiser)1 TreeSetStringSerialiser (uk.gov.gchq.gaffer.serialisation.implementation.TreeSetStringSerialiser)1 IsA (uk.gov.gchq.koryphe.impl.predicate.IsA)1 IsXMoreThanY (uk.gov.gchq.koryphe.impl.predicate.IsXMoreThanY)1 TupleAdaptedBinaryOperator (uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator)1 TupleAdaptedPredicate (uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate)1