|
| 1 | +/* |
| 2 | + * Copyright 2020-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.security.oauth2.server.authorization.jackson2; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.LinkedHashSet; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Set; |
| 25 | + |
| 26 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 27 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 28 | +import org.junit.Before; |
| 29 | +import org.junit.Test; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests for {@link OAuth2AuthorizationServerJackson2Module}. |
| 35 | + * |
| 36 | + * @author Steve Riesenberg |
| 37 | + */ |
| 38 | +public class OAuth2AuthorizationServerJackson2ModuleTests { |
| 39 | + |
| 40 | + private static final TypeReference<Map<String, Object>> STRING_OBJECT_MAP = new TypeReference<Map<String, Object>>() { |
| 41 | + }; |
| 42 | + private static final TypeReference<Set<String>> STRING_SET = new TypeReference<Set<String>>() { |
| 43 | + }; |
| 44 | + |
| 45 | + private ObjectMapper objectMapper; |
| 46 | + |
| 47 | + @Before |
| 48 | + public void setup() { |
| 49 | + this.objectMapper = new ObjectMapper(); |
| 50 | + this.objectMapper.registerModule(new OAuth2AuthorizationServerJackson2Module()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void readValueWhenUnmodifiableMapThenSuccess() throws Exception { |
| 55 | + Map<String, Object> map = Collections.unmodifiableMap(new HashMap<>(Collections.singletonMap("key", "value"))); |
| 56 | + String json = this.objectMapper.writeValueAsString(map); |
| 57 | + assertThat(this.objectMapper.readValue(json, STRING_OBJECT_MAP)).isEqualTo(map); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void readValueWhenHashSetThenSuccess() throws Exception { |
| 62 | + Set<String> set = new HashSet<>(Arrays.asList("one", "two")); |
| 63 | + String json = this.objectMapper.writeValueAsString(set); |
| 64 | + assertThat(this.objectMapper.readValue(json, STRING_SET)).isEqualTo(set); |
| 65 | + } |
| 66 | + |
| 67 | + // gh-457 |
| 68 | + @Test |
| 69 | + public void readValueWhenLinkedHashSetThenSuccess() throws Exception { |
| 70 | + Set<String> set = new LinkedHashSet<>(Arrays.asList("one", "two")); |
| 71 | + String json = this.objectMapper.writeValueAsString(set); |
| 72 | + assertThat(this.objectMapper.readValue(json, STRING_SET)).isEqualTo(set); |
| 73 | + } |
| 74 | +} |
0 commit comments