|
1 | 1 | package fi.helsinki.cs.tmc.cli.command; |
2 | 2 |
|
| 3 | +import static org.junit.Assert.assertEquals; |
3 | 4 | import static org.junit.Assert.assertTrue; |
4 | 5 | import static org.mockito.Matchers.any; |
5 | 6 | import static org.mockito.Matchers.anyListOf; |
|
9 | 10 | import static org.mockito.Mockito.spy; |
10 | 11 | import static org.mockito.Mockito.when; |
11 | 12 | import static org.powermock.api.mockito.PowerMockito.mockStatic; |
| 13 | +import static org.powermock.api.mockito.PowerMockito.verifyStatic; |
12 | 14 |
|
13 | 15 | import fi.helsinki.cs.tmc.cli.Application; |
14 | 16 | import fi.helsinki.cs.tmc.cli.CliContext; |
|
27 | 29 |
|
28 | 30 | import org.junit.After; |
29 | 31 | import org.junit.Before; |
30 | | -import org.junit.Ignore; |
31 | 32 | import org.junit.Test; |
32 | 33 | import org.junit.runner.RunWith; |
| 34 | +import org.mockito.ArgumentCaptor; |
33 | 35 | import org.powermock.core.classloader.annotations.PrepareForTest; |
34 | 36 | import org.powermock.modules.junit4.PowerMockRunner; |
| 37 | +import org.powermock.reflect.Whitebox; |
35 | 38 |
|
36 | 39 | import java.io.File; |
37 | 40 | import java.io.IOException; |
@@ -205,19 +208,68 @@ public void failsToLoadExercises() throws ParseException { |
205 | 208 | io.assertContains("and of which 1 failed."); |
206 | 209 | } |
207 | 210 |
|
208 | | - @Ignore |
209 | 211 | @Test |
210 | 212 | public void findFromMultipleServer() { |
211 | | - List<Course> list1 = Arrays.asList(new Course("course1")); |
212 | | - List<Course> list2 = Arrays.asList(new Course("course2")); |
213 | | - when(TmcUtil.listCourses(eq(ctx))).thenReturn(list1).thenReturn(list2); |
| 213 | + Settings settings1 = new Settings("http://test.test", "", ""); |
| 214 | + Settings settings2 = new Settings("http://hello.test", "", ""); |
| 215 | + |
| 216 | + when(TmcUtil.findCourse(eq(ctx), eq("course1"))).thenReturn(new Course("course1")) |
| 217 | + .thenReturn(new Course("course2")); |
| 218 | + when(SettingsIo.getSettingsList()).thenReturn( |
| 219 | + Arrays.asList(settings1, settings2)); |
| 220 | + |
| 221 | + String[] args = {"download", "course2"}; |
| 222 | + app.run(args); |
214 | 223 | } |
215 | 224 |
|
216 | | - @Ignore |
217 | 225 | @Test |
218 | | - public void findFromMultipleServerWithSameName() { |
219 | | - List<Course> list1 = Arrays.asList(new Course("course1")); |
220 | | - List<Course> list2 = Arrays.asList(new Course("course1")); |
221 | | - when(TmcUtil.listCourses(eq(ctx))).thenReturn(list1).thenReturn(list2); |
| 226 | + public void findFromMultipleServerWithSameNameWithoutTakingAny() { |
| 227 | + Settings settings1 = new Settings("http://test.test", "abc", ""); |
| 228 | + Settings settings2 = new Settings("http://hello.test", "def", ""); |
| 229 | + |
| 230 | + when(TmcUtil.findCourse(eq(ctx), eq("course1"))).thenReturn(new Course("course1")) |
| 231 | + .thenReturn(new Course("course1")); |
| 232 | + when(SettingsIo.getSettingsList()).thenReturn( |
| 233 | + Arrays.asList(settings1, settings2)); |
| 234 | + |
| 235 | + List<Exercise> exercises = Arrays.asList(); |
| 236 | + when(TmcUtil.downloadExercises(eq(ctx), anyListOf(Exercise.class), |
| 237 | + any(ProgressObserver.class))).thenReturn(exercises); |
| 238 | + |
| 239 | + String[] args = {"download", "course1"}; |
| 240 | + io.addConfirmationPrompt(false); |
| 241 | + io.addConfirmationPrompt(false); |
| 242 | + app.run(args); |
| 243 | + io.assertContains("There is 2 courses with same name at different servers"); |
| 244 | + io.assertContains("Download course from http://test.test with 'abc' account"); |
| 245 | + io.assertContains("Download course from http://hello.test with 'def' account"); |
| 246 | + io.assertContains("The previous course was last that matched"); |
| 247 | + io.assertAllPromptsUsed(); |
| 248 | + } |
| 249 | + |
| 250 | + @Test |
| 251 | + public void findFromMultipleServerWithSameNameWithTakingFirst() { |
| 252 | + Settings settings1 = new Settings("http://test.test", "abc", ""); |
| 253 | + Settings settings2 = new Settings("http://hello.test", "def", ""); |
| 254 | + |
| 255 | + when(TmcUtil.findCourse(eq(ctx), eq("course1"))).thenReturn(new Course("course1")) |
| 256 | + .thenReturn(new Course("course1")); |
| 257 | + when(SettingsIo.getSettingsList()).thenReturn( |
| 258 | + Arrays.asList(settings1, settings2)); |
| 259 | + |
| 260 | + String[] args = {"download", "course1"}; |
| 261 | + io.addConfirmationPrompt(true); |
| 262 | + app.run(args); |
| 263 | + io.assertContains("There is 2 courses with same name at different servers"); |
| 264 | + io.assertContains("Download course from http://test.test with 'abc' account"); |
| 265 | + io.assertAllPromptsUsed(); |
| 266 | + |
| 267 | + ArgumentCaptor<CliContext> ctxCaptor = ArgumentCaptor.forClass(CliContext.class); |
| 268 | + verifyStatic(); |
| 269 | + TmcUtil.downloadExercises(ctxCaptor.capture(), anyListOf(Exercise.class), |
| 270 | + any(ProgressObserver.class)); |
| 271 | + |
| 272 | + Settings usedSettings = Whitebox.getInternalState(ctx, "settings"); |
| 273 | + assertEquals(usedSettings, settings1); |
222 | 274 | } |
223 | 275 | } |
0 commit comments