Use assertj assertions

This commit is contained in:
Marcus Hert Da Coregio 2023-11-17 08:27:19 -03:00
parent 382cfd63ed
commit a7da9491d9
9 changed files with 99 additions and 102 deletions

View File

@ -29,7 +29,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ -461,7 +460,7 @@ public class Saml2LoginConfigurerTests {
Authentication authentication = this.securityContextRepository
.loadContext(new HttpRequestResponseHolder(this.request, this.response))
.getAuthentication();
Assertions.assertNotNull(authentication, "Expected a valid authentication object.");
assertThat(authentication).as("Expected a valid authentication object.").isNotNull();
assertThat(authentication.getAuthorities()).hasSize(1);
assertThat(authentication.getAuthorities()).first()
.isInstanceOf(SimpleGrantedAuthority.class)

View File

@ -16,7 +16,6 @@
package org.springframework.security.access.intercept;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.security.authentication.BadCredentialsException;
@ -50,7 +49,7 @@ public class RunAsImplAuthenticationProviderTests {
RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
provider.setKey("my_password");
Authentication result = provider.authenticate(token);
Assertions.assertTrue(result instanceof RunAsUserToken, "Should have returned RunAsUserToken");
assertThat(result instanceof RunAsUserToken).as("Should have returned RunAsUserToken").isTrue();
RunAsUserToken resultCast = (RunAsUserToken) result;
assertThat(resultCast.getKeyHash()).isEqualTo("my_password".hashCode());
}

View File

@ -20,7 +20,6 @@ import java.security.SecureRandom;
import java.util.Random;
import java.util.UUID;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -29,6 +28,8 @@ import org.springframework.security.crypto.encrypt.AesBytesEncryptor.CipherAlgor
import org.springframework.security.crypto.keygen.BytesKeyGenerator;
import org.springframework.security.crypto.keygen.KeyGenerators;
import static org.assertj.core.api.Assertions.assertThat;
public class BouncyCastleAesBytesEncryptorEquivalencyTests {
private byte[] testData;
@ -96,11 +97,11 @@ public class BouncyCastleAesBytesEncryptorEquivalencyTests {
// and can decrypt back to the original input
byte[] leftEncrypted = left.encrypt(this.testData);
byte[] rightEncrypted = right.encrypt(this.testData);
Assertions.assertArrayEquals(leftEncrypted, rightEncrypted);
assertThat(rightEncrypted).containsExactly(leftEncrypted);
byte[] leftDecrypted = left.decrypt(leftEncrypted);
byte[] rightDecrypted = right.decrypt(rightEncrypted);
Assertions.assertArrayEquals(this.testData, leftDecrypted);
Assertions.assertArrayEquals(this.testData, rightDecrypted);
assertThat(leftDecrypted).containsExactly(this.testData);
assertThat(rightDecrypted).containsExactly(this.testData);
}
}
@ -114,8 +115,8 @@ public class BouncyCastleAesBytesEncryptorEquivalencyTests {
byte[] rightEncrypted = right.encrypt(this.testData);
byte[] leftDecrypted = left.decrypt(rightEncrypted);
byte[] rightDecrypted = right.decrypt(leftEncrypted);
Assertions.assertArrayEquals(this.testData, leftDecrypted);
Assertions.assertArrayEquals(this.testData, rightDecrypted);
assertThat(leftDecrypted).containsExactly(this.testData);
assertThat(rightDecrypted).containsExactly(this.testData);
}
}

View File

@ -20,13 +20,13 @@ import java.security.SecureRandom;
import java.util.UUID;
import org.bouncycastle.util.Arrays;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.security.crypto.codec.Hex;
import org.springframework.security.crypto.keygen.KeyGenerators;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
public class BouncyCastleAesBytesEncryptorTests {
@ -64,11 +64,11 @@ public class BouncyCastleAesBytesEncryptorTests {
private void generatesDifferentCipherTexts(BytesEncryptor bcEncryptor) {
byte[] encrypted1 = bcEncryptor.encrypt(this.testData);
byte[] encrypted2 = bcEncryptor.encrypt(this.testData);
Assertions.assertFalse(Arrays.areEqual(encrypted1, encrypted2));
assertThat(Arrays.areEqual(encrypted1, encrypted2)).isFalse();
byte[] decrypted1 = bcEncryptor.decrypt(encrypted1);
byte[] decrypted2 = bcEncryptor.decrypt(encrypted2);
Assertions.assertArrayEquals(this.testData, decrypted1);
Assertions.assertArrayEquals(this.testData, decrypted2);
assertThat(decrypted1).containsExactly(this.testData);
assertThat(decrypted2).containsExactly(this.testData);
}
@Test

View File

@ -22,7 +22,6 @@ import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -30,6 +29,8 @@ import org.springframework.core.convert.converter.Converter;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link DefaultMapOAuth2AccessTokenResponseConverter}.
*
@ -56,24 +57,24 @@ public class DefaultMapOAuth2AccessTokenResponseConverterTests {
map.put("custom_parameter_2", "custom-value-2");
OAuth2AccessTokenResponse converted = this.messageConverter.convert(map);
OAuth2AccessToken accessToken = converted.getAccessToken();
Assertions.assertNotNull(accessToken);
Assertions.assertEquals("access-token-1234", accessToken.getTokenValue());
Assertions.assertEquals(OAuth2AccessToken.TokenType.BEARER, accessToken.getTokenType());
assertThat(accessToken).isNotNull();
assertThat(accessToken.getTokenValue()).isEqualTo("access-token-1234");
assertThat(accessToken.getTokenType()).isEqualTo(OAuth2AccessToken.TokenType.BEARER);
Set<String> scopes = accessToken.getScopes();
Assertions.assertNotNull(scopes);
Assertions.assertEquals(2, scopes.size());
Assertions.assertTrue(scopes.contains("read"));
Assertions.assertTrue(scopes.contains("write"));
Assertions.assertEquals(3600,
Duration.between(accessToken.getIssuedAt(), accessToken.getExpiresAt()).getSeconds());
assertThat(scopes).isNotNull();
assertThat(scopes.size()).isEqualTo(2);
assertThat(scopes.contains("read")).isTrue();
assertThat(scopes.contains("write")).isTrue();
assertThat(Duration.between(accessToken.getIssuedAt(), accessToken.getExpiresAt()).getSeconds())
.isEqualTo(3600);
OAuth2RefreshToken refreshToken = converted.getRefreshToken();
Assertions.assertNotNull(refreshToken);
Assertions.assertEquals("refresh-token-1234", refreshToken.getTokenValue());
assertThat(refreshToken).isNotNull();
assertThat(refreshToken.getTokenValue()).isEqualTo("refresh-token-1234");
Map<String, Object> additionalParameters = converted.getAdditionalParameters();
Assertions.assertNotNull(additionalParameters);
Assertions.assertEquals(2, additionalParameters.size());
Assertions.assertEquals("custom-value-1", additionalParameters.get("custom_parameter_1"));
Assertions.assertEquals("custom-value-2", additionalParameters.get("custom_parameter_2"));
assertThat(additionalParameters).isNotNull();
assertThat(additionalParameters.size()).isEqualTo(2);
assertThat(additionalParameters.get("custom_parameter_1")).isEqualTo("custom-value-1");
assertThat(additionalParameters.get("custom_parameter_2")).isEqualTo("custom-value-2");
}
@Test
@ -83,19 +84,18 @@ public class DefaultMapOAuth2AccessTokenResponseConverterTests {
map.put("token_type", "bearer");
OAuth2AccessTokenResponse converted = this.messageConverter.convert(map);
OAuth2AccessToken accessToken = converted.getAccessToken();
Assertions.assertNotNull(accessToken);
Assertions.assertEquals("access-token-1234", accessToken.getTokenValue());
Assertions.assertEquals(OAuth2AccessToken.TokenType.BEARER, accessToken.getTokenType());
assertThat(accessToken).isNotNull();
assertThat(accessToken.getTokenValue()).isEqualTo("access-token-1234");
assertThat(accessToken.getTokenType()).isEqualTo(OAuth2AccessToken.TokenType.BEARER);
Set<String> scopes = accessToken.getScopes();
Assertions.assertNotNull(scopes);
Assertions.assertEquals(0, scopes.size());
Assertions.assertEquals(1,
Duration.between(accessToken.getIssuedAt(), accessToken.getExpiresAt()).getSeconds());
assertThat(scopes).isNotNull();
assertThat(scopes.size()).isEqualTo(0);
assertThat(Duration.between(accessToken.getIssuedAt(), accessToken.getExpiresAt()).getSeconds()).isEqualTo(1);
OAuth2RefreshToken refreshToken = converted.getRefreshToken();
Assertions.assertNull(refreshToken);
assertThat(refreshToken).isNull();
Map<String, Object> additionalParameters = converted.getAdditionalParameters();
Assertions.assertNotNull(additionalParameters);
Assertions.assertEquals(0, additionalParameters.size());
assertThat(additionalParameters).isNotNull();
assertThat(additionalParameters.size()).isEqualTo(0);
}
@Test
@ -106,19 +106,18 @@ public class DefaultMapOAuth2AccessTokenResponseConverterTests {
map.put("expires_in", "2100-01-01-abc");
OAuth2AccessTokenResponse converted = this.messageConverter.convert(map);
OAuth2AccessToken accessToken = converted.getAccessToken();
Assertions.assertNotNull(accessToken);
Assertions.assertEquals("access-token-1234", accessToken.getTokenValue());
Assertions.assertEquals(OAuth2AccessToken.TokenType.BEARER, accessToken.getTokenType());
assertThat(accessToken).isNotNull();
assertThat(accessToken.getTokenValue()).isEqualTo("access-token-1234");
assertThat(accessToken.getTokenType()).isEqualTo(OAuth2AccessToken.TokenType.BEARER);
Set<String> scopes = accessToken.getScopes();
Assertions.assertNotNull(scopes);
Assertions.assertEquals(0, scopes.size());
Assertions.assertEquals(1,
Duration.between(accessToken.getIssuedAt(), accessToken.getExpiresAt()).getSeconds());
assertThat(scopes).isNotNull();
assertThat(scopes.size()).isEqualTo(0);
assertThat(Duration.between(accessToken.getIssuedAt(), accessToken.getExpiresAt()).getSeconds()).isEqualTo(1);
OAuth2RefreshToken refreshToken = converted.getRefreshToken();
Assertions.assertNull(refreshToken);
assertThat(refreshToken).isNull();
Map<String, Object> additionalParameters = converted.getAdditionalParameters();
Assertions.assertNotNull(additionalParameters);
Assertions.assertEquals(0, additionalParameters.size());
assertThat(additionalParameters).isNotNull();
assertThat(additionalParameters.size()).isEqualTo(0);
}
// gh-9685
@ -130,11 +129,11 @@ public class DefaultMapOAuth2AccessTokenResponseConverterTests {
map.put("expires_in", 3600);
OAuth2AccessTokenResponse converted = this.messageConverter.convert(map);
OAuth2AccessToken accessToken = converted.getAccessToken();
Assertions.assertNotNull(accessToken);
Assertions.assertEquals("access-token-1234", accessToken.getTokenValue());
Assertions.assertEquals(OAuth2AccessToken.TokenType.BEARER, accessToken.getTokenType());
Assertions.assertEquals(3600,
Duration.between(accessToken.getIssuedAt(), accessToken.getExpiresAt()).getSeconds());
assertThat(accessToken).isNotNull();
assertThat(accessToken.getTokenValue()).isEqualTo("access-token-1234");
assertThat(accessToken.getTokenType()).isEqualTo(OAuth2AccessToken.TokenType.BEARER);
assertThat(Duration.between(accessToken.getIssuedAt(), accessToken.getExpiresAt()).getSeconds())
.isEqualTo(3600);
}
// gh-9685
@ -153,24 +152,24 @@ public class DefaultMapOAuth2AccessTokenResponseConverterTests {
map.put("custom_parameter_2", "custom-value-2");
OAuth2AccessTokenResponse converted = this.messageConverter.convert(map);
OAuth2AccessToken accessToken = converted.getAccessToken();
Assertions.assertNotNull(accessToken);
Assertions.assertEquals("access-token-1234", accessToken.getTokenValue());
Assertions.assertEquals(OAuth2AccessToken.TokenType.BEARER, accessToken.getTokenType());
assertThat(accessToken).isNotNull();
assertThat(accessToken.getTokenValue()).isEqualTo("access-token-1234");
assertThat(accessToken.getTokenType()).isEqualTo(OAuth2AccessToken.TokenType.BEARER);
Set<String> scopes = accessToken.getScopes();
Assertions.assertNotNull(scopes);
Assertions.assertEquals(2, scopes.size());
Assertions.assertTrue(scopes.contains("read"));
Assertions.assertTrue(scopes.contains("write"));
Assertions.assertEquals(3600,
Duration.between(accessToken.getIssuedAt(), accessToken.getExpiresAt()).getSeconds());
assertThat(scopes).isNotNull();
assertThat(scopes.size()).isEqualTo(2);
assertThat(scopes.contains("read")).isTrue();
assertThat(scopes.contains("write")).isTrue();
assertThat(Duration.between(accessToken.getIssuedAt(), accessToken.getExpiresAt()).getSeconds())
.isEqualTo(3600);
OAuth2RefreshToken refreshToken = converted.getRefreshToken();
Assertions.assertNotNull(refreshToken);
Assertions.assertEquals("refresh-token-1234", refreshToken.getTokenValue());
assertThat(refreshToken).isNotNull();
assertThat(refreshToken.getTokenValue()).isEqualTo("refresh-token-1234");
Map<String, Object> additionalParameters = converted.getAdditionalParameters();
Assertions.assertNotNull(additionalParameters);
Assertions.assertEquals(2, additionalParameters.size());
Assertions.assertEquals(nestedObject, additionalParameters.get("custom_parameter_1"));
Assertions.assertEquals("custom-value-2", additionalParameters.get("custom_parameter_2"));
assertThat(additionalParameters).isNotNull();
assertThat(additionalParameters.size()).isEqualTo(2);
assertThat(additionalParameters.get("custom_parameter_1")).isEqualTo(nestedObject);
assertThat(additionalParameters.get("custom_parameter_2")).isEqualTo("custom-value-2");
}
}

View File

@ -22,13 +22,14 @@ import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.convert.converter.Converter;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link DefaultOAuth2AccessTokenResponseMapConverter}.
*
@ -61,14 +62,14 @@ public class DefaultOAuth2AccessTokenResponseMapConverterTests {
.build();
// @formatter:on
Map<String, Object> result = this.messageConverter.convert(build);
Assertions.assertEquals(7, result.size());
Assertions.assertEquals("access-token-value-1234", result.get("access_token"));
Assertions.assertEquals("refresh-token-value-1234", result.get("refresh_token"));
Assertions.assertEquals("read write", result.get("scope"));
Assertions.assertEquals("Bearer", result.get("token_type"));
Assertions.assertNotNull(result.get("expires_in"));
Assertions.assertEquals("custom-value-1", result.get("custom_parameter_1"));
Assertions.assertEquals("custom-value-2", result.get("custom_parameter_2"));
assertThat(result.size()).isEqualTo(7);
assertThat(result.get("access_token")).isEqualTo("access-token-value-1234");
assertThat(result.get("refresh_token")).isEqualTo("refresh-token-value-1234");
assertThat(result.get("scope")).isEqualTo("read write");
assertThat(result.get("token_type")).isEqualTo("Bearer");
assertThat(result.get("expires_in")).isNotNull();
assertThat(result.get("custom_parameter_1")).isEqualTo("custom-value-1");
assertThat(result.get("custom_parameter_2")).isEqualTo("custom-value-2");
}
@Test
@ -79,10 +80,10 @@ public class DefaultOAuth2AccessTokenResponseMapConverterTests {
.build();
// @formatter:on
Map<String, Object> result = this.messageConverter.convert(build);
Assertions.assertEquals(3, result.size());
Assertions.assertEquals("access-token-value-1234", result.get("access_token"));
Assertions.assertEquals("Bearer", result.get("token_type"));
Assertions.assertNotNull(result.get("expires_in"));
assertThat(result.size()).isEqualTo(3);
assertThat(result.get("access_token")).isEqualTo("access-token-value-1234");
assertThat(result.get("token_type")).isEqualTo("Bearer");
assertThat(result.get("expires_in")).isNotNull();
}
// gh-9685
@ -107,14 +108,14 @@ public class DefaultOAuth2AccessTokenResponseMapConverterTests {
.build();
// @formatter:on
Map<String, Object> result = this.messageConverter.convert(build);
Assertions.assertEquals(7, result.size());
Assertions.assertEquals("access-token-value-1234", result.get("access_token"));
Assertions.assertEquals("refresh-token-value-1234", result.get("refresh_token"));
Assertions.assertEquals("read write", result.get("scope"));
Assertions.assertEquals("Bearer", result.get("token_type"));
Assertions.assertNotNull(result.get("expires_in"));
Assertions.assertEquals(nestedObject, result.get("custom_parameter_1"));
Assertions.assertEquals("custom-value-2", result.get("custom_parameter_2"));
assertThat(result.size()).isEqualTo(7);
assertThat(result.get("access_token")).isEqualTo("access-token-value-1234");
assertThat(result.get("refresh_token")).isEqualTo("refresh-token-value-1234");
assertThat(result.get("scope")).isEqualTo("read write");
assertThat(result.get("token_type")).isEqualTo("Bearer");
assertThat(result.get("expires_in")).isNotNull();
assertThat(result.get("custom_parameter_1")).isEqualTo(nestedObject);
assertThat(result.get("custom_parameter_2")).isEqualTo("custom-value-2");
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,7 +20,6 @@ import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport;
@ -190,14 +189,14 @@ public class OpenSamlAuthenticationRequestFactoryTests {
@Test
public void createAuthenticationRequestWhenDefaultThenReturnsPostBinding() {
AuthnRequest authn = getAuthNRequest(Saml2MessageBinding.POST);
Assertions.assertEquals(SAMLConstants.SAML2_POST_BINDING_URI, authn.getProtocolBinding());
assertThat(SAMLConstants.SAML2_POST_BINDING_URI).isEqualTo(authn.getProtocolBinding());
}
@Test
public void createAuthenticationRequestWhenSetUriThenReturnsCorrectBinding() {
this.factory.setProtocolBinding(SAMLConstants.SAML2_REDIRECT_BINDING_URI);
AuthnRequest authn = getAuthNRequest(Saml2MessageBinding.POST);
Assertions.assertEquals(SAMLConstants.SAML2_REDIRECT_BINDING_URI, authn.getProtocolBinding());
assertThat(SAMLConstants.SAML2_REDIRECT_BINDING_URI).isEqualTo(authn.getProtocolBinding());
}
@Test

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -20,7 +20,6 @@ import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport;
@ -190,7 +189,7 @@ public class OpenSaml4AuthenticationRequestFactoryTests {
@Test
public void createAuthenticationRequestWhenDefaultThenReturnsPostBinding() {
AuthnRequest authn = getAuthNRequest(Saml2MessageBinding.POST);
Assertions.assertEquals(SAMLConstants.SAML2_POST_BINDING_URI, authn.getProtocolBinding());
assertThat(SAMLConstants.SAML2_POST_BINDING_URI).isEqualTo(authn.getProtocolBinding());
}
@Test

View File

@ -18,7 +18,6 @@ package org.springframework.security.saml2.provider.service.web.authentication;
import javax.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -46,6 +45,7 @@ import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
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.assertThatNoException;
@ -94,7 +94,7 @@ public class Saml2WebSsoAuthenticationFilterTests {
@Test
public void requiresAuthenticationWhenHappyPathThenReturnsTrue() {
Assertions.assertTrue(this.filter.requiresAuthentication(this.request, this.response));
assertThat(this.filter.requiresAuthentication(this.request, this.response)).isTrue();
}
@Test
@ -102,7 +102,7 @@ public class Saml2WebSsoAuthenticationFilterTests {
this.filter = new Saml2WebSsoAuthenticationFilter(this.repository, "/some/other/path/{registrationId}");
this.request.setPathInfo("/some/other/path/idp-registration-id");
this.request.setParameter(Saml2ParameterNames.SAML_RESPONSE, "xml-data-goes-here");
Assertions.assertTrue(this.filter.requiresAuthentication(this.request, this.response));
assertThat(this.filter.requiresAuthentication(this.request, this.response)).isTrue();
}
@Test
@ -143,7 +143,7 @@ public class Saml2WebSsoAuthenticationFilterTests {
this.filter.setAuthenticationDetailsSource(authenticationDetailsSource);
this.request.setPathInfo("/some/other/path/idp-registration-id");
this.filter.attemptAuthentication(this.request, this.response);
Assertions.assertEquals(details, token.getDetails());
assertThat(token.getDetails()).isEqualTo(details);
}
@Test