Added tests for layers pattern
This commit is contained in:
parent
a0151826ad
commit
5948a82cf2
@ -32,5 +32,10 @@
|
|||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
@ -30,9 +30,9 @@ public class CakeBakingServiceImpl implements CakeBakingService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException {
|
public void bakeNewCake(CakeInfo cakeInfo) throws CakeBakingException {
|
||||||
List<CakeToppingInfo> allToppings = getAvailableToppings();
|
List<CakeTopping> allToppings = getAvailableToppingEntities();
|
||||||
List<CakeToppingInfo> matchingToppings =
|
List<CakeTopping> matchingToppings =
|
||||||
allToppings.stream().filter((t) -> t.name.equals(cakeInfo.cakeToppingInfo.name))
|
allToppings.stream().filter((t) -> t.getName().equals(cakeInfo.cakeToppingInfo.name))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
if (matchingToppings.isEmpty()) {
|
if (matchingToppings.isEmpty()) {
|
||||||
throw new CakeBakingException(String.format("Topping %s is not available",
|
throw new CakeBakingException(String.format("Topping %s is not available",
|
||||||
@ -50,7 +50,7 @@ public class CakeBakingServiceImpl implements CakeBakingService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
CakeToppingDao toppingBean = context.getBean(CakeToppingDao.class);
|
CakeToppingDao toppingBean = context.getBean(CakeToppingDao.class);
|
||||||
CakeTopping topping = toppingBean.findOne(matchingToppings.iterator().next().id.get());
|
CakeTopping topping = toppingBean.findOne(matchingToppings.iterator().next().getId());
|
||||||
CakeDao cakeBean = context.getBean(CakeDao.class);
|
CakeDao cakeBean = context.getBean(CakeDao.class);
|
||||||
Cake cake = new Cake();
|
Cake cake = new Cake();
|
||||||
cake.setTopping(topping);
|
cake.setTopping(topping);
|
||||||
|
@ -34,7 +34,7 @@ public class CakeInfo {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("CakeInfo id=%d topping=%s layers=%s totalCalories=%d", id.get(),
|
return String.format("CakeInfo id=%d topping=%s layers=%s totalCalories=%d", id.orElse(-1L),
|
||||||
cakeToppingInfo, cakeLayerInfos, calculateTotalCalories());
|
cakeToppingInfo, cakeLayerInfos, calculateTotalCalories());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,6 @@ public class CakeLayerInfo {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("CakeLayerInfo id=%d name=%s calories=%d", id.get(), name, calories);
|
return String.format("CakeLayerInfo id=%d name=%s calories=%d", id.orElse(-1L), name, calories);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,7 +58,7 @@ public class CakeTopping {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("id=%s name=%s calories=%d", name, calories);
|
return String.format("id=%s name=%s calories=%d", id, name, calories);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Cake getCake() {
|
public Cake getCake() {
|
||||||
|
@ -27,6 +27,6 @@ public class CakeToppingInfo {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("CakeToppingInfo id=%d name=%s calories=%d", id.get(), name, calories);
|
return String.format("CakeToppingInfo id=%d name=%s calories=%d", id.orElse(-1L), name, calories);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
package com.iluwatar.layers;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 12/15/15 - 7:57 PM
|
||||||
|
*
|
||||||
|
* @author Jeroen Meulemeester
|
||||||
|
*/
|
||||||
|
public class CakeBakingExceptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConstructor() throws Exception {
|
||||||
|
final CakeBakingException exception = new CakeBakingException();
|
||||||
|
assertNull(exception.getMessage());
|
||||||
|
assertNull(exception.getCause());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConstructorWithMessage() throws Exception {
|
||||||
|
final String expectedMessage = "message";
|
||||||
|
final CakeBakingException exception = new CakeBakingException(expectedMessage);
|
||||||
|
assertEquals(expectedMessage, exception.getMessage());
|
||||||
|
assertNull(exception.getCause());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,159 @@
|
|||||||
|
package com.iluwatar.layers;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 12/15/15 - 9:55 PM
|
||||||
|
*
|
||||||
|
* @author Jeroen Meulemeester
|
||||||
|
*/
|
||||||
|
public class CakeBakingServiceImplTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLayers() throws CakeBakingException {
|
||||||
|
final CakeBakingServiceImpl service = new CakeBakingServiceImpl();
|
||||||
|
|
||||||
|
final List<CakeLayerInfo> initialLayers = service.getAvailableLayers();
|
||||||
|
assertNotNull(initialLayers);
|
||||||
|
assertTrue(initialLayers.isEmpty());
|
||||||
|
|
||||||
|
service.saveNewLayer(new CakeLayerInfo("Layer1", 1000));
|
||||||
|
service.saveNewLayer(new CakeLayerInfo("Layer2", 2000));
|
||||||
|
|
||||||
|
final List<CakeLayerInfo> availableLayers = service.getAvailableLayers();
|
||||||
|
assertNotNull(availableLayers);
|
||||||
|
assertEquals(2, availableLayers.size());
|
||||||
|
for (final CakeLayerInfo layer : availableLayers) {
|
||||||
|
assertNotNull(layer.id);
|
||||||
|
assertNotNull(layer.name);
|
||||||
|
assertNotNull(layer.toString());
|
||||||
|
assertTrue(layer.calories > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToppings() throws CakeBakingException {
|
||||||
|
final CakeBakingServiceImpl service = new CakeBakingServiceImpl();
|
||||||
|
|
||||||
|
final List<CakeToppingInfo> initialToppings = service.getAvailableToppings();
|
||||||
|
assertNotNull(initialToppings);
|
||||||
|
assertTrue(initialToppings.isEmpty());
|
||||||
|
|
||||||
|
service.saveNewTopping(new CakeToppingInfo("Topping1", 1000));
|
||||||
|
service.saveNewTopping(new CakeToppingInfo("Topping2", 2000));
|
||||||
|
|
||||||
|
final List<CakeToppingInfo> availableToppings = service.getAvailableToppings();
|
||||||
|
assertNotNull(availableToppings);
|
||||||
|
assertEquals(2, availableToppings.size());
|
||||||
|
for (final CakeToppingInfo topping : availableToppings) {
|
||||||
|
assertNotNull(topping.id);
|
||||||
|
assertNotNull(topping.name);
|
||||||
|
assertNotNull(topping.toString());
|
||||||
|
assertTrue(topping.calories > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBakeCakes() throws CakeBakingException {
|
||||||
|
final CakeBakingServiceImpl service = new CakeBakingServiceImpl();
|
||||||
|
|
||||||
|
final List<CakeInfo> initialCakes = service.getAllCakes();
|
||||||
|
assertNotNull(initialCakes);
|
||||||
|
assertTrue(initialCakes.isEmpty());
|
||||||
|
|
||||||
|
final CakeToppingInfo topping1 = new CakeToppingInfo("Topping1", 1000);
|
||||||
|
final CakeToppingInfo topping2 = new CakeToppingInfo("Topping2", 2000);
|
||||||
|
service.saveNewTopping(topping1);
|
||||||
|
service.saveNewTopping(topping2);
|
||||||
|
|
||||||
|
final CakeLayerInfo layer1 = new CakeLayerInfo("Layer1", 1000);
|
||||||
|
final CakeLayerInfo layer2 = new CakeLayerInfo("Layer2", 2000);
|
||||||
|
final CakeLayerInfo layer3 = new CakeLayerInfo("Layer3", 2000);
|
||||||
|
service.saveNewLayer(layer1);
|
||||||
|
service.saveNewLayer(layer2);
|
||||||
|
service.saveNewLayer(layer3);
|
||||||
|
|
||||||
|
service.bakeNewCake(new CakeInfo(topping1, Arrays.asList(layer1, layer2)));
|
||||||
|
service.bakeNewCake(new CakeInfo(topping2, Collections.singletonList(layer3)));
|
||||||
|
|
||||||
|
final List<CakeInfo> allCakes = service.getAllCakes();
|
||||||
|
assertNotNull(allCakes);
|
||||||
|
assertEquals(2, allCakes.size());
|
||||||
|
for (final CakeInfo cakeInfo : allCakes) {
|
||||||
|
assertNotNull(cakeInfo.id);
|
||||||
|
assertNotNull(cakeInfo.cakeToppingInfo);
|
||||||
|
assertNotNull(cakeInfo.cakeLayerInfos);
|
||||||
|
assertNotNull(cakeInfo.toString());
|
||||||
|
assertFalse(cakeInfo.cakeLayerInfos.isEmpty());
|
||||||
|
assertTrue(cakeInfo.calculateTotalCalories() > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = CakeBakingException.class)
|
||||||
|
public void testBakeCakeMissingTopping() throws CakeBakingException {
|
||||||
|
final CakeBakingServiceImpl service = new CakeBakingServiceImpl();
|
||||||
|
|
||||||
|
final CakeLayerInfo layer1 = new CakeLayerInfo("Layer1", 1000);
|
||||||
|
final CakeLayerInfo layer2 = new CakeLayerInfo("Layer2", 2000);
|
||||||
|
service.saveNewLayer(layer1);
|
||||||
|
service.saveNewLayer(layer2);
|
||||||
|
|
||||||
|
final CakeToppingInfo missingTopping = new CakeToppingInfo("Topping1", 1000);
|
||||||
|
service.bakeNewCake(new CakeInfo(missingTopping, Arrays.asList(layer1, layer2)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = CakeBakingException.class)
|
||||||
|
public void testBakeCakeMissingLayer() throws CakeBakingException {
|
||||||
|
final CakeBakingServiceImpl service = new CakeBakingServiceImpl();
|
||||||
|
|
||||||
|
final List<CakeInfo> initialCakes = service.getAllCakes();
|
||||||
|
assertNotNull(initialCakes);
|
||||||
|
assertTrue(initialCakes.isEmpty());
|
||||||
|
|
||||||
|
final CakeToppingInfo topping1 = new CakeToppingInfo("Topping1", 1000);
|
||||||
|
service.saveNewTopping(topping1);
|
||||||
|
|
||||||
|
final CakeLayerInfo layer1 = new CakeLayerInfo("Layer1", 1000);
|
||||||
|
service.saveNewLayer(layer1);
|
||||||
|
|
||||||
|
final CakeLayerInfo missingLayer = new CakeLayerInfo("Layer2", 2000);
|
||||||
|
service.bakeNewCake(new CakeInfo(topping1, Arrays.asList(layer1, missingLayer)));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = CakeBakingException.class)
|
||||||
|
public void testBakeCakesUsedLayer() throws CakeBakingException {
|
||||||
|
final CakeBakingServiceImpl service = new CakeBakingServiceImpl();
|
||||||
|
|
||||||
|
final List<CakeInfo> initialCakes = service.getAllCakes();
|
||||||
|
assertNotNull(initialCakes);
|
||||||
|
assertTrue(initialCakes.isEmpty());
|
||||||
|
|
||||||
|
final CakeToppingInfo topping1 = new CakeToppingInfo("Topping1", 1000);
|
||||||
|
final CakeToppingInfo topping2 = new CakeToppingInfo("Topping2", 2000);
|
||||||
|
service.saveNewTopping(topping1);
|
||||||
|
service.saveNewTopping(topping2);
|
||||||
|
|
||||||
|
final CakeLayerInfo layer1 = new CakeLayerInfo("Layer1", 1000);
|
||||||
|
final CakeLayerInfo layer2 = new CakeLayerInfo("Layer2", 2000);
|
||||||
|
service.saveNewLayer(layer1);
|
||||||
|
service.saveNewLayer(layer2);
|
||||||
|
|
||||||
|
service.bakeNewCake(new CakeInfo(topping1, Arrays.asList(layer1, layer2)));
|
||||||
|
service.bakeNewCake(new CakeInfo(topping2, Collections.singletonList(layer2)));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
97
layers/src/test/java/com/iluwatar/layers/CakeTest.java
Normal file
97
layers/src/test/java/com/iluwatar/layers/CakeTest.java
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
package com.iluwatar.layers;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 12/15/15 - 8:02 PM
|
||||||
|
*
|
||||||
|
* @author Jeroen Meulemeester
|
||||||
|
*/
|
||||||
|
public class CakeTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetId() {
|
||||||
|
final Cake cake = new Cake();
|
||||||
|
assertNull(cake.getId());
|
||||||
|
|
||||||
|
final Long expectedId = Long.valueOf(1234L);
|
||||||
|
cake.setId(expectedId);
|
||||||
|
assertEquals(expectedId, cake.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetTopping() {
|
||||||
|
final Cake cake = new Cake();
|
||||||
|
assertNull(cake.getTopping());
|
||||||
|
|
||||||
|
final CakeTopping expectedTopping = new CakeTopping("DummyTopping", 1000);
|
||||||
|
cake.setTopping(expectedTopping);
|
||||||
|
assertEquals(expectedTopping, cake.getTopping());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetLayers() {
|
||||||
|
final Cake cake = new Cake();
|
||||||
|
assertNotNull(cake.getLayers());
|
||||||
|
assertTrue(cake.getLayers().isEmpty());
|
||||||
|
|
||||||
|
final Set<CakeLayer> expectedLayers = new HashSet<>();
|
||||||
|
expectedLayers.add(new CakeLayer("layer1", 1000));
|
||||||
|
expectedLayers.add(new CakeLayer("layer2", 2000));
|
||||||
|
expectedLayers.add(new CakeLayer("layer3", 3000));
|
||||||
|
|
||||||
|
cake.setLayers(expectedLayers);
|
||||||
|
assertEquals(expectedLayers, cake.getLayers());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAddLayer() {
|
||||||
|
final Cake cake = new Cake();
|
||||||
|
assertNotNull(cake.getLayers());
|
||||||
|
assertTrue(cake.getLayers().isEmpty());
|
||||||
|
|
||||||
|
final Set<CakeLayer> initialLayers = new HashSet<>();
|
||||||
|
initialLayers.add(new CakeLayer("layer1", 1000));
|
||||||
|
initialLayers.add(new CakeLayer("layer2", 2000));
|
||||||
|
|
||||||
|
cake.setLayers(initialLayers);
|
||||||
|
assertEquals(initialLayers, cake.getLayers());
|
||||||
|
|
||||||
|
final CakeLayer newLayer = new CakeLayer("layer3", 3000);
|
||||||
|
cake.addLayer(newLayer);
|
||||||
|
|
||||||
|
final Set<CakeLayer> expectedLayers = new HashSet<>();
|
||||||
|
expectedLayers.addAll(initialLayers);
|
||||||
|
expectedLayers.addAll(initialLayers);
|
||||||
|
expectedLayers.add(newLayer);
|
||||||
|
assertEquals(expectedLayers, cake.getLayers());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToString() {
|
||||||
|
final CakeTopping topping = new CakeTopping("topping", 20);
|
||||||
|
topping.setId(2345L);
|
||||||
|
|
||||||
|
final CakeLayer layer = new CakeLayer("layer", 100);
|
||||||
|
layer.setId(3456L);
|
||||||
|
|
||||||
|
final Cake cake = new Cake();
|
||||||
|
cake.setId(1234L);
|
||||||
|
cake.setTopping(topping);
|
||||||
|
cake.addLayer(layer);
|
||||||
|
|
||||||
|
final String expected = "id=1234 topping=id=2345 name=topping calories=20 " +
|
||||||
|
"layers=[id=3456 name=layer calories=100]";
|
||||||
|
assertEquals(expected, cake.toString());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
package com.iluwatar.layers;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 12/15/15 - 10:04 PM
|
||||||
|
*
|
||||||
|
* @author Jeroen Meulemeester
|
||||||
|
*/
|
||||||
|
public class CakeViewImplTest extends StdOutTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verify if the cake view renders the expected result
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testRender() {
|
||||||
|
|
||||||
|
final List<CakeLayerInfo> layers = new ArrayList<>();
|
||||||
|
layers.add(new CakeLayerInfo("layer1", 1000));
|
||||||
|
layers.add(new CakeLayerInfo("layer2", 2000));
|
||||||
|
layers.add(new CakeLayerInfo("layer3", 3000));
|
||||||
|
|
||||||
|
final List<CakeInfo> cakes = new ArrayList<>();
|
||||||
|
final CakeInfo cake = new CakeInfo(new CakeToppingInfo("topping", 1000), layers);
|
||||||
|
cakes.add(cake);
|
||||||
|
|
||||||
|
final CakeBakingService bakingService = mock(CakeBakingService.class);
|
||||||
|
when(bakingService.getAllCakes()).thenReturn(cakes);
|
||||||
|
|
||||||
|
final CakeViewImpl cakeView = new CakeViewImpl(bakingService);
|
||||||
|
|
||||||
|
verifyZeroInteractions(getStdOutMock());
|
||||||
|
|
||||||
|
cakeView.render();
|
||||||
|
verify(getStdOutMock(), times(1)).println(cake);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
54
layers/src/test/java/com/iluwatar/layers/StdOutTest.java
Normal file
54
layers/src/test/java/com/iluwatar/layers/StdOutTest.java
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package com.iluwatar.layers;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
|
||||||
|
import java.io.PrintStream;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date: 12/10/15 - 8:37 PM
|
||||||
|
*
|
||||||
|
* @author Jeroen Meulemeester
|
||||||
|
*/
|
||||||
|
public abstract class StdOutTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The mocked standard out {@link PrintStream}, required since the actions of the views don't have
|
||||||
|
* any influence on any other accessible objects, except for writing to std-out using {@link
|
||||||
|
* System#out}
|
||||||
|
*/
|
||||||
|
private final PrintStream stdOutMock = mock(PrintStream.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Keep the original std-out so it can be restored after the test
|
||||||
|
*/
|
||||||
|
private final PrintStream stdOutOrig = System.out;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
|
||||||
|
*/
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
System.setOut(this.stdOutMock);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
|
||||||
|
*/
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
System.setOut(this.stdOutOrig);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the mocked stdOut {@link PrintStream}
|
||||||
|
*
|
||||||
|
* @return The stdOut print stream mock, renewed before each test
|
||||||
|
*/
|
||||||
|
final PrintStream getStdOutMock() {
|
||||||
|
return this.stdOutMock;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user