Search in sources :

Example 1 with HashParameters

use of io.helidon.common.http.HashParameters in project helidon by oracle.

the class HashParametersTest method putIfAbsent.

@Test
public void putIfAbsent() throws Exception {
    HashParameters hp = HashParameters.create();
    List<String> result = hp.putIfAbsent("a", "v1", "v2", "v3");
    assertThat(result, IsCollectionWithSize.hasSize(0));
    assertThat(hp.all("a"), contains("v1", "v2", "v3"));
    result = hp.putIfAbsent("a", "x1", "x2", "x3");
    assertThat(result, contains("v1", "v2", "v3"));
    assertThat(hp.all("a"), contains("v1", "v2", "v3"));
    hp.putIfAbsent("b", Arrays.asList("v1", "v2", "v3"));
    assertThat(hp.all("b"), contains("v1", "v2", "v3"));
    hp.putIfAbsent("b", Arrays.asList("x1", "x2", "x3"));
    assertThat(hp.all("b"), contains("v1", "v2", "v3"));
    hp.putIfAbsent("b");
    assertThat(hp.all("b"), contains("v1", "v2", "v3"));
    hp.putIfAbsent("b", (Iterable<String>) null);
    assertThat(hp.all("b"), contains("v1", "v2", "v3"));
    result = hp.putIfAbsent("c");
    assertThat(result, IsCollectionWithSize.hasSize(0));
    result = hp.putIfAbsent("c", (Iterable<String>) null);
    assertThat(result, IsCollectionWithSize.hasSize(0));
}
Also used : HashParameters(io.helidon.common.http.HashParameters) Test(org.junit.jupiter.api.Test)

Example 2 with HashParameters

use of io.helidon.common.http.HashParameters in project helidon by oracle.

the class HashParametersTest method putAll.

@Test
public void putAll() throws Exception {
    HashParameters hp = HashParameters.create();
    hp.put("a", "a1", "a2");
    hp.put("b", "b1", "b2");
    HashParameters hp2 = HashParameters.create();
    hp2.put("c", "c1", "c2");
    hp2.put("b", "b3", "b4");
    hp.putAll(hp2);
    assertThat(hp.all("a"), contains("a1", "a2"));
    assertThat(hp.all("b"), contains("b3", "b4"));
    assertThat(hp.all("c"), contains("c1", "c2"));
    hp.putAll(null);
    assertThat(hp.all("a"), contains("a1", "a2"));
    assertThat(hp.all("b"), contains("b3", "b4"));
}
Also used : HashParameters(io.helidon.common.http.HashParameters) Test(org.junit.jupiter.api.Test)

Example 3 with HashParameters

use of io.helidon.common.http.HashParameters in project helidon by oracle.

the class HashParametersTest method computeIfAbsent.

@Test
public void computeIfAbsent() throws Exception {
    AtomicBoolean visited = new AtomicBoolean(false);
    HashParameters hp = HashParameters.create();
    List<String> result = hp.computeIfAbsent("a", k -> {
        visited.set(true);
        return null;
    });
    assertThat(visited.get(), is(true));
    assertThat(result, IsCollectionWithSize.hasSize(0));
    assertThat(hp.all("a"), IsCollectionWithSize.hasSize(0));
    visited.set(false);
    result = hp.computeIfAbsent("a", k -> {
        visited.set(true);
        return Arrays.asList("v1", "v2", "v3");
    });
    assertThat(visited.get(), is(true));
    assertThat(result, contains("v1", "v2", "v3"));
    assertThat(hp.all("a"), contains("v1", "v2", "v3"));
    visited.set(false);
    result = hp.computeIfAbsent("a", k -> {
        visited.set(true);
        return Arrays.asList("x1", "x2", "x3");
    });
    assertThat(visited.get(), is(false));
    assertThat(result, contains("v1", "v2", "v3"));
    assertThat(hp.all("a"), contains("v1", "v2", "v3"));
    visited.set(false);
    result = hp.computeSingleIfAbsent("b", k -> {
        visited.set(true);
        return "x1";
    });
    assertThat(visited.get(), is(true));
    assertThat(result, contains("x1"));
    assertThat(hp.all("b"), contains("x1"));
    visited.set(false);
    result = hp.computeSingleIfAbsent("c", k -> {
        visited.set(true);
        return null;
    });
    assertThat(visited.get(), is(true));
    assertThat(result, IsCollectionWithSize.hasSize(0));
    assertThat(hp.first("c").isPresent(), is(false));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Optional.empty(java.util.Optional.empty) Arrays(java.util.Arrays) IsIterableContainingInOrder.contains(org.hamcrest.collection.IsIterableContainingInOrder.contains) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashParameters(io.helidon.common.http.HashParameters) ArrayList(java.util.ArrayList) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) Test(org.junit.jupiter.api.Test) List(java.util.List) Map(java.util.Map) Parameters(io.helidon.common.http.Parameters) Is.is(org.hamcrest.core.Is.is) IsCollectionWithSize(org.hamcrest.collection.IsCollectionWithSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashParameters(io.helidon.common.http.HashParameters) Test(org.junit.jupiter.api.Test)

Example 4 with HashParameters

use of io.helidon.common.http.HashParameters in project helidon by oracle.

the class HashParametersTest method nonExistentKey.

@Test
public void nonExistentKey() throws Exception {
    HashParameters hashParameters = HashParameters.create();
    assertThat(hashParameters.all("a"), hasSize(0));
    assertThat(hashParameters.first("a"), is(empty()));
}
Also used : HashParameters(io.helidon.common.http.HashParameters) Test(org.junit.jupiter.api.Test)

Example 5 with HashParameters

use of io.helidon.common.http.HashParameters in project helidon by oracle.

the class HashParametersTest method concat.

@Test
public void concat() throws Exception {
    HashParameters p1 = HashParameters.create();
    p1.add("a", "1", "2");
    p1.add("b", "3", "4", "5");
    HashParameters p2 = HashParameters.create();
    p2.add("a", "6");
    p2.add("c", "7", "8");
    HashParameters p3 = HashParameters.create();
    HashParameters p4 = HashParameters.create();
    p2.add("a", "9");
    p2.add("c", "10");
    p2.add("d", "11", "12");
    HashParameters concat = HashParameters.concat(p1, p2, null, p3, null, p4, null, null);
    assertThat(concat.all("a"), contains("1", "2", "6", "9"));
    assertThat(concat.all("b"), contains("3", "4", "5"));
    assertThat(concat.all("c"), contains("7", "8", "10"));
    assertThat(concat.all("d"), contains("11", "12"));
    concat = HashParameters.concat(p1);
    assertThat(concat, is(p1));
}
Also used : HashParameters(io.helidon.common.http.HashParameters) Test(org.junit.jupiter.api.Test)

Aggregations

HashParameters (io.helidon.common.http.HashParameters)15 Test (org.junit.jupiter.api.Test)15 ArrayList (java.util.ArrayList)3 Parameters (io.helidon.common.http.Parameters)2 List (java.util.List)2 Arrays (java.util.Arrays)1 Map (java.util.Map)1 Optional.empty (java.util.Optional.empty)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 IsCollectionWithSize (org.hamcrest.collection.IsCollectionWithSize)1 IsCollectionWithSize.hasSize (org.hamcrest.collection.IsCollectionWithSize.hasSize)1 IsIterableContainingInOrder.contains (org.hamcrest.collection.IsIterableContainingInOrder.contains)1 Is.is (org.hamcrest.core.Is.is)1 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)1