Skip to content

Commit

Permalink
Add JsonUptodateSchemaTest
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmytro Marchuk committed Jul 4, 2023
1 parent 5f34ea2 commit d877d1e
Showing 15 changed files with 148 additions and 46 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/docker-onpush.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Push to DockerHub
on: [ push ]
env:
DOCKER_USER: ${{secrets.DOCKER_USER}}
DOCKER_PASSWORD: ${{secrets.DOCKER_PASSWORD}}
jobs:
push-to-docker-hub:
runs-on: ubuntu-latest
if: ${{ contains(github.event.head_commit.message, '#test') }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 11
cache: gradle
- name: Code build
run: ./gradlew build --no-daemon -x test -x check
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@ dependencies {
testImplementation("com.willowtreeapps.assertk:assertk-jvm:0.25")
testImplementation("ch.qos.logback:logback-core:$logback")
testImplementation("ch.qos.logback:logback-classic:$logback")
testImplementation("io.projectreactor:reactor-tools:3.5.7")
testImplementation(kotlin("test"))
}

Original file line number Diff line number Diff line change
@@ -245,10 +245,10 @@ class LunchmoneyApi internal constructor(

fun createCategory(
name: String,
isIncome: Boolean,
excludeFromBudget: Boolean,
excludeFromTotals: Boolean,
description: String? = null,
isIncome: Boolean? = null,
excludeFromBudget: Boolean? = null,
excludeFromTotals: Boolean? = null,
categoryIds: List<Long>? = null,
groupId: Long? = null
): Mono<Long> = execute(
@@ -283,20 +283,20 @@ class LunchmoneyApi internal constructor(

fun updateCategory(
categoryId: Long,
isIncome: Boolean,
excludeFromBudget: Boolean,
excludeFromTotals: Boolean,
name: String? = null,
description: String? = null,
isIncome: Boolean? = null,
excludeFromBudget: Boolean? = null,
excludeFromTotals: Boolean? = null,
categoryIds: List<Long>? = null,
groupId: Long? = null
): Mono<Boolean> = execute(
UpdateCategoryRequest(
categoryId,
CreateUpdateCategoryRequestParams(
name = name,
description = description,
isIncome = isIncome,
description = description,
excludeFromBudget = excludeFromBudget,
excludeFromTotals = excludeFromTotals,
categoryIds = categoryIds,
@@ -477,6 +477,5 @@ class LunchmoneyApi internal constructor(

companion object {
const val LUNCHMONEY_DEV_BASE_URL = "https://dev.lunchmoney.app/v1"
const val LUNCHMONEY_APP_BASE_URL = "https://my.lunchmoney.app"
}
}
Original file line number Diff line number Diff line change
@@ -48,6 +48,11 @@ internal class RequestExecutor(
.send(Mono.just(body))
.responseSingle { resp, byteBufMono ->
processResponse(resp, byteBufMono, responseSerializer)
.doOnNext { model ->
log.debug { "Response (${resp.status()}): $model" }
}
}.doOnSubscribe {
log.debug { "Performing Lunchmoney API request $request" }
}
}

Original file line number Diff line number Diff line change
@@ -8,17 +8,24 @@ class LunchmoneyApiResponseException : LunchmoneyApiException {
val statusCode: Int
val body: String

constructor(body: String, cause: Throwable?, statusCode: Int) : super(cause!!) {
apiErrorResponse = null
constructor(
body: String,
cause: Throwable?,
statusCode: Int
) : super(cause!!) {
this.apiErrorResponse = null
this.body = body
this.statusCode = statusCode
}

constructor(
apiErrorResponse: ApiErrorResponse?,
body: String,
apiErrorResponse: ApiErrorResponse,
statusCode: Int
) : super(apiErrorResponse?.message ?: "Received erroneous response from Lunchmoney API") {
) : super(
apiErrorResponse.message ?: apiErrorResponse.error?.joinToString(", ")
?: "Received erroneous response from Lunchmoney API"
) {
this.apiErrorResponse = apiErrorResponse
this.body = body
this.statusCode = statusCode
Original file line number Diff line number Diff line change
@@ -9,4 +9,7 @@ internal abstract class LunchmoneyAbstractApiRequest<TResponse, TBody : Any> {

fun pathAndQuery() = pathAndQuery.toString()
open fun body(): TBody? = null

override fun toString(): String =
"${method()} $pathAndQuery${body()?.let { ": $it" } ?: ""}"
}
Original file line number Diff line number Diff line change
@@ -5,10 +5,10 @@ import kotlinx.serialization.Serializable
@Serializable
internal class CreateUpdateCategoryRequestParams(
val name: String?,
val isIncome: Boolean,
val excludeFromBudget: Boolean,
val excludeFromTotals: Boolean,
val description: String?,
val isIncome: Boolean?,
val excludeFromBudget: Boolean?,
val excludeFromTotals: Boolean?,
val categoryIds: List<Long>?,
val groupId: Long?
)
Original file line number Diff line number Diff line change
@@ -14,6 +14,6 @@ data class ApiErrorResponse(
var dependents: LunchmoneyCategoryDeletionDependency? = null
) {
fun toException(body: String, statusCode: Int): LunchmoneyApiResponseException {
return LunchmoneyApiResponseException(this, body, statusCode)
return LunchmoneyApiResponseException(body, this, statusCode)
}
}
13 changes: 2 additions & 11 deletions src/test/kotlin/io/github/smaugfm/lunchmoney/AuthorizationTest.kt
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@ import assertk.assertions.isNotNull
import assertk.assertions.prop
import io.github.smaugfm.lunchmoney.exception.LunchmoneyApiResponseException
import io.github.smaugfm.lunchmoney.request.user.GetCurrentUserRequest
import io.github.smaugfm.lunchmoney.response.ApiErrorResponse
import org.junit.jupiter.api.Test

internal class AuthorizationTest : TestMockServerBase() {
@@ -28,15 +27,7 @@ internal class AuthorizationTest : TestMockServerBase() {
.cause()
.isNotNull()
.isInstanceOf(LunchmoneyApiResponseException::class)
.prop(LunchmoneyApiResponseException::apiErrorResponse)
.isNotNull()
.isEqualTo(
ApiErrorResponse(
"Error",
"Access token does not exist.",
null,
null
)
)
.prop(LunchmoneyApiResponseException::message)
.isEqualTo("Access token does not exist.")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.github.smaugfm.lunchmoney

import assertk.assertThat
import assertk.assertions.hasSize
import assertk.assertions.isEqualTo
import assertk.assertions.isTrue
import assertk.assertions.prop
import io.github.smaugfm.lunchmoney.api.LunchmoneyApi
import io.github.smaugfm.lunchmoney.model.LunchmoneyCategorySingle
import io.github.smaugfm.lunchmoney.model.LunchmoneyUser
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable
import reactor.tools.agent.ReactorDebugAgent

@EnabledIfEnvironmentVariable(named = "LUNCHMONEY_TEST_TOKEN", matches = "\\w+")
class JsonSchemaUpToDateTest {

companion object {
@JvmStatic
@BeforeAll
fun initReactor() {
ReactorDebugAgent.init()
}
}

private val api = LunchmoneyApi(System.getenv("LUNCHMONEY_TEST_TOKEN"))

@Test
fun getUserTest() {
val user = api.getCurrentUser().block()!!
assertThat(user).prop(LunchmoneyUser::userName)
.isEqualTo("vasa")
assertThat(user).prop(LunchmoneyUser::userEmail)
.isEqualTo("vasa@vasa.com")
assertThat(user).prop(LunchmoneyUser::budgetName)
.isEqualTo("test")
assertThat(user).prop(LunchmoneyUser::apiKeyLabel)
.isEqualTo("Github Actions")
}

@Test
fun getAllCategoriesTest() {
val categories = api.getAllCategories().block()!!
assertThat(categories)
.hasSize(11)
}

@Test
fun getSingleCategoryTest() {
val cat = api.getSingleCategory(489281).block()!!
assertThat(cat)
.prop(LunchmoneyCategorySingle::id)
.isEqualTo(489281)
}

@Test
fun crudCategoryTest() {
val catName = "test-category"
val catName2 = "test-category2"
val id = api.createCategory(catName, false, true, true).block()!!
try {
var cat = api.getSingleCategory(id).block()!!
assertThat(cat)
.prop(LunchmoneyCategorySingle::id)
.isEqualTo(id)
assertThat(cat)
.prop(LunchmoneyCategorySingle::name)
.isEqualTo(catName)

assertThat(api.updateCategory(id, true, false, false, catName2).block()!!).isTrue()
cat = api.getSingleCategory(id).block()!!
assertThat(cat)
.prop(LunchmoneyCategorySingle::name)
.isEqualTo(catName2)

} finally {
assertThat(api.forceDeleteCategory(id).block()!!).isTrue()
}
}
}
Original file line number Diff line number Diff line change
@@ -63,9 +63,7 @@ internal class CreateCategoryGroupRequestTest : TestMockServerBase() {
.cause()
.isNotNull()
.isInstanceOf(LunchmoneyApiResponseException::class)
.prop(LunchmoneyApiResponseException::apiErrorResponse)
.isNotNull()
.prop(ApiErrorResponse::error)
.prop(LunchmoneyApiResponseException::message)
.isEqualTo(listOf("A category with the same name (vasa) already exists."))
}
}
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ import io.github.smaugfm.lunchmoney.TestMockServerBase
import io.github.smaugfm.lunchmoney.Util.getResourceAsString
import io.github.smaugfm.lunchmoney.exception.LunchmoneyApiResponseException
import io.github.smaugfm.lunchmoney.request.category.params.CreateUpdateCategoryRequestParams
import io.github.smaugfm.lunchmoney.response.ApiErrorResponse
import io.github.smaugfm.lunchmoney.response.CreateCategoryResponse
import org.junit.jupiter.api.Test
import org.mockserver.model.HttpRequest.request
@@ -34,9 +33,9 @@ internal class CreateCategoryRequestTest : TestMockServerBase() {
val request = CreateCategoryRequest(
CreateUpdateCategoryRequestParams(
"vasa",
null,
null,
null,
false,
false,
false,
null,
null,
null
@@ -61,9 +60,9 @@ internal class CreateCategoryRequestTest : TestMockServerBase() {
val createCategoryRequest = CreateCategoryRequest(
CreateUpdateCategoryRequestParams(
"vasa",
null,
null,
null,
false,
false,
false,
null,
null,
null
@@ -75,9 +74,7 @@ internal class CreateCategoryRequestTest : TestMockServerBase() {
.cause()
.isNotNull()
.isInstanceOf(LunchmoneyApiResponseException::class)
.prop(LunchmoneyApiResponseException::apiErrorResponse)
.isNotNull()
.prop(ApiErrorResponse::error)
.isEqualTo(listOf("A category with the same name (vasa) already exists."))
.prop(LunchmoneyApiResponseException::message)
.isEqualTo("A category with the same name (vasa) already exists.")
}
}
Original file line number Diff line number Diff line change
@@ -24,9 +24,9 @@ internal class UpdateCategoryRequestTest : TestMockServerBase() {
id,
CreateUpdateCategoryRequestParams(
"vasa",
null,
null,
null,
true,
true,
true,
null,
null,
null
2 changes: 1 addition & 1 deletion src/test/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<logger name="io.github.smaugfm.lunchmoney" level="DEBUG"/>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"error": "A category with the same name (vasa) already exists."
"error": [
"A category with the same name (vasa) already exists."
]
}

0 comments on commit d877d1e

Please sign in to comment.