?.filterIsInstance<FreenetLinkPart>()
?.map { elementLoader.loadElement(it.link) }
?.filter { !it.failed }
- ?: listOf<LinkedElement>()
+ ?: listOf()
} else {
- listOf<LinkedElement>()
+ listOf()
}
private fun showLinkedImages(currentSone: Sone?, sone: Sone?): Boolean {
package net.pterodactylus.sone.utils
-fun <T> T?.asList() = this?.let(::listOf) ?: emptyList<T>()
+fun <T> T?.asList() = this?.let(::listOf) ?: emptyList()
val Any?.unit get() = Unit
fun <T> T?.throwOnNullIf(throwCondition: Boolean, exception: () -> Throwable) =
}
-fun <T> Iterable<T>.paginate(pageSize: Int) = Pagination<T>(toList(), pageSize)
+fun <T> Iterable<T>.paginate(pageSize: Int) = Pagination(toList(), pageSize)
*/
class LikePostCommandTest : SoneCommandTest() {
- private val post = createPost("PostId", mock<Sone>(), null, 1000, "Text")
+ private val post = createPost("PostId", mock(), null, 1000, "Text")
override fun createCommand(core: Core) = LikePostCommand(core)
@Test
fun `request with valid parameters adds post to liked posts for sone`() {
- whenever(core.getLikes(post)).thenReturn(setOf(mock<Sone>(), mock<Sone>(), mock<Sone>()))
+ whenever(core.getLikes(post)).thenReturn(setOf(mock(), mock(), mock()))
parameters += "Post" to "PostId"
parameters += "Sone" to "LocalSoneId"
val replyParameters = command.execute(parameters).replyParameters
*/
class LikeReplyCommandTest : SoneCommandTest() {
- private val reply = createReply("ReplyId", mock<Sone>(), mock<Post>(), 1000, "Text")
+ private val reply = createReply("ReplyId", mock(), mock(), 1000, "Text")
override fun createCommand(core: Core) = LikeReplyCommand(core)
@Test
fun `request with local sone adds reply id to sone`() {
- whenever(core.getLikes(reply)).thenReturn(setOf(mock<Sone>(), mock<Sone>(), mock<Sone>()))
+ whenever(core.getLikes(reply)).thenReturn(setOf(mock(), mock(), mock()))
parameters += "Reply" to "ReplyId"
parameters += "Sone" to "LocalSoneId"
val replyParameters = command.execute(parameters).replyParameters
@Test
fun `plugin respirator is returned correctly`() {
- assertThat(injector.getInstance<PluginRespirator>(), sameInstance(pluginRespirator))
+ assertThat(injector.getInstance(), sameInstance(pluginRespirator))
}
@Test
@Test
fun `node is returned correctly`() {
- assertThat(injector.getInstance<Node>(), sameInstance(node))
+ assertThat(injector.getInstance(), sameInstance(node))
}
@Test
@Test
fun `high level simply client is returned correctly`() {
- assertThat(injector.getInstance<HighLevelSimpleClient>(), sameInstance(highLevelSimpleClient))
+ assertThat(injector.getInstance(), sameInstance(highLevelSimpleClient))
}
@Test
@Test
fun `session manager is returned correctly`() {
- assertThat(injector.getInstance<SessionManager>(), sameInstance(sessionManager))
+ assertThat(injector.getInstance(), sameInstance(sessionManager))
}
@Test
private val accessor = ImageAccessor()
private val album = mock<Album>()
- private val images = listOf(mock<Image>(), mock<Image>())
+ private val images = listOf(mock<Image>(), mock())
@Before
fun setupImages() {
@Test
fun `filter renders empty span for not loaded elements`() {
- val html = filter.format(null, LinkedElement("KSK@gpl.png", loading = true), emptyMap<String, Any?>()) as String
+ val html = filter.format(null, LinkedElement("KSK@gpl.png", loading = true), emptyMap()) as String
val spanNode = Jsoup.parseBodyFragment(html).body().child(0)
assertThat(spanNode.nodeName(), `is`("span"))
assertThat(spanNode.attr("class"), `is`("linked-element not-loaded"))
@Test
fun `filter can render linked images`() {
- val html = filter.format(null, LinkedElement("KSK@gpl.png", properties = mapOf("type" to "image")), emptyMap<String, Any?>()) as String
+ val html = filter.format(null, LinkedElement("KSK@gpl.png", properties = mapOf("type" to "image")), emptyMap()) as String
val outerSpanNode = Jsoup.parseBodyFragment(html).body().child(0)
assertThat(outerSpanNode.nodeName(), `is`("span"))
assertThat(outerSpanNode.attr("class"), `is`("linked-element loaded"))
@Test
fun `filter can render HTML pages`() {
- val html = filter.format(null, LinkedElement("KSK@gpl.html", properties = mapOf("type" to "html", "title" to "Page Title", "description" to "This is the description.")), emptyMap<String, Any?>()) as String
+ val html = filter.format(null, LinkedElement("KSK@gpl.html", properties = mapOf("type" to "html", "title" to "Page Title", "description" to "This is the description.")), emptyMap()) as String
val outerSpanNode = Jsoup.parseBodyFragment(html).body().child(0)
assertThat(outerSpanNode.nodeName(), equalTo("span"))
assertThat(outerSpanNode.attr("class"), `is`("linked-element loaded"))
@Before
fun setupCore() {
- whenever(core.getImage(eq("avatar-id"), anyBoolean())).thenReturn(mock<Image>())
+ whenever(core.getImage(eq("avatar-id"), anyBoolean())).thenReturn(mock())
}
@Test
fun `returns the likes correctly`() {
- val sones = setOf(mock<Sone>(), mock<Sone>(), mock<Sone>())
+ val sones = setOf(mock<Sone>(), mock(), mock())
whenever(core.getLikes(reply)).thenReturn(sones)
assertThat(accessor.get(templateContext, reply, "likes"), equalTo<Any>(sones))
}
@Test
fun `return that a reply is loaded if its sone is not null`() {
- whenever(reply.sone).thenReturn(mock<Sone>())
+ whenever(reply.sone).thenReturn(mock())
assertThat(accessor.get(templateContext, reply, "loaded"), equalTo<Any>(true))
}
@Test
fun `accessor returns that the sone’s status is not unknown if it is not unknown`() {
- whenever(sone.status).thenReturn(mock<SoneStatus>())
+ whenever(sone.status).thenReturn(mock())
assertAccessorReturnValue("unknown", false)
}
@Test
fun `accessor returns that the sone’s status is not idle if it is not idle`() {
- whenever(sone.status).thenReturn(mock<SoneStatus>())
+ whenever(sone.status).thenReturn(mock())
assertAccessorReturnValue("idle", false)
}
@Test
fun `accessor returns that the sone’s status is not inserting if it is not inserting`() {
- whenever(sone.status).thenReturn(mock<SoneStatus>())
+ whenever(sone.status).thenReturn(mock())
assertAccessorReturnValue("inserting", false)
}
@Test
fun `accessor returns that the sone’s status is not downloading if it is not downloading`() {
- whenever(sone.status).thenReturn(mock<SoneStatus>())
+ whenever(sone.status).thenReturn(mock())
assertAccessorReturnValue("downloading", false)
}
@Test
fun `accessor returns all images in the correct order`() {
- val images = listOf(mock<Image>(), mock<Image>(), mock<Image>(), mock<Image>(), mock<Image>())
+ val images = listOf(mock<Image>(), mock(), mock(), mock(), mock())
val firstAlbum = createAlbum(listOf(), listOf(images[0], images[3]))
val secondAlbum = createAlbum(listOf(), listOf(images[1], images[4], images[2]))
val rootAlbum = createAlbum(listOf(firstAlbum, secondAlbum), listOf())
@Test
fun `accessor returns all albums in the correct order`() {
- val albums = listOf(mock<Album>(), mock<Album>(), mock<Album>(), mock<Album>(), mock<Album>())
+ val albums = listOf(mock<Album>(), mock(), mock(), mock(), mock())
val rootAlbum = createAlbum(albums, listOf())
whenever(sone.rootAlbum).thenReturn(rootAlbum)
assertAccessorReturnValueMatches("albums", contains(*albums.toTypedArray()))
fun <T : Any> KClass<T>.isProvidedBy(instance: T) = Module { it.bind(this.java).toProvider { instance } }
fun <T : Any> KClass<T>.isProvidedBy(provider: com.google.inject.Provider<T>) = Module { it.bind(this.java).toProvider(provider) }
fun <T : Any> KClass<T>.isProvidedBy(provider: KClass<out Provider<T>>) = Module { it.bind(this.java).toProvider(provider.java) }
-inline fun <reified T : Any> KClass<T>.isProvidedByMock() = Module { it.bind(this.java).toProvider { mock<T>() } }
+inline fun <reified T : Any> KClass<T>.isProvidedByMock() = Module { it.bind(this.java).toProvider { mock() } }
inline fun <reified T : Any> Injector.getInstance() = getInstance(T::class.java)!!
Module { it!!.bind(T::class.java).toInstance(implementation) }
inline fun <reified T : Any> bindMock(): Module =
- Module { it!!.bind(T::class.java).toInstance(mock<T>()) }
+ Module { it!!.bind(T::class.java).toInstance(mock()) }
inline fun <reified T: Any?> whenever(methodCall: T) = Mockito.`when`(methodCall)!!
-inline fun <reified T : Any> OngoingStubbing<T>.thenReturnMock(): OngoingStubbing<T> = this.thenReturn(mock<T>())
+inline fun <reified T : Any> OngoingStubbing<T>.thenReturnMock(): OngoingStubbing<T> = this.thenReturn(mock())
operator fun <T> InvocationOnMock.get(index: Int): T = getArgument(index)
private val matchers = mutableListOf<Matcher<A, *>>()
fun <V> expect(description: String, expected: V, actual: (A) -> V) {
- matchers += Matcher<A, V>(expected, actual, description)
+ matchers += Matcher(expected, actual, description)
}
override fun describeTo(description: Description) {
private val sone = mock<Sone>()
init {
- `when`(sone.profile).thenReturn(mock<Profile>())
+ `when`(sone.profile).thenReturn(mock())
`when`(sone.name).thenReturn("sone")
}
class PaginationTest {
private val items = listOf(1, 2, 3, 4, 5)
- private val pagination = Pagination<Int>(items, 2)
+ private val pagination = Pagination(items, 2)
@Test
fun `pagination can be created from iterable`() {
fun `album is deleted and page redirects to album if parent album is not root album`() {
setMethod(POST)
whenever(parentAlbum.isRoot).thenReturn(false)
- whenever(sone.rootAlbum).thenReturn(mock<Album>())
+ whenever(sone.rootAlbum).thenReturn(mock())
addAlbum("album-id", album)
addHttpRequestPart("album", "album-id")
verifyRedirect("imageBrowser.html?album=parent-id") {
whenever(albums).thenReturn(listOf(album))
}
whenever(this.rootAlbum).thenReturn(rootAlbum)
- whenever(this.profile).thenReturn(mock<Profile>())
+ whenever(this.profile).thenReturn(mock())
whenever(id).thenReturn(name.toLowerCase())
whenever(this.name).thenReturn(name)
}
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("type", "post")
addHttpRequestPart("id", "post1 post2 post3")
- val posts = listOf(mock<Post>(), mock<Post>())
+ val posts = listOf(mock<Post>(), mock())
addPost("post1", posts[0])
addPost("post3", posts[1])
verifyRedirect("return.html") {
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("type", "reply")
addHttpRequestPart("id", "reply1 reply2 reply3")
- val replies = listOf(mock<PostReply>(), mock<PostReply>())
+ val replies = listOf(mock<PostReply>(), mock())
addPostReply("reply1", replies[0])
addPostReply("reply3", replies[1])
verifyRedirect("return.html") {
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("type", "sone")
addHttpRequestPart("id", "sone1 sone2 sone3")
- val sones = listOf(mock<Sone>(), mock<Sone>())
+ val sones = listOf(mock<Sone>(), mock())
addSone("sone1", sones[0])
addSone("sone3", sones[1])
verifyRedirect("return.html") {
fun `posts are not duplicated when they come from both new posts and new replies notifications`() {
val extraPost = mock<Post>().withTime(2000)
val posts = asList(mock<Post>().withTime(1000), mock<Post>().withTime(3000))
- val postReplies = asList(mock<PostReply>(), mock<PostReply>())
+ val postReplies = asList(mock<PostReply>(), mock())
whenever(postReplies[0].post).thenReturn(posts[0].asOptional())
whenever(postReplies[1].post).thenReturn(extraPost.asOptional())
whenever(webInterface.getNewPosts(currentSone)).thenReturn(posts)
@Test
fun `searching for sone link without prefix redirects to view sone page`() {
- addSone("sone-id", mock<Sone>())
+ addSone("sone-id", mock())
addHttpRequestParameter("query", "sone-id")
verifyRedirect("viewSone.html?sone=sone-id")
}
@Test
fun `searching for a post link redirects to post page`() {
- addPost("Post-id", mock<Post>())
+ addPost("Post-id", mock())
addHttpRequestParameter("query", "post://Post-id")
verifyRedirect("viewPost.html?post=Post-id")
}
@Test
fun `searching for a post ID without prefix redirects to post page`() {
- addPost("post-id", mock<Post>())
+ addPost("post-id", mock())
addHttpRequestParameter("query", "post-id")
verifyRedirect("viewPost.html?post=post-id")
}
@Test
fun `searching for an album link redirects to the image browser`() {
- addAlbum("album-id", mock<Album>())
+ addAlbum("album-id", mock())
addHttpRequestParameter("query", "album://album-id")
verifyRedirect("imageBrowser.html?album=album-id")
}
@Test
fun `searching for an album ID redirects to the image browser`() {
- addAlbum("album-id", mock<Album>())
+ addAlbum("album-id", mock())
addHttpRequestParameter("query", "album-id")
verifyRedirect("imageBrowser.html?album=album-id")
}
@Test
fun `searching for an image link redirects to the image browser`() {
- addImage("image-id", mock<Image>())
+ addImage("image-id", mock())
addHttpRequestParameter("query", "image://image-id")
verifyRedirect("imageBrowser.html?image=image-id")
}
@Test
fun `searching for an image ID redirects to the image browser`() {
- addImage("image-id", mock<Image>())
+ addImage("image-id", mock())
addHttpRequestParameter("query", "image-id")
verifyRedirect("imageBrowser.html?image=image-id")
}
@Test
fun `local sones are set in template context`() {
- val localSones = listOf(mock<Sone>(), mock<Sone>())
+ val localSones = listOf(mock<Sone>(), mock())
whenever(core.localSones).thenReturn(localSones)
verifyVariableMatches("localSones", containsInAnyOrder(*localSones.toTypedArray()))
}
setMethod(POST)
addHttpRequestPart("returnPage", "return.html")
addHttpRequestPart("sone", "remote-sone")
- addSone("remote-sone", mock<Sone>())
+ addSone("remote-sone", mock())
verifyRedirect("return.html") {
verify(core, never()).unlockSone(any())
}
fun `post request with parent that is not the current sone results in no permission error page`() {
setMethod(POST)
addHttpRequestPart("parent", "parent-id")
- whenever(parentAlbum.sone).thenReturn(mock<Sone>())
+ whenever(parentAlbum.sone).thenReturn(mock())
addAlbum("parent-id", parentAlbum)
verifyRedirect("noPermission.html")
}
private val post1 = createPost("post1", "First Post.", 1000, currentSone)
private val post2 = createPost("post2", "Second Post.", 2000, currentSone)
- private val foreignPost1 = createPost("foreign-post1", "First Foreign Post.", 1000, mock<Sone>())
- private val foreignPost2 = createPost("foreign-post2", "Second Foreign Post.", 2000, mock<Sone>())
- private val foreignPost3 = createPost("foreign-post3", "Third Foreign Post.", 3000, mock<Sone>())
- private val directed1 = createPost("post3", "First directed.", 1500, mock<Sone>(), recipient = currentSone)
- private val directed2 = createPost("post4", "Second directed.", 2500, mock<Sone>(), recipient = currentSone)
+ private val foreignPost1 = createPost("foreign-post1", "First Foreign Post.", 1000, mock())
+ private val foreignPost2 = createPost("foreign-post2", "Second Foreign Post.", 2000, mock())
+ private val foreignPost3 = createPost("foreign-post3", "Third Foreign Post.", 3000, mock())
+ private val directed1 = createPost("post3", "First directed.", 1500, mock(), recipient = currentSone)
+ private val directed2 = createPost("post4", "Second directed.", 2500, mock(), recipient = currentSone)
@Before
fun setup() {
/**
* Base class for web page tests.
*/
-open class WebPageTest(pageSupplier: (Template, WebInterface) -> SoneTemplatePage = { _, _ -> mock<SoneTemplatePage>() }) {
+open class WebPageTest(pageSupplier: (Template, WebInterface) -> SoneTemplatePage = { _, _ -> mock() }) {
val currentSone = mock<Sone>()
val template = mock<Template>()
}
fun addHttpRequestParameter(name: String, value: String) {
- getRequestParameters[name] = getRequestParameters.getOrElse(name) { mutableListOf<String>() }.apply { add(value) }
+ getRequestParameters[name] = getRequestParameters.getOrElse(name) { mutableListOf() }.apply { add(value) }
}
fun addHttpRequestPart(name: String, value: String) {