Search in sources :

Example 11 with ServiceMessage

use of io.scalecube.services.api.ServiceMessage in project scalecube by scalecube.

the class ServiceLocalTest method test_local_bidi_greeting_message_expect_NotAuthorized.

@Test
public void test_local_bidi_greeting_message_expect_NotAuthorized() {
    // get a proxy to the service api.
    GreetingService service = createProxy(microservices);
    Sinks.Many<GreetingRequest> requests = Sinks.many().multicast().directBestEffort();
    // call the service.
    Flux<GreetingResponse> responses = service.bidiGreetingNotAuthorizedMessage(requests.asFlux().onBackpressureBuffer().map(request -> ServiceMessage.builder().data(request).build())).map(ServiceMessage::data);
    StepVerifier.create(responses).then(() -> requests.emitNext(new GreetingRequest("joe-1"), FAIL_FAST)).expectErrorMessage("Not authorized").verify(Duration.ofSeconds(3));
}
Also used : ServiceMessage(io.scalecube.services.api.ServiceMessage) Sinks(reactor.core.publisher.Sinks) GreetingRequest(io.scalecube.services.sut.GreetingRequest) GreetingResponse(io.scalecube.services.sut.GreetingResponse) GreetingService(io.scalecube.services.sut.GreetingService) Test(org.junit.jupiter.api.Test)

Example 12 with ServiceMessage

use of io.scalecube.services.api.ServiceMessage in project scalecube by scalecube.

the class ServiceRemoteTest method test_remote_greeting_message.

@Test
void test_remote_greeting_message() {
    GreetingService service = createProxy();
    ServiceMessage request = ServiceMessage.builder().data(new GreetingRequest("joe")).build();
    // using proxy
    StepVerifier.create(service.greetingMessage(request)).assertNext(message -> {
        assertEquals(GreetingResponse.class, message.data().getClass());
        GreetingResponse resp = message.data();
        assertEquals("1", resp.sender());
        assertEquals("hello to: joe", resp.getResult());
    }).expectComplete().verify(TIMEOUT);
    StepVerifier.create(service.greetingMessage2(request)).assertNext(resp -> {
        assertEquals("1", resp.sender());
        assertEquals("hello to: joe", resp.getResult());
    }).expectComplete().verify(TIMEOUT);
    // using serviceCall directly
    ServiceCall serviceCall = gateway.call();
    StepVerifier.create(serviceCall.requestOne(ServiceMessage.from(request).qualifier("v1/greetings/greetingMessage").build(), GreetingResponse.class)).assertNext(message -> {
        assertEquals(GreetingResponse.class, message.data().getClass());
        GreetingResponse resp = message.data();
        assertEquals("1", resp.sender());
        assertEquals("hello to: joe", resp.getResult());
    }).expectComplete().verify(TIMEOUT);
    StepVerifier.create(serviceCall.requestOne(ServiceMessage.from(request).qualifier("v1/greetings/greetingMessage2").build(), GreetingResponse.class)).assertNext(message -> {
        assertEquals(GreetingResponse.class, message.data().getClass());
        GreetingResponse resp = message.data();
        assertEquals("1", resp.sender());
        assertEquals("hello to: joe", resp.getResult());
    }).expectComplete().verify(TIMEOUT);
}
Also used : ServiceMessage(io.scalecube.services.api.ServiceMessage) GreetingRequest(io.scalecube.services.sut.GreetingRequest) EmptyGreetingRequest(io.scalecube.services.sut.EmptyGreetingRequest) GreetingResponse(io.scalecube.services.sut.GreetingResponse) EmptyGreetingResponse(io.scalecube.services.sut.EmptyGreetingResponse) GreetingService(io.scalecube.services.sut.GreetingService) Test(org.junit.jupiter.api.Test)

Example 13 with ServiceMessage

use of io.scalecube.services.api.ServiceMessage in project scalecube by scalecube.

the class Example2 method main.

/**
 * Start the example.
 *
 * @param args ignored
 */
public static void main(String[] args) {
    // ScaleCube Node node with no members
    Microservices seed = Microservices.builder().discovery("seed", serviceEndpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(serviceEndpoint))).transport(RSocketServiceTransport::new).startAwait();
    // Construct a ScaleCube node which joins the cluster hosting the Greeting Service
    final Address seedAddress = seed.discovery().address();
    Microservices ms = Microservices.builder().discovery(endpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(endpoint)).membership(cfg -> cfg.seedMembers(seedAddress))).transport(RSocketServiceTransport::new).services(new GreetingServiceImpl()).startAwait();
    // Create a proxy to the seed service node
    ServiceCall service = seed.call();
    // Create a ServiceMessage request with service qualifier and data
    ServiceMessage request = ServiceMessage.builder().qualifier(SERVICE_QUALIFIER).data("joe").build();
    // Execute the Greeting Service to emit a single Greeting response
    Publisher<ServiceMessage> publisher = service.requestOne(request, Greeting.class);
    // Convert the Publisher using the Mono API which ensures it will emit 0 or 1 item.
    Mono.from(publisher).subscribe(consumer -> {
        // handle service response
        Greeting greeting = consumer.data();
        System.out.println(greeting.message());
    });
    Mono.whenDelayError(seed.shutdown(), ms.shutdown()).block();
}
Also used : RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) GreetingServiceImpl(io.scalecube.services.examples.helloworld.service.GreetingServiceImpl) Greeting(io.scalecube.services.examples.helloworld.service.api.Greeting) Microservices(io.scalecube.services.Microservices) ServiceCall(io.scalecube.services.ServiceCall) Publisher(org.reactivestreams.Publisher) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) ServiceMessage(io.scalecube.services.api.ServiceMessage) Mono(reactor.core.publisher.Mono) Address(io.scalecube.net.Address) Greeting(io.scalecube.services.examples.helloworld.service.api.Greeting) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) ServiceMessage(io.scalecube.services.api.ServiceMessage) ServiceCall(io.scalecube.services.ServiceCall) Address(io.scalecube.net.Address) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) GreetingServiceImpl(io.scalecube.services.examples.helloworld.service.GreetingServiceImpl) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) Microservices(io.scalecube.services.Microservices)

Example 14 with ServiceMessage

use of io.scalecube.services.api.ServiceMessage in project scalecube by scalecube.

the class GreetingServiceImpl method emptyGreetingMessage.

@Override
public Mono<ServiceMessage> emptyGreetingMessage(ServiceMessage request) {
    EmptyGreetingRequest greetingRequest = request.data();
    ServiceMessage response = ServiceMessage.from(request).data(new EmptyGreetingResponse()).build();
    return Mono.just(response);
}
Also used : ServiceMessage(io.scalecube.services.api.ServiceMessage)

Example 15 with ServiceMessage

use of io.scalecube.services.api.ServiceMessage in project scalecube by scalecube.

the class CompositeProfileAuthExample method main.

/**
 * Main program.
 *
 * @param args arguments
 */
public static void main(String[] args) {
    Microservices service = Microservices.builder().discovery(serviceEndpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(serviceEndpoint))).transport(() -> new RSocketServiceTransport().authenticator(authenticator())).services(call -> Collections.singletonList(ServiceInfo.fromServiceInstance(new SecuredServiceByCompositeProfileImpl()).authenticator(compositeAuthenticator()).build())).startAwait();
    Microservices caller = Microservices.builder().discovery(endpoint -> discovery(service, endpoint)).transport(() -> new RSocketServiceTransport().credentialsSupplier(credentialsSupplier())).startAwait();
    ServiceMessage response = caller.call().requestOne(ServiceMessage.builder().qualifier("securedServiceByCompositeProfile/hello").header("userProfile.name", "SEGA").header("userProfile.role", "ADMIN").data("hello world").build(), String.class).block(Duration.ofSeconds(3));
    System.err.println("### Received 'caller' response: " + response.data());
}
Also used : ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) ServiceInfo(io.scalecube.services.ServiceInfo) Mono(reactor.core.publisher.Mono) Authenticator(io.scalecube.services.auth.Authenticator) ServiceEndpoint(io.scalecube.services.ServiceEndpoint) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) Microservices(io.scalecube.services.Microservices) CredentialsSupplier(io.scalecube.services.auth.CredentialsSupplier) Duration(java.time.Duration) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) ServiceMessage(io.scalecube.services.api.ServiceMessage) Collections(java.util.Collections) UnauthorizedException(io.scalecube.services.exceptions.UnauthorizedException) MonoAuthUtil.deferWithPrincipal(io.scalecube.services.auth.MonoAuthUtil.deferWithPrincipal) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) ServiceMessage(io.scalecube.services.api.ServiceMessage) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) Microservices(io.scalecube.services.Microservices)

Aggregations

ServiceMessage (io.scalecube.services.api.ServiceMessage)37 Test (org.junit.jupiter.api.Test)31 GreetingResponse (io.scalecube.services.sut.GreetingResponse)12 Mono (reactor.core.publisher.Mono)12 Map (java.util.Map)10 Flux (reactor.core.publisher.Flux)10 StepVerifier (reactor.test.StepVerifier)10 Authenticator (io.scalecube.services.auth.Authenticator)9 GreetingRequest (io.scalecube.services.sut.GreetingRequest)9 Method (java.lang.reflect.Method)9 Collections (java.util.Collections)9 DisplayName (org.junit.jupiter.api.DisplayName)9 CommunicationMode (io.scalecube.services.CommunicationMode)8 AUTH_CONTEXT_KEY (io.scalecube.services.auth.Authenticator.AUTH_CONTEXT_KEY)8 PrincipalMapper (io.scalecube.services.auth.PrincipalMapper)8 DefaultErrorMapper (io.scalecube.services.exceptions.DefaultErrorMapper)8 GreetingService (io.scalecube.services.sut.GreetingService)8 ServiceMessageDataDecoder (io.scalecube.services.transport.api.ServiceMessageDataDecoder)8 Assertions (org.junit.jupiter.api.Assertions)8 ArgumentMatchers (org.mockito.ArgumentMatchers)8