Merge "Resolve Bug:853 - remove groovy from config code generator."
[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 java.io.IOException;
29
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;
36
37 public class ConfigPersisterTest {
38     private static final Logger logger = LoggerFactory.getLogger(ConfigPersisterTest.class);
39
40     private MockedBundleContext ctx;
41     private ConfigPersisterActivator configPersisterActivator;
42     private TestingExceptionHandler handler;
43
44
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());
50     }
51
52     @Before
53     public void setUp() {
54         handler = new TestingExceptionHandler();
55         Thread.setDefaultUncaughtExceptionHandler(handler);
56     }
57
58     @After
59     public void tearDown() throws Exception {
60         Thread.setDefaultUncaughtExceptionHandler(null);
61         configPersisterActivator.stop(ctx.getBundleContext());
62     }
63
64     @Test
65     public void testPersisterNotAllCapabilitiesProvided() throws Exception {
66         setUpContextAndStartPersister("required-cap");
67         Thread.sleep(2000);
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]");
70
71     }
72
73     @Test
74     public void testPersisterSuccessfulPush() throws Exception {
75         setUpContextAndStartPersister("cap1");
76         NetconfOperationService service = getWorkingService(getOKDocument());
77         doReturn(service).when(ctx.serviceFactory).createService(anyString());
78         Thread.sleep(2000);
79         assertCannotRegisterAsJMXListener_pushWasSuccessful();
80     }
81
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");
85     }
86
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();
92
93
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();
99         return service;
100     }
101
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" +
105                         "<ok/>\n" +
106                         "</rpc-reply>"
107         );
108     }
109
110
111     @Test
112     public void testPersisterConflictingVersionException() throws Exception {
113         setUpContextAndStartPersister("cap1");
114
115         doReturn(getConflictingService()).when(ctx.serviceFactory).createService(anyString());
116         Thread.sleep(2000);
117         handler.assertException(IllegalStateException.class, "Max wait for conflicting version stabilization timeout");
118     }
119
120     private NetconfOperationService getConflictingService() throws Exception {
121         NetconfOperationService service =  getWorkingService(getOKDocument());
122         ConflictingVersionException cve = new ConflictingVersionException("");
123         try {
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));
129             return service;
130         }
131     }
132
133     @Test
134     public void testSuccessConflictingVersionException() throws Exception {
135         setUpContextAndStartPersister("cap1");
136         doReturn(getConflictingService()).when(ctx.serviceFactory).createService(anyString());
137         Thread.sleep(500);
138         // working service:
139         logger.info("Switching to working service **");
140         doReturn(getWorkingService(getOKDocument())).when(ctx.serviceFactory).createService(anyString());
141         Thread.sleep(1000);
142         assertCannotRegisterAsJMXListener_pushWasSuccessful();
143     }
144
145 }