@Test public void constructorWithoutPathUsesTmcSettings() throws TmcCoreException { String path = "pentti/tmc/java"; settings.setCurrentCourse(new Course()); settings.setTmcMainDirectory(path); DownloadExercises de = new DownloadExercises(new ArrayList<Exercise>(), settings); assertTrue(de.data.containsKey("path")); assertEquals(path, de.data.get("path")); }
@Test public void overwritesToCacheFileIfCacheFileHasBadContents() throws IOException, TmcCoreException { new FileWriter(cache).write(" asdfjljlkasdf "); ExerciseDownloader downloader = Mockito.mock(ExerciseDownloader.class); Mockito.when(downloader.createCourseFolder(anyString(), anyString())).thenReturn(""); Mockito.when(downloader.handleSingleExercise(any(Exercise.class), anyString())) .thenReturn(true); Course course = new Course(); course.setName("test-course"); course.setExercises( new ExerciseBuilder() .withExercise("kissa", 2, "eujwuc") .withExercise("asdf", 793, "alnwnec") .withExercise("ankka", 88, "abcdefg") .build()); parser = Mockito.mock(TmcJsonParser.class); Mockito.when(parser.getCourse(anyInt())).thenReturn(Optional.of(course)); DownloadExercises dl = new DownloadExercises(downloader, "", "8", cache, settings, parser); dl.call(); String json = FileUtils.readFileToString(cache); Gson gson = new Gson(); Map<String, Map<String, String>> checksums; Type typeOfHashMap = new TypeToken<Map<String, Map<String, String>>>() {}.getType(); checksums = gson.fromJson(json, typeOfHashMap); assertNotNull(checksums); assertTrue(checksums.containsKey("test-course")); assertTrue(checksums.get("test-course").containsKey("kissa")); assertTrue(checksums.get("test-course").containsKey("asdf")); assertTrue(checksums.get("test-course").containsKey("ankka")); assertEquals("eujwuc", checksums.get("test-course").get("kissa")); assertEquals("alnwnec", checksums.get("test-course").get("asdf")); assertEquals("abcdefg", checksums.get("test-course").get("ankka")); }
@Test public void keepsOldChecksumsInTheCache() throws IOException, TmcCoreException { try (FileWriter writer = new FileWriter(cache)) { writer.write( "{\"test-course\":{\"kissa\":\"qwerty\",\"asdf2\":\"aijw9\"},\"test-course2\":{\"ankka\":\"22222\"}}"); } ExerciseDownloader mock = Mockito.mock(ExerciseDownloader.class); Mockito.when(mock.createCourseFolder(anyString(), anyString())).thenReturn(""); Mockito.when(mock.handleSingleExercise(any(Exercise.class), anyString())).thenReturn(true); Course course = new Course(); course.setName("test-course"); course.setExercises( new ExerciseBuilder() .withExercise("kissa", 2, "eujwuc") .withExercise("asdf", 793, "alnwnec") .withExercise("ankka", 88, "abcdefg") .build()); parser = Mockito.mock(TmcJsonParser.class); Mockito.when(parser.getCourse(anyInt())).thenReturn(Optional.of(course)); DownloadExercises dl = new DownloadExercises(mock, "", "8", cache, settings, parser); dl.call(); String json = FileUtils.readFileToString(cache); Type typeOfHashMap = new TypeToken<Map<String, Map<String, String>>>() {}.getType(); Map<String, Map<String, String>> checksums = new Gson().fromJson(json, typeOfHashMap); assertNotNull(checksums); assertTrue(checksums.containsKey("test-course")); assertTrue(checksums.containsKey("test-course2")); assertTrue(checksums.get("test-course").containsKey("kissa")); assertTrue(checksums.get("test-course").containsKey("asdf")); assertTrue(checksums.get("test-course").containsKey("ankka")); assertEquals("eujwuc", checksums.get("test-course").get("kissa")); assertEquals("alnwnec", checksums.get("test-course").get("asdf")); assertEquals("aijw9", checksums.get("test-course").get("asdf2")); assertEquals("22222", checksums.get("test-course2").get("ankka")); }
@Test public void testDowloadingWithProgress() throws Exception { CoreTestSettings settings1 = createSettingsAndWiremock(); ProgressObserver observerMock = Mockito.mock(ProgressObserver.class); String folder = System.getProperty("user.dir") + "/testResources/"; ListenableFuture<List<Exercise>> download = core.downloadExercises(folder, "35", settings1, observerMock); List<Exercise> exercises = download.get(); String exercisePath = folder + "2013_ohpeJaOhja/viikko1/Viikko1_001.Nimi"; assertEquals(exercises.size(), 153); assertTrue(new File(exercisePath).exists()); FileUtils.deleteDirectory(new File(exercisePath)); assertFalse(new File(exercisePath).exists()); Mockito.verify(observerMock, times(153)).progress(anyDouble(), anyString()); }
@Test public void downloadAllExercises() throws Exception { CoreTestSettings settings1 = createSettingsAndWiremock(); String folder = System.getProperty("user.dir") + "/testResources/"; ListenableFuture<List<Exercise>> download = core.downloadExercises(folder, "35", settings1, null); List<Exercise> exercises = download.get(); String exercisePath = folder + "2013_ohpeJaOhja/viikko1/Viikko1_001.Nimi"; assertEquals(exercises.size(), 153); assertTrue(new File(exercisePath).exists()); FileUtils.deleteDirectory(new File(exercisePath)); assertFalse(new File(exercisePath).exists()); }