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;
16 import com.google.common.collect.Sets;
17 import java.io.IOException;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.controller.config.api.ConflictingVersionException;
22 import org.opendaylight.controller.netconf.api.Capability;
23 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
24 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
25 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
26 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
27 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
28 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
29 import org.opendaylight.controller.netconf.persist.impl.ConfigPusherImpl;
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;
44 private void setUpContext(String requiredCapability) throws Exception {
45 DummyAdapterWithInitialSnapshot.expectedCapability = requiredCapability;
46 ctx = new MockedBundleContext(1000, 1000);
47 configPersisterActivator = new ConfigPersisterActivator() {
49 protected ConfigPusherImpl newConfigPusher(NetconfOperationServiceFactory service,
50 long maxWaitForCapabilitiesMillis, long conflictingVersionTimeoutMillis) {
51 return new ConfigPusherImpl(service, maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis) {
53 protected void onFailedConfigPush(String message, Exception cause) {
54 handler.uncaughtException(Thread.currentThread(), cause);
55 super.onFailedConfigPush(message, cause);
63 private void setUpContextAndStartPersister(String requiredCapability, final NetconfOperationService conflictingService) throws Exception {
64 setUpContext(requiredCapability);
65 doReturn(conflictingService).when(ctx.serviceFactory).createService(anyString());
66 configPersisterActivator.start(ctx.getBundleContext());
71 handler = new TestingExceptionHandler();
75 public void tearDown() throws Exception {
76 configPersisterActivator.stop(ctx.getBundleContext());
80 public void testPersisterNotAllCapabilitiesProvided() throws Exception {
81 setUpContextAndStartPersister("required-cap", getConflictingService());
83 handler.assertException(IllegalStateException.class, "Required yang models that are missing: [required-cap]");
88 public void testPersisterSuccessfulPush() throws Exception {
89 setUpContextAndStartPersister("cap1", getWorkingService(getOKDocument()));
91 handler.assertNoException();
94 // this means pushing of config was successful
95 public void assertCannotRegisterAsJMXListener_pushWasSuccessful() {
96 handler.assertException(IllegalStateException.class, "Cannot register as JMX listener to netconf");
99 public NetconfOperationService getWorkingService(Document document) throws SAXException, IOException, NetconfDocumentedException {
100 NetconfOperationService service = mock(NetconfOperationService.class);
101 Capability capability = mock(Capability.class);
102 // doReturn(Sets.newHashSet(capability)).when(service).getCapabilities();
103 doReturn("cap1").when(capability).getCapabilityUri();
106 NetconfOperation mockedOperation = mock(NetconfOperation.class);
107 doReturn(Sets.newHashSet(mockedOperation)).when(service).getNetconfOperations();
108 doReturn(HandlingPriority.getHandlingPriority(1)).when(mockedOperation).canHandle(any(Document.class));
109 doReturn(document).when(mockedOperation).handle(any(Document.class), any(NetconfOperationChainedExecution.class));
110 doNothing().when(service).close();
114 private Document getOKDocument() throws SAXException, IOException {
115 return XmlUtil.readXmlToDocument(
116 "<rpc-reply message-id=\"1\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
124 public void testPersisterConflictingVersionException() throws Exception {
125 setUpContextAndStartPersister("cap1", getConflictingService());
128 handler.assertException(IllegalStateException.class, "Max wait for conflicting version stabilization timeout");
131 private NetconfOperationService getConflictingService() throws Exception {
132 NetconfOperationService service = getWorkingService(getOKDocument());
133 ConflictingVersionException cve = new ConflictingVersionException("");
135 NetconfDocumentedException.wrap(cve);
136 throw new AssertionError("Should throw an exception");
137 }catch(NetconfDocumentedException e) {
138 NetconfOperation mockedOperation = service.getNetconfOperations().iterator().next();
139 doThrow(e).when(mockedOperation).handle(any(Document.class), any(NetconfOperationChainedExecution.class));
145 public void testSuccessConflictingVersionException() throws Exception {
146 LOG.info("testSuccessConflictingVersionException starting");
148 setUpContext("cap1");
150 NetconfOperationService conflictingService = getConflictingService();
151 NetconfOperationService workingService = getWorkingService(getOKDocument());
153 doReturn(conflictingService).doReturn(conflictingService).doReturn(conflictingService).
154 doReturn(workingService).when(ctx.serviceFactory).createService(anyString());
156 configPersisterActivator.start(ctx.getBundleContext());
158 handler.assertNoException();