Fix intermittent ConfigPersisterTest failure
[netconf.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 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.persist.impl.osgi.MockedBundleContext.DummyAdapterWithInitialSnapshot;
29 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.w3c.dom.Document;
33 import org.xml.sax.SAXException;
34
35 public class ConfigPersisterTest {
36     private static final Logger LOG = LoggerFactory.getLogger(ConfigPersisterTest.class);
37
38     private MockedBundleContext ctx;
39     private ConfigPersisterActivator configPersisterActivator;
40     private TestingExceptionHandler handler;
41
42     private void setUpContext(String requiredCapability) throws Exception {
43         DummyAdapterWithInitialSnapshot.expectedCapability = requiredCapability;
44         ctx = new MockedBundleContext(1000, 1000);
45         configPersisterActivator = new ConfigPersisterActivator();
46     }
47
48     private void setUpContextAndStartPersister(String requiredCapability, final NetconfOperationService conflictingService) throws Exception {
49         setUpContext(requiredCapability);
50         doReturn(conflictingService).when(ctx.serviceFactory).createService(anyString());
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", getConflictingService());
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", getWorkingService(getOKDocument()));
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", getConflictingService());
114
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         LOG.info("testSuccessConflictingVersionException starting");
135
136         setUpContext("cap1");
137
138         NetconfOperationService conflictingService = getConflictingService();
139         NetconfOperationService workingService = getWorkingService(getOKDocument());
140
141         doReturn(conflictingService).doReturn(conflictingService).doReturn(conflictingService).
142             doReturn(workingService).when(ctx.serviceFactory).createService(anyString());
143
144         configPersisterActivator.start(ctx.getBundleContext());
145
146         Thread.sleep(1000);
147         assertCannotRegisterAsJMXListener_pushWasSuccessful();
148     }
149
150 }