diff --git a/build.gradle b/build.gradle index 58938ac..020a827 100644 --- a/build.gradle +++ b/build.gradle @@ -19,16 +19,16 @@ final def deployDir = 'deploy' final def semverProcessor = "net.thauvin.erik:semver:1.2.0" def isNonStable = { String version -> - def stableKeyword = ['RELEASE', 'FINAL', 'GA', 'JRE'].any { it -> version.toUpperCase().contains(it) } - def regex = /^[0-9,.v-]+(-r)?$/ - return !stableKeyword && !(version ==~ regex) + def stableKeyword = ['RELEASE', 'FINAL', 'GA', 'JRE'].any { it -> version.toUpperCase().contains(it) } + def regex = /^[0-9,.v-]+(-r)?$/ + return !stableKeyword && !(version ==~ regex) } mainClassName = packageName + '.Mobibot' ext.versions = [ - log4j: '2.18.0', - pmd : '6.49.0', + log4j: '2.18.0', + pmd : '6.49.0', ] repositories { @@ -46,12 +46,12 @@ dependencies { //implementation 'com.github.pircbotx:pircbotx:-SNAPSHOT' implementation fileTree(dir: 'lib', include: '*.jar') - // Commons + // Commons (mostly for PircBotX) implementation 'org.apache.commons:commons-lang3:3.12.0' implementation 'org.apache.commons:commons-text:1.9' implementation 'commons-codec:commons-codec:1.15' implementation 'commons-net:commons-net:3.8.0' - + // Google implementation 'com.google.code.gson:gson:2.9.1' implementation 'com.google.guava:guava:31.1-jre' @@ -60,7 +60,7 @@ dependencies { implementation platform('org.jetbrains.kotlin:kotlin-bom') implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4' - implementation'org.jetbrains.kotlinx:kotlinx-cli:0.3.5' + implementation 'org.jetbrains.kotlinx:kotlinx-cli:0.3.5' // Logging implementation 'org.slf4j:slf4j-api:2.0.1' @@ -87,12 +87,21 @@ dependencies { } test { + useTestNG() { + suites('src/test/resources/testng-ci.xml') + } +} + +tasks.withType(Test).configureEach { testLogging { exceptionFormat = 'full' events('skipped', 'failed') } +} + +task testAll(type: Test, group: 'verification', description: 'Run all tests.') { useTestNG() { - options.suites('src/test/resources/testng.xml') + suites('src/test/resources/testng.xml') } } @@ -112,17 +121,18 @@ tasks.withType(JavaCompile) { options.encoding = 'UTF-8' } -compileJava { - dependsOn 'incrementBuildMeta' - options.compilerArgs += ['-Xlint:unchecked', '-Xlint:deprecation'] -} - tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach { kotlinOptions { jvmTarget = java.targetCompatibility.toString() } } +compileJava { + dependsOn 'incrementBuildMeta' + options.compilerArgs += ['-Xlint:unchecked', '-Xlint:deprecation'] +} + + tasks.named("dependencyUpdates").configure { rejectVersionIf { isNonStable(it.candidate.version) @@ -164,7 +174,7 @@ incrementBuildMeta { if (!System.getenv('CI')) { buildMeta = sprintf("%03d", (buildMeta as Integer) + 1) } else { - println 'No increment on CIs.' + println 'No increment with CI.' } } } @@ -209,5 +219,5 @@ task deploy { task release { group = 'Publishing' description = 'Releases new version.' - dependsOn(wrapper, 'deploy') + dependsOn(wrapper, deploy) } diff --git a/config/detekt/baseline.xml b/config/detekt/baseline.xml index a9cd2cf..e5456b7 100644 --- a/config/detekt/baseline.xml +++ b/config/detekt/baseline.xml @@ -48,7 +48,7 @@ NestedBlockDepth:EntryLink.kt$EntryLink$private fun setTags(tags: List<String?>) NestedBlockDepth:FeedsMgr.kt$FeedsMgr.Companion$@JvmStatic @Throws(IOException::class, FeedException::class) fun loadFeed(entries: Entries, currentFile: String = currentXml): String NestedBlockDepth:FeedsMgr.kt$FeedsMgr.Companion$@JvmStatic fun saveFeed(entries: Entries, currentFile: String = currentXml) - NestedBlockDepth:GoogleSearch.kt$GoogleSearch.Companion$@JvmStatic @Throws(ModuleException::class) fun searchGoogle(query: String, apiKey: String?, cseKey: String?): List<Message> + NestedBlockDepth:GoogleSearch.kt$GoogleSearch.Companion$@JvmStatic @Throws(ModuleException::class) fun searchGoogle( query: String, apiKey: String?, cseKey: String?, quotaUser: String = ReleaseInfo.PROJECT ): List<Message> NestedBlockDepth:LinksMgr.kt$LinksMgr$override fun commandResponse(channel: String, args: String, event: GenericMessageEvent) NestedBlockDepth:Lookup.kt$Lookup$override fun commandResponse(channel: String, cmd: String, args: String, event: GenericMessageEvent) NestedBlockDepth:Posting.kt$Posting$override fun commandResponse(channel: String, args: String, event: GenericMessageEvent) @@ -67,7 +67,7 @@ SwallowedException:StockQuoteTest.kt$StockQuoteTest$e: ModuleException SwallowedException:WolframAlpha.kt$WolframAlpha.Companion$ioe: IOException SwallowedException:WolframAlphaTest.kt$WolframAlphaTest$e: ModuleException - ThrowsCount:GoogleSearch.kt$GoogleSearch.Companion$@JvmStatic @Throws(ModuleException::class) fun searchGoogle(query: String, apiKey: String?, cseKey: String?): List<Message> + ThrowsCount:GoogleSearch.kt$GoogleSearch.Companion$@JvmStatic @Throws(ModuleException::class) fun searchGoogle( query: String, apiKey: String?, cseKey: String?, quotaUser: String = ReleaseInfo.PROJECT ): List<Message> ThrowsCount:StockQuote.kt$StockQuote.Companion$@JvmStatic @Throws(ModuleException::class) fun getQuote(symbol: String, apiKey: String?): List<Message> ThrowsCount:StockQuote.kt$StockQuote.Companion$@Throws(ModuleException::class) private fun getJsonResponse(response: String, debugMessage: String): JSONObject ThrowsCount:Weather2.kt$Weather2.Companion$@JvmStatic @Throws(ModuleException::class) fun getWeather(query: String, apiKey: String?): List<Message> diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/seen/NickComparator.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/seen/NickComparator.kt index 8e251b0..8d068c2 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/seen/NickComparator.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/seen/NickComparator.kt @@ -34,10 +34,11 @@ package net.thauvin.erik.mobibot.commands.seen import java.io.Serializable -class NickComparator: Comparator, Serializable { +class NickComparator : Comparator, Serializable { override fun compare(a: String, b: String): Int { return a.lowercase().compareTo(b.lowercase()) } + companion object { private const val serialVersionUID = 1L } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt index 09091cb..6550283 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearch.kt @@ -31,6 +31,7 @@ */ package net.thauvin.erik.mobibot.modules +import net.thauvin.erik.mobibot.ReleaseInfo import net.thauvin.erik.mobibot.Utils.capitalise import net.thauvin.erik.mobibot.Utils.encodeUrl import net.thauvin.erik.mobibot.Utils.helpFormat @@ -64,8 +65,10 @@ class GoogleSearch : ThreadedModule() { if (args.isNotBlank()) { try { val results = searchGoogle( - args, properties[GOOGLE_API_KEY_PROP], - properties[GOOGLE_CSE_KEY_PROP] + args, + properties[GOOGLE_API_KEY_PROP], + properties[GOOGLE_CSE_KEY_PROP], + event.user.nick ) for (msg in results) { event.sendMessage(channel, msg) @@ -96,7 +99,12 @@ class GoogleSearch : ThreadedModule() { */ @JvmStatic @Throws(ModuleException::class) - fun searchGoogle(query: String, apiKey: String?, cseKey: String?): List { + fun searchGoogle( + query: String, + apiKey: String?, + cseKey: String?, + quotaUser: String = ReleaseInfo.PROJECT + ): List { if (apiKey.isNullOrBlank() || cseKey.isNullOrBlank()) { throw ModuleException( "${GoogleSearch::class.java.name} is disabled.", @@ -108,7 +116,7 @@ class GoogleSearch : ThreadedModule() { try { val url = URL( "https://www.googleapis.com/customsearch/v1?key=$apiKey&cx=$cseKey" + - "&q=${query.encodeUrl()}&filter=1&num=5&alt=json" + ""aUser=${quotaUser}&q=${query.encodeUrl()}&filter=1&num=5&alt=json" ) val json = JSONObject(url.reader()) if (json.has("items")) { diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearchTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearchTest.kt index 003e825..1c40d56 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearchTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearchTest.kt @@ -52,35 +52,35 @@ import org.testng.annotations.Test */ class GoogleSearchTest : LocalProperties() { @Test + fun testAPIKeys() { + assertThat( + searchGoogle("", "apikey", "cssKey").first(), + "empty query" + ).isInstanceOf(ErrorMessage::class.java) + + assertThat { searchGoogle("test", "", "apiKey") }.isFailure() + .isInstanceOf(ModuleException::class.java).hasNoCause() + + assertThat { searchGoogle("test", "apiKey", "") }.isFailure() + .isInstanceOf(ModuleException::class.java).hasNoCause() + } + + @Test(groups = ["no-ci"]) @Throws(ModuleException::class) fun testSearchGoogle() { val apiKey = getProperty(GoogleSearch.GOOGLE_API_KEY_PROP) val cseKey = getProperty(GoogleSearch.GOOGLE_CSE_KEY_PROP) - try { - var messages = searchGoogle("mobitopia", apiKey, cseKey) - assertThat(messages, "mobitopia results not empty").isNotEmpty() - assertThat(messages[0].msg, "found mobibtopia").contains("mobitopia", true) - messages = searchGoogle("aapl", apiKey, cseKey) - assertThat(messages, "aapl results not empty").isNotEmpty() - assertThat(messages[0].msg, "found apple").contains("apple", true) + try { + var messages = searchGoogle("mobibot", apiKey, cseKey) + assertThat(messages, "mobibot results not empty").isNotEmpty() + assertThat(messages[0].msg, "found mobibot").contains("mobibot", true) messages = searchGoogle("adadflkjl", apiKey, cseKey) assertThat(messages[0], "not found").all { isInstanceOf(ErrorMessage::class.java) prop(Message::msg).isEqualTo("No results found.") } - - assertThat( - searchGoogle("", "apikey", "cssKey").first(), - "empty query" - ).isInstanceOf(ErrorMessage::class.java) - - assertThat { searchGoogle("test", "", "apiKey") }.isFailure() - .isInstanceOf(ModuleException::class.java).hasNoCause() - - assertThat { searchGoogle("test", "apiKey", "") }.isFailure() - .isInstanceOf(ModuleException::class.java).hasNoCause() } catch (e: ModuleException) { // Avoid displaying api keys in CI logs if ("true" == System.getenv("CI")) { diff --git a/src/test/resources/testng-ci.xml b/src/test/resources/testng-ci.xml new file mode 100644 index 0000000..ee5591c --- /dev/null +++ b/src/test/resources/testng-ci.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + diff --git a/src/test/resources/testng.xml b/src/test/resources/testng.xml index 0486058..f8631de 100644 --- a/src/test/resources/testng.xml +++ b/src/test/resources/testng.xml @@ -1,7 +1,7 @@ - - + + diff --git a/version.properties b/version.properties index 195fb9b..80d7d31 100644 --- a/version.properties +++ b/version.properties @@ -1,9 +1,9 @@ #Generated by the Semver Plugin for Gradle -#Fri Sep 16 14:23:24 PDT 2022 -version.buildmeta=544 +#Fri Sep 16 21:07:43 PDT 2022 +version.buildmeta=574 version.major=0 version.minor=8 version.patch=0 version.prerelease=rc version.project=mobibot -version.semver=0.8.0-rc+544 +version.semver=0.8.0-rc+574