Skip to content

Commit

Permalink
chore: Cleanup some deprecation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Nov 22, 2024
1 parent f6b85fc commit 140915c
Show file tree
Hide file tree
Showing 26 changed files with 156 additions and 124 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package au.com.dius.pact.consumer.junit5

import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.extension.ExtendWith
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy


// Shorthand for @ExtendWith(PactConsumerTestExt::class)
@ExtendWith(PactConsumerTestExt::class)
@Retention(RetentionPolicy.RUNTIME)
@Retention(AnnotationRetention.RUNTIME)
@Target(
AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.CLASS
)
annotation class PactConsumerTest
annotation class PactConsumerTest
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import au.com.dius.pact.core.support.Json
import au.com.dius.pact.core.support.Result
import au.com.dius.pact.core.support.Utils
import au.com.dius.pact.core.support.json.JsonValue
import io.github.oshai.kotlinlogging.KLogging
import io.github.oshai.kotlinlogging.KotlinLogging
import java.io.File
import java.util.Collections
import kotlin.reflect.full.declaredFunctions
import kotlin.reflect.full.memberProperties

private val logger = KotlinLogging.logger {}

/**
* Base Pact class
*/
Expand Down Expand Up @@ -64,7 +66,7 @@ abstract class BasePact @JvmOverloads constructor(

override fun isV4Pact() = false

companion object : KLogging() {
companion object {
@JvmStatic
val DEFAULT_METADATA: Map<String, Map<String, Any?>> by lazy {
Collections.unmodifiableMap(mapOf(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package au.com.dius.pact.core.model

import au.com.dius.pact.core.support.isNotEmpty
import io.github.oshai.kotlinlogging.KLogging
import io.github.oshai.kotlinlogging.KotlinLogging
import org.apache.tika.mime.MediaType
import org.apache.tika.mime.MediaTypeRegistry
import org.apache.tika.mime.MimeTypes
import java.nio.charset.Charset
import java.util.Locale

private val jsonRegex = Regex(".*json")
private val xmlRegex = Regex(".*xml")
private val logger = KotlinLogging.logger {}

class ContentType(val contentType: MediaType?) {

Expand All @@ -21,7 +23,7 @@ class ContentType(val contentType: MediaType?) {
else -> {
if ("vnd.schemaregistry.v1+json" == contentType.subtype)
false
else if (jsonRegex.matches(contentType.subtype.toLowerCase())) {
else if (jsonRegex.matches(contentType.subtype.lowercase(Locale.getDefault()))) {
true
} else {
val superType = registry.getSupertype(contentType)
Expand All @@ -35,7 +37,7 @@ class ContentType(val contentType: MediaType?) {
fun isXml(): Boolean = if (contentType != null) {
when (System.getProperty("pact.content_type.override.${contentType.baseType}")) {
"xml" -> true
else -> xmlRegex.matches(contentType.subtype.toLowerCase())
else -> xmlRegex.matches(contentType.subtype.lowercase(Locale.getDefault()))
}
} else false

Expand Down Expand Up @@ -134,7 +136,7 @@ class ContentType(val contentType: MediaType?) {
}
}

companion object : KLogging() {
companion object {
@JvmStatic
fun fromString(contentType: String?) = if (contentType.isNullOrEmpty()) {
UNKNOWN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.ktor.http.HeaderValue
import io.ktor.http.HeaderValueParam
import io.ktor.http.HeaderValueWithParameters
import io.ktor.http.parseHeaderValue
import java.util.Locale

class HeaderWithParameters(
content: String,
Expand All @@ -19,7 +20,7 @@ object HeaderParser {
fun fromJson(key: String, value: JsonValue): List<String> {
return when {
value is JsonValue.Array -> value.values.map { Json.toString(it).trim() }
SINGLE_VALUE_HEADERS.contains(key.toLowerCase()) -> listOf(Json.toString(value).trim())
SINGLE_VALUE_HEADERS.contains(key.lowercase(Locale.getDefault())) -> listOf(Json.toString(value).trim())
else -> {
val sval = Json.toString(value).trim()
parseHeaderValue(sval).map { hvToString(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import au.com.dius.pact.core.model.generators.Generators
import au.com.dius.pact.core.model.matchingrules.MatchingRules
import au.com.dius.pact.core.support.isNotEmpty
import au.com.dius.pact.core.support.json.JsonValue
import io.github.oshai.kotlinlogging.KLogging
import io.github.oshai.kotlinlogging.KotlinLogging
import java.nio.charset.Charset
import java.util.Base64

private val logger = KotlinLogging.logger {}

/**
* object that represents part of an http message
*/
Expand Down Expand Up @@ -90,7 +92,7 @@ abstract class HttpPart: IHttpPart {
return generators + matchingRuleGenerators
}

companion object : KLogging() {
companion object {
const val CONTENT_TYPE = "Content-Type"

@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import au.com.dius.pact.core.model.ContentType.Companion.UNKNOWN
import au.com.dius.pact.core.model.ContentType.Companion.XMLREGEXP
import au.com.dius.pact.core.model.ContentType.Companion.XMLREGEXP2
import au.com.dius.pact.core.support.json.JsonParser
import io.github.oshai.kotlinlogging.KLogging
import io.github.oshai.kotlinlogging.KotlinLogging
import org.apache.commons.codec.binary.Hex
import org.apache.tika.config.TikaConfig
import org.apache.tika.io.TikaInputStream
import org.apache.tika.metadata.Metadata
import java.util.Base64

private val logger = KotlinLogging.logger {}

/**
* If the content type should be overridden
*/
Expand Down Expand Up @@ -206,7 +208,7 @@ data class OptionalBody @JvmOverloads constructor(
}
}

companion object : KLogging() {
companion object {

@JvmStatic fun missing(): OptionalBody {
return OptionalBody(State.MISSING)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package au.com.dius.pact.core.model

import au.com.dius.pact.core.support.Result
import io.github.oshai.kotlinlogging.KLogging
import io.github.oshai.kotlinlogging.KotlinLogging

private val logger = KotlinLogging.logger {}

data class MergeResult(val ok: Boolean, val message: String, val result: Pact? = null)

/**
* Utility class for merging two pacts together, checking for conflicts
*/
object PactMerge : KLogging() {
object PactMerge {

@JvmStatic
fun merge(newPact: Pact, existing: Pact): MergeResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import au.com.dius.pact.core.support.json.JsonValue
import au.com.dius.pact.core.support.json.map
import au.com.dius.pact.core.support.jsonArray
import au.com.dius.pact.core.support.jsonObject
import au.com.dius.pact.core.support.unwrap
import io.github.oshai.kotlinlogging.KLogging
import io.github.oshai.kotlinlogging.KotlinLogging
import org.apache.hc.client5.http.auth.AuthScope
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials
Expand All @@ -41,6 +39,7 @@ import java.io.Reader
import java.net.URI
import java.net.URL
import java.net.URLDecoder
import java.util.Locale
import kotlin.collections.set

private val logger = KotlinLogging.logger {}
Expand Down Expand Up @@ -83,19 +82,20 @@ fun fetchJsonResource(http: CloseableHttpClient, source: UrlPactSource):
httpGet.addHeader("Content-Type", "application/json")
httpGet.addHeader("Accept", "application/hal+json, application/json")

val response = http.execute(httpGet)
if (response.code < 300) {
val contentType = ContentType.parseLenient(response.entity.contentType)
if (isJsonResponse(contentType)) {
JsonParser.parseString(EntityUtils.toString(response.entity)) to source
http.execute(httpGet) { response ->
if (response.code < 300) {
val contentType = ContentType.parseLenient(response.entity.contentType)
if (isJsonResponse(contentType)) {
JsonParser.parseString(EntityUtils.toString(response.entity)) to source
} else {
throw InvalidHttpResponseException("Expected a JSON response, but got '$contentType'")
}
} else {
throw InvalidHttpResponseException("Expected a JSON response, but got '$contentType'")
}
} else {
when (response.code) {
404 -> throw InvalidHttpResponseException("No JSON document found at source '$source'")
else -> throw InvalidHttpResponseException("Request to source '$source' failed with response " +
"${response.code}")
when (response.code) {
404 -> throw InvalidHttpResponseException("No JSON document found at source '$source'")
else -> throw InvalidHttpResponseException("Request to source '$source' failed with response " +
"${response.code}")
}
}
}
}
Expand All @@ -120,7 +120,7 @@ fun newHttpClient(baseUrl: String, options: Map<String, Any>): CloseableHttpClie
}
options["authentication"] is List<*> -> {
val authentication = options["authentication"] as List<*>
when (val scheme = authentication.first().toString().toLowerCase()) {
when (val scheme = authentication.first().toString().lowercase(Locale.getDefault())) {
"basic" -> {
if (authentication.size > 2) {
basicAuth(baseUrl, authentication[1].toString(), authentication[2].toString(), builder)
Expand Down Expand Up @@ -203,7 +203,7 @@ interface PactReader {
/**
* Default implementation of PactReader
*/
object DefaultPactReader : PactReader, KLogging() {
object DefaultPactReader : PactReader {

private const val CLASSPATH_URI_START = "classpath:"

Expand Down Expand Up @@ -421,10 +421,10 @@ object DefaultPactReader : PactReader, KLogging() {
} else if (source is URL || source is UrlPactSource) {
val urlSource = if (source is URL) UrlSource(source.toString()) else source as UrlPactSource
return loadPactFromUrl(urlSource, options, newHttpClient(urlSource.url, options))
} else if (source is String && source.toLowerCase().matches(Regex("(https?|file)://?.*"))) {
} else if (source is String && source.lowercase(Locale.getDefault()).matches(Regex("(https?|file)://?.*"))) {
val urlSource = UrlSource(source)
return loadPactFromUrl(urlSource, options, newHttpClient(urlSource.url, options))
} else if (source is String && source.toLowerCase().matches(Regex("s3://.*"))) {
} else if (source is String && source.lowercase(Locale.getDefault()).matches(Regex("s3://.*"))) {
return loadPactFromS3Bucket(source)
} else if (source is String && source.startsWith(CLASSPATH_URI_START)) {
return loadPactFromClasspath(source.substring(CLASSPATH_URI_START.length))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package au.com.dius.pact.core.model

import au.com.dius.pact.core.support.Utils.lookupEnvironmentValue
import io.github.oshai.kotlinlogging.KLogging
import io.github.oshai.kotlinlogging.KotlinLogging

private val logger = KotlinLogging.logger {}

/**
* Pact Specification Version
Expand Down Expand Up @@ -34,7 +36,7 @@ enum class PactSpecVersion {
}
}

companion object: KLogging() {
companion object {
@JvmStatic
fun fromInt(version: Int): PactSpecVersion {
return when (version) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import au.com.dius.pact.core.support.Json
import au.com.dius.pact.core.support.Result
import au.com.dius.pact.core.support.json.JsonParser
import au.com.dius.pact.core.support.json.JsonValue
import io.github.oshai.kotlinlogging.KLogging
import io.github.oshai.kotlinlogging.KotlinLogging
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.PrintWriter
import java.io.RandomAccessFile
import java.io.StringWriter
import java.nio.charset.Charset

private val logger = KotlinLogging.logger {}

enum class PactWriteMode {
MERGE, OVERWRITE
}
Expand Down Expand Up @@ -47,7 +49,7 @@ interface PactWriter {
/**
* Default implementation of a Pact writer
*/
object DefaultPactWriter : PactWriter, KLogging() {
object DefaultPactWriter : PactWriter {

/**
* Writes out the pact to the provided pact file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import au.com.dius.pact.core.model.matchingrules.MatchingRules
import au.com.dius.pact.core.model.matchingrules.MatchingRulesImpl
import au.com.dius.pact.core.support.Json
import au.com.dius.pact.core.support.json.JsonValue
import io.github.oshai.kotlinlogging.KLogging
import io.github.oshai.kotlinlogging.KotlinLogging
import java.util.Locale

private val logger = KotlinLogging.logger {}

/**
* Request made by a consumer to a provider
Expand Down Expand Up @@ -88,14 +91,14 @@ class Request @Suppress("LongParameterList") @JvmOverloads constructor(
}

override fun headersWithoutCookie(): Map<String, List<String>> {
return headers.filter { (k, _) -> k.toLowerCase() != COOKIE_KEY }
return headers.filter { (k, _) -> k.lowercase(Locale.getDefault()) != COOKIE_KEY }
}

@Deprecated("use cookies()", ReplaceWith("cookies()"))
fun cookie() = cookies()

override fun cookies(): List<String> {
val cookieEntry = headers.entries.find { (k, _) -> k.toLowerCase() == COOKIE_KEY }
val cookieEntry = headers.entries.find { (k, _) -> k.lowercase(Locale.getDefault()) == COOKIE_KEY }
return if (cookieEntry != null) {
cookieEntry.value.flatMap {
it.split(';')
Expand Down Expand Up @@ -139,7 +142,7 @@ class Request @Suppress("LongParameterList") @JvmOverloads constructor(
return HttpRequest(method, path, query, headers, body, matchingRules, generators)
}

companion object : KLogging() {
companion object {
const val COOKIE_KEY = "cookie"
const val DEFAULT_METHOD = "GET"
const val DEFAULT_PATH = "/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package au.com.dius.pact.core.model

import au.com.dius.pact.core.support.Json
import au.com.dius.pact.core.support.json.JsonParser
import io.github.oshai.kotlinlogging.KLogging
import io.github.oshai.kotlinlogging.KotlinLogging
import java.net.URLEncoder
import java.util.Locale

private val logger = KotlinLogging.logger {}

/**
* Interaction between a consumer and a provider
Expand Down Expand Up @@ -83,13 +86,13 @@ open class RequestResponseInteraction @JvmOverloads constructor(
description, providerStates, request.copy(), response.copyResponse(), interactionId
)

companion object : KLogging() {
companion object {
const val COMMA = ", "

@JvmStatic
fun requestToMap(request: Request, pactSpecVersion: PactSpecVersion?): Map<String, Any?> {
val map = mutableMapOf<String, Any?>(
"method" to request.method.toUpperCase(),
"method" to request.method.uppercase(Locale.getDefault()),
"path" to request.path
)
if (request.headers.isNotEmpty()) {
Expand Down
Loading

0 comments on commit 140915c

Please sign in to comment.