From 23cf63d052e614efc1de843b7481f79245bb6434 Mon Sep 17 00:00:00 2001 From: jbion Date: Sun, 29 Jan 2017 23:06:12 +0100 Subject: Add public/private production concept --- .../luxons/sevenwonders/game/boards/BoardTest.java | 1 + .../ProductionIncreaseSerializerTest.java | 77 ++++---- .../data/serializers/ProductionSerializerTest.java | 198 +++++++++++++++++++++ .../game/effects/ProductionIncreaseTest.java | 24 ++- 4 files changed, 253 insertions(+), 47 deletions(-) create mode 100644 backend/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionSerializerTest.java (limited to 'backend/src/test/java') diff --git a/backend/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java b/backend/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java index f9117146..5756b36a 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/game/boards/BoardTest.java @@ -62,6 +62,7 @@ public class BoardTest { Board board = new Board(TestUtils.createWonder(type), null, new Settings(5)); Resources resources = TestUtils.createResources(type); assertTrue(board.getProduction().contains(resources)); + assertTrue(board.getPublicProduction().contains(resources)); } @Theory diff --git a/backend/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializerTest.java b/backend/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializerTest.java index 17940361..f4b41628 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializerTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionIncreaseSerializerTest.java @@ -26,11 +26,12 @@ public class ProductionIncreaseSerializerTest { gson = new GsonBuilder().registerTypeAdapter(Resources.class, new ResourcesSerializer()) .registerTypeAdapter(ResourceType.class, new ResourceTypeSerializer()) .registerTypeAdapter(resourceTypeList, new ResourceTypesSerializer()) + .registerTypeAdapter(Production.class, new ProductionSerializer()) .registerTypeAdapter(ProductionIncrease.class, new ProductionIncreaseSerializer()) .create(); } - private static ProductionIncrease create(int wood, int stone, int clay) { + private static ProductionIncrease create(boolean sellable, int wood, int stone, int clay) { Production production = new Production(); if (wood > 0) { production.addFixedResource(ResourceType.WOOD, wood); @@ -43,14 +44,16 @@ public class ProductionIncreaseSerializerTest { } ProductionIncrease prodIncrease = new ProductionIncrease(); prodIncrease.setProduction(production); + prodIncrease.setSellable(sellable); return prodIncrease; } - private static ProductionIncrease createChoice(ResourceType... types) { + private static ProductionIncrease createChoice(boolean sellable, ResourceType... types) { Production production = new Production(); production.addChoice(types); ProductionIncrease prodIncrease = new ProductionIncrease(); prodIncrease.setProduction(production); + prodIncrease.setSellable(sellable); return prodIncrease; } @@ -67,62 +70,62 @@ public class ProductionIncreaseSerializerTest { @Test public void serialize_singleType() { - ProductionIncrease prodIncrease = create(1, 0, 0); + ProductionIncrease prodIncrease = create(true, 1, 0, 0); assertEquals("\"W\"", gson.toJson(prodIncrease, ProductionIncrease.class)); } - @Test - public void serialize_multipleTimesSameType() { - ProductionIncrease prodIncrease = create(3, 0, 0); - assertEquals("\"WWW\"", gson.toJson(prodIncrease, ProductionIncrease.class)); - } - @Test public void serialize_mixedTypes() { - ProductionIncrease prodIncrease = create(1, 1, 1); + ProductionIncrease prodIncrease = create(true, 1, 1, 1); assertEquals("\"WSC\"", gson.toJson(prodIncrease, ProductionIncrease.class)); } @Test - public void serialize_mixedTypesMultiple() { - ProductionIncrease prodIncrease = create(2, 1, 2); - assertEquals("\"WWSCC\"", gson.toJson(prodIncrease, ProductionIncrease.class)); + public void serialize_mixedTypes_notSellable() { + ProductionIncrease prodIncrease = create(false, 1, 1, 1); + assertEquals("\"(WSC)\"", gson.toJson(prodIncrease, ProductionIncrease.class)); } @Test public void serialize_choice2() { - ProductionIncrease prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY); + ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY); assertEquals("\"W/C\"", gson.toJson(prodIncrease, ProductionIncrease.class)); } @Test public void serialize_choice3() { - ProductionIncrease prodIncrease = createChoice(ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); + ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); assertEquals("\"W/O/C\"", gson.toJson(prodIncrease, ProductionIncrease.class)); } + @Test + public void serialize_choice3_notSellable() { + ProductionIncrease prodIncrease = createChoice(false, ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); + assertEquals("\"(W/O/C)\"", gson.toJson(prodIncrease, ProductionIncrease.class)); + } + @Test public void serialize_choice2_unordered() { - ProductionIncrease prodIncrease = createChoice(ResourceType.CLAY, ResourceType.WOOD); + ProductionIncrease prodIncrease = createChoice(true, ResourceType.CLAY, ResourceType.WOOD); assertEquals("\"W/C\"", gson.toJson(prodIncrease, ProductionIncrease.class)); } @Test public void serialize_choice3_unordered() { - ProductionIncrease prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE); + ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE); assertEquals("\"W/O/C\"", gson.toJson(prodIncrease, ProductionIncrease.class)); } @Test(expected = IllegalArgumentException.class) public void serialize_failIfMultipleChoices() { - ProductionIncrease prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY); + ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY); prodIncrease.getProduction().addChoice(ResourceType.ORE, ResourceType.GLASS); gson.toJson(prodIncrease, ProductionIncrease.class); } @Test(expected = IllegalArgumentException.class) public void serialize_failIfMixedFixedAndChoices() { - ProductionIncrease prodIncrease = create(1, 0, 0); + ProductionIncrease prodIncrease = create(true, 1, 0, 0); prodIncrease.getProduction().addChoice(ResourceType.WOOD, ResourceType.CLAY); gson.toJson(prodIncrease, ProductionIncrease.class); } @@ -150,50 +153,32 @@ public class ProductionIncreaseSerializerTest { @Test public void deserialize_singleType() { - ProductionIncrease prodIncrease = create(1, 0, 0); + ProductionIncrease prodIncrease = create(true, 1, 0, 0); assertEquals(prodIncrease, gson.fromJson("\"W\"", ProductionIncrease.class)); } @Test - public void deserialize_multipleTimesSameType() { - ProductionIncrease prodIncrease = create(3, 0, 0); - assertEquals(prodIncrease, gson.fromJson("\"WWW\"", ProductionIncrease.class)); + public void deserialize_multipleTimesSameType_notSellable() { + ProductionIncrease prodIncrease = create(false, 3, 0, 0); + assertEquals(prodIncrease, gson.fromJson("\"(WWW)\"", ProductionIncrease.class)); } @Test public void deserialize_mixedTypes() { - ProductionIncrease prodIncrease = create(1, 1, 1); + ProductionIncrease prodIncrease = create(true, 1, 1, 1); assertEquals(prodIncrease, gson.fromJson("\"WCS\"", ProductionIncrease.class)); } - @Test - public void deserialize_mixedTypes_unordered() { - ProductionIncrease prodIncrease = create(1, 3, 2); - assertEquals(prodIncrease, gson.fromJson("\"SCWCSS\"", ProductionIncrease.class)); - } - @Test public void deserialize_choice2() { - ProductionIncrease prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY); - assertEquals(prodIncrease, gson.fromJson("\"W/C\"", ProductionIncrease.class)); - } - - @Test - public void deserialize_choice3() { - ProductionIncrease prodIncrease = createChoice(ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); - assertEquals(prodIncrease, gson.fromJson("\"W/O/C\"", ProductionIncrease.class)); - } - - @Test - public void deserialize_choice2_unordered() { - ProductionIncrease prodIncrease = createChoice(ResourceType.CLAY, ResourceType.WOOD); + ProductionIncrease prodIncrease = createChoice(true, ResourceType.WOOD, ResourceType.CLAY); assertEquals(prodIncrease, gson.fromJson("\"W/C\"", ProductionIncrease.class)); } @Test - public void deserialize_choice3_unordered() { - ProductionIncrease prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE); - assertEquals(prodIncrease, gson.fromJson("\"W/O/C\"", ProductionIncrease.class)); + public void deserialize_choice3_notSellable() { + ProductionIncrease prodIncrease = createChoice(false, ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); + assertEquals(prodIncrease, gson.fromJson("\"(W/O/C)\"", ProductionIncrease.class)); } @Test(expected = IllegalArgumentException.class) diff --git a/backend/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionSerializerTest.java b/backend/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionSerializerTest.java new file mode 100644 index 00000000..34a06ce4 --- /dev/null +++ b/backend/src/test/java/org/luxons/sevenwonders/game/data/serializers/ProductionSerializerTest.java @@ -0,0 +1,198 @@ +package org.luxons.sevenwonders.game.data.serializers; + +import java.lang.reflect.Type; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.luxons.sevenwonders.game.resources.Production; +import org.luxons.sevenwonders.game.resources.ResourceType; +import org.luxons.sevenwonders.game.resources.Resources; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; +import static org.junit.Assert.*; + +public class ProductionSerializerTest { + + private Gson gson; + + @Before + public void setUp() { + Type resourceTypeList = new TypeToken>() {}.getType(); + gson = new GsonBuilder().registerTypeAdapter(Resources.class, new ResourcesSerializer()) + .registerTypeAdapter(ResourceType.class, new ResourceTypeSerializer()) + .registerTypeAdapter(resourceTypeList, new ResourceTypesSerializer()) + .registerTypeAdapter(Production.class, new ProductionSerializer()) + .create(); + } + + private static Production create(int wood, int stone, int clay) { + Production production = new Production(); + if (wood > 0) { + production.addFixedResource(ResourceType.WOOD, wood); + } + if (stone > 0) { + production.addFixedResource(ResourceType.STONE, stone); + } + if (clay > 0) { + production.addFixedResource(ResourceType.CLAY, clay); + } + return production; + } + + private static Production createChoice(ResourceType... types) { + Production production = new Production(); + production.addChoice(types); + return production; + } + + @Test + public void serialize_nullAsNull() { + assertEquals("null", gson.toJson(null, Production.class)); + } + + @Test + public void serialize_emptyProdIncreaseAsNull() { + Production prodIncrease = new Production(); + assertEquals("null", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_singleType() { + Production prodIncrease = create(1, 0, 0); + assertEquals("\"W\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_multipleTimesSameType() { + Production prodIncrease = create(3, 0, 0); + assertEquals("\"WWW\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_mixedTypes() { + Production prodIncrease = create(1, 1, 1); + assertEquals("\"WSC\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_mixedTypesMultiple() { + Production prodIncrease = create(2, 1, 2); + assertEquals("\"WWSCC\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_choice2() { + Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY); + assertEquals("\"W/C\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_choice3() { + Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); + assertEquals("\"W/O/C\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_choice2_unordered() { + Production prodIncrease = createChoice(ResourceType.CLAY, ResourceType.WOOD); + assertEquals("\"W/C\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test + public void serialize_choice3_unordered() { + Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE); + assertEquals("\"W/O/C\"", gson.toJson(prodIncrease, Production.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void serialize_failIfMultipleChoices() { + Production production = createChoice(ResourceType.WOOD, ResourceType.CLAY); + production.addChoice(ResourceType.ORE, ResourceType.GLASS); + gson.toJson(production, Production.class); + } + + @Test(expected = IllegalArgumentException.class) + public void serialize_failIfMixedFixedAndChoices() { + Production production = create(1, 0, 0); + production.addChoice(ResourceType.WOOD, ResourceType.CLAY); + gson.toJson(production, Production.class); + } + + @Test + public void deserialize_nullFromNull() { + assertNull(gson.fromJson("null", Production.class)); + } + + @Test + public void deserialize_emptyList() { + Production prodIncrease = new Production(); + assertEquals(prodIncrease, gson.fromJson("\"\"", Production.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void deserialize_failOnGarbageString() { + gson.fromJson("\"this is garbage\"", Production.class); + } + + @Test(expected = IllegalArgumentException.class) + public void deserialize_failOnGarbageStringWithSlashes() { + gson.fromJson("\"this/is/garbage\"", Production.class); + } + + @Test + public void deserialize_singleType() { + Production prodIncrease = create(1, 0, 0); + assertEquals(prodIncrease, gson.fromJson("\"W\"", Production.class)); + } + + @Test + public void deserialize_multipleTimesSameType() { + Production prodIncrease = create(3, 0, 0); + assertEquals(prodIncrease, gson.fromJson("\"WWW\"", Production.class)); + } + + @Test + public void deserialize_mixedTypes() { + Production prodIncrease = create(1, 1, 1); + assertEquals(prodIncrease, gson.fromJson("\"WCS\"", Production.class)); + } + + @Test + public void deserialize_mixedTypes_unordered() { + Production prodIncrease = create(1, 3, 2); + assertEquals(prodIncrease, gson.fromJson("\"SCWCSS\"", Production.class)); + } + + @Test + public void deserialize_choice2() { + Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY); + assertEquals(prodIncrease, gson.fromJson("\"W/C\"", Production.class)); + } + + @Test + public void deserialize_choice3() { + Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.ORE, ResourceType.CLAY); + assertEquals(prodIncrease, gson.fromJson("\"W/O/C\"", Production.class)); + } + + @Test + public void deserialize_choice2_unordered() { + Production prodIncrease = createChoice(ResourceType.CLAY, ResourceType.WOOD); + assertEquals(prodIncrease, gson.fromJson("\"W/C\"", Production.class)); + } + + @Test + public void deserialize_choice3_unordered() { + Production prodIncrease = createChoice(ResourceType.WOOD, ResourceType.CLAY, ResourceType.ORE); + assertEquals(prodIncrease, gson.fromJson("\"W/O/C\"", Production.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void deserialize_failOnMultipleResourcesInChoice() { + gson.fromJson("\"W/SS/C\"", Production.class); + } + +} \ No newline at end of file diff --git a/backend/src/test/java/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.java b/backend/src/test/java/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.java index 6031e112..d19f6288 100644 --- a/backend/src/test/java/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.java +++ b/backend/src/test/java/org/luxons/sevenwonders/game/effects/ProductionIncreaseTest.java @@ -28,17 +28,39 @@ public class ProductionIncreaseTest { } @Theory - public void apply_boardContainsAddedResourceType(ResourceType initialType, ResourceType addedType, ResourceType extraType) { + public void apply_boardContainsAddedResourceType(ResourceType initialType, ResourceType addedType, + ResourceType extraType) { Board board = TestUtils.createBoard(initialType); ProductionIncrease effect = createProductionIncrease(addedType); + effect.setSellable(false); effect.apply(board); Resources resources = TestUtils.createResources(initialType, addedType); assertTrue(board.getProduction().contains(resources)); + assertFalse(board.getPublicProduction().contains(resources)); Resources moreResources = TestUtils.createResources(initialType, addedType, extraType); assertFalse(board.getProduction().contains(moreResources)); + assertFalse(board.getPublicProduction().contains(moreResources)); + } + + @Theory + public void apply_boardContainsAddedResourceType_sellable(ResourceType initialType, ResourceType addedType, + ResourceType extraType) { + Board board = TestUtils.createBoard(initialType); + ProductionIncrease effect = createProductionIncrease(addedType); + effect.setSellable(true); + + effect.apply(board); + + Resources resources = TestUtils.createResources(initialType, addedType); + assertTrue(board.getProduction().contains(resources)); + assertTrue(board.getPublicProduction().contains(resources)); + + Resources moreResources = TestUtils.createResources(initialType, addedType, extraType); + assertFalse(board.getProduction().contains(moreResources)); + assertFalse(board.getPublicProduction().contains(moreResources)); } @Theory -- cgit