2 * Copyright (c) 2013 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
8 package org.opendaylight.controller.netconf.persist.impl.osgi;
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Matchers.anyString;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.doThrow;
15 import static org.mockito.Mockito.mock;
17 import com.google.common.collect.Sets;
18 import java.io.IOException;
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Ignore;
22 import org.junit.Test;
23 import org.opendaylight.controller.config.api.ConflictingVersionException;
24 import org.opendaylight.controller.netconf.api.Capability;
25 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
26 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
27 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
28 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
29 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
30 import org.opendaylight.controller.netconf.persist.impl.osgi.MockedBundleContext.DummyAdapterWithInitialSnapshot;
31 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.w3c.dom.Document;
35 import org.xml.sax.SAXException;
37 public class ConfigPersisterTest {
38 private static final Logger LOG = LoggerFactory.getLogger(ConfigPersisterTest.class);
40 private MockedBundleContext ctx;
41 private ConfigPersisterActivator configPersisterActivator;
42 private TestingExceptionHandler handler;
45 private void setUpContextAndStartPersister(String requiredCapability, final NetconfOperationService conflictingService) throws Exception {
46 DummyAdapterWithInitialSnapshot.expectedCapability = requiredCapability;
47 ctx = new MockedBundleContext(1000, 1000);
48 doReturn(getConflictingService()).when(ctx.serviceFactory).createService(anyString());
49 configPersisterActivator = new ConfigPersisterActivator();
50 configPersisterActivator.start(ctx.getBundleContext());
55 handler = new TestingExceptionHandler();
56 Thread.setDefaultUncaughtExceptionHandler(handler);
60 public void tearDown() throws Exception {
61 Thread.setDefaultUncaughtExceptionHandler(null);
62 configPersisterActivator.stop(ctx.getBundleContext());
66 public void testPersisterNotAllCapabilitiesProvided() throws Exception {
67 setUpContextAndStartPersister("required-cap", getConflictingService());
69 handler.assertException(IllegalStateException.class, "Max wait for capabilities reached.Not enough capabilities " +
70 "for <data><config-snapshot/></data>. Expected but not found: [required-cap]");
75 public void testPersisterSuccessfulPush() throws Exception {
76 setUpContextAndStartPersister("cap1", getConflictingService());
77 NetconfOperationService service = getWorkingService(getOKDocument());
78 doReturn(service).when(ctx.serviceFactory).createService(anyString());
80 assertCannotRegisterAsJMXListener_pushWasSuccessful();
83 // this means pushing of config was successful
84 public void assertCannotRegisterAsJMXListener_pushWasSuccessful() {
85 handler.assertException(IllegalStateException.class, "Cannot register as JMX listener to netconf");
88 public NetconfOperationService getWorkingService(Document document) throws SAXException, IOException, NetconfDocumentedException {
89 NetconfOperationService service = mock(NetconfOperationService.class);
90 Capability capability = mock(Capability.class);
91 // doReturn(Sets.newHashSet(capability)).when(service).getCapabilities();
92 doReturn("cap1").when(capability).getCapabilityUri();
95 NetconfOperation mockedOperation = mock(NetconfOperation.class);
96 doReturn(Sets.newHashSet(mockedOperation)).when(service).getNetconfOperations();
97 doReturn(HandlingPriority.getHandlingPriority(1)).when(mockedOperation).canHandle(any(Document.class));
98 doReturn(document).when(mockedOperation).handle(any(Document.class), any(NetconfOperationChainedExecution.class));
99 doNothing().when(service).close();
103 private Document getOKDocument() throws SAXException, IOException {
104 return XmlUtil.readXmlToDocument(
105 "<rpc-reply message-id=\"1\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
113 public void testPersisterConflictingVersionException() throws Exception {
114 setUpContextAndStartPersister("cap1", getConflictingService());
117 handler.assertException(IllegalStateException.class, "Max wait for conflicting version stabilization timeout");
120 private NetconfOperationService getConflictingService() throws Exception {
121 NetconfOperationService service = getWorkingService(getOKDocument());
122 ConflictingVersionException cve = new ConflictingVersionException("");
124 NetconfDocumentedException.wrap(cve);
125 throw new AssertionError("Should throw an exception");
126 }catch(NetconfDocumentedException e) {
127 NetconfOperation mockedOperation = service.getNetconfOperations().iterator().next();
128 doThrow(e).when(mockedOperation).handle(any(Document.class), any(NetconfOperationChainedExecution.class));
133 @Ignore("this test needs to be redesigned. Remove bundle bundle context and concurrency to make it more stable")
135 public void testSuccessConflictingVersionException() throws Exception {
136 setUpContextAndStartPersister("cap1", getConflictingService());
137 doReturn(getConflictingService()).when(ctx.serviceFactory).createService(anyString());
140 LOG.info("Switching to working service **");
141 doReturn(getWorkingService(getOKDocument())).when(ctx.serviceFactory).createService(anyString());
143 assertCannotRegisterAsJMXListener_pushWasSuccessful();