Merge "Fix raw ServiceReferences and ServiceRegistrations"
[controller.git] / opendaylight / netconf / config-persister-impl / src / test / java / org / opendaylight / controller / netconf / persist / impl / ConfigPersisterNotificationHandlerTest.java
index 6c45c9c0119073fd4ff8d64869ed6a7068181090..e96b547169c6dda0abbe5a4c1a40ff57e9a2ac70 100644 (file)
@@ -1,34 +1,65 @@
 /*
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
+
 package org.opendaylight.controller.netconf.persist.impl;
 
-import org.junit.Test;
-import org.opendaylight.controller.config.api.ConflictingVersionException;
-import org.opendaylight.controller.netconf.api.NetconfMessage;
-import org.opendaylight.controller.netconf.util.xml.XmlElement;
-import org.opendaylight.controller.netconf.util.xml.XmlUtil;
-import org.w3c.dom.Document;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyObject;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.fail;
-import static org.junit.matchers.JUnitMatchers.containsString;
+import javax.management.MBeanServerConnection;
+import javax.management.NotificationFilter;
+import javax.management.NotificationListener;
+import javax.management.ObjectName;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.opendaylight.controller.config.persist.api.Persister;
 
 public class ConfigPersisterNotificationHandlerTest {
 
+    @Mock
+    private MBeanServerConnection mBeanServer;
+    @Mock
+    private Persister notificationListener;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+        doNothing().when(mBeanServer).addNotificationListener(any(ObjectName.class), any(NotificationListener.class),
+                any(NotificationFilter.class), anyObject());
+    }
+
     @Test
-    public void testConflictingVersionDetection() throws Exception {
-        Document document = XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/conflictingVersionResponse.xml"));
-        try{
-            ConfigPersisterNotificationHandler.checkIsOk(XmlElement.fromDomDocument(document).getOnlyChildElement(), new NetconfMessage(document));
-            fail();
-        }catch(ConflictingVersionException e){
-            assertThat(e.getMessage(), containsString("Optimistic lock failed. Expected parent version 21, was 18"));
-        }
+    public void testNotificationHandler() throws Exception {
+        doReturn(true).when(mBeanServer).isRegistered(any(ObjectName.class));
+        doThrow(Exception.class).when(mBeanServer).removeNotificationListener(any(ObjectName.class), any(NotificationListener.class));
+
+        final ConfigPersisterNotificationHandler testedHandler = new ConfigPersisterNotificationHandler(mBeanServer, notificationListener);
+        verify(mBeanServer).addNotificationListener(any(ObjectName.class), any(NotificationListener.class),
+                any(NotificationFilter.class), anyObject());
+
+        testedHandler.close();
+        verify(mBeanServer).removeNotificationListener(any(ObjectName.class), any(NotificationListener.class));
     }
 
+    @Test
+    public void testNotificationHandlerCloseNotRegistered() throws Exception {
+        doReturn(false).when(mBeanServer).isRegistered(any(ObjectName.class));
+
+        final ConfigPersisterNotificationHandler testedHandler = new ConfigPersisterNotificationHandler(mBeanServer, notificationListener);
+
+        testedHandler.close();
+        verify(mBeanServer, times(0)).removeNotificationListener(any(ObjectName.class), any(NotificationListener.class));
+    }
 }