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 com.google.common.collect.Lists;
13 import org.junit.Test;
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.storage.file.xml.XmlFileStorageAdapter;
17 import org.opendaylight.controller.netconf.persist.impl.osgi.ConfigPersisterActivator;
18 import org.opendaylight.controller.netconf.persist.impl.osgi.PropertiesProviderBaseImpl;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24 import java.util.Properties;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertThat;
29 import static org.junit.Assert.fail;
30 import static org.junit.matchers.JUnitMatchers.containsString;
31 import static org.mockito.Mockito.doReturn;
32 import static org.mockito.Mockito.mock;
33 import static org.opendaylight.controller.netconf.persist.impl.PersisterAggregator.PersisterWithConfiguration;
34 import static org.opendaylight.controller.netconf.persist.impl.PersisterAggregatorTest.TestingPropertiesProvider.loadFile;
36 public class PersisterAggregatorTest {
38 static class TestingPropertiesProvider extends PropertiesProviderBaseImpl {
40 private final Properties prop;
42 public TestingPropertiesProvider(Properties prop) {
47 public static TestingPropertiesProvider loadFile(String fileName) {
48 Properties prop = new Properties();
50 prop.load(TestingPropertiesProvider.class.getClassLoader().getResourceAsStream(fileName));
51 } catch (IOException e) {
52 throw new RuntimeException(e);
54 return new TestingPropertiesProvider(prop);
58 public String getFullKeyForReporting(String key) {
59 return ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER + "." + key;
63 public String getProperty(String key) {
64 return prop.getProperty(getFullKeyForReporting(key));
68 public String getPropertyWithoutPrefix(String fullKey){
69 return prop.getProperty(fullKey);
74 public void testDummyAdapter() throws Exception {
75 PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(loadFile("test1.properties"));
76 List<PersisterWithConfiguration> persisters = persisterAggregator.getPersisterWithConfigurations();
77 assertEquals(1, persisters.size());
78 PersisterWithConfiguration persister = persisters.get(0);
79 assertEquals(DummyAdapter.class.getName(), persister.getStorage().getClass().getName());
80 assertFalse(persister.isReadOnly());
82 persisterAggregator.persistConfig(null);
83 persisterAggregator.loadLastConfigs();
84 persisterAggregator.persistConfig(null);
85 persisterAggregator.loadLastConfigs();
87 assertEquals(2, DummyAdapter.persist);
88 assertEquals(2, DummyAdapter.load);
89 assertEquals(1, DummyAdapter.props);
93 public void testNoopAdapter() throws Exception {
94 final NoOpStorageAdapter noOpStorageAdapter = new NoOpStorageAdapter();
95 final PersisterAggregator persisterAggregator =
96 new PersisterAggregator(Lists.newArrayList(new PersisterWithConfiguration(noOpStorageAdapter, false)));
98 noOpStorageAdapter.instantiate(null);
100 persisterAggregator.persistConfig(null);
101 persisterAggregator.loadLastConfigs();
102 persisterAggregator.persistConfig(null);
103 persisterAggregator.loadLastConfigs();
105 noOpStorageAdapter.close();
109 public void testLoadFromPropertyFile() throws Exception {
110 PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(loadFile("test2.properties"));
111 List<PersisterWithConfiguration> persisters = persisterAggregator.getPersisterWithConfigurations();
112 assertEquals(1, persisters.size());
113 PersisterWithConfiguration persister = persisters.get(0);
114 assertEquals(XmlFileStorageAdapter.class.getName() ,persister.getStorage().getClass().getName());
115 assertFalse(persister.isReadOnly());
119 public void testFileStorageNumberOfBackups() throws Exception {
121 PersisterAggregator.createFromProperties(loadFile("test3.properties"));
123 } catch (RuntimeException e) {
126 containsString("numberOfBackups property should be either set to positive value, or ommited. Can not be set to 0."));
130 private ConfigSnapshotHolder mockHolder(String name){
131 ConfigSnapshotHolder result = mock(ConfigSnapshotHolder.class);
132 doReturn("mock:" + name).when(result).toString();
136 private Persister mockPersister(String name){
137 Persister result = mock(Persister.class);
138 doReturn("mock:" + name).when(result).toString();
143 public void loadLastConfig() throws Exception {
144 List<PersisterWithConfiguration> persisterWithConfigurations = new ArrayList<>();
145 PersisterWithConfiguration first = new PersisterWithConfiguration(mock(Persister.class), false);
147 ConfigSnapshotHolder ignored = mockHolder("ignored");
148 doReturn(Arrays.asList(ignored)).when(first.getStorage()).loadLastConfigs(); // should be ignored
151 ConfigSnapshotHolder used = mockHolder("used");
152 PersisterWithConfiguration second = new PersisterWithConfiguration(mockPersister("p1"), false);
153 doReturn(Arrays.asList(used)).when(second.getStorage()).loadLastConfigs(); // should be used
155 PersisterWithConfiguration third = new PersisterWithConfiguration(mockPersister("p2"), false);
156 doReturn(Arrays.asList()).when(third.getStorage()).loadLastConfigs();
158 persisterWithConfigurations.add(first);
159 persisterWithConfigurations.add(second);
160 persisterWithConfigurations.add(third);
162 PersisterAggregator persisterAggregator = new PersisterAggregator(persisterWithConfigurations);
163 List<ConfigSnapshotHolder> configSnapshotHolderOptional = persisterAggregator.loadLastConfigs();
164 assertEquals(1, configSnapshotHolderOptional.size());
165 assertEquals(used, configSnapshotHolderOptional.get(0));