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.Before;
27 import org.junit.Test;
28 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
29 import org.opendaylight.controller.config.persist.api.Persister;
30 import org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter;
31 import org.opendaylight.controller.netconf.persist.impl.osgi.ConfigPersisterActivator;
32 import org.opendaylight.controller.netconf.persist.impl.osgi.PropertiesProviderBaseImpl;
34 public class PersisterAggregatorTest {
36 static class TestingPropertiesProvider extends PropertiesProviderBaseImpl {
38 private final Properties prop;
40 public TestingPropertiesProvider(Properties prop) {
45 public static TestingPropertiesProvider loadFile(String fileName) {
46 Properties prop = new Properties();
48 prop.load(TestingPropertiesProvider.class.getClassLoader().getResourceAsStream(fileName));
49 } catch (IOException e) {
50 throw new RuntimeException(e);
52 return new TestingPropertiesProvider(prop);
56 public String getFullKeyForReporting(String key) {
57 return ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER + "." + key;
61 public String getProperty(String key) {
62 return prop.getProperty(getFullKeyForReporting(key));
66 public String getPropertyWithoutPrefix(String fullKey){
67 return prop.getProperty(fullKey);
72 public void setUp() throws Exception {
73 if(XmlFileStorageAdapter.getInstance().isPresent()) {
74 XmlFileStorageAdapter.getInstance().get().reset();
79 public void testDummyAdapter() throws Exception {
80 PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(TestingPropertiesProvider.loadFile("test1.properties"));
81 List<PersisterWithConfiguration> persisters = persisterAggregator.getPersisterWithConfigurations();
82 assertEquals(1, persisters.size());
83 PersisterWithConfiguration persister = persisters.get(0);
84 assertEquals(DummyAdapter.class.getName(), persister.getStorage().getClass().getName());
85 assertFalse(persister.isReadOnly());
87 persisterAggregator.persistConfig(null);
88 persisterAggregator.loadLastConfigs();
89 persisterAggregator.persistConfig(null);
90 persisterAggregator.loadLastConfigs();
92 assertEquals(2, DummyAdapter.persist);
93 assertEquals(2, DummyAdapter.load);
94 assertEquals(1, DummyAdapter.props);
98 public void testNoopAdapter() throws Exception {
99 final NoOpStorageAdapter noOpStorageAdapter = new NoOpStorageAdapter();
100 final PersisterAggregator persisterAggregator =
101 new PersisterAggregator(Lists.newArrayList(new PersisterWithConfiguration(noOpStorageAdapter, false)));
103 noOpStorageAdapter.instantiate(null);
105 persisterAggregator.persistConfig(null);
106 persisterAggregator.loadLastConfigs();
107 persisterAggregator.persistConfig(null);
108 persisterAggregator.loadLastConfigs();
110 noOpStorageAdapter.close();
114 public void testLoadFromPropertyFile() throws Exception {
115 PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(TestingPropertiesProvider.loadFile("test2.properties"));
116 List<PersisterWithConfiguration> persisters = persisterAggregator.getPersisterWithConfigurations();
117 assertEquals(1, persisters.size());
118 PersisterWithConfiguration persister = persisters.get(0);
119 assertEquals(XmlFileStorageAdapter.class.getName() ,persister.getStorage().getClass().getName());
120 assertFalse(persister.isReadOnly());
124 public void testFileStorageNumberOfBackups() throws Exception {
126 PersisterAggregator.createFromProperties(TestingPropertiesProvider.loadFile("test3.properties"));
128 } catch (RuntimeException e) {
131 containsString("numberOfBackups property should be either set to positive value, or ommited. Can not be set to 0."));
135 private ConfigSnapshotHolder mockHolder(String name){
136 ConfigSnapshotHolder result = mock(ConfigSnapshotHolder.class);
137 doReturn("mock:" + name).when(result).toString();
141 private Persister mockPersister(String name){
142 Persister result = mock(Persister.class);
143 doReturn("mock:" + name).when(result).toString();
148 public void loadLastConfig() throws Exception {
149 List<PersisterWithConfiguration> persisterWithConfigurations = new ArrayList<>();
150 PersisterWithConfiguration first = new PersisterWithConfiguration(mock(Persister.class), false);
152 ConfigSnapshotHolder ignored = mockHolder("ignored");
153 doReturn(Arrays.asList(ignored)).when(first.getStorage()).loadLastConfigs(); // should be ignored
156 ConfigSnapshotHolder used = mockHolder("used");
157 PersisterWithConfiguration second = new PersisterWithConfiguration(mockPersister("p1"), false);
158 doReturn(Arrays.asList(used)).when(second.getStorage()).loadLastConfigs(); // should be used
160 PersisterWithConfiguration third = new PersisterWithConfiguration(mockPersister("p2"), false);
161 doReturn(Arrays.asList()).when(third.getStorage()).loadLastConfigs();
163 persisterWithConfigurations.add(first);
164 persisterWithConfigurations.add(second);
165 persisterWithConfigurations.add(third);
167 PersisterAggregator persisterAggregator = new PersisterAggregator(persisterWithConfigurations);
168 List<ConfigSnapshotHolder> configSnapshotHolderOptional = persisterAggregator.loadLastConfigs();
169 assertEquals(1, configSnapshotHolderOptional.size());
170 assertEquals(used, configSnapshotHolderOptional.get(0));