Merge "Resolve Bug:707 - ConfigPusher should wait for netconf-impl to register JMX...
[controller.git] / opendaylight / netconf / config-persister-impl / src / test / java / org / opendaylight / controller / netconf / persist / impl / osgi / MockedBundleContext.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.Lists;
11 import com.google.common.collect.Sets;
12 import org.mockito.Mock;
13 import org.mockito.MockitoAnnotations;
14 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
15 import org.opendaylight.controller.config.persist.api.Persister;
16 import org.opendaylight.controller.config.persist.api.PropertiesProvider;
17 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
18 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
19 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
20 import org.opendaylight.controller.netconf.persist.impl.DummyAdapter;
21 import org.osgi.framework.Bundle;
22 import org.osgi.framework.BundleContext;
23 import org.osgi.framework.Filter;
24 import org.osgi.framework.ServiceListener;
25 import org.osgi.framework.ServiceReference;
26
27 import java.io.IOException;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.SortedSet;
31 import java.util.TreeSet;
32
33 import static org.mockito.Matchers.any;
34 import static org.mockito.Matchers.anyString;
35 import static org.mockito.Matchers.eq;
36 import static org.mockito.Mockito.doNothing;
37 import static org.mockito.Mockito.doReturn;
38
39 final class MockedBundleContext {
40     @Mock
41     private BundleContext context;
42     @Mock
43     private Filter outerFilter, innerFilter;
44     @Mock
45     private ServiceReference<?> serviceReference;
46     @Mock
47     private Bundle bundle;
48     @Mock
49     NetconfOperationServiceFactory serviceFactory;
50     @Mock
51     private NetconfOperationService service;
52
53     MockedBundleContext(long maxWaitForCapabilitiesMillis, long conflictingVersionTimeoutMillis) throws Exception {
54         MockitoAnnotations.initMocks(this);
55         doReturn(null).when(context).getProperty(anyString());
56         initContext(maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis);
57
58         String outerFilterString = "(objectClass=org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider)";
59         doReturn(outerFilter).when(context).createFilter(outerFilterString);
60         doNothing().when(context).addServiceListener(any(ServiceListener.class), eq(outerFilterString));
61         ServiceReference<?>[] toBeReturned = {serviceReference};
62         doReturn(toBeReturned).when(context).getServiceReferences(NetconfOperationProvider.class.getName(), null);
63
64         String innerFilterString = "innerfilter";
65         doReturn(innerFilterString).when(outerFilter).toString();
66
67         doReturn(innerFilter).when(context).createFilter(ConfigPersisterActivator.getFilterString());
68         doReturn(innerFilterString).when(innerFilter).toString();
69         doNothing().when(context).addServiceListener(any(ServiceListener.class), eq(innerFilterString));
70
71         doReturn(toBeReturned).when(context).getServiceReferences((String) null, innerFilterString);
72         doReturn(bundle).when(serviceReference).getBundle();
73         doReturn(context).when(bundle).getBundleContext();
74         doReturn("").when(serviceReference).toString();
75         doReturn(serviceFactory).when(context).getService(any(ServiceReference.class));
76         doReturn(service).when(serviceFactory).createService(anyString());
77         doReturn(Collections.emptySet()).when(service).getCapabilities();
78         doNothing().when(service).close();
79         doReturn("serviceFactoryMock").when(serviceFactory).toString();
80     }
81
82     public BundleContext getBundleContext() {
83         return context;
84     }
85
86     private void initContext(long maxWaitForCapabilitiesMillis, long conflictingVersionTimeoutMillis) {
87         initProp(context, "active", "1");
88         initProp(context, "1." + ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX, DummyAdapterWithInitialSnapshot.class.getName());
89         initProp(context, "1." + "readonly", "false");
90         initProp(context, "1." + ".properties.fileStorage", "target/configuration-persister-test/initial/");
91         initProp(context, ConfigPersisterActivator.MAX_WAIT_FOR_CAPABILITIES_MILLIS_PROPERTY, String.valueOf(maxWaitForCapabilitiesMillis));
92         initProp(context, ConfigPersisterActivator.CONFLICTING_VERSION_TIMEOUT_MILLIS_PROPERTY, String.valueOf(conflictingVersionTimeoutMillis));
93     }
94
95     private void initProp(BundleContext context, String key, String value) {
96         initPropNoPrefix(context, ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER + "." + key, value);
97     }
98
99     private void initPropNoPrefix(BundleContext context, String key, String value) {
100         doReturn(value).when(context).getProperty(key);
101     }
102
103     public static class DummyAdapterWithInitialSnapshot extends DummyAdapter {
104
105         public static final String CONFIG_SNAPSHOT = "config-snapshot";
106         public static String expectedCapability = "cap2";
107
108         @Override
109         public List<ConfigSnapshotHolder> loadLastConfigs() throws IOException {
110             return Lists.newArrayList(getConfigSnapshot());
111         }
112
113         @Override
114         public Persister instantiate(PropertiesProvider propertiesProvider) {
115             return this;
116         }
117
118         public ConfigSnapshotHolder getConfigSnapshot() {
119             return new ConfigSnapshotHolder() {
120                 @Override
121                 public String getConfigSnapshot() {
122                     return "<data><" + CONFIG_SNAPSHOT + "/></data>";
123                 }
124
125                 @Override
126                 public SortedSet<String> getCapabilities() {
127                     TreeSet<String> strings = Sets.newTreeSet();
128                     strings.add(expectedCapability);
129                     return strings;
130                 }
131
132                 @Override
133                 public String toString() {
134                     return getConfigSnapshot();
135                 }
136             };
137         }
138     }
139 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.