From 4a55fd6956affadf1b317fe409731871126cf573 Mon Sep 17 00:00:00 2001 From: MohitMali Date: Thu, 1 Sep 2022 17:57:50 +0530 Subject: [PATCH 1/2] Convert MetaLinkNetworkEntity.java to kotlin --- .../core/entity/MetaLinkNetworkEntity.java | 124 ------------------ .../core/entity/MetaLinkNetworkEntity.kt | 83 ++++++++++++ .../core/entity/MetaLinkNetworkEntityTest.kt | 84 ++++++------ 3 files changed, 124 insertions(+), 167 deletions(-) delete mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntity.java create mode 100644 core/src/main/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntity.kt diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntity.java b/core/src/main/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntity.java deleted file mode 100644 index 524d61d34..000000000 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntity.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Kiwix Android - * Copyright (c) 2019 Kiwix - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ -package org.kiwix.kiwixmobile.core.entity; - -import java.util.List; -import java.util.Map; -import org.simpleframework.xml.Attribute; -import org.simpleframework.xml.Element; -import org.simpleframework.xml.ElementList; -import org.simpleframework.xml.ElementMap; -import org.simpleframework.xml.Root; -import org.simpleframework.xml.Text; - -@Root(strict = false, name = "metalink") -public class MetaLinkNetworkEntity { - - @Element - public FileElement file; - - public List getUrls() { - return file.urls; - } - - public Url getRelevantUrl() { - return file.urls.get(0); - } - - public FileElement getFile() { - return file; - } - - @Root(strict = false) - public static class FileElement { - @Attribute - public String name; - @ElementList(inline = true, entry = "url") - public List urls; - @Element - private long size; - @ElementMap(entry = "hash", key = "type", attribute = true, inline = true, required = false) - public Map hashes; - @Element(required = false) - public Pieces pieces; - - public String getName() { - return name; - } - - public long getSize() { - return size; - } - - public int getPieceLength() { - return pieces.length; - } - - public String getPieceHashType() { - return pieces.hashType; - } - - public List getPieceHashes() { - return pieces.pieceHashes; - } - - /** - * Get file hash - * - * @param type Hash type as defined in metalink file - * @return Hash value or {@code null} - */ - public String getHash(String type) { - return hashes.get(type); - } - } - - public static class Pieces { - @Attribute - private int length; - - @Attribute(name = "type") - public String hashType; - - @ElementList(inline = true, entry = "hash") - public List pieceHashes; - } - - public static class Url { - @Attribute - public String location; - - @Attribute - private int priority; - - @Text - public String value; - - public String getValue() { - return value; - } - - public String getLocation() { - return location; - } - - public int getPriority() { - return priority; - } - } -} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntity.kt new file mode 100644 index 000000000..8d633d567 --- /dev/null +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntity.kt @@ -0,0 +1,83 @@ +/* + * Kiwix Android + * Copyright (c) 2019 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package org.kiwix.kiwixmobile.core.entity + +import org.simpleframework.xml.Attribute +import org.simpleframework.xml.Element +import org.simpleframework.xml.ElementList +import org.simpleframework.xml.ElementMap +import org.simpleframework.xml.Root +import org.simpleframework.xml.Text + +@Root(strict = false, name = "metalink") +class MetaLinkNetworkEntity { + @Element + var file: FileElement? = null + val urls: List? + get() = file?.urls + val relevantUrl: Url + get() = file?.urls?.get(0) ?: Url() + + @Root(strict = false) + class FileElement { + @Attribute + var name: String? = null + + @ElementList(inline = true, entry = "url") + var urls: List? = null + @Element val size: Long = 0 + + @ElementMap(entry = "hash", key = "type", attribute = true, inline = true, required = false) + var hashes: Map? = null + + @Element(required = false) + var pieces: Pieces? = null + val pieceHashes: List? + get() = pieces?.pieceHashes + + /** + * Get file hash + * + * @param type Hash type as defined in metalink file + * @return Hash value or `null` + */ + fun getHash(type: String): String? = hashes?.get(type) + } + + class Pieces { + @Attribute + val length = 0 + + @Attribute(name = "type") + var hashType: String? = null + + @ElementList(inline = true, entry = "hash") + var pieceHashes: List? = null + } + + class Url { + @Attribute + var location: String? = null + + @Attribute + var priority = 0 + + @Text + var value: String? = null + } +} diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntityTest.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntityTest.kt index ff29feab3..634ac8f6a 100644 --- a/core/src/test/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntityTest.kt +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntityTest.kt @@ -33,65 +33,63 @@ class MetaLinkNetworkEntityTest { "wikipedia_af_all_nopic_2016-05.zim.meta4" ) ) - - MetaLinkNetworkEntityUrlAssert(result.urls).hasItems( - listOf( - DummyUrl( - "us", - 1, - "http://ftpmirror.your.org/pub/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim" - ), - DummyUrl( - "gb", - 2, - "http://www.mirrorservice.org/sites/download.kiwix.org/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim" // ktlint-disable - ), - DummyUrl( - "us", - 3, - "http://download.wikimedia.org/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim" - ), - DummyUrl( - "de", - 4, - "http://mirror.netcologne.de/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim" - ), - DummyUrl( - "fr", - 5, - "http://mirror3.kiwix.org/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim" + result?.urls?.let { + MetaLinkNetworkEntityUrlAssert(it).hasItems( + listOf( + DummyUrl( + "us", + 1, + "http://ftpmirror.your.org/pub/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim" + ), + DummyUrl( + "gb", + 2, + "http://www.mirrorservice.org/sites/download.kiwix.org/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim" // ktlint-disable + ), + DummyUrl( + "us", + 3, + "http://download.wikimedia.org/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim" + ), + DummyUrl( + "de", + 4, + "http://mirror.netcologne.de/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim" + ), + DummyUrl( + "fr", + 5, + "http://mirror3.kiwix.org/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim" + ) ) ) - ) - + } // Basic file attributes - assertThat(result.getFile().getName()).isEqualTo("wikipedia_af_all_nopic_2016-05.zim") + assertThat(result.file?.name).isEqualTo("wikipedia_af_all_nopic_2016-05.zim") - assertThat(result.getFile().size).isEqualTo(63973123L) + assertThat(result.file?.size).isEqualTo(63973123L) // File hashes - assertThat(result.getFile().getHash("md5")).isEqualTo("6f06866b61c4a921b57f28cfd4307220") + assertThat(result.file?.getHash("md5")).isEqualTo("6f06866b61c4a921b57f28cfd4307220") assertThat( - result.getFile().getHash("sha-1") + result.file?.getHash("sha-1") ).isEqualTo("8aac4c7f89e3cdd45b245695e19ecde5aac59593") assertThat( - result.getFile().getHash("sha-256") + result.file?.getHash("sha-256") ).isEqualTo("83126775538cf588a85edb10db04d6e012321a2025278a08a084b258849b3a5c") // Pieces - assertThat(result.getFile().pieceHashType).isEqualTo("sha-1") - assertThat(result.getFile().pieceLength).isEqualTo(1048576) + assertThat(result.file?.pieces?.pieceHashes).isEqualTo("sha-1") + assertThat(result.file?.pieces?.length).isEqualTo(1048576) // Check only the first and the last elements of the piece hashes - assertThat(result.getFile().pieceHashes.size).isEqualTo(62) + assertThat(result.file?.pieceHashes?.size).isEqualTo(62) assertThat( - result.getFile().pieceHashes[0] - ) - .isEqualTo("f36815d904d4fd563aaef4ee6ef2600fb1fd70b2") + result.file?.pieceHashes?.get(0) ?: "" + ).isEqualTo("f36815d904d4fd563aaef4ee6ef2600fb1fd70b2") assertThat( - result.getFile().pieceHashes[61] - ) - .isEqualTo("8055e515aa6e78f2810bbb0e0cd07330838b8920") + result.file?.pieceHashes?.get(61) ?: "" + ).isEqualTo("8055e515aa6e78f2810bbb0e0cd07330838b8920") } data class DummyUrl(val location: String, val priority: Int, val value: String) From 5d42142951f54364c0d3e2828e941281790364c8 Mon Sep 17 00:00:00 2001 From: MohitMali Date: Fri, 2 Sep 2022 13:12:39 +0530 Subject: [PATCH 2/2] Fix Test Failure --- .../core/entity/MetaLinkNetworkEntity.kt | 32 +++++++++++-------- .../core/entity/MetaLinkNetworkEntityTest.kt | 2 +- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntity.kt index 8d633d567..80c58b886 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntity.kt @@ -26,7 +26,7 @@ import org.simpleframework.xml.Text @Root(strict = false, name = "metalink") class MetaLinkNetworkEntity { - @Element + @field:Element var file: FileElement? = null val urls: List? get() = file?.urls @@ -35,17 +35,23 @@ class MetaLinkNetworkEntity { @Root(strict = false) class FileElement { - @Attribute + @field:Attribute var name: String? = null - @ElementList(inline = true, entry = "url") + @field:ElementList(inline = true, entry = "url") var urls: List? = null - @Element val size: Long = 0 + @field:Element var size: Long = 0 - @ElementMap(entry = "hash", key = "type", attribute = true, inline = true, required = false) + @field:ElementMap( + entry = "hash", + key = "type", + attribute = true, + inline = true, + required = false + ) var hashes: Map? = null - @Element(required = false) + @field:Element(required = false) var pieces: Pieces? = null val pieceHashes: List? get() = pieces?.pieceHashes @@ -60,24 +66,24 @@ class MetaLinkNetworkEntity { } class Pieces { - @Attribute - val length = 0 + @field:Attribute + var length = 0 - @Attribute(name = "type") + @field:Attribute(name = "type") var hashType: String? = null - @ElementList(inline = true, entry = "hash") + @field:ElementList(inline = true, entry = "hash") var pieceHashes: List? = null } class Url { - @Attribute + @field:Attribute var location: String? = null - @Attribute + @field:Attribute var priority = 0 - @Text + @field:Text var value: String? = null } } diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntityTest.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntityTest.kt index 634ac8f6a..4d7cc8900 100644 --- a/core/src/test/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntityTest.kt +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/entity/MetaLinkNetworkEntityTest.kt @@ -79,7 +79,7 @@ class MetaLinkNetworkEntityTest { ).isEqualTo("83126775538cf588a85edb10db04d6e012321a2025278a08a084b258849b3a5c") // Pieces - assertThat(result.file?.pieces?.pieceHashes).isEqualTo("sha-1") + assertThat(result.file?.pieces?.hashType).isEqualTo("sha-1") assertThat(result.file?.pieces?.length).isEqualTo(1048576) // Check only the first and the last elements of the piece hashes