Merge "Fixed Karaf Distribution Archetype add dependent bundles"
[controller.git] / opendaylight / netconf / config-persister-impl / src / test / java / org / opendaylight / controller / netconf / persist / impl / ConfigPersisterNotificationListenerTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.netconf.persist.impl;
10
11 import java.util.Collections;
12
13 import javax.management.Notification;
14
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Matchers;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
23 import org.opendaylight.controller.config.persist.api.Persister;
24 import org.opendaylight.controller.netconf.api.jmx.CommitJMXNotification;
25 import org.opendaylight.controller.netconf.api.jmx.NetconfJMXNotification;
26 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
27
28 import com.google.common.collect.Lists;
29
30 public class ConfigPersisterNotificationListenerTest {
31
32     @Mock
33     private Persister mockPersister;
34     private PersisterAggregator persisterAggregator;
35
36     @Mock
37     private NetconfJMXNotification unknownNetconfNotif;
38     @Mock
39     private CommitJMXNotification commitNetconfNotif;
40     @Mock
41     private Notification unknownNotif;
42
43     @Before
44     public void setUp() throws Exception {
45         MockitoAnnotations.initMocks(this);
46
47         Mockito.doNothing().when(mockPersister).persistConfig(Matchers.any(ConfigSnapshotHolder.class));
48         Mockito.doReturn("persister").when(mockPersister).toString();
49         final PersisterAggregator.PersisterWithConfiguration withCfg = new PersisterAggregator.PersisterWithConfiguration(mockPersister, false);
50         persisterAggregator = new PersisterAggregator(Lists.newArrayList(withCfg));
51
52         Mockito.doReturn("netconfUnknownNotification").when(unknownNetconfNotif).toString();
53         Mockito.doReturn("netconfCommitNotification").when(commitNetconfNotif).toString();
54
55         Mockito.doReturn(XmlUtil.readXmlToElement("<config-snapshot/>")).when(commitNetconfNotif).getConfigSnapshot();
56         Mockito.doReturn(Collections.emptySet()).when(commitNetconfNotif).getCapabilities();
57
58     }
59
60     @Test
61     public void testNotificationListenerUnknownNotification() throws Exception {
62         final ConfigPersisterNotificationListener testeListener = new ConfigPersisterNotificationListener(persisterAggregator);
63         testeListener.handleNotification(unknownNotif, null);
64         Mockito.verifyZeroInteractions(mockPersister);
65     }
66
67     @Test
68     public void testNotificationListenerUnknownNetconfNotification() throws Exception {
69         final ConfigPersisterNotificationListener testeListener = new ConfigPersisterNotificationListener(persisterAggregator);
70         try {
71             testeListener.handleNotification(unknownNetconfNotif, null);
72             Assert.fail("Unknown netconf notification should fail");
73         } catch (final IllegalStateException e) {
74             Mockito.verifyZeroInteractions(mockPersister);
75         }
76     }
77
78     @Test
79     public void testNotificationListenerCommitNetconfNotification() throws Exception {
80         final ConfigPersisterNotificationListener testeListener = new ConfigPersisterNotificationListener(persisterAggregator);
81         testeListener.handleNotification(commitNetconfNotif, null);
82         Mockito.verify(mockPersister).persistConfig(Matchers.any(ConfigSnapshotHolder.class));
83     }
84 }