Index: lams_monitoring/test/java/org/lamsfoundation/lams/monitoring/service/TestMonitoringService.java =================================================================== diff -u -rf7b2dd6bd944a7f6286aeb748e999020b7b01a9b -r7004e007a05141f613649b0833e32a9989d04eda --- lams_monitoring/test/java/org/lamsfoundation/lams/monitoring/service/TestMonitoringService.java (.../TestMonitoringService.java) (revision f7b2dd6bd944a7f6286aeb748e999020b7b01a9b) +++ lams_monitoring/test/java/org/lamsfoundation/lams/monitoring/service/TestMonitoringService.java (.../TestMonitoringService.java) (revision 7004e007a05141f613649b0833e32a9989d04eda) @@ -23,6 +23,8 @@ import java.io.IOException; import java.util.LinkedList; +import javax.sql.DataSource; + import org.lamsfoundation.lams.AbstractLamsTestCase; import org.lamsfoundation.lams.learningdesign.Activity; import org.lamsfoundation.lams.learningdesign.GateActivity; @@ -36,6 +38,7 @@ import org.lamsfoundation.lams.usermanagement.Organisation; import org.lamsfoundation.lams.usermanagement.User; import org.lamsfoundation.lams.usermanagement.service.IUserManagementService; +import org.springframework.jdbc.core.JdbcTemplate; /** @@ -66,7 +69,9 @@ private final long TEST_COPIED_LEARNING_DESIGN_ID = 2; private final Integer TEST_ORGANIZATION_ID = new Integer(1); private final Long TEST_SCHEDULE_GATE_ID = new Long(27); - + private final Long TEST_LESSION_ID = new Long(1); // "Test_Lesson" from insert_test_data script + private final Integer MELCOE_WORKSPACE = new Integer(3); // MELCOE workspace from insert_test_data script + //it might be different because it is automatically generated by database //TODO create a get lesson by design method in lesson dao. private static Long TEST_LESSON_ID = null; @@ -77,6 +82,9 @@ private User testStaff; private User testLearner; private Organisation testOrganisation; + + DataSource dataSource; + /* * @see AbstractLamsCommonTestCase#setUp() */ @@ -87,7 +95,8 @@ usermanageService = (IUserManagementService)this.context.getBean("userManagementService"); lessonDao = (LessonDAO)this.context.getBean("lessonDAO"); activityDao = (ActivityDAO)this.context.getBean("activityDAO"); - + dataSource = (DataSource) this.context.getBean("dataSource"); + initializeTestingData(); } /** @@ -96,6 +105,7 @@ protected void tearDown() throws Exception { super.tearDown(); + context = null; // try to trigger some memory to get freed - running out of memory. } /** @@ -131,21 +141,34 @@ public void testInitializeLesson() { + String lamsLearningDesignTableName = "lams_learning_design"; + String idName = "learning_design_id"; + long previousLDId = getMaxId(lamsLearningDesignTableName, idName); + Lesson testLesson = monitoringService.initializeLesson("Test_Lesson", "Test_Description", TEST_LEARNING_DESIGN_ID, testUser); TEST_LESSON_ID=testLesson.getLessonId(); Lesson createdLesson = lessonDao.getLesson(TEST_LESSON_ID); assertNotNull(createdLesson); - assertEquals("verify the design",TEST_COPIED_LEARNING_DESIGN_ID,createdLesson.getLearningDesign().getLearningDesignId().longValue()); + + // can't reliably predict the id of the copied learning design - depends on what + // has happened before the test and what might be running at the same time. + // so compare it to the last id created in this table. This will usually be the + // right value as it it looked up using getLastInsertId() and that works + // on per connection basis - it is unlikely that this test case will be sharing + // the connection with anything else that will update this table. This may + // change in the future if more test cases are added. + long mostRecentLDId = getMaxId(lamsLearningDesignTableName, idName); + long copiedLDId = testLesson.getLearningDesign().getLearningDesignId().longValue(); + assertTrue("A learning design has been created (previous insert #previousLDId, recent insert #mostRecentLDId", + previousLDId!=mostRecentLDId); + assertTrue("Copied learning design is the new id", copiedLDId == mostRecentLDId); + assertEquals("verify the user", TEST_USER_ID,createdLesson.getUser().getUserId()); assertEquals("verify the lesson state",Lesson.CREATED,createdLesson.getLessonStateId()); - - - //createdLesson.getLessonClass().getGroups().clear(); - //lessonDao.deleteLesson(createdLesson); - } + } public void testCreateLessonClassForLesson() { @@ -205,35 +228,39 @@ System.out.print(packet); } public void testGetLessonDetails() throws IOException{ - String packet = monitoringService.getLessonDetails(new Long(1)); + String packet = monitoringService.getLessonDetails(TEST_LESSION_ID); System.out.print(packet); } public void testGetLessonLearners() throws IOException{ - String packet = monitoringService.getLessonLearners(new Long(1)); + String packet = monitoringService.getLessonLearners(TEST_LESSION_ID); System.out.println(packet); } public void testGetLessonDesign()throws IOException{ - String packet = monitoringService.getLearningDesignDetails(new Long(1)); + String packet = monitoringService.getLearningDesignDetails(TEST_LESSION_ID); System.out.println(packet); } public void testGetAllLearnersProgress() throws IOException{ - String packet = monitoringService.getAllLearnersProgress(new Long(1)); + String packet = monitoringService.getAllLearnersProgress(TEST_LESSION_ID); System.out.println(packet); } public void testGetLearnerActivityURL() throws Exception{ - String packet = monitoringService.getLearnerActivityURL(new Long(29),new Integer(2)); + String packet = monitoringService.getLearnerActivityURL(new Long(29),TEST_LEARNER_ID); System.out.println(packet); } public void testGellAllContributeActivities()throws IOException{ - String packet = monitoringService.getAllContributeActivities(new Long(1)); + String packet = monitoringService.getAllContributeActivities(TEST_LESSION_ID); System.out.println(packet); } + public void testMoveLesson() throws IOException{ - String packet = monitoringService.moveLesson(new Long(1),new Integer(3),new Integer(1)); + String packet = monitoringService.moveLesson(TEST_LESSION_ID,MELCOE_WORKSPACE,TEST_USER_ID); System.out.println(packet); } public void testRenameLesson()throws IOException{ - String packet = monitoringService.renameLesson(new Long(1),"New name after renaming",new Integer(1)); + String newName = "New name after renaming"; + String packet = monitoringService.renameLesson(TEST_LESSION_ID,newName,TEST_USER_ID); + Lesson updatedLesson = lessonDao.getLesson(TEST_LESSION_ID); + assertEquals("Name changed correctly", newName, updatedLesson.getLessonName()); System.out.println(packet); } /** @@ -275,4 +302,15 @@ } + /** Get the largest id that was last inserted into a table. This will normally be the last + * id inserted. Why not use LAST_INSERT_ID() - that works per connection and there is + * no guarantee that this test case is running using the same connection as the + * service. + */ + private long getMaxId(String tablename, String idname) + { + JdbcTemplate jt = new JdbcTemplate(dataSource); + return jt.queryForLong("SELECT max("+idname+") FROM "+tablename); + } + }