Search in sources :

Example 6 with Response

use of io.gravitee.gateway.api.Response in project gravitee-gateway by gravitee-io.

the class CheckSubscriptionPolicyTest method shouldReturnUnauthorized_onException.

@Test
public void shouldReturnUnauthorized_onException() throws PolicyException, TechnicalException {
    CheckSubscriptionPolicy policy = new CheckSubscriptionPolicy();
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    PolicyChain policyChain = mock(PolicyChain.class);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(executionContext.getAttribute(CheckSubscriptionPolicy.CONTEXT_ATTRIBUTE_CLIENT_ID)).thenReturn("my-client-id");
    SubscriptionRepository subscriptionRepository = mock(SubscriptionRepository.class);
    when(executionContext.getComponent(SubscriptionRepository.class)).thenReturn(subscriptionRepository);
    when(subscriptionRepository.search(any(SubscriptionCriteria.class))).thenThrow(TechnicalException.class);
    policy.onRequest(request, response, policyChain, executionContext);
    verify(policyChain, times(1)).failWith(argThat(statusCode(HttpStatusCode.UNAUTHORIZED_401)));
}
Also used : Response(io.gravitee.gateway.api.Response) PolicyChain(io.gravitee.policy.api.PolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) SubscriptionRepository(io.gravitee.repository.management.api.SubscriptionRepository) Request(io.gravitee.gateway.api.Request) SubscriptionCriteria(io.gravitee.repository.management.api.search.SubscriptionCriteria) Test(org.junit.Test)

Example 7 with Response

use of io.gravitee.gateway.api.Response in project gravitee-gateway by gravitee-io.

the class CheckSubscriptionPolicyTest method shouldContinue.

@Test
public void shouldContinue() throws PolicyException, TechnicalException {
    CheckSubscriptionPolicy policy = new CheckSubscriptionPolicy();
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    PolicyChain policyChain = mock(PolicyChain.class);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(executionContext.getAttribute(CheckSubscriptionPolicy.CONTEXT_ATTRIBUTE_CLIENT_ID)).thenReturn("my-client-id");
    SubscriptionRepository subscriptionRepository = mock(SubscriptionRepository.class);
    when(executionContext.getComponent(SubscriptionRepository.class)).thenReturn(subscriptionRepository);
    Subscription subscription = mock(Subscription.class);
    when(subscription.getClientId()).thenReturn("my-client-id");
    when(subscriptionRepository.search(any(SubscriptionCriteria.class))).thenReturn(Collections.singletonList(subscription));
    policy.onRequest(request, response, policyChain, executionContext);
    verify(policyChain, times(1)).doNext(request, response);
}
Also used : Response(io.gravitee.gateway.api.Response) PolicyChain(io.gravitee.policy.api.PolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) SubscriptionRepository(io.gravitee.repository.management.api.SubscriptionRepository) Request(io.gravitee.gateway.api.Request) SubscriptionCriteria(io.gravitee.repository.management.api.search.SubscriptionCriteria) Subscription(io.gravitee.repository.management.model.Subscription) Test(org.junit.Test)

Example 8 with Response

use of io.gravitee.gateway.api.Response in project gravitee-gateway by gravitee-io.

the class PolicyTest method onRequest_mockParameters.

@Test
public void onRequest_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 onRequestMethod = resolvePolicyMethod(DummyPolicy.class, OnRequest.class);
    when(policyDefinition.method(OnRequest.class)).thenReturn(onRequestMethod);
    Policy policy = PolicyImpl.target(policyInst).definition(policyDefinition).build();
    Request mockRequest = mock(Request.class);
    Response mockResponse = mock(Response.class);
    policy.onRequest(mockRequest, mockResponse);
    verify(policyInst, atLeastOnce()).onRequest(any(PolicyChain.class), eq(mockRequest), eq(mockResponse));
}
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)

Example 9 with Response

use of io.gravitee.gateway.api.Response in project gravitee-gateway by gravitee-io.

the class TransactionHandlerTest method shouldHaveTransactionId.

@Test
public void shouldHaveTransactionId() throws InterruptedException {
    final CountDownLatch lock = new CountDownLatch(1);
    when(request.id()).thenReturn(UUID.toString(UUID.random()));
    new TransactionHandler(request1 -> {
        assertNotNull(request1.transactionId());
        assertEquals(request1.transactionId(), request1.headers().getFirst(TransactionHandler.DEFAULT_TRANSACTIONAL_ID_HEADER));
        assertEquals(request1.transactionId(), request1.metrics().getTransactionId());
        assertEquals(request1.transactionId(), response.headers().getFirst(TransactionHandler.DEFAULT_TRANSACTIONAL_ID_HEADER));
        lock.countDown();
    }, response).handle(request);
    assertEquals(true, lock.await(10000, TimeUnit.MILLISECONDS));
}
Also used : HttpHeaders(io.gravitee.common.http.HttpHeaders) Mock(org.mockito.Mock) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Metrics(io.gravitee.reporter.api.http.Metrics) TimeUnit(java.util.concurrent.TimeUnit) MockitoAnnotations(org.mockito.MockitoAnnotations) CountDownLatch(java.util.concurrent.CountDownLatch) Response(io.gravitee.gateway.api.Response) UUID(io.gravitee.common.utils.UUID) Request(io.gravitee.gateway.api.Request) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 10 with Response

use of io.gravitee.gateway.api.Response in project gravitee-gateway by gravitee-io.

the class TransactionHandlerTest method shouldPropagateSameTransactionIdWithCustomHeader.

@Test
public void shouldPropagateSameTransactionIdWithCustomHeader() throws InterruptedException {
    final CountDownLatch lock = new CountDownLatch(1);
    String transactionId = UUID.toString(UUID.random());
    request.headers().set(CUSTOM_TRANSACTION_ID_HEADER, transactionId);
    new TransactionHandler(CUSTOM_TRANSACTION_ID_HEADER, request1 -> {
        assertNotNull(request1.transactionId());
        assertEquals(transactionId, request1.transactionId());
        assertEquals(transactionId, request1.headers().getFirst(CUSTOM_TRANSACTION_ID_HEADER));
        assertEquals(transactionId, request1.metrics().getTransactionId());
        assertEquals(request1.transactionId(), response.headers().getFirst(CUSTOM_TRANSACTION_ID_HEADER));
        lock.countDown();
    }, response).handle(request);
    assertEquals(true, lock.await(10000, TimeUnit.MILLISECONDS));
}
Also used : HttpHeaders(io.gravitee.common.http.HttpHeaders) Mock(org.mockito.Mock) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Metrics(io.gravitee.reporter.api.http.Metrics) TimeUnit(java.util.concurrent.TimeUnit) MockitoAnnotations(org.mockito.MockitoAnnotations) CountDownLatch(java.util.concurrent.CountDownLatch) Response(io.gravitee.gateway.api.Response) UUID(io.gravitee.common.utils.UUID) Request(io.gravitee.gateway.api.Request) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

Request (io.gravitee.gateway.api.Request)21 Response (io.gravitee.gateway.api.Response)21 Test (org.junit.Test)14 ExecutionContext (io.gravitee.gateway.api.ExecutionContext)13 PolicyChain (io.gravitee.policy.api.PolicyChain)11 HttpHeaders (io.gravitee.common.http.HttpHeaders)7 SubscriptionRepository (io.gravitee.repository.management.api.SubscriptionRepository)7 SubscriptionCriteria (io.gravitee.repository.management.api.search.SubscriptionCriteria)7 Before (org.junit.Before)6 Subscription (io.gravitee.repository.management.model.Subscription)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 UUID (io.gravitee.common.utils.UUID)4 Metrics (io.gravitee.reporter.api.http.Metrics)4 TimeUnit (java.util.concurrent.TimeUnit)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 Assert.assertNotNull (org.junit.Assert.assertNotNull)4 Mock (org.mockito.Mock)4 Mockito.when (org.mockito.Mockito.when)4 MockitoAnnotations (org.mockito.MockitoAnnotations)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2