8
8
9
9
import com .fasterxml .jackson .core .type .TypeReference ;
10
10
import com .fasterxml .jackson .databind .ObjectMapper ;
11
+ import org .assertj .core .api .WithAssertions ;
11
12
import org .gridsuite .useradmin .server .repository .ConnectionEntity ;
12
13
import org .gridsuite .useradmin .server .repository .ConnectionRepository ;
13
14
import org .gridsuite .useradmin .server .repository .UserAdminRepository ;
18
19
import org .springframework .beans .factory .annotation .Autowired ;
19
20
import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
20
21
import org .springframework .boot .test .context .SpringBootTest ;
22
+ import org .springframework .dao .DataIntegrityViolationException ;
21
23
import org .springframework .test .context .ContextConfiguration ;
22
24
import org .springframework .test .context .junit4 .SpringRunner ;
23
25
import org .springframework .test .web .servlet .MockMvc ;
24
26
25
27
import java .time .LocalDateTime ;
26
- import java .time .ZoneOffset ;
27
28
import java .util .List ;
28
29
import java .util .UUID ;
29
30
30
- import static org .junit .Assert .*;
31
31
import static org .springframework .http .MediaType .APPLICATION_JSON ;
32
32
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
33
33
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
39
39
@ AutoConfigureMockMvc
40
40
@ SpringBootTest
41
41
@ ContextConfiguration (classes = {UserAdminApplication .class })
42
- public class UserAdminTest {
42
+ public class UserAdminTest implements WithAssertions {
43
43
44
44
@ Autowired
45
45
private MockMvc mockMvc ;
@@ -78,10 +78,9 @@ public void testUserAdmin() throws Exception {
78
78
.contentType (APPLICATION_JSON ))
79
79
.andExpect (status ().isOk ())
80
80
.andReturn ().getResponse ().getContentAsString (),
81
- new TypeReference <>() {
82
- });
81
+ new TypeReference <>() { });
83
82
84
- assertEquals ( 0 , userEntities . size () );
83
+ assertThat ( userEntities ). isEmpty ( );
85
84
86
85
mockMvc .perform (head ("/" + UserAdminApi .API_VERSION + "/users/{sub}" , ADMIN_USER ))
87
86
.andExpect (status ().isOk ())
@@ -99,10 +98,9 @@ public void testUserAdmin() throws Exception {
99
98
.contentType (APPLICATION_JSON ))
100
99
.andExpect (status ().isOk ())
101
100
.andReturn ().getResponse ().getContentAsString (),
102
- new TypeReference <>() {
103
- });
101
+ new TypeReference <>() { });
104
102
105
- assertEquals ( 1 , userEntities . size () );
103
+ assertThat ( userEntities ). hasSize ( 1 );
106
104
107
105
UUID userId = userEntities .get (0 ).getId ();
108
106
@@ -113,17 +111,20 @@ public void testUserAdmin() throws Exception {
113
111
mockMvc .perform (head ("/" + UserAdminApi .API_VERSION + "/users/{sub}" , "UNKNOWN" ))
114
112
.andExpect (status ().isNoContent ())
115
113
.andReturn ();
116
- assertEquals (3 , connectionRepository .findAll ().size ());
117
- assertTrue (connectionRepository .findBySub (USER_SUB ).get ().getConnectionAccepted ());
118
- assertFalse (connectionRepository .findBySub ("UNKNOWN" ).get ().getConnectionAccepted ());
114
+ assertThat (connectionRepository .findAll ()).hasSize (3 );
115
+ assertThat (connectionRepository .findBySub (USER_SUB )).get ()
116
+ .extracting (ConnectionEntity ::getConnectionAccepted , BOOLEAN ).isTrue ();
117
+ assertThat (connectionRepository .findBySub ("UNKNOWN" )).get ()
118
+ .extracting (ConnectionEntity ::getConnectionAccepted , BOOLEAN ).isFalse ();
119
119
LocalDateTime firstConnectionDate = connectionRepository .findBySub (USER_SUB ).get ().getFirstConnectionDate ();
120
120
//firstConnectionDate and lastConnectionDate are equals cause this is the first connection for this user
121
- assertTrue (firstConnectionDate .toEpochSecond (ZoneOffset .UTC ) < connectionRepository .findBySub (USER_SUB ).get ().getLastConnectionDate ().toEpochSecond (ZoneOffset .UTC ) + 2 );
121
+ assertThat (connectionRepository .findBySub (USER_SUB )).get ()
122
+ .extracting (ConnectionEntity ::getLastConnectionDate , LOCAL_DATE_TIME ).isAfterOrEqualTo (firstConnectionDate );
122
123
123
124
mockMvc .perform (head ("/" + UserAdminApi .API_VERSION + "/users/{sub}" , USER_SUB ))
124
125
.andExpect (status ().isOk ())
125
126
.andReturn ();
126
- assertEquals ( firstConnectionDate , connectionRepository .findBySub (USER_SUB ).get ().getFirstConnectionDate () );
127
+ assertThat ( connectionRepository .findBySub (USER_SUB )) .get ().extracting ( ConnectionEntity :: getFirstConnectionDate , LOCAL_DATE_TIME ). isEqualTo ( firstConnectionDate );
127
128
128
129
mockMvc .perform (delete ("/" + UserAdminApi .API_VERSION + "/users/{id}" , userId )
129
130
.header ("userId" , ADMIN_USER )
@@ -137,9 +138,8 @@ public void testUserAdmin() throws Exception {
137
138
.contentType (APPLICATION_JSON ))
138
139
.andExpect (status ().isOk ())
139
140
.andReturn ().getResponse ().getContentAsString (),
140
- new TypeReference <>() {
141
- });
142
- assertEquals (0 , userEntities .size ());
141
+ new TypeReference <>() { });
142
+ assertThat (userEntities ).isEmpty ();
143
143
144
144
mockMvc .perform (delete ("/" + UserAdminApi .API_VERSION + "/users/{id}" , userId )
145
145
.header ("userId" , NOT_ADMIN )
@@ -180,10 +180,9 @@ public void testGetConnections() throws Exception {
180
180
.contentType (APPLICATION_JSON ))
181
181
.andExpect (status ().isOk ())
182
182
.andReturn ().getResponse ().getContentAsString (),
183
- new TypeReference <>() {
184
- });
183
+ new TypeReference <>() { });
185
184
186
- assertEquals ( 2 , userEntities . size () );
185
+ assertThat ( userEntities ). hasSize ( 2 );
187
186
188
187
mockMvc .perform (head ("/" + UserAdminApi .API_VERSION + "/users/{sub}" , USER_SUB ))
189
188
.andExpect (status ().isOk ())
@@ -199,24 +198,30 @@ public void testGetConnections() throws Exception {
199
198
.contentType (APPLICATION_JSON ))
200
199
.andExpect (status ().isOk ())
201
200
.andReturn ().getResponse ().getContentAsString (),
202
- new TypeReference <>() {
203
- });
204
-
205
- assertEquals (2 , connectionEntities .size ());
206
-
207
- connectionRepository .save (new ConnectionEntity (USER_SUB , LocalDateTime .now (), LocalDateTime .now (), true ));
208
- connectionRepository .save (new ConnectionEntity (USER_SUB , LocalDateTime .now ().minusSeconds (5 ), LocalDateTime .now (), true ));
209
- connectionRepository .save (new ConnectionEntity (USER_SUB2 , LocalDateTime .now (), LocalDateTime .now (), false ));
201
+ new TypeReference <>() { });
202
+
203
+ assertThat (connectionEntities ).hasSize (2 );
204
+
205
+ assertThatThrownBy (() -> connectionRepository .save (new ConnectionEntity (USER_SUB , LocalDateTime .now (), LocalDateTime .now (), true )))
206
+ .isInstanceOf (DataIntegrityViolationException .class )
207
+ .hasMessageContaining ("Unique index or primary key violation" );
208
+ connectionRepository .save (connectionRepository .getBySub (USER_SUB )
209
+ .setFirstConnectionDate (LocalDateTime .now ().minusSeconds (5 ))
210
+ .setLastConnectionDate (LocalDateTime .now ())
211
+ .setConnectionAccepted (true ));
212
+ connectionRepository .save (connectionRepository .getBySub (USER_SUB2 )
213
+ .setFirstConnectionDate (LocalDateTime .now ())
214
+ .setLastConnectionDate (LocalDateTime .now ())
215
+ .setConnectionAccepted (false ));
210
216
211
217
connectionEntities = objectMapper .readValue (
212
218
mockMvc .perform (get ("/" + UserAdminApi .API_VERSION + "/connections" )
213
219
.header ("userId" , ADMIN_USER )
214
220
.contentType (APPLICATION_JSON ))
215
221
.andExpect (status ().isOk ())
216
222
.andReturn ().getResponse ().getContentAsString (),
217
- new TypeReference <>() {
218
- });
219
- assertEquals (2 , connectionEntities .size ());
223
+ new TypeReference <>() { });
224
+ assertThat (connectionEntities ).hasSize (2 );
220
225
221
226
mockMvc .perform (get ("/" + UserAdminApi .API_VERSION + "/connections" )
222
227
.header ("userId" , NOT_ADMIN )
0 commit comments