-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathListOfItemsControllerTest.kt
99 lines (84 loc) · 3.77 KB
/
ListOfItemsControllerTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package io.pivotal
import org.hamcrest.Matchers.*
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.security.test.context.support.WithMockUser
import org.springframework.test.context.junit4.SpringRunner
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import org.springframework.test.web.servlet.result.MockMvcResultHandlers.print
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.web.context.WebApplicationContext
@RunWith(SpringRunner::class)
@SpringBootTest
class ListOfItemsControllerTest {
@Autowired lateinit var itemRepository: ItemRepository
@Autowired lateinit var context: WebApplicationContext
lateinit var mvc: MockMvc
@Before
fun setUp() {
mvc = MockMvcBuilders.webAppContextSetup(this.context).build()
}
@Test
fun whenDefaultPlaceholdersLoadedTwoTasksShouldShow() {
mvc.perform(get("/resource/dummylist/"))
.andExpect(status().isOk)
.andDo(print())
.andExpect(jsonPath("$", hasSize<Any>(2)))
.andExpect(jsonPath("$[0].id", equalTo("1")))
.andExpect(jsonPath("$[1].id", equalTo("2")))
.andExpect(jsonPath("$[0].title", equalTo("Go for a swim")))
.andExpect(jsonPath("$[1].title", equalTo("Visit farmer's market")))
.andExpect(jsonPath("$[0].content", equalTo("Go swimming on Monday night")))
.andExpect(jsonPath("$[1].content", equalTo("Buy dairy and eggs at farmers market")))
}
@Test
fun whenItemIsCheckedAsDoneModelIsUpdated() {
val item = Item("Fake Todo", "Do Lots of stuff")
itemRepository.save(item)
mvc.perform(post(String.format("/resource/done/%d/yes/", item.id)))
.andDo(print())
.andExpect(status().isOk)
val newItem = itemRepository.findOne(item.id)
assertEquals(item.done, "no")
assertEquals(newItem.done, "yes")
}
@Test
fun whenEditIsMadeTheModelIsUpdated() {
val newTitle = "New Test Title"
val item = Item("Test Todo", "Do Lots of stuff")
itemRepository.save(item)
mvc.perform(post(String.format("/resource/save/%d/%s/%s/no/", item.id, newTitle, item.content)))
.andExpect(status().isOk)
assertNotEquals(item.title, newTitle)
val newItem = itemRepository.findOne(item.id)
assertEquals(newItem.title, newTitle)
assertEquals(newItem.content, item.content)
}
@Test
@WithMockUser("Navya")
fun whenCreateIsHitANewItemIsCreatedAndReturned() {
mvc.perform(post("/resource/create/"))
.andExpect(status().isOk)
.andDo(print())
.andExpect(jsonPath("$.title", isEmptyString()))
.andExpect(jsonPath("$.content", isEmptyString()))
.andExpect(jsonPath("$.done", equalTo("no")))
}
@Test
fun whenDeleteIsHitItemIsTrashed() {
val item = Item("Test Todo", "Do Lots of stuff - then delete")
itemRepository.save(item)
mvc.perform(post(String.format("/resource/delete/%d/", item.id)))
.andExpect(status().isOk)
val newItem = itemRepository.findOne(item.id) // returns Null if not found
assertNull(newItem)
}
}