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