@Test
  public void scopesMatch_structured() {
    Set<String> expected = Sets.newHashSet("foo", "bar", "baz");
    Set<String> actualGood = Sets.newHashSet("foo:value", "baz", "bar");
    Set<String> actualBad = Sets.newHashSet("foo:value", "bar:value");

    // note: we have to use "thenAnswer" here to mimic the repository not serializing the
    // structuredValue field
    Mockito.when(repository.getByValue("foo"))
        .thenAnswer(
            new Answer<SystemScope>() {
              @Override
              public SystemScope answer(InvocationOnMock invocation) throws Throwable {
                SystemScope foo = new SystemScope("foo");
                foo.setStructured(true);
                return foo;
              }
            });

    assertThat(service.scopesMatch(expected, actualGood), is(true));

    assertThat(service.scopesMatch(expected, actualBad), is(false));
  }
  /** Assumes these SystemScope defaults: isDefaultScope=false and isAllowDynReg=false. */
  @Before
  public void prepare() {

    Mockito.reset(repository);

    // two default and dynamically registerable scopes
    defaultDynScope1 = new SystemScope(defaultDynScope1String);
    defaultDynScope2 = new SystemScope(defaultDynScope2String);
    defaultDynScope1.setAllowDynReg(true);
    defaultDynScope2.setAllowDynReg(true);
    defaultDynScope1.setDefaultScope(true);
    defaultDynScope2.setDefaultScope(true);

    // two strictly default scopes (isAllowDynReg false)
    defaultScope1 = new SystemScope(defaultScope1String);
    defaultScope2 = new SystemScope(defaultScope2String);
    defaultScope1.setDefaultScope(true);
    defaultScope2.setDefaultScope(true);

    // one strictly dynamically registerable scope (isDefault false)
    dynScope1 = new SystemScope(dynScope1String);
    dynScope1.setAllowDynReg(true);

    // extraScope1 : extra scope that is neither (defaults to false/false)
    extraScope1 = new SystemScope(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<SystemScope>() {
              @Override
              public SystemScope answer(InvocationOnMock invocation) throws Throwable {
                SystemScope s = new SystemScope(structuredScope1String);
                s.setStructured(true);
                return s;
              }
            });

    Mockito.when(repository.getAll()).thenReturn(allScopes);
  }