@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")); }