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.anyBoolean;
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.Sone;
31 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
32 import net.pterodactylus.sone.freenet.fcp.Command.Response;
33 import net.pterodactylus.sone.freenet.fcp.FcpException;
35 import freenet.support.SimpleFieldSet;
37 import com.google.common.base.Optional;
38 import org.junit.Test;
41 * Tests for {@link UnlockSoneCommand}.
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public class LockSoneCommandTest {
48 public void testLockingALocalSone() throws FcpException {
49 Sone localSone = mock(Sone.class);
50 when(localSone.getId()).thenReturn("LocalSone");
51 when(localSone.isLocal()).thenReturn(true);
52 Core core = mock(Core.class);
53 when(core.getSone(eq("LocalSone"))).thenReturn(Optional.of(localSone));
54 when(core.getLocalSone(eq("LocalSone"), anyBoolean())).thenReturn(localSone);
55 SimpleFieldSet fields = new SimpleFieldSetBuilder().put("Sone", "LocalSone").get();
57 LockSoneCommand lockSoneCommand = new LockSoneCommand(core);
58 Response response = lockSoneCommand.execute(fields, null, null);
60 verify(core).lockSone(eq(localSone));
61 assertThat(response, notNullValue());
62 assertThat(response.getReplyParameters(), notNullValue());
63 assertThat(response.getReplyParameters().get("Message"), is("SoneLocked"));
64 assertThat(response.getReplyParameters().get("Sone"), is("LocalSone"));
67 @Test(expected = FcpException.class)
68 public void testLockingARemoteSone() throws FcpException {
69 Sone removeSone = mock(Sone.class);
70 Core core = mock(Core.class);
71 when(core.getSone(eq("RemoteSone"))).thenReturn(Optional.of(removeSone));
72 SimpleFieldSet fields = new SimpleFieldSetBuilder().put("Sone", "RemoteSone").get();
74 LockSoneCommand lockSoneCommand = new LockSoneCommand(core);
75 lockSoneCommand.execute(fields, null, null);
78 @Test(expected = FcpException.class)
79 public void testMissingSone() throws FcpException {
80 Core core = mock(Core.class);
81 SimpleFieldSet fields = new SimpleFieldSetBuilder().get();
83 LockSoneCommand lockSoneCommand = new LockSoneCommand(core);
84 lockSoneCommand.execute(fields, null, null);