-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
gh-2: adds partial Siren support
- Loading branch information
Showing
10 changed files
with
264 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
code/tic-tac-tow-service/src/main/kotlin/pt/isel/daw/tictactow/http/LinkRelation.kt
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
code/tic-tac-tow-service/src/main/kotlin/pt/isel/daw/tictactow/http/Rels.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package pt.isel.daw.tictactow.http | ||
|
||
import pt.isel.daw.tictactow.infra.LinkRelation | ||
|
||
object Rels { | ||
|
||
val SELF = LinkRelation("self") | ||
|
||
val HOME = LinkRelation( | ||
"https://github.com/isel-leic-daw/s2223i-51d-51n-public/tree/main/code/tic-tac-tow-service/docs/" + | ||
"rels/home" | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
code/tic-tac-tow-service/src/main/kotlin/pt/isel/daw/tictactow/http/model/HomeOutputModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package pt.isel.daw.tictactow.http.model | ||
|
||
data class HomeOutputModel( | ||
val links: List<LinkOutputModel>, | ||
val credits: String, | ||
) |
12 changes: 0 additions & 12 deletions
12
code/tic-tac-tow-service/src/main/kotlin/pt/isel/daw/tictactow/http/model/LinkOutputModel.kt
This file was deleted.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
code/tic-tac-tow-service/src/main/kotlin/pt/isel/daw/tictactow/infra/LinkRelation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package pt.isel.daw.tictactow.infra | ||
|
||
@JvmInline | ||
value class LinkRelation( | ||
val value: String | ||
) |
122 changes: 122 additions & 0 deletions
122
code/tic-tac-tow-service/src/main/kotlin/pt/isel/daw/tictactow/infra/Siren.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package pt.isel.daw.tictactow.infra | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import org.springframework.http.HttpMethod | ||
import java.net.URI | ||
|
||
data class SirenModel<T>( | ||
@get:JsonProperty("class") | ||
val clazz: List<String>, | ||
val properties: T, | ||
val links: List<LinkModel>, | ||
val entities: List<EntityModel<*>>, | ||
val actions: List<ActionModel> | ||
) | ||
|
||
data class LinkModel( | ||
val rel: List<String>, | ||
val href: String, | ||
) | ||
|
||
data class EntityModel<T>( | ||
val properties: T, | ||
val links: List<LinkModel>, | ||
val rel: List<String>, | ||
) | ||
|
||
data class ActionModel( | ||
val name: String, | ||
val href: String, | ||
val method: String, | ||
val type: String, | ||
val fields: List<FieldModel>, | ||
) | ||
|
||
data class FieldModel( | ||
val name: String, | ||
val type: String, | ||
val value: String? = null, | ||
) | ||
|
||
class SirenBuilderScope<T>( | ||
val properties: T, | ||
) { | ||
private val links = mutableListOf<LinkModel>() | ||
private val entities = mutableListOf<EntityModel<*>>() | ||
private val classes = mutableListOf<String>() | ||
private val actions = mutableListOf<ActionModel>() | ||
|
||
fun clazz(value: String) { | ||
classes.add(value) | ||
} | ||
|
||
fun link(href: URI, rel: LinkRelation) { | ||
links.add(LinkModel(listOf(rel.value), href.toASCIIString())) | ||
} | ||
|
||
fun <U> entity(value: U, rel: LinkRelation, block: EntityBuilderScope<U>.() -> Unit) { | ||
val scope = EntityBuilderScope(value, listOf(rel.value)) | ||
scope.block() | ||
entities.add(scope.build()) | ||
} | ||
|
||
fun action(name: String, href: URI, method: HttpMethod, type: String, block: ActionBuilderScope.() -> Unit) { | ||
val scope = ActionBuilderScope(name, href, method, type) | ||
scope.block() | ||
actions.add(scope.build()) | ||
} | ||
|
||
fun build(): SirenModel<T> = SirenModel( | ||
clazz = classes, | ||
properties = properties, | ||
links = links, | ||
entities = entities, | ||
actions = actions | ||
) | ||
} | ||
|
||
class EntityBuilderScope<T>( | ||
val properties: T, | ||
val rel: List<String>, | ||
) { | ||
private val links = mutableListOf<LinkModel>() | ||
|
||
fun link(href: URI, rel: LinkRelation) { | ||
links.add(LinkModel(listOf(rel.value), href.toASCIIString())) | ||
} | ||
|
||
fun build(): EntityModel<T> = EntityModel( | ||
properties = properties, | ||
links = links, | ||
rel = rel, | ||
) | ||
} | ||
|
||
class ActionBuilderScope( | ||
private val name: String, | ||
private val href: URI, | ||
private val method: HttpMethod, | ||
private val type: String, | ||
) { | ||
private val fields = mutableListOf<FieldModel>() | ||
|
||
fun textField(name: String) { | ||
fields.add(FieldModel(name, "text")) | ||
} | ||
|
||
fun numberField(name: String) { | ||
fields.add(FieldModel(name, "number")) | ||
} | ||
|
||
fun hiddenField(name: String, value: String) { | ||
fields.add(FieldModel(name, "hidden", value)) | ||
} | ||
|
||
fun build() = ActionModel(name, href.toASCIIString(), method.name, type, fields) | ||
} | ||
|
||
fun <T> siren(value: T, block: SirenBuilderScope<T>.() -> Unit): SirenModel<T> { | ||
val scope = SirenBuilderScope(value) | ||
scope.block() | ||
return scope.build() | ||
} |
103 changes: 103 additions & 0 deletions
103
code/tic-tac-tow-service/src/test/kotlin/pt/isel/daw/tictactow/infra/SirenTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package pt.isel.daw.tictactow.infra | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import org.junit.jupiter.api.Test | ||
import org.skyscreamer.jsonassert.JSONAssert | ||
import org.springframework.http.HttpMethod | ||
import java.net.URI | ||
|
||
class SirenTests { | ||
|
||
@Test | ||
fun `can produce siren representation`() { | ||
// given: model classes | ||
class GameModel( | ||
val name: String, | ||
val description: String | ||
) | ||
|
||
class PlayerModel( | ||
val name: String, | ||
) | ||
|
||
// and: link relations | ||
val self = LinkRelation("self") | ||
val player = LinkRelation("https://example.com/rels/player") | ||
|
||
// and: a Jackson mapper | ||
val mapper = ObjectMapper().apply { | ||
setSerializationInclusion(JsonInclude.Include.NON_NULL) | ||
} | ||
|
||
// when: producing a Siren model | ||
val sirenModel = siren( | ||
GameModel( | ||
name = "the name", | ||
description = "the description", | ||
) | ||
) { | ||
clazz("game") | ||
link(URI("https://example.com/games/1"), self) | ||
entity(PlayerModel("Alice"), player) { | ||
link(URI("https://example.com/users/1"), self) | ||
} | ||
entity(PlayerModel("Bob"), player) { | ||
link(URI("https://example.com/users/2"), self) | ||
} | ||
action( | ||
"cancel", | ||
URI("https://example.com/games/1/cancel"), | ||
HttpMethod.POST, | ||
"application/json" | ||
) { | ||
textField("reason") | ||
} | ||
} | ||
|
||
// and: serializing it to JSON | ||
val jsonString = mapper.writeValueAsString(sirenModel) | ||
|
||
// then: the serialization is the expected one | ||
val expected = """ | ||
{ | ||
"class":["game"], | ||
"properties": { | ||
"name": "the name", | ||
"description": "the description" | ||
}, | ||
"entities":[ | ||
{ | ||
"rel": ["https://example.com/rels/player"], | ||
"properties": { | ||
"name": "Alice" | ||
}, | ||
"links": [ | ||
{"rel": ["self"], "href": "https://example.com/users/1"} | ||
] | ||
}, | ||
{ | ||
"rel": ["https://example.com/rels/player"], | ||
"properties": { | ||
"name": "Bob" | ||
}, | ||
"links": [ | ||
{"rel": ["self"], "href": "https://example.com/users/2"} | ||
] | ||
} | ||
], | ||
"links": [ | ||
{"rel": ["self"], "href": "https://example.com/games/1"} | ||
], | ||
"actions": [ | ||
{"name": "cancel", "href":"https://example.com/games/1/cancel", "method":"POST", | ||
"type": "application/json", | ||
"fields": [ | ||
{"name":"reason", "type": "text"} | ||
]} | ||
] | ||
} | ||
""".trimIndent() | ||
JSONAssert.assertEquals(expected, jsonString, true) | ||
} | ||
} |