1. Project Clover database Dom fev 10 2019 22:52:55 BRST
  2. Package rest

File RepositoriesTests.java

 

Code metrics

0
40
8
1
187
137
8
0,2
5
8
1

Classes

Class Line # Actions
RepositoriesTests 30 40 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 7 tests. .

Source view

1    package rest;
2   
3    import static org.hamcrest.Matchers.containsString;
4    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
5    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
6    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
7    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
8    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
9    import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
10    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
11    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
12    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
13   
14    import org.junit.Before;
15    import org.junit.Test;
16    import org.junit.runner.RunWith;
17    import org.springframework.beans.factory.annotation.Autowired;
18    import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
19    import org.springframework.boot.test.context.SpringBootTest;
20    import org.springframework.test.context.junit4.SpringRunner;
21    import org.springframework.test.web.servlet.MockMvc;
22    import org.springframework.test.web.servlet.MvcResult;
23   
24    import rest.db1.FirstAccountRepository;
25    import rest.db2.SecondAccountRepository;
26   
27    @RunWith(SpringRunner.class)
28    @SpringBootTest
29    @AutoConfigureMockMvc
 
30    public class RepositoriesTests {
31   
32    @Autowired
33    private MockMvc mockMvc;
34   
35    @Autowired
36    private FirstAccountRepository firstAccountRepository;
37   
38    @Autowired
39    private SecondAccountRepository secondAccountRepository;
40   
 
41  7 toggle @Before
42    public void deleteAllBeforeTests() throws Exception {
43  7 firstAccountRepository.deleteAll();
44  7 secondAccountRepository.deleteAll();
45    }
46   
 
47  1 toggle @Test
48    public void shouldReturnRepositoryIndex() throws Exception {
49   
50  1 mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
51    .andExpect(jsonPath("$._links.account1").exists())
52    .andExpect(jsonPath("$._links.account2").exists());
53   
54    }
55   
 
56  1 toggle @Test
57    public void shouldCreateEntity() throws Exception {
58   
59  1 mockMvc.perform(post("/account1").content("{\"name\": \"Rick\", \"total\":\"100.0\"}"))
60    .andExpect(status().isCreated())
61    .andExpect(header().string("Location", containsString("account1/")));
62   
63  1 mockMvc.perform(post("/account2").content("{\"name\": \"Rick\", \"total\":\"100.0\"}"))
64    .andExpect(status().isCreated())
65    .andExpect(header().string("Location", containsString("account2/")));
66    }
67   
 
68  1 toggle @Test
69    public void shouldRetrieveEntity() throws Exception {
70   
71  1 MvcResult mvcResult =
72    mockMvc.perform(post("/account1").content("{\"name\": \"Rick\", \"total\":\"100.0\"}"))
73    .andExpect(status().isCreated()).andReturn();
74   
75  1 String location = mvcResult.getResponse().getHeader("Location");
76  1 mockMvc.perform(get(location)).andExpect(status().isOk())
77    .andExpect(jsonPath("$.name").value("Rick")).andExpect(jsonPath("$.total").value("100.0"));
78   
79  1 mvcResult =
80    mockMvc.perform(post("/account2").content("{\"name\": \"Rick\", \"total\":\"100.0\"}"))
81    .andExpect(status().isCreated()).andReturn();
82   
83  1 location = mvcResult.getResponse().getHeader("Location");
84  1 mockMvc.perform(get(location)).andExpect(status().isOk())
85    .andExpect(jsonPath("$.name").value("Rick")).andExpect(jsonPath("$.total").value("100.0"));
86    }
87   
 
88  1 toggle @Test
89    public void shouldQueryEntity() throws Exception {
90   
91  1 mockMvc.perform(post("/account1").content("{ \"name\": \"Rick\", \"total\":\"100.0\"}"))
92    .andExpect(status().isCreated());
93   
94  1 mockMvc.perform(get("/account1/search/findByName?name={name}", "Rick"))
95    .andExpect(status().isOk())
96    .andExpect(jsonPath("$._embedded.account1[0].total").value("100.0"));
97   
98  1 mockMvc.perform(post("/account2").content("{ \"name\": \"Rick\", \"total\":\"100.0\"}"))
99    .andExpect(status().isCreated());
100   
101  1 mockMvc.perform(get("/account2/search/findByName?name={name}", "Rick"))
102    .andExpect(status().isOk())
103    .andExpect(jsonPath("$._embedded.account2[0].total").value("100.0"));
104   
105  1 mockMvc.perform(get("/account")).andExpect(status().isOk());
106    }
107   
 
108  1 toggle @Test
109    public void shouldUpdateEntity() throws Exception {
110   
111  1 MvcResult mvcResult =
112    mockMvc.perform(post("/account1").content("{\"name\": \"Rick\", \"total\":\"100.0\"}"))
113    .andExpect(status().isCreated()).andReturn();
114   
115  1 String location = mvcResult.getResponse().getHeader("Location");
116   
117  1 mockMvc.perform(put(location).content("{\"name\": \"Morty\", \"total\":\"500.0\"}"))
118    .andExpect(status().isNoContent());
119   
120  1 mockMvc.perform(get(location)).andExpect(status().isOk())
121    .andExpect(jsonPath("$.name").value("Morty")).andExpect(jsonPath("$.total").value("500.0"));
122   
123  1 mvcResult =
124    mockMvc.perform(post("/account2").content("{\"name\": \"Rick\", \"total\":\"100.0\"}"))
125    .andExpect(status().isCreated()).andReturn();
126   
127  1 location = mvcResult.getResponse().getHeader("Location");
128   
129  1 mockMvc.perform(put(location).content("{\"name\": \"Morty\", \"total\":\"500.0\"}"))
130    .andExpect(status().isNoContent());
131   
132  1 mockMvc.perform(get(location)).andExpect(status().isOk())
133    .andExpect(jsonPath("$.name").value("Morty")).andExpect(jsonPath("$.total").value("500.0"));
134    }
135   
 
136  1 toggle @Test
137    public void shouldPartiallyUpdateEntity() throws Exception {
138   
139  1 MvcResult mvcResult =
140    mockMvc.perform(post("/account1").content("{\"name\": \"Rick\", \"total\":\"100.0\"}"))
141    .andExpect(status().isCreated()).andReturn();
142   
143  1 String location = mvcResult.getResponse().getHeader("Location");
144   
145  1 mockMvc.perform(patch(location).content("{\"current\": \"10.0\"}"))
146    .andExpect(status().isNoContent());
147   
148  1 mockMvc.perform(get(location)).andExpect(status().isOk())
149    .andExpect(jsonPath("$.current").value("10.0"))
150    .andExpect(jsonPath("$.total").value("100.0"));
151   
152  1 mvcResult =
153    mockMvc.perform(post("/account2").content("{\"name\": \"Rick\", \"total\":\"100.0\"}"))
154    .andExpect(status().isCreated()).andReturn();
155   
156  1 location = mvcResult.getResponse().getHeader("Location");
157   
158  1 mockMvc.perform(patch(location).content("{\"current\": \"10.0\"}"))
159    .andExpect(status().isNoContent());
160   
161  1 mockMvc.perform(get(location)).andExpect(status().isOk())
162    .andExpect(jsonPath("$.current").value("10.0"))
163    .andExpect(jsonPath("$.total").value("100.0"));
164    }
165   
 
166  1 toggle @Test
167    public void shouldDeleteEntity() throws Exception {
168   
169  1 MvcResult mvcResult =
170    mockMvc.perform(post("/account1").content("{ \"name\": \"Morty\", \"total\":\"100.0\"}"))
171    .andExpect(status().isCreated()).andReturn();
172   
173  1 String location = mvcResult.getResponse().getHeader("Location");
174  1 mockMvc.perform(delete(location)).andExpect(status().isNoContent());
175   
176  1 mockMvc.perform(get(location)).andExpect(status().isNotFound());
177   
178  1 mvcResult =
179    mockMvc.perform(post("/account2").content("{ \"name\": \"Morty\", \"total\":\"100.0\"}"))
180    .andExpect(status().isCreated()).andReturn();
181   
182  1 location = mvcResult.getResponse().getHeader("Location");
183  1 mockMvc.perform(delete(location)).andExpect(status().isNoContent());
184   
185  1 mockMvc.perform(get(location)).andExpect(status().isNotFound());
186    }
187    }