Moved Pinboard to kotlin and added tests.

This commit is contained in:
Erik C. Thauvin 2020-06-19 14:19:23 -07:00
parent d6a85ef4af
commit b729eb2729
10 changed files with 254 additions and 182 deletions

View file

@ -1,7 +1,7 @@
/*
* LocalProperties.java
*
* Copyright (c) 2004-2019, Erik C. Thauvin (erik@thauvin.net)
* Copyright (c) 2004-2020, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -30,7 +30,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.mobibot.modules;
package net.thauvin.erik.mobibot;
import org.apache.commons.lang3.StringUtils;
import org.testng.annotations.BeforeSuite;
@ -49,10 +49,10 @@ import java.util.Properties;
* @created 2019-04-08
* @since 1.0
*/
class LocalProperties {
public class LocalProperties {
private static final Properties localProps = new Properties();
static String getProperty(final String key) {
public static String getProperty(final String key) {
if (localProps.containsKey(key)) {
return localProps.getProperty(key);
} else {

View file

@ -0,0 +1,86 @@
/*
* PinboardTest.kt
*
* Copyright (c) 2004-2020, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.mobibot
import net.thauvin.erik.mobibot.entries.EntryLink
import net.thauvin.erik.pinboard.PinboardPoster
import org.testng.Assert
import org.testng.annotations.Test
import java.io.IOException
import java.net.URI
import java.net.URISyntaxException
import java.net.URLEncoder
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.nio.charset.StandardCharsets
class PinboardUtilsTest : LocalProperties() {
@Test
@Throws(InterruptedException::class, IOException::class, URISyntaxException::class)
fun pinboardTest() {
val apiToken = getProperty("pinboard-api-token")
val pinboard = PinboardPoster(apiToken)
val url = "https://www.example.com/"
val ircServer = "irc.test.com"
val entry = EntryLink(url, "Test Example", "ErikT", "", "#mobitopia", listOf("test"))
PinboardUtils.addPin(pinboard, ircServer, entry)
Assert.assertTrue(validatePin(apiToken, ircServer, entry.link), "add")
entry.link = "https://www.foo.com/"
PinboardUtils.updatePin(pinboard, ircServer, url, entry)
Assert.assertTrue(validatePin(apiToken, ircServer, entry.link), "update")
PinboardUtils.deletePin(pinboard, entry)
Assert.assertFalse(validatePin(apiToken, url = entry.link), "delete")
}
@Throws(IOException::class, URISyntaxException::class, InterruptedException::class)
private fun validatePin(apiToken: String, ircServer: String = "", url: String): Boolean {
val request = HttpRequest.newBuilder().uri(
URI(
"https://api.pinboard.in/v1/posts/get?auth_token=${apiToken}&tag=test&"
+ URLEncoder.encode(url, StandardCharsets.UTF_8)
)
).GET().build()
val response = HttpClient.newBuilder()
.build()
.send(request, HttpResponse.BodyHandlers.ofString())
return if (response.statusCode() == 200) {
response.body().contains(url) && response.body().contains(ircServer)
} else false
}
}

View file

@ -33,6 +33,7 @@
package net.thauvin.erik.mobibot.modules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import net.thauvin.erik.mobibot.LocalProperties;
import net.thauvin.erik.mobibot.msg.Message;
import org.testng.annotations.Test;

View file

@ -33,6 +33,7 @@
package net.thauvin.erik.mobibot.modules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import net.thauvin.erik.mobibot.LocalProperties;
import net.thauvin.erik.mobibot.msg.Message;
import org.testng.annotations.Test;

View file

@ -33,6 +33,7 @@
package net.thauvin.erik.mobibot.modules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import net.thauvin.erik.mobibot.LocalProperties;
import org.testng.annotations.Test;
import java.net.InetAddress;

View file

@ -34,6 +34,7 @@ package net.thauvin.erik.mobibot.modules;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import net.aksingh.owmjapis.api.APIException;
import net.thauvin.erik.mobibot.LocalProperties;
import net.thauvin.erik.mobibot.msg.Message;
import org.testng.annotations.Test;