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.PluginEnabledState;
6 import com.atlassian.plugin.manager.PluginPersistentState;
7 import com.atlassian.plugin.manager.PluginPersistentStateStore;
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.junit.runner.RunWith;
11 import org.mockito.Mock;
12 import org.mockito.junit.MockitoJUnitRunner;
13
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import static com.atlassian.plugin.manager.PluginEnabledState.getPluginEnabledStateWithCurrentTime;
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.ArgumentMatchers.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, PluginEnabledState> state = new HashMap<>();
42 state.put("foo-1.0.0", pluginEnabled());
43 state.put("restarting--foo2-1.0.0", pluginEnabled());
44 state.put("foo;singleton:=true-1.0.0", pluginEnabled());
45 state.put("bar;singleton:=true-1.0.0", pluginEnabled());
46 state.put("bar;singleton:=true-2.0.0", pluginEnabled());
47 state.put("alpha;singleton:=true", pluginEnabled());
48 state.put("beta-2.0.0", pluginEnabled());
49 builder.addPluginEnabledState(state);
50
51 when(customStore.load()).thenReturn(builder.toState());
52 doAnswer(invocationOnMock -> {
53 final PluginPersistentState pluginState = (PluginPersistentState) invocationOnMock.getArguments()[0];
54 when(customStore.load()).thenReturn(pluginState);
55 return null;
56 }).when(customStore).save(isA(PluginPersistentState.class));
57 }
58
59 @Test
60 public void testRemoveDirectives() {
61 removeDirectives(customStore);
62
63 Map<String, PluginEnabledState> newState = customStore.load().getStatesMap();
64 assertThat(newState.size(), is(7));
65 assertThat(newState, hasKey("foo111"));
66 assertThat(newState, hasKey("foo-1.0.0"));
67 assertThat(newState, hasKey("restarting--foo2-1.0.0"));
68 assertThat(newState, hasKey("bar-1.0.0"));
69 assertThat(newState, hasKey("bar-2.0.0"));
70 assertThat(newState, hasKey("alpha"));
71 assertThat(newState, hasKey("beta-2.0.0"));
72 }
73
74 private static PluginEnabledState pluginEnabled(){
75 return getPluginEnabledStateWithCurrentTime(true);
76 }
77 }