Search in sources :

Example 1 with AUTH_CONTEXT_KEY

use of io.scalecube.services.auth.Authenticator.AUTH_CONTEXT_KEY in project scalecube by scalecube.

the class ServiceMethodInvokerTest method testInvokeBidirectionalWhenReturnNull.

@Test
@DisplayName("invokeBidirectional should return empty response when service returns null")
void testInvokeBidirectionalWhenReturnNull() throws Exception {
    final String methodName = "returnNull3";
    final Class<? extends StubService> serviceClass = stubService.getClass();
    final Method method = serviceClass.getMethod(methodName, Flux.class);
    final MethodInfo methodInfo = new MethodInfo(serviceClass.getName(), methodName, method.getReturnType(), IS_RETURN_TYPE_SERVICE_MESSAGE, CommunicationMode.REQUEST_CHANNEL, method.getParameterCount(), Void.TYPE, IS_REQUEST_TYPE_SERVICE_MESSAGE, AUTH);
    serviceMethodInvoker = new ServiceMethodInvoker(method, stubService, methodInfo, DefaultErrorMapper.INSTANCE, dataDecoder, nullAuthenticator, nullPrincipalMapper);
    ServiceMessage message = ServiceMessage.builder().qualifier(qualifierPrefix + methodName).build();
    StepVerifier.create(Flux.deferContextual(context -> serviceMethodInvoker.invokeBidirectional(Flux.just(message))).contextWrite(context -> context.put(AUTH_CONTEXT_KEY, AUTH_DATA))).verifyComplete();
}
Also used : ArgumentMatchers(org.mockito.ArgumentMatchers) StepVerifier(reactor.test.StepVerifier) Mono(reactor.core.publisher.Mono) AUTH_CONTEXT_KEY(io.scalecube.services.auth.Authenticator.AUTH_CONTEXT_KEY) CommunicationMode(io.scalecube.services.CommunicationMode) Authenticator(io.scalecube.services.auth.Authenticator) DefaultErrorMapper(io.scalecube.services.exceptions.DefaultErrorMapper) DisplayName(org.junit.jupiter.api.DisplayName) PrincipalMapper(io.scalecube.services.auth.PrincipalMapper) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Flux(reactor.core.publisher.Flux) ServiceMessageDataDecoder(io.scalecube.services.transport.api.ServiceMessageDataDecoder) Map(java.util.Map) Assertions(org.junit.jupiter.api.Assertions) ServiceMessage(io.scalecube.services.api.ServiceMessage) Method(java.lang.reflect.Method) Collections(java.util.Collections) ServiceMessage(io.scalecube.services.api.ServiceMessage) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 2 with AUTH_CONTEXT_KEY

use of io.scalecube.services.auth.Authenticator.AUTH_CONTEXT_KEY in project scalecube by scalecube.

the class ServiceMethodInvokerTest method testInvokeManyWhenThrowException.

@Test
@DisplayName("invokeMany should return error response when service throws exception")
void testInvokeManyWhenThrowException() throws Exception {
    final String methodName = "throwException2";
    final Class<? extends StubService> serviceClass = stubService.getClass();
    final Method method = serviceClass.getMethod(methodName);
    final MethodInfo methodInfo = new MethodInfo(serviceClass.getName(), methodName, method.getReturnType(), IS_RETURN_TYPE_SERVICE_MESSAGE, CommunicationMode.REQUEST_STREAM, method.getParameterCount(), Void.TYPE, IS_REQUEST_TYPE_SERVICE_MESSAGE, AUTH);
    serviceMethodInvoker = new ServiceMethodInvoker(method, stubService, methodInfo, DefaultErrorMapper.INSTANCE, dataDecoder, nullAuthenticator, nullPrincipalMapper);
    ServiceMessage message = ServiceMessage.builder().qualifier(qualifierPrefix + methodName).build();
    final Flux<ServiceMessage> invokeOne = Flux.deferContextual(context -> serviceMethodInvoker.invokeMany(message)).contextWrite(context -> context.put(AUTH_CONTEXT_KEY, AUTH_DATA));
    StepVerifier.create(invokeOne).assertNext(serviceMessage -> Assertions.assertTrue(serviceMessage.isError())).verifyComplete();
}
Also used : ArgumentMatchers(org.mockito.ArgumentMatchers) StepVerifier(reactor.test.StepVerifier) Mono(reactor.core.publisher.Mono) AUTH_CONTEXT_KEY(io.scalecube.services.auth.Authenticator.AUTH_CONTEXT_KEY) CommunicationMode(io.scalecube.services.CommunicationMode) Authenticator(io.scalecube.services.auth.Authenticator) DefaultErrorMapper(io.scalecube.services.exceptions.DefaultErrorMapper) DisplayName(org.junit.jupiter.api.DisplayName) PrincipalMapper(io.scalecube.services.auth.PrincipalMapper) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Flux(reactor.core.publisher.Flux) ServiceMessageDataDecoder(io.scalecube.services.transport.api.ServiceMessageDataDecoder) Map(java.util.Map) Assertions(org.junit.jupiter.api.Assertions) ServiceMessage(io.scalecube.services.api.ServiceMessage) Method(java.lang.reflect.Method) Collections(java.util.Collections) ServiceMessage(io.scalecube.services.api.ServiceMessage) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 3 with AUTH_CONTEXT_KEY

use of io.scalecube.services.auth.Authenticator.AUTH_CONTEXT_KEY in project scalecube by scalecube.

the class ServiceMethodInvokerTest method testAuthMethodWhenThereIsContextAndNoAuthenticator.

@Test
@DisplayName("invocation of auth method should return empty response " + "if auth.context exists and no authenticator")
void testAuthMethodWhenThereIsContextAndNoAuthenticator() throws Exception {
    final String methodName = "helloAuthContext";
    final Class<? extends StubService> serviceClass = stubService.getClass();
    final Method method = serviceClass.getMethod(methodName);
    final MethodInfo methodInfo = new MethodInfo(serviceClass.getName(), methodName, method.getReturnType(), IS_RETURN_TYPE_SERVICE_MESSAGE, CommunicationMode.REQUEST_RESPONSE, method.getParameterCount(), Void.TYPE, IS_REQUEST_TYPE_SERVICE_MESSAGE, AUTH);
    serviceMethodInvoker = new ServiceMethodInvoker(method, stubService, methodInfo, DefaultErrorMapper.INSTANCE, dataDecoder, nullAuthenticator, principalMapper);
    ServiceMessage message = ServiceMessage.builder().qualifier(qualifierPrefix + methodName).build();
    StepVerifier.create(Mono.deferContextual(context -> serviceMethodInvoker.invokeOne(message)).contextWrite(context -> context.put(AUTH_CONTEXT_KEY, AUTH_DATA))).verifyComplete();
}
Also used : ArgumentMatchers(org.mockito.ArgumentMatchers) StepVerifier(reactor.test.StepVerifier) Mono(reactor.core.publisher.Mono) AUTH_CONTEXT_KEY(io.scalecube.services.auth.Authenticator.AUTH_CONTEXT_KEY) CommunicationMode(io.scalecube.services.CommunicationMode) Authenticator(io.scalecube.services.auth.Authenticator) DefaultErrorMapper(io.scalecube.services.exceptions.DefaultErrorMapper) DisplayName(org.junit.jupiter.api.DisplayName) PrincipalMapper(io.scalecube.services.auth.PrincipalMapper) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Flux(reactor.core.publisher.Flux) ServiceMessageDataDecoder(io.scalecube.services.transport.api.ServiceMessageDataDecoder) Map(java.util.Map) Assertions(org.junit.jupiter.api.Assertions) ServiceMessage(io.scalecube.services.api.ServiceMessage) Method(java.lang.reflect.Method) Collections(java.util.Collections) ServiceMessage(io.scalecube.services.api.ServiceMessage) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 4 with AUTH_CONTEXT_KEY

use of io.scalecube.services.auth.Authenticator.AUTH_CONTEXT_KEY in project scalecube by scalecube.

the class ServiceMethodInvokerTest method testInvokeManyWhenReturnNull.

@Test
@DisplayName("invokeMany should return empty response when service returns null")
void testInvokeManyWhenReturnNull() throws Exception {
    final String methodName = "returnNull2";
    final Class<? extends StubService> serviceClass = stubService.getClass();
    final Method method = serviceClass.getMethod(methodName);
    final MethodInfo methodInfo = new MethodInfo(serviceClass.getName(), methodName, method.getReturnType(), IS_RETURN_TYPE_SERVICE_MESSAGE, CommunicationMode.REQUEST_STREAM, method.getParameterCount(), Void.TYPE, IS_REQUEST_TYPE_SERVICE_MESSAGE, AUTH);
    serviceMethodInvoker = new ServiceMethodInvoker(method, stubService, methodInfo, DefaultErrorMapper.INSTANCE, dataDecoder, nullAuthenticator, nullPrincipalMapper);
    ServiceMessage message = ServiceMessage.builder().qualifier(qualifierPrefix + methodName).build();
    StepVerifier.create(Flux.deferContextual(context -> serviceMethodInvoker.invokeMany(message)).contextWrite(context -> context.put(AUTH_CONTEXT_KEY, AUTH_DATA))).verifyComplete();
}
Also used : ArgumentMatchers(org.mockito.ArgumentMatchers) StepVerifier(reactor.test.StepVerifier) Mono(reactor.core.publisher.Mono) AUTH_CONTEXT_KEY(io.scalecube.services.auth.Authenticator.AUTH_CONTEXT_KEY) CommunicationMode(io.scalecube.services.CommunicationMode) Authenticator(io.scalecube.services.auth.Authenticator) DefaultErrorMapper(io.scalecube.services.exceptions.DefaultErrorMapper) DisplayName(org.junit.jupiter.api.DisplayName) PrincipalMapper(io.scalecube.services.auth.PrincipalMapper) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Flux(reactor.core.publisher.Flux) ServiceMessageDataDecoder(io.scalecube.services.transport.api.ServiceMessageDataDecoder) Map(java.util.Map) Assertions(org.junit.jupiter.api.Assertions) ServiceMessage(io.scalecube.services.api.ServiceMessage) Method(java.lang.reflect.Method) Collections(java.util.Collections) ServiceMessage(io.scalecube.services.api.ServiceMessage) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 5 with AUTH_CONTEXT_KEY

use of io.scalecube.services.auth.Authenticator.AUTH_CONTEXT_KEY in project scalecube by scalecube.

the class ServiceMethodInvokerTest method testInvokeBidirectionalWhenThrowException.

@Test
@DisplayName("invokeBidirectional should return error response when service throws exception")
void testInvokeBidirectionalWhenThrowException() throws Exception {
    final String methodName = "throwException3";
    final Class<? extends StubService> serviceClass = stubService.getClass();
    final Method method = serviceClass.getMethod(methodName, Flux.class);
    final MethodInfo methodInfo = new MethodInfo(serviceClass.getName(), methodName, method.getReturnType(), IS_RETURN_TYPE_SERVICE_MESSAGE, CommunicationMode.REQUEST_CHANNEL, method.getParameterCount(), Void.TYPE, IS_REQUEST_TYPE_SERVICE_MESSAGE, AUTH);
    serviceMethodInvoker = new ServiceMethodInvoker(method, stubService, methodInfo, DefaultErrorMapper.INSTANCE, dataDecoder, nullAuthenticator, nullPrincipalMapper);
    ServiceMessage message = ServiceMessage.builder().qualifier(qualifierPrefix + methodName).build();
    // invokeOne
    final Flux<ServiceMessage> invokeOne = Flux.deferContextual(context -> serviceMethodInvoker.invokeBidirectional(Flux.just(message))).contextWrite(context -> context.put(AUTH_CONTEXT_KEY, AUTH_DATA));
    StepVerifier.create(invokeOne).assertNext(serviceMessage -> Assertions.assertTrue(serviceMessage.isError())).verifyComplete();
}
Also used : ArgumentMatchers(org.mockito.ArgumentMatchers) StepVerifier(reactor.test.StepVerifier) Mono(reactor.core.publisher.Mono) AUTH_CONTEXT_KEY(io.scalecube.services.auth.Authenticator.AUTH_CONTEXT_KEY) CommunicationMode(io.scalecube.services.CommunicationMode) Authenticator(io.scalecube.services.auth.Authenticator) DefaultErrorMapper(io.scalecube.services.exceptions.DefaultErrorMapper) DisplayName(org.junit.jupiter.api.DisplayName) PrincipalMapper(io.scalecube.services.auth.PrincipalMapper) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Flux(reactor.core.publisher.Flux) ServiceMessageDataDecoder(io.scalecube.services.transport.api.ServiceMessageDataDecoder) Map(java.util.Map) Assertions(org.junit.jupiter.api.Assertions) ServiceMessage(io.scalecube.services.api.ServiceMessage) Method(java.lang.reflect.Method) Collections(java.util.Collections) ServiceMessage(io.scalecube.services.api.ServiceMessage) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

CommunicationMode (io.scalecube.services.CommunicationMode)7 ServiceMessage (io.scalecube.services.api.ServiceMessage)7 Authenticator (io.scalecube.services.auth.Authenticator)7 AUTH_CONTEXT_KEY (io.scalecube.services.auth.Authenticator.AUTH_CONTEXT_KEY)7 PrincipalMapper (io.scalecube.services.auth.PrincipalMapper)7 DefaultErrorMapper (io.scalecube.services.exceptions.DefaultErrorMapper)7 ServiceMessageDataDecoder (io.scalecube.services.transport.api.ServiceMessageDataDecoder)7 Method (java.lang.reflect.Method)7 Collections (java.util.Collections)7 Map (java.util.Map)7 Assertions (org.junit.jupiter.api.Assertions)7 DisplayName (org.junit.jupiter.api.DisplayName)7 Test (org.junit.jupiter.api.Test)7 ArgumentMatchers (org.mockito.ArgumentMatchers)7 Mockito (org.mockito.Mockito)7 Flux (reactor.core.publisher.Flux)7 Mono (reactor.core.publisher.Mono)7 StepVerifier (reactor.test.StepVerifier)7