Search in sources :

Example 6 with QueryResponse

use of org.forgerock.json.resource.QueryResponse in project OpenAM by OpenRock.

the class UmaPolicyServiceImplTest method shouldUpdateUmaPolicy.

@Test
public void shouldUpdateUmaPolicy() throws Exception {
    //Given
    Context context = createContext();
    JsonValue policy = createUmaPolicyJson("RESOURCE_SET_ID", "SCOPE_A", "SCOPE_C");
    policy.remove(new JsonPointer("/permissions/0/scopes/1"));
    List<ResourceResponse> updatedPolicies = new ArrayList<>();
    ResourceResponse updatedPolicy1 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectOneUpdatedPolicyJson());
    ResourceResponse updatedPolicy2 = newResourceResponse("ID_2", "REVISION_1", createBackendSubjectTwoPolicyJson());
    updatedPolicies.add(updatedPolicy1);
    updatedPolicies.add(updatedPolicy2);
    Promise<List<ResourceResponse>, ResourceException> updatePolicyPromise = newResultPromise(updatedPolicies);
    List<ResourceResponse> currentPolicies = new ArrayList<>();
    ResourceResponse currentPolicy1 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectOnePolicyJson());
    ResourceResponse currentPolicy2 = newResourceResponse("ID_2", "REVISION_1", createBackendSubjectTwoPolicyJson());
    currentPolicies.add(currentPolicy1);
    currentPolicies.add(currentPolicy2);
    Promise<Pair<QueryResponse, List<ResourceResponse>>, ResourceException> currentPolicyPromise = newResultPromise(Pair.of((QueryResponse) null, currentPolicies));
    setupQueries(currentPolicyPromise, updatedPolicy1, updatedPolicy2);
    given(policyResourceDelegate.updatePolicies(eq(context), Matchers.<Set<JsonValue>>anyObject())).willReturn(updatePolicyPromise);
    //When
    UmaPolicy umaPolicy = policyService.updatePolicy(context, "RESOURCE_SET_ID", policy).getOrThrowUninterruptibly();
    //Then
    InOrder inOrder = inOrder(resourceDelegationFilter, policyResourceDelegate);
    inOrder.verify(resourceDelegationFilter).beforeResourceSharedModification(any(UmaPolicy.class), any(UmaPolicy.class));
    inOrder.verify(policyResourceDelegate, times(2)).updatePolicies(any(Context.class), anySetOf(JsonValue.class));
    assertThat(umaPolicy.getId()).isEqualTo("RESOURCE_SET_ID");
    assertThat(umaPolicy.getRevision()).isNotNull();
    JsonValue expectedPolicyJson = createUmaPolicyJson("RESOURCE_SET_ID", "SCOPE_A", "SCOPE_C");
    expectedPolicyJson.remove(new JsonPointer("/permissions/0/scopes/1"));
    assertThat(umaPolicy.asJson().asMap()).isEqualTo(expectedPolicyJson.asMap());
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) InOrder(org.mockito.InOrder) JsonValue(org.forgerock.json.JsonValue) ArrayList(java.util.ArrayList) JsonPointer(org.forgerock.json.JsonPointer) ResourceResponse(org.forgerock.json.resource.ResourceResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) List(java.util.List) ArrayList(java.util.ArrayList) ResourceException(org.forgerock.json.resource.ResourceException) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) Pair(org.forgerock.util.Pair) Test(org.testng.annotations.Test)

Example 7 with QueryResponse

use of org.forgerock.json.resource.QueryResponse in project OpenAM by OpenRock.

the class UmaPolicyServiceImplTest method mockBackendQuery.

private void mockBackendQuery(Context context, JsonValue... policies) {
    QueryResponse queryResult = newQueryResponse();
    List<ResourceResponse> policyResources = new ArrayList<>();
    for (JsonValue policy : policies) {
        policyResources.add(newResourceResponse("ID_1", "REVISION_1", policy));
    }
    Promise<Pair<QueryResponse, List<ResourceResponse>>, ResourceException> backendQueryPromise = newResultPromise(Pair.of(queryResult, policyResources));
    given(policyResourceDelegate.queryPolicies(eq(context), Matchers.<QueryRequest>anyObject())).willReturn(backendQueryPromise);
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue) ResourceException(org.forgerock.json.resource.ResourceException) Pair(org.forgerock.util.Pair)

Example 8 with QueryResponse

use of org.forgerock.json.resource.QueryResponse in project OpenAM by OpenRock.

the class UmaPolicyServiceImplTest method shouldDeleteUmaPolicy.

@Test
@SuppressWarnings("unchecked")
public void shouldDeleteUmaPolicy() throws Exception {
    //Given
    Context context = createContext();
    List<ResourceResponse> readPolicies = new ArrayList<ResourceResponse>();
    ResourceResponse readPolicy1 = newResourceResponse("ID_1", "REVISION_1", createBackendSubjectOnePolicyJson());
    ResourceResponse readPolicy2 = newResourceResponse("ID_2", "REVISION_2", createBackendSubjectTwoPolicyJson());
    readPolicies.add(readPolicy1);
    readPolicies.add(readPolicy2);
    Promise<Pair<QueryResponse, List<ResourceResponse>>, ResourceException> currentPolicyPromise = newResultPromise(Pair.of((QueryResponse) null, readPolicies));
    setupQueries(currentPolicyPromise);
    Promise<List<ResourceResponse>, ResourceException> deletePoliciesPromise = newResultPromise(readPolicies);
    given(policyResourceDelegate.deletePolicies(eq(context), anyListOf(String.class))).willReturn(deletePoliciesPromise);
    //When
    policyService.deletePolicy(context, "RESOURCE_SET_ID").getOrThrowUninterruptibly();
    //Then
    InOrder inOrder = inOrder(resourceDelegationFilter, policyResourceDelegate);
    inOrder.verify(resourceDelegationFilter).onResourceSharedDeletion(any(UmaPolicy.class));
    inOrder.verify(policyResourceDelegate).deletePolicies(eq(context), anyListOf(String.class));
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) InOrder(org.mockito.InOrder) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) ResourceResponse(org.forgerock.json.resource.ResourceResponse) QueryResponse(org.forgerock.json.resource.QueryResponse) ResourceException(org.forgerock.json.resource.ResourceException) List(java.util.List) ArrayList(java.util.ArrayList) UmaPolicy(org.forgerock.openam.uma.UmaPolicy) Pair(org.forgerock.util.Pair) Test(org.testng.annotations.Test)

Example 9 with QueryResponse

use of org.forgerock.json.resource.QueryResponse in project OpenAM by OpenRock.

the class UmaPolicyServiceImplTest method shouldQueryUmaPoliciesByUnknownResourceServer.

@Test
@SuppressWarnings("unchecked")
public void shouldQueryUmaPoliciesByUnknownResourceServer() throws Exception {
    //Given
    Context context = createContext();
    QueryRequest request = Requests.newQueryRequest("").setQueryFilter(QueryFilter.equalTo(new JsonPointer("resourceServer"), "OTHER_CLIENT_ID"));
    mockBackendQuery(context);
    //When
    Pair<QueryResponse, Collection<UmaPolicy>> queryResult = policyService.queryPolicies(context, request).getOrThrowUninterruptibly();
    //Then
    assertThat(queryResult.getSecond()).hasSize(0);
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) QueryResponse(org.forgerock.json.resource.QueryResponse) Collection(java.util.Collection) JsonPointer(org.forgerock.json.JsonPointer) Test(org.testng.annotations.Test)

Example 10 with QueryResponse

use of org.forgerock.json.resource.QueryResponse in project OpenAM by OpenRock.

the class UmaPolicyServiceImplTest method shouldQueryUmaPoliciesByUnknownSubjectOrResourceServer.

@Test
@SuppressWarnings("unchecked")
public void shouldQueryUmaPoliciesByUnknownSubjectOrResourceServer() throws Exception {
    //Given
    Context context = createContext();
    QueryRequest request = Requests.newQueryRequest("").setQueryFilter(QueryFilter.or(QueryFilter.equalTo(new JsonPointer("permissions/subject"), "SUBJECT_OTHER"), QueryFilter.equalTo(new JsonPointer("resourceServer"), "CLIENT_ID")));
    mockBackendQuery(context, createBackendSubjectOnePolicyJson(), createBackendSubjectTwoPolicyJson());
    //When
    Pair<QueryResponse, Collection<UmaPolicy>> queryResult = policyService.queryPolicies(context, request).getOrThrowUninterruptibly();
    //Then
    assertThat(queryResult.getSecond()).hasSize(1);
}
Also used : ClientContext(org.forgerock.services.context.ClientContext) RealmContext(org.forgerock.openam.rest.RealmContext) SubjectContext(org.forgerock.openam.rest.resource.SubjectContext) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Context(org.forgerock.services.context.Context) QueryRequest(org.forgerock.json.resource.QueryRequest) QueryResponse(org.forgerock.json.resource.QueryResponse) Collection(java.util.Collection) JsonPointer(org.forgerock.json.JsonPointer) Test(org.testng.annotations.Test)

Aggregations

QueryResponse (org.forgerock.json.resource.QueryResponse)57 Test (org.testng.annotations.Test)49 ResourceException (org.forgerock.json.resource.ResourceException)43 QueryRequest (org.forgerock.json.resource.QueryRequest)42 Context (org.forgerock.services.context.Context)42 RealmContext (org.forgerock.openam.rest.RealmContext)37 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)26 ClientContext (org.forgerock.services.context.ClientContext)25 ResourceResponse (org.forgerock.json.resource.ResourceResponse)24 Collection (java.util.Collection)23 QueryResourceHandler (org.forgerock.json.resource.QueryResourceHandler)20 JsonPointer (org.forgerock.json.JsonPointer)19 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)19 Pair (org.forgerock.util.Pair)19 UmaPolicy (org.forgerock.openam.uma.UmaPolicy)16 HashSet (java.util.HashSet)14 JsonValue (org.forgerock.json.JsonValue)13 ArrayList (java.util.ArrayList)12 Subject (javax.security.auth.Subject)12 Responses.newQueryResponse (org.forgerock.json.resource.Responses.newQueryResponse)12