Skip to content

Commit

Permalink
sonar refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
strehle committed May 23, 2023
1 parent a1aef75 commit f156c53
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

Expand All @@ -33,18 +34,18 @@
*/
public class JsonWebKeySet<T extends JsonWebKey> {

private static final String KEYS = "keys";
private static final String JSON_PROPERTY_KEYS = "keys";
private final List<T> keys;

public JsonWebKeySet(@JsonProperty(KEYS) List<T> keys) {
public JsonWebKeySet(@JsonProperty(JSON_PROPERTY_KEYS) List<T> keys) {
Set<T> set = new LinkedHashSet<>();
//rules for how to override duplicates
for (T key : keys) {
if(key == null) continue;
set.remove(key);
set.add(key);
}
this.keys = new LinkedList(set);
this.keys = new LinkedList<>(set);
}

public List<T> getKeys() {
Expand All @@ -54,9 +55,12 @@ public List<T> getKeys() {
@JsonIgnore
public Map<String, Object> getKeySetMap() {
Map<String, Object> keySet = new HashMap<>();
ArrayList keyArray = new ArrayList();
Optional.ofNullable(keys).orElseThrow(() -> new IllegalStateException("No keys found.")).stream().forEach(k -> keyArray.add(k.getKeyProperties()));
keySet.put(KEYS, keyArray);
ArrayList<Map<String, Object>> keyArray = new ArrayList<>();
long keyCount = Optional.ofNullable(keys).orElseThrow(() -> new IllegalStateException("No keys found.")).stream()
.map(k -> keyArray.add(k.getKeyProperties())).filter(Objects::nonNull).count();
if (keyCount > 0) {
keySet.put(JSON_PROPERTY_KEYS, keyArray);
}
return keySet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.cloudfoundry.identity.uaa.util.JsonUtils;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Map;

import static org.cloudfoundry.identity.uaa.test.ModelTestUtils.getResourceAsString;
Expand All @@ -26,6 +27,7 @@ void testWebKeyPublic() {
// then
assertEquals(samlKeySet.getKeys().get(0).getKid(), jsonWebKey.getKid());
assertEquals(samlKeySet.getKeys().get(0).getX5t(), jsonWebKey.getX5t());
assertEquals(3, ((ArrayList) samlKeySet.getKeySetMap().get("keys")).size());
}

@Test
Expand Down

0 comments on commit f156c53

Please sign in to comment.