From 23e6594201b63aca5955113106011542810e2778 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 2 Jul 2019 14:50:37 +0200 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Don=E2=80=99t=20use=20Optio?= =?utf8?q?nal=20as=20parameter=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/main/java/net/pterodactylus/sone/core/Core.java | 7 +++---- src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java | 4 ++-- .../kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.kt | 2 +- .../kotlin/net/pterodactylus/sone/web/pages/CreatePostPage.kt | 2 +- .../kotlin/net/pterodactylus/sone/fcp/CreatePostCommandTest.kt | 4 ++-- .../net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt | 8 ++++---- .../kotlin/net/pterodactylus/sone/web/pages/CreatePostPageTest.kt | 8 ++++---- 7 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index f004cf2..2ff27c9 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -98,7 +98,6 @@ import net.pterodactylus.util.service.AbstractService; import net.pterodactylus.util.thread.NamedThreadFactory; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Optional; import com.google.common.collect.FluentIterable; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; @@ -1063,7 +1062,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * The text of the post * @return The created post */ - public Post createPost(Sone sone, Optional recipient, String text) { + public Post createPost(Sone sone, @Nullable Sone recipient, String text) { checkNotNull(text, "text must not be null"); checkArgument(text.trim().length() > 0, "text must not be empty"); if (!sone.isLocal()) { @@ -1072,8 +1071,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } PostBuilder postBuilder = database.newPostBuilder(); postBuilder.from(sone.getId()).randomId().currentTime().withText(text.trim()); - if (recipient.isPresent()) { - postBuilder.to(recipient.get().getId()); + if (recipient != null) { + postBuilder.to(recipient.getId()); } final Post post = postBuilder.build(); database.storePost(post); diff --git a/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java b/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java index 2c8456d..221ea7a 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/CreatePostCommand.java @@ -29,7 +29,7 @@ import freenet.support.SimpleFieldSet; /** * FCP command that creates a new {@link Post}. * - * @see Core#createPost(Sone, Optional, String) + * @see Core#createPost(Sone, Sone, String) */ public class CreatePostCommand extends AbstractSoneCommand { @@ -57,7 +57,7 @@ public class CreatePostCommand extends AbstractSoneCommand { if (sone.equals(recipient)) { return new ErrorResponse("Sone and Recipient must not be the same."); } - Post post = getCore().createPost(sone, Optional.fromNullable(recipient), text); + Post post = getCore().createPost(sone, recipient, text); return new Response("PostCreated", new SimpleFieldSetBuilder().put("Post", post.getId()).get()); } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.kt index 79e66ad..28af3e3 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPage.kt @@ -19,7 +19,7 @@ class CreatePostAjaxPage @Inject constructor(webInterface: WebInterface) : Logge ?.let { text -> val sender = request.parameters["sender"].emptyToNull?.let(core::getSone) ?: currentSone val recipient = request.parameters["recipient"]?.let(core::getSone) - core.createPost(sender, recipient.asOptional(), text).let { post -> + core.createPost(sender, recipient, text).let { post -> createSuccessJsonObject().apply { put("postId", post.id) put("sone", sender.id) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/CreatePostPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/CreatePostPage.kt index b47c2b1..2f8e054 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/CreatePostPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/CreatePostPage.kt @@ -28,7 +28,7 @@ class CreatePostPage @Inject constructor(webInterface: WebInterface, loaders: Lo } val sender = soneRequest.core.getLocalSone(soneRequest.httpRequest.getPartAsStringFailsafe("sender", 43)) ?: currentSone val recipient = soneRequest.core.getSone(soneRequest.httpRequest.getPartAsStringFailsafe("recipient", 43)) - soneRequest.core.createPost(sender, recipient.asOptional(), TextFilter.filter(soneRequest.httpRequest.getHeader("Host"), text)) + soneRequest.core.createPost(sender, recipient, TextFilter.filter(soneRequest.httpRequest.getHeader("Host"), text)) throw RedirectException(returnPage) } } diff --git a/src/test/kotlin/net/pterodactylus/sone/fcp/CreatePostCommandTest.kt b/src/test/kotlin/net/pterodactylus/sone/fcp/CreatePostCommandTest.kt index 29db41c..cfbda94 100644 --- a/src/test/kotlin/net/pterodactylus/sone/fcp/CreatePostCommandTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/fcp/CreatePostCommandTest.kt @@ -56,7 +56,7 @@ class CreatePostCommandTest : SoneCommandTest() { parameters += "Text" to "Test" whenever(core.getSone("LocalSoneId")).thenReturn(localSone) val post = mock().apply { whenever(id).thenReturn("PostId") } - whenever(core.createPost(localSone, absent(), "Test")).thenReturn(post) + whenever(core.createPost(localSone, null, "Test")).thenReturn(post) val response = command.execute(parameters) assertThat(response.replyParameters.get("Message"), equalTo("PostCreated")) assertThat(response.replyParameters.get("Post"), equalTo("PostId")) @@ -90,7 +90,7 @@ class CreatePostCommandTest : SoneCommandTest() { whenever(core.getSone("LocalSoneId")).thenReturn(localSone) whenever(core.getSone("RemoteSoneId")).thenReturn(remoteSone) val post = mock().apply { whenever(id).thenReturn("PostId") } - whenever(core.createPost(localSone, of(remoteSone), "Test")).thenReturn(post) + whenever(core.createPost(localSone, remoteSone, "Test")).thenReturn(post) val response = command.execute(parameters) assertThat(response.replyParameters.get("Message"), equalTo("PostCreated")) assertThat(response.replyParameters.get("Post"), equalTo("PostId")) diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt index 821198f..b79e6df 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/CreatePostAjaxPageTest.kt @@ -40,7 +40,7 @@ class CreatePostAjaxPageTest : JsonPageTest("createPost.ajax", pageSupplier = :: fun `request with valid data creates post`() { addRequestParameter("text", "test") val post = createPost() - whenever(core.createPost(currentSone, Optional.absent(), "test")).thenReturn(post) + whenever(core.createPost(currentSone, null, "test")).thenReturn(post) assertThatJsonIsSuccessful() assertThat(json["postId"]?.asText(), equalTo("id")) assertThat(json["sone"]?.asText(), equalTo(currentSone.id)) @@ -52,7 +52,7 @@ class CreatePostAjaxPageTest : JsonPageTest("createPost.ajax", pageSupplier = :: addRequestParameter("text", "test") addRequestParameter("recipient", "invalid") val post = createPost() - whenever(core.createPost(currentSone, Optional.absent(), "test")).thenReturn(post) + whenever(core.createPost(currentSone, null, "test")).thenReturn(post) assertThatJsonIsSuccessful() assertThat(json["postId"]?.asText(), equalTo("id")) assertThat(json["sone"]?.asText(), equalTo(currentSone.id)) @@ -66,7 +66,7 @@ class CreatePostAjaxPageTest : JsonPageTest("createPost.ajax", pageSupplier = :: val recipient = mock().apply { whenever(id).thenReturn("valid") } addSone(recipient) val post = createPost("valid") - whenever(core.createPost(currentSone, Optional.of(recipient), "test")).thenReturn(post) + whenever(core.createPost(currentSone, recipient, "test")).thenReturn(post) assertThatJsonIsSuccessful() assertThat(json["postId"]?.asText(), equalTo("id")) assertThat(json["sone"]?.asText(), equalTo(currentSone.id)) @@ -78,7 +78,7 @@ class CreatePostAjaxPageTest : JsonPageTest("createPost.ajax", pageSupplier = :: addRequestParameter("text", "Link http://freenet.test:8888/KSK@foo is filtered") addRequestHeader("Host", "freenet.test:8888") val post = createPost() - whenever(core.createPost(currentSone, Optional.absent(), "Link KSK@foo is filtered")).thenReturn(post) + whenever(core.createPost(currentSone, null, "Link KSK@foo is filtered")).thenReturn(post) assertThatJsonIsSuccessful() assertThat(json["postId"]?.asText(), equalTo("id")) assertThat(json["sone"]?.asText(), equalTo(currentSone.id)) diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/CreatePostPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/CreatePostPageTest.kt index 2bf0c05..6212c90 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/CreatePostPageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/CreatePostPageTest.kt @@ -40,7 +40,7 @@ class CreatePostPageTest : WebPageTest(::CreatePostPage) { addHttpRequestPart("returnPage", "return.html") addHttpRequestPart("text", "post text") verifyRedirect("return.html") { - verify(core).createPost(currentSone, absent(), "post text") + verify(core).createPost(currentSone, null, "post text") } } @@ -62,7 +62,7 @@ class CreatePostPageTest : WebPageTest(::CreatePostPage) { val sender = mock() addLocalSone("sender-id", sender) verifyRedirect("return.html") { - verify(core).createPost(sender, absent(), "post text") + verify(core).createPost(sender, null, "post text") } } @@ -75,7 +75,7 @@ class CreatePostPageTest : WebPageTest(::CreatePostPage) { val recipient = mock() addSone("recipient-id", recipient) verifyRedirect("return.html") { - verify(core).createPost(currentSone, recipient.asOptional(), "post text") + verify(core).createPost(currentSone, recipient, "post text") } } @@ -86,7 +86,7 @@ class CreatePostPageTest : WebPageTest(::CreatePostPage) { addHttpRequestPart("text", "post http://localhost:12345/KSK@foo text") addHttpRequestHeader("Host", "localhost:12345") verifyRedirect("return.html") { - verify(core).createPost(currentSone, absent(), "post KSK@foo text") + verify(core).createPost(currentSone, null, "post KSK@foo text") } } -- 2.7.4