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.base.Optional;
12 import org.junit.Test;
13 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
14 import org.opendaylight.controller.config.persist.api.Persister;
15 import org.opendaylight.controller.config.persist.storage.file.FileStorageAdapter;
16 import org.opendaylight.controller.netconf.persist.impl.osgi.ConfigPersisterActivator;
17 import org.opendaylight.controller.netconf.persist.impl.osgi.PropertiesProviderBaseImpl;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Properties;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.fail;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.assertThat;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.matchers.JUnitMatchers.containsString;
30 import static org.mockito.Mockito.doReturn;
31 import static org.mockito.Mockito.mock;
32 import static org.opendaylight.controller.netconf.persist.impl.PersisterAggregator.PersisterWithConfiguration;
33 import static org.opendaylight.controller.netconf.persist.impl.PersisterAggregatorTest.TestingPropertiesProvider.loadFile;
35 public class PersisterAggregatorTest {
37 static class TestingPropertiesProvider extends PropertiesProviderBaseImpl {
39 private static Properties prop = new Properties();
41 public TestingPropertiesProvider() {
45 public static TestingPropertiesProvider loadFile(String fileName) {
47 prop.load(TestingPropertiesProvider.class.getClassLoader().getResourceAsStream(fileName));
48 } catch (IOException e) {
49 throw new RuntimeException(e);
51 return new TestingPropertiesProvider();
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(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.loadLastConfig();
81 persisterAggregator.persistConfig(null);
82 persisterAggregator.loadLastConfig();
84 assertEquals(2, DummyAdapter.persist);
85 assertEquals(2, DummyAdapter.load);
86 assertEquals(1, DummyAdapter.props);
90 public void testLoadFromPropertyFile() throws Exception {
91 PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(loadFile("test2.properties"));
92 List<PersisterWithConfiguration> persisters = persisterAggregator.getPersisterWithConfigurations();
93 assertEquals(1, persisters.size());
94 PersisterWithConfiguration persister = persisters.get(0);
95 assertEquals(FileStorageAdapter.class.getName() ,persister.getStorage().getClass().getName());
96 assertFalse(persister.isReadOnly());
100 public void testFileStorageNumberOfBackups() throws Exception {
102 PersisterAggregator.createFromProperties(loadFile("test3.properties"));
104 } catch (RuntimeException e) {
107 containsString("numberOfBackups property should be either set to positive value, or ommited. Can not be set to 0."));
112 public void loadLastConfig() throws Exception {
113 List<PersisterWithConfiguration> persisterWithConfigurations = new ArrayList<>();
114 PersisterWithConfiguration first = new PersisterWithConfiguration(mock(Persister.class), false);
116 ConfigSnapshotHolder ignored = mock(ConfigSnapshotHolder.class);
117 doReturn(Optional.of(ignored)).when(first.getStorage()).loadLastConfig(); // should be ignored
119 ConfigSnapshotHolder used = mock(ConfigSnapshotHolder.class);
120 PersisterWithConfiguration second = new PersisterWithConfiguration(mock(Persister.class), false);
121 doReturn(Optional.of(used)).when(second.getStorage()).loadLastConfig(); // should be used
123 PersisterWithConfiguration third = new PersisterWithConfiguration(mock(Persister.class), false);
124 doReturn(Optional.absent()).when(third.getStorage()).loadLastConfig();
126 persisterWithConfigurations.add(first);
127 persisterWithConfigurations.add(second);
128 persisterWithConfigurations.add(third);
130 PersisterAggregator persisterAggregator = new PersisterAggregator(persisterWithConfigurations);
131 Optional<ConfigSnapshotHolder> configSnapshotHolderOptional = persisterAggregator.loadLastConfig();
132 assertTrue(configSnapshotHolderOptional.isPresent());
133 assertEquals(used, configSnapshotHolderOptional.get());