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 {
45 TestingPropertiesProvider() {
50 public String getFullKeyForReporting(String key) {
51 return "prefix." + key;
55 public String getProperty(String key) {
56 throw new UnsupportedOperationException("should be mocked");
61 public void setUpMocks() {
62 MockitoAnnotations.initMocks(this);
63 doCallRealMethod().when(propertiesProvider).getFullKeyForReporting(anyString());
68 public void testFromProperties() throws Exception {
69 doReturn("").when(propertiesProvider).getProperty(ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER);
70 doReturn(MockAdapter.class.getName()).when(propertiesProvider).getProperty(
71 ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX);
72 doReturn("false").when(propertiesProvider).getProperty("readOnly");
74 PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(propertiesProvider);
75 persisterAggregator.persistConfig(null);
76 persisterAggregator.loadLastConfig();
77 persisterAggregator.persistConfig(null);
78 persisterAggregator.loadLastConfig();
80 assertEquals(2, MockAdapter.persist);
81 assertEquals(2, MockAdapter.load);
82 assertEquals(1, MockAdapter.props);
88 public void testFromProperties2() throws Exception {
90 doReturn(prefix).when(propertiesProvider).getProperty(ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER);
91 doReturn(FileStorageAdapter.class.getName()).when(propertiesProvider).getProperty(
92 ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX);
94 doReturn("target" + File.separator + "generated-test-sources" + File.separator + "testFile").when(
95 propertiesProvider).getProperty("prefix.properties.fileStorage");
96 doReturn("propertiesProvider").when(propertiesProvider).toString();
97 doReturn(null).when(propertiesProvider).getProperty("prefix.properties.numberOfBackups");
99 PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(propertiesProvider);
104 public void testFromProperties3() throws Exception {
105 doReturn("").when(propertiesProvider).getProperty(ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER);
106 doReturn(FileStorageAdapter.class.getName()).when(propertiesProvider).getProperty(
107 ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX);
108 doReturn("target" + File.separator + "generated-test-sources" + File.separator + "testFile").when(
109 propertiesProvider).getProperty("prefix.properties.fileStorage");
110 doReturn("false").when(propertiesProvider).getProperty("readOnly");
111 doReturn("propertiesProvider").when(propertiesProvider).toString();
112 doReturn("0").when(propertiesProvider).getProperty("prefix.properties.numberOfBackups");
114 PersisterAggregator.createFromProperties(propertiesProvider);
116 } catch (RuntimeException e) {
119 containsString("numberOfBackups property should be either set to positive value, or ommited. Can not be set to 0."));
124 public void loadLastConfig() throws Exception {
125 List<PersisterAggregator.PersisterWithConfiguration> persisterWithConfigurations = new ArrayList<>();
126 PersisterAggregator.PersisterWithConfiguration first = new PersisterAggregator.PersisterWithConfiguration(mock(Persister.class), false);
128 ConfigSnapshotHolder ignored = mock(ConfigSnapshotHolder.class);
129 doReturn(Optional.of(ignored)).when(first.storage).loadLastConfig(); // should be ignored
131 ConfigSnapshotHolder used = mock(ConfigSnapshotHolder.class);
132 PersisterAggregator.PersisterWithConfiguration second = new PersisterAggregator.PersisterWithConfiguration(mock(Persister.class), false);
133 doReturn(Optional.of(used)).when(second.storage).loadLastConfig(); // should be used
135 PersisterAggregator.PersisterWithConfiguration third = new PersisterAggregator.PersisterWithConfiguration(mock(Persister.class), false);
136 doReturn(Optional.absent()).when(third.storage).loadLastConfig();
138 persisterWithConfigurations.add(first);
139 persisterWithConfigurations.add(second);
140 persisterWithConfigurations.add(third);
142 PersisterAggregator persisterAggregator = new PersisterAggregator(persisterWithConfigurations);
143 Optional<ConfigSnapshotHolder> configSnapshotHolderOptional = persisterAggregator.loadLastConfig();
144 assertTrue(configSnapshotHolderOptional.isPresent());
145 assertEquals(used, configSnapshotHolderOptional.get());
150 public void test() throws Exception {
151 // Persister storage = mock(Persister.class);
152 // doReturn(null).when(storage).loadLastConfig();
153 // doNothing().when(storage).persistConfig(any(ConfigSnapshotHolder.class));
155 // PersisterAggregator persister = new PersisterAggregator(storage);
156 // persister.loadLastConfig();
157 // persister.persistConfig(null);
159 // verify(storage).loadLastConfig();
160 // verify(storage).persistConfig(any(ConfigSnapshotHolder.class));
163 public static class MockAdapter implements StorageAdapter, Persister {
165 static int persist = 0;
168 public void persistConfig(ConfigSnapshotHolder holder) throws IOException {
175 public Optional<ConfigSnapshotHolder> loadLastConfig() throws IOException {
177 return Optional.absent();
180 static int props = 0;
183 public Persister instantiate(PropertiesProvider propertiesProvider) {
189 public void close() {