Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-2: starts adding the HTTP layer #6

Merged
merged 1 commit into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions code/tic-tac-tow-service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ group = "pt.isel.daw"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17

val isArm = System.getProperty("os.arch") == "aarch64"
val isMac = System.getProperty("os.name").toLowerCase().contains("mac")

repositories {
mavenCentral()
}
Expand All @@ -34,6 +37,12 @@ dependencies {
implementation("org.postgresql:postgresql:42.5.0")

testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.boot:spring-boot-starter-webflux")

// TODO may need to consider other environments as well
if(isMac && isArm) {
runtimeOnly("io.netty:netty-resolver-dns-native-macos:4.1.82.Final:osx-aarch_64")
}
testImplementation(kotlin("test"))

ktlint("com.pinterest:ktlint:0.47.1")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pt.isel.daw.tictactow.http

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
import pt.isel.daw.tictactow.http.model.HomeOutputModel
import pt.isel.daw.tictactow.http.model.LinkOutputModel

@RestController
class HomeController {

@GetMapping(Uris.HOME)
fun getHome() = HomeOutputModel(
links = listOf(
LinkOutputModel(
Uris.home(),
LinkRelation.SELF
),
LinkOutputModel(
Uris.home(),
LinkRelation.HOME
),
)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pt.isel.daw.tictactow.http

class LinkRelation(
val value: String
) {
companion object {

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"
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pt.isel.daw.tictactow.http

import org.springframework.web.util.UriTemplate
import pt.isel.daw.tictactow.domain.Game
import java.net.URI

object Uris {

const val HOME = "/"
const val USER_HOME = "/me"
const val GAME_BY_ID = "/games/{gid}"

fun home(): URI = URI(HOME)
fun userHome(): URI = URI(USER_HOME)
fun gameById(game: Game) = UriTemplate(GAME_BY_ID).expand(game.id)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pt.isel.daw.tictactow.http.model

data class HomeOutputModel(
val links: List<LinkOutputModel>,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pt.isel.daw.tictactow.http.model

import pt.isel.daw.tictactow.http.LinkRelation
import java.net.URI

data class LinkOutputModel(
private val targetUri: URI,
private val relation: LinkRelation
) {
val href = targetUri.toASCIIString()
val rel = relation.value
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package pt.isel.daw.tictactow.http

import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.web.server.LocalServerPort
import org.springframework.test.web.reactive.server.WebTestClient
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse.BodyHandlers
import kotlin.test.assertEquals

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ExampleTests {

// One of the very few places where we use property injection
@LocalServerPort
var port: Int = 0

@Test
fun exampleTestUsingWebTestClient() {
val client = WebTestClient.bindToServer().baseUrl("http://localhost:$port").build()
client.get().uri("/")
.exchange()
.expectStatus().isOk
.expectBody()
.jsonPath("links[0].rel").isEqualTo("self")
}

@Test
fun exampleUsingHttpClient() {
val client = HttpClient.newHttpClient()
val response = client.send(
HttpRequest
.newBuilder()
.uri(URI("http://localhost:$port"))
.GET()
.build(),
BodyHandlers.ofString()
)
assertEquals(200, response.statusCode())
}
}