瀏覽代碼

Get 100% coverage on model classes.

Sam Jaffe 5 年之前
父節點
當前提交
755a6289ff

+ 9 - 3
src/main/lombok/org/leumasjaffe/recipe/model/CompoundRecipeComponent.java

@@ -1,10 +1,10 @@
 package org.leumasjaffe.recipe.model;
 
 import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.stream.Stream;
 
-import org.leumasjaffe.recipe.util.Collator;
-
 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
 
 @JsonIgnoreProperties({"duration", "ingredients", "components", "ingredientsAsStream"})
@@ -20,6 +20,12 @@ interface CompoundRecipeComponent extends RecipeComponent {
 	
 	@Override
 	default Collection<Ingredient> getIngredients() {
-		return Collator.collateBy(getIngredientsAsStream(), Ingredient::key, Ingredient::plus);
+		final Map<String, Ingredient> map = new HashMap<>();
+		getIngredientsAsStream().forEach(value -> {
+        	final String key = value.key();
+            map.computeIfPresent(key, (k, v) -> value.plus(v));
+            map.computeIfAbsent(key, k -> value);
+        });
+        return map.values();
 	}
 }

+ 3 - 5
src/main/lombok/org/leumasjaffe/recipe/model/Product.java

@@ -4,7 +4,6 @@ import java.util.List;
 import java.util.stream.Stream;
 
 import org.leumasjaffe.observer.Observable;
-import org.leumasjaffe.recipe.util.Collator;
 
 import lombok.Data;
 import lombok.EqualsAndHashCode;
@@ -16,13 +15,12 @@ public class Product extends Observable.Instance implements CompoundRecipeCompon
 
 	@Override
 	public Stream<Ingredient> getIngredientsAsStream() {
-		return Collator.collateBy(cards.stream().flatMap(Card::getIngredientsAsStream)
-				.map(i -> new Ingredient(i.getName(), "", i.getAmount())),
-				Ingredient::key, Ingredient::plus).stream();
+		return getComponents().flatMap(Card::getIngredientsAsStream)
+				.map(i -> new Ingredient(i.getName(), "", i.getAmount()));
 	}
 
 	@Override
-	public Stream<? extends RecipeComponent> getComponents() {
+	public Stream<Card> getComponents() {
 		return cards.stream();
 	}	
 }

+ 2 - 2
src/main/lombok/org/leumasjaffe/recipe/model/Recipe.java

@@ -19,12 +19,12 @@ public class Recipe implements CompoundRecipeComponent {
 	List<Product> products = new ArrayList<>();
 	
 	@Override
-	public Stream<? extends RecipeComponent> getComponents() {
+	public Stream<Product> getComponents() {
 		return products.stream();
 	}
 	
 	@Override
 	public Stream<Ingredient> getIngredientsAsStream() {
-		return products.stream().flatMap(Product::getIngredientsAsStream);
+		return getComponents().flatMap(Product::getIngredientsAsStream);
 	}
 }

+ 0 - 24
src/main/lombok/org/leumasjaffe/recipe/util/Collator.java

@@ -1,24 +0,0 @@
-package org.leumasjaffe.recipe.util;
-
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.function.BinaryOperator;
-import java.util.function.Function;
-import java.util.stream.Stream;
-
-import lombok.experimental.UtilityClass;
-
-@UtilityClass
-public class Collator {
-	public <T> Collection<T> collateBy(final Stream<T> stream, final Function<T, String> getKey,
-			final BinaryOperator<T> folder) {
-		final Map<String, T> map = new HashMap<>();
-        stream.forEach(value -> {
-        	final String key = getKey.apply(value);
-            map.computeIfPresent(key, (k, v) -> folder.apply(v, value));
-            map.computeIfAbsent(key, k -> value);
-        });
-        return map.values();
-	}
-}

+ 96 - 0
src/test/java/org/leumasjaffe/recipe/model/CardTest.java

@@ -0,0 +1,96 @@
+package org.leumasjaffe.recipe.model;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.hamcrest.MatcherAssert.*;
+import static org.hamcrest.core.Is.*;
+import static org.hamcrest.core.IsCollectionContaining.*;
+
+import java.util.Arrays;
+import java.util.Optional;
+
+import org.junit.jupiter.api.Test;
+
+class CardTest {
+	private static final Amount _1g = new Amount("1 g");
+	private static final Duration dur = new Duration(Duration.Display.SECONDS, false, 10, 20);
+
+	@Test
+	void cannotAddNullPreparation() {
+		final Card card = new Card();
+		assertThrows(NullPointerException.class, () -> card.setPreparation(null));
+	}
+	
+	@Test
+	void testDurationIsZeroByDefault() {
+		final Card card = new Card();
+		assertEquals(Duration.ZERO, card.getDuration());
+	}
+	
+	@Test
+	void testSumsTogetherStepDurations() {
+		final Card card = new Card();
+		final Step step = new Step();
+		step.setDuration(dur);
+		card.setCooking(Arrays.asList(step, step));
+		assertEquals(new Duration(Duration.Display.SECONDS, false, 20, 40),
+				card.getDuration());
+	}
+	
+	@Test
+	void testDoesNotAddPrepDurationIfPresent() {
+		final Card card = new Card();
+		final Preparation prep = new Preparation();
+		prep.setDuration(dur);
+		card.setPreparation(prep);
+		assertEquals(Duration.ZERO, card.getDuration());
+	}
+	
+	@Test
+	void testDoesNotAddRestDurationIfPresent() {
+		final Card card = new Card();
+		final Rest rest = new Rest();
+		rest.setWhere(Rest.Where.REFRIGERATOR);
+		rest.setDuration(dur);
+		card.setRest(Optional.of(rest));
+		assertEquals(Duration.ZERO, card.getDuration());
+	}
+
+	@Test
+	void testMergesLikeIngredients() {
+		final Card card = new Card();
+		final Step step = new Step();
+		step.setIngredients(Arrays.asList(new Ingredient("TEST", "", _1g)));
+		card.setCooking(Arrays.asList(step, step));
+		// TODO Figure out why hamcrest-all isn't loading...
+		assertThat(card.getIngredients().size(), is(1));
+		assertThat(card.getIngredients(),
+				hasItem(new Ingredient("TEST", "", new Amount("2 g"))));
+	}
+
+	@Test
+	void testDoesNotMergeIngredientsWithDifferentPrep() {
+		final Card card = new Card();
+		final Step step = new Step();
+		step.setIngredients(Arrays.asList(
+				new Ingredient("TEST", "A", _1g),
+				new Ingredient("TEST", "B", _1g)));
+		card.setCooking(Arrays.asList(step));
+		// TODO Figure out why hamcrest-all isn't loading...
+		assertThat(card.getIngredients().size(), is(2));
+	}
+
+	@Test
+	void testPrepIngredientsAreCardIngredientsWithPrep() {
+		final Card card = new Card();
+		final Step step = new Step();
+		step.setIngredients(Arrays.asList(
+				new Ingredient("A", "", _1g),
+				new Ingredient("B", "TEST", _1g)));
+		card.setCooking(Arrays.asList(step));
+		card.setPreparation(new Preparation());
+		assertThat(card.getIngredients().size(), is(2));
+		final Preparation prep = card.getPreparation().get();
+		assertThat(prep.getIngredients().size(), is(1));
+		assertThat(prep.getIngredients(), hasItem(new Ingredient("B", "TEST", _1g)));
+	}
+}

+ 6 - 6
src/test/java/org/leumasjaffe/recipe/model/DurationTest.java

@@ -18,8 +18,8 @@ class DurationTest {
 		final Duration inSec = new Duration(Duration.Display.SECONDS, false, 10, 20);
 		final Duration inMin = new Duration(Duration.Display.MINUTES, false, 60, 120);
 		
-		assertEquals(inSec.plus(inMin).getDisplayAs(), Duration.Display.SECONDS);
-		assertEquals(inMin.plus(inSec).getDisplayAs(), Duration.Display.SECONDS);
+		assertEquals(Duration.Display.SECONDS, inSec.plus(inMin).getDisplayAs());
+		assertEquals(Duration.Display.SECONDS, inMin.plus(inSec).getDisplayAs());
 	}
 
 	@Test
@@ -27,8 +27,8 @@ class DurationTest {
 		final Duration inSec = new Duration(Duration.Display.SECONDS, true, 10, 20);
 		final Duration inMin = new Duration(Duration.Display.MINUTES, false, 60, 120);
 		
-		assertEquals(inSec.plus(inMin).isApproximate(), true);
-		assertEquals(inMin.plus(inSec).isApproximate(), true);
+		assertTrue(inSec.plus(inMin).isApproximate());
+		assertTrue(inMin.plus(inSec).isApproximate());
 	}
 	
 	@Test
@@ -66,7 +66,7 @@ class DurationTest {
 	void testUnitControlsOutputScale() {
 		final Duration inSec = new Duration(Duration.Display.SECONDS, false, 10, 20);
 		final Duration inMin = new Duration(Duration.Display.MINUTES, false, 10, 20);
-		assertEquals(inSec.toString(), "10 - 20 s");
-		assertEquals(inMin.toString(), "0 - 0 min");
+		assertEquals("10 - 20 s", inSec.toString());
+		assertEquals("0 - 0 min", inMin.toString());
 	}
 }

+ 29 - 0
src/test/java/org/leumasjaffe/recipe/model/ProductTest.java

@@ -0,0 +1,29 @@
+package org.leumasjaffe.recipe.model;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsCollectionContaining.hasItem;
+
+import java.util.Arrays;
+
+import org.junit.jupiter.api.Test;
+
+class ProductTest {
+	private static final Amount _1g = new Amount("1 g");
+
+	@Test
+	void testMergesIngredientsWithDifferentPrep() {
+		final Product prod = new Product();
+		final Card card = new Card();
+		final Step step = new Step();
+		step.setIngredients(Arrays.asList(
+				new Ingredient("TEST", "A", _1g),
+				new Ingredient("TEST", "B", _1g)));
+		card.setCooking(Arrays.asList(step));
+		prod.setCards(Arrays.asList(card));
+		// TODO Figure out why hamcrest-all isn't loading...
+		assertThat(prod.getIngredients().size(), is(1));
+		assertThat(prod.getIngredients(),
+				hasItem(new Ingredient("TEST", "", new Amount("2 g"))));
+	}
+}

+ 32 - 0
src/test/java/org/leumasjaffe/recipe/model/RecipeTest.java

@@ -0,0 +1,32 @@
+package org.leumasjaffe.recipe.model;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsCollectionContaining.hasItem;
+
+import java.util.Arrays;
+
+import org.junit.jupiter.api.Test;
+
+class RecipeTest {
+	private static final Amount _1g = new Amount("1 g");
+
+	@Test
+	void testMergesIngredientsWithDifferentPrep() {
+		final Recipe recipe = new Recipe();
+		final Product prod = new Product();
+		final Card card = new Card();
+		final Step step = new Step();
+		step.setIngredients(Arrays.asList(
+				new Ingredient("TEST", "A", _1g),
+				new Ingredient("TEST", "B", _1g)));
+		card.setCooking(Arrays.asList(step));
+		prod.setCards(Arrays.asList(card));
+		recipe.setProducts(Arrays.asList(prod, prod));
+		// TODO Figure out why hamcrest-all isn't loading...
+		assertThat(recipe.getIngredients().size(), is(1));
+		assertThat(recipe.getIngredients(),
+				hasItem(new Ingredient("TEST", "", new Amount("4 g"))));
+	}
+
+}

+ 23 - 0
src/test/java/org/leumasjaffe/recipe/model/StepTest.java

@@ -0,0 +1,23 @@
+package org.leumasjaffe.recipe.model;
+
+import static org.hamcrest.MatcherAssert.*;
+import static org.hamcrest.core.Is.*;
+
+import java.util.Arrays;
+
+import org.junit.jupiter.api.Test;
+
+class StepTest {
+	private static final Amount _1g = new Amount("1 g");
+
+	@Test
+	void testDoesNotMergeLikeIngredients() {
+		final Step step = new Step();
+		step.setIngredients(Arrays.asList(
+				new Ingredient("TEST", "", _1g),
+				new Ingredient("TEST", "", _1g)));
+		// TODO Figure out why hamcrest-all isn't loading...
+		assertThat(step.getIngredients().size(), is(2));
+	}
+
+}