Skip to content

Commit

Permalink
Added suppliers test.
Browse files Browse the repository at this point in the history
  • Loading branch information
sagaofsilence committed May 17, 2024
1 parent 166b177 commit 417a12a
Showing 1 changed file with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.reflectoring.function;

import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.*;
import java.util.stream.DoubleStream;
import java.util.stream.LongStream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

public class SupplierTest {
@Test
void supplier() {
// Supply random numbers
Supplier<Integer> randomNumberSupplier = () -> new Random().nextInt(100);
int result = randomNumberSupplier.get();
Assertions.assertTrue(result >= 0 && result < 100);
}

@Test
void intSupplier() {
IntSupplier nextWinner = () -> new Random().nextInt(100, 200);
int result = nextWinner.getAsInt();
Assertions.assertTrue(result >= 100 && result < 200);
}

@Test
void longSupplier() {
LongSupplier nextWinner = () -> new Random().nextLong(100, 200);
LongStream winners = LongStream.generate(nextWinner).limit(10);
Assertions.assertEquals(10, winners.toArray().length);
}

@Test
void doubleSupplier() {
// Random data for plotting graph
DoubleSupplier weightSupplier = () -> new Random().nextDouble(100, 200);
DoubleStream dataSample = DoubleStream.generate(weightSupplier).limit(10);
Assertions.assertEquals(10, dataSample.toArray().length);
}

@ParameterizedTest
@CsvSource(value = {"ON,true", "OFF,false"})
void booleanSupplier(String statusCode, boolean expected) {
AtomicReference<String> status = new AtomicReference<>();
status.set(statusCode);
// Simulate a service health check
BooleanSupplier isServiceHealthy =
() -> {
// Here, we could check the actual health of a service.
// simplified for test purpose
return status.toString().equals("ON");
};
boolean result = isServiceHealthy.getAsBoolean();
Assertions.assertEquals(expected, result);
}
}

0 comments on commit 417a12a

Please sign in to comment.