From 0299808b054bf3c5609dc805aa8cb0e1621c538e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Kov=C3=A1=C4=8D?= Date: Wed, 13 Oct 2021 20:07:43 +0200 Subject: [PATCH] Add ClaimAccessor tests Add tests for ClaimAccessor#getClaimAsMap and ClaimAccessor#getClaimAsStringList Issue gh-10117 --- .../oauth2/core/ClaimAccessorTests.java | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/ClaimAccessorTests.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/ClaimAccessorTests.java index f1f50836c2..456a2a29d6 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/ClaimAccessorTests.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/ClaimAccessorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 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. @@ -18,6 +18,7 @@ package org.springframework.security.oauth2.core; import java.time.Instant; import java.util.Arrays; +import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -27,6 +28,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.assertThatNullPointerException; import static org.assertj.core.api.Assertions.assertThatObject; /** @@ -102,6 +105,61 @@ public class ClaimAccessorTests { assertThat(this.claimAccessor.getClaimAsString(claimName)).isNull(); } + @Test + public void getClaimAsMapWhenNotExistingThenReturnNull() { + assertThat(this.claimAccessor.getClaimAsMap("map")).isNull(); + } + + @Test + public void getClaimAsMapWhenMapTypeThenReturnMap() { + Map expectedClaimValue = Collections.emptyMap(); + String claimName = "map"; + this.claims.put(claimName, expectedClaimValue); + assertThat(this.claimAccessor.getClaimAsMap(claimName)).isEqualTo(expectedClaimValue); + } + + @Test + public void getClaimAsMapWhenValueIsNullThenThrowNullPointerException() { + String claimName = "map"; + this.claims.put(claimName, null); + assertThatNullPointerException().isThrownBy(() -> this.claimAccessor.getClaimAsMap(claimName)); + } + + @Test + public void getClaimAsMapWhenNonMapTypeThenThrowIllegalArgumentException() { + String claimName = "map"; + this.claims.put(claimName, "map"); + assertThatIllegalArgumentException().isThrownBy(() -> this.claimAccessor.getClaimAsMap(claimName)); + } + + @Test + public void getClaimAsStringListWhenNotExistingThenReturnNull() { + assertThat(this.claimAccessor.getClaimAsStringList("list")).isNull(); + } + + @Test + public void getClaimAsStringListWhenStringListTypeThenReturnList() { + List expectedClaimValue = Collections.emptyList(); + String claimName = "list"; + this.claims.put(claimName, expectedClaimValue); + assertThat(this.claimAccessor.getClaimAsStringList(claimName)).isEqualTo(expectedClaimValue); + } + + @Test + public void getClaimAsStringListWhenNonListTypeThenReturnList() { + List expectedClaimValue = Collections.singletonList("list"); + String claimName = "list"; + this.claims.put(claimName, expectedClaimValue.get(0)); + assertThat(this.claimAccessor.getClaimAsStringList(claimName)).isEqualTo(expectedClaimValue); + } + + @Test + public void getClaimAsStringListWhenValueIsNullThenNullPointerException() { + String claimName = "list"; + this.claims.put(claimName, null); + assertThatNullPointerException().isThrownBy(() -> this.claimAccessor.getClaimAsStringList(claimName)); + } + @Test public void getClaimWhenNotExistingThenReturnNull() { String claimName = "list";