Search in sources :

Example 1 with OptionalFields

use of net.morimekta.test.providence.storage.OptionalFields in project providence by morimekta.

the class TestBase method assertConformity.

void assertConformity(MessageStore<String, OptionalFields, OptionalFields._Field> storage) {
    assertThat(storage.put("1234", orig1), is(nullValue()));
    assertThat(storage.putAll(ImmutableMap.of("2345", orig2)).keySet(), hasSize(0));
    assertThat(storage.putBuilder("3456", orig3), is(nullValue()));
    assertThat(storage.putAllBuilders(ImmutableMap.of("4567", orig4)).keySet(), hasSize(0));
    assertThat(storage.keys(), hasSize(4));
    assertThat(storage.keys(), hasItem("1234"));
    assertThat(storage.keys(), hasItem("2345"));
    assertThat(storage.keys(), hasItem("3456"));
    assertThat(storage.keys(), hasItem("4567"));
    OptionalFields get1 = storage.get("1234");
    OptionalFields._Builder get2 = storage.getBuilder("2345");
    OptionalFields get3 = storage.get("3456");
    OptionalFields._Builder get4 = storage.getBuilder("4567");
    OptionalFields get5 = storage.get("5678");
    OptionalFields._Builder get6 = storage.getBuilder("6789");
    // Check that the values are the same.
    assertThat(get1, is(notNullValue()));
    assertThat(get2, is(notNullValue()));
    assertThat(get3, is(notNullValue()));
    assertThat(get4, is(notNullValue()));
    assertThat(get5, is(nullValue()));
    assertThat(get6, is(nullValue()));
    assertThat(get1, CoreMatchers.is(orig1));
    assertThat(get2, CoreMatchers.is(orig2.mutate()));
    assertThat(get3, CoreMatchers.is(orig3.build()));
    assertThat(get4, CoreMatchers.is(orig4));
    // Check that getting all conforms.
    Map<String, OptionalFields> map1 = storage.getAll(ImmutableList.of("1234", "3456", "5678"));
    assertThat(map1.keySet(), hasSize(2));
    assertThat(map1, hasEntry("1234", orig1));
    assertThat(map1, Matchers.hasEntry("3456", orig3.build()));
    Map<String, OptionalFields._Builder> map2 = storage.getAllBuilders(ImmutableList.of("1234", "3456", "5678"));
    assertThat(map2.keySet(), hasSize(2));
    assertThat(map2, Matchers.hasEntry("1234", orig1.mutate()));
    assertThat(map2, hasEntry("3456", orig3));
    // Check remove and removeAll.
    assertThat(storage.remove("1234"), CoreMatchers.is(orig1));
    Map<String, OptionalFields> map3 = storage.removeAll(ImmutableList.of("1234", "3456"));
    assertThat(map3.keySet(), hasSize(1));
    assertThat(map3, Matchers.hasEntry("3456", orig3.build()));
    assertThat(storage.get("1234"), is(nullValue()));
    assertThat(storage.get("3456"), is(nullValue()));
    assertThat(storage.containsKey("1234"), is(false));
    assertThat(storage.containsKey("2345"), is(true));
    // Check put overriding.
    assertThat(storage.put("2345", orig1), CoreMatchers.is(orig2));
    assertThat(storage.putBuilder("2345", orig3), CoreMatchers.is(orig1.mutate()));
    Map<String, OptionalFields> put1 = storage.putAll(ImmutableMap.of("1234", orig1, "2345", orig2));
    assertThat(put1.keySet(), hasSize(1));
    assertThat(put1, Matchers.hasEntry("2345", orig3.build()));
    Map<String, OptionalFields._Builder> put2 = storage.putAllBuilders(ImmutableMap.of("3456", orig3, "4567", orig4));
    assertThat(put2.keySet(), hasSize(1));
    assertThat(put2, hasEntry("4567", orig4));
}
Also used : OptionalFields(net.morimekta.test.providence.storage.OptionalFields)

Example 2 with OptionalFields

use of net.morimekta.test.providence.storage.OptionalFields in project providence by morimekta.

the class InMemoryMessageListStoreTest method testStore.

@Test
public void testStore() {
    MessageListStore<UUID, OptionalFields, OptionalFields._Field> store = new InMemoryMessageListStore<>();
    for (int i = 0; i < 100; ++i) {
        List<OptionalFields> list = new ArrayList<>();
        for (int j = 0; j < 10; ++j) {
            list.add(generator.generate(OptionalFields.kDescriptor));
        }
        store.put(UUID.randomUUID(), list);
    }
    TreeSet<UUID> ids = new TreeSet<>(store.keys());
    assertThat(ids, hasSize(100));
    for (UUID id : ids) {
        assertThat(store.containsKey(id), is(true));
    }
    TreeSet<UUID> missing = new TreeSet<>();
    for (int i = 0; i < 100; ++i) {
        UUID uuid = UUID.randomUUID();
        assertThat(store.containsKey(uuid), is(false));
        ;
        missing.add(uuid);
    }
    assertThat(store.getAll(missing).entrySet(), hasSize(0));
    store.remove(ids.first());
    store.removeAll(new ArrayList<>(ids).subList(45, 55));
    assertThat(store.getAll(ids).entrySet(), hasSize(89));
    Map<UUID, List<OptionalFields._Builder>> bld = store.getAllBuilders(new ArrayList<>(ids).subList(30, 45));
    bld.forEach((k, list) -> {
        for (OptionalFields._Builder b : list) {
            b.clearBinaryValue();
            b.clearBooleanValue();
            b.clearByteValue();
        }
    });
    store.putAllBuilders(bld);
    Map<UUID, List<OptionalFields>> tmp2 = store.getAll(bld.keySet());
    tmp2.forEach((k, list) -> {
        for (OptionalFields v : list) {
            assertThat(v.hasBooleanValue(), is(false));
            assertThat(v.hasByteValue(), is(false));
            assertThat(v.hasBinaryValue(), is(false));
        }
    });
    OptionalFields._Builder builder = OptionalFields.builder();
    builder.setIntegerValue(10);
    builder.setBooleanValue(true);
    builder.setDoubleValue(12345.6789);
    UUID uuid = UUID.randomUUID();
    store.putBuilders(uuid, ImmutableList.of(builder));
    List<OptionalFields> list = store.get(uuid);
    assertThat(list, hasSize(1));
    assertThat(list, hasItem(builder.build()));
    List<OptionalFields._Builder> otherBuilder = store.getBuilders(uuid);
    assertThat(otherBuilder, hasSize(1));
    assertThat(otherBuilder.get(0).build(), is(equalToMessage(builder.build())));
}
Also used : ArrayList(java.util.ArrayList) OptionalFields(net.morimekta.test.providence.storage.OptionalFields) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) UUID(java.util.UUID) Test(org.junit.Test)

Example 3 with OptionalFields

use of net.morimekta.test.providence.storage.OptionalFields in project providence by morimekta.

the class DirectoryMessageListStoreTest method testStore.

@Test
public void testStore() {
    Map<UUID, List<OptionalFields>> source = new HashMap<>();
    for (int i = 0; i < 100; ++i) {
        UUID uuid = UUID.randomUUID();
        for (int j = 0; j < 10; ++j) {
            source.computeIfAbsent(uuid, u -> new ArrayList<>(10)).add(generator.generate(OptionalFields.kDescriptor));
        }
    }
    try (DirectoryMessageListStore<UUID, OptionalFields, OptionalFields._Field> store = new DirectoryMessageListStore<>(tmp.getRoot(), UUID::toString, UUID::fromString, OptionalFields.kDescriptor, new PrettySerializer().config())) {
        store.putAll(source);
    } catch (IOException e) {
        fail(e.getMessage());
    }
    try (DirectoryMessageListStore<UUID, OptionalFields, OptionalFields._Field> store = new DirectoryMessageListStore<>(tmp.getRoot(), UUID::toString, UUID::fromString, OptionalFields.kDescriptor, new PrettySerializer().config())) {
        TreeSet<UUID> ids = new TreeSet<>(store.keys());
        assertThat(ids, hasSize(100));
        for (UUID id : ids) {
            assertThat(store.containsKey(id), is(true));
        }
        TreeSet<UUID> missing = new TreeSet<>();
        for (int i = 0; i < 100; ++i) {
            UUID uuid = UUID.randomUUID();
            assertThat(store.containsKey(uuid), is(false));
            ;
            missing.add(uuid);
        }
        assertThat(store.getAll(missing).entrySet(), hasSize(0));
        store.remove(ids.first());
        store.removeAll(new ArrayList<>(ids).subList(45, 55));
        assertThat(store.getAll(ids).entrySet(), hasSize(89));
        Map<UUID, List<OptionalFields._Builder>> bld = store.getAllBuilders(new ArrayList<>(ids).subList(30, 45));
        bld.forEach((k, list) -> {
            for (OptionalFields._Builder b : list) {
                b.clearBinaryValue();
                b.clearBooleanValue();
                b.clearByteValue();
            }
        });
        store.putAllBuilders(bld);
        Map<UUID, List<OptionalFields>> tmp2 = store.getAll(bld.keySet());
        tmp2.forEach((k, list) -> {
            for (OptionalFields v : list) {
                assertThat(v.hasBooleanValue(), is(false));
                assertThat(v.hasByteValue(), is(false));
                assertThat(v.hasBinaryValue(), is(false));
            }
        });
        OptionalFields._Builder builder = OptionalFields.builder();
        builder.setIntegerValue(10);
        builder.setBooleanValue(true);
        builder.setDoubleValue(12345.6789);
        UUID uuid = UUID.randomUUID();
        store.putBuilders(uuid, ImmutableList.of(builder));
        List<OptionalFields> list = store.get(uuid);
        assertThat(list, hasSize(1));
        assertThat(list, hasItem(builder.build()));
        List<OptionalFields._Builder> otherBuilder = store.getBuilders(uuid);
        assertThat(otherBuilder, hasSize(1));
        assertThat(otherBuilder.get(0).build(), is(equalToMessage(builder.build())));
    } catch (IOException e) {
        fail(e.getMessage());
    }
}
Also used : OptionalFields(net.morimekta.test.providence.storage.OptionalFields) Test(org.junit.Test) IOException(java.io.IOException) HashMap(java.util.HashMap) UUID(java.util.UUID) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) List(java.util.List) Rule(org.junit.Rule) Matchers.hasItem(org.hamcrest.Matchers.hasItem) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) ProvidenceMatchers.equalToMessage(net.morimekta.providence.testing.ProvidenceMatchers.equalToMessage) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Matchers.is(org.hamcrest.Matchers.is) Assert.fail(org.junit.Assert.fail) TemporaryFolder(org.junit.rules.TemporaryFolder) HashMap(java.util.HashMap) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) OptionalFields(net.morimekta.test.providence.storage.OptionalFields) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) UUID(java.util.UUID) Test(org.junit.Test)

Example 4 with OptionalFields

use of net.morimekta.test.providence.storage.OptionalFields in project providence by morimekta.

the class TestBase method assertConformity.

void assertConformity(MessageListStore<String, OptionalFields, OptionalFields._Field> storage) {
    assertThat(storage.put("1234", list1), is(nullValue()));
    assertThat(storage.putAll(ImmutableMap.of("2345", list2)).keySet(), is(empty()));
    assertThat(storage.putBuilders("3456", list3), is(nullValue()));
    assertThat(storage.putAllBuilders(ImmutableMap.of("4567", list4)).keySet(), is(empty()));
    assertThat(storage.keys(), hasSize(4));
    assertThat(storage.keys(), hasItems("1234", "2345", "3456", "4567"));
    assertThat(storage.containsKey("1234"), is(true));
    assertThat(storage.containsKey("5678"), is(false));
    List<OptionalFields> opts = storage.get("1234");
    // Check that the values are the same.
    assertThat(opts, is(notNullValue()));
    assertThat(opts, is(list1));
    for (int i = 0; i < 100; ++i) {
        List<OptionalFields> list = new ArrayList<>();
        for (int j = 0; j < 10; ++j) {
            list.add(generator.generate(OptionalFields.kDescriptor));
        }
        storage.put(UUID.randomUUID().toString(), list);
    }
    TreeSet<String> ids = new TreeSet<>(storage.keys());
    assertThat(ids, hasSize(104));
    for (String id : ids) {
        assertThat(storage.containsKey(id), Matchers.is(true));
    }
    TreeSet<String> missing = new TreeSet<>();
    for (int i = 0; i < 100; ++i) {
        String uuid = UUID.randomUUID().toString();
        assertThat(storage.containsKey(uuid), Matchers.is(false));
        ;
        missing.add(uuid);
    }
    assertThat(storage.getAll(missing).entrySet(), hasSize(0));
    storage.remove(ids.first());
    storage.removeAll(new ArrayList<>(ids).subList(45, 55));
    assertThat(storage.getAll(ids).entrySet(), hasSize(93));
    Map<String, List<OptionalFields._Builder>> bld = storage.getAllBuilders(new ArrayList<>(ids).subList(30, 45));
    bld.forEach((k, list) -> {
        for (OptionalFields._Builder b : list) {
            b.clearBinaryValue();
            b.clearBooleanValue();
            b.clearByteValue();
        }
    });
    storage.putAllBuilders(bld);
    Map<String, List<OptionalFields>> tmp2 = storage.getAll(bld.keySet());
    tmp2.forEach((k, list) -> {
        for (OptionalFields v : list) {
            assertThat(v.hasBooleanValue(), Matchers.is(false));
            assertThat(v.hasByteValue(), Matchers.is(false));
            assertThat(v.hasBinaryValue(), Matchers.is(false));
        }
    });
    OptionalFields._Builder builder = OptionalFields.builder();
    builder.setIntegerValue(10);
    builder.setBooleanValue(true);
    builder.setDoubleValue(12345.6789);
    String uuid = UUID.randomUUID().toString();
    storage.putBuilders(uuid, ImmutableList.of(builder));
    List<OptionalFields> list = storage.get(uuid);
    assertThat(list, hasSize(1));
    assertThat(list, Matchers.hasItem(builder.build()));
    List<OptionalFields._Builder> otherBuilder = storage.getBuilders(uuid);
    assertThat(otherBuilder, notNullValue());
    assertThat(otherBuilder, hasSize(1));
    assertThat(otherBuilder.get(0).build(), Matchers.is(equalToMessage(builder.build())));
    String uuid2 = UUID.randomUUID().toString();
    List<OptionalFields> expectedEmpty = new ArrayList<>();
    storage.put(uuid2, expectedEmpty);
    List<OptionalFields> actualEmpty = storage.get(uuid2);
    assertThat(actualEmpty, is(not(sameInstance(expectedEmpty))));
    assertThat(actualEmpty, is(empty()));
}
Also used : ArrayList(java.util.ArrayList) OptionalFields(net.morimekta.test.providence.storage.OptionalFields) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Aggregations

OptionalFields (net.morimekta.test.providence.storage.OptionalFields)4 ImmutableList (com.google.common.collect.ImmutableList)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 TreeSet (java.util.TreeSet)3 UUID (java.util.UUID)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 PrettySerializer (net.morimekta.providence.serializer.PrettySerializer)1 ProvidenceMatchers.equalToMessage (net.morimekta.providence.testing.ProvidenceMatchers.equalToMessage)1 Matchers.hasItem (org.hamcrest.Matchers.hasItem)1 Matchers.hasSize (org.hamcrest.Matchers.hasSize)1 Matchers.is (org.hamcrest.Matchers.is)1 Assert.assertThat (org.junit.Assert.assertThat)1 Assert.fail (org.junit.Assert.fail)1 Rule (org.junit.Rule)1 TemporaryFolder (org.junit.rules.TemporaryFolder)1