summaryrefslogtreecommitdiff
path: root/trunk/qpid/java/management/client/src/test/java/org/apache/qpid/management/domain/model/QpidEventTest.java
blob: 4b36d9e5cc7b67e54ceb19162f7149d2ddfaa933 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 *
 * 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.domain.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.ObjectName;
import javax.management.ReflectionException;

import junit.framework.TestCase;

import org.apache.qpid.management.TestConstants;
import org.apache.qpid.management.configuration.ConfigurationException;
import org.apache.qpid.management.configuration.Configurator;
import org.apache.qpid.management.domain.model.QpidEvent.QManManagedEvent;

/**
 * Test case for qpid class entity.
 * 
 * @author Andrea Gazzarini
 */
public class QpidEventTest extends TestCase
{
    private QpidEvent _event;
    private QpidPackage _qpidPackage;
    
    @Override
    protected void setUp () throws Exception
    {
        _qpidPackage = new QpidPackage(TestConstants.QPID_PACKAGE_NAME,TestConstants.DOMAIN_MODEL);
        _event = new QpidEvent(TestConstants.BIND_EVENT_NAME,TestConstants.HASH,_qpidPackage);
    }
        
    /**
     * Tests the execution of the createEventInstance() method.
     * Basically it tests the addition of a new event instance.
     * 
     * <br>precondition: event deifinition has no object instances.
     * <br>precondition : event definition contains the new object instance.
     */
    public void testCreateEventInstance() 
    {            
        assertTrue(
                "A just created event should be empty. I mean there shouldn't be event instances inside.", 
        		_event.hasNoInstances());

        QManManagedEvent instance = createEventInstance();
        
        assertFalse (
                "Now a new instance should be there...",
                _event.hasNoInstances());
        
        assertEquals(TestConstants.TEST_RAW_DATA,instance._rawEventData);
        assertEquals(TestConstants.NOW,instance._timestamp);
        assertEquals(TestConstants.SEVERITY, instance._severity);
    }
    
    /**
     * Tests the internal state change of an event definition.
     */
    public void testStateChange() throws UnableToBuildFeatureException
    {
    	// Let's override this class because this is not an online tests and therefore
    	// QMan is not supposed to be up.
    	_event = new QpidEvent(TestConstants.BIND_EVENT_NAME,TestConstants.HASH,_qpidPackage)
    	{
    		@Override
    		void requestSchema() throws Exception {
    			// Do Nothing.
    		}
    		
    		@Override
    		void setSchema(List<Map<String, Object>> argumentDefinitions)throws UnableToBuildFeatureException {
    			_state = _schemaInjected;
    		}
    	};
    	
    	assertSame(
    			"Initial state doesn't match.",
    			_event._schemaNotRequested,
    			_event._state);
    	
    	_event.addEventData(TestConstants.TEST_RAW_DATA, TestConstants.NOW, TestConstants.SEVERITY);
    	
    	assertSame(
    			"Request schema has been requested but not yet injected. The current state is not indicating that!",
    			_event._schemaRequestedButNotYetInjected,
    			_event._state);
    	
    	_event.setSchema(TestConstants.EMPTY_ARGUMENTS_SCHEMA);
    	
    	assertSame(
    			"Request schema has been injected. The current state is not indicating that!",
    			_event._schemaInjected,
    			_event._state);    	
    }
    
    /**
     * Tests the injection of a valid schema on a QpidEvent.
     * 
     * <br>precondition : a valid arguments is injected on the qpid event.
     * <br>postcondition : event definition is built successfully.
     */
    public void testSchemaInjectionOK() throws UnableToBuildFeatureException, ConfigurationException, InstanceNotFoundException, MBeanException, ReflectionException
    {
    	_event = new QpidEvent(TestConstants.BIND_EVENT_NAME,TestConstants.HASH,_qpidPackage)
    	{
    		@Override
    		void requestSchema() throws Exception 
    		{
    			// DO NOTHING : QMan is not running and therefore the schema will be manually injected.
    		}
    		
    		@Override
    		void updateEventInstanceWithData(QManManagedEvent instance) 
    		{
    			// DO NOTHING : otherwise we should supply a valid raw data to be converted. ;-)
    		}
    	};
    	
       Configurator configurator = new Configurator();
        configurator.configure();

        List<Map<String,Object>> arguments = new ArrayList<Map<String, Object>>();
        arguments.add(createArgument(TestConstants.AGE_ATTRIBUTE_NAME, TestConstants.AGE_ATTRIBUTE_DESCRIPTION));
        arguments.add(createArgument(TestConstants.SURNAME_ATTRIBUTE_NAME, TestConstants.SURNAME_ATTRIBUTE_DESCRIPTION));
        
        // Incoming data : that will fire the schema request and a state change from schema-not-requested to schema-requested-but-not-injected
        _event.addEventData(TestConstants.TEST_RAW_DATA, TestConstants.NOW, TestConstants.SEVERITY);
        
        // I must be sure that what is obvious for me it's obvious for QMan... :)
        assertSame(
    			"Request schema has been requested but not yet injected. The current state is not indicating that!",
    			_event._schemaRequestedButNotYetInjected,
    			_event._state);
        
        // Inject schema
        _event.setSchema(arguments);

        // Arguments must be 2 + 2 (severity)
        assertEquals(2,_event._arguments.size());
        
        QpidProperty argument = _event._arguments.get(TestConstants.AGE_ATTRIBUTE_NAME);
        assertEquals(TestConstants.AGE_ATTRIBUTE_NAME,argument.getName());
        assertEquals(AccessMode.RO,argument.getAccessMode());
        assertEquals(TestConstants.AGE_ATTRIBUTE_DESCRIPTION,argument.getDescription());
        assertEquals(Short.class,argument.getJavaType());
        assertFalse(argument.isOptional());
        
        argument = _event._arguments.get(TestConstants.SURNAME_ATTRIBUTE_NAME);
        assertEquals(TestConstants.SURNAME_ATTRIBUTE_NAME,argument.getName());
        assertEquals(AccessMode.RO,argument.getAccessMode());
        assertEquals(TestConstants.SURNAME_ATTRIBUTE_DESCRIPTION,argument.getDescription());
        assertEquals(Short.class,argument.getJavaType());
        assertFalse(argument.isOptional());
        
        assertEquals(1,_event._eventInstances.size());
        
        JmxService service = new JmxService();
        Set<ObjectName> objectNames = service.getEventMBeans();
        
        assertEquals(1,objectNames.size());
    }    
    
    /**
     * Tests the behaviour of the event class when a schema request can't be made.
     * 
     * <br>precondition : event must be in "schema-not-requested" state when incoming data arrives.
     * <br>postcondition : no exception is thrown and no state transition happens.
     */
    public void testStateChange_withRequestSchemaFailure()
    {
    	_event = new QpidEvent(TestConstants.BIND_EVENT_NAME,TestConstants.HASH,_qpidPackage)
    	{
    		@Override
    		void requestSchema() throws Exception {
    			throw new Exception();
    		}
    		
    		@Override
    		void setSchema(List<Map<String, Object>> argumentDefinitions)throws UnableToBuildFeatureException {
    			_state = _schemaInjected;
    		}
    	};
    	
    	assertSame(
    			"Initial state must be schema-not-requested.",
    			_event._schemaNotRequested,
    			_event._state);
    	
    	_event.addEventData(TestConstants.TEST_RAW_DATA, TestConstants.NOW, TestConstants.SEVERITY);
    	
    	assertSame(
    			"Current state must be still schema-not-requested.",
    			_event._schemaNotRequested,
    			_event._state);
    }
    
    /**
     * Tests the behaviour of the event class when a schema injection fails.
     * 
     * <br>precondition : event must be in "schema-not-requested" state when incoming data arrives.
     * <br>postcondition : an exception is thrown and no state transition happens.
     */
    public void testStateChange_withSchemaInjectionFailure()
    {
    	_event = new QpidEvent(TestConstants.BIND_EVENT_NAME,TestConstants.HASH,_qpidPackage)
    	{
    		@Override
    		void requestSchema() throws Exception {
    			// DO NOTHING.
    		}
    		
    		@Override
    		void setSchema(List<Map<String, Object>> argumentDefinitions)throws UnableToBuildFeatureException {
    			throw new UnableToBuildFeatureException("");
    		}
    	};
    	
    	assertSame(
    			"Initial state must be schema-not-requested.",
    			_event._schemaNotRequested,
    			_event._state);
    	
    	_event.addEventData(TestConstants.TEST_RAW_DATA, TestConstants.NOW, TestConstants.SEVERITY);
    	
      	assertSame(
    			"Request schema has been requested but not yet injected. The current state is not indicating that!",
    			_event._schemaRequestedButNotYetInjected,
    			_event._state);
      	
      	try {
			_event.setSchema(TestConstants.EMPTY_ARGUMENTS_SCHEMA);
			fail("If we are here something was wrong becuase the setSchema() of this event is throwing an exception...");
		} catch (UnableToBuildFeatureException expected) {
	      	assertSame(
	    			"Request schema has been requested but not yet injected. The current state is not indicating that!",
	    			_event._schemaRequestedButNotYetInjected,
	    			_event._state);
		}
    }    

    /**
     * Factory method for qpid managed event instances.
     * 
     * @return a new QpidManagedEvent with test data inside.
     */
    private QManManagedEvent createEventInstance()
    {
    	return  _event.createEventInstance(
        		TestConstants.TEST_RAW_DATA, 
        		TestConstants.NOW,
        		TestConstants.SEVERITY);
    }
    
    /**
     * Factory method for event argument.
     * 
     * @return a new argument metadata.
     */
    private Map<String,Object> createArgument(String name,String desc)
    {
    	 Map <String,Object> argument = new HashMap<String, Object>();
         argument.put(QpidFeatureBuilder.Attribute.name.name(),name);
         argument.put(QpidFeatureBuilder.Attribute.desc.name(), desc);
         argument.put(QpidFeatureBuilder.Attribute.type.name(), TestConstants._1);       
         return argument;
    }
}