ScopeLocatorImpl implements ScopeLocator { @Override public Collection<String> getScopeAliases(long companyId) { Collection<String> scopesAliases = new HashSet<>(); Set<String> applicationNames = _scopeFinderByNameServiceTrackerMap.keySet(); for (String applicationName : applicationNames) { scopesAliases.addAll(getScopeAliases(companyId, applicationName)); } return scopesAliases; }  @Override LiferayOAuth2Scope getLiferayOAuth2Scope(
		long companyId, String applicationName, String scope); @Override Collection<LiferayOAuth2Scope> getLiferayOAuth2Scopes(
		long companyId); @Override Collection<LiferayOAuth2Scope> getLiferayOAuth2Scopes(
		long companyId, String scopesAlias); @Override Collection<LiferayOAuth2Scope> getLiferayOAuth2Scopes(
		long companyId, String scopesAlias, String applicationName); @Override Collection<String> getScopeAliases(long companyId); @Override Collection<String> getScopeAliases(
		long companyId, String applicationName);  }
@Test public void testPrefixHandlerFactoryByNameAndCompany() throws Exception { String applicationName2 = "com.liferay.test2"; PrefixHandler defaultPrefixHandler = target -> "default/" + target; ScopeFinder scopeFinder = () -> scopesSet1; Builder builder = new Builder(); ScopeLocatorImpl scopeLocatorImpl = builder.withPrefixHandlerFactories( propertyAccessor -> defaultPrefixHandler, registrator -> { } ).withScopeFinders( registrator -> { registrator.register( _COMPANY_ID, _APPLICATION_NAME, scopeFinder); registrator.register( _COMPANY_ID, applicationName2, scopeFinder); } ).build(); Collection<String> application1ScopeAliases = scopeLocatorImpl.getScopeAliases(_COMPANY_ID, _APPLICATION_NAME); Collection<String> application2ScopeAliases = scopeLocatorImpl.getScopeAliases(_COMPANY_ID, applicationName2); for (String scope : scopesSet1) { Assert.assertThat( application1ScopeAliases, hasItem(defaultPrefixHandler.addPrefix(scope))); Assert.assertThat( application2ScopeAliases, hasItem(defaultPrefixHandler.addPrefix(scope))); } PrefixHandler appPrefixHandler = target -> "app/" + target; PrefixHandler companyPrefixHandler = target -> "company/" + target; builder = new Builder(); scopeLocatorImpl = builder.withPrefixHandlerFactories( propertyAccessor -> defaultPrefixHandler, registrator -> { registrator.register( null, _APPLICATION_NAME, propertyAccessor -> appPrefixHandler); registrator.register( _COMPANY_ID, null, propertyAccessor -> companyPrefixHandler); } ).withScopeFinders( registrator -> { registrator.register( _COMPANY_ID, _APPLICATION_NAME, scopeFinder); registrator.register( _COMPANY_ID, applicationName2, scopeFinder); } ).build(); application1ScopeAliases = scopeLocatorImpl.getScopeAliases( _COMPANY_ID, _APPLICATION_NAME); application2ScopeAliases = scopeLocatorImpl.getScopeAliases( _COMPANY_ID, applicationName2); for (String scope : scopesSet1) { Assert.assertThat( application1ScopeAliases, hasItem(appPrefixHandler.addPrefix(scope))); Assert.assertThat( application2ScopeAliases, hasItem(companyPrefixHandler.addPrefix(scope))); } }
@Test public void testScopeFinderByName() throws Exception { String applicationName2 = "com.liferay.test2"; ScopeFinder application1ScopeFinder = () -> scopesSet1; ScopeFinder application2ScopeFinder = () -> scopedSet2; Builder builder = new Builder(); ScopeLocatorImpl scopeLocatorImpl = builder.withScopeFinders( registrator -> { registrator.register( _COMPANY_ID, _APPLICATION_NAME, application1ScopeFinder); registrator.register( _COMPANY_ID, applicationName2, application2ScopeFinder); } ).build(); Collection<String> application1ScopeAliases = scopeLocatorImpl.getScopeAliases(_COMPANY_ID, _APPLICATION_NAME); Collection<String> application2ScopesAliasesDefault = scopeLocatorImpl.getScopeAliases(_COMPANY_ID, applicationName2); for (String scope : scopesSet1) { Assert.assertThat(application1ScopeAliases, hasItem(scope)); } for (String scope : scopedSet2) { Assert.assertThat(application2ScopesAliasesDefault, hasItem(scope)); } Assert.assertNotEquals( application1ScopeAliases, application2ScopesAliasesDefault); }
@Test public void testScopeMapperByNameAndCompany() throws Exception { String applicationName2 = "com.liferay.test2"; ScopeMapper defaultScopeMapper = ScopeMapper.PASS_THROUGH_SCOPE_MAPPER; ScopeFinder scopeFinder = () -> scopesSet1; Builder builder = new Builder(); ScopeLocatorImpl scopeLocatorImpl = builder.withScopeMappers( defaultScopeMapper, registrator -> { } ).withScopeFinders( registrator -> { registrator.register( _COMPANY_ID, _APPLICATION_NAME, scopeFinder); registrator.register( _COMPANY_ID, applicationName2, scopeFinder); } ).build(); Collection<String> application1ScopeAliases = scopeLocatorImpl.getScopeAliases(_COMPANY_ID, _APPLICATION_NAME); Collection<String> application2ScopeAliases = scopeLocatorImpl.getScopeAliases(_COMPANY_ID, applicationName2); for (String scope : scopesSet1) { Assert.assertThat(application1ScopeAliases, hasItem(scope)); Assert.assertThat(application2ScopeAliases, hasItem(scope)); } ScopeMapper appScopeMapper = scope -> Collections.singleton( "app/" + scope); ScopeMapper companyScopeMapper = scope -> Collections.singleton( "company/" + scope); builder = new Builder(); scopeLocatorImpl = builder.withScopeMappers( defaultScopeMapper, registrator -> { registrator.register(null, _APPLICATION_NAME, appScopeMapper); registrator.register(_COMPANY_ID, null, companyScopeMapper); } ).withScopeFinders( registrator -> { registrator.register( _COMPANY_ID, _APPLICATION_NAME, scopeFinder); registrator.register( _COMPANY_ID, applicationName2, scopeFinder); } ).build(); Collection<String> application1ScopesAliases = scopeLocatorImpl.getScopeAliases(_COMPANY_ID, _APPLICATION_NAME); Collection<String> application2ScopesAliases = scopeLocatorImpl.getScopeAliases(_COMPANY_ID, applicationName2); for (String scope : scopesSet1) { Assert.assertThat( application1ScopesAliases, hasItems( appScopeMapper.map( scope ).toArray( new String[0] ))); Assert.assertThat( application2ScopesAliases, hasItems( companyScopeMapper.map( scope ).toArray( new String[0] ))); } }