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