ScopeLocatorImpl implements ScopeLocator { @Override public Collection<LiferayOAuth2Scope> getLiferayOAuth2Scopes( long companyId) { Collection<LiferayOAuth2Scope> liferayOAuth2Scopes = new ArrayList<>(); for (String key : _scopeFinderByNameServiceTrackerMap.keySet()) { ScopeFinder scopeFinder = _scopeFindersScopedServiceTrackerMap.getService(companyId, key); ServiceReferenceServiceTuple<?, ScopeFinder> serviceReferenceServiceTuple = _scopeFinderByNameServiceTrackerMap.getService(key); if (scopeFinder == null) { scopeFinder = serviceReferenceServiceTuple.getService(); } Bundle bundle = getBundle( serviceReferenceServiceTuple.getServiceReference()); for (String scope : scopeFinder.findScopes()) { liferayOAuth2Scopes.add( new LiferayOAuth2ScopeImpl(key, bundle, scope)); } } return liferayOAuth2Scopes; }  @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 testScopeMatcherByCompany() throws Exception { String applicationName2 = "com.liferay.test2"; ScopeFinder service = () -> scopesSet1; Set<String> matchScopes = Collections.singleton("everything.readonly"); ScopeMatcherFactory explicitScopeMatcherFactory = scopeAlias -> scope -> scope.equals(scopeAlias) && matchScopes.contains(scope); Builder builder = new Builder(); ScopeLocatorImpl scopeLocatorImpl = builder.withScopeFinders( registrator -> { registrator.register(_COMPANY_ID, _APPLICATION_NAME, service); registrator.register(_COMPANY_ID, applicationName2, service); } ).withScopeMatcherFactories( scopeAlias -> scopeAlias::equals, registrator -> registrator.register( String.valueOf(_COMPANY_ID), explicitScopeMatcherFactory) ).build(); Collection<LiferayOAuth2Scope> matchedLiferayOAuth2Scopes = scopeLocatorImpl.getLiferayOAuth2Scopes( _COMPANY_ID, "everything", _APPLICATION_NAME); Set<String> matchedScopes = _getScopes(matchedLiferayOAuth2Scopes); Assert.assertFalse(matchedScopes.contains("everything")); matchedLiferayOAuth2Scopes = scopeLocatorImpl.getLiferayOAuth2Scopes( _COMPANY_ID, "everything.readonly", _APPLICATION_NAME); matchedScopes = _getScopes(matchedLiferayOAuth2Scopes); Assert.assertTrue(matchedScopes.contains("everything.readonly")); }
@Test public void testScopeMatcherIsolatedFromPrefixHanderFactory() throws Exception { PrefixHandlerFactory testPrefixHandlerFactory = propertyAccessor -> target -> "test/" + target; final ScopeMatcherFactory scopeMatcherFactory = Mockito.spy( new StrictScopeMatcherFactory()); Builder builder = new Builder(); ScopeLocatorImpl scopeLocatorImpl = builder.withPrefixHandlerFactories( propertyAccessor -> PrefixHandler.PASS_THROUGH_PREFIX_HANDLER, registrator -> registrator.register( _COMPANY_ID, _APPLICATION_NAME, testPrefixHandlerFactory) ).withScopeMatcherFactories( scopeAlias -> scopeAlias::equals, registrator -> registrator.register( String.valueOf(_COMPANY_ID), scopeMatcherFactory) ).withScopeFinders( registrator -> registrator.register( _COMPANY_ID, _APPLICATION_NAME, () -> scopesSet1) ).build(); Collection<LiferayOAuth2Scope> matchedLiferayOAuth2Scopes = scopeLocatorImpl.getLiferayOAuth2Scopes( _COMPANY_ID, "test/everything", _APPLICATION_NAME); Mockito.verify( scopeMatcherFactory, Mockito.atLeast(1) ).create( "everything" ); Set<String> matchedScopes = _getScopes(matchedLiferayOAuth2Scopes); Assert.assertTrue(matchedScopes.contains("everything")); }