Skip to content

Commit

Permalink
Replace expected @test attributes with AssertJ
Browse files Browse the repository at this point in the history
Replace JUnit expected @test attributes with AssertJ calls.
  • Loading branch information
philwebb authored and jzheaux committed Sep 22, 2020
1 parent 20baa7d commit c502312
Show file tree
Hide file tree
Showing 243 changed files with 2,125 additions and 1,601 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,16 @@ public void insertAceAddsElementAtCorrectIndex() {
assertThat(acl.getEntries().get(2).getSid()).isEqualTo(new GrantedAuthoritySid("ROLE_TEST2"));
}

@Test(expected = NotFoundException.class)
@Test
public void insertAceFailsForNonExistentElement() {
MutableAcl acl = new AclImpl(this.objectIdentity, 1, this.authzStrategy, this.pgs, null, null, true,
new PrincipalSid("joe"));
MockAclService service = new MockAclService();
// Insert one permission
acl.insertAce(0, BasePermission.READ, new GrantedAuthoritySid("ROLE_TEST1"), true);
service.updateAcl(acl);
acl.insertAce(55, BasePermission.READ, new GrantedAuthoritySid("ROLE_TEST2"), true);
assertThatExceptionOfType(NotFoundException.class)
.isThrownBy(() -> acl.insertAce(55, BasePermission.READ, new GrantedAuthoritySid("ROLE_TEST2"), true));
}

@Test
Expand Down Expand Up @@ -411,38 +412,40 @@ public void isSidLoadedBehavesAsExpected() {
.isFalse();
}

@Test(expected = NotFoundException.class)
@Test
public void insertAceRaisesNotFoundExceptionForIndexLessThanZero() {
AclImpl acl = new AclImpl(this.objectIdentity, 1, this.authzStrategy, this.pgs, null, null, true,
new PrincipalSid("joe"));
acl.insertAce(-1, mock(Permission.class), mock(Sid.class), true);
assertThatExceptionOfType(NotFoundException.class)
.isThrownBy(() -> acl.insertAce(-1, mock(Permission.class), mock(Sid.class), true));
}

@Test(expected = NotFoundException.class)
@Test
public void deleteAceRaisesNotFoundExceptionForIndexLessThanZero() {
AclImpl acl = new AclImpl(this.objectIdentity, 1, this.authzStrategy, this.pgs, null, null, true,
new PrincipalSid("joe"));
acl.deleteAce(-1);
assertThatExceptionOfType(NotFoundException.class).isThrownBy(() -> acl.deleteAce(-1));
}

@Test(expected = NotFoundException.class)
@Test
public void insertAceRaisesNotFoundExceptionForIndexGreaterThanSize() {
AclImpl acl = new AclImpl(this.objectIdentity, 1, this.authzStrategy, this.pgs, null, null, true,
new PrincipalSid("joe"));
// Insert at zero, OK.
acl.insertAce(0, mock(Permission.class), mock(Sid.class), true);
// Size is now 1
acl.insertAce(2, mock(Permission.class), mock(Sid.class), true);
assertThatExceptionOfType(NotFoundException.class)
.isThrownBy(() -> acl.insertAce(2, mock(Permission.class), mock(Sid.class), true));
}

// SEC-1151
@Test(expected = NotFoundException.class)
@Test
public void deleteAceRaisesNotFoundExceptionForIndexEqualToSize() {
AclImpl acl = new AclImpl(this.objectIdentity, 1, this.authzStrategy, this.pgs, null, null, true,
new PrincipalSid("joe"));
acl.insertAce(0, mock(Permission.class), mock(Sid.class), true);
// Size is now 1
acl.deleteAce(1);
assertThatExceptionOfType(NotFoundException.class).isThrownBy(() -> acl.deleteAce(1));
}

// SEC-1795
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public void testGetIdMethodConstraints() {
assertThatNoException().isThrownBy(() -> new ObjectIdentityImpl(mockId));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void constructorRejectsInvalidTypeParameter() {
new ObjectIdentityImpl("", 1L);
assertThatIllegalArgumentException().isThrownBy(() -> new ObjectIdentityImpl("", 1L));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

/**
* Tests {@link BasicLookupStrategy}
Expand Down Expand Up @@ -294,12 +295,13 @@ public void testReadAllObjectIdentitiesWhenLastElementIsAlreadyCached() {
assertThat(foundParent2Acl.isGranted(checkPermission, sids, false)).isTrue();
}

@Test(expected = IllegalArgumentException.class)
@Test
public void nullOwnerIsNotSupported() {
String query = "INSERT INTO acl_object_identity(ID,OBJECT_ID_CLASS,OBJECT_ID_IDENTITY,PARENT_OBJECT,OWNER_SID,ENTRIES_INHERITING) VALUES (6,2,104,null,null,1);";
getJdbcTemplate().execute(query);
ObjectIdentity oid = new ObjectIdentityImpl(TARGET_CLASS, 104L);
this.strategy.readAclsById(Arrays.asList(oid), Arrays.asList(BEN_SID));
assertThatIllegalArgumentException()
.isThrownBy(() -> this.strategy.readAclsById(Arrays.asList(oid), Arrays.asList(BEN_SID)));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.core.convert.ConversionService;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.BDDMockito.given;

/**
Expand Down Expand Up @@ -125,14 +126,14 @@ public void shouldReturnStringWhenStringClassIdType() throws SQLException {
assertThat(newIdentifier).isEqualTo(identifier);
}

@Test(expected = IllegalArgumentException.class)
@Test
public void shouldNotAcceptNullConversionServiceInConstruction() {
new AclClassIdUtils(null);
assertThatIllegalArgumentException().isThrownBy(() -> new AclClassIdUtils(null));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void shouldNotAcceptNullConversionServiceInSetter() {
this.aclClassIdUtils.setConversionService(null);
assertThatIllegalArgumentException().isThrownBy(() -> this.aclClassIdUtils.setConversionService(null));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.springframework.security.acls.model.Acl;
import org.springframework.security.acls.model.ObjectIdentity;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

/**
* Tests {@link BasicLookupStrategy} with Acl Class type id set to UUID.
*
Expand Down Expand Up @@ -110,10 +112,11 @@ public void testReadObjectIdentityUsingLongTypeWithConversionServiceEnabled() {
Assert.assertNotNull(foundAcls.get(oid));
}

@Test(expected = ConversionFailedException.class)
@Test
public void testReadObjectIdentityUsingNonUuidInDatabase() {
ObjectIdentity oid = new ObjectIdentityImpl(TARGET_CLASS_WITH_UUID, OBJECT_IDENTITY_LONG_AS_UUID);
this.uuidEnabledStrategy.readAclsById(Arrays.asList(oid), Arrays.asList(BEN_SID));
assertThatExceptionOfType(ConversionFailedException.class)
.isThrownBy(() -> this.uuidEnabledStrategy.readAclsById(Arrays.asList(oid), Arrays.asList(BEN_SID)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ public void cleanup() {
SecurityContextHolder.clearContext();
}

@Test(expected = IllegalArgumentException.class)
@Test
public void constructorRejectsNullParameters() {
new EhCacheBasedAclCache(null, new DefaultPermissionGrantingStrategy(new ConsoleAuditLogger()),
new AclAuthorizationStrategyImpl(new SimpleGrantedAuthority("ROLE_USER")));
assertThatIllegalArgumentException().isThrownBy(
() -> new EhCacheBasedAclCache(null, new DefaultPermissionGrantingStrategy(new ConsoleAuditLogger()),
new AclAuthorizationStrategyImpl(new SimpleGrantedAuthority("ROLE_USER"))));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.springframework.security.acls.model.Sid;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
Expand Down Expand Up @@ -95,13 +96,14 @@ public void tearDownEmbeddedDatabase() {
}

// SEC-1898
@Test(expected = NotFoundException.class)
@Test
public void readAclByIdMissingAcl() {
Map<ObjectIdentity, Acl> result = new HashMap<>();
given(this.lookupStrategy.readAclsById(anyList(), anyList())).willReturn(result);
ObjectIdentity objectIdentity = new ObjectIdentityImpl(Object.class, 1);
List<Sid> sids = Arrays.<Sid>asList(new PrincipalSid("user"));
this.aclService.readAclById(objectIdentity, sids);
assertThatExceptionOfType(NotFoundException.class)
.isThrownBy(() -> this.aclService.readAclById(objectIdentity, sids));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.springframework.security.util.FieldUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

/**
* Tests {@link org.springframework.security.acls.domain.SpringCacheBasedAclCache}
Expand Down Expand Up @@ -74,9 +75,9 @@ private Cache getCache() {
return cache;
}

@Test(expected = IllegalArgumentException.class)
@Test
public void constructorRejectsNullParameters() {
new SpringCacheBasedAclCache(null, null, null);
assertThatIllegalArgumentException().isThrownBy(() -> new SpringCacheBasedAclCache(null, null, null));
}

@SuppressWarnings("rawtypes")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ public void securedInterfaceMethodAllowsAllAccess() {
this.secured.securedMethod();
}

@Test(expected = AuthenticationCredentialsNotFoundException.class)
@Test
public void securedClassMethodDeniesUnauthenticatedAccess() {
this.secured.securedClassMethod();
assertThatExceptionOfType(AuthenticationCredentialsNotFoundException.class)
.isThrownBy(() -> this.secured.securedClassMethod());
}

@Test
Expand All @@ -109,17 +110,17 @@ public void securedClassMethodAllowsAccessToRoleA() {
this.secured.securedClassMethod();
}

@Test(expected = AccessDeniedException.class)
@Test
public void internalPrivateCallIsIntercepted() {
SecurityContextHolder.getContext().setAuthentication(this.anne);
assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(() -> this.secured.publicCallsPrivate());
this.securedSub.publicCallsPrivate();
assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(() -> this.securedSub.publicCallsPrivate());
}

@Test(expected = AccessDeniedException.class)
@Test
public void protectedMethodIsIntercepted() {
SecurityContextHolder.getContext().setAuthentication(this.anne);
this.secured.protectedMethod();
assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(() -> this.secured.protectedMethod());
}

@Test
Expand All @@ -129,11 +130,11 @@ public void overriddenProtectedMethodIsNotIntercepted() {
}

// SEC-1262
@Test(expected = AccessDeniedException.class)
@Test
public void denyAllPreAuthorizeDeniesAccess() {
configureForElAnnotations();
SecurityContextHolder.getContext().setAuthentication(this.anne);
this.prePostSecured.denyAllMethod();
assertThatExceptionOfType(AccessDeniedException.class).isThrownBy(this.prePostSecured::denyAllMethod);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import org.springframework.security.web.authentication.WebAuthenticationDetails;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -199,7 +201,7 @@ public void authenticateAllAuthenticationIsSuccessful() throws Exception {
assertThatIllegalStateException().isThrownBy(() -> cap.authenticate(token));
}

@Test(expected = BadCredentialsException.class)
@Test
public void missingTicketIdIsDetected() throws Exception {
CasAuthenticationProvider cap = new CasAuthenticationProvider();
cap.setAuthenticationUserDetailsService(new MockAuthoritiesPopulator());
Expand All @@ -211,10 +213,10 @@ public void missingTicketIdIsDetected() throws Exception {
cap.afterPropertiesSet();
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
CasAuthenticationFilter.CAS_STATEFUL_IDENTIFIER, "");
cap.authenticate(token);
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> cap.authenticate(token));
}

@Test(expected = BadCredentialsException.class)
@Test
public void invalidKeyIsDetected() throws Exception {
final Assertion assertion = new AssertionImpl("test");
CasAuthenticationProvider cap = new CasAuthenticationProvider();
Expand All @@ -227,30 +229,30 @@ public void invalidKeyIsDetected() throws Exception {
cap.afterPropertiesSet();
CasAuthenticationToken token = new CasAuthenticationToken("WRONG_KEY", makeUserDetails(), "credentials",
AuthorityUtils.createAuthorityList("XX"), makeUserDetails(), assertion);
cap.authenticate(token);
assertThatExceptionOfType(BadCredentialsException.class).isThrownBy(() -> cap.authenticate(token));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void detectsMissingAuthoritiesPopulator() throws Exception {
CasAuthenticationProvider cap = new CasAuthenticationProvider();
cap.setKey("qwerty");
cap.setStatelessTicketCache(new MockStatelessTicketCache());
cap.setTicketValidator(new MockTicketValidator(true));
cap.setServiceProperties(makeServiceProperties());
cap.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(() -> cap.afterPropertiesSet());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void detectsMissingKey() throws Exception {
CasAuthenticationProvider cap = new CasAuthenticationProvider();
cap.setAuthenticationUserDetailsService(new MockAuthoritiesPopulator());
cap.setStatelessTicketCache(new MockStatelessTicketCache());
cap.setTicketValidator(new MockTicketValidator(true));
cap.setServiceProperties(makeServiceProperties());
cap.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(() -> cap.afterPropertiesSet());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void detectsMissingStatelessTicketCache() throws Exception {
CasAuthenticationProvider cap = new CasAuthenticationProvider();
// set this explicitly to null to test failure
Expand All @@ -259,17 +261,17 @@ public void detectsMissingStatelessTicketCache() throws Exception {
cap.setKey("qwerty");
cap.setTicketValidator(new MockTicketValidator(true));
cap.setServiceProperties(makeServiceProperties());
cap.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(() -> cap.afterPropertiesSet());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void detectsMissingTicketValidator() throws Exception {
CasAuthenticationProvider cap = new CasAuthenticationProvider();
cap.setAuthenticationUserDetailsService(new MockAuthoritiesPopulator());
cap.setKey("qwerty");
cap.setStatelessTicketCache(new MockStatelessTicketCache());
cap.setServiceProperties(makeServiceProperties());
cap.afterPropertiesSet();
assertThatIllegalArgumentException().isThrownBy(() -> cap.afterPropertiesSet());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ public void testConstructorRejectsNulls() {
"Password", AuthorityUtils.createAuthorityList("ROLE_1", null), makeUserDetails(), assertion));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void constructorWhenEmptyKeyThenThrowsException() {
new CasAuthenticationToken("", "user", "password", Collections.<GrantedAuthority>emptyList(),
new User("user", "password", Collections.<GrantedAuthority>emptyList()), null);
assertThatIllegalArgumentException().isThrownBy(
() -> new CasAuthenticationToken("", "user", "password", Collections.<GrantedAuthority>emptyList(),
new User("user", "password", Collections.<GrantedAuthority>emptyList()), null));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

/**
* Tests
Expand Down Expand Up @@ -56,9 +57,9 @@ public void testCacheOperation() throws Exception {
assertThat(cache.getByTicketId("UNKNOWN_SERVICE_TICKET")).isNull();
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testStartupDetectsMissingCache() throws Exception {
new SpringCacheBasedTicketCache(null);
assertThatIllegalArgumentException().isThrownBy(() -> new SpringCacheBasedTicketCache(null));
}

}
Loading

0 comments on commit c502312

Please sign in to comment.