Bug 4560: Improve config system logging for debuggability
[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 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;
36
37 public class ConfigPersisterTest {
38     private static final Logger LOG = LoggerFactory.getLogger(ConfigPersisterTest.class);
39
40     private MockedBundleContext ctx;
41     private ConfigPersisterActivator configPersisterActivator;
42     private TestingExceptionHandler handler;
43
44     private void setUpContext(String requiredCapability) throws Exception {
45         DummyAdapterWithInitialSnapshot.expectedCapability = requiredCapability;
46         ctx = new MockedBundleContext(1000, 1000);
47         configPersisterActivator = new ConfigPersisterActivator() {
48             @Override
49             protected ConfigPusherImpl newConfigPusher(NetconfOperationServiceFactory service,
50                     long maxWaitForCapabilitiesMillis, long conflictingVersionTimeoutMillis) {
51                 return new ConfigPusherImpl(service, maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis) {
52                     @Override
53                     protected void onFailedConfigPush(String message, Exception cause) {
54                         handler.uncaughtException(Thread.currentThread(), cause);
55                         super.onFailedConfigPush(message, cause);
56                     }
57
58                 };
59             }
60         };
61     }
62
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());
67     }
68
69     @Before
70     public void setUp() {
71         handler = new TestingExceptionHandler();
72     }
73
74     @After
75     public void tearDown() throws Exception {
76         configPersisterActivator.stop(ctx.getBundleContext());
77     }
78
79     @Test
80     public void testPersisterNotAllCapabilitiesProvided() throws Exception {
81         setUpContextAndStartPersister("required-cap", getConflictingService());
82         Thread.sleep(2000);
83         handler.assertException(IllegalStateException.class, "Required yang models that are missing: [required-cap]");
84
85     }
86
87     @Test
88     public void testPersisterSuccessfulPush() throws Exception {
89         setUpContextAndStartPersister("cap1", getWorkingService(getOKDocument()));
90         Thread.sleep(2000);
91         handler.assertNoException();
92     }
93
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");
97     }
98
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();
104
105
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();
111         return service;
112     }
113
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" +
117                         "<ok/>\n" +
118                         "</rpc-reply>"
119         );
120     }
121
122
123     @Test
124     public void testPersisterConflictingVersionException() throws Exception {
125         setUpContextAndStartPersister("cap1", getConflictingService());
126
127         Thread.sleep(2000);
128         handler.assertException(IllegalStateException.class, "Max wait for conflicting version stabilization timeout");
129     }
130
131     private NetconfOperationService getConflictingService() throws Exception {
132         NetconfOperationService service =  getWorkingService(getOKDocument());
133         ConflictingVersionException cve = new ConflictingVersionException("");
134         try {
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));
140             return service;
141         }
142     }
143
144     @Test
145     public void testSuccessConflictingVersionException() throws Exception {
146         LOG.info("testSuccessConflictingVersionException starting");
147
148         setUpContext("cap1");
149
150         NetconfOperationService conflictingService = getConflictingService();
151         NetconfOperationService workingService = getWorkingService(getOKDocument());
152
153         doReturn(conflictingService).doReturn(conflictingService).doReturn(conflictingService).
154             doReturn(workingService).when(ctx.serviceFactory).createService(anyString());
155
156         configPersisterActivator.start(ctx.getBundleContext());
157         Thread.sleep(2000);
158         handler.assertNoException();
159     }
160
161 }