@Test public void shouldGenerateMapFromASingleProperty() { // Given final Object vertex = "source vertex"; final String prop1 = "property 1"; final int count = 10; final Element element = new Entity.Builder() .group(TestGroups.ENTITY) .vertex(vertex) .property(TestPropertyNames.PROP_1, prop1) .property(TestPropertyNames.COUNT, count) .build(); final MapGenerator generator = new MapGenerator.Builder() .property(TestPropertyNames.PROP_1, "field1") .build(); // When final Map<String, Object> map = generator._apply(element); // Then final Map<String, Object> expectedMap = new LinkedHashMap<>(); expectedMap.put("field1", prop1); assertEquals(expectedMap, map); }
@Test public void shouldTestElementOnLambdaPredicate() { // Given final Predicate<Object> predicate = p -> null == p || String.class.isAssignableFrom(p.getClass()); final ElementFilter filter = new ElementFilter.Builder() .select("prop1") .execute(predicate) .build(); final Entity element1 = new Entity.Builder() .property("prop1", "value") .build(); final Entity element2 = new Entity.Builder() .property("prop1", 1) .build(); // When final boolean result1 = filter.test(element1); final boolean result2 = filter.test(element2); // Then assertTrue(result1); assertFalse(result2); }
@Test public void shouldCompareWhenFirstElementsHasWrongGroup() { // Given final ElementPropertyComparator comparator = new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY_2) .property(TestPropertyNames.PROP_1) .comparator(new ComparatorImpl()) .build(); final Entity smallEntity = new Entity.Builder().group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, new IntegerWrapper(1)) .build(); final Entity largeEntity = new Entity.Builder().group(TestGroups.ENTITY_2) .property(TestPropertyNames.PROP_1, new IntegerWrapper(2)) .build(); // When final int result = comparator.compare(smallEntity, largeEntity); // Then assertThat(result, greaterThan(0)); }
@Test public void shouldCompareWhenBothElementsHaveWrongGroup() { // Given final ElementPropertyComparator comparator = new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY_2) .property(TestPropertyNames.PROP_1) .comparator(new ComparatorImpl()) .build(); final Entity smallEntity = new Entity.Builder().group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, new IntegerWrapper(1)) .build(); final Entity largeEntity = new Entity.Builder().group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, new IntegerWrapper(2)) .build(); // When final int result = comparator.compare(smallEntity, largeEntity); // Then assertThat(result, equalTo(0)); }
@Test public void shouldCompare() { // Given final ElementPropertyComparator comparator = new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1) .comparator(new ComparatorImpl()) .build(); final Entity smallEntity = new Entity.Builder().group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, new IntegerWrapper(1)) .build(); final Entity largeEntity = new Entity.Builder().group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, new IntegerWrapper(2)) .build(); // When final int result = comparator.compare(smallEntity, largeEntity); // Then assertThat(result, lessThan(0)); }
@Test public void shouldCompareWhenSecondElementsHasWrongGroup() { // Given final ElementPropertyComparator comparator = new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY_2) .property(TestPropertyNames.PROP_1) .comparator(new ComparatorImpl()) .build(); final Entity smallEntity = new Entity.Builder().group(TestGroups.ENTITY_2) .property(TestPropertyNames.PROP_1, new IntegerWrapper(1)) .build(); final Entity largeEntity = new Entity.Builder().group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, new IntegerWrapper(2)) .build(); // When final int result = comparator.compare(smallEntity, largeEntity); // Then assertThat(result, lessThan(0)); }
@Test public void shouldCompareWhenFirstElementHasMissingProperties() { // Given final ElementPropertyComparator comparator = new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY) .property(TestPropertyNames.PROP_2) .comparator(new ComparatorImpl()) .build(); final Entity smallEntity = new Entity.Builder().group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, new IntegerWrapper(1)) .build(); final Entity largeEntity = new Entity.Builder().group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_2, new IntegerWrapper(2)) .build(); // When final int result = comparator.compare(smallEntity, largeEntity); // Then assertThat(result, greaterThan(0)); }
@Test public void shouldCompareWhenBothElementsHaveMissingProperties() { // Given final ElementPropertyComparator comparator = new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY) .property(TestPropertyNames.PROP_2) .comparator(new ComparatorImpl()) .build(); final Entity smallEntity = new Entity.Builder().group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, new IntegerWrapper(1)) .build(); final Entity largeEntity = new Entity.Builder().group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, new IntegerWrapper(2)) .build(); // When final int result = comparator.compare(smallEntity, largeEntity); // Then assertThat(result, equalTo(0)); }
@Test public void shouldTestElementOnInlinePredicate() { // Given final ElementFilter filter = new ElementFilter.Builder() .select("prop1") .execute("value"::equals) .build(); final Entity element1 = new Entity.Builder() .property("prop1", "value") .build(); final Entity element2 = new Entity.Builder() .property("prop1", "unknown") .build(); // When final boolean result1 = filter.test(element1); final boolean result2 = filter.test(element2); // Then assertTrue(result1); assertFalse(result2); }
@Test public void shouldTransformElementUsingIdentityFunction() { // Given final ElementTransformer transformer = new ElementTransformer.Builder() .select("prop1") .execute(new Identity()) .project("prop3") .build(); final Entity element = new Entity.Builder() .group("test") .property("prop1", "value") .property("prop2", 1) .build(); // When final Element result = transformer.apply(element); // Then assertEquals(element.getProperty("prop1"), result.getProperty("prop3")); }
@Test public void shouldTransformElementUsingInlineFunction() { // Given final Function<String, Integer> function = String::length; final ElementTransformer transformer = new ElementTransformer.Builder() .select("prop1") .execute(function) .project("prop3") .build(); final Entity element = new Entity.Builder() .group("test") .property("prop1", "value") .property("prop2", 1) .build(); // When final Element result = transformer.apply(element); // Then assertEquals("prop1".length(), result.getProperty("prop3")); }
@Test public void shouldNotBeEqualOnDifferentElement() { // Given final Entity testEntity1 = new Entity.Builder() .group(TestGroups.ENTITY_3) .vertex("vertex") .property(TestPropertyNames.SET, CollectionUtil.treeSet("3")) .property(TestPropertyNames.COUNT, 3L) .build(); final Entity testEntity2 = new Entity.Builder() .group(TestGroups.ENTITY_4) .vertex("vertex") .property(TestPropertyNames.SET, CollectionUtil.treeSet("3")) .property(TestPropertyNames.COUNT, 3L) .build(); ElementJoinComparator elementJoinComparator = new ElementJoinComparator(); // When / Then assertFalse(elementJoinComparator.test(testEntity1, testEntity2)); }
@Test public void shouldCompareReversed() { // Given final ElementComparator comparator = new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1) .comparator(new ComparatorImpl()) .reverse(true) .build(); final Entity smallEntity = new Entity.Builder().group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, new IntegerWrapper(1)) .build(); final Entity largeEntity = new Entity.Builder().group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, new IntegerWrapper(2)) .build(); // When final int result = comparator.compare(smallEntity, largeEntity); // Then assertThat(result, greaterThan(0)); }
@Test public void shouldNotBeEqualOnSameElementWithDifferentPropertyWithGroupBy() { // Given final Entity testEntity1 = new Entity.Builder() .group(TestGroups.ENTITY_3) .vertex("vertex") .property(TestPropertyNames.SET, CollectionUtil.treeSet("3")) .property(TestPropertyNames.COUNT, 3L) .build(); final Entity testEntity2 = new Entity.Builder() .group(TestGroups.ENTITY_3) .vertex("vertex") .property(TestPropertyNames.SET, CollectionUtil.treeSet("3")) .property(TestPropertyNames.COUNT, 5L) .build(); ElementJoinComparator elementJoinComparator = new ElementJoinComparator("count"); // When / Then assertFalse(elementJoinComparator.test(testEntity1, testEntity2)); } }
@Test public void shouldGenerateAnEmptyMap() { // Given final Object vertex = "source vertex"; final String prop1 = "property 1"; final int count = 10; final Element element = new Entity.Builder() .group(TestGroups.ENTITY) .vertex(vertex) .property(TestPropertyNames.PROP_1, prop1) .property(TestPropertyNames.COUNT, count) .build(); final MapGenerator generator = new MapGenerator(); // When final Map<String, Object> map = generator._apply(element); // Then final Map<String, Object> expectedMap = new LinkedHashMap<>(); assertEquals(expectedMap, map); } }
@Test public void shouldBuildEntity() { // Given final String vertex = "vertex1"; final String propValue = "propValue"; // When final Entity entity = new Entity.Builder() .group(TestGroups.ENTITY) .vertex(vertex) .property(TestPropertyNames.STRING, propValue) .build(); // Then assertEquals(TestGroups.ENTITY, entity.getGroup()); assertEquals(vertex, entity.getVertex()); assertEquals(propValue, entity.getProperty(TestPropertyNames.STRING)); }
@Test public void shouldCompareWhenSecondElementHasMissingProperties() { // Given final ElementPropertyComparator comparator = new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY) .property(TestPropertyNames.PROP_2) .comparator(new ComparatorImpl()) .build(); final Entity smallEntity = new Entity.Builder().group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_2, new IntegerWrapper(1)) .build(); final Entity largeEntity = new Entity.Builder().group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, new IntegerWrapper(2)) .build(); // When final int result = comparator.compare(smallEntity, largeEntity); // Then assertThat(result, lessThan(0)); }
@Test public void shouldBeEqualOnSameElementWithDifferentPropertyWhenNoGroupBys() { // Given final Entity testEntity1 = new Entity.Builder() .group(TestGroups.ENTITY_3) .vertex("vertex") .property(TestPropertyNames.SET, CollectionUtil.treeSet("3")) .property(TestPropertyNames.COUNT, 3L) .build(); final Entity testEntity2 = new Entity.Builder() .group(TestGroups.ENTITY_3) .vertex("vertex") .property(TestPropertyNames.SET, CollectionUtil.treeSet("3")) .property(TestPropertyNames.COUNT, 5L) .build(); ElementJoinComparator elementJoinComparator = new ElementJoinComparator(); // When / Then assertTrue(elementJoinComparator.test(testEntity1, testEntity2)); }
@Test public void shouldCompareWithNoProvidedComparatorInstance() { // Given final ElementPropertyComparator comparator = new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1) .build(); final Entity smallEntity = new Entity.Builder().group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, 1) .build(); final Entity largeEntity = new Entity.Builder().group(TestGroups.ENTITY) .property(TestPropertyNames.PROP_1, 2) .build(); // When final int result = comparator.compare(smallEntity, largeEntity); // Then assertThat(result, lessThan(0)); }
@Test public void shouldBeEqualOnSameElement() { // Given final Entity testEntity1 = new Entity.Builder() .group(TestGroups.ENTITY_3) .vertex("vertex") .property(TestPropertyNames.SET, CollectionUtil.treeSet("3")) .property(TestPropertyNames.COUNT, 3L) .build(); final Entity testEntity2 = testEntity1.shallowClone(); ElementJoinComparator elementJoinComparator = new ElementJoinComparator(); // When / Then assertTrue(elementJoinComparator.test(testEntity1, testEntity2)); }