use of io.vavr.collection.Map in project tutorials by eugenp.
the class CollectionAPIUnitTest method givenParams_whenListAPI_thenCorrect.
@Test
public void givenParams_whenListAPI_thenCorrect() {
List<String> list = List.of("Java", "PHP", "Jquery", "JavaScript", "JShell", "JAVA");
List list1 = list.drop(2);
assertFalse(list1.contains("Java") && list1.contains("PHP"));
List list2 = list.dropRight(2);
assertFalse(list2.contains("JAVA") && list2.contains("JShell"));
List list3 = list.dropUntil(s -> s.contains("Shell"));
assertEquals(list3.size(), 2);
List list4 = list.dropWhile(s -> s.length() > 0);
assertTrue(list4.isEmpty());
List list5 = list.take(1);
assertEquals(list5.single(), "Java");
List list6 = list.takeRight(1);
assertEquals(list6.single(), "JAVA");
List list7 = list.takeUntil(s -> s.length() > 6);
assertEquals(list7.size(), 3);
List list8 = list.distinctBy((s1, s2) -> s1.startsWith(s2.charAt(0) + "") ? 0 : 1);
assertEquals(list8.size(), 2);
Iterator<List<String>> iterator = list.grouped(2);
assertEquals(iterator.head().size(), 2);
Map<Boolean, List<String>> map = list.groupBy(e -> e.startsWith("J"));
assertEquals(map.size(), 2);
assertEquals(map.get(false).get().size(), 1);
assertEquals(map.get(true).get().size(), 5);
String words = List.of("Boys", "Girls").intersperse("and").reduce((s1, s2) -> s1.concat(" " + s2)).trim();
assertEquals(words, "Boys and Girls");
}
use of io.vavr.collection.Map in project resilience4j by resilience4j.
the class CircuitBreakerExportsTest method testExportsCircuitBreakerStates.
@Test
public void testExportsCircuitBreakerStates() {
// Given
final CollectorRegistry registry = new CollectorRegistry();
final CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("foo");
CircuitBreakerExports.ofIterable("boo_circuit_breaker", singletonList(circuitBreaker)).register(registry);
final Supplier<Map<String, Double>> values = () -> HashSet.of("closed", "open", "half_open").map(state -> Tuple.of(state, registry.getSampleValue("boo_circuit_breaker_states", new String[] { "name", "state" }, new String[] { "foo", state }))).toMap(t -> t);
// When
final Map<String, Double> closedStateValues = values.get();
circuitBreaker.transitionToOpenState();
final Map<String, Double> openStateValues = values.get();
circuitBreaker.transitionToHalfOpenState();
final Map<String, Double> halfOpenStateValues = values.get();
circuitBreaker.transitionToClosedState();
final Map<String, Double> closedStateValues2 = values.get();
// Then
assertThat(closedStateValues).isEqualTo(HashMap.of("closed", 1.0, "open", 0.0, "half_open", 0.0));
assertThat(openStateValues).isEqualTo(HashMap.of("closed", 0.0, "open", 1.0, "half_open", 0.0));
assertThat(halfOpenStateValues).isEqualTo(HashMap.of("closed", 0.0, "open", 0.0, "half_open", 1.0));
assertThat(closedStateValues2).isEqualTo(HashMap.of("closed", 1.0, "open", 0.0, "half_open", 0.0));
}
use of io.vavr.collection.Map in project jdbi by jdbi.
the class TestVavrMapCollectorWithDB method uniqueIndex.
/**
* from {@link org.jdbi.v3.core.mapper.MapEntryMapperTest}
*/
@Test
public void uniqueIndex() {
Handle h = dbRule.getSharedHandle();
h.execute("create table user (id int, name varchar)");
h.prepareBatch("insert into user (id, name) values (?, ?)").add(1, "alice").add(2, "bob").add(3, "cathy").add(4, "dilbert").execute();
Map<Integer, User> map = h.createQuery("select * from user").setMapKeyColumn("id").registerRowMapper(ConstructorMapper.factory(User.class)).collectInto(new GenericType<Map<Integer, User>>() {
});
assertThat(map).containsOnly(Tuple.of(1, new User(1, "alice")), Tuple.of(2, new User(2, "bob")), Tuple.of(3, new User(3, "cathy")), Tuple.of(4, new User(4, "dilbert")));
}
use of io.vavr.collection.Map in project resilience4j by resilience4j.
the class CircuitBreakerExportsTest method testExportsCircuitBreakerMetrics.
@Test
public void testExportsCircuitBreakerMetrics() {
// Given
final CollectorRegistry registry = new CollectorRegistry();
final CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("foo");
CircuitBreakerExports.ofIterable("boo_circuit_breaker", singletonList(circuitBreaker)).register(registry);
final Supplier<Map<String, Double>> values = () -> HashSet.of("successful", "failed", "not_permitted", "buffered", "buffered_max").map(callType -> Tuple.of(callType, registry.getSampleValue("boo_circuit_breaker_calls", new String[] { "name", "call_result" }, new String[] { "foo", callType }))).toMap(t -> t);
// When
final Map<String, Double> initialValues = values.get();
circuitBreaker.executeRunnable(() -> {
});
final Map<String, Double> afterSuccessValues = values.get();
try {
circuitBreaker.executeRunnable(() -> {
throw new SomeAppException("Some exception");
});
} catch (RuntimeException e) {
// expected
}
final Map<String, Double> afterFailureValues = values.get();
circuitBreaker.transitionToOpenState();
try {
circuitBreaker.executeRunnable(() -> {
});
} catch (CircuitBreakerOpenException e) {
// expected
}
final Map<String, Double> afterDeclinedValues = values.get();
// Then
assertThat(initialValues).isEqualTo(HashMap.of("successful", 0.0, "failed", 0.0, "not_permitted", 0.0, "buffered", 0.0, "buffered_max", 100.0));
assertThat(afterSuccessValues).isEqualTo(HashMap.of("successful", 1.0, "failed", 0.0, "not_permitted", 0.0, "buffered", 1.0, "buffered_max", 100.0));
assertThat(afterFailureValues).isEqualTo(HashMap.of("successful", 1.0, "failed", 1.0, "not_permitted", 0.0, "buffered", 2.0, "buffered_max", 100.0));
assertThat(afterDeclinedValues).isEqualTo(HashMap.of("successful", 1.0, "failed", 1.0, "not_permitted", 1.0, "buffered", 2.0, "buffered_max", 100.0));
}
use of io.vavr.collection.Map in project resilience4j by resilience4j.
the class RateLimiterExportsTest method testExportsRateLimiterMetrics.
@Test
public void testExportsRateLimiterMetrics() {
// Given
final CollectorRegistry registry = new CollectorRegistry();
final RateLimiter rateLimiter = RateLimiter.ofDefaults("foo");
RateLimiterExports.ofIterable("boo_rate_limiter", singletonList(rateLimiter)).register(registry);
final Supplier<Map<String, Double>> values = () -> HashSet.of("available_permissions", "waiting_threads").map(param -> Tuple.of(param, registry.getSampleValue("boo_rate_limiter", new String[] { "name", "param" }, new String[] { "foo", param }))).toMap(t -> t);
// When
final Map<String, Double> initialValues = values.get();
// Then
assertThat(initialValues).isEqualTo(HashMap.of("available_permissions", 50.0, "waiting_threads", 0.0));
}
Aggregations