1313import fi .helsinki .cs .tmc .cli .io .TestIo ;
1414import fi .helsinki .cs .tmc .cli .tmcstuff .WorkDir ;
1515import fi .helsinki .cs .tmc .core .TmcCore ;
16-
16+ import fi . helsinki . cs . tmc . core . commands . GetUpdatableExercises ;
1717import fi .helsinki .cs .tmc .core .domain .Course ;
1818import fi .helsinki .cs .tmc .core .domain .Exercise ;
1919import fi .helsinki .cs .tmc .core .domain .ProgressObserver ;
2020
21+ import org .apache .commons .io .FileUtils ;
22+ import org .junit .After ;
2123import org .junit .Before ;
2224import org .junit .BeforeClass ;
25+ import org .junit .Ignore ;
2326import org .junit .Test ;
2427
28+ import java .io .IOException ;
2529import java .nio .file .Path ;
2630import java .nio .file .Paths ;
2731import java .util .ArrayList ;
@@ -33,6 +37,7 @@ public class UpdateCommandTest {
3337 private static final String COURSE_NAME = "2016-aalto-c" ;
3438
3539 static Path pathToDummyCourse ;
40+ static Path tempDir ;
3641
3742 private Application app ;
3843 private TestIo io ;
@@ -44,14 +49,25 @@ public static void setUpClass() throws Exception {
4449 pathToDummyCourse = Paths .get (SubmitCommandTest .class .getClassLoader ()
4550 .getResource ("dummy-courses/" + COURSE_NAME ).toURI ());
4651 assertNotNull (pathToDummyCourse );
52+
53+ tempDir = Paths .get (System .getProperty ("java.io.tmpdir" ))
54+ .resolve ("updateCommandTests" );
55+ assertNotNull (tempDir );
4756 }
4857
4958 @ Before
50- public void setUp () {
59+ public void setUp () throws IOException {
5160 io = new TestIo ();
5261 app = new Application (io );
5362 mockCore = mock (TmcCore .class );
5463 app .setTmcCore (mockCore );
64+
65+ FileUtils .copyDirectory (pathToDummyCourse .toFile (), tempDir .toFile ());
66+ }
67+
68+ @ After
69+ public void tearDown () throws IOException {
70+ FileUtils .deleteDirectory (tempDir .toFile ());
5571 }
5672
5773 @ Test
@@ -82,18 +98,36 @@ public void printsAnErrorMessageIfUsedOutsideCourseDirectory() {
8298
8399 @ Test
84100 public void worksRightIfAllExercisesAreUpToDate () {
85- Callable <List <Exercise >> callableExercise = new Callable <List <Exercise >>() {
86- @ Override
87- public List <Exercise > call () throws Exception {
88- ArrayList <Exercise > tmp = new ArrayList <>();
89- return tmp ;
90- }
91- };
101+ // Callable<List<Exercise>> callableExercise = new Callable<List<Exercise>>() {
102+ // @Override
103+ // public List<Exercise> call() throws Exception {
104+ // ArrayList<Exercise> tmp = new ArrayList<>();
105+ // return tmp;
106+ // }
107+ // };
108+ //
109+ // when(mockCore.getExerciseUpdates(any(ProgressObserver.class), any(Course.class)))
110+ // .thenReturn(callableExercise);
111+
112+ Callable <GetUpdatableExercises .UpdateResult > callableResult
113+ = new Callable <GetUpdatableExercises .UpdateResult >() {
114+
115+ @ Override
116+ public GetUpdatableExercises .UpdateResult call () throws Exception {
117+ GetUpdatableExercises .UpdateResult result = mock (
118+ GetUpdatableExercises .UpdateResult .class );
119+ when (result .getNewExercises ()).thenReturn (
120+ new ArrayList <Exercise >());
121+ when (result .getUpdatedExercises ()).thenReturn (
122+ new ArrayList <Exercise >());
123+ return result ;
124+ }
125+ };
92126
93127 when (mockCore .getExerciseUpdates (any (ProgressObserver .class ), any (Course .class )))
94- .thenReturn (callableExercise );
128+ .thenReturn (callableResult );
95129
96- workDir = new WorkDir (pathToDummyCourse );
130+ workDir = new WorkDir (tempDir );
97131 app .setWorkdir (workDir );
98132
99133 String [] args = {"update" };
@@ -102,27 +136,73 @@ public List<Exercise> call() throws Exception {
102136 }
103137
104138 @ Test
139+ @ Ignore
105140 public void worksRightIfUpdatesAvailable () {
106- Callable <List <Exercise >> callableExercise = new Callable <List <Exercise >>() {
141+ // Callable<List<Exercise>> callableExercise = new Callable<List<Exercise>>() {
142+ // @Override
143+ // public List<Exercise> call() throws Exception {
144+ // ArrayList<Exercise> tmp = new ArrayList<>();
145+ // tmp.add(new Exercise("exercise1"));
146+ // tmp.add(new Exercise("exercise2"));
147+ // return tmp;
148+ // }
149+ // };
150+ //
151+ // when(mockCore.getExerciseUpdates(any(ProgressObserver.class), any(Course.class)))
152+ // .thenReturn(callableExercise);
153+
154+ final List <Exercise > unlockedList = new ArrayList <>();
155+ unlockedList .add (new Exercise ("unlocked_exercise" ));
156+
157+ final List <Exercise > changedList = new ArrayList <>();
158+ unlockedList .add (new Exercise ("Module_1-02_intro" ));
159+
160+ Callable <GetUpdatableExercises .UpdateResult > callableResult
161+ = new Callable <GetUpdatableExercises .UpdateResult >() {
162+ @ Override
163+ public GetUpdatableExercises .UpdateResult call () throws Exception {
164+ GetUpdatableExercises .UpdateResult result = mock (
165+ GetUpdatableExercises .UpdateResult .class );
166+ when (result .getNewExercises ()).thenReturn (unlockedList );
167+ when (result .getUpdatedExercises ()).thenReturn (changedList );
168+ return result ;
169+ }
170+ };
171+ when (mockCore .getExerciseUpdates (any (ProgressObserver .class ), any (Course .class )))
172+ .thenReturn (callableResult );
173+
174+ Callable <List <Course >> callableCourseList = new Callable <List <Course >>() {
107175 @ Override
108- public List <Exercise > call () throws Exception {
109- ArrayList <Exercise > tmp = new ArrayList <>();
110- tmp .add (new Exercise ("exercise1" ));
111- tmp .add (new Exercise ("exercise2" ));
112- return tmp ;
176+ public List <Course > call () throws Exception {
177+ return new ArrayList <>();
113178 }
114179 };
180+ when (mockCore .listCourses (any (ProgressObserver .class ))).thenReturn (callableCourseList );
115181
116- when (mockCore .getExerciseUpdates (any (ProgressObserver .class ), any (Course .class )))
117- .thenReturn (callableExercise );
182+ Callable <Course > callableCourse = new Callable <Course >() {
183+ @ Override
184+ public Course call () throws Exception {
185+ Course course = new Course ("2016-aalto-c" );
186+ List <Exercise > exercises = new ArrayList <>();
187+ exercises .add (new Exercise ("Module_1-02_intro" ));
188+ exercises .add (new Exercise ("unlocked_course" ));
189+ course .setExercises (exercises );
190+ return course ;
191+ }
192+ };
118193
119- workDir = new WorkDir (pathToDummyCourse );
194+ when (mockCore .getCourseDetails (any (ProgressObserver .class ), any (Course .class )))
195+ .thenReturn (callableCourse );
196+
197+ workDir = new WorkDir (tempDir );
120198 app .setWorkdir (workDir );
121199
122200 String [] args = {"update" };
123201 app .run (args );
124- assertTrue (io .getPrint ().contains ("Updates available for:" ));
125- assertTrue (io .getPrint ().contains ("exercise1" ));
126- assertTrue (io .getPrint ().contains ("exercise2" ));
202+ assertTrue (io .getPrint ().contains ("New exercises:" ));
203+ assertTrue (io .getPrint ().contains ("unlocked_exercise" ));
204+
205+ //assertTrue(io.getPrint().contains("Modified exercises:"));
206+ //assertTrue(io.getPrint().contains("Module_1-02_intro"));
127207 }
128208}
0 commit comments