-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kotlin version of JAX-RS guide (#1225)
Close #1221
- Loading branch information
Showing
11 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
guides/micronaut-jaxrs-jdbc/kotlin/src/main/kotlin/example/micronaut/NameDto.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 example.micronaut | ||
|
||
import io.micronaut.serde.annotation.Serdeable | ||
|
||
@Serdeable // <1> | ||
data class NameDto(val name: String) |
15 changes: 15 additions & 0 deletions
15
guides/micronaut-jaxrs-jdbc/kotlin/src/main/kotlin/example/micronaut/Pet.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,15 @@ | ||
package example.micronaut | ||
|
||
import io.micronaut.data.annotation.MappedEntity | ||
import io.micronaut.serde.annotation.Serdeable | ||
import io.micronaut.data.annotation.GeneratedValue | ||
import io.micronaut.data.annotation.Id | ||
import javax.validation.constraints.NotBlank | ||
|
||
@Serdeable // <1> | ||
@MappedEntity // <2> | ||
data class Pet(@NotBlank val name: String, | ||
val type: PetType = PetType.DOG) { | ||
@Id // <3> | ||
@GeneratedValue(GeneratedValue.Type.AUTO) var id: Long? = null // <4> | ||
} |
16 changes: 16 additions & 0 deletions
16
guides/micronaut-jaxrs-jdbc/kotlin/src/main/kotlin/example/micronaut/PetRepository.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,16 @@ | ||
package example.micronaut | ||
|
||
import io.micronaut.data.jdbc.annotation.JdbcRepository | ||
import io.micronaut.data.model.query.builder.sql.Dialect | ||
import io.micronaut.data.repository.CrudRepository | ||
import java.util.* | ||
import javax.validation.constraints.NotBlank | ||
|
||
@JdbcRepository(dialect = Dialect.MYSQL) // <1> | ||
interface PetRepository : CrudRepository<Pet, Long> { // <2> | ||
fun list() : List<NameDto> // <3> | ||
|
||
fun findByName(@NotBlank name: String) : Optional<Pet> | ||
|
||
fun save(@NotBlank name: String, type: PetType) : Pet | ||
} |
26 changes: 26 additions & 0 deletions
26
guides/micronaut-jaxrs-jdbc/kotlin/src/main/kotlin/example/micronaut/PetResource.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,26 @@ | ||
package example.micronaut | ||
|
||
import io.micronaut.http.HttpStatus | ||
import io.micronaut.http.annotation.Body | ||
import io.micronaut.http.annotation.Status | ||
import javax.ws.rs.GET | ||
import javax.ws.rs.POST | ||
import javax.ws.rs.Path | ||
import javax.ws.rs.PathParam | ||
|
||
@Path("/pets") // <1> | ||
class PetResource(val petRepository: PetRepository) { // <2> | ||
|
||
@GET // <3> | ||
fun all() = petRepository.findAll() // <4> | ||
|
||
@GET // <3> | ||
@Path("/{name}") // <4> | ||
fun byName(@PathParam("name") petsName: String) = petRepository.findByName(petsName) // <5> | ||
|
||
@POST | ||
@Status(HttpStatus.CREATED) | ||
fun save(@Body petSave: PetSave) { | ||
petRepository.save(petSave.name, petSave.type) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
guides/micronaut-jaxrs-jdbc/kotlin/src/main/kotlin/example/micronaut/PetSave.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,7 @@ | ||
package example.micronaut | ||
|
||
import io.micronaut.serde.annotation.Serdeable | ||
import javax.validation.constraints.NotBlank | ||
|
||
@Serdeable | ||
data class PetSave(@NotBlank val name: String, val type: PetType) |
6 changes: 6 additions & 0 deletions
6
guides/micronaut-jaxrs-jdbc/kotlin/src/main/kotlin/example/micronaut/PetType.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 example.micronaut | ||
|
||
enum class PetType { | ||
DOG, | ||
CAT | ||
} |
97 changes: 97 additions & 0 deletions
97
guides/micronaut-jaxrs-jdbc/kotlin/src/test/kotlin/example/micronaut/PetResourceTest.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,97 @@ | ||
package example.micronaut | ||
|
||
import io.micronaut.core.type.Argument | ||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.HttpStatus | ||
import io.micronaut.http.client.HttpClient | ||
import io.micronaut.http.client.annotation.Client | ||
import io.micronaut.http.client.exceptions.HttpClientResponseException | ||
import io.micronaut.http.uri.UriBuilder | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest | ||
import jakarta.inject.Inject | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.Test | ||
|
||
@MicronautTest(transactional = false) // <1> | ||
class PetResourceTest { | ||
|
||
@Inject | ||
@field:Client("/") | ||
lateinit var httpClient : HttpClient // <2> | ||
|
||
@Inject | ||
lateinit var repository: PetRepository | ||
|
||
@Test | ||
fun testAll() { | ||
val dino = Pet("Dino") | ||
val pb = Pet("Baby Puss", PetType.CAT) | ||
val hoppy = Pet("Hoppy") | ||
|
||
repository.saveAll(listOf(dino, pb, hoppy)) | ||
|
||
val client = httpClient.toBlocking() | ||
val request: HttpRequest<Any> = HttpRequest.GET("/pets") | ||
val response = client.exchange<Any, MutableList<NameDto>>(request, | ||
Argument.listOf(NameDto::class.java)) | ||
assertEquals(HttpStatus.OK, response.status) | ||
val petNames = response.body() | ||
assertNotNull(petNames) | ||
assertEquals(3, petNames.size) | ||
repository.deleteAll() | ||
} | ||
|
||
@Test | ||
fun testGet() { | ||
val name = "Dino" | ||
val dino = Pet(name) | ||
val id = repository.save(dino).id | ||
val uri = UriBuilder.of("/pets").path(name).build() | ||
val request : HttpRequest<Any> = HttpRequest.GET(uri) | ||
val client = httpClient.toBlocking() | ||
val response = client.exchange(request, Pet::class.java) | ||
assertEquals(HttpStatus.OK, response.status) | ||
val pet = response.body() | ||
assertNotNull(pet) | ||
if (pet != null) { | ||
assertNotNull(pet.id) | ||
assertNotNull(pet.type) | ||
assertEquals(PetType.DOG, pet.type) | ||
assertEquals(name, pet.name) | ||
} | ||
if (id != null) { | ||
repository.deleteById(id) | ||
} | ||
} | ||
|
||
@Test | ||
fun testGetIfPetDoesNotExistsAPIReturnsNotFound() { | ||
val name = "Dino" | ||
val dino = Pet(name) | ||
val id = repository.save(dino).id | ||
val uri = UriBuilder.of("/pets").path("Foo").build() | ||
val client = httpClient.toBlocking() | ||
val request : HttpRequest<Any> = HttpRequest.GET(uri) | ||
val thrown: HttpClientResponseException = assertThrows(HttpClientResponseException::class.java) { | ||
client.exchange(request, Pet::class.java) | ||
} | ||
assertEquals(HttpStatus.NOT_FOUND, thrown.response.status()) // <3> | ||
if (id != null) { | ||
repository.deleteById(id) | ||
} | ||
} | ||
|
||
@Test | ||
fun testSave() { | ||
val name = "Dino" | ||
val oldCount = repository.count() | ||
val petSave = PetSave(name, PetType.DOG) | ||
val request: HttpRequest<Any> = HttpRequest.POST("/pets", petSave) | ||
val client = httpClient.toBlocking() | ||
val response = client.exchange(request, Pet::class.java) | ||
assertEquals(HttpStatus.CREATED, response.status()) | ||
val count = repository.count() | ||
assertEquals(oldCount + 1, count) | ||
repository.deleteAll() | ||
} | ||
} |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.