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());
}
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());
}
Aggregations