use of io.gravitee.am.service.model.NewResource in project gravitee-access-management by gravitee-io.
the class ResourceServiceImpl method create.
@Override
public Single<Resource> create(NewResource newResource, String domain, String client, String userId) {
LOGGER.debug("Creating resource for resource owner {} and client {}", userId, client);
Resource toCreate = new Resource();
toCreate.setResourceScopes(newResource.getResourceScopes()).setDescription(newResource.getDescription()).setIconUri(newResource.getIconUri()).setName(newResource.getName()).setType(newResource.getType()).setDomain(domain).setClientId(client).setUserId(userId).setCreatedAt(new Date()).setUpdatedAt(toCreate.getCreatedAt());
return this.validateScopes(toCreate).flatMap(this::validateIconUri).flatMap(repository::create).flatMap(r -> {
AccessPolicy accessPolicy = new AccessPolicy();
accessPolicy.setName("Deny all");
accessPolicy.setDescription("Default deny access policy. Created by Gravitee.io.");
accessPolicy.setType(AccessPolicyType.GROOVY);
accessPolicy.setCondition("{\"onRequestScript\":\"import io.gravitee.policy.groovy.PolicyResult.State\\nresult.state = State.FAILURE;\"}");
accessPolicy.setEnabled(true);
accessPolicy.setDomain(domain);
accessPolicy.setResource(r.getId());
return accessPolicyRepository.create(accessPolicy).map(__ -> r);
});
}
use of io.gravitee.am.service.model.NewResource in project gravitee-access-management by gravitee-io.
the class ResourceServiceTest method create_success.
@Test
public void create_success() {
NewResource newResource = new JsonObject("{\"resource_scopes\":[\"scope\"]}").mapTo(NewResource.class);
when(repository.create(any())).thenReturn(Single.just(new Resource()));
when(accessPolicyRepository.create(any())).thenReturn(Single.just(new AccessPolicy()));
TestObserver<Resource> testObserver = service.create(newResource, DOMAIN_ID, CLIENT_ID, USER_ID).test();
testObserver.assertComplete().assertNoErrors();
ArgumentCaptor<Resource> rsCaptor = ArgumentCaptor.forClass(Resource.class);
verify(repository, times(1)).create(rsCaptor.capture());
verify(accessPolicyRepository, times(1)).create(any());
Assert.assertTrue(this.assertResourceValues(rsCaptor.getValue()));
}
use of io.gravitee.am.service.model.NewResource in project gravitee-access-management by gravitee-io.
the class ResourceServiceTest method update_existingResource.
@Test
public void update_existingResource() {
NewResource newResource = new JsonObject("{\"resource_scopes\":[\"scope\"],\"icon_uri\":\"https://gravitee.io/icon\"}").mapTo(NewResource.class);
Resource exitingRS = new Resource().setId(RESOURCE_ID).setDomain(DOMAIN_ID);
when(repository.findByDomainAndClientAndUserAndResource(DOMAIN_ID, CLIENT_ID, USER_ID, RESOURCE_ID)).thenReturn(Maybe.just(exitingRS));
when(repository.update(exitingRS)).thenReturn(Single.just(exitingRS));
TestObserver<Resource> testObserver = service.update(newResource, DOMAIN_ID, CLIENT_ID, USER_ID, RESOURCE_ID).test();
testObserver.assertComplete().assertNoErrors().assertValue(this::assertResourceValues);
}
use of io.gravitee.am.service.model.NewResource in project gravitee-access-management by gravitee-io.
the class ResourceServiceTest method update_scopeMissing.
@Test
public void update_scopeMissing() {
NewResource newResource = new JsonObject("{\"resource_scopes\":[]}").mapTo(NewResource.class);
Resource exitingRS = new Resource().setId(RESOURCE_ID).setDomain(DOMAIN_ID);
when(repository.findByDomainAndClientAndUserAndResource(DOMAIN_ID, CLIENT_ID, USER_ID, RESOURCE_ID)).thenReturn(Maybe.just(exitingRS));
TestObserver<Resource> testObserver = service.update(newResource, DOMAIN_ID, CLIENT_ID, USER_ID, RESOURCE_ID).test();
testObserver.assertError(MissingScopeException.class);
verify(repository, times(0)).update(any());
}
use of io.gravitee.am.service.model.NewResource in project gravitee-access-management by gravitee-io.
the class ResourceServiceTest method update_malformedIconUri.
@Test
public void update_malformedIconUri() {
NewResource newResource = new JsonObject("{\"resource_scopes\":[\"scope\"],\"icon_uri\":\"badIconUriFormat\"}").mapTo(NewResource.class);
Resource exitingRS = new Resource().setId(RESOURCE_ID).setDomain(DOMAIN_ID);
when(repository.findByDomainAndClientAndUserAndResource(DOMAIN_ID, CLIENT_ID, USER_ID, RESOURCE_ID)).thenReturn(Maybe.just(exitingRS));
TestObserver<Resource> testObserver = service.update(newResource, DOMAIN_ID, CLIENT_ID, USER_ID, RESOURCE_ID).test();
testObserver.assertError(MalformedIconUriException.class);
verify(repository, times(0)).update(any());
}
Aggregations