Skip to content

Commit 0299808

Browse files
qavidjzheaux
authored andcommitted
Add ClaimAccessor tests
Add tests for ClaimAccessor#getClaimAsMap and ClaimAccessor#getClaimAsStringList Issue gh-10117
1 parent 125d33e commit 0299808

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/ClaimAccessorTests.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.time.Instant;
2020
import java.util.Arrays;
21+
import java.util.Collections;
2122
import java.util.Date;
2223
import java.util.HashMap;
2324
import java.util.List;
@@ -27,6 +28,8 @@
2728
import org.junit.jupiter.api.Test;
2829

2930
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
32+
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
3033
import static org.assertj.core.api.Assertions.assertThatObject;
3134

3235
/**
@@ -102,6 +105,61 @@ public void getClaimAsStringWhenValueIsNullThenReturnNull() {
102105
assertThat(this.claimAccessor.getClaimAsString(claimName)).isNull();
103106
}
104107

108+
@Test
109+
public void getClaimAsMapWhenNotExistingThenReturnNull() {
110+
assertThat(this.claimAccessor.getClaimAsMap("map")).isNull();
111+
}
112+
113+
@Test
114+
public void getClaimAsMapWhenMapTypeThenReturnMap() {
115+
Map<Object, Object> expectedClaimValue = Collections.emptyMap();
116+
String claimName = "map";
117+
this.claims.put(claimName, expectedClaimValue);
118+
assertThat(this.claimAccessor.getClaimAsMap(claimName)).isEqualTo(expectedClaimValue);
119+
}
120+
121+
@Test
122+
public void getClaimAsMapWhenValueIsNullThenThrowNullPointerException() {
123+
String claimName = "map";
124+
this.claims.put(claimName, null);
125+
assertThatNullPointerException().isThrownBy(() -> this.claimAccessor.getClaimAsMap(claimName));
126+
}
127+
128+
@Test
129+
public void getClaimAsMapWhenNonMapTypeThenThrowIllegalArgumentException() {
130+
String claimName = "map";
131+
this.claims.put(claimName, "map");
132+
assertThatIllegalArgumentException().isThrownBy(() -> this.claimAccessor.getClaimAsMap(claimName));
133+
}
134+
135+
@Test
136+
public void getClaimAsStringListWhenNotExistingThenReturnNull() {
137+
assertThat(this.claimAccessor.getClaimAsStringList("list")).isNull();
138+
}
139+
140+
@Test
141+
public void getClaimAsStringListWhenStringListTypeThenReturnList() {
142+
List<String> expectedClaimValue = Collections.emptyList();
143+
String claimName = "list";
144+
this.claims.put(claimName, expectedClaimValue);
145+
assertThat(this.claimAccessor.getClaimAsStringList(claimName)).isEqualTo(expectedClaimValue);
146+
}
147+
148+
@Test
149+
public void getClaimAsStringListWhenNonListTypeThenReturnList() {
150+
List<String> expectedClaimValue = Collections.singletonList("list");
151+
String claimName = "list";
152+
this.claims.put(claimName, expectedClaimValue.get(0));
153+
assertThat(this.claimAccessor.getClaimAsStringList(claimName)).isEqualTo(expectedClaimValue);
154+
}
155+
156+
@Test
157+
public void getClaimAsStringListWhenValueIsNullThenNullPointerException() {
158+
String claimName = "list";
159+
this.claims.put(claimName, null);
160+
assertThatNullPointerException().isThrownBy(() -> this.claimAccessor.getClaimAsStringList(claimName));
161+
}
162+
105163
@Test
106164
public void getClaimWhenNotExistingThenReturnNull() {
107165
String claimName = "list";

0 commit comments

Comments
 (0)