mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-15 02:18:04 -04:00
Changing to assertj in MetaLinkNetworkEntityTest
This commit is contained in:
parent
f0f34c9561
commit
2e81fedda1
@ -17,108 +17,118 @@
|
|||||||
*/
|
*/
|
||||||
package org.kiwix.kiwixmobile.core.entity
|
package org.kiwix.kiwixmobile.core.entity
|
||||||
|
|
||||||
import org.hamcrest.CoreMatchers
|
import org.assertj.core.api.AbstractAssert
|
||||||
import org.hamcrest.Description
|
import org.assertj.core.api.Assertions.assertThat
|
||||||
import org.hamcrest.Matcher
|
|
||||||
import org.hamcrest.TypeSafeMatcher
|
|
||||||
import org.junit.Assert
|
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.simpleframework.xml.core.Persister
|
import org.simpleframework.xml.core.Persister
|
||||||
|
|
||||||
class MetaLinkNetworkEntityTest {
|
class MetaLinkNetworkEntityTest {
|
||||||
private fun url(
|
|
||||||
location: String, priority: Int, value: String
|
|
||||||
): Matcher<MetaLinkNetworkEntity.Url> = UrlMatcher(location, priority, value)
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun testDeserialize() {
|
fun testDeserialize() {
|
||||||
val serializer = Persister()
|
val serializer = Persister()
|
||||||
val result = serializer.read(
|
val result = serializer.read(
|
||||||
MetaLinkNetworkEntity::class.java,
|
MetaLinkNetworkEntity::class.java,
|
||||||
MetaLinkNetworkEntityTest::class.java!!.classLoader.getResourceAsStream(
|
MetaLinkNetworkEntityTest::class.java.classLoader.getResourceAsStream(
|
||||||
"wikipedia_af_all_nopic_2016-05.zim.meta4"
|
"wikipedia_af_all_nopic_2016-05.zim.meta4"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
Assert.assertThat(result.urls.size, CoreMatchers.`is`(5))
|
|
||||||
Assert.assertThat(
|
|
||||||
result.urls, CoreMatchers.hasItems(
|
MetaLinkNetworkEntityUrlAssert(result.urls).hasItems(
|
||||||
url(
|
listOf(
|
||||||
"us", 1,
|
DummyUrl(
|
||||||
|
"us",
|
||||||
|
1,
|
||||||
"http://ftpmirror.your.org/pub/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"
|
"http://ftpmirror.your.org/pub/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"
|
||||||
),
|
),
|
||||||
url(
|
DummyUrl(
|
||||||
"gb", 2,
|
"gb",
|
||||||
|
2,
|
||||||
"http://www.mirrorservice.org/sites/download.kiwix.org/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"
|
"http://www.mirrorservice.org/sites/download.kiwix.org/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"
|
||||||
),
|
),
|
||||||
url(
|
DummyUrl(
|
||||||
"us", 3,
|
"us",
|
||||||
|
3,
|
||||||
"http://download.wikimedia.org/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"
|
"http://download.wikimedia.org/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"
|
||||||
),
|
),
|
||||||
url(
|
DummyUrl(
|
||||||
"de", 4,
|
"de",
|
||||||
|
4,
|
||||||
"http://mirror.netcologne.de/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"
|
"http://mirror.netcologne.de/kiwix/zim/wikipedia/wikipedia_af_all_nopic_2016-05.zim"
|
||||||
),
|
),
|
||||||
url("fr", 5, "http://mirror3.kiwix.org/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
|
// Basic file attributes
|
||||||
Assert.assertThat(
|
assertThat(result.getFile().getName()).isEqualTo("wikipedia_af_all_nopic_2016-05.zim")
|
||||||
result.getFile().getName(),
|
|
||||||
CoreMatchers.`is`("wikipedia_af_all_nopic_2016-05.zim")
|
assertThat(result.getFile().size).isEqualTo(63973123L)
|
||||||
)
|
|
||||||
Assert.assertThat(result.getFile().size, CoreMatchers.`is`(63973123L))
|
|
||||||
// File hashes
|
// File hashes
|
||||||
Assert.assertThat(
|
assertThat(result.getFile().getHash("md5")).isEqualTo("6f06866b61c4a921b57f28cfd4307220")
|
||||||
result.getFile().getHash("md5"),
|
assertThat(
|
||||||
CoreMatchers.`is`("6f06866b61c4a921b57f28cfd4307220")
|
result.getFile().getHash("sha-1")
|
||||||
)
|
).isEqualTo("8aac4c7f89e3cdd45b245695e19ecde5aac59593")
|
||||||
Assert.assertThat(
|
assertThat(
|
||||||
result.getFile().getHash("sha-1"),
|
result.getFile().getHash("sha-256")
|
||||||
CoreMatchers.`is`("8aac4c7f89e3cdd45b245695e19ecde5aac59593")
|
).isEqualTo("83126775538cf588a85edb10db04d6e012321a2025278a08a084b258849b3a5c")
|
||||||
)
|
|
||||||
Assert.assertThat(
|
|
||||||
result.getFile().getHash("sha-256"),
|
|
||||||
CoreMatchers.`is`("83126775538cf588a85edb10db04d6e012321a2025278a08a084b258849b3a5c")
|
|
||||||
)
|
|
||||||
// Pieces
|
// Pieces
|
||||||
Assert.assertThat(result.getFile().pieceHashType, CoreMatchers.`is`("sha-1"))
|
assertThat(result.getFile().pieceHashType).isEqualTo("sha-1")
|
||||||
Assert.assertThat(result.getFile().pieceLength, CoreMatchers.`is`(1048576))
|
assertThat(result.getFile().pieceLength).isEqualTo(1048576)
|
||||||
|
|
||||||
// Check only the first and the last elements of the piece hashes
|
// Check only the first and the last elements of the piece hashes
|
||||||
Assert.assertThat(result.getFile().pieceHashes.size, CoreMatchers.`is`(62))
|
assertThat(result.getFile().pieceHashes.size).isEqualTo(62)
|
||||||
Assert.assertThat(
|
assertThat(
|
||||||
result.getFile().pieceHashes[0],
|
result.getFile().pieceHashes[0]
|
||||||
CoreMatchers.`is`("f36815d904d4fd563aaef4ee6ef2600fb1fd70b2")
|
|
||||||
)
|
)
|
||||||
Assert.assertThat(
|
.isEqualTo("f36815d904d4fd563aaef4ee6ef2600fb1fd70b2")
|
||||||
result.getFile().pieceHashes[61],
|
assertThat(
|
||||||
CoreMatchers.`is`("8055e515aa6e78f2810bbb0e0cd07330838b8920")
|
result.getFile().pieceHashes[61]
|
||||||
)
|
)
|
||||||
|
.isEqualTo("8055e515aa6e78f2810bbb0e0cd07330838b8920")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data class DummyUrl(val location: String, val priority: Int, val value: String)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implemented as a matcher only to avoid putting extra code into {@code MetaLinkNetworkEntity}.
|
* Implemented as a matcher only to avoid putting extra code into {@code MetaLinkNetworkEntity}.
|
||||||
* However in case {@code equals} and {@code hashCode} methods are added to
|
* However in case {@code equals} and {@code hashCode} methods are added to
|
||||||
* {@code MetaLinkNetworkEntity.Url} class itself, this Matcher should be deleted.
|
* {@code MetaLinkNetworkEntity.Url} class itself, this Matcher should be deleted.
|
||||||
*/
|
*/
|
||||||
private class UrlMatcher(
|
class MetaLinkNetworkEntityUrlAssert(
|
||||||
val location: String, val priority: Int,
|
actual: List<MetaLinkNetworkEntity.Url>
|
||||||
val value: String
|
|
||||||
) :
|
) :
|
||||||
|
AbstractAssert<MetaLinkNetworkEntityUrlAssert, List<MetaLinkNetworkEntity.Url>>(
|
||||||
TypeSafeMatcher<MetaLinkNetworkEntity.Url>() {
|
actual,
|
||||||
|
MetaLinkNetworkEntityUrlAssert::class.java
|
||||||
override fun matchesSafely(item: MetaLinkNetworkEntity.Url): Boolean {
|
) {
|
||||||
return (location == item.location && priority == item.priority
|
private fun <S, T> intersectionWith(
|
||||||
&& value == item.value)
|
first: List<S>,
|
||||||
|
second: List<T>,
|
||||||
|
function: (S, T) -> Boolean
|
||||||
|
): Boolean {
|
||||||
|
val filtered = first.filter { a -> second.any { b -> function(a, b) } }
|
||||||
|
if (filtered.isNotEmpty())
|
||||||
|
return true
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun describeTo(description: Description) {
|
fun hasItems(items: List<DummyUrl>): Boolean {
|
||||||
description.appendText(
|
return intersectionWith(actual, items) { a, b ->
|
||||||
String.format(
|
(a.location == b.location && a.priority == b.priority && a.value == b.value)
|
||||||
"Url (location=%s, priority=%d, value=%s", location, priority, value
|
}
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun assertThat(actual: List<MetaLinkNetworkEntity.Url>): MetaLinkNetworkEntityUrlAssert =
|
||||||
|
MetaLinkNetworkEntityUrlAssert(actual)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user