From 487557faf23383906909830070dc2c71cd32f628 Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Wed, 4 Jul 2018 12:32:22 +0200 Subject: [PATCH 1/8] A short example of real-time event streaming using Spring Webflux --- .../client/CpuUsageEventConsumer.java | 31 ++++++++++++++++++ .../controller/CpuUsageEventProducer.java | 24 ++++++++++++++ .../reactive/model/CpuUsageEvent.java | 17 ++++++++++ .../com/baeldung/reactive/utils/CpuUtils.java | 14 ++++++++ .../CpuUsageEventProducerIntegrationTest.java | 32 +++++++++++++++++++ .../baeldung/reactive/utils/CpuUtilsTest.java | 24 ++++++++++++++ 6 files changed, 142 insertions(+) create mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/client/CpuUsageEventConsumer.java create mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/controller/CpuUsageEventProducer.java create mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/model/CpuUsageEvent.java create mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/utils/CpuUtils.java create mode 100644 spring-5-reactive/src/test/java/com/baeldung/reactive/controller/CpuUsageEventProducerIntegrationTest.java create mode 100644 spring-5-reactive/src/test/java/com/baeldung/reactive/utils/CpuUtilsTest.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/client/CpuUsageEventConsumer.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/client/CpuUsageEventConsumer.java new file mode 100644 index 0000000000..3b9fbf7838 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/client/CpuUsageEventConsumer.java @@ -0,0 +1,31 @@ +package com.baeldung.reactive.client; + +import com.baeldung.reactive.model.CpuUsageEvent; +import org.springframework.boot.CommandLineRunner; +import org.springframework.context.annotation.Bean; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.function.client.WebClient; + +/** + * @author Krzysztof Majewski + */ +@Component +public class CpuUsageEventConsumer { + + @Bean + WebClient client() { + return WebClient.create("http://localhost:8080"); + } + + @Bean + CommandLineRunner eventsConsumer(WebClient client) { + return args -> client.get() + .uri("/events/stream") + .accept(MediaType.TEXT_EVENT_STREAM) + .exchange() + .flatMapMany(cr -> cr.bodyToFlux(CpuUsageEvent.class)) + .subscribe(System.out::println); + } + +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/CpuUsageEventProducer.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/CpuUsageEventProducer.java new file mode 100644 index 0000000000..5271debb84 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/CpuUsageEventProducer.java @@ -0,0 +1,24 @@ +package com.baeldung.reactive.controller; + +import com.baeldung.reactive.model.CpuUsageEvent; +import com.baeldung.reactive.utils.CpuUtils; +import java.time.Duration; +import java.time.LocalDateTime; +import java.util.stream.Stream; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Flux; +import reactor.util.function.Tuple2; + +@RestController +public class CpuUsageEventProducer { + + @GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE, value = "/events/stream") + Flux events() { + Flux eventFlux = Flux + .fromStream(Stream.generate(() -> new CpuUsageEvent(CpuUtils.getUsage(), LocalDateTime.now()))); + Flux durationFlux = Flux.interval(Duration.ofSeconds(5)); + return Flux.zip(eventFlux, durationFlux).map(Tuple2::getT1); + } +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/model/CpuUsageEvent.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/model/CpuUsageEvent.java new file mode 100644 index 0000000000..1ad41be65c --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/model/CpuUsageEvent.java @@ -0,0 +1,17 @@ +package com.baeldung.reactive.model; + +import java.time.LocalDateTime; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.ToString; + +@Data +@ToString +@AllArgsConstructor +public class CpuUsageEvent { + + private Double cpuUsage; + + private LocalDateTime time; + +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/utils/CpuUtils.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/utils/CpuUtils.java new file mode 100644 index 0000000000..c70aad9d2e --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/utils/CpuUtils.java @@ -0,0 +1,14 @@ +package com.baeldung.reactive.utils; + +import com.sun.management.OperatingSystemMXBean; +import java.lang.management.ManagementFactory; + +public class CpuUtils { + + private static OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); + + public static Double getUsage() { + return (operatingSystemMXBean.getSystemCpuLoad() / operatingSystemMXBean.getAvailableProcessors()) * 100; + } + +} diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/controller/CpuUsageEventProducerIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/controller/CpuUsageEventProducerIntegrationTest.java new file mode 100644 index 0000000000..81897cd8d6 --- /dev/null +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/controller/CpuUsageEventProducerIntegrationTest.java @@ -0,0 +1,32 @@ +package com.baeldung.reactive.controller; + +import com.baeldung.reactive.Spring5ReactiveApplication; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.reactive.server.WebTestClient; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Spring5ReactiveApplication.class) +public class CpuUsageEventProducerIntegrationTest { + + @Autowired + private WebTestClient webTestClient; + + @Test + public void whenGetCpuUsageEvents_thenReturns200() { + webTestClient.get() + .uri("/events/stream") + .accept(MediaType.TEXT_EVENT_STREAM) + .exchange() + .expectStatus().isOk() + .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8) + .expectBody() + .jsonPath("$.cpuUsage").isNotEmpty() + .jsonPath("$.time").isNotEmpty(); + } + +} diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/utils/CpuUtilsTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/utils/CpuUtilsTest.java new file mode 100644 index 0000000000..a36f9c8528 --- /dev/null +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/utils/CpuUtilsTest.java @@ -0,0 +1,24 @@ +package com.baeldung.reactive.utils; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + +import com.baeldung.reactive.Spring5ReactiveApplication; +import org.hamcrest.Matchers; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Spring5ReactiveApplication.class) +public class CpuUtilsTest { + + @Test + public void whenGetUsage_returnCorrectValue() { + Double usage = CpuUtils.getUsage(); + assertNotNull(usage); + assertThat(usage, Matchers.lessThanOrEqualTo(100d)); + } + +} From c9a9a9614d44dc244183fe40c63eacfe145f0783 Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Thu, 19 Jul 2018 10:03:07 +0200 Subject: [PATCH 2/8] BAEL-1912 --- .../com/baeldung/random/RandomNumberTest.kt | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/random/RandomNumberTest.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomNumberTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomNumberTest.kt new file mode 100644 index 0000000000..ac851c97d2 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomNumberTest.kt @@ -0,0 +1,45 @@ + +import org.junit.jupiter.api.Test +import java.util.concurrent.ThreadLocalRandom + +class RandomNumberTest { + + @Test + fun givenUsingJavaUtilMath_whenGeneratingRandomNumber_thenCorrect() { + val randomNumber = Math.random() + println(randomNumber) + } + + @Test + fun givenUsingThreadLocalRandom_whenGeneratingRandomNumber_thenCorrect() { + val randomDouble = ThreadLocalRandom.current().nextDouble() + val randomInteger = ThreadLocalRandom.current().nextInt() + val randomLong = ThreadLocalRandom.current().nextLong() + println(randomDouble) + println(randomInteger) + println(randomLong) + } + + @Test + fun givenUsingKotlinJsMath_whenGeneratingRandomNumber_thenCorrect() { + val randomDouble = Math.random() + println(randomDouble) + } + + @Test + fun givenUsingKotlinNumberRange_whenGeneratingRandomNumberInGivenRange_thenCorrect() { + val randomInteger = (1..12).shuffled().first() + println(randomInteger) + } + + @Test + fun givenUsingThreadLocalRandom_whenGeneratingRandomNumberInGivenRange_thenCorrect() { + val randomDouble = ThreadLocalRandom.current().nextDouble(1.0, 10.0) + val randomInteger = ThreadLocalRandom.current().nextInt(1, 10) + val randomLong = ThreadLocalRandom.current().nextLong(1, 10) + println(randomDouble) + println(randomInteger) + println(randomLong) + } + +} \ No newline at end of file From 1a7cb15c7156f10896abde94de2424d3ce338fbe Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Wed, 4 Jul 2018 12:32:22 +0200 Subject: [PATCH 3/8] A short example of real-time event streaming using Spring Webflux --- .../client/CpuUsageEventConsumer.java | 31 ++++++++++++++++++ .../controller/CpuUsageEventProducer.java | 24 ++++++++++++++ .../reactive/model/CpuUsageEvent.java | 17 ++++++++++ .../com/baeldung/reactive/utils/CpuUtils.java | 14 ++++++++ .../CpuUsageEventProducerIntegrationTest.java | 32 +++++++++++++++++++ .../baeldung/reactive/utils/CpuUtilsTest.java | 24 ++++++++++++++ 6 files changed, 142 insertions(+) create mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/client/CpuUsageEventConsumer.java create mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/controller/CpuUsageEventProducer.java create mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/model/CpuUsageEvent.java create mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/utils/CpuUtils.java create mode 100644 spring-5-reactive/src/test/java/com/baeldung/reactive/controller/CpuUsageEventProducerIntegrationTest.java create mode 100644 spring-5-reactive/src/test/java/com/baeldung/reactive/utils/CpuUtilsTest.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/client/CpuUsageEventConsumer.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/client/CpuUsageEventConsumer.java new file mode 100644 index 0000000000..3b9fbf7838 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/client/CpuUsageEventConsumer.java @@ -0,0 +1,31 @@ +package com.baeldung.reactive.client; + +import com.baeldung.reactive.model.CpuUsageEvent; +import org.springframework.boot.CommandLineRunner; +import org.springframework.context.annotation.Bean; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.function.client.WebClient; + +/** + * @author Krzysztof Majewski + */ +@Component +public class CpuUsageEventConsumer { + + @Bean + WebClient client() { + return WebClient.create("http://localhost:8080"); + } + + @Bean + CommandLineRunner eventsConsumer(WebClient client) { + return args -> client.get() + .uri("/events/stream") + .accept(MediaType.TEXT_EVENT_STREAM) + .exchange() + .flatMapMany(cr -> cr.bodyToFlux(CpuUsageEvent.class)) + .subscribe(System.out::println); + } + +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/CpuUsageEventProducer.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/CpuUsageEventProducer.java new file mode 100644 index 0000000000..5271debb84 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/CpuUsageEventProducer.java @@ -0,0 +1,24 @@ +package com.baeldung.reactive.controller; + +import com.baeldung.reactive.model.CpuUsageEvent; +import com.baeldung.reactive.utils.CpuUtils; +import java.time.Duration; +import java.time.LocalDateTime; +import java.util.stream.Stream; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Flux; +import reactor.util.function.Tuple2; + +@RestController +public class CpuUsageEventProducer { + + @GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE, value = "/events/stream") + Flux events() { + Flux eventFlux = Flux + .fromStream(Stream.generate(() -> new CpuUsageEvent(CpuUtils.getUsage(), LocalDateTime.now()))); + Flux durationFlux = Flux.interval(Duration.ofSeconds(5)); + return Flux.zip(eventFlux, durationFlux).map(Tuple2::getT1); + } +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/model/CpuUsageEvent.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/model/CpuUsageEvent.java new file mode 100644 index 0000000000..1ad41be65c --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/model/CpuUsageEvent.java @@ -0,0 +1,17 @@ +package com.baeldung.reactive.model; + +import java.time.LocalDateTime; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.ToString; + +@Data +@ToString +@AllArgsConstructor +public class CpuUsageEvent { + + private Double cpuUsage; + + private LocalDateTime time; + +} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/utils/CpuUtils.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/utils/CpuUtils.java new file mode 100644 index 0000000000..c70aad9d2e --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/utils/CpuUtils.java @@ -0,0 +1,14 @@ +package com.baeldung.reactive.utils; + +import com.sun.management.OperatingSystemMXBean; +import java.lang.management.ManagementFactory; + +public class CpuUtils { + + private static OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); + + public static Double getUsage() { + return (operatingSystemMXBean.getSystemCpuLoad() / operatingSystemMXBean.getAvailableProcessors()) * 100; + } + +} diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/controller/CpuUsageEventProducerIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/controller/CpuUsageEventProducerIntegrationTest.java new file mode 100644 index 0000000000..81897cd8d6 --- /dev/null +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/controller/CpuUsageEventProducerIntegrationTest.java @@ -0,0 +1,32 @@ +package com.baeldung.reactive.controller; + +import com.baeldung.reactive.Spring5ReactiveApplication; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.reactive.server.WebTestClient; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Spring5ReactiveApplication.class) +public class CpuUsageEventProducerIntegrationTest { + + @Autowired + private WebTestClient webTestClient; + + @Test + public void whenGetCpuUsageEvents_thenReturns200() { + webTestClient.get() + .uri("/events/stream") + .accept(MediaType.TEXT_EVENT_STREAM) + .exchange() + .expectStatus().isOk() + .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8) + .expectBody() + .jsonPath("$.cpuUsage").isNotEmpty() + .jsonPath("$.time").isNotEmpty(); + } + +} diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/utils/CpuUtilsTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/utils/CpuUtilsTest.java new file mode 100644 index 0000000000..a36f9c8528 --- /dev/null +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/utils/CpuUtilsTest.java @@ -0,0 +1,24 @@ +package com.baeldung.reactive.utils; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + +import com.baeldung.reactive.Spring5ReactiveApplication; +import org.hamcrest.Matchers; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Spring5ReactiveApplication.class) +public class CpuUtilsTest { + + @Test + public void whenGetUsage_returnCorrectValue() { + Double usage = CpuUtils.getUsage(); + assertNotNull(usage); + assertThat(usage, Matchers.lessThanOrEqualTo(100d)); + } + +} From 182a8837429708eada10f310c2ee23ee3c684f48 Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Thu, 19 Jul 2018 10:03:07 +0200 Subject: [PATCH 4/8] BAEL-1912 --- .../com/baeldung/random/RandomNumberTest.kt | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/random/RandomNumberTest.kt diff --git a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomNumberTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomNumberTest.kt new file mode 100644 index 0000000000..ac851c97d2 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomNumberTest.kt @@ -0,0 +1,45 @@ + +import org.junit.jupiter.api.Test +import java.util.concurrent.ThreadLocalRandom + +class RandomNumberTest { + + @Test + fun givenUsingJavaUtilMath_whenGeneratingRandomNumber_thenCorrect() { + val randomNumber = Math.random() + println(randomNumber) + } + + @Test + fun givenUsingThreadLocalRandom_whenGeneratingRandomNumber_thenCorrect() { + val randomDouble = ThreadLocalRandom.current().nextDouble() + val randomInteger = ThreadLocalRandom.current().nextInt() + val randomLong = ThreadLocalRandom.current().nextLong() + println(randomDouble) + println(randomInteger) + println(randomLong) + } + + @Test + fun givenUsingKotlinJsMath_whenGeneratingRandomNumber_thenCorrect() { + val randomDouble = Math.random() + println(randomDouble) + } + + @Test + fun givenUsingKotlinNumberRange_whenGeneratingRandomNumberInGivenRange_thenCorrect() { + val randomInteger = (1..12).shuffled().first() + println(randomInteger) + } + + @Test + fun givenUsingThreadLocalRandom_whenGeneratingRandomNumberInGivenRange_thenCorrect() { + val randomDouble = ThreadLocalRandom.current().nextDouble(1.0, 10.0) + val randomInteger = ThreadLocalRandom.current().nextInt(1, 10) + val randomLong = ThreadLocalRandom.current().nextLong(1, 10) + println(randomDouble) + println(randomInteger) + println(randomLong) + } + +} \ No newline at end of file From 760ff1561b1fc2e6a98f662433e6c46e3c6efafd Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Thu, 19 Jul 2018 10:27:30 +0200 Subject: [PATCH 5/8] remove --- .../client/CpuUsageEventConsumer.java | 31 ------------------ .../controller/CpuUsageEventProducer.java | 24 -------------- .../reactive/model/CpuUsageEvent.java | 17 ---------- .../com/baeldung/reactive/utils/CpuUtils.java | 14 -------- .../CpuUsageEventProducerIntegrationTest.java | 32 ------------------- .../baeldung/reactive/utils/CpuUtilsTest.java | 24 -------------- 6 files changed, 142 deletions(-) delete mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/client/CpuUsageEventConsumer.java delete mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/controller/CpuUsageEventProducer.java delete mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/model/CpuUsageEvent.java delete mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/utils/CpuUtils.java delete mode 100644 spring-5-reactive/src/test/java/com/baeldung/reactive/controller/CpuUsageEventProducerIntegrationTest.java delete mode 100644 spring-5-reactive/src/test/java/com/baeldung/reactive/utils/CpuUtilsTest.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/client/CpuUsageEventConsumer.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/client/CpuUsageEventConsumer.java deleted file mode 100644 index 3b9fbf7838..0000000000 --- a/spring-5-reactive/src/main/java/com/baeldung/reactive/client/CpuUsageEventConsumer.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.baeldung.reactive.client; - -import com.baeldung.reactive.model.CpuUsageEvent; -import org.springframework.boot.CommandLineRunner; -import org.springframework.context.annotation.Bean; -import org.springframework.http.MediaType; -import org.springframework.stereotype.Component; -import org.springframework.web.reactive.function.client.WebClient; - -/** - * @author Krzysztof Majewski - */ -@Component -public class CpuUsageEventConsumer { - - @Bean - WebClient client() { - return WebClient.create("http://localhost:8080"); - } - - @Bean - CommandLineRunner eventsConsumer(WebClient client) { - return args -> client.get() - .uri("/events/stream") - .accept(MediaType.TEXT_EVENT_STREAM) - .exchange() - .flatMapMany(cr -> cr.bodyToFlux(CpuUsageEvent.class)) - .subscribe(System.out::println); - } - -} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/CpuUsageEventProducer.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/CpuUsageEventProducer.java deleted file mode 100644 index 5271debb84..0000000000 --- a/spring-5-reactive/src/main/java/com/baeldung/reactive/controller/CpuUsageEventProducer.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.baeldung.reactive.controller; - -import com.baeldung.reactive.model.CpuUsageEvent; -import com.baeldung.reactive.utils.CpuUtils; -import java.time.Duration; -import java.time.LocalDateTime; -import java.util.stream.Stream; -import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; -import reactor.core.publisher.Flux; -import reactor.util.function.Tuple2; - -@RestController -public class CpuUsageEventProducer { - - @GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE, value = "/events/stream") - Flux events() { - Flux eventFlux = Flux - .fromStream(Stream.generate(() -> new CpuUsageEvent(CpuUtils.getUsage(), LocalDateTime.now()))); - Flux durationFlux = Flux.interval(Duration.ofSeconds(5)); - return Flux.zip(eventFlux, durationFlux).map(Tuple2::getT1); - } -} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/model/CpuUsageEvent.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/model/CpuUsageEvent.java deleted file mode 100644 index 1ad41be65c..0000000000 --- a/spring-5-reactive/src/main/java/com/baeldung/reactive/model/CpuUsageEvent.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.reactive.model; - -import java.time.LocalDateTime; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.ToString; - -@Data -@ToString -@AllArgsConstructor -public class CpuUsageEvent { - - private Double cpuUsage; - - private LocalDateTime time; - -} diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/utils/CpuUtils.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/utils/CpuUtils.java deleted file mode 100644 index c70aad9d2e..0000000000 --- a/spring-5-reactive/src/main/java/com/baeldung/reactive/utils/CpuUtils.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.reactive.utils; - -import com.sun.management.OperatingSystemMXBean; -import java.lang.management.ManagementFactory; - -public class CpuUtils { - - private static OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); - - public static Double getUsage() { - return (operatingSystemMXBean.getSystemCpuLoad() / operatingSystemMXBean.getAvailableProcessors()) * 100; - } - -} diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/controller/CpuUsageEventProducerIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/controller/CpuUsageEventProducerIntegrationTest.java deleted file mode 100644 index 81897cd8d6..0000000000 --- a/spring-5-reactive/src/test/java/com/baeldung/reactive/controller/CpuUsageEventProducerIntegrationTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.reactive.controller; - -import com.baeldung.reactive.Spring5ReactiveApplication; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.MediaType; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.web.reactive.server.WebTestClient; - -@RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Spring5ReactiveApplication.class) -public class CpuUsageEventProducerIntegrationTest { - - @Autowired - private WebTestClient webTestClient; - - @Test - public void whenGetCpuUsageEvents_thenReturns200() { - webTestClient.get() - .uri("/events/stream") - .accept(MediaType.TEXT_EVENT_STREAM) - .exchange() - .expectStatus().isOk() - .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8) - .expectBody() - .jsonPath("$.cpuUsage").isNotEmpty() - .jsonPath("$.time").isNotEmpty(); - } - -} diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/utils/CpuUtilsTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/utils/CpuUtilsTest.java deleted file mode 100644 index a36f9c8528..0000000000 --- a/spring-5-reactive/src/test/java/com/baeldung/reactive/utils/CpuUtilsTest.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.baeldung.reactive.utils; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; - -import com.baeldung.reactive.Spring5ReactiveApplication; -import org.hamcrest.Matchers; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Spring5ReactiveApplication.class) -public class CpuUtilsTest { - - @Test - public void whenGetUsage_returnCorrectValue() { - Double usage = CpuUtils.getUsage(); - assertNotNull(usage); - assertThat(usage, Matchers.lessThanOrEqualTo(100d)); - } - -} From 9b05faa15f333e1022fb276fb888fb8ba7d5c2b2 Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Mon, 23 Jul 2018 07:54:10 +0200 Subject: [PATCH 6/8] BAEL-1912 fix --- .../com/baeldung/random/RandomNumberTest.kt | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomNumberTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomNumberTest.kt index ac851c97d2..2956a35f8a 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/random/RandomNumberTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/random/RandomNumberTest.kt @@ -1,45 +1,55 @@ import org.junit.jupiter.api.Test import java.util.concurrent.ThreadLocalRandom +import kotlin.test.assertTrue class RandomNumberTest { @Test - fun givenUsingJavaUtilMath_whenGeneratingRandomNumber_thenCorrect() { + fun whenRandomNumberWithJavaUtilMath_thenResultIsBetween0And1() { val randomNumber = Math.random() - println(randomNumber) + assertTrue { randomNumber >=0 } + assertTrue { randomNumber <= 1 } } @Test - fun givenUsingThreadLocalRandom_whenGeneratingRandomNumber_thenCorrect() { + fun whenRandomNumberWithJavaThreadLocalRandom_thenResultsInDefaultRanges() { val randomDouble = ThreadLocalRandom.current().nextDouble() val randomInteger = ThreadLocalRandom.current().nextInt() val randomLong = ThreadLocalRandom.current().nextLong() - println(randomDouble) - println(randomInteger) - println(randomLong) + assertTrue { randomDouble >= 0 } + assertTrue { randomDouble <= 1 } + assertTrue { randomInteger >= Integer.MIN_VALUE } + assertTrue { randomInteger <= Integer.MAX_VALUE } + assertTrue { randomLong >= Long.MIN_VALUE } + assertTrue { randomLong <= Long.MAX_VALUE } } @Test - fun givenUsingKotlinJsMath_whenGeneratingRandomNumber_thenCorrect() { + fun whenRandomNumberWithKotlinJSMath_thenResultIsBetween0And1() { val randomDouble = Math.random() - println(randomDouble) + assertTrue { randomDouble >=0 } + assertTrue { randomDouble <= 1 } } @Test - fun givenUsingKotlinNumberRange_whenGeneratingRandomNumberInGivenRange_thenCorrect() { + fun whenRandomNumberWithKotlinNumberRange_thenResultInGivenRange() { val randomInteger = (1..12).shuffled().first() - println(randomInteger) + assertTrue { randomInteger >= 1 } + assertTrue { randomInteger <= 12 } } @Test - fun givenUsingThreadLocalRandom_whenGeneratingRandomNumberInGivenRange_thenCorrect() { + fun whenRandomNumberWithJavaThreadLocalRandom_thenResultsInGivenRanges() { val randomDouble = ThreadLocalRandom.current().nextDouble(1.0, 10.0) val randomInteger = ThreadLocalRandom.current().nextInt(1, 10) val randomLong = ThreadLocalRandom.current().nextLong(1, 10) - println(randomDouble) - println(randomInteger) - println(randomLong) + assertTrue { randomDouble >= 1 } + assertTrue { randomDouble <= 10 } + assertTrue { randomInteger >= 1 } + assertTrue { randomInteger <= 10 } + assertTrue { randomLong >= 1 } + assertTrue { randomLong <= 10 } } } \ No newline at end of file From 711a352d7fab7ebc57884e57902b20727b303ff6 Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Fri, 10 Aug 2018 09:31:34 +0200 Subject: [PATCH 7/8] merge --- .../java/com/baeldung/reactive/util/CpuUtils.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 spring-5-reactive/src/main/java/com/baeldung/reactive/util/CpuUtils.java diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/util/CpuUtils.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/util/CpuUtils.java new file mode 100644 index 0000000000..8d16434920 --- /dev/null +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/util/CpuUtils.java @@ -0,0 +1,15 @@ +package com.baeldung.reactive.util; + +import com.sun.management.OperatingSystemMXBean; + +import java.lang.management.ManagementFactory; + +public class CpuUtils { + + private static OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); + + static Double getUsage() { + return (operatingSystemMXBean.getSystemCpuLoad() / operatingSystemMXBean.getAvailableProcessors()) * 100; + } + +} From 9ea9b7ae72243a02f9d59adc8c398a37ba77d24b Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Fri, 10 Aug 2018 10:05:51 +0200 Subject: [PATCH 8/8] BAEL-2056 --- .../java/collections/CollectionsEmpty.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 core-java-collections/src/test/java/org/baeldung/java/collections/CollectionsEmpty.java diff --git a/core-java-collections/src/test/java/org/baeldung/java/collections/CollectionsEmpty.java b/core-java-collections/src/test/java/org/baeldung/java/collections/CollectionsEmpty.java new file mode 100644 index 0000000000..09ecebe47b --- /dev/null +++ b/core-java-collections/src/test/java/org/baeldung/java/collections/CollectionsEmpty.java @@ -0,0 +1,26 @@ +package org.baeldung.java.collections; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.junit.Assert; +import org.junit.Test; + +class CollectionsEmpty { + + @Test + public void givenArrayList_whenAddingElement_addsNewElement() { + ArrayList mutableList = new ArrayList<>(); + mutableList.add("test"); + + Assert.assertEquals(mutableList.size(), 1); + Assert.assertEquals(mutableList.get(0), "test"); + } + + @Test(expected = UnsupportedOperationException.class) + public void givenCollectionsEmptyList_whenAddingElement_throwsUnsupportedOperationException() { + List immutableList = Collections.emptyList(); + immutableList.add("test"); + } + +}