1 package com.atlassian.plugin.manager;
2
3 import com.atlassian.plugin.ModuleDescriptor;
4 import com.atlassian.plugin.Plugin;
5 import com.atlassian.plugin.impl.StaticPlugin;
6 import org.junit.Before;
7 import org.junit.Test;
8 import org.junit.runner.RunWith;
9 import org.mockito.ArgumentCaptor;
10 import org.mockito.Mock;
11 import org.mockito.invocation.InvocationOnMock;
12 import org.mockito.junit.MockitoJUnitRunner;
13 import org.mockito.stubbing.Answer;
14
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.Matchers.is;
17 import static org.mockito.ArgumentMatchers.isA;
18 import static org.mockito.Mockito.doAnswer;
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.Mockito.when;
21
22 @RunWith(MockitoJUnitRunner.Silent.class)
23 public class TestPluginPersistentStateModifier {
24 Plugin plugin;
25 PluginPersistentStateModifier persistentStateModifier;
26 ArgumentCaptor<PluginPersistentState> argument;
27
28 @Mock
29 ModuleDescriptor descriptor;
30
31 @Mock
32 PluginPersistentStateStore customStore;
33
34 @Before
35 public void setUp() throws Exception {
36 plugin = new StaticPlugin();
37 plugin.setKey("foo");
38 plugin.setEnabledByDefault(true);
39
40 PluginPersistentState.Builder builder = PluginPersistentState.Builder.create();
41 builder.setEnabled(plugin, false);
42 when(customStore.load()).thenReturn(builder.toState());
43 doAnswer(new Answer<Void>() {
44 @Override
45 public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
46 final PluginPersistentState pluginState = (PluginPersistentState) invocationOnMock.getArguments()[0];
47 when(customStore.load()).thenReturn(pluginState);
48 return null;
49 }
50 }).when(customStore).save(isA(PluginPersistentState.class));
51
52 when(descriptor.getCompleteKey()).thenReturn("foo:bar");
53
54 persistentStateModifier = new PluginPersistentStateModifier(customStore);
55 argument = ArgumentCaptor.forClass(PluginPersistentState.class);
56 }
57
58 @Test
59 public void testDisablePlugin() throws Exception {
60 plugin.setEnabledByDefault(true);
61 persistentStateModifier.disable(plugin);
62
63 verify(customStore).save(argument.capture());
64 assertThat(argument.getValue().isEnabled(plugin), is(false));
65 }
66
67 @Test
68 public void testEnablePlugin() throws Exception {
69 plugin.setEnabledByDefault(false);
70 persistentStateModifier.enable(plugin);
71
72 verify(customStore).save(argument.capture());
73 assertThat(argument.getValue().isEnabled(plugin), is(true));
74 }
75
76 @Test
77 public void testDisableModule() throws Exception {
78 when(descriptor.isEnabledByDefault()).thenReturn(true);
79 persistentStateModifier.disable(descriptor);
80
81 verify(customStore).save(argument.capture());
82 assertThat(argument.getValue().isEnabled(descriptor), is(false));
83 }
84
85 @Test
86 public void testEnableModule() throws Exception {
87 when(descriptor.isEnabledByDefault()).thenReturn(false);
88 persistentStateModifier.enable(descriptor);
89
90 verify(customStore).save(argument.capture());
91 assertThat(argument.getValue().isEnabled(descriptor), is(true));
92 }
93 }