Search in sources :

Example 1 with Map

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");
}
Also used : IntStream(java.util.stream.IntStream) java.util(java.util) Queue(io.vavr.collection.Queue) Assert.assertNotSame(org.junit.Assert.assertNotSame) BitSet(io.vavr.collection.BitSet) SortedMap(io.vavr.collection.SortedMap) TreeMap(io.vavr.collection.TreeMap) List(io.vavr.collection.List) io.vavr.collection(io.vavr.collection) Assert.assertSame(org.junit.Assert.assertSame) SortedSet(io.vavr.collection.SortedSet) TreeSet(io.vavr.collection.TreeSet) HashSet(io.vavr.collection.HashSet) Iterator(io.vavr.collection.Iterator) Vector(io.vavr.collection.Vector) Tuple(io.vavr.Tuple) Set(io.vavr.collection.Set) HashMap(io.vavr.collection.HashMap) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) Tuple2(io.vavr.Tuple2) Assert.assertFalse(org.junit.Assert.assertFalse) Map(io.vavr.collection.Map) Assert.assertEquals(org.junit.Assert.assertEquals) List(io.vavr.collection.List) Test(org.junit.Test)

Example 2 with Map

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));
}
Also used : Tuple(io.vavr.Tuple) CollectorRegistry(io.prometheus.client.CollectorRegistry) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) InMemoryCircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.internal.InMemoryCircuitBreakerRegistry) HashMap(io.vavr.collection.HashMap) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Supplier(java.util.function.Supplier) CircuitBreakerOpenException(io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException) CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) Collections.singletonList(java.util.Collections.singletonList) Collections.singleton(java.util.Collections.singleton) HashSet(io.vavr.collection.HashSet) Map(io.vavr.collection.Map) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CollectorRegistry(io.prometheus.client.CollectorRegistry) HashMap(io.vavr.collection.HashMap) Map(io.vavr.collection.Map) Test(org.junit.Test)

Example 3 with Map

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")));
}
Also used : HashMap(io.vavr.collection.HashMap) Map(io.vavr.collection.Map) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 4 with Map

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));
}
Also used : Tuple(io.vavr.Tuple) CollectorRegistry(io.prometheus.client.CollectorRegistry) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) InMemoryCircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.internal.InMemoryCircuitBreakerRegistry) HashMap(io.vavr.collection.HashMap) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Supplier(java.util.function.Supplier) CircuitBreakerOpenException(io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException) CircuitBreakerRegistry(io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry) Collections.singletonList(java.util.Collections.singletonList) Collections.singleton(java.util.Collections.singleton) HashSet(io.vavr.collection.HashSet) Map(io.vavr.collection.Map) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CollectorRegistry(io.prometheus.client.CollectorRegistry) HashMap(io.vavr.collection.HashMap) Map(io.vavr.collection.Map) CircuitBreakerOpenException(io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException) Test(org.junit.Test)

Example 5 with Map

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));
}
Also used : Tuple(io.vavr.Tuple) CollectorRegistry(io.prometheus.client.CollectorRegistry) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) HashMap(io.vavr.collection.HashMap) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) InMemoryRateLimiterRegistry(io.github.resilience4j.ratelimiter.internal.InMemoryRateLimiterRegistry) Supplier(java.util.function.Supplier) Collections.singletonList(java.util.Collections.singletonList) Collections.singleton(java.util.Collections.singleton) HashSet(io.vavr.collection.HashSet) RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig) RateLimiterRegistry(io.github.resilience4j.ratelimiter.RateLimiterRegistry) Map(io.vavr.collection.Map) CollectorRegistry(io.prometheus.client.CollectorRegistry) HashMap(io.vavr.collection.HashMap) Map(io.vavr.collection.Map) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) Test(org.junit.Test)

Aggregations

HashMap (io.vavr.collection.HashMap)5 Map (io.vavr.collection.Map)5 Test (org.junit.Test)5 Tuple (io.vavr.Tuple)4 HashSet (io.vavr.collection.HashSet)4 CollectorRegistry (io.prometheus.client.CollectorRegistry)3 Collections.singleton (java.util.Collections.singleton)3 Collections.singletonList (java.util.Collections.singletonList)3 Supplier (java.util.function.Supplier)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)2 CircuitBreakerOpenException (io.github.resilience4j.circuitbreaker.CircuitBreakerOpenException)2 CircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry)2 InMemoryCircuitBreakerRegistry (io.github.resilience4j.circuitbreaker.internal.InMemoryCircuitBreakerRegistry)2 RateLimiter (io.github.resilience4j.ratelimiter.RateLimiter)1 RateLimiterConfig (io.github.resilience4j.ratelimiter.RateLimiterConfig)1 RateLimiterRegistry (io.github.resilience4j.ratelimiter.RateLimiterRegistry)1 InMemoryRateLimiterRegistry (io.github.resilience4j.ratelimiter.internal.InMemoryRateLimiterRegistry)1 Tuple2 (io.vavr.Tuple2)1 io.vavr.collection (io.vavr.collection)1