Browse Source

Add ClaimAccessor tests

Add tests for ClaimAccessor#getClaimAsMap and ClaimAccessor#getClaimAsStringList

Issue gh-10117
Dávid Kováč 3 years ago
parent
commit
0299808b05

+ 59 - 1
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<Object, Object> 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<String> expectedClaimValue = Collections.emptyList();
+		String claimName = "list";
+		this.claims.put(claimName, expectedClaimValue);
+		assertThat(this.claimAccessor.getClaimAsStringList(claimName)).isEqualTo(expectedClaimValue);
+	}
+
+	@Test
+	public void getClaimAsStringListWhenNonListTypeThenReturnList() {
+		List<String> 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";