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.Before;
13 import org.junit.Ignore;
14 import org.junit.Test;
15 import org.mockito.Mock;
16 import org.mockito.MockitoAnnotations;
17 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
18 import org.opendaylight.controller.config.persist.api.Persister;
19 import org.opendaylight.controller.config.persist.api.PropertiesProvider;
20 import org.opendaylight.controller.config.persist.api.StorageAdapter;
21 import org.opendaylight.controller.config.persist.storage.file.FileStorageAdapter;
22 import org.opendaylight.controller.netconf.persist.impl.osgi.ConfigPersisterActivator;
23 import org.opendaylight.controller.netconf.persist.impl.osgi.PropertiesProviderBaseImpl;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.List;
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertThat;
32 import static org.junit.Assert.assertTrue;
33 import static org.junit.Assert.fail;
34 import static org.junit.matchers.JUnitMatchers.containsString;
35 import static org.mockito.Matchers.anyString;
36 import static org.mockito.Mockito.doCallRealMethod;
37 import static org.mockito.Mockito.doReturn;
38 import static org.mockito.Mockito.mock;
40 public class PersisterAggregatorTest {
42 TestingPropertiesProvider propertiesProvider;
44 class TestingPropertiesProvider extends PropertiesProviderBaseImpl {
46 TestingPropertiesProvider() {
51 public String getFullKeyForReporting(String key) {
52 return "prefix." + key;
56 public String getProperty(String key) {
57 throw new UnsupportedOperationException("should be mocked");
62 public void setUpMocks() {
63 MockitoAnnotations.initMocks(this);
64 doCallRealMethod().when(propertiesProvider).getFullKeyForReporting(anyString());
69 public void testFromProperties() throws Exception {
70 doReturn("").when(propertiesProvider).getProperty(ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER);
71 doReturn(MockAdapter.class.getName()).when(propertiesProvider).getProperty(
72 ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX);
73 doReturn("false").when(propertiesProvider).getProperty("readOnly");
75 PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(propertiesProvider);
76 persisterAggregator.persistConfig(null);
77 persisterAggregator.loadLastConfig();
78 persisterAggregator.persistConfig(null);
79 persisterAggregator.loadLastConfig();
81 assertEquals(2, MockAdapter.persist);
82 assertEquals(2, MockAdapter.load);
83 assertEquals(1, MockAdapter.props);
89 public void testFromProperties2() throws Exception {
91 doReturn(prefix).when(propertiesProvider).getProperty(ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER);
92 doReturn(FileStorageAdapter.class.getName()).when(propertiesProvider).getProperty(
93 ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX);
95 doReturn("target" + File.separator + "generated-test-sources" + File.separator + "testFile").when(
96 propertiesProvider).getProperty("prefix.properties.fileStorage");
97 doReturn("propertiesProvider").when(propertiesProvider).toString();
98 doReturn(null).when(propertiesProvider).getProperty("prefix.properties.numberOfBackups");
100 PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(propertiesProvider);
105 public void testFromProperties3() throws Exception {
106 doReturn("").when(propertiesProvider).getProperty(ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER);
107 doReturn(FileStorageAdapter.class.getName()).when(propertiesProvider).getProperty(
108 ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX);
109 doReturn("target" + File.separator + "generated-test-sources" + File.separator + "testFile").when(
110 propertiesProvider).getProperty("prefix.properties.fileStorage");
111 doReturn("false").when(propertiesProvider).getProperty("readOnly");
112 doReturn("propertiesProvider").when(propertiesProvider).toString();
113 doReturn("0").when(propertiesProvider).getProperty("prefix.properties.numberOfBackups");
115 PersisterAggregator.createFromProperties(propertiesProvider);
117 } catch (RuntimeException e) {
120 containsString("numberOfBackups property should be either set to positive value, or ommited. Can not be set to 0."));
125 public void loadLastConfig() throws Exception {
126 List<PersisterAggregator.PersisterWithConfiguration> persisterWithConfigurations = new ArrayList<>();
127 PersisterAggregator.PersisterWithConfiguration first = new PersisterAggregator.PersisterWithConfiguration(mock(Persister.class), false);
129 ConfigSnapshotHolder ignored = mock(ConfigSnapshotHolder.class);
130 doReturn(Optional.of(ignored)).when(first.storage).loadLastConfig(); // should be ignored
132 ConfigSnapshotHolder used = mock(ConfigSnapshotHolder.class);
133 PersisterAggregator.PersisterWithConfiguration second = new PersisterAggregator.PersisterWithConfiguration(mock(Persister.class), false);
134 doReturn(Optional.of(used)).when(second.storage).loadLastConfig(); // should be used
136 PersisterAggregator.PersisterWithConfiguration third = new PersisterAggregator.PersisterWithConfiguration(mock(Persister.class), false);
137 doReturn(Optional.absent()).when(third.storage).loadLastConfig();
139 persisterWithConfigurations.add(first);
140 persisterWithConfigurations.add(second);
141 persisterWithConfigurations.add(third);
143 PersisterAggregator persisterAggregator = new PersisterAggregator(persisterWithConfigurations);
144 Optional<ConfigSnapshotHolder> configSnapshotHolderOptional = persisterAggregator.loadLastConfig();
145 assertTrue(configSnapshotHolderOptional.isPresent());
146 assertEquals(used, configSnapshotHolderOptional.get());
151 public void test() throws Exception {
152 // Persister storage = mock(Persister.class);
153 // doReturn(null).when(storage).loadLastConfig();
154 // doNothing().when(storage).persistConfig(any(ConfigSnapshotHolder.class));
156 // PersisterAggregator persister = new PersisterAggregator(storage);
157 // persister.loadLastConfig();
158 // persister.persistConfig(null);
160 // verify(storage).loadLastConfig();
161 // verify(storage).persistConfig(any(ConfigSnapshotHolder.class));
164 public static class MockAdapter implements StorageAdapter, Persister {
166 static int persist = 0;
169 public void persistConfig(ConfigSnapshotHolder holder) throws IOException {
176 public Optional<ConfigSnapshotHolder> loadLastConfig() throws IOException {
178 return Optional.absent();
181 static int props = 0;
184 public Persister instantiate(PropertiesProvider propertiesProvider) {
190 public void close() {