diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2016-04-09 15:53:24 -0700 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2016-04-09 15:53:24 -0700 |
commit | 371ccae3d1f31ec38f4af77fb7fcd175d49b3cd5 (patch) | |
tree | 38c4f1e8828f9af9c4b77a173bee0d312b321698 /src/test/java/com/p4square/grow/model | |
parent | bbf907e51dfcf157bdee24dead1d531122aa25db (diff) | |
parent | 3102d8bce3426d9cf41aeaf201c360d342677770 (diff) |
Merge pull request #10 from PuyallupFoursquare/maven
Switching from Ivy+Ant to Maven.
Diffstat (limited to 'src/test/java/com/p4square/grow/model')
14 files changed, 1424 insertions, 0 deletions
diff --git a/src/test/java/com/p4square/grow/model/AnswerTest.java b/src/test/java/com/p4square/grow/model/AnswerTest.java new file mode 100644 index 0000000..1747773 --- /dev/null +++ b/src/test/java/com/p4square/grow/model/AnswerTest.java @@ -0,0 +1,137 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests for the Answer class. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class AnswerTest { + private static final double DELTA = 1e-15; + + public static void main(String... args) { + org.junit.runner.JUnitCore.main(AnswerTest.class.getName()); + } + + /** + * Verify that the correct default values are returned. + */ + @Test + public void testDefaults() { + Answer a = new Answer(); + + // Type should default to AVERAGE + assertEquals(Answer.ScoreType.AVERAGE, a.getType()); + + // NextQuestion should default to null + assertNull(a.getNextQuestion()); + } + + /** + * Verify that getters and setters function correctly. + */ + @Test + public void testGetAndSet() { + Answer a = new Answer(); + + a.setText("Answer Text"); + assertEquals("Answer Text", a.getText()); + + a.setType(Answer.ScoreType.TRUMP); + assertEquals(Answer.ScoreType.TRUMP, a.getType()); + + a.setScore(10); + assertEquals(10, a.getScore(), DELTA); + + a.setNextQuestion("nextQuestion"); + assertEquals("nextQuestion", a.getNextQuestion()); + } + + /** + * Verify that when the ScoreType is NONE, the score is 0. + */ + @Test + public void testScoreTypeNone() { + Answer a = new Answer(); + + a.setScore(10); + assertEquals(10, a.getScore(), DELTA); + + a.setType(Answer.ScoreType.NONE); + assertEquals(0, a.getScore(), DELTA); + } + + /** + * Test score() with type TRUMP. + */ + @Test + public void testScoreTrump() { + Score score = new Score(); + score.sum = 10; + score.count = 2; + + Answer a = new Answer(); + a.setType(Answer.ScoreType.TRUMP); + a.setScore(5); + + assertFalse(a.score(score)); + + assertEquals(5, score.getSum(), DELTA); + assertEquals(1, score.getCount()); + } + + /** + * Test score() with type NONE. + */ + @Test + public void testScoreNone() { + Score score = new Score(); + score.sum = 10; + score.count = 2; + + Answer a = new Answer(); + a.setScore(5); + a.setType(Answer.ScoreType.NONE); + + assertTrue(a.score(score)); + + assertEquals(10, score.getSum(), DELTA); + assertEquals(2, score.getCount()); + } + + /** + * Test score() with type AVERAGE. + */ + @Test + public void testScoreAverage() { + Score score = new Score(); + score.sum = 10; + score.count = 2; + + Answer a = new Answer(); + a.setScore(5); + a.setType(Answer.ScoreType.AVERAGE); + + assertTrue(a.score(score)); + + assertEquals(15, score.getSum(), DELTA); + assertEquals(3, score.getCount()); + } + + /** + * Verify that ScoreType.toString() returns the proper strings. + */ + @Test + public void testScoreTypeToString() { + assertEquals("none", Answer.ScoreType.NONE.toString()); + assertEquals("average", Answer.ScoreType.AVERAGE.toString()); + assertEquals("trump", Answer.ScoreType.TRUMP.toString()); + } +} diff --git a/src/test/java/com/p4square/grow/model/CircleQuestionTest.java b/src/test/java/com/p4square/grow/model/CircleQuestionTest.java new file mode 100644 index 0000000..222cda5 --- /dev/null +++ b/src/test/java/com/p4square/grow/model/CircleQuestionTest.java @@ -0,0 +1,92 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests for CircleQuestion. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class CircleQuestionTest { + private static final double DELTA = 1e-4; + + public static void main(String... args) { + org.junit.runner.JUnitCore.main(CircleQuestionTest.class.getName()); + } + + private CircleQuestion mQuestion; + + @Before + public void setUp() { + mQuestion = new CircleQuestion(); + + Answer a1 = new Answer(); + a1.setScore(2); + + Answer a2 = new Answer(); + a2.setScore(4); + + mQuestion.getAnswers().put("1.00,0.00", a1); + mQuestion.getAnswers().put("-1.00,0.00", a2); + } + + /** + * Verify the getters and setters function correctly. + */ + @Test + public void testGetAndSet() { + mQuestion.setTopLeft("TopLeft String"); + assertEquals("TopLeft String", mQuestion.getTopLeft()); + + mQuestion.setTopRight("TopRight String"); + assertEquals("TopRight String", mQuestion.getTopRight()); + + mQuestion.setBottomRight("BottomRight String"); + assertEquals("BottomRight String", mQuestion.getBottomRight()); + + mQuestion.setBottomLeft("BottomLeft String"); + assertEquals("BottomLeft String", mQuestion.getBottomLeft()); + } + + /** + * The ScoringEngines are tested extensively independently, so simply + * verify that we get the expected results for our input. + */ + @Test + public void testScoreAnswer() { + Score score = new Score(); + RecordedAnswer answer = new RecordedAnswer(); + + answer.setAnswerId("0.5,0.5"); + assertTrue(mQuestion.scoreAnswer(score, answer)); + assertEquals(2, score.sum, DELTA); + assertEquals(1, score.count); + + answer.setAnswerId("-0.5,-0.5"); + assertTrue(mQuestion.scoreAnswer(score, answer)); + assertEquals(6, score.sum, DELTA); + assertEquals(2, score.count); + + try { + answer.setAnswerId("notAPoint"); + assertTrue(mQuestion.scoreAnswer(score, answer)); + fail("Should have thrown exception."); + } catch (IllegalArgumentException e) { + } + } + + /** + * Verify the correct type string is returned. + */ + @Test + public void testType() { + assertEquals("circle", mQuestion.getType().toString()); + } +} diff --git a/src/test/java/com/p4square/grow/model/ImageQuestionTest.java b/src/test/java/com/p4square/grow/model/ImageQuestionTest.java new file mode 100644 index 0000000..28ccdb2 --- /dev/null +++ b/src/test/java/com/p4square/grow/model/ImageQuestionTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Test for ImageQuestion. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class ImageQuestionTest { + private static final double DELTA = 1e-4; + + public static void main(String... args) { + org.junit.runner.JUnitCore.main(ImageQuestionTest.class.getName()); + } + + private Question mQuestion; + + @Before + public void setUp() { + mQuestion = new ImageQuestion(); + + Answer a1 = new Answer(); + a1.setScore(2); + + Answer a2 = new Answer(); + a2.setScore(4); + + mQuestion.getAnswers().put("a1", a1); + mQuestion.getAnswers().put("a2", a2); + } + + /** + * The ScoringEngines are tested extensively independently, so simply + * verify that we get the expected results for our input. + */ + @Test + public void testScoreAnswer() { + Score score = new Score(); + RecordedAnswer answer = new RecordedAnswer(); + + answer.setAnswerId("a1"); + assertTrue(mQuestion.scoreAnswer(score, answer)); + assertEquals(2, score.sum, DELTA); + assertEquals(1, score.count); + + answer.setAnswerId("a2"); + assertTrue(mQuestion.scoreAnswer(score, answer)); + assertEquals(6, score.sum, DELTA); + assertEquals(2, score.count); + + try { + answer.setAnswerId("unknown"); + assertTrue(mQuestion.scoreAnswer(score, answer)); + fail("Should have thrown exception."); + } catch (IllegalArgumentException e) { + } + } + + /** + * Verify the correct type string is returned. + */ + @Test + public void testType() { + assertEquals("image", mQuestion.getType().toString()); + } +} diff --git a/src/test/java/com/p4square/grow/model/PlaylistTest.java b/src/test/java/com/p4square/grow/model/PlaylistTest.java new file mode 100644 index 0000000..9c893f6 --- /dev/null +++ b/src/test/java/com/p4square/grow/model/PlaylistTest.java @@ -0,0 +1,154 @@ +/* + * Copyright 2014 Jesse Morgan + */ + +package com.p4square.grow.model; + +import java.util.Date; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests for Playlist. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class PlaylistTest { + public static void main(String... args) { + org.junit.runner.JUnitCore.main(PlaylistTest.class.getName()); + } + + /** + * Tests for Playlist and Chapter methods not covered in the deserialization test. + */ + @Test + public void testPlaylistAndChapter() { + // Create a playlist for the test + Playlist playlist = new Playlist(); + playlist.add("chapter1", "video1"); + playlist.add("chapter1", "video2"); + + // Chapter should not be complete + assertFalse(playlist.isChapterComplete("chapter1")); + + // We should find the chapter in the map + Map<String, Chapter> chapterMap = playlist.getChaptersMap(); + Chapter chapter1 = chapterMap.get("chapter1"); + assertTrue(null != chapter1); + + // We should find the videos in the map. + Map<String, VideoRecord> videoMap = chapter1.getVideos(); + assertTrue(null != videoMap.get("video1")); + assertTrue(null != videoMap.get("video2")); + assertTrue(null == videoMap.get("video3")); + + // Mark the videos as complete + VideoRecord video1 = videoMap.get("video1"); + VideoRecord video2 = videoMap.get("video2"); + video1.complete(); + video2.complete(); + + // Chapter should be complete now. + assertTrue(playlist.isChapterComplete("chapter1")); + assertFalse(playlist.isChapterComplete("bogusChapter")); + } + + /** + * Tests for Playlist default values. + */ + @Test + public void testPlaylistDefaults() { + Date before = new Date(); + Playlist p = new Playlist(); + + // Verify that a playlist without an explicit lastUpdated date is older than now. + assertTrue(p.getLastUpdated().before(before)); + } + + /** + * Tests for the Playlist merge method. + */ + @Test + public void testMergePlaylist() { + Playlist oldList = new Playlist(); + oldList.add("chapter1", "video1").setRequired(true); + oldList.add("chapter2", "video2").setRequired(false); + oldList.add("chapter2", "video3").complete(); + oldList.setLastUpdated(new Date(100)); + + Playlist newList = new Playlist(); + newList.add("chapter1", "video4").setRequired(true); + newList.add("chapter2", "video5").setRequired(false); + newList.add("chapter3", "video6").setRequired(false); + newList.setLastUpdated(new Date(500)); + + // Verify that you can't merge the old into the new + newList.merge(oldList); + assertTrue(null == newList.find("video2")); + + // Merge the new list into the old and verify results + oldList.merge(newList); + + // All Videos Present + assertTrue(oldList.find("video1").getRequired()); + assertFalse(oldList.find("video2").getRequired()); + assertTrue(oldList.find("video3").getComplete()); + assertTrue(oldList.find("video4").getRequired()); + assertFalse(oldList.find("video5").getRequired()); + assertFalse(oldList.find("video6").getRequired()); + + // New Chapter added + Map<String, Chapter> chapters = oldList.getChaptersMap(); + assertEquals(3, chapters.size()); + assertTrue(null != chapters.get("chapter3")); + + // Date updated + assertEquals(newList.getLastUpdated(), oldList.getLastUpdated()); + + // Video objects are actually independent + VideoRecord oldVideo4 = oldList.find("video4"); + VideoRecord newVideo4 = newList.find("video4"); + assertTrue(oldVideo4 != newVideo4); + } + + /** + * Tests for merges that move videos. + */ + @Test + public void testMergeMoveVideoRecord() { + Playlist oldList = new Playlist(); + oldList.add("chapter1", "video1").setRequired(true); + VideoRecord toMove = oldList.add("chapter1", "video2"); + toMove.setRequired(true); + toMove.complete(); + oldList.add("chapter2", "video3").complete(); + oldList.setLastUpdated(new Date(100)); + + Playlist newList = new Playlist(); + newList.add("chapter1", "video1").setRequired(true); + newList.add("chapter2", "video2").setRequired(true); + newList.add("chapter3", "video3").complete(); + newList.setLastUpdated(new Date(500)); + + // Merge the new list into the old and verify results + oldList.merge(newList); + + // All Videos Present + assertTrue(oldList.find("video1").getRequired()); + assertTrue(oldList.find("video2").getRequired()); + assertTrue(oldList.find("video3").getComplete()); + + // toMove is in the correct chapter. + assertNull(oldList.getChaptersMap().get("chapter1").getVideoRecord("video2")); + VideoRecord afterMove = oldList.getChaptersMap().get("chapter2").getVideoRecord("video2"); + assertSame(toMove, afterMove); + + // video3 got moved to the new chapter3 + assertNull(oldList.getChaptersMap().get("chapter2").getVideoRecord("video3")); + assertTrue(oldList.getChaptersMap().get("chapter3").getVideoRecord("video3").getComplete()); + } +} diff --git a/src/test/java/com/p4square/grow/model/PointTest.java b/src/test/java/com/p4square/grow/model/PointTest.java new file mode 100644 index 0000000..a4e7cc0 --- /dev/null +++ b/src/test/java/com/p4square/grow/model/PointTest.java @@ -0,0 +1,129 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests for the Point class. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class PointTest { + private static final double DELTA = 1e-15; + + public static void main(String... args) { + org.junit.runner.JUnitCore.main(PointTest.class.getName()); + } + + /** + * Verify that the constructor works properly. + */ + @Test + public void testHappyCase() { + Point p = new Point(1, 2); + assertEquals(1, p.getX(), DELTA); + assertEquals(2, p.getY(), DELTA); + } + + /** + * Verify distance is computed correctly. + */ + @Test + public void testDistance() { + Point p1, p2; + + // Simple line + p1 = new Point(2, 1); + p2 = new Point(-2, 1); + assertEquals(4, p1.distance(p2), DELTA); + assertEquals(4, p2.distance(p1), DELTA); + + // Across origin + p1 = new Point(5, 1); + p2 = new Point(-3, -2); + assertEquals(Math.sqrt(73), p1.distance(p2), DELTA); + assertEquals(Math.sqrt(73), p2.distance(p1), DELTA); + } + + /** + * Verify toString returns the expected string. + */ + @Test + public void testToString() { + Point p = new Point(-1.12345, 2.3); + assertEquals("-1.12,2.30", p.toString()); + } + + /** + * Verify that valueOf correctly parses a variety of strings. + */ + @Test + public void testValueOfHappyCase() { + Point p; + + p = Point.valueOf("1,2"); + assertEquals(1, p.getX(), DELTA); + assertEquals(2, p.getY(), DELTA); + + p = Point.valueOf("1.5,2.0"); + assertEquals(1.5, p.getX(), DELTA); + assertEquals(2.0, p.getY(), DELTA); + + p = Point.valueOf("-1.5,2.0"); + assertEquals(-1.5, p.getX(), DELTA); + assertEquals(2.0, p.getY(), DELTA); + + p = Point.valueOf("1.5,-2.0"); + assertEquals(1.5, p.getX(), DELTA); + assertEquals(-2.0, p.getY(), DELTA); + + p = Point.valueOf("-1.5,-2.0"); + assertEquals(-1.5, p.getX(), DELTA); + assertEquals(-2.0, p.getY(), DELTA); + } + + /** + * Verify that valueOf fails on null string. + */ + @Test(expected = NullPointerException.class) + public void testValueOfNull() { + Point.valueOf(null); + } + + /** + * Verify that valueOf fails on empty string. + */ + @Test(expected = IllegalArgumentException.class) + public void testValueOfEmptyString() { + Point.valueOf(""); + } + + /** + * Verify that valueOf fails on missing comma. + */ + @Test(expected = IllegalArgumentException.class) + public void testValueOfMissingComma() { + Point.valueOf("123"); + } + + /** + * Verify that valueOf fails on missing x. + */ + @Test(expected = IllegalArgumentException.class) + public void testValueOfMissingX() { + Point.valueOf(",12"); + } + + /** + * Verify that valueOf fails on missing y. + */ + @Test(expected = IllegalArgumentException.class) + public void testValueOfMissingY() { + Point.valueOf("12,"); + } +} diff --git a/src/test/java/com/p4square/grow/model/QuadQuestionTest.java b/src/test/java/com/p4square/grow/model/QuadQuestionTest.java new file mode 100644 index 0000000..389148a --- /dev/null +++ b/src/test/java/com/p4square/grow/model/QuadQuestionTest.java @@ -0,0 +1,92 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Test for QuadQuestion. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class QuadQuestionTest { + private static final double DELTA = 1e-4; + + public static void main(String... args) { + org.junit.runner.JUnitCore.main(QuadQuestionTest.class.getName()); + } + + private QuadQuestion mQuestion; + + @Before + public void setUp() { + mQuestion = new QuadQuestion(); + + Answer a1 = new Answer(); + a1.setScore(2); + + Answer a2 = new Answer(); + a2.setScore(4); + + mQuestion.getAnswers().put("1.00,0.00", a1); + mQuestion.getAnswers().put("-1.00,0.00", a2); + } + + /** + * Verify the getters and setters function correctly. + */ + @Test + public void testGetAndSet() { + mQuestion.setTop("Top String"); + assertEquals("Top String", mQuestion.getTop()); + + mQuestion.setBottom("Bottom String"); + assertEquals("Bottom String", mQuestion.getBottom()); + + mQuestion.setLeft("Left String"); + assertEquals("Left String", mQuestion.getLeft()); + + mQuestion.setRight("Right String"); + assertEquals("Right String", mQuestion.getRight()); + } + + /** + * The ScoringEngines are tested extensively independently, so simply + * verify that we get the expected results for our input. + */ + @Test + public void testScoreAnswer() { + Score score = new Score(); + RecordedAnswer answer = new RecordedAnswer(); + + answer.setAnswerId("0.5,0.5"); + assertTrue(mQuestion.scoreAnswer(score, answer)); + assertEquals(2, score.sum, DELTA); + assertEquals(1, score.count); + + answer.setAnswerId("-0.5,-0.5"); + assertTrue(mQuestion.scoreAnswer(score, answer)); + assertEquals(6, score.sum, DELTA); + assertEquals(2, score.count); + + try { + answer.setAnswerId("notAPoint"); + assertTrue(mQuestion.scoreAnswer(score, answer)); + fail("Should have thrown exception."); + } catch (IllegalArgumentException e) { + } + } + + /** + * Verify the correct type string is returned. + */ + @Test + public void testType() { + assertEquals("quad", mQuestion.getType().toString()); + } +} diff --git a/src/test/java/com/p4square/grow/model/QuadScoringEngineTest.java b/src/test/java/com/p4square/grow/model/QuadScoringEngineTest.java new file mode 100644 index 0000000..246a59f --- /dev/null +++ b/src/test/java/com/p4square/grow/model/QuadScoringEngineTest.java @@ -0,0 +1,85 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Test the QuadScoringEngine. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class QuadScoringEngineTest { + private static final double DELTA = 1e-4; + + public static void main(String... args) { + org.junit.runner.JUnitCore.main(QuadScoringEngineTest.class.getName()); + } + + private Question mQuestion; + private ScoringEngine mEngine; + + @Before + public void setup() { + // Setup the Question + mQuestion = new QuadQuestion(); + Map<String, Answer> answers = mQuestion.getAnswers(); + + // Create four answers at (-1,-1), (1, -1), (-1, 1), (1, 1) + for (int i = 1; i <= 4; i++) { + int x = i % 2 == 0 ? 1 : -1; + int y = i > 2 ? 1 : -1; + + Answer a = new Answer(); + a.setScore(i); + answers.put(x + ".00," + y + ".00", a); + } + + mEngine = new QuadScoringEngine(); + } + + /** + * Test a point inside each quadrant. + */ + @Test + public void testEachQuadrant() { + Score score; + RecordedAnswer answer = new RecordedAnswer(); + + // 0.5,0.5 == 4 + score = new Score(); + answer.setAnswerId("0.5,0.5"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(4, score.getSum(), DELTA); + assertEquals(1, score.getCount()); + + // 0.5,-0.5 == 2 + score = new Score(); + answer.setAnswerId("0.5,-0.5"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(2, score.getSum(), DELTA); + assertEquals(1, score.getCount()); + + // -0.5,0.5 == 3 + score = new Score(); + answer.setAnswerId("-0.5,0.5"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(3, score.getSum(), DELTA); + assertEquals(1, score.getCount()); + + // -0.5,-0.5 == 0.5 + score = new Score(); + answer.setAnswerId("-0.5,-0.5"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(1, score.getSum(), DELTA); + assertEquals(1, score.getCount()); + } + +} diff --git a/src/test/java/com/p4square/grow/model/QuestionTest.java b/src/test/java/com/p4square/grow/model/QuestionTest.java new file mode 100644 index 0000000..d09d2d8 --- /dev/null +++ b/src/test/java/com/p4square/grow/model/QuestionTest.java @@ -0,0 +1,80 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests for the Question class. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class QuestionTest { + public static void main(String... args) { + org.junit.runner.JUnitCore.main(QuestionTest.class.getName()); + } + + /** + * Verify that all the getters and setters function. + */ + @Test + public void testGetAndSet() { + TextQuestion q = new TextQuestion(); + + q.setId("123"); + assertEquals("123", q.getId()); + + q.setQuestion("Hello World"); + assertEquals("Hello World", q.getQuestion()); + + q.setPreviousQuestion("122"); + assertEquals("122", q.getPreviousQuestion()); + + q.setNextQuestion("124"); + assertEquals("124", q.getNextQuestion()); + } + + /** + * Verify the correct next question is returned. + */ + @Test + public void testGetNextQuestion() { + // Setup the Question + TextQuestion q = new TextQuestion(); + q.setNextQuestion("defaultNext"); + + Answer answerWithNext = new Answer(); + answerWithNext.setNextQuestion("answerNext"); + + q.getAnswers().put("withNext", answerWithNext); + q.getAnswers().put("withoutNext", new Answer()); + + // Answer without a nextQuestion should return default. + assertEquals("defaultNext", q.getNextQuestion("withoutNext")); + + // Answer with a nextQuestion should return it's next question. + assertEquals("answerNext", q.getNextQuestion("withNext")); + + // Unknown answer should also return the default + assertEquals("defaultNext", q.getNextQuestion("unknownAnswer")); + } + + /** + * Validate the toString() results for the enum. + * + * This may seem like an odd test, but it is very important for these to be + * lowercase to match the values in the JSON files. + */ + @Test + public void testToString() { + assertEquals("text", Question.QuestionType.TEXT.toString()); + assertEquals("image", Question.QuestionType.IMAGE.toString()); + assertEquals("slider", Question.QuestionType.SLIDER.toString()); + assertEquals("quad", Question.QuestionType.QUAD.toString()); + assertEquals("circle", Question.QuestionType.CIRCLE.toString()); + } +} diff --git a/src/test/java/com/p4square/grow/model/ScoreTest.java b/src/test/java/com/p4square/grow/model/ScoreTest.java new file mode 100644 index 0000000..5c7be46 --- /dev/null +++ b/src/test/java/com/p4square/grow/model/ScoreTest.java @@ -0,0 +1,111 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Test for the Score class. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class ScoreTest { + private static final double DELTA = 1e-4; + + public static void main(String... args) { + org.junit.runner.JUnitCore.main(ScoreTest.class.getName()); + } + + private Score mScore; + + @Before + public void setUp() { + mScore = new Score(); + } + + /** + * Verify getters and setters function. + */ + @Test + public void testGetAndSet() { + // getSum() + mScore.sum = 1.1; + assertEquals(1.1, mScore.getSum(), DELTA); + + // getCount() + mScore.count = 5; + assertEquals(5, mScore.getCount()); + } + + /** + * Verify that the average is computed by getScore(). + */ + @Test + public void testGetScore() { + mScore.sum = 7; + mScore.count = 2; + assertEquals(3.5, mScore.getScore(), DELTA); + } + + /** + * Verify that numericScore() returns the correct mappings. + */ + @Test + public void testNumericScore() { + assertEquals(3.5, Score.numericScore("teacher"), DELTA); + assertEquals(2.5, Score.numericScore("disciple"), DELTA); + assertEquals(1.5, Score.numericScore("believer"), DELTA); + assertEquals(0, Score.numericScore("seeker"), DELTA); + } + + /** + * Verify that toString() returns the correct mappings. + */ + @Test + public void testToString() { + mScore.count = 1; + + // Seeker is defined as score < 1.5 + mScore.sum = 0; + assertEquals("seeker", mScore.toString()); + mScore.sum = 0.5; + assertEquals("seeker", mScore.toString()); + mScore.sum = 1; + assertEquals("seeker", mScore.toString()); + mScore.sum = 1.49; + assertEquals("seeker", mScore.toString()); + + // Believer is defined as 1.5 <= score < 2.5 + mScore.sum = 1.5; + assertEquals("believer", mScore.toString()); + mScore.sum = 2; + assertEquals("believer", mScore.toString()); + mScore.sum = 2.49; + assertEquals("believer", mScore.toString()); + + // Disciple is defined as 2.5 <= score < 3.5 + mScore.sum = 2.5; + assertEquals("disciple", mScore.toString()); + mScore.sum = 3; + assertEquals("disciple", mScore.toString()); + mScore.sum = 3.49; + assertEquals("disciple", mScore.toString()); + + // Teacher is defined as 3.5 <= score + mScore.sum = 3.5; + assertEquals("teacher", mScore.toString()); + mScore.sum = 4; + assertEquals("teacher", mScore.toString()); + mScore.sum = 4.5; + assertEquals("teacher", mScore.toString()); + mScore.sum = 4.99; + assertEquals("teacher", mScore.toString()); + mScore.sum = 5; + assertEquals("teacher", mScore.toString()); + } +} diff --git a/src/test/java/com/p4square/grow/model/SimpleScoringEngineTest.java b/src/test/java/com/p4square/grow/model/SimpleScoringEngineTest.java new file mode 100644 index 0000000..1a1bc95 --- /dev/null +++ b/src/test/java/com/p4square/grow/model/SimpleScoringEngineTest.java @@ -0,0 +1,86 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Test the SimpleScoringEngine. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class SimpleScoringEngineTest { + private static final double DELTA = 1e-15; + + public static void main(String... args) { + org.junit.runner.JUnitCore.main(SimpleScoringEngineTest.class.getName()); + } + + private Question mQuestion; + private ScoringEngine mEngine; + + @Before + public void setup() { + // Setup the Question + mQuestion = new TextQuestion(); + + for (int i = 0; i <= 4; i++) { + Answer a = new Answer(); + a.setScore(i); + mQuestion.getAnswers().put("a" + i, a); + } + + mEngine = new SimpleScoringEngine(); + } + + /** + * Test that each individual answer is scored correctly. + */ + @Test + public void testAllAnswers() { + for (int i = 1; i <= 4; i++) { + Score score = new Score(); + RecordedAnswer answer = new RecordedAnswer(); + answer.setAnswerId("a" + i); + + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + + assertEquals(1, score.count); + assertEquals(i, score.sum, DELTA); + } + } + + /** + * Test that each answer score forms an increasing sum. + */ + @Test + public void testAllAnswersIncremental() { + Score score = new Score(); + + for (int i = 1; i <= 4; i++) { + RecordedAnswer answer = new RecordedAnswer(); + answer.setAnswerId("a" + i); + + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + } + + assertEquals(4, score.count); + assertEquals(10, score.sum, DELTA); + } + + /** + * Verify exception is thrown for undefined answer. + */ + @Test(expected = IllegalArgumentException.class) + public void testUnknownAnswer() { + Score score = new Score(); + RecordedAnswer answer = new RecordedAnswer(); + answer.setAnswerId("unknown"); + mEngine.scoreAnswer(score, mQuestion, answer); + } +} diff --git a/src/test/java/com/p4square/grow/model/SliderQuestionTest.java b/src/test/java/com/p4square/grow/model/SliderQuestionTest.java new file mode 100644 index 0000000..b5c4e83 --- /dev/null +++ b/src/test/java/com/p4square/grow/model/SliderQuestionTest.java @@ -0,0 +1,64 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests for SliderQuestion. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class SliderQuestionTest { + private static final double DELTA = 1e-4; + + public static void main(String... args) { + org.junit.runner.JUnitCore.main(SliderQuestionTest.class.getName()); + } + + private Question mQuestion; + + @Before + public void setUp() { + mQuestion = new SliderQuestion(); + + // Add some "answers" for the scoring engine. + mQuestion.getAnswers().put("1", new Answer()); + mQuestion.getAnswers().put("2", new Answer()); + mQuestion.getAnswers().put("3", new Answer()); + mQuestion.getAnswers().put("4", new Answer()); + } + + /** + * The ScoringEngines are tested extensively independently, so simply + * verify that we get the expected results for our input. + */ + @Test + public void testScoreAnswer() { + Score score = new Score(); + RecordedAnswer answer = new RecordedAnswer(); + + answer.setAnswerId("0.66666"); + assertTrue(mQuestion.scoreAnswer(score, answer)); + assertEquals(3, score.sum, DELTA); + assertEquals(1, score.count); + + answer.setAnswerId("1"); + assertTrue(mQuestion.scoreAnswer(score, answer)); + assertEquals(7, score.sum, DELTA); + assertEquals(2, score.count); + } + + /** + * Verify the correct type string is returned. + */ + @Test + public void testType() { + assertEquals("slider", mQuestion.getType().toString()); + } +} diff --git a/src/test/java/com/p4square/grow/model/SliderScoringEngineTest.java b/src/test/java/com/p4square/grow/model/SliderScoringEngineTest.java new file mode 100644 index 0000000..1ea0d4c --- /dev/null +++ b/src/test/java/com/p4square/grow/model/SliderScoringEngineTest.java @@ -0,0 +1,161 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Test the SliderScoringEngine. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class SliderScoringEngineTest { + private static final double DELTA = 1e-4; + + public static void main(String... args) { + org.junit.runner.JUnitCore.main(SliderScoringEngineTest.class.getName()); + } + + private Question mQuestion; + private ScoringEngine mEngine; + + @Before + public void setup() { + // Setup the Question + mQuestion = new SliderQuestion(); + mEngine = new SliderScoringEngine(); + } + + /** + * Test the scoreAnswer() method with four answers. + */ + @Test + public void testScoreAnswerFourAnswers() { + // Create the four answers. + createAnswers(4); + + Score score = new Score(); + RecordedAnswer answer = new RecordedAnswer(); + + // Test 0 + answer.setAnswerId("0"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(1, score.count); + assertEquals(1, score.sum, DELTA); + + // Test 1 + answer.setAnswerId("1"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(2, score.count); + assertEquals(5, score.sum, DELTA); + + // Test 0.33. Should be 2. + answer.setAnswerId("0.33333"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(3, score.count); + assertEquals(7, score.sum, DELTA); + + // Test 0.9, should be 4. + answer.setAnswerId("0.9"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(4, score.count); + assertEquals(11, score.sum, DELTA); + } + + /** + * Test the scoreAnswer() method with six answers. + */ + @Test + public void testScoreAnswerSixAnswers() { + // Create the four answers. + createAnswers(6); + + Score score = new Score(); + RecordedAnswer answer = new RecordedAnswer(); + + // Test 0 + answer.setAnswerId("0"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(1, score.count); + assertEquals(1, score.sum, DELTA); + + // Test 1 + answer.setAnswerId("1"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(2, score.count); + assertEquals(5, score.sum, DELTA); + + // Test 0.33. Should score as 1.33 + answer.setAnswerId("0.33333"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(3, score.count); + assertEquals(6.3333, score.sum, DELTA); + + // Test 0.55. Should score as 2.66 + answer.setAnswerId("0.55"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(4, score.count); + assertEquals(9, score.sum, DELTA); + + // Test 0.9. Should score as 4. + answer.setAnswerId("0.90"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(5, score.count); + assertEquals(13, score.sum, DELTA); + } + + /** + * Verify exception is thrown for non-numeric answer. + */ + @Test(expected = IllegalArgumentException.class) + public void testNonNumericAnswer() { + Score score = new Score(); + RecordedAnswer answer = new RecordedAnswer(); + answer.setAnswerId("unknown"); + mEngine.scoreAnswer(score, mQuestion, answer); + } + + /** + * Verify exception is thrown for negative answer. + */ + @Test(expected = IllegalArgumentException.class) + public void testNegativeAnswer() { + createAnswers(4); + Score score = new Score(); + RecordedAnswer answer = new RecordedAnswer(); + answer.setAnswerId("-1"); + mEngine.scoreAnswer(score, mQuestion, answer); + } + + /** + * Verify exception is thrown for out of bounds answer. + */ + @Test(expected = IllegalArgumentException.class) + public void testAnswerOutOfBounds() { + createAnswers(4); + Score score = new Score(); + RecordedAnswer answer = new RecordedAnswer(); + answer.setAnswerId("1.1"); + mEngine.scoreAnswer(score, mQuestion, answer); + } + + /** + * Helper method to create a number of questions on the slider. + * + * @param count Number of answers on the questions. + */ + private void createAnswers(int count) { + Map<String, Answer> answers = mQuestion.getAnswers(); + answers.clear(); + for (int i = 0; i < count; i++) { + answers.put(String.valueOf(i), new Answer()); + } + } +} diff --git a/src/test/java/com/p4square/grow/model/TextQuestionTest.java b/src/test/java/com/p4square/grow/model/TextQuestionTest.java new file mode 100644 index 0000000..d85ed86 --- /dev/null +++ b/src/test/java/com/p4square/grow/model/TextQuestionTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests for TextQuestion. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class TextQuestionTest { + private static final double DELTA = 1e-4; + + public static void main(String... args) { + org.junit.runner.JUnitCore.main(TextQuestionTest.class.getName()); + } + + private Question mQuestion; + + @Before + public void setUp() { + mQuestion = new TextQuestion(); + + Answer a1 = new Answer(); + a1.setScore(2); + + Answer a2 = new Answer(); + a2.setScore(4); + + mQuestion.getAnswers().put("a1", a1); + mQuestion.getAnswers().put("a2", a2); + } + + /** + * The ScoringEngines are tested extensively independently, so simply + * verify that we get the expected results for our input. + */ + @Test + public void testScoreAnswer() { + Score score = new Score(); + RecordedAnswer answer = new RecordedAnswer(); + + answer.setAnswerId("a1"); + assertTrue(mQuestion.scoreAnswer(score, answer)); + assertEquals(2, score.sum, DELTA); + assertEquals(1, score.count); + + answer.setAnswerId("a2"); + assertTrue(mQuestion.scoreAnswer(score, answer)); + assertEquals(6, score.sum, DELTA); + assertEquals(2, score.count); + + try { + answer.setAnswerId("unknown"); + assertTrue(mQuestion.scoreAnswer(score, answer)); + fail("Should have thrown exception."); + } catch (IllegalArgumentException e) { + } + } + + /** + * Verify the correct type string is returned. + */ + @Test + public void testType() { + assertEquals("text", mQuestion.getType().toString()); + } +} diff --git a/src/test/java/com/p4square/grow/model/TrainingRecordTest.java b/src/test/java/com/p4square/grow/model/TrainingRecordTest.java new file mode 100644 index 0000000..43caa90 --- /dev/null +++ b/src/test/java/com/p4square/grow/model/TrainingRecordTest.java @@ -0,0 +1,85 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import java.io.InputStream; +import java.util.Date; +import java.util.Map; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Test TrainingRecord, Playlist, and Chapter. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class TrainingRecordTest { + public static void main(String... args) { + org.junit.runner.JUnitCore.main(TrainingRecordTest.class.getName()); + } + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + /** + * Test deserialization of a JSON Training record. + */ + @Test + public void testDeserialization() throws Exception { + InputStream in = getClass().getResourceAsStream("trainingrecord.json"); + TrainingRecord record = MAPPER.readValue(in, TrainingRecord.class); + + // Last Video + assertEquals("teacher-1", record.getLastVideo()); + + // Playlist + Playlist playlist = record.getPlaylist(); + + // Find video successfully + VideoRecord r = playlist.find("teacher-1"); + assertEquals(true, r.getRequired()); + assertEquals(true, r.getComplete()); + assertEquals(new Date(1379288806266L), r.getCompletionDate()); + + // Find non-existent video + r = playlist.find("not-a-video"); + assertEquals(null, r); + + // isChapterComplete + assertTrue(playlist.isChapterComplete("seeker")); // Complete because not required. + assertTrue(playlist.isChapterComplete("disciple")); // Required and completed. + assertFalse(playlist.isChapterComplete("teacher")); // Not complete. + + // getChapterStatuses + Map<String, Boolean> statuses = playlist.getChapterStatuses(); + assertTrue(statuses.get("seeker")); // Complete because not required. + assertTrue(statuses.get("disciple")); // Required and completed. + assertFalse(statuses.get("teacher")); // Not complete. + } + + /** + * Tests for VideoRecord. + */ + @Test + public void testVideoRecord() { + VideoRecord record = new VideoRecord(); + + // Verify defaults + assertTrue(record.getRequired()); + assertFalse(record.getComplete()); + assertEquals(null, record.getCompletionDate()); + + // Verify completion + long now = System.currentTimeMillis(); + record.complete(); + assertTrue(record.getRequired()); + assertTrue(record.getComplete()); + assertTrue(now <= record.getCompletionDate().getTime()); + } +} |