Skip to content

Commit

Permalink
Added block patching
Browse files Browse the repository at this point in the history
Signed-off-by: Arnau Mora <arnyminerz@proton.me>
  • Loading branch information
ArnyminerZ committed Sep 4, 2023
1 parent f5ad728 commit 1343364
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,26 @@ data class BlockingRecurrenceYearly(
"to_day" to toDay,
"to_month" to toMonth
)

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as BlockingRecurrenceYearly

if (fromDay != other.fromDay) return false
if (fromMonth != other.fromMonth) return false
if (toDay != other.toDay) return false
if (toMonth != other.toMonth) return false

return true
}

override fun hashCode(): Int {
var result = fromDay.hashCode()
result = 31 * result + fromMonth.hashCode()
result = 31 * result + toDay.hashCode()
result = 31 * result + toMonth.hashCode()
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Blocking(id: EntityID<Int>) : BaseEntity(id), JsonSerializable {
else
null
set(value) {
println("Updating recurrence: $value")
_fromDay = value?.fromDay
_fromMonth = value?.fromMonth
_toDay = value?.toDay
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.arnyminerz.escalaralcoiaicomtat.backend.utils.getJSONObjectOrNull
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.getStringOrNull
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.json
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.jsonOf
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.ApplicationCall
import io.ktor.server.application.call
import io.ktor.server.request.receiveText
Expand Down Expand Up @@ -52,7 +53,8 @@ object AddBlockEndpoint: SecureEndpointBase() {
}

respondSuccess(
data = jsonOf("element" to blocking)
data = jsonOf("element" to blocking),
httpStatusCode = HttpStatusCode.Created
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.arnyminerz.escalaralcoiaicomtat.backend.server.endpoints.blocking

import com.arnyminerz.escalaralcoiaicomtat.backend.ServerDatabase
import com.arnyminerz.escalaralcoiaicomtat.backend.data.BlockingRecurrenceYearly
import com.arnyminerz.escalaralcoiaicomtat.backend.data.BlockingTypes
import com.arnyminerz.escalaralcoiaicomtat.backend.database.entity.Blocking
import com.arnyminerz.escalaralcoiaicomtat.backend.server.endpoints.EndpointBase
import com.arnyminerz.escalaralcoiaicomtat.backend.server.error.Errors
import com.arnyminerz.escalaralcoiaicomtat.backend.server.response.respondFailure
import com.arnyminerz.escalaralcoiaicomtat.backend.server.response.respondSuccess
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.areAllNull
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.getEnumOrNull
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.getJSONObjectOrNull
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.getStringOrNull
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.json
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.jsonOf
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.ApplicationCall
import io.ktor.server.application.call
import io.ktor.server.request.receiveText
import io.ktor.server.util.getValue
import io.ktor.util.pipeline.PipelineContext
import java.time.LocalDateTime

object PatchBlockEndpoint: EndpointBase() {
override suspend fun PipelineContext<Unit, ApplicationCall>.endpoint() {
val blockId: Int by call.parameters

val block: Blocking = ServerDatabase.instance.query { Blocking.findById(blockId) }
?: return respondFailure(Errors.ObjectNotFound)

val body = call.receiveText().json
val type = body.getEnumOrNull(BlockingTypes::class, "type")
val recurrence = body.getJSONObjectOrNull("recurrence")?.let(BlockingRecurrenceYearly::fromJson)
val endDate = body.getStringOrNull("end_date")?.let(LocalDateTime::parse)

if (areAllNull(type, recurrence, endDate)) {
return respondSuccess(httpStatusCode = HttpStatusCode.NoContent)
}

println("Updating block. Type=$type, recurrence=$recurrence, endDate=$endDate")

val updatedBlock = ServerDatabase.instance.query {
type?.let { block.type = it }
recurrence?.let { block.recurrence = it }
endDate?.let { block.endDate = it }

block.toJson()
}

respondSuccess(
jsonOf("element" to updatedBlock)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.arnyminerz.escalaralcoiaicomtat.backend.server.endpoints.RootEndpoint
import com.arnyminerz.escalaralcoiaicomtat.backend.server.endpoints.blocking.AddBlockEndpoint
import com.arnyminerz.escalaralcoiaicomtat.backend.server.endpoints.blocking.DeleteBlockEndpoint
import com.arnyminerz.escalaralcoiaicomtat.backend.server.endpoints.blocking.GetBlockEndpoint
import com.arnyminerz.escalaralcoiaicomtat.backend.server.endpoints.blocking.PatchBlockEndpoint
import com.arnyminerz.escalaralcoiaicomtat.backend.server.endpoints.create.NewAreaEndpoint
import com.arnyminerz.escalaralcoiaicomtat.backend.server.endpoints.create.NewPathEndpoint
import com.arnyminerz.escalaralcoiaicomtat.backend.server.endpoints.create.NewSectorEndpoint
Expand All @@ -30,6 +31,7 @@ import com.arnyminerz.escalaralcoiaicomtat.backend.system.EnvironmentVariables
import io.ktor.server.application.Application
import io.ktor.server.routing.delete
import io.ktor.server.routing.get
import io.ktor.server.routing.patch
import io.ktor.server.routing.post
import io.ktor.server.routing.routing

Expand Down Expand Up @@ -73,6 +75,7 @@ fun Application.configureEndpoints() {
post("/block/{pathId}") { AddBlockEndpoint.call(this) }
get("/block/{pathId}") { GetBlockEndpoint.call(this) }
delete("/block/{blockId}") { DeleteBlockEndpoint.call(this) }
patch("/block/{blockId}") { PatchBlockEndpoint.call(this) }

get("/file/{uuid}") { RequestFileEndpoint.call(this) }
get("/download/{uuid}") { DownloadFileEndpoint.call(this) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import org.json.JSONObject
* @param statusCode If the request doesn't respond OK (200), you can modify it here.
* @param block If any, allows handling the response if any.
*/
suspend fun HttpResponse.assertSuccess(
suspend inline fun HttpResponse.assertSuccess(
statusCode: HttpStatusCode = HttpStatusCode.OK,
block: ((data: JSONObject?) -> Unit)? = null
block: (data: JSONObject?) -> Unit = {}
) {
val json = bodyAsText().json
val error = json.getJSONObjectOrNull("error")
Expand All @@ -47,7 +47,7 @@ suspend fun HttpResponse.assertSuccess(
)

assertEquals(true, json.getBoolean("success"))
block?.invoke(json.getJSONObjectOrNull("data"))
block(json.getJSONObjectOrNull("data"))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.ktor.client.request.HttpRequestBuilder
import io.ktor.client.request.delete
import io.ktor.client.request.get
import io.ktor.client.request.header
import io.ktor.client.request.patch
import io.ktor.client.request.post
import io.ktor.http.HttpHeaders
import io.ktor.server.testing.ApplicationTestBuilder
Expand Down Expand Up @@ -69,6 +70,14 @@ abstract class ApplicationTestBase {
block()
}

suspend fun ApplicationTestBuilder.patch(
urlString: String,
block: HttpRequestBuilder.() -> Unit = {}
) = client.patch(urlString) {
header(HttpHeaders.Authorization, "Bearer $AUTH_TOKEN")
block()
}

suspend fun ApplicationTestBuilder.delete(
urlString: String,
block: HttpRequestBuilder.() -> Unit = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.arnyminerz.escalaralcoiaicomtat.backend.server.error.Errors
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.getJSONObjectOrNull
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.jsonOf
import io.ktor.client.request.setBody
import io.ktor.http.HttpStatusCode
import java.time.LocalDateTime
import java.time.Month
import java.time.temporal.ChronoUnit
Expand All @@ -36,7 +37,7 @@ class TestAddBlockEndpoint: ApplicationTestBase() {
).toString()
)
}.apply {
assertSuccess {
assertSuccess(HttpStatusCode.Created) {
assertNotNull(it?.getJSONObjectOrNull("element"))
}
}
Expand Down Expand Up @@ -118,7 +119,7 @@ class TestAddBlockEndpoint: ApplicationTestBase() {
).toString()
)
}.apply {
assertSuccess {
assertSuccess(HttpStatusCode.Created) {
assertNotNull(it?.getJSONObjectOrNull("element"))
}
}
Expand Down Expand Up @@ -151,7 +152,7 @@ class TestAddBlockEndpoint: ApplicationTestBase() {
).toString()
)
}.apply {
assertSuccess {
assertSuccess(HttpStatusCode.Created) {
assertNotNull(it?.getJSONObjectOrNull("element"))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.arnyminerz.escalaralcoiaicomtat.backend.server.DataProvider
import com.arnyminerz.escalaralcoiaicomtat.backend.server.base.ApplicationTestBase
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.jsonOf
import io.ktor.client.request.setBody
import io.ktor.http.HttpStatusCode
import kotlin.test.Test
import kotlin.test.assertNotNull
import kotlin.test.assertNull
Expand All @@ -28,7 +29,7 @@ class TestDeleteBlockEndpoint: ApplicationTestBase() {
).toString()
)
}.apply {
assertSuccess()
assertSuccess(HttpStatusCode.Created)
}

val block = ServerDatabase.instance.query {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.arnyminerz.escalaralcoiaicomtat.backend.utils.getJSONObjectOrNull
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.getStringOrNull
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.jsonOf
import io.ktor.client.request.setBody
import io.ktor.http.HttpStatusCode
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
Expand All @@ -30,7 +31,7 @@ class TestGetBlockEndpoint: ApplicationTestBase() {
).toString()
)
}.apply {
assertSuccess()
assertSuccess(HttpStatusCode.Created)
}

get("/block/$pathId").apply {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.arnyminerz.escalaralcoiaicomtat.backend.server.endpoints.block

import com.arnyminerz.escalaralcoiaicomtat.backend.assertions.assertSuccess
import com.arnyminerz.escalaralcoiaicomtat.backend.data.BlockingRecurrenceYearly
import com.arnyminerz.escalaralcoiaicomtat.backend.data.BlockingTypes
import com.arnyminerz.escalaralcoiaicomtat.backend.server.DataProvider
import com.arnyminerz.escalaralcoiaicomtat.backend.server.base.ApplicationTestBase
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.getJSONObjectOrNull
import com.arnyminerz.escalaralcoiaicomtat.backend.utils.jsonOf
import io.ktor.client.request.setBody
import io.ktor.http.HttpStatusCode
import java.time.LocalDateTime
import java.time.Month
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import org.json.JSONObject

class TestPatchBlockEndpoint: ApplicationTestBase() {
private fun patch(parameterName: String, parameterValue: Any, assert: (element: JSONObject) -> Unit) = test {
val areaId = DataProvider.provideSampleArea()
val zoneId = DataProvider.provideSampleZone(areaId)
val sectorId = DataProvider.provideSampleSector(zoneId)
val pathId = DataProvider.provideSamplePath(sectorId)

var blockId: Int? = null

post("/block/$pathId") {
setBody(
jsonOf(
"type" to BlockingTypes.BUILD
).toString()
)
}.apply {
assertSuccess(HttpStatusCode.Created) {
val element = it?.getJSONObjectOrNull("element")
assertNotNull(element)
blockId = element.getInt("id")
}
}
assertNotNull(blockId)

patch("/block/$blockId") {
setBody(
jsonOf(parameterName to parameterValue).toString()
)
}.apply {
assertSuccess {
val element = it?.getJSONObjectOrNull("element")
assertNotNull(element)
assert(element)
}
}
}

@Test
fun `test block patching - type`() = patch("type", BlockingTypes.BIRD) {
assertEquals(BlockingTypes.BIRD, it.getEnum(BlockingTypes::class.java, "type"))
}

@Test
fun `test block patching - recurrence`() {
val recurrence = BlockingRecurrenceYearly(1U, Month.MARCH, 1U, Month.JULY)

patch("recurrence", recurrence) {
val actual = BlockingRecurrenceYearly.fromJson(it.getJSONObject("recurrence"))

assertEquals(recurrence, actual)
}
}

@Test
fun `test block patching - endDate`() {
val endDate = LocalDateTime.of(2023, 1, 1, 0, 0, 0, 0)

patch("end_date", endDate.toString()) {
assertEquals(endDate.toString(), it.getString("end_date"))
}
}
}

0 comments on commit 1343364

Please sign in to comment.