2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.config.persist.impl;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyString;
13 import static org.mockito.Mockito.doReturn;
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;
37 public class ConfigPersisterNotificationListenerTest {
40 private Persister mockPersister;
41 private PersisterAggregator persisterAggregator;
44 private ConfigJMXNotification unknownNetconfNotif;
46 private CommitJMXNotification commitNetconfNotif;
48 private Notification unknownNotif;
50 private ConfigSubsystemFacadeFactory facadeFactory;
52 private ConfigSubsystemFacade facade;
54 private ConfigRegistryClient configRegistryClient;
56 private Capability cap;
59 public void setUp() throws Exception {
60 MockitoAnnotations.initMocks(this);
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));
67 doReturn("netconfUnknownNotification").when(unknownNetconfNotif).toString();
68 doReturn("netconfCommitNotification").when(commitNetconfNotif).toString();
70 doReturn("config client").when(configRegistryClient).toString();
72 doReturn("cap").when(cap).getCapabilityUri();
73 doReturn(facade).when(facadeFactory).createFacade(anyString());
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));
81 public void testNotificationListenerUnknownNotification() throws Exception {
82 final ConfigPersisterNotificationListener testeListener = new ConfigPersisterNotificationListener(persisterAggregator, facadeFactory);
83 testeListener.handleNotification(unknownNotif, null);
84 Mockito.verifyZeroInteractions(mockPersister);
88 public void testNotificationListenerUnknownNetconfNotification() throws Exception {
89 final ConfigPersisterNotificationListener testeListener = new ConfigPersisterNotificationListener(persisterAggregator, facadeFactory);
91 testeListener.handleNotification(unknownNetconfNotif, null);
92 Assert.fail("Unknown netconf notification should fail");
93 } catch (final IllegalStateException e) {
94 Mockito.verifyZeroInteractions(mockPersister);
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));