summaryrefslogtreecommitdiff
path: root/qpid/java/management/client/src/test/java/org/apache/qpid/management/wsdm/SetResourcePropertiesTestCase.java
blob: 87f8905e01763df5fd8ea6524673383ac93e88fb (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
/*
 *
 * 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.wsdm;

import java.lang.reflect.Array;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.management.MBeanAttributeInfo;
import javax.xml.namespace.QName;

import org.apache.muse.ws.addressing.soap.SoapFault;
import org.apache.muse.ws.resource.WsrfConstants;
import org.apache.qpid.management.Names;

/**
 * Test case for Set Resource Properties interfaces.
 * 
 * @author Andrea Gazzarini
 */
public class SetResourcePropertiesTestCase extends BaseWsDmAdapterTestCase
{
	/**
	 * Test the WS-RP SetResourceProperty interface of the WS-DM adapter.
	 * 
	 * <br>precondition : a WS-Resource exists and is registered. 
	 * <br>postcondition : property values are correctly updated on the target WS-Resource..
	 */
	public void testSetResourcePropertiesOK() throws Exception
	{
		Map<String, Object> sampleMap = new HashMap<String, Object>();
		sampleMap.put("Key1", "BLABALABLABALBAL");
		sampleMap.put("Key2", 182838484l);
		sampleMap.put("Key3", -928376362);
		sampleMap.put("Key4", 23762736276.33D);
		sampleMap.put("Key4", 2327363.2F);
		
		Map<String, Object> sampleValues = new HashMap<String, Object>();
		sampleValues.put(String.class.getName(),"SAMPLE_STRING");
		sampleValues.put(UUID.class.getName(),UUID.randomUUID());
		sampleValues.put(Boolean.class.getName(),Boolean.FALSE);
		sampleValues.put(Map.class.getName(),sampleMap);
		sampleValues.put(Long.class.getName(),283781273L);
		sampleValues.put(Integer.class.getName(),12727);
		sampleValues.put(Short.class.getName(),new Short((short)22));
		sampleValues.put(Date.class.getName(),new Date());
		
		MBeanAttributeInfo [] attributesMetadata = _mbeanInfo.getAttributes();
		boolean atLeastThereIsOneWritableProperty = false;
		
		for (MBeanAttributeInfo attributeMetadata : attributesMetadata)
		{
			String name = attributeMetadata.getName();
			
			if (attributeMetadata.isWritable())
			{	
				atLeastThereIsOneWritableProperty = true;
				Object sampleValue = sampleValues.get(attributeMetadata.getType());
				Object []values = new Object[]{sampleValue};
				
				Object result = _managementServer.getAttribute(_resourceObjectName, name);
				if (result == null)
				{
					_resourceClient.insertResourceProperty(	
							new QName(
									Names.NAMESPACE_URI,
									name,
									Names.PREFIX),
							values);
				} else 
				{
					_resourceClient.updateResourceProperty(	
							new QName(
									Names.NAMESPACE_URI,
									name,
									Names.PREFIX),
							values);					
				}
				
				Object propertyValues = _resourceClient.getPropertyAsObject(
						new QName(
								Names.NAMESPACE_URI,
								name,
								Names.PREFIX),
						Class.forName(attributeMetadata.getType()));
				int length = Array.getLength(propertyValues);
				if (length != 0)
				{
					Object propertyValue = Array.get(propertyValues, 0);
					
					assertEquals(
							"Comparison failed for property "+name,
							sampleValue,
							propertyValue);
				} else {
					assertNull(
							String.format(
									"\"%s\" property value shouldn't be null. Its value is %s",
									name,
									_managementServer.getAttribute(_resourceObjectName,name)),
									sampleValue);
				}				
			}
		}
		assertTrue(
				"It's not possibile to run successfully this test case if " +
					"the target WS-Resource has no at least one writable property",
				atLeastThereIsOneWritableProperty);
	}

	/**
	 * Test the WS-RP SetResourceProperty interface of the WS-DM adapter when the 
	 * target property is null.
	 * According to WS-RP specs this operation is not allowed because in this case a SetResourceProperty with an "Insert"
	 * message should be sent in order to initialize the property.
	 * 
	 * <br>precondition : a ws resource exists and is registered. The value of the target property is null. 
	 * <br>postcondition : a Soap fault is received indicating the failuire.
	 */
	public void testSetResourcePropertiesKO() throws Exception
	{
		Object typePropertyValue = _managementServer.getAttribute(_resourceObjectName, "Type");
		assertNull(typePropertyValue);
		
		try 
		{		
			_resourceClient.updateResourceProperty(
					new QName(
							Names.NAMESPACE_URI,
							"Type",
							Names.PREFIX),
					new Object[]{"sampleValue"});					
			fail(
					"If the property is null on the target ws resource, according " +
					"to WS-RP specs, an update of its value is not possible.");
		} catch(SoapFault expected)
		{
			
		}
	}
	
	/**
	 * Tests the SetResourceProperties (update) interface when the request contains 
	 * an unknwon target resource.
	 * 
	 * <br>precondition : the SetResourceProperties contains an unknwon resource.
	 * <br>postcondition : a SoapFault is thrown and the corresponding detail contains an 
	 * 								UnknownResourceFault element.
	 */
	public void testUpdateResourcePropertiesKO_WithUnknownResourceFault() throws Exception
	{
		try 
		{
			_resourceClient.getEndpointReference().removeParameter(Names.RESOURCE_ID_QNAME);
			_resourceClient.getEndpointReference().addParameter(Names.RESOURCE_ID_QNAME,"lablabalbal");
			
			_resourceClient.updateResourceProperty(
					new QName(
							Names.NAMESPACE_URI,
							"Type",
							Names.PREFIX),
					new Object[]{"sampleValue"});		
		} catch(SoapFault expected)
		{
			assertEquals(
					WsrfConstants.RESOURCE_UNKNOWN_QNAME.getLocalPart(),
					expected.getDetail().getLocalName());
		}
	}
	
	/**
	 * Tests the SetResourceProperties (insert) interface when the request contains 
	 * an unknwon target resource.
	 * 
	 * <br>precondition : the SetResourceProperties contains an unknwon resource.
	 * <br>postcondition : a SoapFault is thrown and the corresponding detail contains an 
	 * 								UnknownResourceFault element.
	 */
	public void testInsertResourcePropertiesKO_WithUnknownResourceFault() throws Exception
	{
		try 
		{
			_resourceClient.getEndpointReference().removeParameter(Names.RESOURCE_ID_QNAME);
			_resourceClient.getEndpointReference().addParameter(Names.RESOURCE_ID_QNAME,"lablabalbal");
			
			_resourceClient.insertResourceProperty(
					new QName(
							Names.NAMESPACE_URI,
							"Type",
							Names.PREFIX),
					new Object[]{"sampleValue"});		
		} catch(SoapFault expected)
		{
			assertEquals(
					WsrfConstants.RESOURCE_UNKNOWN_QNAME.getLocalPart(),
					expected.getDetail().getLocalName());
		}		
	}	
}