Java Code Examples for org.springframework.test.web.reactive.server.FluxExchangeResult

The following examples show how to use org.springframework.test.web.reactive.server.FluxExchangeResult. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Source File: ResponseEntityTests.java    License: MIT License 6 votes vote down vote up
@Test
public void entityStream() {

	FluxExchangeResult<Person> result = this.client.get()
			.accept(TEXT_EVENT_STREAM)
			.exchange()
			.expectStatus().isOk()
			.expectHeader().contentTypeCompatibleWith(TEXT_EVENT_STREAM)
			.returnResult(Person.class);

	StepVerifier.create(result.getResponseBody())
			.expectNext(new Person("N0"), new Person("N1"), new Person("N2"))
			.expectNextCount(4)
			.consumeNextWith(person -> assertThat(person.getName(), endsWith("7")))
			.thenCancel()
			.verify();
}
 
Example 2
Source Project: java-technology-stack   Source File: ResponseEntityTests.java    License: MIT License 6 votes vote down vote up
@Test
public void entityStream() {

	FluxExchangeResult<Person> result = this.client.get()
			.accept(TEXT_EVENT_STREAM)
			.exchange()
			.expectStatus().isOk()
			.expectHeader().contentTypeCompatibleWith(TEXT_EVENT_STREAM)
			.returnResult(Person.class);

	StepVerifier.create(result.getResponseBody())
			.expectNext(new Person("N0"), new Person("N1"), new Person("N2"))
			.expectNextCount(4)
			.consumeNextWith(person -> assertThat(person.getName(), endsWith("7")))
			.thenCancel()
			.verify();
}
 
Example 3
@Test
public void rateLimiterWorks() {
	WebTestClient authClient = client.mutate()
			.filter(basicAuthentication("user", "password"))
			.build();

	boolean wasLimited = false;

	for (int i = 0; i < 20; i++) {
		FluxExchangeResult<Map> result = authClient.get()
				.uri("/anything/1")
				.header("Host", "www.limited.org")
				.exchange()
				.returnResult(Map.class);
		if (result.getStatus().equals(HttpStatus.TOO_MANY_REQUESTS)) {
			System.out.println("Received result: "+result);
			wasLimited = true;
			break;
		}
	}

	assertThat(wasLimited)
			.as("A HTTP 429 TOO_MANY_REQUESTS was not received")
			.isTrue();

}
 
Example 4
@Test
public void streamAllBlogPostsIntegrationTest() throws Exception {
	FluxExchangeResult<BlogPost> result = this.webTestClient.get()
		.uri("/blogposts")
		.accept(TEXT_EVENT_STREAM)
		.exchange()
		.expectStatus().isOk()
		.expectHeader().contentType(TEXT_EVENT_STREAM)
		.returnResult(BlogPost.class);

	StepVerifier.create(result.getResponseBody())
		.expectNext(expectedBlogPosts.get(0), expectedBlogPosts.get(1))
		.expectNextCount(1)
		.consumeNextWith(blogPost -> assertThat(blogPost.getAuthorId(), endsWith("4")))
		.thenCancel()
		.verify();
}
 
Example 5
@Test
public void streamAllProjectsIntegrationTest() throws Exception {
	FluxExchangeResult<Project> result = this.webTestClient.get()
		.uri("/projects")
		.accept(TEXT_EVENT_STREAM)
		.exchange()
		.expectStatus().isOk()
		.expectHeader().contentType(TEXT_EVENT_STREAM)
		.returnResult(Project.class);

	StepVerifier.create(result.getResponseBody())
		.expectNext(expectedProjects.get(0), expectedProjects.get(1))
		.expectNextCount(1)
		.consumeNextWith(project -> assertThat(project.getName(), endsWith("4")))
		.thenCancel()
		.verify();
}
 
Example 6
@Test
public void whenInitiatesRequest_ThenGetsStream() throws Exception {
    when(rSocketRequester.route("feedMarketData")).thenReturn(requestSpec);
    when(requestSpec.data(any())).thenReturn(requestSpec);
    MarketData firstMarketData = new MarketData("X", 1);
    MarketData secondMarketData = new MarketData("X", 2);
    when(requestSpec.retrieveFlux(MarketData.class)).thenReturn(Flux.just(firstMarketData, secondMarketData));

    FluxExchangeResult<MarketData> result = testClient.get()
                                                      .uri("/feed/{stock}", "X")
                                                      .accept(TEXT_EVENT_STREAM)
                                                      .exchange()
                                                      .expectStatus()
                                                      .isOk()
                                                      .returnResult(MarketData.class);
    Flux<MarketData> marketDataFlux = result.getResponseBody();
    StepVerifier.create(marketDataFlux)
                .expectNext(firstMarketData)
                .expectNext(secondMarketData)
                .thenCancel()
                .verify();
}
 
Example 7
Source Project: Spring-5.0-Cookbook   Source File: TestReactService.java    License: MIT License 5 votes vote down vote up
@Test
  public void testDeptById(){
  		    	
  	FluxExchangeResult<Department> result = webTestClient.get().uri("http://localhost:8090/ch10-dept/selectReactDept/359").accept(MediaType.APPLICATION_JSON_UTF8)
  			.exchange().returnResult(Department.class);
  	assertEquals( result.getStatus().value(), 200);
Department dept = result.getResponseBody().blockFirst();
System.out.println(dept.getName());

  }
 
Example 8
Source Project: Spring-5.0-Cookbook   Source File: TestReactService.java    License: MIT License 5 votes vote down vote up
@Test
  public void testDeptByIdRouter(){
  	
  	
  	FluxExchangeResult<Department> result = webTestClient.get().uri("http://localhost:8901/selectDeptById/359").accept(MediaType.APPLICATION_JSON_UTF8)
  			.exchange().returnResult(Department.class);
  	assertEquals( result.getStatus().value(), 200);
Department dept = result.getResponseBody().blockFirst();
System.out.println(dept.getName());

  }
 
Example 9
Source Project: Spring-5.0-Cookbook   Source File: TestReactService.java    License: MIT License 5 votes vote down vote up
@Test
  public void testDeptById(){
  		    	
  	FluxExchangeResult<Department> result = webTestClient.get().uri("http://localhost:8090/ch10-dept/selectReactDept/359").accept(MediaType.APPLICATION_JSON_UTF8)
  			.exchange().returnResult(Department.class);
  	assertEquals( result.getStatus().value(), 200);
Department dept = result.getResponseBody().blockFirst();
System.out.println(dept.getName());

  }
 
Example 10
Source Project: Spring-5.0-Cookbook   Source File: TestReactService.java    License: MIT License 5 votes vote down vote up
@Test
  public void testDeptByIdRouter(){
  	
  	
  	FluxExchangeResult<Department> result = webTestClient.get().uri("http://localhost:8901/selectDeptById/359").accept(MediaType.APPLICATION_JSON_UTF8)
  			.exchange().returnResult(Department.class);
  	assertEquals( result.getStatus().value(), 200);
Department dept = result.getResponseBody().blockFirst();
System.out.println(dept.getName());

  }
 
Example 11
@Test
public void postCrudOperations() {
    int randomInt = new Random().nextInt();
    String title = "Post test " + randomInt;
    FluxExchangeResult<Void> postResult = client
        .mutate().filter(basicAuthentication("user", "password")).build()
        .post()
        .uri("/posts")
        .body(BodyInserters.fromObject(Post.builder().title(title).content("content of " + title).build()))
        .exchange()
        .expectStatus().isEqualTo(HttpStatus.CREATED)
        .returnResult(Void.class);

    URI location = postResult.getResponseHeaders().getLocation();
    assertNotNull(location);

    EntityExchangeResult<byte[]> getResult = client
        .get()
        .uri(location)
        .exchange()
        .expectStatus().isOk()
        .expectBody().jsonPath("$.title").isEqualTo(title)
        .returnResult();

    String getPost = new String(getResult.getResponseBody());
    assertTrue(getPost.contains(title));
}
 
Example 12
@Test
public void postCrudOperations() {
    int randomInt = new Random().nextInt();
    String title = "Post test " + randomInt;
    FluxExchangeResult<Void> postResult = client
        .mutate().filter(basicAuthentication("user", "password")).build()
        .post()
        .uri("/posts")
        .body(BodyInserters.fromObject(Post.builder().title(title).content("content of " + title).build()))
        .exchange()
        .expectStatus().isEqualTo(HttpStatus.CREATED)
        .returnResult(Void.class);

    URI location = postResult.getResponseHeaders().getLocation();
    assertNotNull(location);

    EntityExchangeResult<byte[]> getResult = client
        .get()
        .uri(location)
        .exchange()
        .expectStatus().isOk()
        .expectBody().jsonPath("$.title").isEqualTo(title)
        .returnResult();

    String getPost = new String(getResult.getResponseBody());
    assertTrue(getPost.contains(title));
}
 
Example 13
@Test
public void userDefinedMappingsSecureByDefault() throws Exception {

    FluxExchangeResult<Map<String, String>> result = this.rest
        .get()
        .uri("/sessionId")
        .header("Authorization", getBasicAuth())
        .exchange()
        .returnResult(new ParameterizedTypeReference<Map<String, String>>() {
        });

    assertThat(result.getStatus()).isEqualTo(HttpStatus.OK);

    List<String> sessionHeaders = result.getResponseHeaders().get("X-SESSION-ID");
    assertThat(sessionHeaders.size()).isEqualTo(1);

    String sessionId = result.getResponseBody().blockFirst().get("id");

    assertThat(sessionHeaders.get(0)).isEqualTo(sessionId);

    this.rest
        .get()
        .uri("/posts")
        .header("X-SESSION-ID", sessionId)
        .exchange()
        .expectStatus().isOk();
}
 
Example 14
@Test
public void postCrudOperations() {
    int randomInt = new Random().nextInt();
    String title = "Post test " + randomInt;
    FluxExchangeResult<Void> postResult = client
        .mutate().filter(basicAuthentication("user", "password")).build()
        .post()
        .uri("/posts")
        .body(BodyInserters.fromObject(Post.builder().title(title).content("content of " + title).build()))
        .exchange()
        .expectStatus().isEqualTo(HttpStatus.CREATED)
        .returnResult(Void.class);

    URI location = postResult.getResponseHeaders().getLocation();
    assertNotNull(location);

    EntityExchangeResult<byte[]> getResult = client
        .get()
        .uri(location)
        .exchange()
        .expectStatus().isOk()
        .expectBody().jsonPath("$.title").isEqualTo(title)
        .returnResult();

    String getPost = new String(getResult.getResponseBody());
    assertTrue(getPost.contains(title));
}
 
Example 15
@Test
public void postCrudOperations() {
    int randomInt = new Random().nextInt();
    String title = "Post test " + randomInt;
    FluxExchangeResult<Void> postResult = client
        .mutate().filter(basicAuthentication("user", "password")).build()
        .post()
        .uri("/posts")
        .body(BodyInserters.fromObject(Post.builder().title(title).content("content of " + title).build()))
        .exchange()
        .expectStatus().isEqualTo(HttpStatus.CREATED)
        .returnResult(Void.class);

    URI location = postResult.getResponseHeaders().getLocation();
    assertNotNull(location);

    EntityExchangeResult<byte[]> getResult = client
        .get()
        .uri(location)
        .exchange()
        .expectStatus().isOk()
        .expectBody().jsonPath("$.title").isEqualTo(title)
        .returnResult();

    String getPost = new String(getResult.getResponseBody());
    assertTrue(getPost.contains(title));
}
 
Example 16
Source Project: kafka-graphs   Source File: GraphIntegrationTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testConnectedComponents() {
    webTestClient
        .post()
        .uri("/import")
        .syncBody(generateCCBody())
        .exchange()
        .expectStatus().isOk()
        .expectBody(Void.class);

    GroupEdgesBySourceRequest prepareRequest = new GroupEdgesBySourceRequest();
    prepareRequest.setAlgorithm(GraphAlgorithmType.wcc);
    prepareRequest.setInitialVerticesTopic("initial-cc-vertices");
    prepareRequest.setInitialEdgesTopic("initial-cc-edges");
    prepareRequest.setVerticesTopic("new-cc-vertices");
    prepareRequest.setEdgesGroupedBySourceTopic("new-cc-edges");
    prepareRequest.setAsync(false);

    webTestClient
        .post()
        .uri("/prepare")
        .contentType(MediaType.APPLICATION_JSON)
        .syncBody(prepareRequest)
        .exchange()
        .expectStatus().isOk()
        .expectBody(Void.class);

    GraphAlgorithmCreateRequest createRequest = new GraphAlgorithmCreateRequest();
    createRequest.setAlgorithm(GraphAlgorithmType.wcc);
    createRequest.setVerticesTopic("new-cc-vertices");
    createRequest.setEdgesGroupedBySourceTopic("new-cc-edges");

    EntityExchangeResult<GraphAlgorithmId> createResponse = webTestClient
        .post()
        .uri("/pregel")
        .contentType(MediaType.APPLICATION_JSON)
        .syncBody(createRequest)
        .exchange()
        .expectStatus().isOk()
        .expectBody(GraphAlgorithmId.class)
        .returnResult();
    String id = createResponse.getResponseBody().getId();

    GraphAlgorithmRunRequest runRequest = new GraphAlgorithmRunRequest();

    EntityExchangeResult<GraphAlgorithmStatus> runResponse = webTestClient
        .post()
        .uri("/pregel/{id}", id)
        .contentType(MediaType.APPLICATION_JSON)
        .syncBody(runRequest)
        .exchange()
        .expectStatus().isOk()
        .expectBody(GraphAlgorithmStatus.class)
        .returnResult();

    GraphAlgorithmState.State state = GraphAlgorithmState.State.RUNNING;
    while (state == GraphAlgorithmState.State.RUNNING) {
        EntityExchangeResult<GraphAlgorithmStatus> statusResponse = webTestClient
            .get()
            .uri("/pregel/{id}", id)
            .exchange()
            .expectStatus().isOk()
            .expectBody(GraphAlgorithmStatus.class)
            .returnResult();
        state = statusResponse.getResponseBody().getState();
    }

    FluxExchangeResult<KeyValue> result = webTestClient
        .get()
        .uri("/pregel/{id}/result", id)
        .accept(MediaType.TEXT_EVENT_STREAM)
        .exchange()
        .expectStatus().isOk()
        .returnResult(KeyValue.class);

    Map<String, String> map = result.getResponseBody().collectMap(KeyValue::getKey, KeyValue::getValue).block();
    for (int i = 0; i < 10; i++) {
        assertEquals("0", map.get(String.valueOf(i)));
    }
    for (int i = 10; i < 21; i++) {
        assertEquals("10", map.get(String.valueOf(i)));
    }
}
 
Example 17
Source Project: kafka-graphs   Source File: GraphIntegrationTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testSvdpp() {
    webTestClient
        .post()
        .uri("/import")
        .syncBody(generateSvdppBody())
        .exchange()
        .expectStatus().isOk()
        .expectBody(Void.class);

    GroupEdgesBySourceRequest prepareRequest = new GroupEdgesBySourceRequest();
    prepareRequest.setAlgorithm(GraphAlgorithmType.svdpp);
    prepareRequest.setInitialEdgesTopic("initial-svdpp-edges");
    prepareRequest.setVerticesTopic("new-svdpp-vertices");
    prepareRequest.setEdgesGroupedBySourceTopic("new-svdpp-edges");
    prepareRequest.setAsync(false);

    webTestClient
        .post()
        .uri("/prepare")
        .contentType(MediaType.APPLICATION_JSON)
        .syncBody(prepareRequest)
        .exchange()
        .expectStatus().isOk()
        .expectBody(Void.class);

    Map<String, String> configs = new HashMap<>();
    configs.put(Svdpp.RANDOM_SEED, "0");
    configs.put(Svdpp.ITERATIONS, "3");
    GraphAlgorithmCreateRequest createRequest = new GraphAlgorithmCreateRequest();
    createRequest.setConfigs(configs);
    createRequest.setAlgorithm(GraphAlgorithmType.svdpp);
    createRequest.setVerticesTopic("new-svdpp-vertices");
    createRequest.setEdgesGroupedBySourceTopic("new-svdpp-edges");

    EntityExchangeResult<GraphAlgorithmId> createResponse = webTestClient
        .post()
        .uri("/pregel")
        .contentType(MediaType.APPLICATION_JSON)
        .syncBody(createRequest)
        .exchange()
        .expectStatus().isOk()
        .expectBody(GraphAlgorithmId.class)
        .returnResult();
    String id = createResponse.getResponseBody().getId();

    GraphAlgorithmRunRequest runRequest = new GraphAlgorithmRunRequest();

    EntityExchangeResult<GraphAlgorithmStatus> runResponse = webTestClient
        .post()
        .uri("/pregel/{id}", id)
        .contentType(MediaType.APPLICATION_JSON)
        .syncBody(runRequest)
        .exchange()
        .expectStatus().isOk()
        .expectBody(GraphAlgorithmStatus.class)
        .returnResult();

    GraphAlgorithmState.State state = GraphAlgorithmState.State.RUNNING;
    while (state == GraphAlgorithmState.State.RUNNING) {
        EntityExchangeResult<GraphAlgorithmStatus> statusResponse = webTestClient
            .get()
            .uri("/pregel/{id}", id)
            .exchange()
            .expectStatus().isOk()
            .expectBody(GraphAlgorithmStatus.class)
            .returnResult();
        state = statusResponse.getResponseBody().getState();
    }

    FluxExchangeResult<KeyValue> result = webTestClient
        .get()
        .uri("/pregel/{id}/result", id)
        .accept(MediaType.TEXT_EVENT_STREAM)
        .exchange()
        .expectStatus().isOk()
        .returnResult(KeyValue.class);

    NavigableMap<CfLongId, String> map = (NavigableMap<CfLongId, String>) result.getResponseBody().collectMap(
        kv -> new CfLongId(kv.getKey()),
        KeyValue::getValue,
        TreeMap::new
    ).block();
    assertEquals("(1, 0)=(0.11611404, [0.006397, 0.008010])", map.firstEntry().toString());
    assertEquals("(20, 1)=(0.6374174, [0.007310, 0.002405])", map.lastEntry().toString());
}