Search in sources :

Example 6 with PrettySerializer

use of net.morimekta.providence.serializer.PrettySerializer 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 7 with PrettySerializer

use of net.morimekta.providence.serializer.PrettySerializer in project providence by morimekta.

the class DirectoryMessageStoreTest method testDirectoryStore.

@Test
public void testDirectoryStore() throws IOException, InterruptedException {
    Map<UUID, Containers> source = new HashMap<>();
    for (int i = 0; i < 100; ++i) {
        source.put(UUID.randomUUID(), generator.generate(Containers.kDescriptor));
    }
    try (DirectoryMessageStore<UUID, Containers, Containers._Field> store = new DirectoryMessageStore<>(tmp.getRoot(), UUID::toString, UUID::fromString, Containers.kDescriptor, new PrettySerializer().config())) {
        store.putAll(source);
    } catch (IOException e) {
        fail(e.getMessage());
    }
    Thread.sleep(1);
    try (DirectoryMessageStore<UUID, Containers, Containers._Field> store = new DirectoryMessageStore<>(tmp.getRoot(), UUID::toString, UUID::fromString, Containers.kDescriptor, new PrettySerializer().config())) {
        assertThat(store.keys(), is(source.keySet()));
        Set<UUID> random5 = new HashSet<>();
        source.forEach((k, v) -> {
            if (random5.size() < 5) {
                random5.add(k);
            }
            assertThat(store.containsKey(k), is(true));
            assertThat(k.toString(), store.get(k), is(equalToMessage(v)));
        });
        for (int i = 0; i < 100; ++i) {
            assertThat(store.containsKey(UUID.randomUUID()), is(false));
        }
        assertThat(store.getAll(source.keySet()), is(source));
        store.removeAll(random5);
        for (UUID k : random5) {
            assertThat(store.containsKey(k), is(false));
            assertThat(store.get(k), is(nullValue()));
        }
    } catch (IOException e) {
        fail(e.getMessage());
    }
}
Also used : HashMap(java.util.HashMap) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) Containers(net.morimekta.test.providence.storage.Containers) IOException(java.io.IOException) UUID(java.util.UUID) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

PrettySerializer (net.morimekta.providence.serializer.PrettySerializer)7 Test (org.junit.Test)5 IOException (java.io.IOException)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 HashMap (java.util.HashMap)2 UUID (java.util.UUID)2 BinarySerializer (net.morimekta.providence.serializer.BinarySerializer)2 JsonSerializer (net.morimekta.providence.serializer.JsonSerializer)2 Serializer (net.morimekta.providence.serializer.Serializer)2 ImmutableList (com.google.common.collect.ImmutableList)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1