Search in sources :

Example 1 with NewResource

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);
    });
}
Also used : Resource(io.gravitee.am.model.uma.Resource) NewResource(io.gravitee.am.service.model.NewResource) AccessPolicy(io.gravitee.am.model.uma.policy.AccessPolicy)

Example 2 with NewResource

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()));
}
Also used : NewResource(io.gravitee.am.service.model.NewResource) Resource(io.gravitee.am.model.uma.Resource) NewResource(io.gravitee.am.service.model.NewResource) JsonObject(io.vertx.core.json.JsonObject) AccessPolicy(io.gravitee.am.model.uma.policy.AccessPolicy) Test(org.junit.Test)

Example 3 with NewResource

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);
}
Also used : NewResource(io.gravitee.am.service.model.NewResource) Resource(io.gravitee.am.model.uma.Resource) NewResource(io.gravitee.am.service.model.NewResource) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 4 with NewResource

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());
}
Also used : NewResource(io.gravitee.am.service.model.NewResource) Resource(io.gravitee.am.model.uma.Resource) NewResource(io.gravitee.am.service.model.NewResource) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 5 with NewResource

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());
}
Also used : NewResource(io.gravitee.am.service.model.NewResource) Resource(io.gravitee.am.model.uma.Resource) NewResource(io.gravitee.am.service.model.NewResource) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Aggregations

NewResource (io.gravitee.am.service.model.NewResource)7 Resource (io.gravitee.am.model.uma.Resource)6 Test (org.junit.Test)6 JsonObject (io.vertx.core.json.JsonObject)5 AccessPolicy (io.gravitee.am.model.uma.policy.AccessPolicy)2 TestObserver (io.reactivex.observers.TestObserver)1