1 package net.pterodactylus.sone.core
3 import com.google.common.base.Ticker
4 import com.google.common.io.ByteStreams
5 import freenet.keys.FreenetURI
6 import net.pterodactylus.sone.core.FreenetInterface.BackgroundFetchCallback
7 import net.pterodactylus.sone.test.*
8 import org.hamcrest.MatcherAssert.assertThat
9 import org.hamcrest.Matchers.equalTo
11 import org.mockito.ArgumentMatchers.any
12 import org.mockito.ArgumentMatchers.eq
13 import org.mockito.Mockito.times
14 import org.mockito.Mockito.verify
15 import java.io.ByteArrayOutputStream
16 import java.util.concurrent.TimeUnit
19 * Unit test for [DefaultElementLoaderTest].
21 class DefaultElementLoaderTest {
23 private val freenetInterface = mock<FreenetInterface>()
24 private val ticker = mock<Ticker>()
25 private val elementLoader = DefaultElementLoader(freenetInterface, ticker)
26 private val callback = capture<BackgroundFetchCallback>()
29 fun `image loader starts request for link that is not known`() {
30 elementLoader.loadElement(IMAGE_ID)
31 verify(freenetInterface).startFetch(eq(freenetURI), any<BackgroundFetchCallback>())
35 fun `element loader only starts request once`() {
36 elementLoader.loadElement(IMAGE_ID)
37 elementLoader.loadElement(IMAGE_ID)
38 verify(freenetInterface).startFetch(eq(freenetURI), any<BackgroundFetchCallback>())
42 fun `element loader returns loading element on first call`() {
43 assertThat(elementLoader.loadElement(IMAGE_ID).loading, equalTo(true))
47 fun `element loader does not cancel on image mime type with 2 mib size`() {
48 elementLoader.loadElement(IMAGE_ID)
49 verify(freenetInterface).startFetch(eq(freenetURI), callback.capture())
50 assertThat(callback.value.shouldCancel(freenetURI, "image/png", sizeOkay), equalTo(false))
54 fun `element loader does cancel on image mime type with more than 2 mib size`() {
55 elementLoader.loadElement(IMAGE_ID)
56 verify(freenetInterface).startFetch(eq(freenetURI), callback.capture())
57 assertThat(callback.value.shouldCancel(freenetURI, "image/png", sizeNotOkay), equalTo(true))
61 fun `element loader does cancel on audio mime type`() {
62 elementLoader.loadElement(IMAGE_ID)
63 verify(freenetInterface).startFetch(eq(freenetURI), callback.capture())
64 assertThat(callback.value.shouldCancel(freenetURI, "audio/mpeg", sizeOkay), equalTo(true))
68 fun `element loader does cancel on video mime type`() {
69 elementLoader.loadElement(IMAGE_ID)
70 verify(freenetInterface).startFetch(eq(freenetURI), callback.capture())
71 assertThat(callback.value.shouldCancel(freenetURI, "video/mkv", sizeOkay), equalTo(true))
75 fun `element loader does cancel on text mime type`() {
76 elementLoader.loadElement(IMAGE_ID)
77 verify(freenetInterface).startFetch(eq(freenetURI), callback.capture())
78 assertThat(callback.value.shouldCancel(freenetURI, "text/plain", sizeOkay), equalTo(true))
82 fun `element loader does not cancel on text html mime type`() {
83 elementLoader.loadElement(IMAGE_ID)
84 verify(freenetInterface).startFetch(eq(freenetURI), callback.capture())
85 assertThat(callback.value.shouldCancel(freenetURI, "text/html", sizeOkay), equalTo(false))
89 fun `image loader can load image`() {
90 elementLoader.loadElement(decomposedKey)
91 verify(freenetInterface).startFetch(eq(FreenetURI(decomposedKey)), callback.capture())
92 callback.value.loaded(FreenetURI(normalizedKey), "image/png", read("/static/images/unknown-image-0.png"))
93 val linkedElement = elementLoader.loadElement(decomposedKey)
94 assertThat(linkedElement, equalTo(LinkedElement(normalizedKey, properties = mapOf(
95 "type" to "image", "size" to 2451, "sizeHuman" to "2 KiB"
100 fun `element loader can extract description from description header`() {
101 elementLoader.loadElement(textKey)
102 verify(freenetInterface).startFetch(eq(FreenetURI(textKey)), callback.capture())
103 callback.value.loaded(FreenetURI(textKey), "text/html; charset=UTF-8", read("element-loader.html"))
104 val linkedElement = elementLoader.loadElement(textKey)
105 assertThat(linkedElement, equalTo(LinkedElement(textKey, properties = mapOf(
108 "sizeHuman" to "266 B",
109 "title" to "Some Nice Page Title",
110 "description" to "This is an example of a very nice freesite."
115 fun `element loader can extract description from first non-heading paragraph`() {
116 elementLoader.loadElement(textKey)
117 verify(freenetInterface).startFetch(eq(FreenetURI(textKey)), callback.capture())
118 callback.value.loaded(FreenetURI(textKey), "text/html; charset=UTF-8", read("element-loader2.html"))
119 val linkedElement = elementLoader.loadElement(textKey)
120 assertThat(linkedElement, equalTo(LinkedElement(textKey, properties = mapOf(
123 "sizeHuman" to "185 B",
124 "title" to "Some Nice Page Title",
125 "description" to "This is the first paragraph of the very nice freesite."
130 fun `element loader can not extract description if html is more complicated`() {
131 elementLoader.loadElement(textKey)
132 verify(freenetInterface).startFetch(eq(FreenetURI(textKey)), callback.capture())
133 callback.value.loaded(FreenetURI(textKey), "text/html; charset=UTF-8", read("element-loader3.html"))
134 val linkedElement = elementLoader.loadElement(textKey)
135 assertThat(linkedElement, equalTo(LinkedElement(textKey, properties = mapOf(
138 "sizeHuman" to "204 B",
139 "title" to "Some Nice Page Title",
140 "description" to null
145 fun `element loader can not extract title if it is missing`() {
146 elementLoader.loadElement(textKey)
147 verify(freenetInterface).startFetch(eq(FreenetURI(textKey)), callback.capture())
148 callback.value.loaded(FreenetURI(textKey), "text/html; charset=UTF-8", read("element-loader4.html"))
149 val linkedElement = elementLoader.loadElement(textKey)
150 assertThat(linkedElement, equalTo(LinkedElement(textKey, properties = mapOf(
153 "sizeHuman" to "229 B",
155 "description" to "This is an example of a very nice freesite."
160 fun `image is not loaded again after it failed`() {
161 elementLoader.loadElement(IMAGE_ID)
162 verify(freenetInterface).startFetch(eq(freenetURI), callback.capture())
163 callback.value.failed(freenetURI)
164 assertThat(elementLoader.loadElement(IMAGE_ID).failed, equalTo(true))
165 verify(freenetInterface).startFetch(eq(freenetURI), callback.capture())
169 fun `image is loaded again after failure cache is expired`() {
170 elementLoader.loadElement(IMAGE_ID)
171 verify(freenetInterface).startFetch(eq(freenetURI), callback.capture())
172 callback.value.failed(freenetURI)
173 whenever(ticker.read()).thenReturn(TimeUnit.MINUTES.toNanos(31))
174 val linkedElement = elementLoader.loadElement(IMAGE_ID)
175 assertThat(linkedElement.failed, equalTo(false))
176 assertThat(linkedElement.loading, equalTo(true))
177 verify(freenetInterface, times(2)).startFetch(eq(freenetURI), callback.capture())
180 private fun read(resource: String): ByteArray =
181 javaClass.getResourceAsStream(resource)?.use { input ->
182 ByteArrayOutputStream().use {
183 ByteStreams.copy(input, it)
190 private const val IMAGE_ID = "KSK@gpl.png"
191 private val freenetURI = FreenetURI(IMAGE_ID)
192 private const val decomposedKey = "CHK@DCiVgTWW9nnWHJc9EVwtFJ6jAfBSVyy~rgiPvhUKbS4,mNY85V0x7dYcv7SnEYo1PCC6y2wNWMDNt-y9UWQx9fI,AAMC--8/fru%CC%88hstu%CC%88ck.jpg"
193 private const val normalizedKey = "CHK@DCiVgTWW9nnWHJc9EVwtFJ6jAfBSVyy~rgiPvhUKbS4,mNY85V0x7dYcv7SnEYo1PCC6y2wNWMDNt-y9UWQx9fI,AAMC--8/frühstück.jpg"
194 private const val textKey = "KSK@gpl.html"
195 private const val sizeOkay = 2097152L
196 private const val sizeNotOkay = sizeOkay + 1