diff options
Diffstat (limited to 'src/test/java/com/p4square')
20 files changed, 2340 insertions, 0 deletions
diff --git a/src/test/java/com/p4square/grow/backend/resources/ResourceTestBase.java b/src/test/java/com/p4square/grow/backend/resources/ResourceTestBase.java new file mode 100644 index 0000000..b4d7864 --- /dev/null +++ b/src/test/java/com/p4square/grow/backend/resources/ResourceTestBase.java @@ -0,0 +1,101 @@ +/* + * Copyright 2015 Jesse Morgan + */ + +package com.p4square.grow.backend.resources; + +import java.io.IOException; + +import org.restlet.Application; + +import com.p4square.grow.model.Playlist; +import com.p4square.grow.model.Question; +import com.p4square.grow.model.TrainingRecord; +import com.p4square.grow.model.UserRecord; + +import com.p4square.grow.backend.feed.FeedDataProvider; +import com.p4square.grow.provider.CollectionProvider; +import com.p4square.grow.provider.MapCollectionProvider; +import com.p4square.grow.provider.MapProvider; +import com.p4square.grow.provider.Provider; +import com.p4square.grow.provider.ProvidesAssessments; +import com.p4square.grow.provider.ProvidesQuestions; +import com.p4square.grow.provider.ProvidesStrings; +import com.p4square.grow.provider.ProvidesTrainingRecords; +import com.p4square.grow.provider.ProvidesUserRecords; +import com.p4square.grow.provider.ProvidesVideos; + +import org.junit.Before; + +/** + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class ResourceTestBase { + + protected TestApplication mApplication; + + @Before + public void setup() throws Exception { + mApplication = new TestApplication(); + Application.setCurrent(mApplication); + } + + public static class TestApplication extends Application implements + ProvidesQuestions, ProvidesTrainingRecords, ProvidesUserRecords, + ProvidesStrings, ProvidesAssessments + { + + private final Provider<String, UserRecord> mUserRecordProvider; + private final Provider<String, Question> mQuestionProvider; + private final Provider<String, TrainingRecord> mTrainingRecordProvider; + private final Provider<String, String> mStringProvider; + private final CollectionProvider<String, String, String> mAnswerProvider; + + private Playlist mDefaultPlaylist; + + public TestApplication() { + mStringProvider = new MapProvider<String, String>(); + mUserRecordProvider = new MapProvider<String, UserRecord>(); + mQuestionProvider = new MapProvider<String, Question>(); + mTrainingRecordProvider = new MapProvider<String, TrainingRecord>(); + mAnswerProvider = new MapCollectionProvider<String, String, String>(); + + mDefaultPlaylist = new Playlist(); + } + + @Override + public Provider<String, UserRecord> getUserRecordProvider() { + return mUserRecordProvider; + } + + @Override + public Provider<String, Question> getQuestionProvider() { + return mQuestionProvider; + } + + @Override + public Provider<String, TrainingRecord> getTrainingRecordProvider() { + return mTrainingRecordProvider; + } + + public void setDefaultPlaylist(Playlist playlist) { + mDefaultPlaylist = playlist; + } + + @Override + public Playlist getDefaultPlaylist() throws IOException { + return mDefaultPlaylist; + } + + @Override + public Provider<String, String> getStringProvider() { + return mStringProvider; + } + + @Override + public CollectionProvider<String, String, String> getAnswerProvider() { + return mAnswerProvider; + } + } +} diff --git a/src/test/java/com/p4square/grow/backend/resources/TrainingRecordResourceTest.java b/src/test/java/com/p4square/grow/backend/resources/TrainingRecordResourceTest.java new file mode 100644 index 0000000..db85051 --- /dev/null +++ b/src/test/java/com/p4square/grow/backend/resources/TrainingRecordResourceTest.java @@ -0,0 +1,142 @@ +/* + * Copyright 2015 Jesse Morgan + */ + +package com.p4square.grow.backend.resources; + +import java.util.Map; +import java.util.HashMap; + +import org.restlet.data.Method; +import org.restlet.Request; +import org.restlet.Response; +import org.restlet.ext.jackson.JacksonRepresentation; + +import com.p4square.grow.model.Playlist; +import com.p4square.grow.model.TrainingRecord; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests for the TrainingRecordResource + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class TrainingRecordResourceTest extends ResourceTestBase { + + private final String USER_ID = "1234"; + + private TrainingRecordResource mResource; + private Request mRequest; + private Response mResponse; + + @Before + public void setup() throws Exception { + super.setup(); + + mResource = new TrainingRecordResource(); + + mRequest = new Request(Method.GET, "/"); + Map<String, Object> attributes = new HashMap<>(); + attributes.put("userId", USER_ID); + mRequest.setAttributes(attributes); + mResponse = new Response(mRequest); + + Playlist playlist = new Playlist(); + playlist.add("introduction", "intro-1"); + playlist.add("seeker", "seeker-1"); + playlist.add("believer", "believer-1"); + playlist.add("believer", "believer-2"); + playlist.add("disciple", "disciple-1"); + playlist.add("teacher", "teacher-1"); + playlist.add("leader", "leader-1"); + mApplication.setDefaultPlaylist(playlist); + } + + private <T> T run(Class<T> type) throws Exception { + mResource.init(mApplication.getContext(), mRequest, mResponse); + mResource.handle(); + mResource.release(); + + return new JacksonRepresentation<T>(mResponse.getEntity(), type).getObject(); + } + + @Test + public void testSkipAssessedChaptersLow() throws Exception { + // Set the User's score. + mApplication.getAnswerProvider().put(USER_ID, "summary", "{\"sum\": 0.0, \"count\": 1}"); + + // Run the test + TrainingRecord record = run(TrainingRecord.class); + + // Assert correct videos required. + Playlist actualPlaylist = record.getPlaylist(); + assertTrue(actualPlaylist.find("intro-1").getRequired()); // Always required. + assertTrue(actualPlaylist.find("seeker-1").getRequired()); // Required by assessment. + assertTrue(actualPlaylist.find("believer-1").getRequired()); + assertTrue(actualPlaylist.find("believer-1").getRequired()); + assertTrue(actualPlaylist.find("disciple-1").getRequired()); + assertTrue(actualPlaylist.find("teacher-1").getRequired()); + assertTrue(actualPlaylist.find("leader-1").getRequired()); + } + + @Test + public void testSkipAssessedChaptersSeeker() throws Exception { + // Set the User's score. + mApplication.getAnswerProvider().put(USER_ID, "summary", "{\"sum\": 1.0, \"count\": 1}"); + + // Run the test + TrainingRecord record = run(TrainingRecord.class); + + // Assert correct videos required. + Playlist actualPlaylist = record.getPlaylist(); + assertTrue(actualPlaylist.find("intro-1").getRequired()); // Always required. + assertTrue(actualPlaylist.find("seeker-1").getRequired()); // Required by assessment. + assertTrue(actualPlaylist.find("believer-1").getRequired()); + assertTrue(actualPlaylist.find("believer-1").getRequired()); + assertTrue(actualPlaylist.find("disciple-1").getRequired()); + assertTrue(actualPlaylist.find("teacher-1").getRequired()); + assertTrue(actualPlaylist.find("leader-1").getRequired()); + } + + @Test + public void testSkipAssessedChaptersBeliever() throws Exception { + // Set the User's score. + mApplication.getAnswerProvider().put(USER_ID, "summary", "{\"sum\": 2.0, \"count\": 1}"); + + // Run the test + TrainingRecord record = run(TrainingRecord.class); + + // Assert correct videos required. + Playlist actualPlaylist = record.getPlaylist(); + assertTrue(actualPlaylist.find("intro-1").getRequired()); // Always required. + assertFalse(actualPlaylist.find("seeker-1").getRequired()); // Not required by assessment. + assertTrue(actualPlaylist.find("believer-1").getRequired()); // Required by assessment. + assertTrue(actualPlaylist.find("believer-1").getRequired()); + assertTrue(actualPlaylist.find("disciple-1").getRequired()); + assertTrue(actualPlaylist.find("teacher-1").getRequired()); + assertTrue(actualPlaylist.find("leader-1").getRequired()); + } + + @Test + public void testSkipAssessedChaptersHigh() throws Exception { + // Set the User's score. + mApplication.getAnswerProvider().put(USER_ID, "summary", "{\"sum\": 4.0, \"count\": 1}"); + + // Run the test + TrainingRecord record = run(TrainingRecord.class); + + // Assert correct videos required. + Playlist actualPlaylist = record.getPlaylist(); + assertTrue(actualPlaylist.find("intro-1").getRequired()); // Always required. + assertFalse(actualPlaylist.find("seeker-1").getRequired()); // Not required by assessment. + assertFalse(actualPlaylist.find("believer-1").getRequired()); + assertFalse(actualPlaylist.find("believer-1").getRequired()); + assertFalse(actualPlaylist.find("disciple-1").getRequired()); + assertTrue(actualPlaylist.find("teacher-1").getRequired()); // Required by assessment. + assertTrue(actualPlaylist.find("leader-1").getRequired()); + } +} diff --git a/src/test/java/com/p4square/grow/ccb/CCBProgressReporterTest.java b/src/test/java/com/p4square/grow/ccb/CCBProgressReporterTest.java new file mode 100644 index 0000000..63a973a --- /dev/null +++ b/src/test/java/com/p4square/grow/ccb/CCBProgressReporterTest.java @@ -0,0 +1,231 @@ +package com.p4square.grow.ccb; + +import com.p4square.ccbapi.CCBAPI; +import com.p4square.ccbapi.model.*; +import org.easymock.Capture; +import org.easymock.EasyMock; +import org.junit.Before; +import org.junit.Test; + +import java.time.LocalDate; +import java.util.Date; + +import static org.junit.Assert.*; + +/** + * Tests for the CCBProgressReporter. + */ +public class CCBProgressReporterTest { + + private static final String GROW_LEVEL = "GrowLevelTrain"; + private static final String ASSESSMENT_LEVEL = "GrowLevelAsmnt"; + + private CCBProgressReporter reporter; + + private CCBAPI api; + private CustomFieldCache cache; + + private CCBUser user; + private Date date; + + @Before + public void setUp() { + // Setup some data for testing. + IndividualProfile profile = new IndividualProfile(); + profile.setId(123); + profile.setFirstName("Larry"); + profile.setLastName("Cucumber"); + profile.setEmail("larry.cucumber@example.com"); + + user = new CCBUser(profile); + date = new Date(1427889600000L); // 2015-04-01 + + // Setup the mocks. + api = EasyMock.mock(CCBAPI.class); + cache = EasyMock.mock(CustomFieldCache.class); + reporter = new CCBProgressReporter(api, cache); + } + + private void setupCacheMocks() { + // Setup the Grow Level field. + CustomField growLevelDate = new CustomField(); + growLevelDate.setName("udf_ind_date_1"); + growLevelDate.setLabel(GROW_LEVEL); + + CustomField growLevelPulldown = new CustomField(); + growLevelPulldown.setName("udf_ind_pulldown_1"); + growLevelPulldown.setLabel(GROW_LEVEL); + + LookupTableItem believer = new LookupTableItem(); + believer.setId(1); + believer.setOrder(2); + believer.setName("Believer"); + + EasyMock.expect(cache.getDateFieldByLabel(GROW_LEVEL)) + .andReturn(growLevelDate).anyTimes(); + EasyMock.expect(cache.getIndividualPulldownByLabel(GROW_LEVEL)) + .andReturn(growLevelPulldown).anyTimes(); + EasyMock.expect(cache.getPulldownItemByName(LookupTableType.UDF_IND_PULLDOWN_1, "Believer")) + .andReturn(believer).anyTimes(); + + // Setup the Grow Assessment field. + CustomField growAssessmentDate = new CustomField(); + growAssessmentDate.setName("udf_ind_date_2"); + growAssessmentDate.setLabel(ASSESSMENT_LEVEL); + + CustomField growAssessmentPulldown = new CustomField(); + growAssessmentPulldown.setName("udf_ind_pulldown_2"); + growAssessmentPulldown.setLabel(ASSESSMENT_LEVEL); + + EasyMock.expect(cache.getDateFieldByLabel(ASSESSMENT_LEVEL)) + .andReturn(growAssessmentDate).anyTimes(); + EasyMock.expect(cache.getIndividualPulldownByLabel(ASSESSMENT_LEVEL)) + .andReturn(growAssessmentPulldown).anyTimes(); + EasyMock.expect(cache.getPulldownItemByName(LookupTableType.UDF_IND_PULLDOWN_2, "Believer")) + .andReturn(believer).anyTimes(); + } + + @Test + public void reportAssessmentComplete() throws Exception { + // Setup mocks + setupCacheMocks(); + Capture<UpdateIndividualProfileRequest> reqCapture = EasyMock.newCapture(); + EasyMock.expect(api.updateIndividualProfile(EasyMock.capture(reqCapture))) + .andReturn(EasyMock.mock(UpdateIndividualProfileResponse.class)); + replay(); + + // Test reporter + reporter.reportAssessmentComplete(user, "Believer", date, "Data"); + + // Assert that the profile was updated. + verify(); + assertTrue(reqCapture.hasCaptured()); + UpdateIndividualProfileRequest req = reqCapture.getValue(); + assertEquals(1, req.getCustomPulldownFields().get("udf_pulldown_2").intValue()); + assertEquals("2015-04-01", req.getCustomDateFields().get("udf_date_2").toString()); + } + + @Test + public void testReportChapterCompleteNoPreviousChapter() throws Exception { + // Setup mocks + setupCacheMocks(); + Capture<UpdateIndividualProfileRequest> reqCapture = EasyMock.newCapture(); + EasyMock.expect(api.updateIndividualProfile(EasyMock.capture(reqCapture))) + .andReturn(EasyMock.mock(UpdateIndividualProfileResponse.class)); + replay(); + + // Test reporter + reporter.reportChapterComplete(user, "Believer", date); + + // Assert that the profile was updated. + verify(); + assertTrue(reqCapture.hasCaptured()); + UpdateIndividualProfileRequest req = reqCapture.getValue(); + assertEquals(1, req.getCustomPulldownFields().get("udf_pulldown_1").intValue()); + assertEquals("2015-04-01", req.getCustomDateFields().get("udf_date_1").toString()); + } + + @Test + public void testReportChapterCompleteLowerPreviousChapter() throws Exception { + // Setup mocks + setupCacheMocks(); + Capture<UpdateIndividualProfileRequest> reqCapture = EasyMock.newCapture(); + EasyMock.expect(api.updateIndividualProfile(EasyMock.capture(reqCapture))) + .andReturn(EasyMock.mock(UpdateIndividualProfileResponse.class)); + + setUserPulldownSelection(GROW_LEVEL, "Seeker"); + + replay(); + + // Test reporter + reporter.reportChapterComplete(user, "Believer", date); + + // Assert that the profile was updated. + verify(); + assertTrue(reqCapture.hasCaptured()); + UpdateIndividualProfileRequest req = reqCapture.getValue(); + assertEquals(1, req.getCustomPulldownFields().get("udf_pulldown_1").intValue()); + assertEquals("2015-04-01", req.getCustomDateFields().get("udf_date_1").toString()); + } + + @Test + public void testReportChapterCompleteHigherPreviousChapter() throws Exception { + // Setup mocks + setupCacheMocks(); + setUserPulldownSelection(GROW_LEVEL, "Disciple"); + + replay(); + + // Test reporter + reporter.reportChapterComplete(user, "Believer", date); + + // Assert that the profile was updated. + verify(); + } + + @Test + public void testReportChapterCompleteNoCustomField() throws Exception { + // Setup mocks + EasyMock.expect(cache.getDateFieldByLabel(EasyMock.anyString())).andReturn(null).anyTimes(); + EasyMock.expect(cache.getIndividualPulldownByLabel(EasyMock.anyString())).andReturn(null).anyTimes(); + EasyMock.expect(cache.getPulldownItemByName(EasyMock.anyObject(), EasyMock.anyString())) + .andReturn(null).anyTimes(); + replay(); + + // Test reporter + reporter.reportChapterComplete(user, "Believer", date); + + // Assert that the profile was updated. + verify(); + } + + @Test + public void testReportChapterCompleteNoSuchValue() throws Exception { + // Setup mocks + setupCacheMocks(); + EasyMock.expect(cache.getPulldownItemByName(LookupTableType.UDF_IND_PULLDOWN_1, "Foo")) + .andReturn(null).anyTimes(); + Capture<UpdateIndividualProfileRequest> reqCapture = EasyMock.newCapture(); + EasyMock.expect(api.updateIndividualProfile(EasyMock.capture(reqCapture))) + .andReturn(EasyMock.mock(UpdateIndividualProfileResponse.class)); + replay(); + + // Test reporter + reporter.reportChapterComplete(user, "Foo", date); + + // Assert that the profile was updated. + verify(); + assertTrue(reqCapture.hasCaptured()); + UpdateIndividualProfileRequest req = reqCapture.getValue(); + assertNull(req.getCustomPulldownFields().get("udf_pulldown_1")); + assertEquals("2015-04-01", req.getCustomDateFields().get("udf_date_1").toString()); + } + + private void setUserPulldownSelection(final String field, final String value) { + // Get the pulldown field collection for the user. + CustomFieldCollection<CustomPulldownFieldValue> pulldowns = user.getProfile().getCustomPulldownFields(); + if (pulldowns == null) { + pulldowns = new CustomFieldCollection<>(); + user.getProfile().setCustomPulldownFields(pulldowns); + } + + // Create the selection for the value. + PulldownSelection selection = new PulldownSelection(); + selection.setLabel(value); + + // Create the field/value pair and add it to the collection. + CustomPulldownFieldValue fieldValue = new CustomPulldownFieldValue(); + fieldValue.setName(field); // This is unused by the test, but it should be a udf_ identifier. + fieldValue.setLabel(field); + fieldValue.setSelection(selection); + pulldowns.add(fieldValue); + } + + private void replay() { + EasyMock.replay(api, cache); + } + + private void verify() { + EasyMock.verify(api, cache); + } +}
\ No newline at end of file diff --git a/src/test/java/com/p4square/grow/ccb/CCBUserVerifierTest.java b/src/test/java/com/p4square/grow/ccb/CCBUserVerifierTest.java new file mode 100644 index 0000000..d17b698 --- /dev/null +++ b/src/test/java/com/p4square/grow/ccb/CCBUserVerifierTest.java @@ -0,0 +1,139 @@ +package com.p4square.grow.ccb; + +import com.p4square.ccbapi.CCBAPI; +import com.p4square.ccbapi.model.GetIndividualProfilesRequest; +import com.p4square.ccbapi.model.GetIndividualProfilesResponse; +import com.p4square.ccbapi.model.IndividualProfile; +import org.easymock.EasyMock; +import org.junit.Before; +import org.junit.Test; +import org.restlet.Request; +import org.restlet.Response; +import org.restlet.data.ChallengeResponse; +import org.restlet.data.ChallengeScheme; +import org.restlet.data.ClientInfo; +import org.restlet.security.Verifier; + +import java.io.IOException; +import java.util.Collections; + +import static org.junit.Assert.*; + +/** + * Tests for CCBUserVerifier. + */ +public class CCBUserVerifierTest { + + private IndividualProfile mProfile = new IndividualProfile(); + + private CCBAPI mAPI; + private CCBUserVerifier verifier; + + private ClientInfo mClientInfo; + private Request mMockRequest; + private Response mMockResponse; + + @Before + public void setUp() { + mAPI = EasyMock.mock(CCBAPI.class); + verifier = new CCBUserVerifier(mAPI); + + mClientInfo = new ClientInfo(); + mMockRequest = EasyMock.mock(Request.class); + EasyMock.expect(mMockRequest.getClientInfo()).andReturn(mClientInfo).anyTimes(); + + mMockResponse = EasyMock.mock(Response.class); + + mProfile.setId(48); + mProfile.setFirstName("Larry"); + mProfile.setLastName("Bob"); + mProfile.setEmail("larry.bob@example.com"); + } + + private void replay() { + EasyMock.replay(mAPI, mMockRequest, mMockResponse); + } + + private void verify() { + EasyMock.verify(mAPI, mMockRequest, mMockResponse); + } + + + @Test + public void testVerifyNoCredentials() throws Exception { + // Prepare mocks + EasyMock.expect(mMockRequest.getChallengeResponse()).andReturn(null).anyTimes(); + replay(); + + // Test + int result = verifier.verify(mMockRequest, mMockResponse); + + // Verify + verify(); + assertEquals(Verifier.RESULT_MISSING, result); + assertNull(mClientInfo.getUser()); + } + + @Test + public void testVerifyAuthFailure() throws Exception { + // Prepare mocks + ChallengeResponse challenge = new ChallengeResponse(ChallengeScheme.HTTP_BASIC, "user", "pass"); + EasyMock.expect(mMockRequest.getChallengeResponse()).andReturn(challenge).anyTimes(); + GetIndividualProfilesResponse response = new GetIndividualProfilesResponse(); + response.setIndividuals(Collections.<IndividualProfile>emptyList()); + EasyMock.expect(mAPI.getIndividualProfiles(new GetIndividualProfilesRequest() + .withLoginPassword("user", "pass".toCharArray()))).andReturn(response); + replay(); + + // Test + int result = verifier.verify(mMockRequest, mMockResponse); + + // Verify + verify(); + assertEquals(Verifier.RESULT_INVALID, result); + assertNull(mClientInfo.getUser()); + } + + @Test + public void testVerifyAuthException() throws Exception { + // Prepare mocks + ChallengeResponse challenge = new ChallengeResponse(ChallengeScheme.HTTP_BASIC, "user", "pass"); + EasyMock.expect(mMockRequest.getChallengeResponse()).andReturn(challenge).anyTimes(); + EasyMock.expect(mAPI.getIndividualProfiles(EasyMock.anyObject(GetIndividualProfilesRequest.class))) + .andThrow(new IOException()); + replay(); + + // Test + int result = verifier.verify(mMockRequest, mMockResponse); + + // Verify + verify(); + assertEquals(Verifier.RESULT_INVALID, result); + assertNull(mClientInfo.getUser()); + } + + @Test + public void testVerifyAuthSuccess() throws Exception { + // Prepare mocks + ChallengeResponse challenge = new ChallengeResponse(ChallengeScheme.HTTP_BASIC, "user", "pass"); + EasyMock.expect(mMockRequest.getChallengeResponse()).andReturn(challenge).anyTimes(); + GetIndividualProfilesResponse response = new GetIndividualProfilesResponse(); + response.setIndividuals(Collections.singletonList(mProfile)); + EasyMock.expect(mAPI.getIndividualProfiles(new GetIndividualProfilesRequest() + .withLoginPassword("user", "pass".toCharArray()))).andReturn(response); + + replay(); + + // Test + int result = verifier.verify(mMockRequest, mMockResponse); + + // Verify + verify(); + assertEquals(Verifier.RESULT_VALID, result); + assertNotNull(mClientInfo.getUser()); + assertEquals("CCB-48", mClientInfo.getUser().getIdentifier()); + assertEquals("Larry", mClientInfo.getUser().getFirstName()); + assertEquals("Bob", mClientInfo.getUser().getLastName()); + assertEquals("larry.bob@example.com", mClientInfo.getUser().getEmail()); + } +}
\ No newline at end of file diff --git a/src/test/java/com/p4square/grow/ccb/CustomFieldCacheTest.java b/src/test/java/com/p4square/grow/ccb/CustomFieldCacheTest.java new file mode 100644 index 0000000..bcfd260 --- /dev/null +++ b/src/test/java/com/p4square/grow/ccb/CustomFieldCacheTest.java @@ -0,0 +1,241 @@ +package com.p4square.grow.ccb; + +import com.p4square.ccbapi.CCBAPI; +import com.p4square.ccbapi.model.*; +import org.easymock.Capture; +import org.easymock.EasyMock; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.util.Arrays; + +import static org.junit.Assert.*; + +/** + * Tests for the CustomFieldCache. + */ +public class CustomFieldCacheTest { + + private CustomFieldCache cache; + + private CCBAPI api; + private GetCustomFieldLabelsResponse customFieldsResponse; + private GetLookupTableResponse lookupTableResponse; + + @Before + public void setUp() { + api = EasyMock.mock(CCBAPI.class); + cache = new CustomFieldCache(api); + + // Prepare some custom fields for the test. + CustomField textField = new CustomField(); + textField.setName("udf_ind_text_6"); + textField.setLabel("Grow Level"); + + CustomField dateField = new CustomField(); + dateField.setName("udf_ind_date_6"); + dateField.setLabel("Grow Level"); + + CustomField pullDown = new CustomField(); + pullDown.setName("udf_ind_pulldown_6"); + pullDown.setLabel("Grow Level"); + + customFieldsResponse = new GetCustomFieldLabelsResponse(); + customFieldsResponse.setCustomFields(Arrays.asList(textField, dateField, pullDown)); + + // Prepare some pulldown items for the tests. + LookupTableItem seeker = new LookupTableItem(); + seeker.setId(1); + seeker.setOrder(1); + seeker.setName("Seeker"); + + LookupTableItem believer = new LookupTableItem(); + believer.setId(2); + believer.setOrder(2); + believer.setName("Believer"); + + lookupTableResponse = new GetLookupTableResponse(); + lookupTableResponse.setItems(Arrays.asList(seeker, believer)); + } + + @Test + public void testGetTextFieldByLabel() throws Exception { + // Setup mocks + EasyMock.expect(api.getCustomFieldLabels()).andReturn(customFieldsResponse); + EasyMock.replay(api); + + // Test the cache + CustomField field = cache.getTextFieldByLabel("Grow Level"); + + // Verify result. + EasyMock.verify(api); + assertEquals("udf_ind_text_6", field.getName()); + assertEquals("Grow Level", field.getLabel()); + } + + @Test + public void testGetDateFieldByLabel() throws Exception { + // Setup mocks + EasyMock.expect(api.getCustomFieldLabels()).andReturn(customFieldsResponse); + EasyMock.replay(api); + + // Test the cache + CustomField field = cache.getDateFieldByLabel("Grow Level"); + + // Verify result. + EasyMock.verify(api); + assertEquals("udf_ind_date_6", field.getName()); + assertEquals("Grow Level", field.getLabel()); + } + + @Test + public void testGetPullDownFieldByLabel() throws Exception { + // Setup mocks + EasyMock.expect(api.getCustomFieldLabels()).andReturn(customFieldsResponse); + EasyMock.replay(api); + + // Test the cache + CustomField field = cache.getIndividualPulldownByLabel("Grow Level"); + + // Verify result. + EasyMock.verify(api); + assertEquals("udf_ind_pulldown_6", field.getName()); + assertEquals("Grow Level", field.getLabel()); + } + + @Test + public void testGetPullDownFieldByLabelMissing() throws Exception { + // Setup mocks + EasyMock.expect(api.getCustomFieldLabels()).andReturn(customFieldsResponse); + EasyMock.replay(api); + + // Test the cache + CustomField field = cache.getIndividualPulldownByLabel("Missing Label"); + + // Verify result. + EasyMock.verify(api); + assertNull(field); + } + + @Test + public void testGetPullDownFieldByLabelException() throws Exception { + // Setup mocks + EasyMock.expect(api.getCustomFieldLabels()).andThrow(new IOException()); + EasyMock.expect(api.getCustomFieldLabels()).andReturn(customFieldsResponse); + EasyMock.replay(api); + + // Test the cache + CustomField field1 = cache.getIndividualPulldownByLabel("Grow Level"); + CustomField field2 = cache.getIndividualPulldownByLabel("Grow Level"); + + // Verify result. + EasyMock.verify(api); + assertNull(field1); + assertNotNull(field2); + } + + @Test + public void testGetMultipleFields() throws Exception { + // Setup mocks + // Note: only one API call. + EasyMock.expect(api.getCustomFieldLabels()).andReturn(customFieldsResponse); + EasyMock.replay(api); + + // Test the cache + CustomField field1 = cache.getTextFieldByLabel("Grow Level"); + CustomField field2 = cache.getIndividualPulldownByLabel("Grow Level"); + + // Verify result. + EasyMock.verify(api); + assertEquals("udf_ind_text_6", field1.getName()); + assertEquals("Grow Level", field1.getLabel()); + assertEquals("udf_ind_pulldown_6", field2.getName()); + assertEquals("Grow Level", field2.getLabel()); + } + + @Test + public void testGetPullDownOptions() throws Exception { + // Setup mocks + Capture<GetLookupTableRequest> requestCapture = EasyMock.newCapture(); + EasyMock.expect(api.getLookupTable(EasyMock.capture(requestCapture))).andReturn(lookupTableResponse); + EasyMock.replay(api); + + // Test the cache + LookupTableItem item = cache.getPulldownItemByName( + LookupTableType.valueOf("udf_ind_pulldown_6".toUpperCase()), + "Believer"); + + // Verify result. + EasyMock.verify(api); + assertEquals(LookupTableType.UDF_IND_PULLDOWN_6, requestCapture.getValue().getType()); + assertEquals(2, item.getId()); + assertEquals(2, item.getOrder()); + assertEquals("Believer", item.getName()); + } + + @Test + public void testGetPullDownOptionsMixedCase() throws Exception { + // Setup mocks + Capture<GetLookupTableRequest> requestCapture = EasyMock.newCapture(); + EasyMock.expect(api.getLookupTable(EasyMock.capture(requestCapture))).andReturn(lookupTableResponse); + EasyMock.replay(api); + + // Test the cache + LookupTableItem item = cache.getPulldownItemByName( + LookupTableType.valueOf("udf_ind_pulldown_6".toUpperCase()), + "BeLiEvEr"); + + // Verify result. + EasyMock.verify(api); + assertEquals(LookupTableType.UDF_IND_PULLDOWN_6, requestCapture.getValue().getType()); + assertEquals(2, item.getId()); + assertEquals(2, item.getOrder()); + assertEquals("Believer", item.getName()); + } + + @Test + public void testGetPullDownOptionMissing() throws Exception { + // Setup mocks + EasyMock.expect(api.getLookupTable(EasyMock.anyObject())).andReturn(lookupTableResponse); + EasyMock.replay(api); + + // Test the cache + LookupTableItem item = cache.getPulldownItemByName(LookupTableType.UDF_IND_PULLDOWN_6, "Something else"); + + // Verify result. + EasyMock.verify(api); + assertNull(item); + } + + @Test + public void testGetPullDownMissing() throws Exception { + // Setup mocks + EasyMock.expect(api.getLookupTable(EasyMock.anyObject())).andReturn(new GetLookupTableResponse()); + EasyMock.replay(api); + + // Test the cache + LookupTableItem item = cache.getPulldownItemByName(LookupTableType.UDF_IND_PULLDOWN_6, "Believer"); + + // Verify result. + EasyMock.verify(api); + assertNull(item); + } + + @Test + public void testGetPullDownException() throws Exception { + // Setup mocks + EasyMock.expect(api.getLookupTable(EasyMock.anyObject())).andThrow(new IOException()); + EasyMock.expect(api.getLookupTable(EasyMock.anyObject())).andReturn(lookupTableResponse); + EasyMock.replay(api); + + // Test the cache + LookupTableItem item1 = cache.getPulldownItemByName(LookupTableType.UDF_IND_PULLDOWN_6, "Believer"); + LookupTableItem item2 = cache.getPulldownItemByName(LookupTableType.UDF_IND_PULLDOWN_6, "Believer"); + + // Verify result. + EasyMock.verify(api); + assertNull(item1); + assertNotNull(item2); + } +}
\ No newline at end of file diff --git a/src/test/java/com/p4square/grow/config/ConfigTest.java b/src/test/java/com/p4square/grow/config/ConfigTest.java new file mode 100644 index 0000000..ccb39da --- /dev/null +++ b/src/test/java/com/p4square/grow/config/ConfigTest.java @@ -0,0 +1,62 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.config; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class ConfigTest { + public static void main(String... args) { + org.junit.runner.JUnitCore.main(ConfigTest.class.getName()); + } + + @Test + public void basicTest() throws Exception { + // Load a config file + Config domain1 = new Config(); + domain1.setDomain("domain1"); + Config domain2 = new Config(); + domain2.setDomain("domain2"); + + domain1.updateConfig(getClass().getResourceAsStream("ConfigTest.properties")); + domain2.updateConfig(getClass().getResourceAsStream("ConfigTest.properties")); + + // Non-existent key returns default + assertEquals("default", domain1.getString("doesNotExist", "default")); + assertSame(null, domain1.getString("doesNotExist")); + + // Domain keys return different values for different domains + assertEquals("domain1Value", domain1.getString("domainSpecific")); + assertEquals("domain2Value", domain2.getString("domainSpecific")); + + // Domain key takes priority over *. key + assertEquals("domain1Value", domain1.getString("onlyInDomain1")); + assertEquals("wildValue", domain2.getString("onlyInDomain1")); + + // Wildcard domain returns value + assertEquals("wildValue", domain1.getString("wildcardOnly")); + + // Empty value gives empty string + assertEquals("", domain1.getString("emptyValue")); + + // Number is returned + assertEquals(5, domain1.getInt("number")); + + // Non number test + assertEquals(Integer.MIN_VALUE, domain1.getInt("notANumber")); + + // Test Boolean values + assertTrue(domain1.getBoolean("boolean1")); + assertTrue(domain1.getBoolean("boolean2")); + assertFalse(domain1.getBoolean("boolean3")); + assertFalse(domain1.getBoolean("notABool")); + assertTrue(domain1.getBoolean("notABool", true)); + } +} 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()); + } +} |