Search in sources :

Example 21 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project grails-core by grails.

the class ChainedTransactionManagerTests method shouldRollbackAllTransactionManagers.

@Test
public void shouldRollbackAllTransactionManagers() throws Exception {
    PlatformTransactionManager first = createNonFailingTransactionManager("first");
    PlatformTransactionManager second = createNonFailingTransactionManager("second");
    setupTransactionManagers(first, second);
    createAndRollbackTransaction();
    assertThat(first, wasRolledback());
    assertThat(second, wasRolledback());
}
Also used : TestPlatformTransactionManager(org.grails.transaction.ChainedTransactionManagerTests.TestPlatformTransactionManager) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Example 22 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project opennms by OpenNMS.

the class CollectorComplianceTest method canCollectUsingMinionWorkflow.

@Test
public void canCollectUsingMinionWorkflow() throws CollectionInitializationException, CollectionException {
    Assume.assumeTrue(runsOnMinion);
    // create the agent
    OnmsNode node = mock(OnmsNode.class);
    when(node.getId()).thenReturn(1);
    OnmsIpInterface iface = mock(OnmsIpInterface.class);
    when(iface.getNode()).thenReturn(node);
    when(iface.getIpAddress()).thenReturn(InetAddrUtils.getLocalHostAddress());
    IpInterfaceDao ifaceDao = mock(IpInterfaceDao.class);
    when(ifaceDao.load(1)).thenReturn(iface);
    PlatformTransactionManager transMgr = mock(PlatformTransactionManager.class);
    final SnmpCollectionAgent agent = DefaultCollectionAgent.create(1, ifaceDao, transMgr);
    // init() should execute without any exceptions
    final ServiceCollector opennmsCollector = getCollector();
    initialize(opennmsCollector);
    // getEffectiveLocation() should return the original location
    final String targetLocation = "!" + MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID;
    assertEquals("Location cannot be altered.", targetLocation, opennmsCollector.getEffectiveLocation(targetLocation));
    // getRuntimeAttributes() should return a valid map
    final Map<String, Object> requiredParams = getRequiredParameters();
    final Map<String, Object> runtimeAttrs = opennmsCollector.getRuntimeAttributes(agent, Collections.unmodifiableMap(requiredParams));
    // marshalParameters() should marshal all parameters to strings
    final Map<String, Object> allParms = new HashMap<>();
    allParms.putAll(requiredParams);
    allParms.putAll(runtimeAttrs);
    final Map<String, String> marshaledParms = opennmsCollector.marshalParameters(Collections.unmodifiableMap(allParms));
    beforeMinion();
    // create a separate instance of the collector
    final ServiceCollector minionCollector = getNewCollector();
    // unmarshalParameters() should unmarshal all parameters from strings
    final Map<String, Object> unmarshaledParms = minionCollector.unmarshalParameters(Collections.unmodifiableMap(marshaledParms));
    // collect() should return a valid collection set
    final CollectionAgentDTO agentDTO = new CollectionAgentDTO(agent);
    final CollectionSet collectionSet = minionCollector.collect(agentDTO, Collections.unmodifiableMap(unmarshaledParms));
    assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
    afterMinion();
    // the collection set should be marshalable
    JaxbUtils.marshal(collectionSet);
    // getRrdRepository() should return a valid repository
    assertNotNull(opennmsCollector.getRrdRepository(getCollectionName()));
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) SnmpCollectionAgent(org.opennms.netmgt.collectd.SnmpCollectionAgent) HashMap(java.util.HashMap) CollectionAgentDTO(org.opennms.netmgt.collection.dto.CollectionAgentDTO) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) IpInterfaceDao(org.opennms.netmgt.dao.api.IpInterfaceDao) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) ServiceCollector(org.opennms.netmgt.collection.api.ServiceCollector) Test(org.junit.Test)

Example 23 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project opennms by OpenNMS.

the class CollectorComplianceTest method canCollectUsingOpenNMSWorkflow.

@Test
public void canCollectUsingOpenNMSWorkflow() throws CollectionInitializationException, CollectionException {
    // create the agent
    OnmsNode node = mock(OnmsNode.class);
    OnmsIpInterface iface = mock(OnmsIpInterface.class);
    when(iface.getNode()).thenReturn(node);
    when(iface.getIpAddress()).thenReturn(InetAddrUtils.getLocalHostAddress());
    IpInterfaceDao ifaceDao = mock(IpInterfaceDao.class);
    when(ifaceDao.load(1)).thenReturn(iface);
    PlatformTransactionManager transMgr = mock(PlatformTransactionManager.class);
    final SnmpCollectionAgent agent = DefaultCollectionAgent.create(1, ifaceDao, transMgr);
    // init() should execute without any exceptions
    final ServiceCollector opennmsCollector = getCollector();
    initialize(opennmsCollector);
    // getEffectiveLocation() should execute without any exceptions
    // in this context there are no requirements on its return value
    final String targetLocation = "!" + MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID;
    opennmsCollector.getEffectiveLocation(targetLocation);
    // getRuntimeAttributes() should return a valid map
    final Map<String, Object> requiredParams = getRequiredParameters();
    final Map<String, Object> runtimeAttrs = opennmsCollector.getRuntimeAttributes(agent, Collections.unmodifiableMap(requiredParams));
    // collect() should return a valid collection set
    final Map<String, Object> allParms = new HashMap<>();
    allParms.putAll(requiredParams);
    allParms.putAll(runtimeAttrs);
    final CollectionSet collectionSet = opennmsCollector.collect(agent, Collections.unmodifiableMap(allParms));
    assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
    // getRrdRepository() should return a valid repository
    assertNotNull(opennmsCollector.getRrdRepository(getCollectionName()));
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) SnmpCollectionAgent(org.opennms.netmgt.collectd.SnmpCollectionAgent) HashMap(java.util.HashMap) ServiceCollector(org.opennms.netmgt.collection.api.ServiceCollector) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) IpInterfaceDao(org.opennms.netmgt.dao.api.IpInterfaceDao) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) Test(org.junit.Test)

Example 24 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project opennms by OpenNMS.

the class PersistRegexSelectorStrategyTest method setUp.

@Before
public void setUp() throws Exception {
    ipInterfaceDao = EasyMock.createMock(IpInterfaceDao.class);
    String localhost = InetAddress.getLocalHost().getHostAddress();
    NetworkBuilder builder = new NetworkBuilder();
    builder.addNode("myNode");
    builder.addInterface(localhost).setIsManaged("M").setIsSnmpPrimary("P");
    OnmsNode node = builder.getCurrentNode();
    node.setId(1);
    OnmsIpInterface ipInterface = node.getIpInterfaces().iterator().next();
    EasyMock.expect(ipInterfaceDao.load(1)).andReturn(ipInterface).anyTimes();
    EasyMock.replay(ipInterfaceDao);
    Package pkg = new Package();
    pkg.setName("junitTestPackage");
    Filter filter = new Filter();
    filter.setContent("IPADDR != '0.0.0.0'");
    pkg.setFilter(filter);
    Service service = new Service();
    service.setName("SNMP");
    pkg.addService(service);
    Map<String, Object> map = new TreeMap<String, Object>();
    List<org.opennms.netmgt.config.collectd.Parameter> params = pkg.getService("SNMP").getParameters();
    for (org.opennms.netmgt.config.collectd.Parameter p : params) {
        map.put(p.getKey(), p.getValue());
    }
    map.put("collection", "default");
    serviceParams = new ServiceParameters(map);
    LocationAwareSnmpClient locationAwareSnmpClient = new LocationAwareSnmpClientRpcImpl(new MockRpcClientFactory());
    PlatformTransactionManager ptm = new MockPlatformTransactionManager();
    SnmpCollectionAgent agent = DefaultCollectionAgent.create(1, ipInterfaceDao, ptm);
    OnmsSnmpCollection snmpCollection = new OnmsSnmpCollection(agent, serviceParams, new MockDataCollectionConfigDao(), locationAwareSnmpClient);
    org.opennms.netmgt.config.datacollection.ResourceType rt = new org.opennms.netmgt.config.datacollection.ResourceType();
    rt.setName("myResourceType");
    StorageStrategy storageStrategy = new StorageStrategy();
    storageStrategy.setClazz("org.opennms.netmgt.collection.support.IndexStorageStrategy");
    rt.setStorageStrategy(storageStrategy);
    PersistenceSelectorStrategy persistenceSelectorStrategy = new PersistenceSelectorStrategy();
    persistenceSelectorStrategy.setClazz("org.opennms.netmgt.collectd.PersistRegexSelectorStrategy");
    Parameter param = new Parameter();
    param.setKey(PersistRegexSelectorStrategy.MATCH_EXPRESSION);
    param.setValue("#name matches '^agalue.*$'");
    persistenceSelectorStrategy.addParameter(param);
    rt.setPersistenceSelectorStrategy(persistenceSelectorStrategy);
    GenericIndexResourceType resourceType = new GenericIndexResourceType(agent, snmpCollection, rt);
    resourceA = new GenericIndexResource(resourceType, rt.getName(), new SnmpInstId("1.2.3.4.5.6.7.8.9.1.1"));
    AttributeGroupType groupType = new AttributeGroupType("mib2-interfaces", AttributeGroupType.IF_TYPE_ALL);
    MibObject mibObject = new MibObject();
    mibObject.setOid(".1.2.3.4.5.6.7.8.9.2.1");
    mibObject.setInstance("1");
    mibObject.setAlias("name");
    mibObject.setType("string");
    StringAttributeType attributeType = new StringAttributeType(resourceType, snmpCollection.getName(), mibObject, groupType);
    SnmpValue snmpValue = new Snmp4JValueFactory().getOctetString("agalue rules!".getBytes());
    resourceA.setAttributeValue(attributeType, snmpValue);
    resourceB = new GenericIndexResource(resourceType, rt.getName(), new SnmpInstId("1.2.3.4.5.6.7.8.9.1.2"));
}
Also used : StorageStrategy(org.opennms.netmgt.config.datacollection.StorageStrategy) Snmp4JValueFactory(org.opennms.netmgt.snmp.snmp4j.Snmp4JValueFactory) NetworkBuilder(org.opennms.netmgt.model.NetworkBuilder) LocationAwareSnmpClientRpcImpl(org.opennms.netmgt.snmp.proxy.common.LocationAwareSnmpClientRpcImpl) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) MockPlatformTransactionManager(org.opennms.core.test.MockPlatformTransactionManager) MockDataCollectionConfigDao(org.opennms.netmgt.dao.mock.MockDataCollectionConfigDao) OnmsNode(org.opennms.netmgt.model.OnmsNode) Service(org.opennms.netmgt.config.collectd.Service) TreeMap(java.util.TreeMap) MockPlatformTransactionManager(org.opennms.core.test.MockPlatformTransactionManager) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) IpInterfaceDao(org.opennms.netmgt.dao.api.IpInterfaceDao) MockRpcClientFactory(org.opennms.core.rpc.mock.MockRpcClientFactory) LocationAwareSnmpClient(org.opennms.netmgt.snmp.proxy.LocationAwareSnmpClient) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) Filter(org.opennms.netmgt.config.collectd.Filter) AttributeGroupType(org.opennms.netmgt.collection.api.AttributeGroupType) Parameter(org.opennms.netmgt.config.datacollection.Parameter) MibObject(org.opennms.netmgt.config.datacollection.MibObject) Package(org.opennms.netmgt.config.collectd.Package) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) MibObject(org.opennms.netmgt.config.datacollection.MibObject) PersistenceSelectorStrategy(org.opennms.netmgt.config.datacollection.PersistenceSelectorStrategy) Before(org.junit.Before)

Example 25 with PlatformTransactionManager

use of org.springframework.transaction.PlatformTransactionManager in project opennms by OpenNMS.

the class DefaultCollectionAgentTest method canGetLocationAwareAgentConfig.

/**
     * Verifies that the SNMP agent configuration is retrieved using
     * the location name that is associated with the interface/node.
     */
@Test
public void canGetLocationAwareAgentConfig() {
    // Mock the peer factory
    SnmpPeerFactory snmpPeerFactory = mock(SnmpPeerFactory.class);
    SnmpPeerFactory.setInstance(snmpPeerFactory);
    // Mock the other arguments required to create a DefaultCollectionAgent
    Integer ifaceId = 1;
    IpInterfaceDao ifaceDao = mock(IpInterfaceDao.class);
    PlatformTransactionManager transMgr = mock(PlatformTransactionManager.class);
    OnmsIpInterface ipIface = mock(OnmsIpInterface.class, RETURNS_DEEP_STUBS);
    when(ifaceDao.load(ifaceId)).thenReturn(ipIface);
    when(ipIface.getNode().getLocation().getLocationName()).thenReturn("Ocracoke");
    // Retrieve the agent configuration
    SnmpCollectionAgent agent = DefaultCollectionAgent.create(ifaceId, ifaceDao, transMgr);
    agent.getAgentConfig();
    // Verify
    verify(snmpPeerFactory, times(1)).getAgentConfig(any(), eq("Ocracoke"));
}
Also used : OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) SnmpPeerFactory(org.opennms.netmgt.config.SnmpPeerFactory) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) IpInterfaceDao(org.opennms.netmgt.dao.api.IpInterfaceDao) Test(org.junit.Test)

Aggregations

PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)52 Test (org.junit.Test)32 TransactionStatus (org.springframework.transaction.TransactionStatus)13 ITestBean (org.springframework.tests.sample.beans.ITestBean)10 TestBean (org.springframework.tests.sample.beans.TestBean)10 BeanFactory (org.springframework.beans.factory.BeanFactory)7 Method (java.lang.reflect.Method)6 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)6 TestPlatformTransactionManager (org.grails.transaction.ChainedTransactionManagerTests.TestPlatformTransactionManager)5 IpInterfaceDao (org.opennms.netmgt.dao.api.IpInterfaceDao)5 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)5 CannotCreateTransactionException (org.springframework.transaction.CannotCreateTransactionException)4 TransactionException (org.springframework.transaction.TransactionException)4 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)4 HashMap (java.util.HashMap)3 DataSource (javax.sql.DataSource)3 Before (org.junit.Before)3 OnmsNode (org.opennms.netmgt.model.OnmsNode)3 Connection (java.sql.Connection)2 Properties (java.util.Properties)2