import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.doAnswer;
-import static org.mockito.Mockito.doThrow;
-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;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.*;
import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.atomic.AtomicInteger;
-
-import net.pterodactylus.sone.core.WebOfTrustUpdater.AddContextJob;
-import net.pterodactylus.sone.core.WebOfTrustUpdater.RemoveContextJob;
-import net.pterodactylus.sone.core.WebOfTrustUpdater.SetPropertyJob;
-import net.pterodactylus.sone.core.WebOfTrustUpdater.SetTrustJob;
-import net.pterodactylus.sone.core.WebOfTrustUpdater.WebOfTrustContextUpdateJob;
-import net.pterodactylus.sone.core.WebOfTrustUpdater.WebOfTrustUpdateJob;
+
+import net.pterodactylus.sone.core.WebOfTrustUpdaterImpl.AddContextJob;
+import net.pterodactylus.sone.core.WebOfTrustUpdaterImpl.RemoveContextJob;
+import net.pterodactylus.sone.core.WebOfTrustUpdaterImpl.SetPropertyJob;
+import net.pterodactylus.sone.core.WebOfTrustUpdaterImpl.SetTrustJob;
+import net.pterodactylus.sone.core.WebOfTrustUpdaterImpl.WebOfTrustContextUpdateJob;
+import net.pterodactylus.sone.core.WebOfTrustUpdaterImpl.WebOfTrustUpdateJob;
import net.pterodactylus.sone.freenet.plugin.PluginException;
import net.pterodactylus.sone.freenet.wot.Identity;
import net.pterodactylus.sone.freenet.wot.OwnIdentity;
import org.mockito.stubbing.Answer;
/**
- * Unit test for {@link WebOfTrustUpdater} and its subclasses.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ * Unit test for {@link WebOfTrustUpdaterImpl} and its subclasses.
*/
public class WebOfTrustUpdaterTest {
private static final String TRUST_COMMENT = "set in a test";
private static final String PROPERTY_NAME = "test-property";
private final WebOfTrustConnector webOfTrustConnector = mock(WebOfTrustConnector.class);
- private final WebOfTrustUpdater webOfTrustUpdater = new WebOfTrustUpdater(webOfTrustConnector);
+ private final WebOfTrustUpdaterImpl webOfTrustUpdater = new WebOfTrustUpdaterImpl(webOfTrustConnector);
private final OwnIdentity ownIdentity = when(mock(OwnIdentity.class).getId()).thenReturn("own-identity-id").getMock();
private final WebOfTrustUpdateJob successfulWebOfTrustUpdateJob = createWebOfTrustUpdateJob(true);
private final WebOfTrustUpdateJob failingWebOfTrustUpdateJob = createWebOfTrustUpdateJob(false);
}
@Test
- public void multipleCallsToAddContextAreCollapsed() throws InterruptedException, PluginException {
- final AtomicInteger errorCount = new AtomicInteger();
- final CountDownLatch addContextsFinished = new CountDownLatch(2);
- for (int i = 1; i <= 2; i++) {
- /* this is so fucking volatile. */
- if (i > 1) {
- sleep(200);
- }
- new Thread(new Runnable() {
- public void run() {
- if (!webOfTrustUpdater.addContextWait(ownIdentity, CONTEXT)) {
- errorCount.incrementAndGet();
- }
- addContextsFinished.countDown();
- }
- }).start();
- }
- webOfTrustUpdater.start();
- assertThat(addContextsFinished.await(1, SECONDS), is(true));
- verify(ownIdentity).addContext(eq(CONTEXT));
- assertThat(errorCount.get(), is(0));
- }
-
- @Test
public void removeContextRemovesAContext() throws InterruptedException, PluginException {
webOfTrustUpdater.start();
final CountDownLatch removeContextTrigger = new CountDownLatch(1);
@Test
public void setTrustSetsTrust() throws InterruptedException, PluginException {
- final CountDownLatch trustSetTrigger =new CountDownLatch(1);
+ final CountDownLatch trustSetTrigger = new CountDownLatch(1);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
@Test
public void setTrustRequestsAreCoalesced() throws InterruptedException, PluginException {
- final CountDownLatch trustSetTrigger = new CountDownLatch(1);
- doAnswer(new Answer<Void>() {
- @Override
- public Void answer(InvocationOnMock invocation) throws Throwable {
- trustSetTrigger.countDown();
- return null;
- }
+ final CountDownLatch firstTrigger = new CountDownLatch(1);
+ doAnswer((Answer<Void>) invocation -> {
+ firstTrigger.countDown();
+ return null;
}).when(trustee).setTrust(eq(ownIdentity), eq(new Trust(SCORE, null, 0)));
- for (int i = 1; i <= 2; i++) {
- /* this is so fucking volatile. */
- if (i > 1) {
- sleep(200);
- }
- new Thread(new Runnable() {
- public void run() {
- webOfTrustUpdater.setTrust(ownIdentity, trustee, SCORE, TRUST_COMMENT);
- }
- }).start();
- }
+ Identity secondTrustee = when(mock(Identity.class).getId()).thenReturn("trustee-id2").getMock();
+ final CountDownLatch secondTrigger = new CountDownLatch(1);
+ doAnswer((Answer<Void>) invocation -> {
+ secondTrigger.countDown();
+ return null;
+ }).when(secondTrustee).setTrust(eq(ownIdentity), eq(new Trust(SCORE, null, 0)));
+ webOfTrustUpdater.setTrust(ownIdentity, trustee, SCORE, TRUST_COMMENT);
+ webOfTrustUpdater.setTrust(ownIdentity, secondTrustee, SCORE, TRUST_COMMENT);
+ webOfTrustUpdater.setTrust(ownIdentity, trustee, SCORE, TRUST_COMMENT);
webOfTrustUpdater.start();
- assertThat(trustSetTrigger.await(1, SECONDS), is(true));
- verify(trustee).setTrust(eq(ownIdentity), eq(new Trust(SCORE, null, 0)));
+ assertThat(firstTrigger.await(1, SECONDS), is(true));
+ assertThat(secondTrigger.await(1, SECONDS), is(true));
+ verify(trustee, times(1)).setTrust(eq(ownIdentity), eq(new Trust(SCORE, null, 0)));
verify(webOfTrustConnector).setTrust(eq(ownIdentity), eq(trustee), eq(SCORE), eq(TRUST_COMMENT));
}