summaryrefslogtreecommitdiff
path: root/qpid/java/broker/src/test/java/org/apache/qpid/server/model/ConfiguredObjectStateTransitionTest.java
blob: 40f98b3b094eb0549e2dd2d113b0ba8f304c34a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package org.apache.qpid.server.model;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.qpid.server.BrokerOptions;
import org.apache.qpid.server.configuration.ConfigurationEntry;
import org.apache.qpid.server.configuration.ConfigurationEntryStore;
import org.apache.qpid.server.configuration.ConfiguredObjectRecoverer;
import org.apache.qpid.server.configuration.RecovererProvider;
import org.apache.qpid.server.configuration.startup.DefaultRecovererProvider;
import org.apache.qpid.server.configuration.updater.TaskExecutor;
import org.apache.qpid.server.security.auth.manager.AnonymousAuthenticationManagerFactory;
import org.apache.qpid.server.security.group.FileGroupManagerFactory;
import org.apache.qpid.server.stats.StatisticsGatherer;
import org.apache.qpid.server.util.BrokerTestHelper;
import org.apache.qpid.test.utils.QpidTestCase;

public class ConfiguredObjectStateTransitionTest extends QpidTestCase
{
    private Broker _broker;
    private RecovererProvider _recovererProvider;
    private ConfigurationEntryStore _store;
    private File _resourceToDelete;

    @Override
    public void setUp() throws Exception
    {
        super.setUp();
        BrokerTestHelper.setUp();

        _broker = BrokerTestHelper.createBrokerMock();
        StatisticsGatherer statisticsGatherer = mock(StatisticsGatherer.class);
        TaskExecutor executor = mock(TaskExecutor.class);
        when(executor.isTaskExecutorThread()).thenReturn(true);
        when(_broker.getTaskExecutor()).thenReturn(executor);

        _recovererProvider = new DefaultRecovererProvider(statisticsGatherer, _broker.getVirtualHostRegistry(),
                _broker.getLogRecorder(), _broker.getRootMessageLogger(), executor, new BrokerOptions());

        _store = mock(ConfigurationEntryStore.class);

        _resourceToDelete = new File(TMP_FOLDER, getTestName());
    }

    @Override
    public void tearDown() throws Exception
    {
        try
        {
            BrokerTestHelper.tearDown();
            if (_resourceToDelete.exists())
            {
                _resourceToDelete.delete();
            }
        }
        finally
        {
            super.tearDown();
        }
    }

    public void testGroupProviderValidStateTransitions() throws Exception
    {
        ConfigurationEntry providerEntry = getGroupProviderConfigurationEntry();
        ConfiguredObject provider = createConfiguredObject(providerEntry);
        provider.setDesiredState(State.INITIALISING, State.QUIESCED);
        assertValidStateTransition(provider, State.QUIESCED, State.STOPPED);

        provider = createConfiguredObject(providerEntry);
        assertValidStateTransition(provider, State.INITIALISING, State.DELETED);

        providerEntry = getGroupProviderConfigurationEntry();
        provider = createConfiguredObject(providerEntry);
        provider.setDesiredState(State.INITIALISING, State.QUIESCED);
        assertValidStateTransition(provider, State.QUIESCED, State.DELETED);

        providerEntry = getGroupProviderConfigurationEntry();
        provider = createConfiguredObject(providerEntry);
        provider.setDesiredState(State.INITIALISING, State.ACTIVE);
        assertValidStateTransition(provider, State.ACTIVE, State.DELETED);
    }

    public void testGroupProviderInvalidStateTransitions() throws Exception
    {
        ConfigurationEntry providerEntry = getGroupProviderConfigurationEntry();
        assertAllInvalidStateTransitions(providerEntry);
    }

    public void testAuthenticationProviderValidStateTransitions()
    {
        ConfigurationEntry providerEntry = getAuthenticationProviderConfigurationEntry();
        assertAllValidStateTransitions(providerEntry);
    }

    public void testAuthenticationProviderInvalidStateTransitions()
    {
        ConfigurationEntry providerEntry = getAuthenticationProviderConfigurationEntry();
        assertAllInvalidStateTransitions(providerEntry);
    }

    public void testPortValidStateTransitions()
    {
        ConfigurationEntry providerEntry = getPortConfigurationEntry();
        assertAllValidStateTransitions(providerEntry);
    }

    public void testPortInvalidStateTransitions()
    {
        ConfigurationEntry providerEntry = getPortConfigurationEntry();
        assertAllInvalidStateTransitions(providerEntry);
    }

    private void assertAllInvalidStateTransitions(ConfigurationEntry providerEntry)
    {
        ConfiguredObject provider = createConfiguredObject(providerEntry);
        assertInvalidStateTransition(provider, State.INITIALISING, State.REPLICA);

        provider.setDesiredState(State.INITIALISING, State.QUIESCED);
        assertInvalidStateTransition(provider, State.QUIESCED, State.INITIALISING);

        provider.setDesiredState(State.QUIESCED, State.ACTIVE);
        assertInvalidStateTransition(provider, State.ACTIVE, State.INITIALISING);

        provider.setDesiredState(State.ACTIVE, State.DELETED);
        assertInvalidStateTransition(provider, State.DELETED, State.INITIALISING);
        assertInvalidStateTransition(provider, State.DELETED, State.QUIESCED);
        assertInvalidStateTransition(provider, State.DELETED, State.ACTIVE);
        assertInvalidStateTransition(provider, State.DELETED, State.REPLICA);
        assertInvalidStateTransition(provider, State.DELETED, State.ERRORED);
    }

    private void assertAllValidStateTransitions(ConfigurationEntry providerEntry)
    {
        ConfiguredObject provider = createConfiguredObject(providerEntry);
        assertNormalStateTransition(provider);

        provider = createConfiguredObject(providerEntry);
        provider.setDesiredState(State.INITIALISING, State.QUIESCED);
        assertValidStateTransition(provider, State.QUIESCED, State.STOPPED);

        provider = createConfiguredObject(providerEntry);
        assertValidStateTransition(provider, State.INITIALISING, State.DELETED);

        provider = createConfiguredObject(providerEntry);
        provider.setDesiredState(State.INITIALISING, State.QUIESCED);
        assertValidStateTransition(provider, State.QUIESCED, State.DELETED);

        provider = createConfiguredObject(providerEntry);
        provider.setDesiredState(State.INITIALISING, State.ACTIVE);
        assertValidStateTransition(provider, State.ACTIVE, State.DELETED);
    }

    private void assertInvalidStateTransition(ConfiguredObject object, State initialState, State... invalidStates)
    {
        assertEquals("Unepxceted state", initialState, object.getActualState());
        for (State state : invalidStates)
        {
            try
            {
                object.setDesiredState(initialState, state);
            }
            catch (IllegalStateException e)
            {
                // expected
            }
            assertEquals("Transition from state " + initialState + " into state " + state + " did occur", initialState,
                    object.getActualState());
        }
    }

    private void assertValidStateTransition(ConfiguredObject object, State initialState, State... validStateSequence)
    {
        assertEquals("Unexpected state", initialState, object.getActualState());
        State currentState = initialState;
        for (State state : validStateSequence)
        {
            object.setDesiredState(currentState, state);
            assertEquals("Transition from state " + currentState + " into state " + state + " did not occur", state,
                    object.getActualState());
            currentState = state;
        }
    }

    private void assertNormalStateTransition(ConfiguredObject object)
    {
        assertValidStateTransition(object, State.INITIALISING, State.QUIESCED, State.ACTIVE, State.STOPPED, State.DELETED);
    }

    private ConfiguredObject createConfiguredObject(ConfigurationEntry entry)
    {
        @SuppressWarnings("unchecked")
        ConfiguredObjectRecoverer<ConfiguredObject> recoverer =
                (ConfiguredObjectRecoverer<ConfiguredObject>)_recovererProvider.getRecoverer(entry.getType());
        return recoverer.create(_recovererProvider, entry, _broker);
    }

    private ConfigurationEntry createConfigurationEntry(String type, Map<String, Object> attributes, ConfigurationEntryStore store)
    {
        return new ConfigurationEntry(UUID.randomUUID(), type, attributes, Collections.<UUID>emptySet(), store);
    }

    private ConfigurationEntry getAuthenticationProviderConfigurationEntry()
    {
        Map<String, Object> attributes = new HashMap<String, Object>();
        attributes.put(AuthenticationProvider.NAME, getTestName());
        attributes.put(AuthenticationProvider.TYPE, AnonymousAuthenticationManagerFactory.PROVIDER_TYPE);
        return createConfigurationEntry(AuthenticationProvider.class.getSimpleName(), attributes , _store);
    }

    private ConfigurationEntry getGroupProviderConfigurationEntry() throws Exception
    {
        Map<String, Object> attributes = new HashMap<String, Object>();
        attributes.put(GroupProvider.NAME, getTestName());
        attributes.put(GroupProvider.TYPE, FileGroupManagerFactory.GROUP_FILE_PROVIDER_TYPE);
        attributes.put(FileGroupManagerFactory.PATH, _resourceToDelete.getAbsolutePath());
        if (!_resourceToDelete.exists())
        {
            _resourceToDelete.createNewFile();
        }
        return createConfigurationEntry(GroupProvider.class.getSimpleName(), attributes , _store);
    }

    private ConfigurationEntry getPortConfigurationEntry()
    {
        ConfigurationEntry authProviderEntry = getAuthenticationProviderConfigurationEntry();
        AuthenticationProvider authProvider = (AuthenticationProvider)createConfiguredObject(authProviderEntry);

        Map<String, Object> attributes = new HashMap<String, Object>();
        attributes.put(Port.NAME, getTestName());
        attributes.put(Port.PROTOCOLS, Collections.<Protocol>singleton(Protocol.HTTP));
        attributes.put(Port.AUTHENTICATION_PROVIDER, authProvider.getName());
        attributes.put(Port.PORT, 0);

        when(_broker.findAuthenticationProviderByName(authProvider.getName())).thenReturn(authProvider);
        return createConfigurationEntry(Port.class.getSimpleName(), attributes , _store);
    }

}