Package org.jboss.on.embedded.manager.pc.test

Source Code of org.jboss.on.embedded.manager.pc.test.PluginContainerResourceManagerTest

/*
* Embedded Jopr Project
* Copyright (C) 2006-2009 Red Hat, Inc.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.jboss.on.embedded.manager.pc.test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import org.rhq.core.clientapi.agent.measurement.MeasurementAgentService;
import org.rhq.core.domain.configuration.Configuration;
import org.rhq.core.domain.configuration.Property;
import org.rhq.core.domain.configuration.PropertySimple;
import org.rhq.core.domain.configuration.definition.ConfigurationDefinition;
import org.rhq.core.domain.measurement.MeasurementData;
import org.rhq.core.domain.measurement.MeasurementDefinition;
import org.rhq.core.domain.resource.Resource;
import org.rhq.core.domain.resource.ResourceCategory;
import org.rhq.core.domain.resource.ResourceType;
import org.rhq.core.gui.util.PropertyIdGeneratorUtility;
import org.rhq.core.pc.PluginContainer;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import org.jboss.on.embedded.ResourceTypes;
import org.jboss.on.embedded.manager.ResourceManager;
import org.jboss.on.embedded.util.EmbeddedPluginContainerHelper;

/**
* @author Jason Dobies
*/
public class PluginContainerResourceManagerTest extends EmbeddedPluginContainerHelper
{
    // Attributes  --------------------------------------------

    private ResourceManager manager;

    // Tests  --------------------------------------------

    @BeforeClass
    public void initializeTest()
    {
        manager = getManager();
    }

    @Override
    protected String getScenario()
    {
        return "scenario3.xml";
    }

    @Test
    public void getResources()
    {
        Resource platform = manager.getPlatform();

        assert platform != null : "Platform is null";
        ResourceType resourceType = platform.getResourceType();
        assert resourceType != null : "Resource type was returned as null";
        assert platform.getResourceType().getCategory() == ResourceCategory.PLATFORM : "resource is not a platform!?";

        Set<Resource> children = platform.getChildResources();
        assert children != null : "Child resources were null";
        assert children.size() == 1 : "# of children doesn't equal 1, its [" + children.size() + "]";

    }


    //@Test
    //@TODO this doesn't work in the PluginContainerResourceManager
    public void testGetResourceType()
    {
        ResourceType resourceType = manager.getResourceType(ResourceTypes.RESOURCE_TYPE_NO_TX_DATASOURCE);

        assert resourceType != null : "Resource type was returned as null";
        assert resourceType.getName().equals(ResourceTypes.RESOURCE_TYPE_NO_TX_DATASOURCE);
    }

    //@Test
    public void testGetResourceTypeInvalidResourceType()
    {
        ResourceType resourceType = manager.getResourceType("foo");
        assert resourceType == null : "Resource type instance was returned for invalid resource type";
    }

    //@Test(dependsOnMethods = "testGetResourceType")
    //@TODO manager.getResourceType() does not work.
    public void testGetResources()
    {
        ResourceType resourceType = manager.getResourceType(ResourceTypes.RESOURCE_TYPE_JMS_TOPIC);
        Set<Resource> resources = manager.getResources(resourceType, manager.getPlatform());

        assert resourceType != null : "null ResourceType returned";
        assert resources != null : "Null service list returned";
        assert resources.size() == 1 : "Incorrect number of resources returned. Expected 1, found " + resources.size();
    }


    /**
     * Test to make sure that resources that belong to an embedded server and not the root server are accessible by the
     * getResources(String ResourceType) method
     */
    //@Test(dependsOnMethods ="testGetResourceType")
    //@TODO manager.getResourceType() does not work.
    public void testGetChildResourceByResourceType()
    {
        ResourceType resourceType = manager.getResourceType(ResourceTypes.RESOURCE_TYPE_VM_MEMORY);
        Set<Resource> resources = manager.getResources(resourceType, manager.getPlatform());

        assert resourceType != null : "null ResourceType returned";
        assert resources != null : "Null service list returned";
        assert resources.size() == 1 : "Incorrect number of resources returned. Expected 1, found " + resources.size();
    }


    /**
     * Test to make sure that resources that belong to an embedded server and not the root server are accessible by the
     * getResource(int id) method
     */
    //@Test(dependsOnMethods = "testGetResourceType")
    //@TODO manager.getResourceType() does not work.
    public void testGetChildResourceByResourceId()
    {
        ResourceType resourceType = manager.getResourceType(ResourceTypes.RESOURCE_TYPE_VM_MEMORY);
        Set<Resource> resources = manager.getResources(resourceType, manager.getPlatform());

        // get one of the returned resources
        Resource expected = null;
        for (Resource r : resources)
        {
            expected = r;
            break;
        }
        assert resourceType != null : "null ResourceType returned";
        assert expected != null : "null resource returned";

        Resource actual = manager.getResource(expected.getId());

        assert expected.equals(actual) : "actual resource was not what was expected";
    }


    //@Test(dependsOnMethods = "testGetResources")
    //@TODO manager.getResourceType() does not work.
    public void testGetResourceConfigurationDefinition()
    {
        ResourceType resourceType = manager.getResourceType(ResourceTypes.RESOURCE_TYPE_NO_TX_DATASOURCE);

        ConfigurationDefinition definition = resourceType.getResourceConfigurationDefinition();
        assert definition != null : "Definition not loaded for " + ResourceTypes.RESOURCE_TYPE_NO_TX_DATASOURCE;

        assert definition.getPropertyDefinitionSimple("jndiName") != null : "Could not find sample property jndi-name";
    }

    /**
     * related to JBNADM-1325 - resource.getId() was always returning 0
     */
    //@Test
    public void testGetNonZeroResourceId()
    {
        Resource jonds = getResourceInstance(ResourceTypes.RESOURCE_TYPE_XA_DATASOURCE, "RHQDS");
        assert jonds != null : "RHQDS could not be found in services";
        assert jonds.getId() != 0 : "Resource ID was 0";
    }


    /**
     * related to JBNADM-1330 - getRealTimeMeasurementValue would return on one MeasurementData item, even if mutliple
     * metrics were collected
     */
    //@Test
    //@TODO manager.getResourceType() does not work
    public void testGetMultipleMeasurements()
    {
        String resourceTypeName = ResourceTypes.RESOURCE_TYPE_XA_DATASOURCE;
        String resourceInstanceName = "RHQDS";
        Resource jonds = getResourceInstance(resourceTypeName, resourceInstanceName);

        assert jonds != null : "RHQDS could not be found in services";

        ResourceType resourceType = manager.getResourceType(resourceTypeName);
        Set<String> measurementNames = new HashSet<String>();

        Set<MeasurementDefinition> metricDefinitions = resourceType.getMetricDefinitions();
        for (MeasurementDefinition md : metricDefinitions)
        {
            measurementNames.add(md.getName());
        }

        // get the actual measurement data for those measurement names
        int resourceId = jonds.getId();
        String[] measurementNamesArray = measurementNames.toArray(new String[]{});
        MeasurementAgentService measurementAgentService = PluginContainer.getInstance().getMeasurementAgentService();

        ArrayList measurementDataList = new ArrayList<MeasurementData>(measurementAgentService.getRealTimeMeasurementValue(
                resourceId, null, measurementNamesArray));

        assert measurementDataList.size() == 3 : "there should be 3 measurement data items for RHQDS";
    }

    //@Test
    public void testGetResourceConfiguration()
    {
        Resource jonds = getResourceInstance(ResourceTypes.RESOURCE_TYPE_XA_DATASOURCE, "RHQDS");

        assert jonds != null : "RHQDS could not be found in services";

        Configuration configuration = manager.getResourceConfiguration(jonds);
        assert configuration != null : "Resource configuration returned as null";

        PropertySimple jndiProperty = configuration.getSimple("jndiName");
        assert jndiProperty != null : "JNDI property could not be found";
        assert "RHQDS".equals(jndiProperty.getStringValue()) : "Value read incorrectly: " + jndiProperty.getStringValue();
    }

    //@Test
    // no tests in core-gui so test here
    public void testPropertyIdGeneratorUtility()
    {
        Resource jonds = getResourceInstance(ResourceTypes.RESOURCE_TYPE_XA_DATASOURCE, "RHQDS");

        assert jonds != null : "RHQDS could not be found in services";

        Configuration configuration = manager.getResourceConfiguration(jonds);
        assert configuration != null : "Resource configuration returned as null";

        for (Property prop : configuration.getProperties())
        {
            String id = PropertyIdGeneratorUtility.getIdentifier(prop, null);
            assert id != null;
            assert !id.equals("");
            System.out.println(id);
        }
    }

    //@Test
    public void testRenameDatasourceJndi()
    {
        //ResourceType resourceType = manager.getResourceType(ResourceTypes.RESOURCE_TYPE_XA_DATASOURCE);

        // Find the datasource to be modified and ensure it's there
        Resource datasource = getResourceInstance(ResourceTypes.RESOURCE_TYPE_XA_DATASOURCE, "RHQDS");

        assert datasource != null : "Test datasource could not be retrieved";

        // Rename the JNDI name for that resource

        Configuration configuration = manager.getResourceConfiguration(datasource);
        assert configuration.getSimple("jndiName").getStringValue().equals("RHQDS") : "Incorrect configuration retrieved";

        configuration = configuration.deepCopy();
        configuration.getSimple("jndiName").setStringValue("NewDS");

        // Tell the manager to update the configuration

        manager.updateResource(datasource, configuration);

        // Configuration changes are done on a separate thread, so wait to allow them to finish before continuing
        try
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        // Trigger a discovery, which should cause a new resource to be detected as NewDS.
        // RHQDS should be no longer detected and thus not returned after the discovery.

        manager.discoverResources();

        // Search the resources to see if a the datasource name has been updated

        //Resource jonDs = getResourceInstance( resourceType, "RHQDS" );
        //Resource newDs = getResourceInstance( resourceType, "NewDS" );

        Resource jonDs = getResourceInstance(ResourceTypes.RESOURCE_TYPE_XA_DATASOURCE, "RHQDS");
        Resource newDs = getResourceInstance(ResourceTypes.RESOURCE_TYPE_XA_DATASOURCE, "NewDS");

        assert newDs != null : "NewDS was not discovered as a datasource";
        assert jonDs == null : "RHQDS was still discovered as a datasource";

    }

    //@Test
    //@TODO getResourceType is currently not working correct
    public void testDeleteRenamedDatasource()
    {
        ResourceType resourceType = manager.getResourceType(ResourceTypes.RESOURCE_TYPE_LOCAL_DATASOURCE);

        // Find the datasource to be modified and ensure it's there
        Set<Resource> resources = manager.getResources(resourceType, manager.getPlatform());

        Resource datasource = getResourceInstance(resourceType, "DefaultDS");

        assert datasource != null : "Test datasource could not be retrieved";

        // Rename the JNDI name for that resource

        Configuration configuration = manager.getResourceConfiguration(datasource);
        assert configuration.getSimple("jndiName").getStringValue().equals("DefaultDS") : "Incorrect configuration retrieved";

        configuration = configuration.deepCopy();
        configuration.getSimple("jndiName").setStringValue("DeleteMeDS");

        // Tell the manager to update the configuration

        manager.updateResource(datasource, configuration);

        // Configuration changes are done on a separate thread, so wait to allow them to finish before continuing
        try
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        // Trigger a discovery, which should cause a new resource to be detected as NewDS.
        // RHQDS should be no longer detected and thus not returned after the discovery.

        manager.discoverResources();

        Resource renamedDs = getResourceInstance(resourceType, "DeleteMeDS");

        assert renamedDs != null : "Could not find renamed datasource";

        manager.removeResource(renamedDs);

        // Trigger a discovery, which should not return the renamed DS

        manager.discoverResources();

        // See if deleted resource came back
        renamedDs = getResourceInstance(resourceType, "DeleteMeDS");

        assert renamedDs == null : "Renamed resource was not deleted correctly";
    }
}
TOP

Related Classes of org.jboss.on.embedded.manager.pc.test.PluginContainerResourceManagerTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.