diff --git a/src/main/kotlin/bandcampcollectiondownloader/BandcampCollectionDownloader.kt b/src/main/kotlin/bandcampcollectiondownloader/BandcampCollectionDownloader.kt index 16ceb07..5f06568 100644 --- a/src/main/kotlin/bandcampcollectiondownloader/BandcampCollectionDownloader.kt +++ b/src/main/kotlin/bandcampcollectiondownloader/BandcampCollectionDownloader.kt @@ -1,6 +1,7 @@ package bandcampcollectiondownloader import com.google.gson.Gson +import com.google.gson.JsonSyntaxException import com.google.gson.annotations.SerializedName import org.jsoup.Jsoup import org.zeroturnaround.zip.ZipUtil @@ -42,6 +43,12 @@ data class ParsedStatDownload( fun parsedCookiesToMap(parsedCookies: Array): Map { val result = HashMap() for (parsedCookie in parsedCookies) { + if (parsedCookie.contentRaw == null) { + throw BandCampDownloaderError("Missing 'Content raw' field in cookie number ${parsedCookies.indexOf(parsedCookie) + 1}.") + } + if (parsedCookie.nameRaw == null) { + throw BandCampDownloaderError("Missing 'Name raw' field in cookie number ${parsedCookies.indexOf(parsedCookie) + 1}.") + } result.put(parsedCookie.nameRaw, parsedCookie.contentRaw) } return result @@ -109,7 +116,12 @@ fun downloadAll(cookiesFile: Path, bandcampUser: String, downloadFormat: String, throw BandCampDownloaderError("Cookies file '$cookiesFile' cannot be found.") } val jsonData = String(Files.readAllBytes(cookiesFile)) - val parsedCookies = gson.fromJson(jsonData, Array::class.java) + val parsedCookies = + try { + gson.fromJson(jsonData, Array::class.java) + } catch (e: JsonSyntaxException) { + throw BandCampDownloaderError("Cookies file '$cookiesFile' is not well formed: ${e.message}") + } val cookies = parsedCookiesToMap(parsedCookies) // Get collection page with cookies, hence with download links diff --git a/src/test/kotlin/bandcampcollectiodownloader/test/BandcampCollectionDownloaderTests.kt b/src/test/kotlin/bandcampcollectiodownloader/test/BandcampCollectionDownloaderTests.kt new file mode 100644 index 0000000..3034641 --- /dev/null +++ b/src/test/kotlin/bandcampcollectiodownloader/test/BandcampCollectionDownloaderTests.kt @@ -0,0 +1,42 @@ +package bandcampcollectiodownloader.test + +import bandcampcollectiondownloader.BandCampDownloaderError +import bandcampcollectiondownloader.downloadAll +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import java.nio.file.Paths + +/** + * Note: bli is a valid bandcamp user (completely randomly chosen), + * but for which we have no credentials (ie. valid cookies). + */ +class BandcampCollectionDownloaderTests { + + @Test + fun testErrorCookiesFileNotFound() { + assertThrows { + downloadAll(Paths.get("bli"),"bli","bli", Paths.get("bli")) + } + } + + @Test + fun testErrorCookiesFileInvalidJson() { + assertThrows { + downloadAll(Paths.get("./test-data/notjsoncookies.json"),"bli","bli", Paths.get("bli")) + } + } + + @Test + fun testErrorCookiesFileInvalidContent_wrongkey() { + //assertThrows { + downloadAll(Paths.get("./test-data/invalidcookies_wrongkeys.json"),"bli","bli", Paths.get("bli")) + //} + } + + @Test + fun testErrorCookiesFileInvalidContent_noarray() { + //assertThrows { + downloadAll(Paths.get("./test-data/invalidcookies_noarray.json"),"bli","bli", Paths.get("bli")) + //} + } +} \ No newline at end of file diff --git a/test-data/invalidcookies_noarray.json b/test-data/invalidcookies_noarray.json new file mode 100644 index 0000000..a55db6e --- /dev/null +++ b/test-data/invalidcookies_noarray.json @@ -0,0 +1,3 @@ +{ + "hello" : 123 +} \ No newline at end of file diff --git a/test-data/invalidcookies_wrongkeys.json b/test-data/invalidcookies_wrongkeys.json new file mode 100644 index 0000000..33c01d0 --- /dev/null +++ b/test-data/invalidcookies_wrongkeys.json @@ -0,0 +1,3 @@ +[{ + "hello" : 123 +}] \ No newline at end of file diff --git a/test-data/notjsoncookies.json b/test-data/notjsoncookies.json new file mode 100644 index 0000000..7a8fb35 --- /dev/null +++ b/test-data/notjsoncookies.json @@ -0,0 +1 @@ +Nope, not json. \ No newline at end of file