io.vertx.reactivex.ext.web.codec.BodyCodec Java Examples

The following examples show how to use io.vertx.reactivex.ext.web.codec.BodyCodec. 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: AppTest.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Test
public void testCdiInjection(TestContext context) {
  final ConsoleHandler consoleHandler = new ConsoleHandler();
  consoleHandler.setLevel(Level.FINEST);
  consoleHandler.setFormatter(new SimpleFormatter());

  final Logger app = Logger.getLogger("org.jboss.weld.vertx");
  app.setLevel(Level.FINEST);
  app.addHandler(consoleHandler);

  Async async = context.async();
    webClient.get("/test/injection")
    .as(BodyCodec.string())
    .rxSend()
    .subscribe(body -> {
        context.assertEquals(200, body.statusCode());
        async.complete();
    }, x -> { 
        x.printStackTrace();
        context.fail(x);
        async.complete();
    });
}
 
Example #2
Source File: ApiTest.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Test
public void checkTemplateWithHtmlExtension(TestContext context) {
	Async async = context.async();

	webClient
	.get("/negoWithHtmlExtension")
	.as(BodyCodec.string())
	.rxSend()
	.map(r -> {
		context.assertEquals(200, r.statusCode());
		context.assertEquals("<html>\n" + 
				" <head>\n" + 
				"  <title>my title</title>\n" + 
				" </head>\n" + 
				" <body>my message</body>\n" + 
				"</html>", r.body());
		context.assertEquals("text/html", r.getHeader("Content-Type"));
		return r;
	})
	.doOnError(x -> context.fail(x))
	.subscribe(response -> {
		async.complete();
	});
}
 
Example #3
Source File: ApiTest.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Test
public void checkTemplateWithHtmlAndTemplateExtension(TestContext context) {
	Async async = context.async();

	webClient
	.get("/negoWithHtmlAndTemplateExtension")
	.as(BodyCodec.string())
	.rxSend()
	.map(r -> {
		context.assertEquals(200, r.statusCode());
		context.assertEquals("<html>\n" + 
				" <head>\n" + 
				"  <title>my title</title>\n" + 
				" </head>\n" + 
				" <body>my message</body>\n" + 
				"</html>", r.body());
		context.assertEquals("text/html", r.getHeader("Content-Type"));
		return r;
	})
	.doOnError(x -> context.fail(x))
	.subscribe(response -> {
		async.complete();
	});
}
 
Example #4
Source File: DashboardWebAppVerticle.java    From vertx-in-action with MIT License 6 votes vote down vote up
private void hydrate() {
  WebClient webClient = WebClient.create(vertx);
  webClient
    .get(3001, "localhost", "/ranking-last-24-hours")
    .as(BodyCodec.jsonArray())
    .rxSend()
    .delay(5, TimeUnit.SECONDS, RxHelper.scheduler(vertx))
    .retry(5)
    .map(HttpResponse::body)
    .flattenAsFlowable(Functions.identity())
    .cast(JsonObject.class)
    .flatMapSingle(json -> whoOwnsDevice(webClient, json))
    .flatMapSingle(json -> fillWithUserProfile(webClient, json))
    .subscribe(
      this::hydrateEntryIfPublic,
      err -> logger.error("Hydratation error", err),
      () -> logger.info("Hydratation completed"));
}
 
Example #5
Source File: ApiTest.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Test
public void checkTemplateNegociationText2(TestContext context) {
	Async async = context.async();

	webClient
	.get("/nego")
	.putHeader("Accept", "text/plain, text/html;q=0.9")
	.as(BodyCodec.string())
	.rxSend()
	.map(r -> {
		context.assertEquals(200, r.statusCode());
		context.assertEquals("## my title ##\n" + 
				"\n" + 
				"my message", r.body());
		context.assertEquals("text/plain", r.getHeader("Content-Type"));
		return r;
	})
	.doOnError(x -> context.fail(x))
	.subscribe(response -> {
		async.complete();
	});
}
 
Example #6
Source File: ApiTest.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Test
public void checkTemplateNegociationHtml(TestContext context) {
	Async async = context.async();

	webClient
	.get("/nego")
	.putHeader("Accept", "text/html")
	.as(BodyCodec.string())
	.rxSend()
	.map(r -> {
		context.assertEquals(200, r.statusCode());
		context.assertEquals("<html>\n" + 
				" <head>\n" + 
				"  <title>my title</title>\n" + 
				" </head>\n" + 
				" <body>my message</body>\n" + 
				"</html>", r.body());
		context.assertEquals("text/html", r.getHeader("Content-Type"));
		return r;
	})
	.doOnError(x -> context.fail(x))
	.subscribe(response -> {
		async.complete();
	});
}
 
Example #7
Source File: ApiTest.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Test
public void checkTemplateNegociationHtml2(TestContext context) {
	Async async = context.async();

	webClient
	.get("/nego")
	.putHeader("Accept", "text/html, text/plain;q=0.9")
	.as(BodyCodec.string())
	.rxSend()
	.map(r -> {
		context.assertEquals(200, r.statusCode());
		context.assertEquals("<html>\n" + 
				" <head>\n" + 
				"  <title>my title</title>\n" + 
				" </head>\n" + 
				" <body>my message</body>\n" + 
				"</html>", r.body());
		context.assertEquals("text/html", r.getHeader("Content-Type"));
		return r;
	})
	.doOnError(x -> context.fail(x))
	.subscribe(response -> {
		async.complete();
	});
}
 
Example #8
Source File: ApiTest.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Test
public void checkTemplateNegociationNotAcceptable(TestContext context) {
	Async async = context.async();

	webClient
	.get("/nego")
	.putHeader("Accept", "text/stef")
	.as(BodyCodec.string())
	.rxSend()
	.map(r -> {
		context.assertEquals(Status.NOT_ACCEPTABLE.getStatusCode(), r.statusCode());
		return r;
	})
	.doOnError(x -> context.fail(x))
	.subscribe(response -> {
		async.complete();
	});
}
 
Example #9
Source File: ApiTest.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Test
public void checkTemplateNegociationSingleHtml(TestContext context) {
	Async async = context.async();

	webClient
	.get("/single")
	.as(BodyCodec.string())
	.rxSend()
	.map(r -> {
		context.assertEquals(200, r.statusCode());
		context.assertEquals("<html>\n" + 
				" <head>\n" + 
				"  <title>my title</title>\n" + 
				" </head>\n" + 
				" <body>my message</body>\n" + 
				"</html>", r.body());
		context.assertEquals("text/html", r.getHeader("Content-Type"));
		return r;
	})
	.doOnError(x -> context.fail(x))
	.subscribe(response -> {
		async.complete();
	});
}
 
Example #10
Source File: ApiTest.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Test
public void checkTemplateDefaultName(TestContext context) {
	Async async = context.async();

	webClient
	.get("/defaultTemplate")
	.as(BodyCodec.string())
	.rxSend()
	.map(r -> {
		context.assertEquals(200, r.statusCode());
		context.assertEquals("<html>\n" + 
				" <head>\n" + 
				"  <title>my title</title>\n" + 
				" </head>\n" + 
				" <body>my message</body>\n" + 
				"</html>", r.body());
		context.assertEquals("text/html", r.getHeader("Content-Type"));
		return r;
	})
	.doOnError(x -> context.fail(x))
	.subscribe(response -> {
		async.complete();
	});
}
 
Example #11
Source File: DiscoveryTest.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Test
public void checkServiceDiscovery(TestContext context) {
	Async async = context.async();

	webClient
	.get("/test/service-discovery")
	.as(BodyCodec.string())
	.rxSend()
	.map(r -> {
		context.assertEquals("ok", r.body());
		return r;
	})
	.doOnError(x -> context.fail(x))
	.subscribe(response -> {
		async.complete();
	});
}
 
Example #12
Source File: DiscoveryTest.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Test
public void checkWebClient(TestContext context) {
	Async async = context.async();

	webClient
	.get("/test/web-client")
	.as(BodyCodec.string())
	.rxSend()
	.map(r -> {
		context.assertEquals("hello", r.body());
		return r;
	})
	.doOnError(x -> context.fail(x))
	.subscribe(response -> {
		async.complete();
	});
}
 
Example #13
Source File: AppTest.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Test
public void testCdi(TestContext context) {
  final ConsoleHandler consoleHandler = new ConsoleHandler();
  consoleHandler.setLevel(Level.FINEST);
  consoleHandler.setFormatter(new SimpleFormatter());

  final Logger app = Logger.getLogger("org.jboss.weld.vertx");
  app.setLevel(Level.FINEST);
  app.addHandler(consoleHandler);

    Async async = context.async();
    webClient.get("/test")
    .as(BodyCodec.string())
    .rxSend()
    .subscribe(body -> {
        context.assertEquals(200, body.statusCode());
        context.assertEquals("hello response from event bus", body.body());
        async.complete();
    }, x -> { 
        x.printStackTrace();
        context.fail(x);
        async.complete();
    });
}
 
Example #14
Source File: ApiTest.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Test
public void checkTemplateNegociationText(TestContext context) {
	Async async = context.async();

	webClient
	.get("/nego")
	.putHeader("Accept", "text/plain")
	.as(BodyCodec.string())
	.rxSend()
	.map(r -> {
		context.assertEquals(200, r.statusCode());
		context.assertEquals("## my title ##\n" + 
				"\n" + 
				"my message", r.body());
		context.assertEquals("text/plain", r.getHeader("Content-Type"));
		return r;
	})
	.doOnError(x -> context.fail(x))
	.subscribe(response -> {
		async.complete();
	});
}
 
Example #15
Source File: ApiTest.java    From redpipe with Apache License 2.0 6 votes vote down vote up
private void checkHelloObservableCollect(String prefix, TestContext context) {
	Async async = context.async();

	webClient
	.get(prefix+"/hello-observable-collect")
	.as(BodyCodec.jsonArray())
	.rxSend()
	.map(r -> {
		context.assertEquals(new JsonArray().add("one").add("two"), r.body());
		return r;
	})
	.doOnError(x -> context.fail(x))
	.subscribe(response -> {
		async.complete();
	});
}
 
Example #16
Source File: ApiTest.java    From redpipe with Apache License 2.0 6 votes vote down vote up
private void checkRequest(int expectedStatus, String expectedBody, String url, TestContext context, String authHeader) {
	Async async = context.async();

	HttpRequest<Buffer> request = webClient
	.get(url);
	
	if(authHeader != null)
		request.putHeader(HttpHeaders.AUTHORIZATION, authHeader);
	
	request.as(BodyCodec.string())
	.rxSend()
	.map(r -> {
		context.assertEquals(expectedStatus, r.statusCode());
		if(expectedBody != IGNORE)
			context.assertEquals(expectedBody, r.body());
		return r;
	})
	.doOnError(context::fail)
	.subscribe(response -> async.complete());
}
 
Example #17
Source File: ServerExtendableTest.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Test
public void checkHasNoSession(TestContext context) {
    Async async = context.async();

    webClient
            .get("/test")
            .as(BodyCodec.string())
            .rxSend()
            .map(r -> {
                String sessionCookie = r.getHeader("set-cookie");
                context.assertNull(sessionCookie, "shouldn't have a session");
                return r.body();
            })
            .doOnError(x -> context.fail(x))
            .subscribe(response -> {
                async.complete();
            });
}
 
Example #18
Source File: UpsertBookByIdHandlerTest.java    From vertx-postgresql-starter with MIT License 6 votes vote down vote up
@Test
public void restApiTest(TestContext testContext) {
  expectedResponseStatusCode = 200;

  JsonObject expectedResponseBody = new JsonObject()
    .put("id", 3)
    .put("title", "Java Concurrency in Practice")
    .put("category", "java")
    .put("publicationDate", "2006-05-19");

  JsonObject requestBody = new JsonObject()
    .put("title", "Java Concurrency in Practice")
    .put("category", "java")
    .put("publicationDate", "2006-05-19");

  webClient.request(PUT, 1234, "localhost", "/books/3")
    .putHeader("Content-Type", "application/json; charset=utf-8")
    .as(BodyCodec.jsonObject())
    .sendJsonObject(requestBody, testContext.asyncAssertSuccess(resp -> {
      testContext.assertEquals(expectedResponseStatusCode, resp.statusCode());
      testContext.assertEquals(expectedResponseBody, resp.body());
    }));
}
 
Example #19
Source File: AddBookHandlerTest.java    From vertx-postgresql-starter with MIT License 6 votes vote down vote up
@Test
public void restApiTest(TestContext testContext) {
  expectedResponseStatusCode = 200;

  JsonObject expectedResponseBody = new JsonObject()
    .put("id", 3)
    .put("title", "Design Patterns")
    .put("category", "design")
    .put("publicationDate", "1995-01-15");

  JsonObject requestBody = new JsonObject()
    .put("id", 3)
    .put("title", "Design Patterns")
    .put("category", "design")
    .put("publicationDate", "1995-01-15");

  webClient.request(POST, 1234, "localhost", "/books")
    .putHeader("Content-Type", "application/json; charset=utf-8")
    .as(BodyCodec.jsonObject())
    .sendJsonObject(requestBody, testContext.asyncAssertSuccess(resp -> {
      testContext.assertEquals(expectedResponseStatusCode, resp.statusCode());
      testContext.assertEquals(expectedResponseBody, resp.body());
    }));
}
 
Example #20
Source File: GetBooksHandlerTest.java    From vertx-postgresql-starter with MIT License 6 votes vote down vote up
@Test
public void restApiTest(TestContext testContext) {
  expectedResponseStatusCode = 200;

  JsonArray expectedResponseBody = new JsonArray()
    .add(new JsonObject()
      .put("id", 1)
      .put("title", "Effective Java")
      .put("category", "java")
      .put("publicationDate", "2009-01-01"))
    .add(new JsonObject()
      .put("id", 2)
      .put("title", "Thinking in Java")
      .put("category", "java")
      .put("publicationDate", "2006-02-20"));

  webClient.request(GET, 1234, "localhost", "/books")
    .putHeader("Content-Type", "application/json; charset=utf-8")
    .as(BodyCodec.jsonArray())
    .send(testContext.asyncAssertSuccess(resp -> {
      testContext.assertEquals(expectedResponseStatusCode, resp.statusCode());
      testContext.assertEquals(expectedResponseBody, resp.body());
    }));
}
 
Example #21
Source File: GetBookByIdHandlerTest.java    From vertx-postgresql-starter with MIT License 6 votes vote down vote up
@Test
public void restApiTest(TestContext testContext) {
  expectedResponseStatusCode = 200;

  JsonObject expectedResponseBody = new JsonObject()
    .put("id", 1)
    .put("title", "Effective Java")
    .put("category", "java")
    .put("publicationDate", "2009-01-01");

  webClient.request(GET, 1234, "localhost", "/books/1")
    .putHeader("Content-Type", "application/json; charset=utf-8")
    .as(BodyCodec.jsonObject())
    .send(testContext.asyncAssertSuccess(resp -> {
      testContext.assertEquals(expectedResponseStatusCode, resp.statusCode());
      testContext.assertEquals(expectedResponseBody, resp.body());
    }));
}
 
Example #22
Source File: PortfolioServiceImpl.java    From vertx-kubernetes-workshop with Apache License 2.0 6 votes vote down vote up
private Single<Double> getValueForCompany(WebClient client, String company, int numberOfShares) {
        //TODO
        //----

        return client.get("/?name=" + encode(company))
            .as(BodyCodec.jsonObject())
            .rxSend()
            .map(HttpResponse::body)
            .map(json -> json.getDouble("bid"))
            .map(val -> val * numberOfShares);

//        return Single.just(0.0);
        // ---


    }
 
Example #23
Source File: WebClientTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testGet() {
  int times = 5;
  waitFor(times);
  HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
  server.requestStream().handler(req -> req.response().setChunked(true).end("some_content"));
  try {
    server.listen(ar -> {
      client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
      Single<HttpResponse<Buffer>> single = client
              .get(8080, "localhost", "/the_uri")
              .as(BodyCodec.buffer())
              .rxSend();
      for (int i = 0; i < times; i++) {
        single.subscribe(resp -> {
          Buffer body = resp.body();
          assertEquals("some_content", body.toString("UTF-8"));
          complete();
        }, this::fail);
      }
    });
    await();
  } finally {
    server.close();
  }
}
 
Example #24
Source File: WebClientTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testResponseBodyAsAsJsonMapped() throws Exception {
  JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu");
  HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
  server.requestStream().handler(req -> req.response().end(expected.encode()));
  try {
    server.listen(ar -> {
      client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
      Single<HttpResponse<WineAndCheese>> single = client
              .get(8080, "localhost", "/the_uri")
              .as(BodyCodec.json(WineAndCheese.class))
              .rxSend();
      single.subscribe(resp -> {
        assertEquals(200, resp.statusCode());
        assertEquals(new WineAndCheese().setCheese("Goat Cheese").setWine("Condrieu"), resp.body());
        testComplete();
      }, this::fail);
    });
    await();
  } finally {
    server.close();
  }
}
 
Example #25
Source File: WebClientTest.java    From vertx-rx with Apache License 2.0 6 votes vote down vote up
@Test
public void testErrorHandling() throws Exception {
  try {
    client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
    Single<HttpResponse<WineAndCheese>> single = client
            .get(-1, "localhost", "/the_uri")
            .as(BodyCodec.json(WineAndCheese.class))
            .rxSend();
    single.subscribe(resp -> fail(), error -> {
      assertEquals(IllegalArgumentException.class, error.getClass());
      testComplete();
    });
    await();
  } catch (Throwable t) {
    fail();
  }
}
 
Example #26
Source File: CongratsTest.java    From vertx-in-action with MIT License 6 votes vote down vote up
@Test
@DisplayName("No email must be sent below 10k steps")
void checkNothingBelow10k(Vertx vertx, VertxTestContext testContext) {
  producer
    .rxSend(record("123", 5000))
    .ignoreElement()
    .delay(3, TimeUnit.SECONDS, RxHelper.scheduler(vertx))
    .andThen(webClient
      .get(8025, "localhost", "/api/v2/search?kind=to&[email protected]")
      .as(BodyCodec.jsonObject()).rxSend())
    .map(HttpResponse::body)
    .subscribe(
      json -> {
        testContext.verify(() -> assertThat(json.getInteger("total")).isEqualTo(0));
        testContext.completeNow();
      },
      testContext::failNow);
}
 
Example #27
Source File: ApiTest.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Test
public void checkTemplateWithTemplateExtension(TestContext context) {
	Async async = context.async();

	webClient
	.get("/indexWithTemplateExtension")
	.as(BodyCodec.string())
	.rxSend()
	.map(r -> {
		System.err.println("body: "+r.body());
		context.assertEquals(200, r.statusCode());
		context.assertEquals("<html>\n" + 
				" <head>\n" + 
				"  <title>my title</title>\n" + 
				" </head>\n" + 
				" <body>my message</body>\n" + 
				"</html>", r.body());
		return r;
	})
	.doOnError(x -> context.fail(x))
	.subscribe(response -> {
		async.complete();
	});
}
 
Example #28
Source File: ApiTest.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Test
public void checkTemplate(TestContext context) {
	Async async = context.async();

	webClient
	.get("/")
	.as(BodyCodec.string())
	.rxSend()
	.map(r -> {
		System.err.println("body: "+r.body());
		context.assertEquals(200, r.statusCode());
		context.assertEquals("<html>\n" + 
				" <head>\n" + 
				"  <title>my title</title>\n" + 
				" </head>\n" + 
				" <body>my message</body>\n" + 
				"</html>", r.body());
		return r;
	})
	.doOnError(x -> context.fail(x))
	.subscribe(response -> {
		async.complete();
	});
}
 
Example #29
Source File: ApiTest.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Test
public void checkTemplateNegociationDefault(TestContext context) {
	Async async = context.async();

	webClient
	.get("/nego")
	.as(BodyCodec.string())
	.rxSend()
	.map(r -> {
		context.assertEquals(200, r.statusCode());
		context.assertEquals("<html>\n" + 
				" <head>\n" + 
				"  <title>my title</title>\n" + 
				" </head>\n" + 
				" <body>my message</body>\n" + 
				"</html>", r.body());
		context.assertEquals("text/html", r.getHeader("Content-Type"));
		return r;
	})
	.doOnError(x -> context.fail(x))
	.subscribe(response -> {
		async.complete();
	});
}
 
Example #30
Source File: Code13.java    From rxjava2-lab with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    SuperHeroesService.run();

    Single<Character> random_heroes = client()
        .get("/heroes/random")
        .as(BodyCodec.json(Character.class))
        .rxSend()
        .map(HttpResponse::body);

    Single<Character> random_villains = client()
        .get("/villains/random")
        .as(BodyCodec.json(Character.class))
        .rxSend()
        .map(HttpResponse::body);

    // Associate the items emitted by both single and call the fight method.
    // In the subscribe print the returned json object (using encodePrettily).
    

}