From 6152a943d879ece549accdf9a6b3f1ae2581d642 Mon Sep 17 00:00:00 2001 From: Justin Richer Date: Fri, 30 Aug 2013 15:15:55 -0400 Subject: [PATCH] serialize structured scopes properly (with tests) --- .../impl/DefaultSystemScopeService.java | 7 ++- .../impl/TestDefaultSystemScopeService.java | 44 ++++++++++++++----- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/openid-connect-server/src/main/java/org/mitre/oauth2/service/impl/DefaultSystemScopeService.java b/openid-connect-server/src/main/java/org/mitre/oauth2/service/impl/DefaultSystemScopeService.java index 316df841b..8aceb4f55 100644 --- a/openid-connect-server/src/main/java/org/mitre/oauth2/service/impl/DefaultSystemScopeService.java +++ b/openid-connect-server/src/main/java/org/mitre/oauth2/service/impl/DefaultSystemScopeService.java @@ -30,6 +30,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.google.common.base.Function; +import com.google.common.base.Joiner; import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.base.Splitter; @@ -96,7 +97,11 @@ public class DefaultSystemScopeService implements SystemScopeService { if (input == null) { return null; } else { - return input.getValue(); + if (input.isStructured() && !Strings.isNullOrEmpty(input.getStructuredValue())) { + return Joiner.on(":").join(input.getValue(), input.getStructuredValue()); + } else { + return input.getValue(); + } } } }; diff --git a/openid-connect-server/src/test/java/org/mitre/oauth2/service/impl/TestDefaultSystemScopeService.java b/openid-connect-server/src/test/java/org/mitre/oauth2/service/impl/TestDefaultSystemScopeService.java index bd9e252c1..abbb951eb 100644 --- a/openid-connect-server/src/test/java/org/mitre/oauth2/service/impl/TestDefaultSystemScopeService.java +++ b/openid-connect-server/src/test/java/org/mitre/oauth2/service/impl/TestDefaultSystemScopeService.java @@ -51,6 +51,8 @@ public class TestDefaultSystemScopeService { private SystemScope defaultScope2; private SystemScope dynScope1; private SystemScope extraScope1; + private SystemScope structuredScope1; + private SystemScope structuredScope1Value; private String defaultDynScope1String = "defaultDynScope1"; private String defaultDynScope2String = "defaultDynScope2"; @@ -58,6 +60,8 @@ public class TestDefaultSystemScopeService { private String defaultScope2String = "defaultScope2"; private String dynScope1String = "dynScope1"; private String extraScope1String = "extraScope1"; + private String structuredScope1String = "structuredScope1"; + private String structuredValue = "structuredValue"; private Set allScopes; private Set allScopeStrings; @@ -96,10 +100,36 @@ public class TestDefaultSystemScopeService { // extraScope1 : extra scope that is neither (defaults to false/false) extraScope1 = new SystemScope(extraScope1String); - - allScopes = Sets.newHashSet(defaultDynScope1, defaultDynScope2, defaultScope1, defaultScope2, dynScope1, extraScope1); - allScopeStrings = Sets.newHashSet(defaultDynScope1String, defaultDynScope2String, defaultScope1String, defaultScope2String, dynScope1String, extraScope1String); - + + // structuredScope1 : structured scope + structuredScope1 = new SystemScope(structuredScope1String); + structuredScope1.setStructured(true); + + // structuredScope1Value : structured scope with value + structuredScope1Value = new SystemScope(structuredScope1String); + structuredScope1Value.setStructured(true); + structuredScope1Value.setStructuredValue(structuredValue); + + allScopes = Sets.newHashSet(defaultDynScope1, defaultDynScope2, defaultScope1, defaultScope2, dynScope1, extraScope1, structuredScope1, structuredScope1Value); + allScopeStrings = Sets.newHashSet(defaultDynScope1String, defaultDynScope2String, defaultScope1String, defaultScope2String, dynScope1String, extraScope1String, structuredScope1String, structuredScope1String + ":" + structuredValue); + + Mockito.when(repository.getByValue(defaultDynScope1String)).thenReturn(defaultDynScope1); + Mockito.when(repository.getByValue(defaultDynScope2String)).thenReturn(defaultDynScope2); + Mockito.when(repository.getByValue(defaultScope1String)).thenReturn(defaultScope1); + Mockito.when(repository.getByValue(defaultScope2String)).thenReturn(defaultScope2); + Mockito.when(repository.getByValue(dynScope1String)).thenReturn(dynScope1); + Mockito.when(repository.getByValue(extraScope1String)).thenReturn(extraScope1); + // we re-use this value so we've got to use thenAnswer instead + Mockito.when(repository.getByValue(structuredScope1String)).thenAnswer(new Answer() { + @Override + public SystemScope answer(InvocationOnMock invocation) throws Throwable { + SystemScope s = new SystemScope(structuredScope1String); + s.setStructured(true); + return s; + } + + }); + Mockito.when(repository.getAll()).thenReturn(allScopes); } @@ -131,12 +161,6 @@ public class TestDefaultSystemScopeService { // check null condition assertThat(service.fromStrings(null), is(nullValue())); - // reinitialize the set of SystemScope objects to clear boolean flags.. - allScopes = Sets.newHashSet(); - for (String scope : allScopeStrings) { - allScopes.add(new SystemScope(scope)); - } - assertThat(service.fromStrings(allScopeStrings), equalTo(allScopes)); }