Search in sources :

Example 1 with ApplicationService

use of io.gravitee.am.service.ApplicationService in project gravitee-access-management by gravitee-io.

the class ApplicationScopeSettingsUpgraderTest method shouldUpgrade.

@Test
public void shouldUpgrade() {
    when(systemTaskRepository.findById(anyString())).thenReturn(Maybe.empty());
    final SystemTask task = new SystemTask();
    task.setStatus(SystemTaskStatus.INITIALIZED.name());
    when(systemTaskRepository.create(any())).thenReturn(Single.just(task));
    final Application appNoSettings = new Application();
    appNoSettings.setSettings(null);
    final Application appNoOauthSetings = new Application();
    appNoOauthSetings.setSettings(new ApplicationSettings());
    final Application appNoScopes = new Application();
    final ApplicationSettings settings = new ApplicationSettings();
    settings.setOauth(new ApplicationOAuthSettings());
    appNoScopes.setSettings(settings);
    final Application appScopes = new Application();
    final ApplicationSettings settingsWithScopes = new ApplicationSettings();
    final ApplicationOAuthSettings oauth = new ApplicationOAuthSettings();
    oauth.setScopes(Arrays.asList(SCOPE_OPENID, SCOPE_PROFILE));
    settingsWithScopes.setOauth(oauth);
    appScopes.setSettings(settingsWithScopes);
    final Application appScopesWithOptions = new Application();
    final ApplicationSettings settingsWithScopesWithOptions = new ApplicationSettings();
    final ApplicationOAuthSettings oauthWithOptions = new ApplicationOAuthSettings();
    oauthWithOptions.setScopes(Arrays.asList(SCOPE_OPENID, SCOPE_PROFILE));
    oauthWithOptions.setDefaultScopes(Arrays.asList(SCOPE_OPENID));
    oauthWithOptions.setScopeApprovals(Maps.<String, Integer>builder().put(SCOPE_PROFILE, 42).build());
    settingsWithScopes.setOauth(oauthWithOptions);
    appScopesWithOptions.setSettings(settingsWithScopesWithOptions);
    when(applicationService.findAll()).thenReturn(Single.just(Set.of(appNoSettings, appNoOauthSetings, appNoScopes, appScopes, appScopesWithOptions)));
    when(applicationService.update(any())).thenReturn(Single.just(new Application()));
    when(systemTaskRepository.updateIf(any(), anyString())).thenAnswer((args) -> {
        SystemTask sysTask = args.getArgument(0);
        sysTask.setOperationId(args.getArgument(1));
        return Single.just(sysTask);
    });
    upgrader.upgrade();
    verify(systemTaskRepository, times(1)).findById(anyString());
    verify(applicationService).findAll();
    verify(applicationService, atMost(2)).update(argThat(app -> {
        return app.getSettings() == null || app.getSettings().getOauth() == null;
    }));
    verify(applicationService, atMost(1)).update(argThat(app -> {
        return app.getSettings() != null && app.getSettings().getOauth() != null && app.getSettings().getOauth().getScopeSettings() == null;
    }));
    verify(applicationService, atMost(1)).update(argThat(app -> {
        final boolean withScopeSettings = app.getSettings() != null && app.getSettings().getOauth() != null && app.getSettings().getOauth().getScopeSettings() != null;
        return withScopeSettings && app.getSettings().getOauth().getScopeSettings().stream().allMatch(a -> {
            return (a.getScope().equalsIgnoreCase(SCOPE_OPENID) || a.getScope().equalsIgnoreCase(SCOPE_PROFILE)) && !a.isDefaultScope() && a.getScopeApproval() == null;
        });
    }));
    verify(applicationService, atMost(1)).update(argThat(app -> {
        final boolean withScopeSettings = app.getSettings() != null && app.getSettings().getOauth() != null && app.getSettings().getOauth().getScopeSettings() != null;
        return withScopeSettings && app.getSettings().getOauth().getScopeSettings().stream().allMatch(a -> {
            return (a.getScope().equalsIgnoreCase(SCOPE_OPENID) && a.isDefaultScope() && a.getScopeApproval() != null && a.getScopeApproval() == 42) || (a.getScope().equalsIgnoreCase(SCOPE_PROFILE) && !a.isDefaultScope() && a.getScopeApproval() == null);
        });
    }));
    verify(systemTaskRepository, times(2)).updateIf(any(), any());
}
Also used : ApplicationOAuthSettings(io.gravitee.am.model.application.ApplicationOAuthSettings) InjectMocks(org.mockito.InjectMocks) ApplicationService(io.gravitee.am.service.ApplicationService) SystemTask(io.gravitee.am.model.SystemTask) Arrays(java.util.Arrays) ApplicationOAuthSettings(io.gravitee.am.model.application.ApplicationOAuthSettings) Mock(org.mockito.Mock) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) Set(java.util.Set) SystemTaskRepository(io.gravitee.am.repository.management.api.SystemTaskRepository) Test(org.junit.Test) Application(io.gravitee.am.model.Application) UUID(java.util.UUID) SystemTaskStatus(io.gravitee.am.model.SystemTaskStatus) ApplicationSettings(io.gravitee.am.model.application.ApplicationSettings) Single(io.reactivex.Single) Maps(io.gravitee.common.util.Maps) Mockito(org.mockito.Mockito) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) ApplicationSettings(io.gravitee.am.model.application.ApplicationSettings) SystemTask(io.gravitee.am.model.SystemTask) Application(io.gravitee.am.model.Application) Test(org.junit.Test)

Example 2 with ApplicationService

use of io.gravitee.am.service.ApplicationService in project gravitee-access-management by gravitee-io.

the class ApplicationScopeSettingsUpgraderTest method shouldUpgradeOngoing.

@Test
public void shouldUpgradeOngoing() {
    String id = UUID.randomUUID().toString();
    SystemTask ongoingTask = new SystemTask();
    ongoingTask.setOperationId(id);
    ongoingTask.setId(id);
    ongoingTask.setStatus(SystemTaskStatus.ONGOING.name());
    SystemTask finalizedTask = new SystemTask();
    finalizedTask.setOperationId(id);
    finalizedTask.setId(id);
    finalizedTask.setStatus(SystemTaskStatus.SUCCESS.name());
    // first call no task, then ongoing and finally the successful one
    when(systemTaskRepository.findById(any())).thenReturn(Maybe.empty(), Maybe.just(ongoingTask), Maybe.just(finalizedTask));
    when(systemTaskRepository.create(any())).thenReturn(Single.error(new Exception()));
    upgrader.upgrade();
    verify(systemTaskRepository, times(3)).findById(anyString());
    verify(applicationService, never()).findAll();
    verify(systemTaskRepository, never()).updateIf(argThat(t -> t.getStatus().equalsIgnoreCase(SystemTaskStatus.SUCCESS.name())), anyString());
}
Also used : InjectMocks(org.mockito.InjectMocks) ApplicationService(io.gravitee.am.service.ApplicationService) SystemTask(io.gravitee.am.model.SystemTask) Arrays(java.util.Arrays) ApplicationOAuthSettings(io.gravitee.am.model.application.ApplicationOAuthSettings) Mock(org.mockito.Mock) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) Set(java.util.Set) SystemTaskRepository(io.gravitee.am.repository.management.api.SystemTaskRepository) Test(org.junit.Test) Application(io.gravitee.am.model.Application) UUID(java.util.UUID) SystemTaskStatus(io.gravitee.am.model.SystemTaskStatus) ApplicationSettings(io.gravitee.am.model.application.ApplicationSettings) Single(io.reactivex.Single) Maps(io.gravitee.common.util.Maps) Mockito(org.mockito.Mockito) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) SystemTask(io.gravitee.am.model.SystemTask) Test(org.junit.Test)

Aggregations

Application (io.gravitee.am.model.Application)2 SystemTask (io.gravitee.am.model.SystemTask)2 SystemTaskStatus (io.gravitee.am.model.SystemTaskStatus)2 ApplicationOAuthSettings (io.gravitee.am.model.application.ApplicationOAuthSettings)2 ApplicationSettings (io.gravitee.am.model.application.ApplicationSettings)2 SystemTaskRepository (io.gravitee.am.repository.management.api.SystemTaskRepository)2 ApplicationService (io.gravitee.am.service.ApplicationService)2 Maps (io.gravitee.common.util.Maps)2 Maybe (io.reactivex.Maybe)2 Single (io.reactivex.Single)2 Arrays (java.util.Arrays)2 Set (java.util.Set)2 UUID (java.util.UUID)2 Test (org.junit.Test)2 RunWith (org.junit.runner.RunWith)2 InjectMocks (org.mockito.InjectMocks)2 Mock (org.mockito.Mock)2 Mockito (org.mockito.Mockito)2 MockitoJUnitRunner (org.mockito.junit.MockitoJUnitRunner)2