88
99import com .fasterxml .jackson .core .type .TypeReference ;
1010import com .fasterxml .jackson .databind .ObjectMapper ;
11+ import org .assertj .core .api .WithAssertions ;
1112import org .gridsuite .useradmin .server .repository .ConnectionEntity ;
1213import org .gridsuite .useradmin .server .repository .ConnectionRepository ;
1314import org .gridsuite .useradmin .server .repository .UserAdminRepository ;
1819import org .springframework .beans .factory .annotation .Autowired ;
1920import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
2021import org .springframework .boot .test .context .SpringBootTest ;
22+ import org .springframework .dao .DataIntegrityViolationException ;
2123import org .springframework .test .context .ContextConfiguration ;
2224import org .springframework .test .context .junit4 .SpringRunner ;
2325import org .springframework .test .web .servlet .MockMvc ;
2426
2527import java .time .LocalDateTime ;
26- import java .time .ZoneOffset ;
2728import java .util .List ;
2829import java .util .UUID ;
2930
30- import static org .junit .Assert .*;
3131import static org .springframework .http .MediaType .APPLICATION_JSON ;
3232import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
3333import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
3939@ AutoConfigureMockMvc
4040@ SpringBootTest
4141@ ContextConfiguration (classes = {UserAdminApplication .class })
42- public class UserAdminTest {
42+ public class UserAdminTest implements WithAssertions {
4343
4444 @ Autowired
4545 private MockMvc mockMvc ;
@@ -78,10 +78,9 @@ public void testUserAdmin() throws Exception {
7878 .contentType (APPLICATION_JSON ))
7979 .andExpect (status ().isOk ())
8080 .andReturn ().getResponse ().getContentAsString (),
81- new TypeReference <>() {
82- });
81+ new TypeReference <>() { });
8382
84- assertEquals ( 0 , userEntities . size () );
83+ assertThat ( userEntities ). isEmpty ( );
8584
8685 mockMvc .perform (head ("/" + UserAdminApi .API_VERSION + "/users/{sub}" , ADMIN_USER ))
8786 .andExpect (status ().isOk ())
@@ -99,10 +98,9 @@ public void testUserAdmin() throws Exception {
9998 .contentType (APPLICATION_JSON ))
10099 .andExpect (status ().isOk ())
101100 .andReturn ().getResponse ().getContentAsString (),
102- new TypeReference <>() {
103- });
101+ new TypeReference <>() { });
104102
105- assertEquals ( 1 , userEntities . size () );
103+ assertThat ( userEntities ). hasSize ( 1 );
106104
107105 UUID userId = userEntities .get (0 ).getId ();
108106
@@ -113,17 +111,20 @@ public void testUserAdmin() throws Exception {
113111 mockMvc .perform (head ("/" + UserAdminApi .API_VERSION + "/users/{sub}" , "UNKNOWN" ))
114112 .andExpect (status ().isNoContent ())
115113 .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 ();
119119 LocalDateTime firstConnectionDate = connectionRepository .findBySub (USER_SUB ).get ().getFirstConnectionDate ();
120120 //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 );
122123
123124 mockMvc .perform (head ("/" + UserAdminApi .API_VERSION + "/users/{sub}" , USER_SUB ))
124125 .andExpect (status ().isOk ())
125126 .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 );
127128
128129 mockMvc .perform (delete ("/" + UserAdminApi .API_VERSION + "/users/{id}" , userId )
129130 .header ("userId" , ADMIN_USER )
@@ -137,9 +138,8 @@ public void testUserAdmin() throws Exception {
137138 .contentType (APPLICATION_JSON ))
138139 .andExpect (status ().isOk ())
139140 .andReturn ().getResponse ().getContentAsString (),
140- new TypeReference <>() {
141- });
142- assertEquals (0 , userEntities .size ());
141+ new TypeReference <>() { });
142+ assertThat (userEntities ).isEmpty ();
143143
144144 mockMvc .perform (delete ("/" + UserAdminApi .API_VERSION + "/users/{id}" , userId )
145145 .header ("userId" , NOT_ADMIN )
@@ -180,10 +180,9 @@ public void testGetConnections() throws Exception {
180180 .contentType (APPLICATION_JSON ))
181181 .andExpect (status ().isOk ())
182182 .andReturn ().getResponse ().getContentAsString (),
183- new TypeReference <>() {
184- });
183+ new TypeReference <>() { });
185184
186- assertEquals ( 2 , userEntities . size () );
185+ assertThat ( userEntities ). hasSize ( 2 );
187186
188187 mockMvc .perform (head ("/" + UserAdminApi .API_VERSION + "/users/{sub}" , USER_SUB ))
189188 .andExpect (status ().isOk ())
@@ -199,24 +198,30 @@ public void testGetConnections() throws Exception {
199198 .contentType (APPLICATION_JSON ))
200199 .andExpect (status ().isOk ())
201200 .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 ));
210216
211217 connectionEntities = objectMapper .readValue (
212218 mockMvc .perform (get ("/" + UserAdminApi .API_VERSION + "/connections" )
213219 .header ("userId" , ADMIN_USER )
214220 .contentType (APPLICATION_JSON ))
215221 .andExpect (status ().isOk ())
216222 .andReturn ().getResponse ().getContentAsString (),
217- new TypeReference <>() {
218- });
219- assertEquals (2 , connectionEntities .size ());
223+ new TypeReference <>() { });
224+ assertThat (connectionEntities ).hasSize (2 );
220225
221226 mockMvc .perform (get ("/" + UserAdminApi .API_VERSION + "/connections" )
222227 .header ("userId" , NOT_ADMIN )
0 commit comments