Remove yang-test
[controller.git] / opendaylight / config / config-persister-impl / src / test / java / org / opendaylight / controller / config / 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.config.persist.impl;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyString;
13 import static org.mockito.Mockito.doReturn;
14
15 import com.google.common.base.Optional;
16 import com.google.common.collect.Lists;
17 import java.util.Collections;
18 import javax.management.Notification;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.controller.config.api.jmx.notifications.CommitJMXNotification;
26 import org.opendaylight.controller.config.api.jmx.notifications.ConfigJMXNotification;
27 import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacade;
28 import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacadeFactory;
29 import org.opendaylight.controller.config.facade.xml.Datastore;
30 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
31 import org.opendaylight.controller.config.persist.api.Persister;
32 import org.opendaylight.controller.config.util.ConfigRegistryClient;
33 import org.opendaylight.controller.config.util.capability.Capability;
34 import org.opendaylight.controller.config.util.xml.XmlUtil;
35 import org.w3c.dom.Document;
36
37 public class ConfigPersisterNotificationListenerTest {
38
39     @Mock
40     private Persister mockPersister;
41     private PersisterAggregator persisterAggregator;
42
43     @Mock
44     private ConfigJMXNotification unknownNetconfNotif;
45     @Mock
46     private CommitJMXNotification commitNetconfNotif;
47     @Mock
48     private Notification unknownNotif;
49     @Mock
50     private ConfigSubsystemFacadeFactory facadeFactory;
51     @Mock
52     private ConfigSubsystemFacade facade;
53     @Mock
54     private ConfigRegistryClient configRegistryClient;
55     @Mock
56     private Capability cap;
57
58     @Before
59     public void setUp() throws Exception {
60         MockitoAnnotations.initMocks(this);
61
62         Mockito.doNothing().when(mockPersister).persistConfig(any(ConfigSnapshotHolder.class));
63         doReturn("persister").when(mockPersister).toString();
64         final PersisterAggregator.PersisterWithConfiguration withCfg = new PersisterAggregator.PersisterWithConfiguration(mockPersister, false);
65         persisterAggregator = new PersisterAggregator(Lists.newArrayList(withCfg));
66
67         doReturn("netconfUnknownNotification").when(unknownNetconfNotif).toString();
68         doReturn("netconfCommitNotification").when(commitNetconfNotif).toString();
69
70         doReturn("config client").when(configRegistryClient).toString();
71
72         doReturn("cap").when(cap).getCapabilityUri();
73         doReturn(facade).when(facadeFactory).createFacade(anyString());
74
75         doReturn(Collections.singleton(cap)).when(facadeFactory).getCurrentCapabilities();
76         doReturn(XmlUtil.readXmlToElement("<snapshot/>")).when(facade)
77                 .getConfiguration(any(Document.class), any(Datastore.class), any(Optional.class));
78     }
79
80     @Test
81     public void testNotificationListenerUnknownNotification() throws Exception {
82         final ConfigPersisterNotificationListener testeListener = new ConfigPersisterNotificationListener(persisterAggregator, facadeFactory);
83         testeListener.handleNotification(unknownNotif, null);
84         Mockito.verifyZeroInteractions(mockPersister);
85     }
86
87     @Test
88     public void testNotificationListenerUnknownNetconfNotification() throws Exception {
89         final ConfigPersisterNotificationListener testeListener = new ConfigPersisterNotificationListener(persisterAggregator, facadeFactory);
90         try {
91             testeListener.handleNotification(unknownNetconfNotif, null);
92             Assert.fail("Unknown netconf notification should fail");
93         } catch (final IllegalStateException e) {
94             Mockito.verifyZeroInteractions(mockPersister);
95         }
96     }
97
98     @Test
99     public void testNotificationListenerCommitNetconfNotification() throws Exception {
100         final ConfigPersisterNotificationListener testeListener = new ConfigPersisterNotificationListener(persisterAggregator, facadeFactory);
101         testeListener.handleNotification(commitNetconfNotif, null);
102         Mockito.verify(mockPersister).persistConfig(any(ConfigSnapshotHolder.class));
103     }
104 }