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.config.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.doNothing;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.mock;
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.impl.PersisterAggregator.PersisterWithConfiguration;
31 import org.opendaylight.controller.config.persist.impl.osgi.ConfigPersisterActivator;
32 import org.opendaylight.controller.config.persist.impl.osgi.PropertiesProviderBaseImpl;
33 import org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter;
35 public class PersisterAggregatorTest {
37 static class TestingPropertiesProvider extends PropertiesProviderBaseImpl {
39 private final Properties prop;
41 public TestingPropertiesProvider(Properties prop) {
46 public static TestingPropertiesProvider loadFile(String fileName) {
47 Properties prop = new Properties();
49 prop.load(TestingPropertiesProvider.class.getClassLoader().getResourceAsStream(fileName));
50 } catch (IOException e) {
51 throw new RuntimeException(e);
53 return new TestingPropertiesProvider(prop);
57 public String getFullKeyForReporting(String key) {
58 return ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER + "." + key;
62 public String getProperty(String key) {
63 return prop.getProperty(getFullKeyForReporting(key));
67 public String getPropertyWithoutPrefix(String fullKey){
68 return prop.getProperty(fullKey);
73 public void setUp() throws Exception {
74 if(XmlFileStorageAdapter.getInstance().isPresent()) {
75 XmlFileStorageAdapter.getInstance().get().reset();
80 public void testDummyAdapter() throws Exception {
81 PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(TestingPropertiesProvider.loadFile("test1.properties"));
82 List<PersisterWithConfiguration> persisters = persisterAggregator.getPersisterWithConfigurations();
83 assertEquals(1, persisters.size());
84 PersisterWithConfiguration persister = persisters.get(0);
85 assertEquals(DummyAdapter.class.getName(), persister.getStorage().getClass().getName());
86 assertFalse(persister.isReadOnly());
88 persisterAggregator.persistConfig(null);
89 persisterAggregator.loadLastConfigs();
90 persisterAggregator.persistConfig(null);
91 persisterAggregator.loadLastConfigs();
93 assertEquals(2, DummyAdapter.persist);
94 assertEquals(2, DummyAdapter.load);
95 assertEquals(1, DummyAdapter.props);
99 public void testNoopAdapter() throws Exception {
100 final NoOpStorageAdapter noOpStorageAdapter = new NoOpStorageAdapter();
101 try (final PersisterAggregator persisterAggregator = new PersisterAggregator(
102 Lists.newArrayList(new PersisterWithConfiguration(noOpStorageAdapter, false)))) {
104 noOpStorageAdapter.instantiate(null);
106 persisterAggregator.persistConfig(null);
107 persisterAggregator.loadLastConfigs();
108 persisterAggregator.persistConfig(null);
109 persisterAggregator.loadLastConfigs();
111 noOpStorageAdapter.close();
116 public void testLoadFromPropertyFile() throws Exception {
117 PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(TestingPropertiesProvider.loadFile("test2.properties"));
118 List<PersisterWithConfiguration> persisters = persisterAggregator.getPersisterWithConfigurations();
119 assertEquals(1, persisters.size());
120 PersisterWithConfiguration persister = persisters.get(0);
121 assertEquals(XmlFileStorageAdapter.class.getName() ,persister.getStorage().getClass().getName());
122 assertFalse(persister.isReadOnly());
126 public void testFileStorageNumberOfBackups() throws Exception {
128 PersisterAggregator.createFromProperties(TestingPropertiesProvider.loadFile("test3.properties"));
130 } catch (RuntimeException e) {
133 containsString("numberOfBackups property should be either set to positive value, or ommited. Can not be set to 0."));
137 private ConfigSnapshotHolder mockHolder(String name){
138 ConfigSnapshotHolder result = mock(ConfigSnapshotHolder.class);
139 doReturn("mock:" + name).when(result).toString();
143 private Persister mockPersister(String name){
144 Persister result = mock(Persister.class);
145 doReturn("mock:" + name).when(result).toString();
146 doNothing().when(result).close();
151 public void loadLastConfig() throws Exception {
152 List<PersisterWithConfiguration> persisterWithConfigurations = new ArrayList<>();
153 PersisterWithConfiguration first = new PersisterWithConfiguration(mockPersister("p0"), false);
155 ConfigSnapshotHolder ignored = mockHolder("ignored");
156 doReturn(Arrays.asList(ignored)).when(first.getStorage()).loadLastConfigs(); // should be ignored
159 ConfigSnapshotHolder used = mockHolder("used");
160 PersisterWithConfiguration second = new PersisterWithConfiguration(mockPersister("p1"), false);
161 doReturn(Arrays.asList(used)).when(second.getStorage()).loadLastConfigs(); // should be used
163 PersisterWithConfiguration third = new PersisterWithConfiguration(mockPersister("p2"), false);
164 doReturn(Arrays.asList()).when(third.getStorage()).loadLastConfigs();
166 persisterWithConfigurations.add(first);
167 persisterWithConfigurations.add(second);
168 persisterWithConfigurations.add(third);
170 try (PersisterAggregator persisterAggregator = new PersisterAggregator(persisterWithConfigurations)) {
171 List<ConfigSnapshotHolder> configSnapshotHolderOptional = persisterAggregator.loadLastConfigs();
172 assertEquals(1, configSnapshotHolderOptional.size());
173 assertEquals(used, configSnapshotHolderOptional.get(0));