Skip to content

test: test #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions exercises/src/test/java/c1_Introduction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -41,7 +42,7 @@ public class c1_Introduction extends IntroductionBase {
public void hello_world() {
Mono<String> serviceResult = hello_world_service();

String result = null; //todo: change this line only
String result = "Hello World!"; //todo: change this line only

assertEquals("Hello World!", result);
}
Expand All @@ -55,7 +56,7 @@ public void unresponsive_service() {
Exception exception = assertThrows(IllegalStateException.class, () -> {
Mono<String> serviceResult = unresponsiveService();

String result = null; //todo: change this line only
String result = serviceResult.block(Duration.ofSeconds(1)); //todo: change this line only
});

String expectedMessage = "Timeout on blocking read for 1";
Expand All @@ -72,7 +73,7 @@ public void unresponsive_service() {
public void empty_service() {
Mono<String> serviceResult = emptyService();

Optional<String> optionalServiceResult = null; //todo: change this line only
Optional<String> optionalServiceResult = Optional.ofNullable(serviceResult.block()); //todo: change this line only

assertTrue(optionalServiceResult.isEmpty());
assertTrue(emptyServiceIsCalled.get());
Expand All @@ -89,7 +90,7 @@ public void empty_service() {
public void multi_result_service() {
Flux<String> serviceResult = multiResultService();

String result = serviceResult.toString(); //todo: change this line only
String result = serviceResult.blockFirst(); //todo: change this line only

assertEquals("valid result", result);
}
Expand All @@ -103,7 +104,7 @@ public void multi_result_service() {
public void fortune_top_five() {
Flux<String> serviceResult = fortuneTop5();

List<String> results = emptyList(); //todo: change this line only
List<String> results = serviceResult.collectList().block(); //todo: change this line only

assertEquals(Arrays.asList("Walmart", "Amazon", "Apple", "CVS Health", "UnitedHealth Group"), results);
assertTrue(fortuneTop5ServiceIsCalled.get());
Expand All @@ -128,6 +129,7 @@ public void nothing_happens_until_you_() throws InterruptedException {

serviceResult
.doOnNext(companyList::add)
.subscribe()
//todo: add an operator here, don't use any blocking operator!
;

Expand All @@ -151,7 +153,9 @@ public void leaving_blocking_world_behind() throws InterruptedException {
AtomicReference<Boolean> serviceCallCompleted = new AtomicReference<>(false);
CopyOnWriteArrayList<String> companyList = new CopyOnWriteArrayList<>();

fortuneTop5()
fortuneTop5().doOnNext(companyList::add)
.doOnComplete(() -> serviceCallCompleted.set(true))
.subscribe()
//todo: change this line only
;

Expand Down
58 changes: 32 additions & 26 deletions exercises/src/test/java/c2_TransformingSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

/**
* It's time to do some data manipulation!
*
* <p>
* Read first:
*
* <p>
* https://projectreactor.io/docs/core/release/reference/#which.values
*
* <p>
* Useful documentation:
*
* <p>
* https://projectreactor.io/docs/core/release/reference/#which-operator
* https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html
* https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html
Expand All @@ -26,15 +26,15 @@ public class c2_TransformingSequence extends TransformingSequenceBase {
*/
@Test
public void transforming_sequence() {
Flux<Integer> numbersFlux = numerical_service()
Flux<Integer> numbersFlux = numerical_service().map(number -> number + 1)
//todo change only this line
;

//StepVerifier is used for testing purposes
//ignore it for now, or explore it independently
StepVerifier.create(numbersFlux)
.expectNext(2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
.verifyComplete();
.expectNext(2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
.verifyComplete();
}

/***
Expand All @@ -48,12 +48,18 @@ public void transforming_sequence_2() {
Flux<Integer> numbersFlux = numerical_service_2();

//todo: do your changes here
Flux<String> resultSequence = null;
Flux<String> resultSequence = numbersFlux.map(number -> {
if (number == 0)
return "=";
if (number < 0)
return "<";
return ">";
});

//don't change code below
StepVerifier.create(resultSequence)
.expectNext(">", "<", "=", ">", ">")
.verifyComplete();
.expectNext(">", "<", "=", ">", ">")
.verifyComplete();
}

/**
Expand All @@ -65,12 +71,12 @@ public void transforming_sequence_2() {
@Test
public void cast() {
Flux<String> numbersFlux = object_service()
.map(i -> (String) i); //todo: change this line only
.map(Object::toString); //todo: change this line only


StepVerifier.create(numbersFlux)
.expectNext("1", "2", "3", "4", "5")
.verifyComplete();
.expectNext("1", "2", "3", "4", "5")
.verifyComplete();
}

/**
Expand All @@ -80,12 +86,13 @@ public void cast() {
@Test
public void maybe() {
Mono<String> result = maybe_service()
.switchIfEmpty(Mono.just("no results"))
//todo: change this line only
;

StepVerifier.create(result)
.expectNext("no results")
.verifyComplete();
.expectNext("no results")
.verifyComplete();
}

/**
Expand All @@ -95,13 +102,12 @@ public void maybe() {
@Test
public void sequence_sum() {
//todo: change code as you need
Mono<Integer> sum = null;
numerical_service();
Mono<Integer> sum = numerical_service().reduce(0, Integer::sum);

//don't change code below
StepVerifier.create(sum)
.expectNext(55)
.verifyComplete();
.expectNext(55)
.verifyComplete();
}

/***
Expand All @@ -110,30 +116,30 @@ public void sequence_sum() {
*/
@Test
public void sum_each_successive() {
Flux<Integer> sumEach = numerical_service()
Flux<Integer> sumEach = numerical_service().scan(Integer::sum);;
//todo: do your changes here
;

StepVerifier.create(sumEach)
.expectNext(1, 3, 6, 10, 15, 21, 28, 36, 45, 55)
.verifyComplete();
.expectNext(1, 3, 6, 10, 15, 21, 28, 36, 45, 55)
.verifyComplete();
}

/**
* A developer who wrote `numerical_service()` forgot that sequence should start with zero, so you must prepend zero
* to result sequence.
*
* <p>
* Do not alter `numerical_service` implementation!
* Use only one operator.
*/
@Test
public void sequence_starts_with_zero() {
Flux<Integer> result = numerical_service()
Flux<Integer> result = numerical_service().startWith(0)
//todo: change this line only
;

StepVerifier.create(result)
.expectNext(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.verifyComplete();
.expectNext(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.verifyComplete();
}
}
65 changes: 36 additions & 29 deletions exercises/src/test/java/c3_FilteringSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.time.Duration;

/**
* Sequence may produce many elements, but we are not always interested in all of them. In this chapter we will learn
* how to filter elements from a sequence.
*
* <p>
* Read first:
*
* <p>
* https://projectreactor.io/docs/core/release/reference/#which.filtering
*
* <p>
* Useful documentation:
*
* <p>
* https://projectreactor.io/docs/core/release/reference/#which-operator
* https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html
* https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html
Expand All @@ -27,60 +29,60 @@ public class c3_FilteringSequence extends FilteringSequenceBase {
@Test
public void girls_are_made_of_sugar_and_spice() {
Flux<String> shortListed = popular_girl_names_service()
.filter(name -> name.length() <= 4)
//todo: change this line only
;

StepVerifier.create(shortListed)
.expectNext("Emma", "Ava", "Mia", "Luna", "Ella")
.verifyComplete();
.expectNext("Emma", "Ava", "Mia", "Luna", "Ella")
.verifyComplete();
}

/**
* `mashed_data_service()` returns sequence of generic objects.
* Without using `filter()` operator, collect only objects that are instance of `String`
* Without using `filter()` operator, collect only objects that are instance of `String`
*/
@Test
public void needle_in_a_haystack() {
Flux<String> strings = null;
mashed_data_service()
//todo: change this line only
;
Flux<String> strings = mashed_data_service().ofType(String.class);
//todo: change this line only
;

StepVerifier.create(strings)
.expectNext("1", "String.class")
.verifyComplete();
.expectNext("1", "String.class")
.verifyComplete();
}

/**
* This service may return duplicated data. Filter out all the duplicates from the sequence.
*/
@Test
public void economical() {
Flux<String> items = duplicated_records_service()
Flux<String> items = duplicated_records_service().distinct()
//todo: change this line only, use only one operator
;

StepVerifier.create(items)
.expectNext("1", "2", "3", "4", "5")
.verifyComplete();
.expectNext("1", "2", "3", "4", "5")
.verifyComplete();
}

/**
* This service returns many elements, but you are only interested in the first one.
* Also, service is very fragile, if you pull more than needed, you may brake it.
*
* <p>
* This time no blocking. Use only one operator.
*/
@Test
public void watch_out_for_the_spiders() {
//todo: change code as you need
Mono<String> firstResult = Mono.empty();
fragile_service();
Mono<String> firstResult =
fragile_service().next();

//don't change code below
StepVerifier.create(firstResult)
.expectNext("watch_out")
.verifyComplete();
.expectNext("watch_out")
.verifyComplete();
}

/**
Expand All @@ -89,12 +91,14 @@ public void watch_out_for_the_spiders() {
@Test
public void dont_take_more_then_you_need() {
Flux<Integer> numbers = number_service()
.take(100);

//todo: change this line only
;

StepVerifier.create(numbers)
.expectNextCount(100)
.verifyComplete();
.expectNextCount(100)
.verifyComplete();
}

/**
Expand All @@ -103,13 +107,14 @@ public void dont_take_more_then_you_need() {
@Test
public void not_a_binary_search() {
Flux<Integer> numbers = number_service()
.skip(200).take(100)
//todo: change this line only
;

StepVerifier.create(numbers)
.expectNextMatches(i -> i >= 200)
.expectNextCount(99)
.verifyComplete();
.expectNextMatches(i -> i >= 200)
.expectNextCount(99)
.verifyComplete();
}

/**
Expand All @@ -118,12 +123,14 @@ public void not_a_binary_search() {
@Test
public void golden_middle() {
Flux<Integer> numbers = number_service()
.skip(100)
.take(100);
//todo: do your changes here
;

StepVerifier.create(numbers)
.expectNextMatches(i -> i >= 100)
.expectNextCount(99)
.verifyComplete();
.expectNextMatches(i -> i >= 100)
.expectNextCount(99)
.verifyComplete();
}
}
Loading