From 7804921fddbc1a9c7e6c739e802b724b45190cc8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 7 Feb 2018 06:57:04 +0100 Subject: [PATCH] Store Sone following time on following a Sone --- .../sone/database/memory/MemoryFriendDatabase.java | 3 + .../sone/database/memory/MemoryDatabaseTest.java | 72 +++++++++++++++------- 2 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryFriendDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryFriendDatabase.java index 0be8738..5ab1b28 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryFriendDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryFriendDatabase.java @@ -48,6 +48,9 @@ class MemoryFriendDatabase { try { if (soneFriends.put(localSoneId, friendSoneId)) { configurationLoader.saveFriends(localSoneId, soneFriends.get(localSoneId)); + if (configurationLoader.getSoneFollowingTime(friendSoneId) == null) { + configurationLoader.setSoneFollowingTime(friendSoneId, System.currentTimeMillis()); + } } } finally { lock.writeLock().unlock(); diff --git a/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java b/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java index 4d88429..47c2c4c 100644 --- a/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java +++ b/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java @@ -30,11 +30,12 @@ import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.emptyIterable; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.lessThan; import static org.hamcrest.Matchers.nullValue; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -57,9 +58,11 @@ import net.pterodactylus.sone.test.TestPostBuilder; import net.pterodactylus.sone.test.TestPostReplyBuilder; import net.pterodactylus.sone.test.TestValue; import net.pterodactylus.util.config.Configuration; +import net.pterodactylus.util.config.ConfigurationException; import net.pterodactylus.util.config.Value; import com.google.common.base.Optional; +import org.hamcrest.CoreMatchers; import org.junit.Before; import org.junit.Test; import org.mockito.invocation.InvocationOnMock; @@ -324,14 +327,28 @@ public class MemoryDatabaseTest { assertThat(memoryDatabase.isFriend(sone, "Friend1"), is(false)); } - private Map> prepareConfigurationValues() { - final Map> configurationValues = new HashMap>(); + private Map> prepareConfigurationValues() { + final Map> configurationValues = new HashMap<>(); when(configuration.getStringValue(anyString())).thenAnswer(new Answer>() { @Override public Value answer(InvocationOnMock invocation) throws Throwable { - Value stringValue = TestValue.from(null); - configurationValues.put((String) invocation.getArguments()[0], stringValue); - return stringValue; + Value value = configurationValues.get(invocation.getArgument(0)); + if (value == null) { + value = TestValue.from(null); + configurationValues.put(invocation.getArgument(0), value); + } + return (Value) value; + } + }); + when(configuration.getLongValue(anyString())).thenAnswer(new Answer>() { + @Override + public Value answer(InvocationOnMock invocation) throws Throwable { + Value value = configurationValues.get(invocation.getArgument(0)); + if (value == null) { + value = TestValue.from(null); + configurationValues.put(invocation.getArgument(0), value); + } + return (Value) value; } }); return configurationValues; @@ -339,13 +356,13 @@ public class MemoryDatabaseTest { @Test public void friendIsAddedCorrectlyToLocalSone() { - Map> configurationValues = prepareConfigurationValues(); + Map> configurationValues = prepareConfigurationValues(); when(sone.isLocal()).thenReturn(true); memoryDatabase.addFriend(sone, "Friend1"); assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/0/ID"), - is(TestValue.from("Friend1"))); + CoreMatchers.>is(TestValue.from("Friend1"))); assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/1/ID"), - is(TestValue.from(null))); + CoreMatchers.>is(TestValue.from(null))); } @Test @@ -355,24 +372,15 @@ public class MemoryDatabaseTest { } @Test - public void configurationIsWrittenOnceIfFriendIsAddedTwice() { - prepareConfigurationValues(); - when(sone.isLocal()).thenReturn(true); - memoryDatabase.addFriend(sone, "Friend1"); - memoryDatabase.addFriend(sone, "Friend1"); - verify(configuration, times(3)).getStringValue(anyString()); - } - - @Test public void friendIsRemovedCorrectlyFromLocalSone() { - Map> configurationValues = prepareConfigurationValues(); + Map> configurationValues = prepareConfigurationValues(); when(sone.isLocal()).thenReturn(true); memoryDatabase.addFriend(sone, "Friend1"); memoryDatabase.removeFriend(sone, "Friend1"); assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/0/ID"), - is(TestValue.from(null))); + CoreMatchers.>is(TestValue.from(null))); assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/1/ID"), - is(TestValue.from(null))); + CoreMatchers.>is(TestValue.from(null))); } @Test @@ -383,4 +391,26 @@ public class MemoryDatabaseTest { verify(configuration).getStringValue(anyString()); } + @Test + public void timeIsStoredInConfigurationWhenASoneIsFollowed() throws ConfigurationException { + prepareConfigurationValues(); + when(sone.isLocal()).thenReturn(true); + memoryDatabase.addFriend(sone, "Friend"); + assertThat(configuration.getStringValue("SoneFollowingTimes/0/Sone").getValue(), equalTo("Friend")); + assertThat(System.currentTimeMillis() - configuration.getLongValue("SoneFollowingTimes/0/Time").getValue(), lessThan(1000L)); + assertThat(configuration.getStringValue("SoneFollowingTimes/1/Sone").getValue(), nullValue()); + } + + @Test + public void existingTimeIsNotOverwrittenWhenASoneIsFollowed() throws ConfigurationException { + prepareConfigurationValues(); + configuration.getStringValue("SoneFollowingTimes/0/Sone").setValue("Friend"); + configuration.getLongValue("SoneFollowingTimes/0/Time").setValue(1000L); + when(sone.isLocal()).thenReturn(true); + memoryDatabase.addFriend(sone, "Friend"); + assertThat(configuration.getStringValue("SoneFollowingTimes/0/Sone").getValue(), equalTo("Friend")); + assertThat(configuration.getLongValue("SoneFollowingTimes/0/Time").getValue(), equalTo(1000L)); + assertThat(configuration.getStringValue("SoneFollowingTimes/1/Sone").getValue(), nullValue()); + } + } -- 2.7.4