Search in sources :

Example 6 with MockEventType

use of com.serotonin.m2m2.rt.event.type.MockEventType in project ma-core-public by infiniteautomation.

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());
        }
    }
}
Also used : User(com.serotonin.m2m2.vo.User) ArrayList(java.util.ArrayList) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) MockEventType(com.serotonin.m2m2.rt.event.type.MockEventType) Test(org.junit.Test)

Example 7 with MockEventType

use of com.serotonin.m2m2.rt.event.type.MockEventType in project ma-core-public by infiniteautomation.

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();
}
Also used : User(com.serotonin.m2m2.vo.User) RunAs(com.infiniteautomation.mango.spring.components.RunAs) MockEventType(com.serotonin.m2m2.rt.event.type.MockEventType) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SynchronousQueue(java.util.concurrent.SynchronousQueue) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) List(java.util.List) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) Test(org.junit.Test)

Example 8 with MockEventType

use of com.serotonin.m2m2.rt.event.type.MockEventType in project ma-core-public by infiniteautomation.

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;
}
Also used : EventTypeMatcher(com.serotonin.m2m2.rt.event.type.EventTypeMatcher) EmailEventHandlerVO(com.serotonin.m2m2.vo.event.EmailEventHandlerVO) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions) MockEventType(com.serotonin.m2m2.rt.event.type.MockEventType)

Example 9 with MockEventType

use of com.serotonin.m2m2.rt.event.type.MockEventType in project ma-core-public by infiniteautomation.

the class UserEventMulticasterTest method testMulticastEventsForUsersWithPermissions.

@Test
public void testMulticastEventsForUsersWithPermissions() {
    PermissionService service = Common.getBean(PermissionService.class);
    int dataPointId = 1;
    int eventCount = 100;
    int userCount = 5 * 6;
    // Add them out of order so the tree is jumbled with permissions hither and yon
    List<User> users = new ArrayList<>();
    int added = 0;
    for (int i = 0; i < (userCount / 6); i++) {
        users.add(createUser("User" + added, "user" + added, "password", "user" + added + "@yourMangoDomain.com", PermissionHolder.SUPERADMIN_ROLE));
        added++;
    }
    for (int i = 0; i < (userCount / 6); i++) {
        users.add(createUser("User" + added, "user" + added, "password", "user" + added + "@yourMangoDomain.com", mockRole.getRole()));
        added++;
    }
    for (int i = 0; i < (userCount / 6); i++) {
        users.add(createUser("User" + added, "user" + added, "password", "user" + added + "@yourMangoDomain.com"));
        added++;
    }
    for (int i = 0; i < (userCount / 6); i++) {
        users.add(createUser("User" + added, "user" + added, "password", "user" + added + "@yourMangoDomain.com", PermissionHolder.SUPERADMIN_ROLE));
        added++;
    }
    for (int i = 0; i < (userCount / 6); i++) {
        users.add(createUser("User" + added, "user" + added, "password", "user" + added + "@yourMangoDomain.com", mockRole.getRole()));
        added++;
    }
    for (int i = 0; i < (userCount / 6); i++) {
        users.add(createUser("User" + added, "user" + added, "password", "user" + added + "@yourMangoDomain.com"));
        added++;
    }
    List<Integer> idsToNotify = new ArrayList<>();
    List<MockUserEventListener> listeners = new ArrayList<>();
    UserEventListener multicaster = null;
    MockEventType mockEventType = new MockEventType(DuplicateHandling.ALLOW, null, 0, dataPointId, this.mockRole.getRole());
    for (User u : users) {
        MockUserEventListener l = new MockUserEventListener(u);
        if (// This work is normally done by the event manager handling the raiseEvent calls
        mockEventType.hasPermission(u, service))
            // through an EventNotifyWorkItem
            idsToNotify.add(u.getId());
        listeners.add(l);
        multicaster = UserEventMulticaster.add(multicaster, l);
    }
    List<EventInstance> events = new ArrayList<>();
    long time = 0;
    for (int i = 0; i < eventCount; i++) {
        EventInstance event = createMockEventInstance(i, dataPointId, time);
        events.add(event);
        event.setIdsToNotify(idsToNotify);
        multicaster.raised(event);
        time += 1;
    }
    // Ack
    for (EventInstance e : events) multicaster.acknowledged(e);
    // Rtn
    for (EventInstance e : events) multicaster.returnToNormal(e);
    // Confirm those with correct permissions permissions saw all raised
    for (MockUserEventListener l : listeners) {
        if (!(service.hasPermission(l.getUser(), MangoPermission.requireAnyRole(mockRole.getRole())) || service.hasAdminRole(l.getUser()))) {
            assertEquals(0, l.getRaised().size());
        } else {
            assertEquals(eventCount, l.getRaised().size());
        }
    }
    // Confirm those with permissions saw all acked
    for (MockUserEventListener l : listeners) {
        if (!(service.hasPermission(l.getUser(), MangoPermission.requireAnyRole(mockRole.getRole())) || service.hasAdminRole(l.getUser()))) {
            assertEquals(0, l.getAcknowledged().size());
        } else {
            assertEquals(eventCount, l.getAcknowledged().size());
        }
    }
    // Confirm those with permissions saw all rtned
    for (MockUserEventListener l : listeners) {
        if (!(service.hasPermission(l.getUser(), MangoPermission.requireAnyRole(mockRole.getRole())) || service.hasAdminRole(l.getUser()))) {
            assertEquals(0, l.getReturned().size());
        } else {
            assertEquals(eventCount, l.getReturned().size());
        }
    }
}
Also used : User(com.serotonin.m2m2.vo.User) ArrayList(java.util.ArrayList) MockEventType(com.serotonin.m2m2.rt.event.type.MockEventType) PermissionService(com.infiniteautomation.mango.spring.service.PermissionService) Test(org.junit.Test)

Example 10 with MockEventType

use of com.serotonin.m2m2.rt.event.type.MockEventType in project ma-core-public by infiniteautomation.

the class EmailEventHandlerServiceTest method newVO.

@Override
EmailEventHandlerVO newVO(User user) {
    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;
}
Also used : EventTypeMatcher(com.serotonin.m2m2.rt.event.type.EventTypeMatcher) EmailEventHandlerVO(com.serotonin.m2m2.vo.event.EmailEventHandlerVO) ScriptPermissions(com.infiniteautomation.mango.util.script.ScriptPermissions) MockEventType(com.serotonin.m2m2.rt.event.type.MockEventType)

Aggregations

MockEventType (com.serotonin.m2m2.rt.event.type.MockEventType)18 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)10 Test (org.junit.Test)10 User (com.serotonin.m2m2.vo.User)8 ArrayList (java.util.ArrayList)8 EmailEventHandlerVO (com.serotonin.m2m2.vo.event.EmailEventHandlerVO)6 ScriptPermissions (com.infiniteautomation.mango.util.script.ScriptPermissions)4 EventTypeMatcher (com.serotonin.m2m2.rt.event.type.EventTypeMatcher)4 EventInstanceVO (com.serotonin.m2m2.vo.event.EventInstanceVO)4 RunAs (com.infiniteautomation.mango.spring.components.RunAs)2 PermissionService (com.infiniteautomation.mango.spring.service.PermissionService)2 List (java.util.List)2 ExecutorService (java.util.concurrent.ExecutorService)2 SynchronousQueue (java.util.concurrent.SynchronousQueue)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2