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 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;
28 import java.io.IOException;
30 import static org.mockito.Matchers.any;
31 import static org.mockito.Matchers.anyString;
32 import static org.mockito.Mockito.doNothing;
33 import static org.mockito.Mockito.doReturn;
34 import static org.mockito.Mockito.doThrow;
35 import static org.mockito.Mockito.mock;
37 public class ConfigPersisterTest {
38 private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterTest.class);
40 private MockedBundleContext ctx;
41 private ConfigPersisterActivator configPersisterActivator;
42 private TestingExceptionHandler handler;
45 private void setUpContextAndStartPersister(String requiredCapability) throws Exception {
46 DummyAdapterWithInitialSnapshot.expectedCapability = requiredCapability;
47 ctx = new MockedBundleContext(1000, 1000);
48 configPersisterActivator = new ConfigPersisterActivator();
49 configPersisterActivator.start(ctx.getBundleContext());
54 handler = new TestingExceptionHandler();
55 Thread.setDefaultUncaughtExceptionHandler(handler);
59 public void tearDown() throws Exception {
60 Thread.setDefaultUncaughtExceptionHandler(null);
61 configPersisterActivator.stop(ctx.getBundleContext());
65 public void testPersisterNotAllCapabilitiesProvided() throws Exception {
66 setUpContextAndStartPersister("required-cap");
68 handler.assertException(IllegalStateException.class, "Max wait for capabilities reached.Not enough capabilities " +
69 "for <data><config-snapshot/></data>. Expected but not found: [required-cap]");
74 public void testPersisterSuccessfulPush() throws Exception {
75 setUpContextAndStartPersister("cap1");
76 NetconfOperationService service = getWorkingService(getOKDocument());
77 doReturn(service).when(ctx.serviceFactory).createService(anyString());
79 assertCannotRegisterAsJMXListener_pushWasSuccessful();
82 // this means pushing of config was successful
83 public void assertCannotRegisterAsJMXListener_pushWasSuccessful() {
84 handler.assertException(IllegalStateException.class, "Cannot register as JMX listener to netconf");
87 public NetconfOperationService getWorkingService(Document document) throws SAXException, IOException, NetconfDocumentedException {
88 NetconfOperationService service = mock(NetconfOperationService.class);
89 Capability capability = mock(Capability.class);
90 doReturn(Sets.newHashSet(capability)).when(service).getCapabilities();
91 doReturn("cap1").when(capability).getCapabilityUri();
94 NetconfOperation mockedOperation = mock(NetconfOperation.class);
95 doReturn(Sets.newHashSet(mockedOperation)).when(service).getNetconfOperations();
96 doReturn(HandlingPriority.getHandlingPriority(1)).when(mockedOperation).canHandle(any(Document.class));
97 doReturn(document).when(mockedOperation).handle(any(Document.class), any(NetconfOperationChainedExecution.class));
98 doNothing().when(service).close();
102 private Document getOKDocument() throws SAXException, IOException {
103 return XmlUtil.readXmlToDocument(
104 "<rpc-reply message-id=\"1\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
112 public void testPersisterConflictingVersionException() throws Exception {
113 setUpContextAndStartPersister("cap1");
115 doReturn(getConflictingService()).when(ctx.serviceFactory).createService(anyString());
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));
134 public void testSuccessConflictingVersionException() throws Exception {
135 setUpContextAndStartPersister("cap1");
136 doReturn(getConflictingService()).when(ctx.serviceFactory).createService(anyString());
139 logger.info("Switching to working service **");
140 doReturn(getWorkingService(getOKDocument())).when(ctx.serviceFactory).createService(anyString());
142 assertCannotRegisterAsJMXListener_pushWasSuccessful();