summaryrefslogtreecommitdiff
path: root/M4-RCs/qpid/java/management/client/src/test/java/org/apache/qpid/management/configuration/ConfigurationTest.java
blob: efd5990bd76684823b44f2118d58dd9e9c0ded2c (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
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.management.configuration;

import java.util.Map;
import java.util.UUID;

import org.apache.qpid.management.TestConstants;
import org.apache.qpid.management.domain.handler.base.IMessageHandler;
import org.apache.qpid.management.domain.handler.impl.ConfigurationMessageHandler;
import org.apache.qpid.management.domain.handler.impl.InstrumentationMessageHandler;
import org.apache.qpid.management.domain.handler.impl.SchemaResponseMessageHandler;
import org.apache.qpid.management.domain.model.AccessMode;
import org.apache.qpid.management.domain.model.type.Type;
import org.apache.qpid.management.domain.model.type.Uint8;

import junit.framework.TestCase;

/**
 * Test case for Configuration singleton.
 * 
 * @author Andrea Gazzarini
 */
public class ConfigurationTest extends TestCase
{
    /**
     * Tests the singleton behaviour of the configuration object.
     */
    public void testSingleton()
    {
        assertSame(Configuration.getInstance(),Configuration.getInstance());
    }
    
    /**
     * Tests the execution of getType() method when a valid code is supplied.
     * 
     * <br>precondition : the requested type already exist on the configuration.
     * <br>postcondition : the requested type is returned and no exception is thrown.
     */
    public void testGetTypeOk() throws UnknownTypeCodeException 
    {
        TypeMapping mapping = new TypeMapping(TestConstants.VALID_CODE,new Uint8());
        Configuration.getInstance().addTypeMapping(mapping);
        Type type = Configuration.getInstance().getType(TestConstants.VALID_CODE);
        
        assertTrue(type instanceof Uint8);
    }
    
    /**
     * Tests the execution of getType() method when a unknown code is supplied.
     * 
     * <br>precondition : the requested type doesn't exist on the configuration.
     * <br>postcondition : an exception is thrown indicating the failure.
     */
    public void testGetTypeKO()
    {
        try
        {
            Configuration.getInstance().getType(Integer.MIN_VALUE);
            fail("If an unknwon code is supplied an exception must be thrown.");
        } catch (UnknownTypeCodeException expected)
        {
            assertEquals(Integer.MIN_VALUE,expected.getCode());
        }        
    }
    
    /**
     * Tests the execution of getAccessMode() method when a valid code is supplied.
     * 
     * <br>precondition : the requested access mode already exist on the configuration.
     * <br>postcondition : the requested access mode is returned and no exception is thrown.
     */
    public void testGetAccessModeOk() throws UnknownAccessCodeException
    {
        AccessModeMapping mapping = new AccessModeMapping(TestConstants.VALID_CODE,AccessMode.RW);
        Configuration.getInstance().addAccessModeMapping(mapping);
        AccessMode accessMode = Configuration.getInstance().getAccessMode(TestConstants.VALID_CODE);

        assertSame(AccessMode.RW,accessMode);
    }
    
    /**
     * Tests the execution of getAccessMode() method when a unknown code is supplied.
     * 
     * <br>precondition : the requested access mode doesn't exist on the configuration.
     * <br>postcondition : an exception is thrown indicating the failure.
     */
    public void testGetAccessModeKO()
    {
        try
        {
            Configuration.getInstance().getAccessMode(Integer.MIN_VALUE);
            fail("If an unknwon code is supplied an exception must be thrown.");
        } catch (UnknownAccessCodeException expected)
        {
            assertEquals(Integer.MIN_VALUE,expected.getCode());
        }        
    }    
    
    /**
     * Tests the execution of the getBrokerConnectionData when a valid broker id is supplied.
     * 
     * <br>precondition : on configuration a connection data is stored and associated with the supplied id.
     * <br>postcondition : the requested connection data is returned and no exception is thrown.
     */
    public void testGetBrokerConnectionDataOK() throws Exception 
    {
        BrokerConnectionData connectionData = new BrokerConnectionData();
        connectionData.setHost("host");
        connectionData.setPort("7001");
        connectionData.setInitialPoolCapacity("0");
        connectionData.setMaxPoolCapacity("10");
        connectionData.setMaxWaitTimeout("1");
        Configuration.getInstance().addBrokerConnectionData(TestConstants.BROKER_ID, connectionData);
        
        BrokerConnectionData result = Configuration.getInstance().getBrokerConnectionData(TestConstants.BROKER_ID);
        assertSame(connectionData, result);
    }
    
    /**
     * Tests the execution of the getBrokerConnectionData when a unknown broker id is supplied.
     * 
     * <br>precondition : on configuration there's no connection data associated with the supplied id.
     * <br>postcondition : an exception is thrown indicating the failure.
     */
    public void testGetBrokerConnectionDataKO_withUnknownBrokerId() 
    {
        UUID brokerId = UUID.randomUUID();
        try 
        {
            Configuration.getInstance().getBrokerConnectionData(brokerId);
            fail("If an unknown broker id is supplied then an exception must be thrown.");
        } catch(UnknownBrokerException expected) 
        {
            assertEquals(brokerId.toString(),expected.getMessage());
        }
    }    
    
    /**
     * Tests the execution of the getBrokerConnectionData when a null id is supplied.
     * 
     * <br>precondition : a null broker is given.
     * <br>postcondition : an exception is thrown indicating the failure.
     */
    public void testGetBrokerConnectionDataKO_withNullBrokerId() 
    {
        try 
        {
            Configuration.getInstance().getBrokerConnectionData(null);
            fail("If a null broker id is supplied then an exception must be thrown.");
        } catch(UnknownBrokerException expected) 
        {
        }
    }       

    /**
     * Tests the behaviour of the getManagementQueueHandlers() method.
     * 
     * <br>precondition: 2 management handlers are in stored configuration
     * <br>postcondition : 2 management handlers are returned.
     */
    public void testGetManagementQueueHandlersOk() 
    {
        String i = "i";
        String c = "c";
        
        String instrMessageHandlerClassName = InstrumentationMessageHandler.class.getName();
        String configMessageHandlerClassName = ConfigurationMessageHandler.class.getName();
        
        MessageHandlerMapping instrMapping = new MessageHandlerMapping();
        MessageHandlerMapping configMapping = new MessageHandlerMapping();
        
        instrMapping.setOpcode(i);
        instrMapping.setMessageHandlerClass(instrMessageHandlerClassName);

        configMapping.setOpcode(c);
        configMapping.setMessageHandlerClass(configMessageHandlerClassName);
        
        Configuration.getInstance().addManagementMessageHandlerMapping(instrMapping);
        Configuration.getInstance().addManagementMessageHandlerMapping(configMapping);
        
        Map<Character, IMessageHandler> handlerMappings = Configuration.getInstance().getManagementQueueHandlers();
        
        assertEquals(instrMessageHandlerClassName,handlerMappings.get(instrMapping.getOpcode()).getClass().getName());
        assertEquals(configMessageHandlerClassName,handlerMappings.get(configMapping.getOpcode()).getClass().getName());        
    }
    
    /**
     * Tests the behaviour of the getManagementQueueHandlers() method.
     * 
     * <br>precondition: 2 management handlers are in stored configuration
     * <br>postcondition : 2 management handlers are returned.
     */
    public void testGetMethodReplyQueueHandlersOk() 
    {
        String s = "s";
        
        String schemaMessageHandlerClassName = SchemaResponseMessageHandler.class.getName();
        
        MessageHandlerMapping schemaMapping = new MessageHandlerMapping();
        
        schemaMapping.setOpcode(s);
        schemaMapping.setMessageHandlerClass(schemaMessageHandlerClassName);

        Configuration.getInstance().addMethodReplyMessageHandlerMapping(schemaMapping);
        
        Map<Character, IMessageHandler> handlerMappings = Configuration.getInstance().getMethodReplyQueueHandlers();
        
        assertEquals(schemaMessageHandlerClassName,handlerMappings.get(schemaMapping.getOpcode()).getClass().getName());
    }    
}