1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * The contents of this file are subject to the terms of either the Universal Permissive License 7 * v 1.0 as shown at http://oss.oracle.com/licenses/upl 8 * 9 * or the following license: 10 * 11 * Redistribution and use in source and binary forms, with or without modification, are permitted 12 * provided that the following conditions are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions 15 * and the following disclaimer. 16 * 17 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of 18 * conditions and the following disclaimer in the documentation and/or other materials provided with 19 * the distribution. 20 * 21 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to 22 * endorse or promote products derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 26 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 31 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 package org.openjdk.jmc.rjmx.test.triggers; 34 35 import static org.junit.Assert.assertTrue; 36 37 import java.io.IOException; 38 import java.util.Collection; 39 40 import javax.security.auth.login.FailedLoginException; 41 42 import org.junit.Before; 43 import org.junit.Test; 44 45 import org.openjdk.jmc.common.unit.IUnit; 46 import org.openjdk.jmc.common.unit.UnitLookup; 47 import org.openjdk.jmc.rjmx.subscription.IMRIMetadata; 48 import org.openjdk.jmc.rjmx.subscription.MRI; 49 import org.openjdk.jmc.rjmx.subscription.MRI.Type; 50 import org.openjdk.jmc.rjmx.test.RjmxTestCase; 51 import org.openjdk.jmc.rjmx.test.triggers.NotificationActionCallback.NotificationActionCallbackReceiver; 52 import org.openjdk.jmc.rjmx.triggers.TriggerEvent; 53 import org.openjdk.jmc.rjmx.triggers.TriggerRule; 54 import org.openjdk.jmc.rjmx.triggers.internal.NotificationRegistry; 55 import org.openjdk.jmc.rjmx.triggers.internal.NotificationTrigger; 56 import org.openjdk.jmc.rjmx.triggers.internal.ValueEvaluatorNumberMin; 57 58 /** 59 * Tests for the notification framework. 60 */ 61 public class NotificationModelTest extends RjmxTestCase 62 implements NotificationActionCallback.NotificationActionCallbackReceiver { 63 private final Object m_monitor = new Object(); 64 private NotificationRegistry m_notificationRegistry; 65 66 /** 67 * Tests registering a notification rule. 68 * 69 * @throws IOException 70 * @throws FailedLoginException 71 */ 72 @Test 73 public void testRegisterRule() throws Exception { 74 TriggerRule rule = createTestNotificationRule(); 75 String serverGuid = m_connectionHandle.getServerDescriptor().getGUID(); 76 m_notificationRegistry.registerRule(rule, serverGuid); 77 Collection<TriggerRule> rulesList = m_notificationRegistry.getRegisteredRules(serverGuid); 78 79 assertTrue("Failed to register anything at all!", rulesList.size() > 0); //$NON-NLS-1$ 80 TriggerRule regRule = rulesList.iterator().next(); 81 assertTrue("Failed rule comparison!", regRule == rule); //$NON-NLS-1$ 82 } 83 84 /** 85 * Tests unregistering a notification rule. 86 * 87 * @throws IOException 88 * @throws FailedLoginException 89 */ 90 @Test 91 public void testUnregisterRule() throws Exception { 92 assertTrue(m_connectionHandle.isConnected()); 93 TriggerRule rule = createTestNotificationRule(); 94 String serverGuid = m_connectionHandle.getServerDescriptor().getGUID(); 95 m_notificationRegistry.registerRule(rule, serverGuid); 96 m_notificationRegistry.unregisterRule(rule, serverGuid); 97 assertTrue("Failed to unregister rule!", m_notificationRegistry.getRegisteredRules(serverGuid).size() == 0); //$NON-NLS-1$ 98 } 99 100 private TriggerRule createTestNotificationRule() throws Exception { 101 assertTrue(m_connectionHandle.isConnected()); 102 NotificationTrigger trigger = new NotificationTrigger(); 103 MRI mri = new MRI(Type.ATTRIBUTE, "java.lang:type=Memory", "HeapMemoryUsage/used"); //$NON-NLS-1$ //$NON-NLS-2$ 104 IMRIMetadata metadata = getMRIMetadataService().getMetadata(mri); 105 IUnit unit = UnitLookup.getUnitOrNull(metadata.getUnitString()); 106 trigger.setAttributeDescriptor(mri); 107 trigger.setValueEvaluator(new ValueEvaluatorNumberMin(unit.quantity(100000))); 108 return new TriggerRule("TestRule", trigger, new NotificationActionCallback(this)); //$NON-NLS-1$ 109 } 110 111 /** 112 * @see NotificationActionCallbackReceiver#callback(TriggerEvent) 113 */ 114 @Override 115 public void onNotificationAction(TriggerEvent e) { 116 synchronized (m_monitor) { 117 m_monitor.notifyAll(); 118 } 119 } 120 121 @Before 122 public void setUp() throws Exception { 123 m_notificationRegistry = new NotificationRegistry(); 124 } 125 }