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 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 testDummyAdapter() throws Exception {
74 PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(loadFile("test1.properties"));
75 List<PersisterWithConfiguration> persisters = persisterAggregator.getPersisterWithConfigurations();
76 assertEquals(1, persisters.size());
77 PersisterWithConfiguration persister = persisters.get(0);
78 assertEquals(DummyAdapter.class.getName(), persister.getStorage().getClass().getName());
79 assertFalse(persister.isReadOnly());
81 persisterAggregator.persistConfig(null);
82 persisterAggregator.loadLastConfig();
83 persisterAggregator.persistConfig(null);
84 persisterAggregator.loadLastConfig();
86 assertEquals(2, DummyAdapter.persist);
87 assertEquals(2, DummyAdapter.load);
88 assertEquals(1, DummyAdapter.props);
92 public void testLoadFromPropertyFile() throws Exception {
93 PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(loadFile("test2.properties"));
94 List<PersisterWithConfiguration> persisters = persisterAggregator.getPersisterWithConfigurations();
95 assertEquals(1, persisters.size());
96 PersisterWithConfiguration persister = persisters.get(0);
97 assertEquals(FileStorageAdapter.class.getName() ,persister.getStorage().getClass().getName());
98 assertFalse(persister.isReadOnly());
102 public void testFileStorageNumberOfBackups() throws Exception {
104 PersisterAggregator.createFromProperties(loadFile("test3.properties"));
106 } catch (RuntimeException e) {
109 containsString("numberOfBackups property should be either set to positive value, or ommited. Can not be set to 0."));
114 public void loadLastConfig() throws Exception {
115 List<PersisterWithConfiguration> persisterWithConfigurations = new ArrayList<>();
116 PersisterWithConfiguration first = new PersisterWithConfiguration(mock(Persister.class), false);
118 ConfigSnapshotHolder ignored = mock(ConfigSnapshotHolder.class);
119 doReturn(Optional.of(ignored)).when(first.getStorage()).loadLastConfig(); // should be ignored
121 ConfigSnapshotHolder used = mock(ConfigSnapshotHolder.class);
122 PersisterWithConfiguration second = new PersisterWithConfiguration(mock(Persister.class), false);
123 doReturn(Optional.of(used)).when(second.getStorage()).loadLastConfig(); // should be used
125 PersisterWithConfiguration third = new PersisterWithConfiguration(mock(Persister.class), false);
126 doReturn(Optional.absent()).when(third.getStorage()).loadLastConfig();
128 persisterWithConfigurations.add(first);
129 persisterWithConfigurations.add(second);
130 persisterWithConfigurations.add(third);
132 PersisterAggregator persisterAggregator = new PersisterAggregator(persisterWithConfigurations);
133 Optional<ConfigSnapshotHolder> configSnapshotHolderOptional = persisterAggregator.loadLastConfig();
134 assertTrue(configSnapshotHolderOptional.isPresent());
135 assertEquals(used, configSnapshotHolderOptional.get());