Skip to content

Commit 5807091

Browse files
authored
Merge pull request #3 from G-khan/develop
integration tests added
2 parents 0a238ec + 6defb88 commit 5807091

File tree

9 files changed

+178
-16
lines changed

9 files changed

+178
-16
lines changed

src/main/kotlin/com/gokhana/demo/controller/UserController.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.gokhana.demo.controller
22

33
import com.gokhana.demo.model.UserDTO
4-
import com.gokhana.demo.model.toUserDTO
54
import com.gokhana.demo.service.UserService
65
import org.springframework.beans.factory.annotation.Autowired
76
import org.springframework.http.HttpStatus
7+
import org.springframework.http.MediaType
88
import org.springframework.http.ResponseEntity
99
import org.springframework.web.bind.annotation.*
1010
import javax.validation.Valid
@@ -17,9 +17,9 @@ class UserController {
1717
@Autowired
1818
lateinit var userService: UserService
1919

20-
@PostMapping
20+
@PostMapping(consumes = [MediaType.APPLICATION_JSON_VALUE], produces = [MediaType.APPLICATION_JSON_VALUE])
2121
fun userRegister(@RequestBody @Valid userDTO: UserDTO): ResponseEntity<UserDTO> {
22-
val savedUser = userService.createUser(userDTO).toUserDTO()
22+
val savedUser = userService.createUser(userDTO)
2323
return ResponseEntity.status(HttpStatus.CREATED).body(savedUser)
2424
}
2525

@@ -29,8 +29,8 @@ class UserController {
2929
val userDTO = userService.retrieveUser(username)
3030
return when (userDTO) {
3131
null -> ResponseEntity.notFound().build()
32-
else -> { // Note the block
33-
ResponseEntity.status(HttpStatus.CREATED).body(userDTO)
32+
else -> {
33+
ResponseEntity.status(HttpStatus.OK).body(userDTO)
3434
}
3535
}
3636
}

src/main/kotlin/com/gokhana/demo/entity/User.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import javax.persistence.*
44

55
@Entity
66
data class User(
7-
@Column(unique=true)
8-
val username: String,
9-
val name: String,
107
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
11-
val id: Long
8+
val id: Long,
9+
val name: String,
10+
@Column(unique = true)
11+
val username: String
1212
)

src/main/kotlin/com/gokhana/demo/model/UserDTO.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ data class UserDTO(
66
val id: Long,
77
@field:NotEmpty
88
val name: String,
9-
@field:NotEmpty
9+
@field:NotEmpty(message = "Username can not empty or null!")
1010
val username: String
1111
)
1212

src/main/kotlin/com/gokhana/demo/repository/UserRepository.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package com.gokhana.demo.repository
22

33
import com.gokhana.demo.entity.User
44
import org.springframework.data.repository.CrudRepository
5-
import java.util.*
65

76
interface UserRepository : CrudRepository<User, Long> {
8-
fun findByUsername(username: String): Optional<User>
7+
fun findByUsername(username: String): User?
98
}

src/main/kotlin/com/gokhana/demo/service/UserService.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ interface UserService {
99
fun saveUser(user: User): User
1010
fun getUser(username: String): User?
1111

12-
fun createUser(userDTO: UserDTO): User {
13-
return saveUser(userDTO.toUser())
12+
fun createUser(userDTO: UserDTO): UserDTO {
13+
return saveUser(userDTO.toUser()).toUserDTO()
1414
}
1515

16-
fun retrieveUser(username:String) : UserDTO? {
16+
fun retrieveUser(username: String): UserDTO? {
1717
return getUser(username)?.toUserDTO()
1818
}
1919

src/main/kotlin/com/gokhana/demo/service/UserServiceImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ class UserServiceImpl : UserService {
1616
}
1717

1818
override fun getUser(username: String): User? {
19-
return userRepository.findByUsername(username).orElse(null)
19+
return userRepository.findByUsername(username)
2020
}
2121
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.gokhana.demo
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper
4+
import com.gokhana.demo.model.UserDTO
5+
import com.gokhana.demo.service.UserService
6+
import org.junit.jupiter.api.Test
7+
import org.mockito.Mockito
8+
import org.mockito.internal.matchers.Any
9+
import org.springframework.beans.factory.annotation.Autowired
10+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
11+
import org.springframework.boot.test.mock.mockito.MockBean
12+
import org.springframework.http.MediaType
13+
import org.springframework.test.web.servlet.MockMvc
14+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
15+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
16+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
17+
18+
@WebMvcTest
19+
class UserControllerTests(@Autowired val mockMvc: MockMvc) {
20+
21+
private val mapper = ObjectMapper()
22+
23+
@MockBean
24+
lateinit var userService: UserService
25+
26+
@Test
27+
fun `When create user then user created with expected id`() {
28+
val userDTO = UserDTO(1, "Gökhan", "G-khan")
29+
Mockito.`when`(userService.createUser(userDTO)).thenReturn(userDTO)
30+
mockMvc.perform(
31+
post("/users").contentType(MediaType.APPLICATION_JSON_VALUE).content(mapper.writeValueAsString(userDTO))
32+
.accept(MediaType.APPLICATION_JSON)
33+
)
34+
.andExpect(status().isCreated)
35+
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
36+
.andExpect(jsonPath("id").value(userDTO.id))
37+
}
38+
39+
@Test
40+
fun `when retrieve user while user is exists then user returns with expected id`() {
41+
val userDTO = UserDTO(1, "Gökhan", "G-khan")
42+
Mockito.`when`(userService.retrieveUser(userDTO.username)).thenReturn(userDTO)
43+
mockMvc.perform(
44+
get("/users").param("user", "G-khan").contentType(MediaType.APPLICATION_JSON_VALUE)
45+
.accept(MediaType.APPLICATION_JSON)
46+
)
47+
.andExpect(status().isOk)
48+
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
49+
.andExpect(jsonPath("id").value(userDTO.id))
50+
}
51+
52+
53+
@Test
54+
fun `When retrieve user while user does not exists then user not found`() {
55+
Mockito.`when`(userService.retrieveUser(Any.ANY.toString())).thenReturn(null)
56+
mockMvc.perform(get("/users").param("user", "G").contentType(MediaType.APPLICATION_JSON_VALUE)
57+
.accept(MediaType.APPLICATION_JSON))
58+
.andExpect(status().isNotFound)
59+
}
60+
61+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.gokhana.demo
2+
3+
import com.gokhana.demo.model.UserDTO
4+
import org.assertj.core.api.Assertions.assertThat
5+
import org.junit.jupiter.api.Order
6+
import org.junit.jupiter.api.Test
7+
import org.junit.jupiter.api.TestInstance
8+
import org.springframework.beans.factory.annotation.Autowired
9+
import org.springframework.boot.test.context.SpringBootTest
10+
import org.springframework.boot.test.web.client.TestRestTemplate
11+
import org.springframework.boot.test.web.client.getForEntity
12+
import org.springframework.boot.test.web.client.postForEntity
13+
import org.springframework.http.HttpStatus
14+
15+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
16+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
17+
class UserIntegrationTests(@Autowired val restTemplate: TestRestTemplate) {
18+
19+
20+
@Test
21+
@Order(1)
22+
fun `When retrieve user by username while user does not exists then user not found`() {
23+
val entity = restTemplate.getForEntity<String>("/users?user=X")
24+
assertThat(entity.statusCode).isEqualTo(HttpStatus.NOT_FOUND)
25+
}
26+
27+
@Test
28+
@Order(2)
29+
fun `When create user while user is correct then create user successfully`() {
30+
val userDTO = UserDTO(1, "Gökhan", "G-khan")
31+
val entity = restTemplate.postForEntity<String>("/users", userDTO)
32+
assertThat(entity.statusCode).isEqualTo(HttpStatus.CREATED)
33+
}
34+
35+
36+
@Test
37+
@Order(2)
38+
fun `Create user while username is empty then bad request`() {
39+
val userDTO = UserDTO(1, "Gökhan", "")
40+
val entity = restTemplate.postForEntity<String>("/users", userDTO)
41+
assertThat(entity.statusCode).isEqualTo(HttpStatus.BAD_REQUEST)
42+
}
43+
44+
@Test
45+
@Order(3)
46+
fun `When retrieve user by username while user is exists then user not found`() {
47+
val entity = restTemplate.getForEntity<String>("/users?user=G-khan")
48+
assertThat(entity.statusCode).isEqualTo(HttpStatus.OK)
49+
}
50+
51+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.gokhana.demo
2+
3+
import com.gokhana.demo.entity.User
4+
import com.gokhana.demo.model.toUserDTO
5+
import com.gokhana.demo.repository.UserRepository
6+
import com.gokhana.demo.service.UserService
7+
import org.assertj.core.api.Assertions.assertThat
8+
import org.junit.jupiter.api.Test
9+
import org.junit.jupiter.api.extension.ExtendWith
10+
import org.mockito.Mockito
11+
import org.springframework.beans.factory.annotation.Autowired
12+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
13+
import org.springframework.boot.test.context.SpringBootTest
14+
import org.springframework.boot.test.mock.mockito.MockBean
15+
import org.springframework.test.context.junit.jupiter.SpringExtension
16+
17+
@ExtendWith(SpringExtension::class)
18+
@SpringBootTest
19+
@AutoConfigureMockMvc
20+
class UserServiceTest(@Autowired val userService: UserService) {
21+
22+
@MockBean
23+
lateinit var userRepository: UserRepository
24+
25+
@Test
26+
fun `save user to repository`() {
27+
val user = User(1, "Gökhan", "G-khan")
28+
val userDTO = user.toUserDTO()
29+
Mockito.`when`(userRepository.save(user)).thenReturn(user)
30+
val savedUserDTO = userService.createUser(userDTO)
31+
assertThat(savedUserDTO).isEqualTo(userDTO)
32+
}
33+
34+
@Test
35+
fun `retrieve saved user from repository`() {
36+
val user = User(1, "Gökhan", "G-khan")
37+
val userDTO = user.toUserDTO()
38+
Mockito.`when`(userRepository.findByUsername(user.username)).thenReturn(user)
39+
val savedUserDTO = userService.retrieveUser(user.username)
40+
assertThat(savedUserDTO).isEqualTo(userDTO)
41+
}
42+
43+
@Test
44+
fun `retrieve not exist user from repository`() {
45+
val user = User(1, "Gökhan", "G-khan")
46+
Mockito.`when`(userRepository.findByUsername(user.username)).thenReturn(null)
47+
val savedUserDTO = userService.retrieveUser(user.username)
48+
assertThat(savedUserDTO).isNull()
49+
}
50+
51+
}

0 commit comments

Comments
 (0)