Merge "Add filtering capability to config.ini in order to reference logging bridge...
[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.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;
25
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;
31
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;
37
38 final class MockedBundleContext {
39     @Mock
40     private BundleContext context;
41     @Mock
42     private Filter filter;
43     @Mock
44     private ServiceReference<?> serviceReference;
45     @Mock
46     private Bundle bundle;
47     @Mock
48     NetconfOperationServiceFactory serviceFactory;
49     @Mock
50     private NetconfOperationService service;
51
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();
69     }
70
71     public BundleContext getBundleContext() {
72         return context;
73     }
74
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));
82     }
83
84     private void initProp(BundleContext context, String key, String value) {
85         initPropNoPrefix(context, ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER + "." + key, value);
86     }
87
88     private void initPropNoPrefix(BundleContext context, String key, String value) {
89         doReturn(value).when(context).getProperty(key);
90     }
91
92     public static class DummyAdapterWithInitialSnapshot extends DummyAdapter {
93
94         public static final String CONFIG_SNAPSHOT = "config-snapshot";
95         public static String expectedCapability = "cap2";
96
97         @Override
98         public List<ConfigSnapshotHolder> loadLastConfigs() throws IOException {
99             return Lists.newArrayList(getConfigSnapshot());
100         }
101
102         @Override
103         public Persister instantiate(PropertiesProvider propertiesProvider) {
104             return this;
105         }
106
107         public ConfigSnapshotHolder getConfigSnapshot() {
108             return new ConfigSnapshotHolder() {
109                 @Override
110                 public String getConfigSnapshot() {
111                     return "<data><" + CONFIG_SNAPSHOT + "/></data>";
112                 }
113
114                 @Override
115                 public SortedSet<String> getCapabilities() {
116                     TreeSet<String> strings = Sets.newTreeSet();
117                     strings.add(expectedCapability);
118                     return strings;
119                 }
120
121                 @Override
122                 public String toString() {
123                     return getConfigSnapshot();
124                 }
125             };
126         }
127     }
128 }