I added a little bit to our Arquillian framework to make it easier to re-use plugin
descriptors.
As it stood before, you had to write your own discovery/resource components and your own
XML descriptor for your tests. Even if everything else was the same, but your resource
component class needed to do something different (to do things that you need to test if
they are working) you had to write another XML descriptor because the <server> class
attribute had to point to your own component class.
What I did was add a method to org.rhq.test.shrinkwrap.RhqAgentPluginDescriptorContainer
called "setPluginDescriptorFromTemplate".
You pass in the name of the descriptor .xml file (as found in the classloader - this is
just the test classloader that Maven gives you - if you put your .xml file in the
src/test/resources folder, it can be found - just pass in the .xml filename) and you also
pass in a Map<String,String> whose keys are token names and values are replacement
strings. This way, you can have one xml descriptor "template" and you just pass
in replacement strings that replace the tokens with your tests own custom values.
For example, we now have a single .xml descriptor template called
"single-metric-rhq-plugin.xml" - it is simple. It defines one <server>
with one <metric>. If you have any tests that need to test metric collections (and
you only need a single resource with a single metric to do your test) you can reuse that
template. A portion of that .xml template looks like this:
<server name="SingleMetricServer"
discovery="@@@discovery@@@"
class="@@@class@@@">
<metric property="metric1"
dataType="measurement"
defaultOn="true"
displayType="summary"
defaultInterval="30000" />
</server>
See the @@@discovery@@@ and @@@class@@@? Those need to be replaced by your test. You do
that by passing in a Map with two keys - one key of "@@@discovery@@@" and one
key with "@@@class@@@" - the values being your component class names.
I also included a simple, reusable discovery component that will discover a single
resource. Many tests just want to get a single resource into inventory that we can test
with - this SingleResourceDiscoveryComponent does that (the full class name is
org.rhq.plugins.test.SingleResourceDiscoveryComponent).
So, a test that wants to get into inventory a single resource that has a single metric can
do this in its getTestPlugin method:
@Deployment(name = "SingleMetricPlugin")
@TargetsContainer("connected-pc-with-metric-collection")
public static RhqAgentPluginArchive getTestPlugin() {
RhqAgentPluginArchive pluginJar = ShrinkWrap.create(RhqAgentPluginArchive.class,
"single-metric-plugin-1.0.jar");
HashMap<String, String> replacements = new HashMap<String, String>();
replacements.put("@@@discovery@@@",
SingleResourceDiscoveryComponent.class.getName());
replacements.put("@@@class@@@",
BZ834019ResourceComponent.class.getName());
return
pluginJar.setPluginDescriptorFromTemplate("single-metric-rhq-plugin.xml",
replacements).addClasses(
SingleResourceDiscoveryComponent.class, BZ834019ResourceComponent.class);
}
Notice I put @@@discovery@@@ in the map, with the value as the full classname of that
shared SingleResourceDiscoveryComponent. But I put in my test's own custom resource
component in the @@@class@@@ entry.
We can use this setPluginDescriptorFromTemplate for any template you want - just create a
.xml file that you think could be reusable, templatize it with strings like @@@variable@@@
and use code such as above to create test plugins using your template.
Show replies by date