2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.netconf.persist.impl.osgi;
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.NetconfOperationService;
18 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
19 import org.opendaylight.controller.netconf.persist.impl.DummyAdapter;
20 import org.osgi.framework.Bundle;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.Filter;
23 import org.osgi.framework.ServiceListener;
24 import org.osgi.framework.ServiceReference;
26 import java.io.IOException;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.SortedSet;
30 import java.util.TreeSet;
32 import static org.mockito.Matchers.any;
33 import static org.mockito.Matchers.anyString;
34 import static org.mockito.Matchers.eq;
35 import static org.mockito.Mockito.doNothing;
36 import static org.mockito.Mockito.doReturn;
38 final class MockedBundleContext {
40 private BundleContext context;
42 private Filter filter;
44 private ServiceReference<?> serviceReference;
46 private Bundle bundle;
48 NetconfOperationServiceFactory serviceFactory;
50 private NetconfOperationService service;
52 MockedBundleContext(long maxWaitForCapabilitiesMillis, long conflictingVersionTimeoutMillis) throws Exception {
53 MockitoAnnotations.initMocks(this);
54 doReturn(null).when(context).getProperty(anyString());
55 initContext(maxWaitForCapabilitiesMillis, conflictingVersionTimeoutMillis);
56 doReturn(filter).when(context).createFilter(ConfigPersisterActivator.getFilterString());
57 String filterString = "filter";
58 doReturn(filterString).when(filter).toString();
59 doNothing().when(context).addServiceListener(any(ServiceListener.class), eq(filterString));
60 ServiceReference<?>[] toBeReturned = {serviceReference};
61 doReturn(toBeReturned).when(context).getServiceReferences((String) null, filterString);
62 doReturn(bundle).when(serviceReference).getBundle();
63 doReturn(context).when(bundle).getBundleContext();
64 doReturn("").when(serviceReference).toString();
65 doReturn(serviceFactory).when(context).getService(any(ServiceReference.class));
66 doReturn(service).when(serviceFactory).createService(anyString());
67 doReturn(Collections.emptySet()).when(service).getCapabilities();
68 doNothing().when(service).close();
71 public BundleContext getBundleContext() {
75 private void initContext(long maxWaitForCapabilitiesMillis, long conflictingVersionTimeoutMillis) {
76 initProp(context, "active", "1");
77 initProp(context, "1." + ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX, DummyAdapterWithInitialSnapshot.class.getName());
78 initProp(context, "1." + "readonly", "false");
79 initProp(context, "1." + ".properties.fileStorage", "target/configuration-persister-test/initial/");
80 initProp(context, ConfigPersisterActivator.MAX_WAIT_FOR_CAPABILITIES_MILLIS_PROPERTY, String.valueOf(maxWaitForCapabilitiesMillis));
81 initProp(context, ConfigPersisterActivator.CONFLICTING_VERSION_TIMEOUT_MILLIS_PROPERTY, String.valueOf(conflictingVersionTimeoutMillis));
84 private void initProp(BundleContext context, String key, String value) {
85 initPropNoPrefix(context, ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER + "." + key, value);
88 private void initPropNoPrefix(BundleContext context, String key, String value) {
89 doReturn(value).when(context).getProperty(key);
92 public static class DummyAdapterWithInitialSnapshot extends DummyAdapter {
94 public static final String CONFIG_SNAPSHOT = "config-snapshot";
95 public static String expectedCapability = "cap2";
98 public List<ConfigSnapshotHolder> loadLastConfigs() throws IOException {
99 return Lists.newArrayList(getConfigSnapshot());
103 public Persister instantiate(PropertiesProvider propertiesProvider) {
107 public ConfigSnapshotHolder getConfigSnapshot() {
108 return new ConfigSnapshotHolder() {
110 public String getConfigSnapshot() {
111 return "<data><" + CONFIG_SNAPSHOT + "/></data>";
115 public SortedSet<String> getCapabilities() {
116 TreeSet<String> strings = Sets.newTreeSet();
117 strings.add(expectedCapability);
122 public String toString() {
123 return getConfigSnapshot();