Merge branch 'next' into feature/album-and-image-links
[Sone.git] / src / test / java / net / pterodactylus / sone / fcp / UnlockSoneCommandTest.java
1 /*
2  * Sone - LockSoneCommandTest.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.fcp;
19
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.eq;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27
28 import net.pterodactylus.sone.core.Core;
29 import net.pterodactylus.sone.data.Sone;
30 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
31 import net.pterodactylus.sone.freenet.fcp.Command.Response;
32 import net.pterodactylus.sone.freenet.fcp.FcpException;
33
34 import freenet.support.SimpleFieldSet;
35
36 import com.google.common.base.Optional;
37 import org.junit.Test;
38
39 /**
40  * Tests for {@link LockSoneCommand}.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class UnlockSoneCommandTest {
45
46         @Test
47         public void testUnlockingALocalSone() throws FcpException {
48                 Sone localSone = mock(Sone.class);
49                 when(localSone.getId()).thenReturn("LocalSone");
50                 when(localSone.isLocal()).thenReturn(true);
51                 Core core = mock(Core.class);
52                 when(core.getSone(eq("LocalSone"))).thenReturn(Optional.of(localSone));
53                 when(core.getLocalSone(eq("LocalSone"))).thenReturn(localSone);
54                 SimpleFieldSet fields = new SimpleFieldSetBuilder().put("Sone", "LocalSone").get();
55
56                 UnlockSoneCommand unlockSoneCommand = new UnlockSoneCommand(core);
57                 Response response = unlockSoneCommand.execute(fields, null, null);
58
59                 verify(core).unlockSone(eq(localSone));
60                 assertThat(response, notNullValue());
61                 assertThat(response.getReplyParameters(), notNullValue());
62                 assertThat(response.getReplyParameters().get("Message"), is("SoneUnlocked"));
63                 assertThat(response.getReplyParameters().get("Sone"), is("LocalSone"));
64         }
65
66         @Test(expected = FcpException.class)
67         public void testUnlockingARemoteSone() throws FcpException {
68                 Sone removeSone = mock(Sone.class);
69                 Core core = mock(Core.class);
70                 when(core.getSone(eq("RemoteSone"))).thenReturn(Optional.of(removeSone));
71                 SimpleFieldSet fields = new SimpleFieldSetBuilder().put("Sone", "RemoteSone").get();
72
73                 UnlockSoneCommand unlockSoneCommand = new UnlockSoneCommand(core);
74                 unlockSoneCommand.execute(fields, null, null);
75         }
76
77         @Test(expected = FcpException.class)
78         public void testMissingSone() throws FcpException {
79                 Core core = mock(Core.class);
80                 SimpleFieldSet fields = new SimpleFieldSetBuilder().get();
81
82                 UnlockSoneCommand unlockSoneCommand = new UnlockSoneCommand(core);
83                 unlockSoneCommand.execute(fields, null, null);
84         }
85
86 }