Merge "Add filtering capability to config.ini in order to reference logging bridge...
[controller.git] / opendaylight / netconf / config-persister-impl / src / test / java / org / opendaylight / controller / netconf / persist / impl / osgi / ConfigPersisterTest.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.controller.netconf.persist.impl.osgi;
9
10 import com.google.common.collect.Sets;
11 import org.junit.After;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.opendaylight.controller.config.api.ConflictingVersionException;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.mapping.api.Capability;
17 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
18 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
19 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
20 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
21 import org.opendaylight.controller.netconf.persist.impl.osgi.MockedBundleContext.DummyAdapterWithInitialSnapshot;
22 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.w3c.dom.Document;
26 import org.xml.sax.SAXException;
27
28 import javax.management.MBeanServer;
29 import java.io.IOException;
30 import java.lang.management.ManagementFactory;
31
32 import static org.mockito.Matchers.any;
33 import static org.mockito.Matchers.anyString;
34 import static org.mockito.Mockito.doNothing;
35 import static org.mockito.Mockito.doReturn;
36 import static org.mockito.Mockito.doThrow;
37 import static org.mockito.Mockito.mock;
38
39 public class ConfigPersisterTest {
40     private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterTest.class);
41
42     private MockedBundleContext ctx;
43     private ConfigPersisterActivator configPersisterActivator;
44     private static final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
45     private TestingExceptionHandler handler;
46
47
48     private void setUpContextAndStartPersister(String requiredCapability) throws Exception {
49         DummyAdapterWithInitialSnapshot.expectedCapability = requiredCapability;
50         ctx = new MockedBundleContext(1000, 1000);
51         configPersisterActivator = new ConfigPersisterActivator();
52         configPersisterActivator.start(ctx.getBundleContext());
53     }
54
55     @Before
56     public void setUp() {
57         handler = new TestingExceptionHandler();
58         Thread.setDefaultUncaughtExceptionHandler(handler);
59     }
60
61     @After
62     public void tearDown() throws Exception {
63         Thread.setDefaultUncaughtExceptionHandler(null);
64         configPersisterActivator.stop(ctx.getBundleContext());
65     }
66
67     @Test
68     public void testPersisterNotAllCapabilitiesProvided() throws Exception {
69         setUpContextAndStartPersister("required-cap");
70         Thread.sleep(2000);
71         handler.assertException(IllegalStateException.class, "Max wait for capabilities reached.Not enough capabilities " +
72                 "for <data><config-snapshot/></data>. Expected but not found: [required-cap]");
73
74     }
75
76     @Test
77     public void testPersisterSuccessfulPush() throws Exception {
78         setUpContextAndStartPersister("cap1");
79         NetconfOperationService service = getWorkingService(getOKDocument());
80         doReturn(service).when(ctx.serviceFactory).createService(anyString());
81         Thread.sleep(2000);
82         assertCannotRegisterAsJMXListener_pushWasSuccessful();
83     }
84
85     // this means pushing of config was successful
86     public void assertCannotRegisterAsJMXListener_pushWasSuccessful() {
87         handler.assertException(RuntimeException.class, "Cannot register as JMX listener to netconf");
88     }
89
90     public NetconfOperationService getWorkingService(Document document) throws SAXException, IOException, NetconfDocumentedException {
91         NetconfOperationService service = mock(NetconfOperationService.class);
92         Capability capability = mock(Capability.class);
93         doReturn(Sets.newHashSet(capability)).when(service).getCapabilities();
94         doReturn("cap1").when(capability).getCapabilityUri();
95
96
97         NetconfOperation mockedOperation = mock(NetconfOperation.class);
98         doReturn(Sets.newHashSet(mockedOperation)).when(service).getNetconfOperations();
99         doReturn(HandlingPriority.getHandlingPriority(1)).when(mockedOperation).canHandle(any(Document.class));
100         doReturn(document).when(mockedOperation).handle(any(Document.class), any(NetconfOperationChainedExecution.class));
101         doNothing().when(service).close();
102         return service;
103     }
104
105     private Document getOKDocument() throws SAXException, IOException {
106         return XmlUtil.readXmlToDocument(
107                 "<rpc-reply message-id=\"1\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
108                         "<ok/>\n" +
109                         "</rpc-reply>"
110         );
111     }
112
113
114     @Test
115     public void testPersisterConflictingVersionException() throws Exception {
116         setUpContextAndStartPersister("cap1");
117
118         doReturn(getConflictingService()).when(ctx.serviceFactory).createService(anyString());
119         Thread.sleep(2000);
120         handler.assertException(IllegalStateException.class, "Max wait for conflicting version stabilization timeout");
121     }
122
123     private NetconfOperationService getConflictingService() throws Exception {
124         NetconfOperationService service =  getWorkingService(getOKDocument());
125         ConflictingVersionException cve = new ConflictingVersionException("");
126         try {
127             NetconfDocumentedException.wrap(cve);
128             throw new AssertionError("Should throw an exception");
129         }catch(NetconfDocumentedException e) {
130             NetconfOperation mockedOperation = service.getNetconfOperations().iterator().next();
131             doThrow(e).when(mockedOperation).handle(any(Document.class), any(NetconfOperationChainedExecution.class));
132             return service;
133         }
134     }
135
136     @Test
137     public void testSuccessConflictingVersionException() throws Exception {
138         setUpContextAndStartPersister("cap1");
139         doReturn(getConflictingService()).when(ctx.serviceFactory).createService(anyString());
140         Thread.sleep(500);
141         // working service:
142         logger.info("Switching to working service **");
143         doReturn(getWorkingService(getOKDocument())).when(ctx.serviceFactory).createService(anyString());
144         Thread.sleep(1000);
145         assertCannotRegisterAsJMXListener_pushWasSuccessful();
146     }
147
148 }