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
9 package org.opendaylight.controller.netconf.persist.impl;
11 import static org.hamcrest.CoreMatchers.containsString;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertThat;
15 import static org.junit.Assert.fail;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.opendaylight.controller.netconf.persist.impl.PersisterAggregator.PersisterWithConfiguration;
20 import com.google.common.collect.Lists;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.Properties;
26 import org.junit.Test;
27 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
28 import org.opendaylight.controller.config.persist.api.Persister;
29 import org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter;
30 import org.opendaylight.controller.netconf.persist.impl.osgi.ConfigPersisterActivator;
31 import org.opendaylight.controller.netconf.persist.impl.osgi.PropertiesProviderBaseImpl;
33 public class PersisterAggregatorTest {
35 static class TestingPropertiesProvider extends PropertiesProviderBaseImpl {
37 private final Properties prop;
39 public TestingPropertiesProvider(Properties prop) {
44 public static TestingPropertiesProvider loadFile(String fileName) {
45 Properties prop = new Properties();
47 prop.load(TestingPropertiesProvider.class.getClassLoader().getResourceAsStream(fileName));
48 } catch (IOException e) {
49 throw new RuntimeException(e);
51 return new TestingPropertiesProvider(prop);
55 public String getFullKeyForReporting(String key) {
56 return ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER + "." + key;
60 public String getProperty(String key) {
61 return prop.getProperty(getFullKeyForReporting(key));
65 public String getPropertyWithoutPrefix(String fullKey){
66 return prop.getProperty(fullKey);
71 public void testDummyAdapter() throws Exception {
72 PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(TestingPropertiesProvider.loadFile("test1.properties"));
73 List<PersisterWithConfiguration> persisters = persisterAggregator.getPersisterWithConfigurations();
74 assertEquals(1, persisters.size());
75 PersisterWithConfiguration persister = persisters.get(0);
76 assertEquals(DummyAdapter.class.getName(), persister.getStorage().getClass().getName());
77 assertFalse(persister.isReadOnly());
79 persisterAggregator.persistConfig(null);
80 persisterAggregator.loadLastConfigs();
81 persisterAggregator.persistConfig(null);
82 persisterAggregator.loadLastConfigs();
84 assertEquals(2, DummyAdapter.persist);
85 assertEquals(2, DummyAdapter.load);
86 assertEquals(1, DummyAdapter.props);
90 public void testNoopAdapter() throws Exception {
91 final NoOpStorageAdapter noOpStorageAdapter = new NoOpStorageAdapter();
92 final PersisterAggregator persisterAggregator =
93 new PersisterAggregator(Lists.newArrayList(new PersisterWithConfiguration(noOpStorageAdapter, false)));
95 noOpStorageAdapter.instantiate(null);
97 persisterAggregator.persistConfig(null);
98 persisterAggregator.loadLastConfigs();
99 persisterAggregator.persistConfig(null);
100 persisterAggregator.loadLastConfigs();
102 noOpStorageAdapter.close();
106 public void testLoadFromPropertyFile() throws Exception {
107 PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(TestingPropertiesProvider.loadFile("test2.properties"));
108 List<PersisterWithConfiguration> persisters = persisterAggregator.getPersisterWithConfigurations();
109 assertEquals(1, persisters.size());
110 PersisterWithConfiguration persister = persisters.get(0);
111 assertEquals(XmlFileStorageAdapter.class.getName() ,persister.getStorage().getClass().getName());
112 assertFalse(persister.isReadOnly());
116 public void testFileStorageNumberOfBackups() throws Exception {
118 PersisterAggregator.createFromProperties(TestingPropertiesProvider.loadFile("test3.properties"));
120 } catch (RuntimeException e) {
123 containsString("numberOfBackups property should be either set to positive value, or ommited. Can not be set to 0."));
127 private ConfigSnapshotHolder mockHolder(String name){
128 ConfigSnapshotHolder result = mock(ConfigSnapshotHolder.class);
129 doReturn("mock:" + name).when(result).toString();
133 private Persister mockPersister(String name){
134 Persister result = mock(Persister.class);
135 doReturn("mock:" + name).when(result).toString();
140 public void loadLastConfig() throws Exception {
141 List<PersisterWithConfiguration> persisterWithConfigurations = new ArrayList<>();
142 PersisterWithConfiguration first = new PersisterWithConfiguration(mock(Persister.class), false);
144 ConfigSnapshotHolder ignored = mockHolder("ignored");
145 doReturn(Arrays.asList(ignored)).when(first.getStorage()).loadLastConfigs(); // should be ignored
148 ConfigSnapshotHolder used = mockHolder("used");
149 PersisterWithConfiguration second = new PersisterWithConfiguration(mockPersister("p1"), false);
150 doReturn(Arrays.asList(used)).when(second.getStorage()).loadLastConfigs(); // should be used
152 PersisterWithConfiguration third = new PersisterWithConfiguration(mockPersister("p2"), false);
153 doReturn(Arrays.asList()).when(third.getStorage()).loadLastConfigs();
155 persisterWithConfigurations.add(first);
156 persisterWithConfigurations.add(second);
157 persisterWithConfigurations.add(third);
159 PersisterAggregator persisterAggregator = new PersisterAggregator(persisterWithConfigurations);
160 List<ConfigSnapshotHolder> configSnapshotHolderOptional = persisterAggregator.loadLastConfigs();
161 assertEquals(1, configSnapshotHolderOptional.size());
162 assertEquals(used, configSnapshotHolderOptional.get(0));