MinHandler implements OutputOperationHandler<Min, Element> { @Override public Element doOperation(final Min operation, final Context context, final Store store) throws OperationException { if (null == operation.getInput() || null == operation.getComparators() || operation.getComparators().isEmpty()) { return null; } try { return getMin(operation.getInput(), operation); } finally { CloseableUtil.close(operation); } }  @Override Element doOperation(final Min operation, final Context context, final Store store);  }
@Test public void shouldFindMinBasedOnProperty() throws OperationException { final Entity entity1 = makeEntity("property", 1); final Entity entity2 = makeEntity("property", 2); final Entity entity3 = makeEntity("property", 3); final Entity entity4 = makeEntity("property", 1); final List<Entity> input = Lists.newArrayList(entity1, entity2, entity3, entity4); final Min min = new Min.Builder().input(input) .comparators(new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY) .property("property") .build()) .build(); final MinHandler handler = new MinHandler(); final Element result = handler.doOperation(min, null, null); assertTrue(result instanceof Entity); assertEquals(1, result.getProperty("property")); }
@Test public void shouldFindMinBasedOnMultipleProperties() throws OperationException { final Entity entity1 = makeEntity(1, 1); final Entity entity2 = makeEntity(1, 2); final Entity entity3 = makeEntity(2, 2); final Entity entity4 = makeEntity(2, 1); final List<Entity> input = Lists.newArrayList(entity1, entity2, entity3, entity4); final Min min = new Min.Builder().input(input) .comparators(new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY) .property("property1") .build(), new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY) .property("property2") .build()) .build(); final MinHandler handler = new MinHandler(); final Element result = handler.doOperation(min, null, null); assertSame(entity1, result); }
@Test public void shouldFindMinBasedOnPropertyWithMissingProperty() throws OperationException { final Entity entity1 = makeEntity("property1", 1); final Entity entity2 = makeEntity("property1", 2); final Entity entity3 = makeEntity("property1", 3); final Entity entity4 = makeEntity("property2", 1); final Entity entity5 = makeEntity("property2", 2); final List<Entity> input = Lists.newArrayList(entity1, entity2, entity3, entity4, entity5); final Min min1 = new Min.Builder().input(input) .comparators(new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY) .property("property1") .build()) .build(); final Min min2 = new Min.Builder().input(input) .comparators(new ElementPropertyComparator.Builder() .groups(TestGroups.ENTITY) .property("property2") .build()) .build(); final MinHandler handler = new MinHandler(); final Element result1 = handler.doOperation(min1, null, null); final Element result2 = handler.doOperation(min2, null, null); assertTrue(result1 instanceof Entity); assertTrue(result2 instanceof Entity); assertEquals(1, result1.getProperty("property1")); assertEquals(1, result2.getProperty("property2")); }
@Test public void shouldFindMinBasedOnElement() throws OperationException { final Entity entity1 = makeEntity(1, 1); final Entity entity2 = makeEntity(2, 2); final Entity entity3 = makeEntity(3, 3); final Entity entity4 = makeEntity(4, 4); final List<Entity> input = Lists.newArrayList(entity1, entity2, entity3, entity4); final Min min = new Min.Builder().input(input) .comparators(new SimpleElementComparator()) .build(); final MinHandler handler = new MinHandler(); final Element result = handler.doOperation(min, null, null); assertTrue(result instanceof Entity); assertEquals(1, result.getProperty("property1")); assertEquals(1, result.getProperty("property2")); }
@Test public void shouldReturnNullIfOperationInputIsNull() throws OperationException { final Min min = new Min.Builder().build(); final MinHandler handler = new MinHandler(); final Element result = handler.doOperation(min, null, null); assertNull(result); }
@Test public void shouldReturnNullIfBothComparatorsAreNull() throws OperationException { final List<Entity> input = Lists.newArrayList(); final Min min = new Min.Builder().input(input) .build(); final MinHandler handler = new MinHandler(); final Element result = handler.doOperation(min, null, null); assertNull(result); }