use of com.serotonin.m2m2.rt.event.type.MockEventType in project ma-core-public by MangoAutomation.
the class UserEventsTest method testDeactivateEvents.
@Test
public void testDeactivateEvents() throws InterruptedException {
// Create some users
List<User> users = createUsers(userCount, PermissionHolder.SUPERADMIN_ROLE);
List<MockUserEventListener> listeners = new ArrayList<>();
for (User u : users) {
MockUserEventListener l = new MockUserEventListener(u, MockEventType.class);
listeners.add(l);
Common.eventManager.addUserEventListener(l);
}
// Raise some events
List<MockEventTypeTime> raised = new ArrayList<>();
for (int i = 0; i < eventCount; i++) {
MockEventType event = new MockEventType(DuplicateHandling.ALLOW, null, i, dataPointId, null);
raised.add(new MockEventTypeTime(event, this.timer.currentTimeMillis()));
Common.eventManager.raiseEvent(event, this.timer.currentTimeMillis(), true, AlarmLevels.URGENT, new TranslatableMessage("common.default", "Mock Event"), null);
timer.fastForwardTo(timer.currentTimeMillis() + 1);
}
assertEvents(listeners, EventAction.RAISED, raised);
// Deactivate
long deactivatedTime = Common.timer.currentTimeMillis();
// Simulate the events here so we can verify them, they should be at time now
Common.eventManager.cancelEventsForDataPoint(dataPointId);
timer.fastForwardTo(timer.currentTimeMillis() + 1000);
// Ensure all events are deactivated at the right time
for (MockUserEventListener l : listeners) {
assertEquals(raised.size(), l.getDeactivated().size());
for (EventInstance e : l.getDeactivated()) {
assertEquals(deactivatedTime, (long) e.getRtnTimestamp());
}
}
}
use of com.serotonin.m2m2.rt.event.type.MockEventType in project ma-core-public by MangoAutomation.
the class UserEventsTest method testListenerAddRemoveSyncrhonization.
@Test
public void testListenerAddRemoveSyncrhonization() throws InterruptedException {
List<User> users = createUsers(1, PermissionHolder.SUPERADMIN_ROLE);
ExecutorService executor = Executors.newFixedThreadPool(3);
SynchronousQueue<MockUserEventListener> queue = new SynchronousQueue<>();
MockUserEventListener l = new MockUserEventListener(users.get(0));
AtomicBoolean removerRunning = new AtomicBoolean(true);
AtomicBoolean generatorRunning = new AtomicBoolean(true);
RunAs runAs = Common.getBean(RunAs.class);
Runnable adder = () -> {
while (removerRunning.get()) {
try {
MockUserEventListener adding = queue.take();
Common.eventManager.addUserEventListener(adding);
} catch (Exception e) {
e.printStackTrace();
}
}
};
Runnable remover = () -> {
while (generatorRunning.get()) {
try {
queue.put(l);
Common.eventManager.removeUserEventListener(l);
} catch (Exception e) {
e.printStackTrace();
}
}
removerRunning.set(false);
};
// Event Generator Thread
Runnable generator = () -> {
runAs.runAs(PermissionHolder.SYSTEM_SUPERADMIN, () -> {
// Raise some events
List<MockEventTypeTime> raised = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
MockEventType event = new MockEventType(DuplicateHandling.ALLOW, null, i, dataPointId, null);
raised.add(new MockEventTypeTime(event, timer.currentTimeMillis()));
Common.eventManager.raiseEvent(event, timer.currentTimeMillis(), true, AlarmLevels.URGENT, new TranslatableMessage("common.default", "Mock Event"), null);
timer.fastForwardTo(timer.currentTimeMillis() + 1);
}
generatorRunning.set(false);
});
};
executor.execute(adder);
executor.execute(remover);
executor.execute(generator);
int maxWaitSeconds = 30;
for (int i = maxWaitSeconds; i > 0; i--) {
if (!generatorRunning.get()) {
break;
}
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
executor.shutdown();
executor.awaitTermination(10000, TimeUnit.MILLISECONDS);
executor.shutdownNow();
}
use of com.serotonin.m2m2.rt.event.type.MockEventType in project ma-core-public by MangoAutomation.
the class EmailEventHandlerDaoTest method testHandlerMappings.
@Test
public void testHandlerMappings() {
EmailEventHandlerVO handler = newVO();
dao.insert(handler);
EmailEventHandlerVO fromDB = (EmailEventHandlerVO) dao.get(handler.getId());
assertEquals(1, fromDB.getEventTypes().size());
// Update
dao.saveEventHandlerMapping(fromDB.getId(), new MockEventType(readRole));
fromDB = (EmailEventHandlerVO) dao.get(handler.getId());
assertEquals(1, fromDB.getEventTypes().size());
// Delete by type and handlerId
dao.deleteEventHandlerMapping(fromDB.getId(), new MockEventType(readRole));
fromDB = (EmailEventHandlerVO) dao.get(handler.getId());
assertEquals(0, fromDB.getEventTypes().size());
// Insert
dao.saveEventHandlerMapping(fromDB.getId(), new MockEventType(readRole));
fromDB = (EmailEventHandlerVO) dao.get(handler.getId());
assertEquals(1, fromDB.getEventTypes().size());
// Delete by type
dao.deleteEventHandlerMappings(new MockEventType(readRole));
fromDB = (EmailEventHandlerVO) dao.get(handler.getId());
assertEquals(0, fromDB.getEventTypes().size());
}
use of com.serotonin.m2m2.rt.event.type.MockEventType in project ma-core-public by MangoAutomation.
the class EmailEventHandlerDaoTest method newVO.
@Override
EmailEventHandlerVO newVO() {
EmailEventHandlerVO vo = (EmailEventHandlerVO) ModuleRegistry.getEventHandlerDefinition(EmailEventHandlerDefinition.TYPE_NAME).baseCreateEventHandlerVO();
vo.setXid(UUID.randomUUID().toString());
vo.setName(UUID.randomUUID().toString());
ScriptPermissions permissions = new ScriptPermissions(Collections.singleton(readRole));
vo.setScriptRoles(permissions);
List<EventTypeMatcher> eventTypes = Collections.singletonList(new EventTypeMatcher(new MockEventType(readRole)));
vo.setEventTypes(eventTypes);
return vo;
}
use of com.serotonin.m2m2.rt.event.type.MockEventType in project ma-core-public by MangoAutomation.
the class EventInstanceDaoTest method newVO.
@Override
EventInstanceVO newVO() {
EventInstanceVO vo = new EventInstanceVO();
vo.setAlarmLevel(AlarmLevels.URGENT);
vo.setEventType(new MockEventType(readRole));
vo.setActiveTimestamp(System.currentTimeMillis());
vo.setRtnApplicable(false);
vo.setMessage(new TranslatableMessage("common.noMessage"));
return vo;
}
Aggregations