Skip to content

Commit 6395dbc

Browse files
author
Mohamed
committed
Fix: Modified test to fit needs of the code
1 parent 5006fd3 commit 6395dbc

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

lesson_16/api/api_app/src/test/java/com/codedifferently/lesson16/web/PatronControllerTest.java

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.codedifferently.lesson16.library.LibraryGuest;
1313
import com.codedifferently.lesson16.library.Patron;
1414
import java.util.HashSet;
15+
import java.util.List;
1516
import java.util.Set;
1617
import java.util.UUID;
1718
import org.junit.jupiter.api.BeforeAll;
@@ -30,54 +31,30 @@ class PatronControllerTest {
3031
private static MockMvc mockMvc;
3132
@Autowired private Library library;
3233

34+
private Library lib = library;
35+
3336
@BeforeAll
3437
static void setUp(WebApplicationContext wac) {
3538
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
3639
}
3740

3841
@Test
39-
void testController_getsAllPatrons() throws Exception {
40-
mockMvc
41-
.perform(get("/patrons").contentType(MediaType.APPLICATION_JSON))
42-
.andExpect(status().isOk())
43-
.andExpect(jsonPath("$.patrons").isArray())
44-
.andExpect(jsonPath("$.patrons.length()").value(5));
45-
}
46-
47-
@Test
48-
void testController_deletesPatron() throws Exception {
49-
Set<LibraryGuest> pat = library.getPatrons();
50-
UUID ids = UUID.fromString("00000000-0000-0000-0000-000000000000");
51-
for (LibraryGuest guest : pat) {
52-
if (guest.getName() == "Alice Johnson") {
53-
ids = guest.getId();
54-
}
55-
}
42+
void testController_getsAnPatron() throws Exception {
43+
List<LibraryGuest> pat = library.getPatrons().stream().toList();
44+
UUID ids = pat.get(3).getId();
5645

5746
mockMvc
58-
.perform(delete("/patrons/" + ids.toString()).contentType(MediaType.APPLICATION_JSON))
59-
.andExpect(status().isNoContent());
60-
int i = 0;
61-
for (LibraryGuest guest : pat) {
62-
if (guest.getName() == "Alice Johnson") {
63-
i++;
64-
}
65-
}
66-
assertThat(i).isEqualTo(0);
47+
.perform(get("/patrons/" + ids.toString()).contentType(MediaType.APPLICATION_JSON))
48+
.andExpect(status().isOk());
6749
}
6850

6951
@Test
70-
void testController_getsAnPatron() throws Exception {
71-
Set<LibraryGuest> pat = library.getPatrons();
72-
UUID ids = UUID.fromString("00000000-0000-0000-0000-000000000000");
73-
for (LibraryGuest guest : pat) {
74-
if (guest.getName() == "Bob Williams") {
75-
ids = guest.getId();
76-
}
77-
}
52+
void testController_getsAllPatrons() throws Exception {
7853
mockMvc
79-
.perform(get("/patrons/" + ids.toString()).contentType(MediaType.APPLICATION_JSON))
80-
.andExpect(status().isOk());
54+
.perform(get("/patrons").contentType(MediaType.APPLICATION_JSON))
55+
.andExpect(status().isOk())
56+
.andExpect(jsonPath("$.patrons").isArray())
57+
.andExpect(jsonPath("$.patrons.length()").value(5));
8158
}
8259

8360
@Test
@@ -140,4 +117,37 @@ void testController_returnsNotFoundOnDeletePatron() throws Exception {
140117
.contentType(MediaType.APPLICATION_JSON))
141118
.andExpect(status().isNotFound());
142119
}
120+
121+
@Test
122+
void testController_deletesPatron() throws Exception {
123+
Library lib = library;
124+
List<LibraryGuest> pat = library.getPatrons().stream().toList();
125+
UUID ids = getGuestId(pat);
126+
127+
mockMvc
128+
.perform(delete("/patrons/" + ids.toString()).contentType(MediaType.APPLICATION_JSON))
129+
.andExpect(status().isNoContent());
130+
int i = 0;
131+
pat = library.getPatrons().stream().toList();
132+
for (LibraryGuest guest : pat) {
133+
if (guest.getId() == ids) {
134+
i++;
135+
}
136+
}
137+
library = lib;
138+
assertThat(i).isEqualTo(0);
139+
}
140+
141+
UUID getGuestId(List<LibraryGuest> list) {
142+
if (list.get(0).getCheckedOutMediaItems().size() == 0) {
143+
return list.get(0).getId();
144+
} else if (list.get(1).getCheckedOutMediaItems().size() == 0) {
145+
return list.get(1).getId();
146+
} else if (list.get(2).getCheckedOutMediaItems().size() == 0) {
147+
return list.get(2).getId();
148+
} else if (list.get(3).getCheckedOutMediaItems().size() == 0) {
149+
return list.get(3).getId();
150+
}
151+
return list.get(4).getId();
152+
}
143153
}

0 commit comments

Comments
 (0)