Merge "Add Enqueue validation check in FlowConfig"
[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.persist.impl.DummyAdapter;
18 import org.osgi.framework.BundleContext;
19
20 import java.io.IOException;
21 import java.util.List;
22 import java.util.SortedSet;
23 import java.util.TreeSet;
24
25 import static org.mockito.Mockito.doReturn;
26
27 final class MockedBundleContext {
28
29     @Mock
30     private BundleContext context;
31
32     MockedBundleContext(String netconfAddress, String netconfPort) {
33         MockitoAnnotations.initMocks(this);
34         initContext(netconfAddress, netconfPort);
35     }
36
37     public BundleContext getBundleContext() {
38         return context;
39     }
40
41     private void initContext(String netconfAddress, String netconfPort) {
42         initProp(context, ConfigPersisterActivator.IGNORED_MISSING_CAPABILITY_REGEX_SUFFIX, null);
43
44         initPropNoPrefix(context, "netconf.tcp.client.address", netconfAddress);
45         initPropNoPrefix(context, "netconf.tcp.client.port", netconfPort);
46
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/");
51
52     }
53
54     private void initProp(BundleContext context, String key, String value) {
55         initPropNoPrefix(context, ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER + "." + key, value);
56     }
57
58     private void initPropNoPrefix(BundleContext context, String key, String value) {
59         doReturn(value).when(context).getProperty(key);
60     }
61
62     public static class DummyAdapterWithInitialSnapshot extends DummyAdapter {
63
64         public static final String CONFIG_SNAPSHOT = "config-snapshot";
65         public static String expectedCapability = "cap2";
66
67         @Override
68         public List<ConfigSnapshotHolder> loadLastConfigs() throws IOException {
69             return Lists.newArrayList(getConfigSnapshopt());
70         }
71
72         @Override
73         public Persister instantiate(PropertiesProvider propertiesProvider) {
74             return this;
75         }
76
77         public ConfigSnapshotHolder getConfigSnapshopt() {
78             return new ConfigSnapshotHolder() {
79                 @Override
80                 public String getConfigSnapshot() {
81                     return "<data><" + CONFIG_SNAPSHOT + "/></data>";
82                 }
83
84                 @Override
85                 public SortedSet<String> getCapabilities() {
86                     TreeSet<String> strings = Sets.newTreeSet();
87                     strings.add(expectedCapability);
88                     return strings;
89                 }
90
91                 @Override
92                 public String toString() {
93                     return getConfigSnapshot();
94                 }
95             };
96         }
97     }
98 }