AbstractExhaustibleResource implements ResourceType { @Override public Resource aggregate(Resource resourceA, Resource resourceB) { checkResource(resourceA); checkResource(resourceB); if (resourceA.getAmount() == -1 || resourceB.getAmount() == -1) { return new ResourceImpl(getId(), -1, getDefaultUnit()); } return new ResourceImpl( getId(), resourceA.getAmount() + resourceB.getAmount(), getDefaultUnit()); }  @Override Resource aggregate(Resource resourceA, Resource resourceB); @Override Resource deduct(Resource total, Resource deduction);  }
@Test public void shouldFindSumResourcesAmountsOnResourcesAggregation() throws Exception { final Resource aggregate = resourceType.aggregate( new ResourceImpl(TestResourceType.ID, 1000, TestResourceType.UNIT), new ResourceImpl(TestResourceType.ID, 500, TestResourceType.UNIT)); assertEquals(aggregate.getType(), TestResourceType.ID); assertEquals(aggregate.getAmount(), 1500); assertEquals(aggregate.getUnit(), TestResourceType.UNIT); }
@Test public void shouldReturnResourceWithMinusOneAmountWhenFirstResourceHasMinusOneAmountOnResourcesDeduction() throws Exception { final Resource aggregate = resourceType.aggregate( new ResourceImpl(TestResourceType.ID, -1, TestResourceType.UNIT), new ResourceImpl(TestResourceType.ID, 500, TestResourceType.UNIT)); assertEquals(aggregate.getType(), TestResourceType.ID); assertEquals(aggregate.getAmount(), -1); assertEquals(aggregate.getUnit(), TestResourceType.UNIT); }
@Test public void shouldReturnResourceWithMinusOneAmountWhenSecondResourceHasMinusOneAmountOnResourcesDeduction() throws Exception { final Resource aggregate = resourceType.aggregate( new ResourceImpl(TestResourceType.ID, 2000, TestResourceType.UNIT), new ResourceImpl(TestResourceType.ID, -1, TestResourceType.UNIT)); assertEquals(aggregate.getType(), TestResourceType.ID); assertEquals(aggregate.getAmount(), -1); assertEquals(aggregate.getUnit(), TestResourceType.UNIT); }
@Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "resources") public void shouldThrowIllegalArgumentExceptionWhenOneOfResourcesHasUnsupportedTypeOrUnitOnResourcesAggregation( ResourceImpl resourceA, ResourceImpl resourceB) { resourceType.aggregate(resourceA, resourceB); }
@Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "resources") public void shouldThrowIllegalArgumentExceptionWhenOneOfResourcesHasUnsupportedTypeOrUnitOnResourcesDeduction( ResourceImpl resourceA, ResourceImpl resourceB) { resourceType.aggregate(resourceA, resourceB); }