Search in sources :

Example 1 with OnResponse

use of io.gravitee.policy.api.annotations.OnResponse in project gravitee-gateway by gravitee-io.

the class PolicyTest method onResponse.

@Test
public void onResponse() throws Exception {
    PolicyMetadata policyDefinition = mock(PolicyMetadata.class);
    when(policyDefinition.policy()).then((Answer<Class>) invocationOnMock -> DummyPolicy.class);
    DummyPolicy policyInst = Mockito.spy((DummyPolicy) policyFactory.create(policyDefinition, null));
    Method onResponseMethod = resolvePolicyMethod(DummyPolicy.class, OnResponse.class);
    when(policyDefinition.method(OnResponse.class)).thenReturn(onResponseMethod);
    Policy policy = PolicyImpl.target(policyInst).definition(policyDefinition).build();
    policy.onResponse();
    verify(policyInst, never()).onRequest(any(), any(), any());
    verify(policyInst, atLeastOnce()).onResponse(any(), any(), any());
}
Also used : PolicyImpl(io.gravitee.gateway.policy.impl.PolicyImpl) OnResponse(io.gravitee.policy.api.annotations.OnResponse) OnRequest(io.gravitee.policy.api.annotations.OnRequest) ReflectionUtils.withAnnotation(org.reflections.ReflectionUtils.withAnnotation) PolicyFactoryImpl(io.gravitee.gateway.policy.impl.PolicyFactoryImpl) Set(java.util.Set) Test(org.junit.Test) ReflectionUtils.withModifier(org.reflections.ReflectionUtils.withModifier) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) Answer(org.mockito.stubbing.Answer) Response(io.gravitee.gateway.api.Response) Request(io.gravitee.gateway.api.Request) Modifier(java.lang.reflect.Modifier) PolicyChain(io.gravitee.policy.api.PolicyChain) Annotation(java.lang.annotation.Annotation) ReflectionUtils(org.reflections.ReflectionUtils) Method(java.lang.reflect.Method) Before(org.junit.Before) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 2 with OnResponse

use of io.gravitee.policy.api.annotations.OnResponse in project gravitee-gateway by gravitee-io.

the class PolicyTest method onResponse_emptyParameters.

@Test
public void onResponse_emptyParameters() throws Exception {
    PolicyMetadata policyDefinition = mock(PolicyMetadata.class);
    when(policyDefinition.policy()).then((Answer<Class>) invocationOnMock -> DummyPolicy.class);
    DummyPolicy policyInst = Mockito.spy((DummyPolicy) policyFactory.create(policyDefinition, null));
    Method onResponseMethod = resolvePolicyMethod(DummyPolicy.class, OnResponse.class);
    when(policyDefinition.method(OnResponse.class)).thenReturn(onResponseMethod);
    Policy policy = PolicyImpl.target(policyInst).definition(policyDefinition).build();
    policy.onResponse();
    verify(policyInst, atLeastOnce()).onResponse(any(), any(), any());
}
Also used : PolicyImpl(io.gravitee.gateway.policy.impl.PolicyImpl) OnResponse(io.gravitee.policy.api.annotations.OnResponse) OnRequest(io.gravitee.policy.api.annotations.OnRequest) ReflectionUtils.withAnnotation(org.reflections.ReflectionUtils.withAnnotation) PolicyFactoryImpl(io.gravitee.gateway.policy.impl.PolicyFactoryImpl) Set(java.util.Set) Test(org.junit.Test) ReflectionUtils.withModifier(org.reflections.ReflectionUtils.withModifier) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) Answer(org.mockito.stubbing.Answer) Response(io.gravitee.gateway.api.Response) Request(io.gravitee.gateway.api.Request) Modifier(java.lang.reflect.Modifier) PolicyChain(io.gravitee.policy.api.PolicyChain) Annotation(java.lang.annotation.Annotation) ReflectionUtils(org.reflections.ReflectionUtils) Method(java.lang.reflect.Method) Before(org.junit.Before) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 3 with OnResponse

use of io.gravitee.policy.api.annotations.OnResponse in project gravitee-gateway by gravitee-io.

the class PolicyTest method onRequest.

@Test
public void onRequest() throws Exception {
    PolicyMetadata policyDefinition = mock(PolicyMetadata.class);
    when(policyDefinition.policy()).then((Answer<Class>) invocationOnMock -> DummyPolicy.class);
    DummyPolicy policyInst = Mockito.spy((DummyPolicy) policyFactory.create(policyDefinition, null));
    Method onRequestMethod = resolvePolicyMethod(DummyPolicy.class, OnRequest.class);
    when(policyDefinition.method(OnRequest.class)).thenReturn(onRequestMethod);
    Policy policy = PolicyImpl.target(policyInst).definition(policyDefinition).build();
    policy.onRequest();
    verify(policyInst, atLeastOnce()).onRequest(any(), any(), any());
    verify(policyInst, never()).onResponse(any(), any(), any());
}
Also used : PolicyImpl(io.gravitee.gateway.policy.impl.PolicyImpl) OnResponse(io.gravitee.policy.api.annotations.OnResponse) OnRequest(io.gravitee.policy.api.annotations.OnRequest) ReflectionUtils.withAnnotation(org.reflections.ReflectionUtils.withAnnotation) PolicyFactoryImpl(io.gravitee.gateway.policy.impl.PolicyFactoryImpl) Set(java.util.Set) Test(org.junit.Test) ReflectionUtils.withModifier(org.reflections.ReflectionUtils.withModifier) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) Answer(org.mockito.stubbing.Answer) Response(io.gravitee.gateway.api.Response) Request(io.gravitee.gateway.api.Request) Modifier(java.lang.reflect.Modifier) PolicyChain(io.gravitee.policy.api.PolicyChain) Annotation(java.lang.annotation.Annotation) ReflectionUtils(org.reflections.ReflectionUtils) Method(java.lang.reflect.Method) Before(org.junit.Before) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 4 with OnResponse

use of io.gravitee.policy.api.annotations.OnResponse in project gravitee-gateway by gravitee-io.

the class PolicyTest method onResponse_mockParameters.

@Test
public void onResponse_mockParameters() throws Exception {
    PolicyMetadata policyDefinition = mock(PolicyMetadata.class);
    when(policyDefinition.policy()).then((Answer<Class>) invocationOnMock -> DummyPolicy.class);
    DummyPolicy policyInst = Mockito.spy((DummyPolicy) policyFactory.create(policyDefinition, null));
    Method onResponseMethod = resolvePolicyMethod(DummyPolicy.class, OnResponse.class);
    when(policyDefinition.method(OnResponse.class)).thenReturn(onResponseMethod);
    Policy policy = PolicyImpl.target(policyInst).definition(policyDefinition).build();
    Request mockRequest = mock(Request.class);
    Response mockResponse = mock(Response.class);
    policy.onResponse(mockRequest, mockResponse);
    verify(policyInst, atLeastOnce()).onResponse(eq(mockRequest), eq(mockResponse), any(PolicyChain.class));
}
Also used : PolicyImpl(io.gravitee.gateway.policy.impl.PolicyImpl) OnResponse(io.gravitee.policy.api.annotations.OnResponse) OnRequest(io.gravitee.policy.api.annotations.OnRequest) ReflectionUtils.withAnnotation(org.reflections.ReflectionUtils.withAnnotation) PolicyFactoryImpl(io.gravitee.gateway.policy.impl.PolicyFactoryImpl) Set(java.util.Set) Test(org.junit.Test) ReflectionUtils.withModifier(org.reflections.ReflectionUtils.withModifier) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) Answer(org.mockito.stubbing.Answer) Response(io.gravitee.gateway.api.Response) Request(io.gravitee.gateway.api.Request) Modifier(java.lang.reflect.Modifier) PolicyChain(io.gravitee.policy.api.PolicyChain) Annotation(java.lang.annotation.Annotation) ReflectionUtils(org.reflections.ReflectionUtils) Method(java.lang.reflect.Method) Before(org.junit.Before) OnResponse(io.gravitee.policy.api.annotations.OnResponse) Response(io.gravitee.gateway.api.Response) PolicyChain(io.gravitee.policy.api.PolicyChain) OnRequest(io.gravitee.policy.api.annotations.OnRequest) Request(io.gravitee.gateway.api.Request) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

Request (io.gravitee.gateway.api.Request)4 Response (io.gravitee.gateway.api.Response)4 PolicyFactoryImpl (io.gravitee.gateway.policy.impl.PolicyFactoryImpl)4 PolicyImpl (io.gravitee.gateway.policy.impl.PolicyImpl)4 PolicyChain (io.gravitee.policy.api.PolicyChain)4 OnRequest (io.gravitee.policy.api.annotations.OnRequest)4 OnResponse (io.gravitee.policy.api.annotations.OnResponse)4 Annotation (java.lang.annotation.Annotation)4 Method (java.lang.reflect.Method)4 Modifier (java.lang.reflect.Modifier)4 Set (java.util.Set)4 Before (org.junit.Before)4 Test (org.junit.Test)4 Matchers.any (org.mockito.Matchers.any)4 Mockito (org.mockito.Mockito)4 Answer (org.mockito.stubbing.Answer)4 ReflectionUtils (org.reflections.ReflectionUtils)4 ReflectionUtils.withAnnotation (org.reflections.ReflectionUtils.withAnnotation)4 ReflectionUtils.withModifier (org.reflections.ReflectionUtils.withModifier)4