Skip to content

Commit 3e622ea

Browse files
committed
Add tests + user creation
1 parent 473ccc2 commit 3e622ea

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed

src/main/kotlin/org/uqbar/tareas/controller/UsuariosController.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package org.uqbar.tareas.controller
22

33
import org.springframework.web.bind.annotation.CrossOrigin
44
import org.springframework.web.bind.annotation.GetMapping
5+
import org.springframework.web.bind.annotation.PostMapping
6+
import org.springframework.web.bind.annotation.RequestBody
57
import org.springframework.web.bind.annotation.RestController
8+
import org.uqbar.tareas.domain.Usuario
69
import org.uqbar.tareas.service.UsuariosService
710

811
@RestController
@@ -14,4 +17,6 @@ class UsuariosController(
1417
@GetMapping("/usuarios")
1518
fun usuarios() = usuariosService.allInstances()
1619

20+
@PostMapping("/usuarios")
21+
fun crear(@RequestBody usuario: Usuario) = usuariosService.crear(usuario)
1722
}

src/main/kotlin/org/uqbar/tareas/domain/Usuario.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.uqbar.tareas.domain
22

3-
class Usuario(val nombre: String) : Entity() {
3+
class Usuario(var nombre: String = "") : Entity() {
44

55
val tareasAsignadas: MutableList<Tarea> = mutableListOf()
66

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package org.uqbar.tareas.service
22

33
import org.springframework.stereotype.Service
4+
import org.uqbar.tareas.domain.Usuario
45
import org.uqbar.tareas.repository.UsuariosRepository
56

67
@Service
78
class UsuariosService(
8-
val usuariosRepository: UsuariosRepository
9+
val usuariosRepository: UsuariosRepository
910
) {
1011

11-
fun allInstances() = usuariosRepository.allInstances()
12+
fun allInstances() = usuariosRepository.allInstances()
13+
fun crear(usuario: Usuario) = usuariosRepository.create(usuario)
1214

1315
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.uqbar.tareas.controller
2+
3+
import org.junit.jupiter.api.DisplayName
4+
import org.junit.jupiter.api.Test
5+
import org.springframework.beans.factory.annotation.Autowired
6+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
7+
import org.springframework.boot.test.context.SpringBootTest
8+
import org.springframework.test.web.servlet.MockMvc
9+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
10+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
11+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
12+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
13+
14+
@SpringBootTest
15+
@AutoConfigureMockMvc
16+
@DisplayName("Dado un controller health")
17+
class HealthControllerTest {
18+
// region GET /health
19+
@Test
20+
fun `devuelve UP si esta levantado`(@Autowired mockMvc: MockMvc) {
21+
mockMvc
22+
.perform(MockMvcRequestBuilders.get("/health"))
23+
.andExpect(status().isOk)
24+
.andExpect(content().contentType("application/json"))
25+
.andExpect(jsonPath("$.status").value("UP"))
26+
}
27+
// endregion
28+
}

src/test/kotlin/org/uqbar/tareas/controller/UsuariosControllerTest.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.junit.jupiter.api.Test
66
import org.springframework.beans.factory.annotation.Autowired
77
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
88
import org.springframework.boot.test.context.SpringBootTest
9+
import org.springframework.http.MediaType
910
import org.springframework.test.web.servlet.MockMvc
1011
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
1112
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
@@ -44,4 +45,19 @@ class UsuariosControllerTest {
4445
.andExpect(jsonPath("$.length()").value(5))
4546
}
4647

48+
@Test
49+
fun `se puede crear un usuarie`() {
50+
mockMvc
51+
.perform(MockMvcRequestBuilders
52+
.post("/usuarios")
53+
.contentType(MediaType.APPLICATION_JSON)
54+
.content("""
55+
{ "nombre": "Fernando Dodino" }
56+
""".trimIndent())
57+
)
58+
.andExpect(status().isOk)
59+
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
60+
.andExpect(jsonPath("$.nombre").value("Fernando Dodino"))
61+
}
62+
4763
}

0 commit comments

Comments
 (0)