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 org.junit.Test;
12 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
13 import org.opendaylight.controller.config.persist.api.Persister;
14 import org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter;
15 import org.opendaylight.controller.netconf.persist.impl.osgi.ConfigPersisterActivator;
16 import org.opendaylight.controller.netconf.persist.impl.osgi.PropertiesProviderBaseImpl;
18 import java.io.IOException;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22 import java.util.Properties;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertThat;
27 import static org.junit.Assert.fail;
28 import static org.junit.matchers.JUnitMatchers.containsString;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.mock;
31 import static org.opendaylight.controller.netconf.persist.impl.PersisterAggregator.PersisterWithConfiguration;
32 import static org.opendaylight.controller.netconf.persist.impl.PersisterAggregatorTest.TestingPropertiesProvider.loadFile;
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 testDummyAdapter() throws Exception {
73 PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(loadFile("test1.properties"));
74 List<PersisterWithConfiguration> persisters = persisterAggregator.getPersisterWithConfigurations();
75 assertEquals(1, persisters.size());
76 PersisterWithConfiguration persister = persisters.get(0);
77 assertEquals(DummyAdapter.class.getName(), persister.getStorage().getClass().getName());
78 assertFalse(persister.isReadOnly());
80 persisterAggregator.persistConfig(null);
81 persisterAggregator.loadLastConfigs();
82 persisterAggregator.persistConfig(null);
83 persisterAggregator.loadLastConfigs();
85 assertEquals(2, DummyAdapter.persist);
86 assertEquals(2, DummyAdapter.load);
87 assertEquals(1, DummyAdapter.props);
91 public void testLoadFromPropertyFile() throws Exception {
92 PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(loadFile("test2.properties"));
93 List<PersisterWithConfiguration> persisters = persisterAggregator.getPersisterWithConfigurations();
94 assertEquals(1, persisters.size());
95 PersisterWithConfiguration persister = persisters.get(0);
96 assertEquals(XmlFileStorageAdapter.class.getName() ,persister.getStorage().getClass().getName());
97 assertFalse(persister.isReadOnly());
101 public void testFileStorageNumberOfBackups() throws Exception {
103 PersisterAggregator.createFromProperties(loadFile("test3.properties"));
105 } catch (RuntimeException e) {
108 containsString("numberOfBackups property should be either set to positive value, or ommited. Can not be set to 0."));
112 private ConfigSnapshotHolder mockHolder(String name){
113 ConfigSnapshotHolder result = mock(ConfigSnapshotHolder.class);
114 doReturn("mock:" + name).when(result).toString();
118 private Persister mockPersister(String name){
119 Persister result = mock(Persister.class);
120 doReturn("mock:" + name).when(result).toString();
125 public void loadLastConfig() throws Exception {
126 List<PersisterWithConfiguration> persisterWithConfigurations = new ArrayList<>();
127 PersisterWithConfiguration first = new PersisterWithConfiguration(mock(Persister.class), false);
129 ConfigSnapshotHolder ignored = mockHolder("ignored");
130 doReturn(Arrays.asList(ignored)).when(first.getStorage()).loadLastConfigs(); // should be ignored
133 ConfigSnapshotHolder used = mockHolder("used");
134 PersisterWithConfiguration second = new PersisterWithConfiguration(mockPersister("p1"), false);
135 doReturn(Arrays.asList(used)).when(second.getStorage()).loadLastConfigs(); // should be used
137 PersisterWithConfiguration third = new PersisterWithConfiguration(mockPersister("p2"), false);
138 doReturn(Arrays.asList()).when(third.getStorage()).loadLastConfigs();
140 persisterWithConfigurations.add(first);
141 persisterWithConfigurations.add(second);
142 persisterWithConfigurations.add(third);
144 PersisterAggregator persisterAggregator = new PersisterAggregator(persisterWithConfigurations);
145 List<ConfigSnapshotHolder> configSnapshotHolderOptional = persisterAggregator.loadLastConfigs();
146 assertEquals(1, configSnapshotHolderOptional.size());
147 assertEquals(used, configSnapshotHolderOptional.get(0));