Search in sources :

Example 1 with VariableNameExpansion

use of org.opennms.features.ifttt.helper.VariableNameExpansion in project opennms by OpenNMS.

the class IfTttDaemonTest method runIfTttDaemonTest.

public Map<String, List<ResultEntry>> runIfTttDaemonTest(int timeout, int entryCount) throws Exception {
    final AlarmDao alarmDao = mock(AlarmDao.class);
    when(alarmDao.findMatching((Criteria) Matchers.anyObject())).thenReturn(alarmMap.values().stream().collect(Collectors.toList()));
    final TransactionOperations transactionOperations = mock(TransactionOperations.class);
    when(transactionOperations.execute(Matchers.anyObject())).thenAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            TransactionCallbackWithoutResult transactionCallbackWithoutResult = invocationOnMock.getArgumentAt(0, TransactionCallbackWithoutResult.class);
            transactionCallbackWithoutResult.doInTransaction(null);
            return null;
        }
    });
    // final List<ResultEntry> receivedEntries = new ArrayList<>();
    final Map<String, List<ResultEntry>> receivedEntries = new HashMap<>();
    final IfTttDaemon ifTttDaemon = new IfTttDaemon(alarmDao, transactionOperations, new File("src/test/resources/etc/ifttt-config.xml")) {

        @Override
        protected void fireIfTttTriggerSet(IfTttConfig ifTttConfig, String categoryFilter, String name, VariableNameExpansion variableNameExpansion) {
            if (!receivedEntries.containsKey(categoryFilter)) {
                receivedEntries.put(categoryFilter, new ArrayList<>());
            }
            receivedEntries.get(categoryFilter).add(new ResultEntry(name, variableNameExpansion));
        }
    };
    ifTttDaemon.start();
    await().atMost(timeout, SECONDS).until(() -> allEntrySizesMatch(receivedEntries, entryCount - 2));
    LOG.debug("#1: {}", receivedEntries);
    addAlarm(12, 4, OnmsSeverity.MAJOR);
    when(alarmDao.findMatching((Criteria) Matchers.anyObject())).thenReturn(alarmMap.values().stream().collect(Collectors.toList()));
    await().atMost(timeout, SECONDS).until(() -> allEntrySizesMatch(receivedEntries, entryCount - 1));
    LOG.debug("#2: {}", receivedEntries);
    ifTttDaemon.stop();
    await().atMost(timeout, SECONDS).until(() -> allEntrySizesMatch(receivedEntries, entryCount));
    LOG.debug("#3: {}", receivedEntries);
    return receivedEntries;
}
Also used : TransactionOperations(org.springframework.transaction.support.TransactionOperations) HashMap(java.util.HashMap) AlarmDao(org.opennms.netmgt.dao.api.AlarmDao) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IfTttConfig(org.opennms.features.ifttt.config.IfTttConfig) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) DefaultVariableNameExpansion(org.opennms.features.ifttt.helper.DefaultVariableNameExpansion) VariableNameExpansion(org.opennms.features.ifttt.helper.VariableNameExpansion)

Aggregations

File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 IfTttConfig (org.opennms.features.ifttt.config.IfTttConfig)1 DefaultVariableNameExpansion (org.opennms.features.ifttt.helper.DefaultVariableNameExpansion)1 VariableNameExpansion (org.opennms.features.ifttt.helper.VariableNameExpansion)1 AlarmDao (org.opennms.netmgt.dao.api.AlarmDao)1 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)1 TransactionOperations (org.springframework.transaction.support.TransactionOperations)1