1 package com.atlassian.plugin.manager.store;
2
3 import com.atlassian.plugin.Plugin;
4 import com.atlassian.plugin.impl.StaticPlugin;
5 import com.atlassian.plugin.manager.PluginPersistentState;
6 import com.atlassian.plugin.manager.PluginPersistentStateStore;
7 import org.junit.Before;
8 import org.junit.Test;
9 import org.junit.runner.RunWith;
10 import org.mockito.Mock;
11 import org.mockito.invocation.InvocationOnMock;
12 import org.mockito.runners.MockitoJUnitRunner;
13 import org.mockito.stubbing.Answer;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import static com.atlassian.plugin.manager.store.PluginPersistentStateStoreMigrator.removeDirectives;
19 import static org.hamcrest.MatcherAssert.assertThat;
20 import static org.hamcrest.Matchers.hasKey;
21 import static org.hamcrest.Matchers.is;
22 import static org.mockito.Matchers.isA;
23 import static org.mockito.Mockito.doAnswer;
24 import static org.mockito.Mockito.when;
25
26 @RunWith(MockitoJUnitRunner.class)
27 public class TestPluginPersistentStateStoreMigrator {
28
29 @Mock
30 private PluginPersistentStateStore customStore;
31
32 @Before
33 public void setUp() throws Exception {
34 PluginPersistentState.Builder builder = PluginPersistentState.Builder.create();
35
36 Plugin plugin = new StaticPlugin();
37 plugin.setKey("foo111");
38 plugin.setEnabledByDefault(true);
39 builder.setEnabled(plugin, false);
40
41 final Map<String, Boolean> state = new HashMap<>();
42 state.put("foo-1.0.0", Boolean.TRUE);
43 state.put("restarting--foo2-1.0.0", Boolean.TRUE);
44 state.put("foo;singleton:=true-1.0.0", Boolean.TRUE);
45 state.put("bar;singleton:=true-1.0.0", Boolean.TRUE);
46 state.put("bar;singleton:=true-2.0.0", Boolean.TRUE);
47 state.put("alpha;singleton:=true", Boolean.TRUE);
48 state.put("beta-2.0.0", Boolean.TRUE);
49 builder.addState(state);
50
51 when(customStore.load()).thenReturn(builder.toState());
52 doAnswer(new Answer<Void>() {
53 @Override
54 public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
55 final PluginPersistentState pluginState = (PluginPersistentState) invocationOnMock.getArguments()[0];
56 when(customStore.load()).thenReturn(pluginState);
57 return null;
58 }
59 }).when(customStore).save(isA(PluginPersistentState.class));
60 }
61
62 @Test
63 public void testRemoveDirectives() throws Exception {
64 removeDirectives(customStore);
65
66 Map<String, Boolean> newState = customStore.load().getMap();
67 assertThat(newState.size(), is(7));
68 assertThat(newState, hasKey("foo111"));
69 assertThat(newState, hasKey("foo-1.0.0"));
70 assertThat(newState, hasKey("restarting--foo2-1.0.0"));
71 assertThat(newState, hasKey("bar-1.0.0"));
72 assertThat(newState, hasKey("bar-2.0.0"));
73 assertThat(newState, hasKey("alpha"));
74 assertThat(newState, hasKey("beta-2.0.0"));
75 }
76 }