12
12
import com .codedifferently .lesson16 .library .LibraryGuest ;
13
13
import com .codedifferently .lesson16 .library .Patron ;
14
14
import java .util .HashSet ;
15
+ import java .util .List ;
15
16
import java .util .Set ;
16
17
import java .util .UUID ;
17
18
import org .junit .jupiter .api .BeforeAll ;
@@ -30,54 +31,30 @@ class PatronControllerTest {
30
31
private static MockMvc mockMvc ;
31
32
@ Autowired private Library library ;
32
33
34
+ private Library lib = library ;
35
+
33
36
@ BeforeAll
34
37
static void setUp (WebApplicationContext wac ) {
35
38
mockMvc = MockMvcBuilders .webAppContextSetup (wac ).build ();
36
39
}
37
40
38
41
@ 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 ();
56
45
57
46
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 ());
67
49
}
68
50
69
51
@ 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 {
78
53
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 ));
81
58
}
82
59
83
60
@ Test
@@ -140,4 +117,37 @@ void testController_returnsNotFoundOnDeletePatron() throws Exception {
140
117
.contentType (MediaType .APPLICATION_JSON ))
141
118
.andExpect (status ().isNotFound ());
142
119
}
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
+ }
143
153
}
0 commit comments