io.vertx.serviceproxy.ServiceBinder Java Examples

The following examples show how to use io.vertx.serviceproxy.ServiceBinder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RouteToEBServiceHandlerTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void authorizedUserTest(Vertx vertx, VertxTestContext testContext) {
  Checkpoint checkpoint = testContext.checkpoint();

  TestService service = new TestServiceImpl(vertx);
  final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress("someAddress");
  consumer = serviceBinder.register(TestService.class, service);

  router
    .get("/test")
    .handler(
      ValidationHandler.builder(parser).build()
    ).handler(rc -> {
      rc.setUser(fakeUser("slinkydeveloper")); // Put user mock into context
      rc.next();
    })
    .handler(
      RouteToEBServiceHandler.build(vertx.eventBus(), "someAddress", "testUser")
    );

  testRequest(client, HttpMethod.GET, "/test")
    .expect(statusCode(200), statusMessage("OK"))
    .expect(jsonBodyResponse(new JsonObject().put("result", "Hello slinkydeveloper!")))
    .send(testContext, checkpoint);
}
 
Example #2
Source File: RouterFactoryApiServiceIntegrationTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void pathAndOperationExtensionMapsMerge(Vertx vertx, VertxTestContext testContext) {
  Checkpoint checkpoint = testContext.checkpoint(2);

  PathExtensionTestService service = new PathExtensionTestServiceImpl();
  final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress("address");
  consumers.add(serviceBinder.register(PathExtensionTestService.class, service));

  loadFactoryAndStartServer(vertx, "src/test/resources/specs/extension_test.yaml", testContext, routerFactory -> {
    routerFactory.setOptions(HANDLERS_TESTS_OPTIONS);
    routerFactory.mountServicesFromExtensions();
  }).onComplete(h -> {
    testRequest(client, HttpMethod.GET, "/testMerge2")
      .expect(statusCode(200), statusMessage("getPathLevel"))
      .send(testContext, checkpoint);
    testRequest(client, HttpMethod.POST, "/testMerge2")
      .expect(statusCode(200), statusMessage("postPathLevel"))
      .send(testContext, checkpoint);
  });
}
 
Example #3
Source File: RouterFactoryApiServiceIntegrationTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void pathAndOperationExtensionMerge(Vertx vertx, VertxTestContext testContext) {
  Checkpoint checkpoint = testContext.checkpoint(2);

  PathExtensionTestService service = new PathExtensionTestServiceImpl();
  final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress("address");
  consumers.add(serviceBinder.register(PathExtensionTestService.class, service));

  loadFactoryAndStartServer(vertx, "src/test/resources/specs/extension_test.yaml", testContext, routerFactory -> {
    routerFactory.setOptions(HANDLERS_TESTS_OPTIONS);
    routerFactory.mountServicesFromExtensions();
  }).onComplete(h -> {
    testRequest(client, HttpMethod.GET, "/testMerge")
      .expect(statusCode(200), statusMessage("getPathLevel"))
      .send(testContext, checkpoint);
    testRequest(client, HttpMethod.POST, "/testMerge")
      .expect(statusCode(200), statusMessage("postPathLevel"))
      .send(testContext, checkpoint);
  });
}
 
Example #4
Source File: RouterFactoryApiServiceIntegrationTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void pathExtension(Vertx vertx, VertxTestContext testContext) {
  Checkpoint checkpoint = testContext.checkpoint(2);

  PathExtensionTestService service = new PathExtensionTestServiceImpl();
  final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress("address");
  consumers.add(serviceBinder.register(PathExtensionTestService.class, service));

  loadFactoryAndStartServer(vertx, "src/test/resources/specs/extension_test.yaml", testContext, routerFactory -> {
    routerFactory.setOptions(HANDLERS_TESTS_OPTIONS);
    routerFactory.mountServicesFromExtensions();
  }).onComplete(h -> {
    testRequest(client, HttpMethod.GET, "/testPathLevel")
      .expect(statusCode(200), statusMessage("pathLevelGet"))
      .send(testContext, checkpoint);
    testRequest(client, HttpMethod.POST, "/testPathLevel")
      .expect(statusCode(200), statusMessage("pathLevelPost"))
      .send(testContext, checkpoint);
  });
}
 
Example #5
Source File: RouterFactoryApiServiceIntegrationTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void serviceProxyWithReflectionsTest(Vertx vertx, VertxTestContext testContext) {
  Checkpoint checkpoint = testContext.checkpoint(2);

  TestService service = new TestServiceImpl(vertx);
  final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress("someAddress");
  consumers.add(serviceBinder.register(TestService.class, service));

  loadFactoryAndStartServer(vertx, "src/test/resources/specs/service_proxy_test.yaml", testContext, routerFactory -> {
    routerFactory.setOptions(HANDLERS_TESTS_OPTIONS);
    routerFactory.mountServiceInterface(service.getClass(), "someAddress");
  }).onComplete(h -> {
    testRequest(client, HttpMethod.POST, "/testA")
      .expect(statusCode(200))
      .expect(jsonBodyResponse(new JsonObject().put("result", "Ciao Francesco!")))
      .sendJson(new JsonObject().put("hello", "Ciao").put("name", "Francesco"), testContext, checkpoint);
    testRequest(client, HttpMethod.POST, "/testB")
      .expect(statusCode(200))
      .expect(jsonBodyResponse(new JsonObject().put("result", "Ciao Francesco?")))
      .sendJson(new JsonObject().put("hello", "Ciao").put("name", "Francesco"), testContext, checkpoint);
  });
}
 
Example #6
Source File: RouterFactoryApiServiceIntegrationTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void serviceProxyManualTest(Vertx vertx, VertxTestContext testContext) {
  TestService service = new TestServiceImpl(vertx);

  final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress("someAddress");
  consumers.add(serviceBinder.register(TestService.class, service));

  loadFactoryAndStartServer(vertx, "src/test/resources/specs/service_proxy_test.yaml", testContext, routerFactory -> {
    routerFactory.setOptions(HANDLERS_TESTS_OPTIONS);

    routerFactory.operation("testA").routeToEventBus("someAddress");
  }).onComplete(h ->
    testRequest(client, HttpMethod.POST, "/testA")
      .expect(statusCode(200))
      .expect(jsonBodyResponse(new JsonObject().put("result", "Ciao Francesco!")))
      .sendJson(new JsonObject().put("hello", "Ciao").put("name", "Francesco"), testContext)
  );
}
 
Example #7
Source File: RouteToEBServiceHandlerTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void binaryDataTest(Vertx vertx, VertxTestContext testContext) {
  Checkpoint checkpoint = testContext.checkpoint();

  BinaryTestService service = new BinaryTestServiceImpl();
  final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress("someAddress");
  consumer = serviceBinder
    .setIncludeDebugInfo(true)
    .register(BinaryTestService.class, service);

  router
    .get("/test")
    .handler(BodyHandler.create())
    .handler(
      ValidationHandler.builder(parser).build()
    ).handler(
      RouteToEBServiceHandler.build(vertx.eventBus(), "someAddress", "binaryTest")
    );

  testRequest(client, HttpMethod.GET, "/test")
    .expect(statusCode(200), statusMessage("OK"))
    .expect(bodyResponse(Buffer.buffer(new byte[] {(byte) 0xb0}), "application/octet-stream"))
    .send(testContext, checkpoint);
}
 
Example #8
Source File: RouteToEBServiceHandlerTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void extraPayloadTest(Vertx vertx, VertxTestContext testContext) {
  Checkpoint checkpoint = testContext.checkpoint();

  TestService service = new TestServiceImpl(vertx);
  final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress("someAddress");
  consumer = serviceBinder.register(TestService.class, service);

  router
    .get("/test")
    .handler(
      ValidationHandler.builder(parser).build()
    ).handler(
      RouteToEBServiceHandler
        .build(vertx.eventBus(), "someAddress", "extraPayload")
        .extraPayloadMapper(rc -> new JsonObject().put("username", "slinkydeveloper"))
    );

  testRequest(client, HttpMethod.GET, "/test")
    .expect(statusCode(200), statusMessage("OK"))
    .expect(jsonBodyResponse(new JsonObject().put("result", "Hello slinkydeveloper!")))
    .send(testContext, checkpoint);
}
 
Example #9
Source File: RouteToEBServiceHandlerTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void emptyOperationResultTest(Vertx vertx, VertxTestContext testContext) {
  Checkpoint checkpoint = testContext.checkpoint();

  TestService service = new TestServiceImpl(vertx);
  final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress("someAddress");
  consumer = serviceBinder.register(TestService.class, service);

  router
    .get("/test")
    .handler(
      ValidationHandler.builder(parser).build()
    ).handler(
      RouteToEBServiceHandler.build(vertx.eventBus(), "someAddress", "testEmptyServiceResponse")
    );

  testRequest(client, HttpMethod.GET, "/test")
    .expect(statusCode(200), statusMessage("OK"))
    .expect(emptyResponse())
    .send(testContext, checkpoint);
}
 
Example #10
Source File: Examples.java    From vertx-service-proxy with Apache License 2.0 6 votes vote down vote up
public void secure(Vertx vertx) {
  // Create an instance of your service implementation
  SomeDatabaseService service = new SomeDatabaseServiceImpl();
  // Register the handler
  new ServiceBinder(vertx)
    .setAddress("database-service-address")
    // Secure the messages in transit
    .addInterceptor(
      new ServiceAuthInterceptor()
        // Tokens will be validated using JWT authentication
        .setAuthenticationProvider(JWTAuth.create(vertx, new JWTAuthOptions()))
        // optionally we can secure permissions too:

        // an admin
        .addAuthorization(RoleBasedAuthorization.create("admin"))
        // that can print
        .addAuthorization(PermissionBasedAuthorization.create("print"))

        // where the authorizations are loaded, let's assume from the token
        // but they could be loaded from a database or a file if needed
        .setAuthorizationProvider(
          JWTAuthorization.create("permissions")))

    .register(SomeDatabaseService.class, service);
}
 
Example #11
Source File: ServiceProxyTest.java    From vertx-service-proxy with Apache License 2.0 6 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  uri1 = new URI("http://foo.com");
  uri2 = new URI("http://bar.com");
  service = TestService.create(vertx);
  localService = TestService.create(vertx);

  consumer = new ServiceBinder(vertx).setAddress(SERVICE_ADDRESS)
    .register(TestService.class, service);
  consumerWithDebugEnabled = new ServiceBinder(vertx)
    .setAddress(SERVICE_WITH_DEBUG_ADDRESS)
    .setIncludeDebugInfo(true)
    .register(TestService.class, service);
  localConsumer = new ServiceBinder(vertx).setAddress(SERVICE_LOCAL_ADDRESS)
    .registerLocal(TestService.class, localService);

  proxy = TestService.createProxy(vertx, SERVICE_ADDRESS);
  localProxy = TestService.createProxy(vertx, SERVICE_LOCAL_ADDRESS);
  proxyWithDebug = TestService.createProxy(vertx, SERVICE_WITH_DEBUG_ADDRESS);
  vertx.eventBus().<String>consumer(TEST_ADDRESS).handler(msg -> {
    assertEquals("ok", msg.body());
    testComplete();
  });
}
 
Example #12
Source File: ElasticSearchServiceVerticle.java    From vertx-elasticsearch-service with Apache License 2.0 6 votes vote down vote up
@Override
public void start() throws Exception {

    // workaround for problem between ES netty and vertx (both wanting to set the same value)
    System.setProperty("es.set.netty.runtime.available.processors", "false");

    String address = config().getString("address");
    if (address == null || address.isEmpty()) {
        throw new IllegalStateException("address field must be specified in config for service verticle");
    }
    String adminAddress = config().getString("address.admin");
    if (adminAddress == null || adminAddress.isEmpty()) {
        adminAddress = address + ".admin";
    }

    // Register service as an event bus proxy
    new ServiceBinder(vertx).setAddress(address).register(ElasticSearchService.class, service);
    new ServiceBinder(vertx).setAddress(adminAddress).register(ElasticSearchAdminService.class, adminService);

    // Start the service
    service.start();

}
 
Example #13
Source File: BookDatabaseVerticle.java    From vertx-postgresql-starter with MIT License 5 votes vote down vote up
@Override
public void start(Future<Void> startFuture) throws Exception {
  PgPoolOptions pgPoolOptions = new PgPoolOptions()
    .setHost(config().getString(CONFIG_PG_HOST, "127.0.0.1"))
    .setPort(config().getInteger(CONFIG_PG_PORT, 5432))
    .setDatabase(config().getString(CONFIG_PG_DATABASE))
    .setUser(config().getString(CONFIG_PG_USERNAME))
    .setPassword(config().getString(CONFIG_PG_PASSWORD))
    .setMaxSize(config().getInteger(CONFIG_PG_POOL_MAX_SIZE, 20));

  this.pgPool = PgClient.pool(vertx, pgPoolOptions);

  String databaseEbAddress = config().getString(CONFIG_DB_EB_QUEUE);

  BookDatabaseService.create(pgPool, result -> {
    if (result.succeeded()) {
      // register the database service
      new ServiceBinder(vertx)
        .setAddress(databaseEbAddress)
        .register(BookDatabaseService.class, result.result())
        .exceptionHandler(throwable -> {
          LOGGER.error("Failed to establish PostgreSQL database service", throwable);
          startFuture.fail(throwable);
        })
        .completionHandler(res -> {
          LOGGER.info("PostgreSQL database service is successfully established in \"" + databaseEbAddress + "\"");
          startFuture.complete();
        });
    } else {
      LOGGER.error("Failed to initiate the connection to database", result.cause());
      startFuture.fail(result.cause());
    }
  });
}
 
Example #14
Source File: Examples.java    From vertx-service-proxy with Apache License 2.0 5 votes vote down vote up
public void unregister(Vertx vertx) {
  ServiceBinder binder = new ServiceBinder(vertx);

  // Create an instance of your service implementation
  SomeDatabaseService service = new SomeDatabaseServiceImpl();
  // Register the handler
  MessageConsumer<JsonObject> consumer = binder
    .setAddress("database-service-address")
    .register(SomeDatabaseService.class, service);

  // ....

  // Unregister your service.
  binder.unregister(consumer);
}
 
Example #15
Source File: SecureServiceBinderTest.java    From vertx-service-proxy with Apache License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  OKService service = new OKServiceImpl();

  ServiceBinder serviceBinder = new ServiceBinder(vertx)
    .setAddress(SERVICE_ADDRESS)
    .addInterceptor(
      new ServiceAuthInterceptor()
        .setAuthenticationProvider(JWTAuth.create(vertx, getJWTConfig())
    ));

  ServiceBinder localServiceBinder = new ServiceBinder(vertx)
    .setAddress(SERVICE_LOCAL_ADDRESS)
    .addInterceptor(
      new ServiceAuthInterceptor()
      .setAuthenticationProvider(JWTAuth.create(vertx, getJWTConfig())
    ));

  consumer = serviceBinder.register(OKService.class, service);
  localConsumer = localServiceBinder.registerLocal(OKService.class, service);

  serviceProxyBuilder = new ServiceProxyBuilder(vertx)
    .setAddress(SERVICE_ADDRESS);

  localServiceProxyBuilder = new ServiceProxyBuilder(vertx)
    .setAddress(SERVICE_LOCAL_ADDRESS);
}
 
Example #16
Source File: ServiceBinderTest.java    From vertx-service-proxy with Apache License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  OKService service = new OKServiceImpl();

  final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress(SERVICE_ADDRESS);
  final ServiceBinder serviceLocalBinder = new ServiceBinder(vertx).setAddress(SERVICE_LOCAL_ADDRESS);
  final ServiceProxyBuilder serviceProxyBuilder = new ServiceProxyBuilder(vertx).setAddress(SERVICE_ADDRESS);
  final ServiceProxyBuilder serviceLocalProxyBuilder = new ServiceProxyBuilder(vertx).setAddress(SERVICE_LOCAL_ADDRESS);

  consumer = serviceBinder.register(OKService.class, service);
  localConsumer = serviceLocalBinder.registerLocal(OKService.class, service);
  proxy = serviceProxyBuilder.build(OKService.class);
  localProxy = serviceLocalProxyBuilder.build(OKService.class);
}
 
Example #17
Source File: Examples.java    From vertx-service-proxy with Apache License 2.0 5 votes vote down vote up
public void register(Vertx vertx) {
  // Create an instance of your service implementation
  SomeDatabaseService service = new SomeDatabaseServiceImpl();
  // Register the handler
  new ServiceBinder(vertx)
    .setAddress("database-service-address")
    .register(SomeDatabaseService.class, service);
}
 
Example #18
Source File: ServiceProxyTest.java    From vertx-service-proxy with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectionTimeout() {

  consumer.unregister();
  consumer = new ServiceBinder(vertx)
    .setAddress(SERVICE_ADDRESS)
    .setTimeoutSeconds(2)
    .register(TestService.class, service);

  checkConnection(proxy, 2L);

  await();
}
 
Example #19
Source File: ServiceProxyTest.java    From vertx-service-proxy with Apache License 2.0 5 votes vote down vote up
@Test
public void testLocalServiceConnectionTimeout() {

  localConsumer.unregister();
  localConsumer = new ServiceBinder(vertx)
    .setAddress(SERVICE_LOCAL_ADDRESS)
    .setTimeoutSeconds(2)
    .register(TestService.class, localService);

  checkConnection(localProxy, 2L);

  await();
}
 
Example #20
Source File: RouterFactoryApiServiceIntegrationTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void operationExtension(Vertx vertx, VertxTestContext testContext) {
  Checkpoint checkpoint = testContext.checkpoint(4);

  TestService service = new TestServiceImpl(vertx);
  final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress("address");
  consumers.add(serviceBinder.register(TestService.class, service));

  AnotherTestService anotherService = AnotherTestService.create(vertx);
  final ServiceBinder anotherServiceBinder = new ServiceBinder(vertx).setAddress("anotherAddress");
  consumers.add(anotherServiceBinder.register(AnotherTestService.class, anotherService));

  loadFactoryAndStartServer(vertx, "src/test/resources/specs/extension_test.yaml", testContext, routerFactory -> {
    routerFactory.setOptions(HANDLERS_TESTS_OPTIONS);
    routerFactory.mountServicesFromExtensions();
  }).onComplete(h -> {
    testRequest(client, HttpMethod.POST, "/testA")
      .expect(jsonBodyResponse(new JsonObject().put("result", "Ciao Francesco!")), statusCode(200))
      .sendJson(new JsonObject().put("hello", "Ciao").put("name", "Francesco"), testContext, checkpoint);

    testRequest(client, HttpMethod.POST, "/testB")
      .expect(jsonBodyResponse(new JsonObject().put("result", "Ciao Francesco?")), statusCode(200))
      .sendJson(new JsonObject().put("hello", "Ciao").put("name", "Francesco"), testContext, checkpoint);

    testRequest(client, HttpMethod.POST, "/testC")
      .expect(jsonBodyResponse(new JsonObject().put("content-type", "application/json").put("anotherResult", "Francesco Ciao?")), statusCode(200))
      .sendJson(new JsonObject().put("hello", "Ciao").put("name", "Francesco"), testContext, checkpoint);

    testRequest(client, HttpMethod.POST, "/testD")
      .expect(jsonBodyResponse(new JsonObject().put("content-type", "application/json").put("anotherResult", "Francesco Ciao?")), statusCode(200))
      .sendJson(new JsonObject().put("hello", "Ciao").put("name", "Francesco"), testContext, checkpoint);
  });
}
 
Example #21
Source File: RouteToEBServiceHandlerTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void serviceProxyDataObjectTest(Vertx vertx, VertxTestContext testContext) {
  Checkpoint checkpoint = testContext.checkpoint();

  AnotherTestService service = new AnotherTestServiceImpl(vertx);
  final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress("someAddress");
  consumer = serviceBinder.register(AnotherTestService.class, service);

  router
    .post("/test")
    .handler(BodyHandler.create())
    .handler(
      ValidationHandler.builder(parser)
        .body(json(ref(JsonPointer.fromURI(URI.create("filter.json")))))
        .build()
    ).handler(
      RouteToEBServiceHandler.build(vertx.eventBus(), "someAddress", "testDataObject")
    );

  FilterData data = FilterData.generate();

  JsonObject result = data.toJson().copy();
  result.remove("message");

  testRequest(client, HttpMethod.POST, "/test")
    .expect(statusCode(200), statusMessage("OK"))
    .expect(jsonBodyResponse(result))
    .sendJson(data.toJson(), testContext, checkpoint);
}
 
Example #22
Source File: ApiCodegenExamples.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
public void serviceMount(Vertx vertx) {
  // Instatiate the service
  TransactionService transactionService = new TransactionServiceImpl();

  // Mount the service on the event bus
  ServiceBinder transactionServiceBinder = new ServiceBinder(vertx);
  transactionServiceBinder
    .setAddress("transactions.myapplication")
    .register(TransactionService.class, transactionService);
}
 
Example #23
Source File: ServiceRegistry.java    From nubes with Apache License 2.0 5 votes vote down vote up
private <T> void createServiceProxy(String address, T service) {
  Class<T> serviceClass = getInterface(service.getClass());
  if (serviceClass == null) {
    LOG.error("Could not find a @ProxyGen super interface for class : " + service.getClass().getName() + " cannot proxy it ver the eventBus");
    return;
  }
  new ServiceBinder(vertx)
          .setAddress(address)
          .register(serviceClass, service);
}
 
Example #24
Source File: RouteToEBServiceHandlerTest.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
@Test
public void serviceProxyManualFailureTest(Vertx vertx, VertxTestContext testContext) {
  Checkpoint checkpoint = testContext.checkpoint(2);

  FailureTestService service = new FailureTestServiceImpl(vertx);
  final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress("someAddress");
  consumer = serviceBinder
    .setIncludeDebugInfo(true)
    .register(FailureTestService.class, service);

  router
    .post("/testFailure")
    .handler(BodyHandler.create())
    .handler(
      ValidationHandler.builder(parser)
        .body(json(
          objectSchema()
            .requiredProperty("hello", stringSchema())
            .requiredProperty("name", stringSchema())
            .allowAdditionalProperties(false)
        )).build()
    ).handler(
      RouteToEBServiceHandler.build(vertx.eventBus(), "someAddress", "testFailure")
    ).failureHandler(
      rc -> rc.response().setStatusCode(501).setStatusMessage(rc.failure().getMessage()).end()
    );

  router
    .post("/testException")
    .handler(BodyHandler.create())
    .handler(
      ValidationHandler.builder(parser)
        .body(json(
          objectSchema()
            .requiredProperty("hello", stringSchema())
            .requiredProperty("name", stringSchema())
            .allowAdditionalProperties(false)
        )).build()
    ).handler(
      RouteToEBServiceHandler.build(vertx.eventBus(), "someAddress", "testException")
    );

  testRequest(client, HttpMethod.POST, "/testFailure")
    .expect(statusCode(501), statusMessage("error for Francesco"))
    .sendJson(new JsonObject().put("hello", "Ciao").put("name", "Francesco"), testContext, checkpoint);

  testRequest(client, HttpMethod.POST, "/testException")
    .expect(statusCode(500), statusMessage("Unknown failure: (RECIPIENT_FAILURE,-1)"))
    .sendJson(new JsonObject().put("hello", "Ciao").put("name", "Francesco"), testContext, checkpoint);
}
 
Example #25
Source File: RouteToEBServiceHandlerTest.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
@Test
public void serviceProxyTypedTest(Vertx vertx, VertxTestContext testContext) {
  Checkpoint checkpoint = testContext.checkpoint(3);

  AnotherTestService service = new AnotherTestServiceImpl(vertx);
  final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress("someAddress");
  consumer = serviceBinder.register(AnotherTestService.class, service);

  router
    .post("/testE/:id")
    .handler(BodyHandler.create())
    .handler(
      ValidationHandler.builder(parser)
        .pathParameter(param("id", intSchema()))
        .body(json(objectSchema().property("value", intSchema())))
        .build()
    ).handler(
      RouteToEBServiceHandler.build(vertx.eventBus(), "someAddress", "testE")
    );

  router
    .post("/testF/:id")
    .handler(BodyHandler.create())
    .handler(
      ValidationHandler.builder(parser)
        .pathParameter(param("id", intSchema()))
        .body(json(
          anyOf(
            objectSchema().property("value", intSchema()),
            arraySchema().items(intSchema())
          )
        ))
        .build()
    ).handler(
      RouteToEBServiceHandler.build(vertx.eventBus(), "someAddress", "testF")
    );


  testRequest(client, HttpMethod.POST, "/testE/123")
    .expect(statusCode(200), statusMessage("OK"))
    .expect(jsonBodyResponse(new JsonObject().put("id", 123).put("value", 1)))
    .sendJson(new JsonObject().put("value", 1), testContext, checkpoint);

  testRequest(client, HttpMethod.POST, "/testF/123")
    .expect(statusCode(200), statusMessage("OK"))
    .expect(jsonBodyResponse(new JsonArray().add(1 + 123).add(2 + 123).add(3 + 123)))
    .sendJson(new JsonArray().add(1).add(2).add(3), testContext, checkpoint);

  testRequest(client, HttpMethod.POST, "/testF/123")
    .expect(statusCode(200), statusMessage("OK"))
    .expect(jsonBodyResponse(new JsonObject().put("id", 123).put("value", 1)))
    .sendJson(new JsonObject().put("value", 1), testContext, checkpoint);
}
 
Example #26
Source File: HandlerParamsTest.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
@BeforeEach
public void setUp(Vertx vertx) {
  ParamsTestServiceImpl service = new ParamsTestServiceImpl();
  final ServiceBinder serviceBinder = new ServiceBinder(vertx).setAddress(ADDRESS);
  consumer = serviceBinder.register(ParamsTestService.class, service);
}
 
Example #27
Source File: DataVerticle.java    From vertx-in-action with MIT License 4 votes vote down vote up
@Override
public void start() {
  new ServiceBinder(vertx)
    .setAddress("sensor.data-service")
    .register(SensorDataService.class, SensorDataService.create(vertx));
}
 
Example #28
Source File: ServiceProviderVerticle.java    From vertx-service-proxy with Apache License 2.0 4 votes vote down vote up
@Override
public void start() throws Exception {
  new ServiceBinder(vertx).setAddress("my.service").register(Service.class, new ServiceProvider());
}
 
Example #29
Source File: LocalServiceProviderVerticle.java    From vertx-service-proxy with Apache License 2.0 4 votes vote down vote up
@Override
public void start() throws Exception {
  new ServiceBinder(vertx).setAddress("my.local.service").registerLocal(Service.class, new ServiceProvider());
}