PersistedGroupDataStore implements GroupDataStore { @Override public GroupData getGroupData(InMemoryGroup group) { try { return getGroupData0(group); } catch (DatabaseException exc) { throw new IllegalStateException(exc); } } PersistedGroupDataStore(StorageProvider storageProvider);  PersistedGroupDataStore(StorageProvider storageProvider, DatabaseProvider provider); @Override GroupData getGroupData(InMemoryGroup group);  }
@Test @DisplayName("Creates persistent group data") void testCreateGroupData(@Mock DatabaseAccessor accessor) throws Exception { when(group.getName()).thenReturn("test.new.data"); when(storageProvider.exists(any())).thenReturn(false); when(databaseProvider.create(any(), anyString())).thenReturn(accessor); GroupData groupData = dataStore.getGroupData(group); assertThat(groupData).isInstanceOf(PersistedGroupData.class); assertThat(groupData.getGroup()).isSameAs(group); assertThat(groupData.getEntries()).hasSize(0); verify(databaseProvider).create(storageProvider, "test.new.data"); }
@Test @DisplayName("Adds initial entries to created group data") void testAddInitialData(@Mock DatabaseAccessor accessor) throws Exception { when(group.getName()).thenReturn("test.new.data"); when(storageProvider.exists(any())).thenReturn(false); when(databaseProvider.create(any(), anyString())).thenReturn(accessor); when(group.validateNewEntry(any(), any())).then(returnsFirstArg()); when(group.initialData()).thenReturn(Arrays.asList( DefaultEntryData.of("id1", TestMap.of("param", "value1")), DefaultEntryData.of("id2", TestMap.of("param", "value2")) )); GroupData groupData = dataStore.getGroupData(group); assertThat(groupData).isInstanceOf(PersistedGroupData.class); assertThat(groupData.getGroup()).isSameAs(group); verify(databaseProvider).create(storageProvider, "test.new.data"); ArgumentCaptor<Event> events = ArgumentCaptor.forClass(Event.class); verify(accessor, times(2)).append(events.capture()); Event event1 = events.getAllValues().get(0); Event event2 = events.getAllValues().get(1); assertThat(event1.getObjectId()).isEqualTo("id1"); assertThat(event1.getOperation()).isEqualTo("ADD"); assertThat(event1.getData()).isEqualTo("{\"param\":\"value1\"}"); assertThat(event2.getObjectId()).isEqualTo("id2"); assertThat(event2.getOperation()).isEqualTo("ADD"); assertThat(event2.getData()).isEqualTo("{\"param\":\"value2\"}"); }
@Test @DisplayName("Reads persistent group data") void testReadGroupData(@Mock Storage storage, @Mock DatabaseAccessor accessor) throws Exception { when(group.getName()).thenReturn("test.new.data"); when(storageProvider.exists(any())).thenReturn(true); when(storageProvider.read(any())).thenReturn(storage); when(databaseProvider.read(any())).thenReturn(accessor); when(accessor.getData()).thenAnswer(__ -> Stream.of( event("none", "1", "ADD", "{\"param\":\"value\"}", LocalDateTime.now()), event("none", "2", "ADD", "{\"param\":\"value\"}", LocalDateTime.now()), event("none", "1", "REPLACE", "{\"param\":\"value\"}", LocalDateTime.now()))); GroupData groupData = dataStore.getGroupData(group); assertThat(groupData).isInstanceOf(PersistedGroupData.class); assertThat(groupData.getGroup()).isSameAs(group); assertThat(groupData.getEntries()).hasSize(2); verify(storageProvider).read("test.new.data"); verify(databaseProvider).read(storage); }
@Test @DisplayName("Propagates Database Exception") void testCreateGroupDataException() throws Exception { when(group.getName()).thenReturn("test.new.data"); when(storageProvider.exists(any())).thenReturn(false); DatabaseException exception = new DatabaseException("Test"); when(databaseProvider.create(any(), any())).thenThrow(exception); assertThatThrownBy(() -> dataStore.getGroupData(group)) .hasCause(exception); }