summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/boards/Science.java2
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializer.java17
-rw-r--r--src/test/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializerTest.java164
3 files changed, 178 insertions, 5 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/boards/Science.java b/src/main/java/org/luxons/sevenwonders/game/boards/Science.java
index f3d6a681..71687657 100644
--- a/src/main/java/org/luxons/sevenwonders/game/boards/Science.java
+++ b/src/main/java/org/luxons/sevenwonders/game/boards/Science.java
@@ -32,7 +32,7 @@ public class Science {
}
public int size() {
- return quantities.size() + jokers;
+ return quantities.values().stream().mapToInt(q -> q).sum() + jokers;
}
public int computePoints() {
diff --git a/src/main/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializer.java b/src/main/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializer.java
index 0535b90a..b6e38540 100644
--- a/src/main/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializer.java
+++ b/src/main/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializer.java
@@ -2,6 +2,10 @@ package org.luxons.sevenwonders.game.data.serializers;
import java.lang.reflect.Type;
+import org.luxons.sevenwonders.game.boards.Science;
+import org.luxons.sevenwonders.game.boards.ScienceType;
+import org.luxons.sevenwonders.game.effects.ScienceProgress;
+
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
@@ -10,9 +14,6 @@ import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
-import org.luxons.sevenwonders.game.boards.Science;
-import org.luxons.sevenwonders.game.boards.ScienceType;
-import org.luxons.sevenwonders.game.effects.ScienceProgress;
public class ScienceProgressSerializer implements JsonSerializer<ScienceProgress>, JsonDeserializer<ScienceProgress> {
@@ -47,9 +48,17 @@ public class ScienceProgressSerializer implements JsonSerializer<ScienceProgress
if ("any".equals(s)) {
science.addJoker(1);
} else {
- science.add(context.deserialize(json, ScienceType.class), 1);
+ science.add(deserializeScienceType(json, context), 1);
}
scienceProgress.setScience(science);
return scienceProgress;
}
+
+ private ScienceType deserializeScienceType(JsonElement json, JsonDeserializationContext context) {
+ ScienceType type = context.deserialize(json, ScienceType.class);
+ if (type == null) {
+ throw new IllegalArgumentException("Invalid science type " + json.getAsString());
+ }
+ return type;
+ }
}
diff --git a/src/test/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializerTest.java b/src/test/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializerTest.java
new file mode 100644
index 00000000..33126aee
--- /dev/null
+++ b/src/test/java/org/luxons/sevenwonders/game/data/serializers/ScienceProgressSerializerTest.java
@@ -0,0 +1,164 @@
+package org.luxons.sevenwonders.game.data.serializers;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.luxons.sevenwonders.game.boards.Science;
+import org.luxons.sevenwonders.game.boards.ScienceType;
+import org.luxons.sevenwonders.game.effects.ScienceProgress;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import static org.junit.Assert.*;
+
+public class ScienceProgressSerializerTest {
+
+ private static final String COMPASS_STR = "\"COMPASS\"";
+
+ private static final String WHEEL_STR = "\"WHEEL\"";
+
+ private static final String TABLET_STR = "\"TABLET\"";
+
+ private static final String JOKER_STR = "\"any\"";
+
+ private Gson gson;
+
+ @Before
+ public void setUp() {
+ gson = new GsonBuilder().registerTypeAdapter(ScienceProgress.class, new ScienceProgressSerializer()).create();
+ }
+
+ private static ScienceProgress create(int compasses, int wheels, int tablets, int jokers) {
+ Science science = new Science();
+ if (compasses > 0) {
+ science.add(ScienceType.COMPASS, compasses);
+ }
+ if (wheels > 0) {
+ science.add(ScienceType.WHEEL, wheels);
+ }
+ if (tablets > 0) {
+ science.add(ScienceType.TABLET, tablets);
+ }
+ if (jokers > 0) {
+ science.addJoker(jokers);
+ }
+ ScienceProgress progress = new ScienceProgress();
+ progress.setScience(science);
+ return progress;
+ }
+
+ @Test
+ public void serialize_emptyToNull() {
+ ScienceProgress progress = create(0,0,0,0);
+ String json = gson.toJson(progress);
+ assertEquals("null", json);
+ }
+
+ @Test
+ public void serialize_oneCompass() {
+ ScienceProgress progress = create(1,0,0,0);
+ String json = gson.toJson(progress);
+ assertEquals(COMPASS_STR, json);
+ }
+
+ @Test
+ public void serialize_oneWheel() {
+ ScienceProgress progress = create(0,1,0,0);
+ String json = gson.toJson(progress);
+ assertEquals(WHEEL_STR, json);
+ }
+
+ @Test
+ public void serialize_oneTablet() {
+ ScienceProgress progress = create(0,0,1,0);
+ String json = gson.toJson(progress);
+ assertEquals(TABLET_STR, json);
+ }
+
+ @Test
+ public void serialize_oneJoker() {
+ ScienceProgress progress = create(0,0,0,1);
+ String json = gson.toJson(progress);
+ assertEquals(JOKER_STR, json);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void serialize_failOnMultipleCompasses() {
+ ScienceProgress progress = create(2,0,0,0);
+ gson.toJson(progress);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void serialize_failOnMultipleWheels() {
+ ScienceProgress progress = create(0,2,0,0);
+ gson.toJson(progress);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void serialize_failOnMultipleTablets() {
+ ScienceProgress progress = create(0,0,2,0);
+ gson.toJson(progress);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void serialize_failOnMultipleJokers() {
+ ScienceProgress progress = create(0,0,0,2);
+ gson.toJson(progress);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void serialize_failOnMixedElements() {
+ ScienceProgress progress = create(1,1,0,0);
+ gson.toJson(progress);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void deserialize_failOnEmptyString() {
+ gson.fromJson("\"\"", ScienceProgress.class);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void deserialize_failOnGarbageString() {
+ gson.fromJson("thisisgarbage", ScienceProgress.class);
+ }
+
+ @Test
+ public void deserialize_compass() {
+ ScienceProgress progress = gson.fromJson(COMPASS_STR, ScienceProgress.class);
+ assertNotNull(progress.getScience());
+ assertEquals(1, progress.getScience().getQuantity(ScienceType.COMPASS));
+ assertEquals(0, progress.getScience().getQuantity(ScienceType.WHEEL));
+ assertEquals(0, progress.getScience().getQuantity(ScienceType.TABLET));
+ assertEquals(0, progress.getScience().getJokers());
+ }
+
+ @Test
+ public void deserialize_wheel() {
+ ScienceProgress progress = gson.fromJson(WHEEL_STR, ScienceProgress.class);
+ assertNotNull(progress.getScience());
+ assertEquals(0, progress.getScience().getQuantity(ScienceType.COMPASS));
+ assertEquals(1, progress.getScience().getQuantity(ScienceType.WHEEL));
+ assertEquals(0, progress.getScience().getQuantity(ScienceType.TABLET));
+ assertEquals(0, progress.getScience().getJokers());
+ }
+
+ @Test
+ public void deserialize_tablet() {
+ ScienceProgress progress = gson.fromJson(TABLET_STR, ScienceProgress.class);
+ assertNotNull(progress.getScience());
+ assertEquals(0, progress.getScience().getQuantity(ScienceType.COMPASS));
+ assertEquals(0, progress.getScience().getQuantity(ScienceType.WHEEL));
+ assertEquals(1, progress.getScience().getQuantity(ScienceType.TABLET));
+ assertEquals(0, progress.getScience().getJokers());
+ }
+
+ @Test
+ public void deserialize_joker() {
+ ScienceProgress progress = gson.fromJson(JOKER_STR, ScienceProgress.class);
+ assertNotNull(progress.getScience());
+ assertEquals(0, progress.getScience().getQuantity(ScienceType.COMPASS));
+ assertEquals(0, progress.getScience().getQuantity(ScienceType.WHEEL));
+ assertEquals(0, progress.getScience().getQuantity(ScienceType.TABLET));
+ assertEquals(1, progress.getScience().getJokers());
+ }
+
+} \ No newline at end of file
bgstack15