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.persist.impl.DummyAdapter;
18 import org.osgi.framework.BundleContext;
20 import java.io.IOException;
21 import java.util.List;
22 import java.util.SortedSet;
23 import java.util.TreeSet;
25 import static org.mockito.Mockito.doReturn;
27 final class MockedBundleContext {
30 private BundleContext context;
32 MockedBundleContext(String netconfAddress, String netconfPort) {
33 MockitoAnnotations.initMocks(this);
34 initContext(netconfAddress, netconfPort);
37 public BundleContext getBundleContext() {
41 private void initContext(String netconfAddress, String netconfPort) {
42 initProp(context, ConfigPersisterActivator.IGNORED_MISSING_CAPABILITY_REGEX_SUFFIX, null);
44 initPropNoPrefix(context, "netconf.tcp.client.address", netconfAddress);
45 initPropNoPrefix(context, "netconf.tcp.client.port", netconfPort);
47 initProp(context, "active", "1");
48 initProp(context, "1." + ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX, DummyAdapterWithInitialSnapshot.class.getName());
49 initProp(context, "1." + "readonly", "false");
50 initProp(context, "1." + ".properties.fileStorage", "target/configuration-persister-test/initial/");
54 private void initProp(BundleContext context, String key, String value) {
55 initPropNoPrefix(context, ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER + "." + key, value);
58 private void initPropNoPrefix(BundleContext context, String key, String value) {
59 doReturn(value).when(context).getProperty(key);
62 public static class DummyAdapterWithInitialSnapshot extends DummyAdapter {
64 public static final String CONFIG_SNAPSHOT = "config-snapshot";
65 public static String expectedCapability = "cap2";
68 public List<ConfigSnapshotHolder> loadLastConfigs() throws IOException {
69 return Lists.newArrayList(getConfigSnapshopt());
73 public Persister instantiate(PropertiesProvider propertiesProvider) {
77 public ConfigSnapshotHolder getConfigSnapshopt() {
78 return new ConfigSnapshotHolder() {
80 public String getConfigSnapshot() {
81 return "<data><" + CONFIG_SNAPSHOT + "/></data>";
85 public SortedSet<String> getCapabilities() {
86 TreeSet<String> strings = Sets.newTreeSet();
87 strings.add(expectedCapability);
92 public String toString() {
93 return getConfigSnapshot();