2 * Sone - LockSoneCommandTest.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.fcp;
20 import static org.hamcrest.CoreMatchers.is;
21 import static org.hamcrest.CoreMatchers.notNullValue;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.mockito.Matchers.anyString;
24 import static org.mockito.Matchers.eq;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
29 import net.pterodactylus.sone.core.Core;
30 import net.pterodactylus.sone.data.LocalSone;
31 import net.pterodactylus.sone.data.Sone;
32 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
33 import net.pterodactylus.sone.freenet.fcp.Command.Response;
34 import net.pterodactylus.sone.freenet.fcp.FcpException;
36 import freenet.support.SimpleFieldSet;
38 import com.google.common.base.Optional;
39 import org.junit.Test;
42 * Tests for {@link LockSoneCommand}.
44 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46 public class UnlockSoneCommandTest {
49 public void testUnlockingALocalSone() throws FcpException {
50 LocalSone localSone = mock(LocalSone.class);
51 when(localSone.getId()).thenReturn("LocalSone");
52 when(localSone.isLocal()).thenReturn(true);
53 Core core = mock(Core.class);
54 when(core.getSone(eq("LocalSone"))).thenReturn(Optional.<Sone>of(localSone));
55 when(core.getLocalSone(eq("LocalSone"))).thenReturn(Optional.of(localSone));
56 SimpleFieldSet fields = new SimpleFieldSetBuilder().put("Sone", "LocalSone").get();
58 UnlockSoneCommand unlockSoneCommand = new UnlockSoneCommand(core);
59 Response response = unlockSoneCommand.execute(fields, null, null);
61 verify(core).unlockSone(eq(localSone));
62 assertThat(response, notNullValue());
63 assertThat(response.getReplyParameters(), notNullValue());
64 assertThat(response.getReplyParameters().get("Message"), is("SoneUnlocked"));
65 assertThat(response.getReplyParameters().get("Sone"), is("LocalSone"));
68 @Test(expected = FcpException.class)
69 public void testUnlockingARemoteSone() throws FcpException {
70 Sone removeSone = mock(Sone.class);
71 Core core = mock(Core.class);
72 when(core.getLocalSone(anyString())).thenReturn(Optional.<LocalSone>absent());
73 when(core.getSone(eq("RemoteSone"))).thenReturn(Optional.of(removeSone));
74 SimpleFieldSet fields = new SimpleFieldSetBuilder().put("Sone", "RemoteSone").get();
76 UnlockSoneCommand unlockSoneCommand = new UnlockSoneCommand(core);
77 unlockSoneCommand.execute(fields, null, null);
80 @Test(expected = FcpException.class)
81 public void testMissingSone() throws FcpException {
82 Core core = mock(Core.class);
83 SimpleFieldSet fields = new SimpleFieldSetBuilder().get();
85 UnlockSoneCommand unlockSoneCommand = new UnlockSoneCommand(core);
86 unlockSoneCommand.execute(fields, null, null);