Resolve Bug:593. Persister should communicate via OSGi SR instead of TCP.
[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.mock;
37
38 public class ConfigPersisterTest {
39     private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterTest.class);
40
41     private MockedBundleContext ctx;
42     private ConfigPersisterActivator configPersisterActivator;
43     private static final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
44     private TestingExceptionHandler handler;
45
46
47     private void setUpContextAndStartPersister(String requiredCapability) throws Exception {
48         DummyAdapterWithInitialSnapshot.expectedCapability = requiredCapability;
49         ctx = new MockedBundleContext(1000, 1000);
50         configPersisterActivator = new ConfigPersisterActivator();
51         configPersisterActivator.start(ctx.getBundleContext());
52     }
53
54     @Before
55     public void setUp() {
56         handler = new TestingExceptionHandler();
57         Thread.setDefaultUncaughtExceptionHandler(handler);
58     }
59
60     @After
61     public void tearDown() throws Exception {
62         Thread.setDefaultUncaughtExceptionHandler(null);
63         configPersisterActivator.stop(ctx.getBundleContext());
64     }
65
66     @Test
67     public void testPersisterNotAllCapabilitiesProvided() throws Exception {
68         setUpContextAndStartPersister("required-cap");
69         Thread.sleep(2000);
70         handler.assertException(IllegalStateException.class, "Max wait for capabilities reached.Not enough capabilities " +
71                 "for <data><config-snapshot/></data>. Expected but not found: [required-cap]");
72
73     }
74
75     @Test
76     public void testPersisterSuccessfulPush() throws Exception {
77         setUpContextAndStartPersister("cap1");
78         NetconfOperationService service = getWorkingService(getOKDocument());
79         doReturn(service).when(ctx.serviceFactory).createService(anyString());
80         Thread.sleep(2000);
81         assertCannotRegisterAsJMXListener_pushWasSuccessful();
82     }
83
84     // this means pushing of config was successful
85     public void assertCannotRegisterAsJMXListener_pushWasSuccessful() {
86         handler.assertException(RuntimeException.class, "Cannot register as JMX listener to netconf");
87     }
88
89     public NetconfOperationService getWorkingService(Document document) throws SAXException, IOException, NetconfDocumentedException {
90         NetconfOperationService service = mock(NetconfOperationService.class);
91         Capability capability = mock(Capability.class);
92         doReturn(Sets.newHashSet(capability)).when(service).getCapabilities();
93         doReturn("cap1").when(capability).getCapabilityUri();
94
95
96         NetconfOperation mockedOperation = mock(NetconfOperation.class);
97         doReturn(Sets.newHashSet(mockedOperation)).when(service).getNetconfOperations();
98         doReturn(HandlingPriority.getHandlingPriority(1)).when(mockedOperation).canHandle(any(Document.class));
99         doReturn(document).when(mockedOperation).handle(any(Document.class), any(NetconfOperationChainedExecution.class));
100         doNothing().when(service).close();
101         return service;
102     }
103
104     private Document getOKDocument() throws SAXException, IOException {
105         return XmlUtil.readXmlToDocument(
106                 "<rpc-reply message-id=\"1\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
107                         "<ok/>\n" +
108                         "</rpc-reply>"
109         );
110     }
111
112
113     @Test
114     public void testPersisterConflictingVersionException() throws Exception {
115         setUpContextAndStartPersister("cap1");
116         NetconfOperationService service = getWorkingService(getConflictVersionDocument());
117         doReturn(service).when(ctx.serviceFactory).createService(anyString());
118         Thread.sleep(2000);
119         handler.assertException(IllegalStateException.class, "Max wait for conflicting version stabilization timeout");
120     }
121
122     private Document getConflictVersionDocument() throws SAXException, IOException {
123         return XmlUtil.readXmlToDocument(
124                 "<rpc-reply message-id=\"1\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
125                         "<rpc-error><error-info><error>" +
126                         ConflictingVersionException.class.getCanonicalName() +
127                         "</error></error-info></rpc-error>\n" +
128                         "</rpc-reply>"
129         );
130     }
131
132     @Test
133     public void testSuccessConflictingVersionException() throws Exception {
134         setUpContextAndStartPersister("cap1");
135         doReturn(getWorkingService(getConflictVersionDocument())).when(ctx.serviceFactory).createService(anyString());
136         Thread.sleep(500);
137         // working service:
138         logger.info("Switching to working service **");
139         doReturn(getWorkingService(getOKDocument())).when(ctx.serviceFactory).createService(anyString());
140         Thread.sleep(1000);
141         assertCannotRegisterAsJMXListener_pushWasSuccessful();
142     }
143
144 }