diff --git a/.editorconfig b/.editorconfig index 25f53f3..b2fc963 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,26 +1,49 @@ # https://editorconfig.org root = true - [*] indent_style = space - end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true - [*.{java,kt,kts,scala,rs,xml,kt.spec,kts.spec}] indent_size = 4 - [*.{kt,kts}] +ktlint_code_style = android_studio ij_kotlin_imports_layout = * ij_kotlin_allow_trailing_comma = false ij_kotlin_allow_trailing_comma_on_call_site = false +# General ktlint_standard_no-wildcard-imports = disabled -max_line_length = off - +ktlint_standard_trailing-comma-on-declaration-site = disabled +ktlint_standard_trailing-comma-on-call-site = disabled +ktlint_standard_max-line-length = disabled +ktlint_standard_function-signature = disabled +ktlint_standard_function-expression-body = disabled +ktlint_standard_function-literal = disabled +ktlint_standard_class-signature = disabled +ktlint_standard_block-comment-initial-star-alignment = disabled +ktlint_standard_indent = disabled +ktlint_standard_no-semi = disabled +ktlint_standard_unnecessary-parentheses-before-trailing-lambda = disabled +# Wrapping +ktlint_standard_argument-list-wrapping = disabled +ktlint_standard_parameter-list-wrapping = disabled +ktlint_standard_binary-expression-wrapping = disabled +ktlint_standard_property-wrapping = disabled +ktlint_standard_parameter-wrapping = disabled +ktlint_standard_enum-wrapping = disabled +ktlint_standard_statement-wrapping = disabled +ktlint_standard_condition-wrapping = disabled +ktlint_standard_wrapping = disabled +# Naming +ktlint_standard_function-naming = disabled +ktlint_standard_property-naming = disabled +ktlint_standard_backing-property-naming = disabled +# Spacing +ktlint_standard_spacing-between-declarations-with-comments = disabled +ktlint_standard_function-type-modifier-spacing = disabled [*.md] trim_trailing_whitespace = false - [gradle/verification-metadata.xml] indent_size = 3 \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index be4722d..6406fa1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,10 +11,10 @@ jobs: steps: - uses: actions/checkout@v3 - - name: set up JDK 11 + - name: set up JDK 17 uses: actions/setup-java@v3 with: - java-version: '11' + java-version: '17' distribution: 'temurin' cache: gradle diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 72b5c3c..d485310 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -11,10 +11,10 @@ jobs: steps: - uses: actions/checkout@v3 - - name: set up JDK 11 + - name: set up JDK 17 uses: actions/setup-java@v3 with: - java-version: '11' + java-version: '17' distribution: 'temurin' cache: gradle diff --git a/.gitignore b/.gitignore index 89cef71..beb75de 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ local.properties /kotlin-js-store secrets.properties private.pgp +/.kotlin diff --git a/README.md b/README.md index 80c6f2a..8f7cc5c 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,11 @@ For Gradle users, add the following to your module’s `build.gradle` ```groovy dependencies { //compatible with all libraries - testImplementation "io.github.denisbronx.netmock:netmock-server:0.4.0" + testImplementation "io.github.denisbronx.netmock:netmock-server:0.5.0" //mutliplatform and lighter weight option for ktor only library users - testImplementation "io.github.denisbronx.netmock:netmock-engine:0.4.0" + testImplementation "io.github.denisbronx.netmock:netmock-engine:0.5.0" + //library for accessing local json files in the test folder + testImplementation "io.github.denisbronx.netmock:netmock-resources:0.5.0" } ``` @@ -167,7 +169,7 @@ by doing so, all the not mocked requests will return the specified response and ## Resources When working with request and response bodies, it may not be ideal to create string constants in your tests (i.e. long JSONs that compromise tests readability, sharing bodies between test classes...). -You can use instead read from a local file in your test `resources` folder: +You can instead read from a local file in your test `resources` folder: ```kotlin //Reads the text from the module's "src/test/resources/responses/products_response_body.json" file val responseBody = readFromResources("responses/products_response_body.json") diff --git a/build.gradle.kts b/build.gradle.kts index 600d058..2a2dca9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,10 +1,11 @@ plugins { - id("com.android.application") version android_plugin_version apply false - id("com.android.library") version android_plugin_version apply false - id("org.jetbrains.kotlin.android") version kotlin_version apply false - id("org.jetbrains.kotlin.jvm") version kotlin_version apply false - id("org.jetbrains.kotlin.plugin.serialization") version kotlin_version - id("org.jetbrains.kotlin.multiplatform") version kotlin_version apply false + alias(libs.plugins.android.application) apply false + alias(libs.plugins.android.library) apply false + alias(libs.plugins.kotlin.android) apply false + alias(libs.plugins.kotlin.jvm) apply false + alias(libs.plugins.kotlin.serialization) apply false + alias(libs.plugins.kotlin.multiplatform) apply false + alias(libs.plugins.kover) apply false id("maven-publish") - id("io.github.gradle-nexus.publish-plugin") version nexus_publish_version + alias(libs.plugins.nexus.publish) } diff --git a/buildSrc/.gitignore b/buildSrc/.gitignore deleted file mode 100644 index 00fd4dd..0000000 --- a/buildSrc/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/build -/.gradle \ No newline at end of file diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts deleted file mode 100644 index f01e2c3..0000000 --- a/buildSrc/build.gradle.kts +++ /dev/null @@ -1,7 +0,0 @@ -repositories { - gradlePluginPortal() -} - -plugins { - `kotlin-dsl` -} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt deleted file mode 100644 index 5f57e72..0000000 --- a/buildSrc/src/main/kotlin/Versions.kt +++ /dev/null @@ -1,13 +0,0 @@ -const val netmock_version = "0.4.0" -const val android_plugin_version = "7.4.1" -const val kotlin_version = "1.8.21" -const val junit_version = "4.13.1" -const val okhttp_version = "4.11.0" -const val serialization_version = "1.5.1" -const val coroutines_test_version = "1.7.1" -const val retrofit_version = "2.9.0" -const val retrofit_serialization_version = "1.0.0" -const val ktor_version = "2.3.1" -const val nexus_publish_version = "1.1.0" -const val kermit_version = "2.0.0-RC4" -const val kover_version = "0.7.1" diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml new file mode 100644 index 0000000..0fd6e57 --- /dev/null +++ b/gradle/libs.versions.toml @@ -0,0 +1,40 @@ +[versions] +agp = "8.6.1" +kotlin = "2.0.20" +junit = "4.13.2" +coroutines-test = "1.7.3" +kover = "0.8.3" +ktor = "2.3.12" +netmock = "0.5.0" +nexus-publish = "1.1.0" +okhttp = "4.12.0" +retrofit = "2.11.0" +retrofit-serialization = "1.0.0" +serialization-version = "1.7.3" +kermit = "2.0.0-RC4" + +[libraries] +kotlin-test = { group = "org.jetbrains.kotlin", name = "kotlin-test", version.ref = "kotlin"} +junit = { group = "junit", name = "junit", version.ref = "junit" } +coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "coroutines-test" } +ktor = { group = "io.ktor", name = "ktor-client-core", version.ref = "ktor" } +ktor-okhttp = { group = "io.ktor", name = "ktor-client-okhttp", version.ref = "ktor" } +ktor-cio = { group = "io.ktor", name = "ktor-client-cio", version.ref = "ktor" } +ktor-content-negotiation = { group = "io.ktor", name = "ktor-client-content-negotiation", version.ref = "ktor" } +ktor-serialization = { group = "io.ktor", name = "ktor-serialization-kotlinx-json", version.ref = "ktor" } +ktor-mock = { group = "io.ktor", name = "ktor-client-mock", version.ref = "ktor" } +kotlin-serialization = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "serialization-version" } +kermit = { group = "co.touchlab", name = "kermit", version.ref = "kermit" } +retrofit = { group = "com.squareup.retrofit2", name = "retrofit", version.ref = "retrofit" } +retrofit-converter-scalars= { group = "com.squareup.retrofit2", name = "converter-scalars", version.ref = "retrofit" } +retrofit-serialization = { group = "com.jakewharton.retrofit", name = "retrofit2-kotlinx-serialization-converter", version.ref = "retrofit-serialization" } + +[plugins] +android-application = { id = "com.android.application", version.ref = "agp" } +android-library = { id = "com.android.library", version.ref = "agp" } +kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" } +kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } +kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } +kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } +kover = { id = "org.jetbrains.kotlinx.kover", version.ref = "kover" } +nexus-publish = { id = "io.github.gradle-nexus.publish-plugin", version.ref = "nexus-publish" } diff --git a/netmock-core/build.gradle.kts b/netmock-core/build.gradle.kts index 5bc8f1a..ba32dd3 100644 --- a/netmock-core/build.gradle.kts +++ b/netmock-core/build.gradle.kts @@ -1,22 +1,22 @@ plugins { - kotlin("multiplatform") version "1.8.21" - id("org.jetbrains.kotlin.plugin.serialization") + alias(libs.plugins.kotlin.multiplatform) + alias(libs.plugins.kotlin.serialization) + alias(libs.plugins.kover) id("maven-publish") id("convention.publication") - id("org.jetbrains.kotlinx.kover") version kover_version } publishing { publications.withType { groupId = "io.github.denisbronx.netmock" artifactId = "netmock-core" - version = netmock_version + version = libs.versions.netmock.get() } } kotlin { + jvmToolchain(17) jvm { - jvmToolchain(11) withJava() testRuns["test"].executionTask.configure { useJUnit() @@ -24,12 +24,15 @@ kotlin { } val hostOs = System.getProperty("os.name") val isMingwX64 = hostOs.startsWith("Windows") - val nativeTarget = when { + when { hostOs == "Mac OS X" -> macosX64("native") hostOs == "Linux" -> linuxX64("native") isMingwX64 -> mingwX64("native") else -> throw GradleException("Host OS is not supported in Kotlin/Native.") } + iosX64() + iosArm64() + iosSimulatorArm64() js(IR) { nodejs() binaries.executable() @@ -44,13 +47,13 @@ kotlin { sourceSets { commonMain { dependencies { - implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:${serialization_version}") - implementation("co.touchlab:kermit:$kermit_version") + implementation(libs.kotlin.serialization) + implementation(libs.kermit) } } commonTest { dependencies { - implementation("org.jetbrains.kotlin:kotlin-test") + implementation(libs.kotlin.test) } } } diff --git a/netmock-core/src/commonMain/kotlin/com/denisbrandi/netmock/NetMock.kt b/netmock-core/src/commonMain/kotlin/com/denisbrandi/netmock/NetMock.kt index 834810a..28a6000 100644 --- a/netmock-core/src/commonMain/kotlin/com/denisbrandi/netmock/NetMock.kt +++ b/netmock-core/src/commonMain/kotlin/com/denisbrandi/netmock/NetMock.kt @@ -21,6 +21,22 @@ interface NetMock { */ fun addMock(request: NetMockRequest, response: NetMockResponse) + /** + * See [NetMock.addMock] + * Alternative way of adding request/response to the queue using builders. + * + * @param [request] The request that has to match with the intercepted HTTP request + * @param [response] Function that allows you to create a [NetMockResponse] using a builder. + */ + fun addMock( + request: NetMockRequest, + response: NetMockResponseBuilder.() -> Unit + ) { + val responseBuilder = NetMockResponseBuilder() + response(responseBuilder) + addMock(request, responseBuilder.build()) + } + /** * See [NetMock.addMock] * Alternative way of adding request/response to the queue using builders. diff --git a/netmock-core/src/commonTest/kotlin/com/denisbrandi/netmock/NetMockTest.kt b/netmock-core/src/commonTest/kotlin/com/denisbrandi/netmock/NetMockTest.kt index a23590a..57a9e38 100644 --- a/netmock-core/src/commonTest/kotlin/com/denisbrandi/netmock/NetMockTest.kt +++ b/netmock-core/src/commonTest/kotlin/com/denisbrandi/netmock/NetMockTest.kt @@ -52,12 +52,21 @@ class NetMockTest { @JsName("call_withRequest") @Test fun `EXPECT interface method properly called with all fields WHEN passing requests and responses`() { + sut.addMock(EXPECTED_REQUEST, EXPECTED_RESPONSE) + + assertEquals( + listOf(NetMockRequestResponse(EXPECTED_REQUEST, EXPECTED_RESPONSE)), + sut.allowedMocks + ) + } + + @JsName("call_withRequestAndBuilder") + @Test + fun `EXPECT interface method properly called with all fields WHEN passing requests and response builder`() { sut.addMock( - request = { - fromRequest(EXPECTED_REQUEST) - }, + request = EXPECTED_REQUEST, response = { - fromResponse(EXPECTED_RESPONSE) + fromBuilder(EXPECTED_RESPONSE_BUILDER) } ) diff --git a/netmock-engine/build.gradle.kts b/netmock-engine/build.gradle.kts index 967795c..c51e66e 100644 --- a/netmock-engine/build.gradle.kts +++ b/netmock-engine/build.gradle.kts @@ -1,22 +1,22 @@ plugins { - kotlin("multiplatform") version "1.8.21" - id("org.jetbrains.kotlin.plugin.serialization") + alias(libs.plugins.kotlin.multiplatform) + alias(libs.plugins.kotlin.serialization) + alias(libs.plugins.kover) id("maven-publish") id("convention.publication") - id("org.jetbrains.kotlinx.kover") version kover_version } publishing { publications.withType { groupId = "io.github.denisbronx.netmock" artifactId = "netmock-engine" - version = netmock_version + version = libs.versions.netmock.get() } } kotlin { + jvmToolchain(17) jvm { - jvmToolchain(11) withJava() testRuns["test"].executionTask.configure { useJUnit() @@ -24,30 +24,32 @@ kotlin { } val hostOs = System.getProperty("os.name") val isMingwX64 = hostOs.startsWith("Windows") - val nativeTarget = when { + when { hostOs == "Mac OS X" -> macosX64("native") hostOs == "Linux" -> linuxX64("native") isMingwX64 -> mingwX64("native") else -> throw GradleException("Host OS is not supported in Kotlin/Native.") } + iosX64() + iosArm64() + iosSimulatorArm64() sourceSets { commonMain { sourceSets { dependencies { api(project(":netmock-core")) - api(project(":netmock-resources")) - api("io.ktor:ktor-client-core:$ktor_version") - implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version") - api("io.ktor:ktor-client-mock:$ktor_version") + api(libs.ktor) + implementation(libs.ktor.serialization) + api(libs.ktor.mock) } } } commonTest { sourceSets { dependencies { - implementation("io.ktor:ktor-client-content-negotiation:$ktor_version") - implementation("org.jetbrains.kotlin:kotlin-test") - implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_test_version") + implementation(libs.ktor.content.negotiation) + implementation(libs.kotlin.test) + implementation(libs.coroutines.test) } } } diff --git a/netmock-engine/src/commonMain/kotlin/com/denisbrandi/netmock/engine/NetMockEngine.kt b/netmock-engine/src/commonMain/kotlin/com/denisbrandi/netmock/engine/NetMockEngine.kt index 73c0d22..35b7470 100644 --- a/netmock-engine/src/commonMain/kotlin/com/denisbrandi/netmock/engine/NetMockEngine.kt +++ b/netmock-engine/src/commonMain/kotlin/com/denisbrandi/netmock/engine/NetMockEngine.kt @@ -1,10 +1,9 @@ package com.denisbrandi.netmock.engine import com.denisbrandi.netmock.NetMock -import com.denisbrandi.netmock.interceptors.DefaultInterceptor -import com.denisbrandi.netmock.interceptors.RequestInterceptor +import com.denisbrandi.netmock.interceptors.* import com.denisbrandi.netmock.matchers.DefaultRequestMatcher -import io.ktor.client.engine.* +import io.ktor.client.engine.HttpClientEngine import io.ktor.client.engine.mock.* /** @@ -25,7 +24,7 @@ class NetMockEngine private constructor( val netMockRequestHandler = NetMockRequestHandler( DefaultInterceptor(DefaultRequestMatcher) ) - config.requestHandlers.add(netMockRequestHandler) + config.requestHandlers.add { netMockRequestHandler(it) } return NetMockEngine( MockEngine(config), netMockRequestHandler diff --git a/netmock-engine/src/commonMain/kotlin/com/denisbrandi/netmock/engine/NetMockRequestHandler.kt b/netmock-engine/src/commonMain/kotlin/com/denisbrandi/netmock/engine/NetMockRequestHandler.kt index abe15cb..0a4b52f 100644 --- a/netmock-engine/src/commonMain/kotlin/com/denisbrandi/netmock/engine/NetMockRequestHandler.kt +++ b/netmock-engine/src/commonMain/kotlin/com/denisbrandi/netmock/engine/NetMockRequestHandler.kt @@ -1,16 +1,14 @@ package com.denisbrandi.netmock.engine -import com.denisbrandi.netmock.engine.mappers.KtorRequestMapper -import com.denisbrandi.netmock.engine.mappers.KtorResponseMapper +import com.denisbrandi.netmock.engine.mappers.* import com.denisbrandi.netmock.interceptors.RequestInterceptor -import io.ktor.client.engine.mock.* import io.ktor.client.request.* internal class NetMockRequestHandler( ktorInterceptor: RequestInterceptor -) : suspend MockRequestHandleScope.(HttpRequestData) -> HttpResponseData, RequestInterceptor by ktorInterceptor { +) : RequestInterceptor by ktorInterceptor { - override suspend fun invoke(scope: MockRequestHandleScope, request: HttpRequestData): HttpResponseData { + operator fun invoke(request: HttpRequestData): HttpResponseData { val netMockResponse = intercept(KtorRequestMapper.mapRequest(request)) return KtorResponseMapper.mapResponse(netMockResponse) } diff --git a/netmock-engine/src/commonTest/resources/request_body.json b/netmock-engine/src/commonTest/kotlin/com/denisbrandi/netmock/engine/FixtureRequestBody.kt similarity index 53% rename from netmock-engine/src/commonTest/resources/request_body.json rename to netmock-engine/src/commonTest/kotlin/com/denisbrandi/netmock/engine/FixtureRequestBody.kt index e52e53c..8a68c4b 100644 --- a/netmock-engine/src/commonTest/resources/request_body.json +++ b/netmock-engine/src/commonTest/kotlin/com/denisbrandi/netmock/engine/FixtureRequestBody.kt @@ -1,5 +1,7 @@ -{ +package com.denisbrandi.netmock.engine + +const val REQUEST_BODY = """{ "id": "some body id", "message": "some body message", "data": "some body text" -} \ No newline at end of file +}""" diff --git a/netmock-engine/src/commonTest/kotlin/com/denisbrandi/netmock/engine/FixtureResponseBody.kt b/netmock-engine/src/commonTest/kotlin/com/denisbrandi/netmock/engine/FixtureResponseBody.kt new file mode 100644 index 0000000..77adf18 --- /dev/null +++ b/netmock-engine/src/commonTest/kotlin/com/denisbrandi/netmock/engine/FixtureResponseBody.kt @@ -0,0 +1,7 @@ +package com.denisbrandi.netmock.engine + +const val RESPONSE_BODY = """{ + "code": 200, + "message": "some message", + "data": "some text" +}""" diff --git a/netmock-engine/src/commonTest/kotlin/com/denisbrandi/netmock/engine/NetMockEngineTest.kt b/netmock-engine/src/commonTest/kotlin/com/denisbrandi/netmock/engine/NetMockEngineTest.kt index 3ce2be0..8738c60 100644 --- a/netmock-engine/src/commonTest/kotlin/com/denisbrandi/netmock/engine/NetMockEngineTest.kt +++ b/netmock-engine/src/commonTest/kotlin/com/denisbrandi/netmock/engine/NetMockEngineTest.kt @@ -1,21 +1,15 @@ package com.denisbrandi.netmock.engine -import com.denisbrandi.netmock.Method -import com.denisbrandi.netmock.NetMockRequest -import com.denisbrandi.netmock.NetMockRequestResponse -import com.denisbrandi.netmock.NetMockResponse -import com.denisbrandi.netmock.resources.readFromCommonResources -import io.ktor.client.* -import io.ktor.client.call.* -import io.ktor.client.plugins.contentnegotiation.* +import com.denisbrandi.netmock.* +import io.ktor.client.HttpClient +import io.ktor.client.call.body +import io.ktor.client.plugins.contentnegotiation.ContentNegotiation import io.ktor.client.request.* -import io.ktor.client.statement.* +import io.ktor.client.statement.HttpResponse import io.ktor.http.* -import io.ktor.serialization.kotlinx.json.* +import io.ktor.serialization.kotlinx.json.json import kotlin.js.JsName -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertTrue +import kotlin.test.* import kotlinx.coroutines.test.runTest import kotlinx.serialization.Serializable @@ -62,25 +56,26 @@ class NetMockEngineTest { @JsName("mappedResponseAndDefaultResponse_noMoreMocks") @Test - fun `EXPECT mapped responses and default response WHEN dispatcher runs out of mocks`() = runTest { - val expectedResponse1 = EXPECTED_RESPONSE.copy(body = "body1") - val expectedResponse2 = EXPECTED_RESPONSE.copy(body = "body2") - netMock.addMock(EXPECTED_COMPLETE_REQUEST, expectedResponse1) - netMock.addMock(EXPECTED_COMPLETE_REQUEST, expectedResponse2) + fun `EXPECT mapped responses and default response WHEN dispatcher runs out of mocks`() = + runTest { + val expectedResponse1 = EXPECTED_RESPONSE.copy(body = "body1") + val expectedResponse2 = EXPECTED_RESPONSE.copy(body = "body2") + netMock.addMock(EXPECTED_COMPLETE_REQUEST, expectedResponse1) + netMock.addMock(EXPECTED_COMPLETE_REQUEST, expectedResponse2) - val response1 = sut.request(getCompleteRequest(BASE_URL)) - val response2 = sut.request(getCompleteRequest(BASE_URL)) - val response3 = sut.request(getCompleteRequest(BASE_URL)) + val response1 = sut.request(getCompleteRequest(BASE_URL)) + val response2 = sut.request(getCompleteRequest(BASE_URL)) + val response3 = sut.request(getCompleteRequest(BASE_URL)) - assertEquals( - listOf(EXPECTED_COMPLETE_REQUEST, EXPECTED_COMPLETE_REQUEST), - netMock.interceptedRequests - ) - assertValidResponse(expectedResponse1, response1) - assertValidResponse(expectedResponse2, response2) - assertEquals(400, response3.status.value) - assertTrue(netMock.allowedMocks.isEmpty()) - } + assertEquals( + listOf(EXPECTED_COMPLETE_REQUEST, EXPECTED_COMPLETE_REQUEST), + netMock.interceptedRequests + ) + assertValidResponse(expectedResponse1, response1) + assertValidResponse(expectedResponse2, response2) + assertEquals(400, response3.status.value) + assertTrue(netMock.allowedMocks.isEmpty()) + } @JsName("validGET") @Test @@ -297,8 +292,6 @@ class NetMockEngineTest { private companion object { const val BASE_URL = "http://google.com" - val REQUEST_BODY = readFromCommonResources("request_body.json") - val RESPONSE_BODY = readFromCommonResources("response_body.json") val REQUEST_OBJECT = RequestObject("some body id", "some body message", "some body text") val RESPONSE_OBJECT = ResponseObject(200, "some message", "some text") val EXPECTED_COMPLETE_REQUEST = NetMockRequest( @@ -306,7 +299,8 @@ class NetMockEngineTest { method = Method.Get, mandatoryHeaders = mapOf("a" to "b", "c" to "d") ) - val EXPECTED_MISSING_FIELDS_REQUEST = NetMockRequest(requestUrl = "http://google.com/", method = Method.Get) + val EXPECTED_MISSING_FIELDS_REQUEST = + NetMockRequest(requestUrl = "http://google.com/", method = Method.Get) val EXPECTED_NOT_MATCHING_REQUEST = EXPECTED_COMPLETE_REQUEST.copy( mandatoryHeaders = mapOf("a" to "b", "c" to "d", "e" to "f") ) @@ -322,6 +316,7 @@ class NetMockEngineTest { ) val DEFAULT_RESPONSE = NetMockResponse(code = 201, mandatoryHeaders = mapOf("a" to "b"), body = "default") + private fun getCompleteRequest(baseUrl: String): HttpRequestBuilder { return getCompleteRequestBuilder(baseUrl).apply { method = HttpMethod.Get diff --git a/netmock-engine/src/commonTest/resources/response_body.json b/netmock-engine/src/commonTest/resources/response_body.json deleted file mode 100644 index a39e265..0000000 --- a/netmock-engine/src/commonTest/resources/response_body.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "code": 200, - "message": "some message", - "data": "some text" -} \ No newline at end of file diff --git a/netmock-integration-test/build.gradle.kts b/netmock-integration-test/build.gradle.kts index 2b0f4dc..8108c31 100644 --- a/netmock-integration-test/build.gradle.kts +++ b/netmock-integration-test/build.gradle.kts @@ -1,13 +1,13 @@ plugins { id("java-library") - id("org.jetbrains.kotlin.jvm") - id("org.jetbrains.kotlin.plugin.serialization") - id("org.jetbrains.kotlinx.kover") version kover_version + alias(libs.plugins.kotlin.jvm) + alias(libs.plugins.kotlin.serialization) + alias(libs.plugins.kover) } java { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 } dependencies { @@ -16,21 +16,21 @@ dependencies { kover(project(":netmock-core")) kover(project(":netmock-engine")) kover(project(":netmock-server")) - testImplementation("junit:junit:$junit_version") - testImplementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$serialization_version") - testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_test_version") + testImplementation(libs.junit) + testImplementation(libs.kotlin.serialization) + testImplementation(libs.coroutines.test) //retrofit - testImplementation(platform("com.squareup.okhttp3:okhttp-bom:$okhttp_version")) + testImplementation(platform("com.squareup.okhttp3:okhttp-bom:${libs.versions.okhttp.get()}")) testImplementation("com.squareup.okhttp3:okhttp") - testImplementation("com.squareup.retrofit2:retrofit:$retrofit_version") - testImplementation("com.squareup.retrofit2:converter-scalars:$retrofit_version") - testImplementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:$retrofit_serialization_version") + testImplementation(libs.retrofit) + testImplementation(libs.retrofit.converter.scalars) + testImplementation(libs.retrofit.serialization) //ktor - testImplementation("io.ktor:ktor-client-core:$ktor_version") - testImplementation("io.ktor:ktor-client-okhttp:$ktor_version") - testImplementation("io.ktor:ktor-client-cio:$ktor_version") - testImplementation("io.ktor:ktor-client-content-negotiation:$ktor_version") - testImplementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version") + testImplementation(libs.ktor) + testImplementation(libs.ktor.okhttp) + testImplementation(libs.ktor.cio) + testImplementation(libs.ktor.content.negotiation) + testImplementation(libs.ktor.serialization) } diff --git a/netmock-resources/build.gradle.kts b/netmock-resources/build.gradle.kts index 9d6d620..1d5d876 100644 --- a/netmock-resources/build.gradle.kts +++ b/netmock-resources/build.gradle.kts @@ -1,5 +1,5 @@ plugins { - kotlin("multiplatform") version "1.8.21" + alias(libs.plugins.kotlin.multiplatform) id("maven-publish") id("convention.publication") } @@ -8,13 +8,13 @@ publishing { publications.withType { groupId = "io.github.denisbronx.netmock" artifactId = "netmock-resources" - version = netmock_version + version = libs.versions.netmock.get() } } kotlin { + jvmToolchain(17) jvm { - jvmToolchain(11) withJava() testRuns["test"].executionTask.configure { useJUnit() @@ -22,7 +22,7 @@ kotlin { } val hostOs = System.getProperty("os.name") val isMingwX64 = hostOs.startsWith("Windows") - val nativeTarget = when { + when { hostOs == "Mac OS X" -> macosX64("native") hostOs == "Linux" -> linuxX64("native") isMingwX64 -> mingwX64("native") @@ -31,7 +31,7 @@ kotlin { sourceSets { commonTest { dependencies { - implementation("org.jetbrains.kotlin:kotlin-test") + implementation(libs.kotlin.test) } } } diff --git a/netmock-resources/src/nativeMain/kotlin/com/denisbrandi/netmock/resources/Resource.kt b/netmock-resources/src/nativeMain/kotlin/com/denisbrandi/netmock/resources/Resource.kt index 312ccaf..7e2ae07 100644 --- a/netmock-resources/src/nativeMain/kotlin/com/denisbrandi/netmock/resources/Resource.kt +++ b/netmock-resources/src/nativeMain/kotlin/com/denisbrandi/netmock/resources/Resource.kt @@ -1,7 +1,10 @@ +@file:OptIn(ExperimentalForeignApi::class) + package com.denisbrandi.netmock.resources import kotlinx.cinterop.ByteVar import kotlinx.cinterop.CPointer +import kotlinx.cinterop.ExperimentalForeignApi import kotlinx.cinterop.allocArray import kotlinx.cinterop.convert import kotlinx.cinterop.memScoped diff --git a/netmock-server/build.gradle.kts b/netmock-server/build.gradle.kts index 38474f7..a70b013 100644 --- a/netmock-server/build.gradle.kts +++ b/netmock-server/build.gradle.kts @@ -1,21 +1,21 @@ plugins { - kotlin("multiplatform") version "1.8.21" + alias(libs.plugins.kotlin.multiplatform) + alias(libs.plugins.kover) id("maven-publish") id("convention.publication") - id("org.jetbrains.kotlinx.kover") version kover_version } publishing { publications.withType { groupId = "io.github.denisbronx.netmock" artifactId = "netmock-server" - version = netmock_version + version = libs.versions.netmock.get() } } kotlin { + jvmToolchain(17) jvm { - jvmToolchain(11) withJava() testRuns["test"].executionTask.configure { useJUnit() @@ -27,7 +27,7 @@ kotlin { dependencies { api(project(":netmock-core")) api(project(":netmock-resources")) - implementation(platform("com.squareup.okhttp3:okhttp-bom:$okhttp_version")) + implementation(platform("com.squareup.okhttp3:okhttp-bom:${libs.versions.okhttp.get()}")) implementation("com.squareup.okhttp3:okhttp") implementation("com.squareup.okhttp3:mockwebserver") } diff --git a/scripts/installKtlint.sh b/scripts/installKtlint.sh index 296476c..5a7777e 100755 --- a/scripts/installKtlint.sh +++ b/scripts/installKtlint.sh @@ -1,3 +1,3 @@ #!/bin/sh set -e -curl -sSLO https://github.com/pinterest/ktlint/releases/download/0.49.1/ktlint && chmod a+x ktlint && sudo mv ktlint /usr/local/bin/ +curl -sSLO https://github.com/pinterest/ktlint/releases/download/1.3.1/ktlint && chmod a+x ktlint && sudo mv ktlint /usr/local/bin/ diff --git a/scripts/ktlintCheck.sh b/scripts/ktlintCheck.sh index 6379108..67b3d0d 100755 --- a/scripts/ktlintCheck.sh +++ b/scripts/ktlintCheck.sh @@ -1 +1 @@ -ktlint "**/*.kt" "!**/generated/**" "!**/build/**" --code-style=android_studio --color --color-name=RED +ktlint "**/*.kt" "!**/generated/**" "!**/build/**" --color --color-name=RED diff --git a/scripts/ktlintFormat.sh b/scripts/ktlintFormat.sh index be7ae79..ef845f8 100755 --- a/scripts/ktlintFormat.sh +++ b/scripts/ktlintFormat.sh @@ -1 +1 @@ -ktlint "**/*.kt" "!**/generated/**" "!**/build/**" -F --code-style=android_studio --color --color-name=RED +ktlint "**/*.kt" "!**/generated/**" "!**/build/**" -F --color --color-name=RED