e6464f8403b57859ce6ce2798a4c20a97cbe85f5
[controller.git] / opendaylight / netconf / config-persister-impl / src / test / java / org / opendaylight / controller / netconf / persist / impl / PersisterAggregatorTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.persist.impl;
10
11 import static org.hamcrest.CoreMatchers.containsString;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertThat;
15 import static org.junit.Assert.fail;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.opendaylight.controller.netconf.persist.impl.PersisterAggregator.PersisterWithConfiguration;
19
20 import com.google.common.collect.Lists;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.Properties;
26 import org.junit.Test;
27 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
28 import org.opendaylight.controller.config.persist.api.Persister;
29 import org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter;
30 import org.opendaylight.controller.netconf.persist.impl.osgi.ConfigPersisterActivator;
31 import org.opendaylight.controller.netconf.persist.impl.osgi.PropertiesProviderBaseImpl;
32
33 public class PersisterAggregatorTest {
34
35     static class TestingPropertiesProvider extends PropertiesProviderBaseImpl {
36
37         private final Properties prop;
38
39         public TestingPropertiesProvider(Properties prop) {
40             super(null);
41             this.prop = prop;
42         }
43
44         public static TestingPropertiesProvider loadFile(String fileName) {
45             Properties prop = new Properties();
46             try {
47                 prop.load(TestingPropertiesProvider.class.getClassLoader().getResourceAsStream(fileName));
48             } catch (IOException e) {
49                 throw new RuntimeException(e);
50             }
51             return new TestingPropertiesProvider(prop);
52         }
53
54         @Override
55         public String getFullKeyForReporting(String key) {
56             return ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER + "." + key;
57         }
58
59         @Override
60         public String getProperty(String key) {
61             return prop.getProperty(getFullKeyForReporting(key));
62         }
63
64         @Override
65         public String getPropertyWithoutPrefix(String fullKey){
66             return prop.getProperty(fullKey);
67         }
68     }
69
70     @Test
71     public void testDummyAdapter() throws Exception {
72         PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(TestingPropertiesProvider.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());
78
79         persisterAggregator.persistConfig(null);
80         persisterAggregator.loadLastConfigs();
81         persisterAggregator.persistConfig(null);
82         persisterAggregator.loadLastConfigs();
83
84         assertEquals(2, DummyAdapter.persist);
85         assertEquals(2, DummyAdapter.load);
86         assertEquals(1, DummyAdapter.props);
87     }
88
89     @Test
90     public void testNoopAdapter() throws Exception {
91         final NoOpStorageAdapter noOpStorageAdapter = new NoOpStorageAdapter();
92         final PersisterAggregator persisterAggregator =
93                 new PersisterAggregator(Lists.newArrayList(new PersisterWithConfiguration(noOpStorageAdapter, false)));
94
95         noOpStorageAdapter.instantiate(null);
96
97         persisterAggregator.persistConfig(null);
98         persisterAggregator.loadLastConfigs();
99         persisterAggregator.persistConfig(null);
100         persisterAggregator.loadLastConfigs();
101
102         noOpStorageAdapter.close();
103     }
104
105     @Test
106     public void testLoadFromPropertyFile() throws Exception {
107         PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(TestingPropertiesProvider.loadFile("test2.properties"));
108         List<PersisterWithConfiguration> persisters = persisterAggregator.getPersisterWithConfigurations();
109         assertEquals(1, persisters.size());
110         PersisterWithConfiguration persister = persisters.get(0);
111         assertEquals(XmlFileStorageAdapter.class.getName() ,persister.getStorage().getClass().getName());
112         assertFalse(persister.isReadOnly());
113     }
114
115     @Test
116     public void testFileStorageNumberOfBackups() throws Exception {
117         try {
118             PersisterAggregator.createFromProperties(TestingPropertiesProvider.loadFile("test3.properties"));
119             fail();
120         } catch (RuntimeException e) {
121             assertThat(
122                     e.getMessage(),
123                     containsString("numberOfBackups property should be either set to positive value, or ommited. Can not be set to 0."));
124         }
125     }
126
127     private ConfigSnapshotHolder mockHolder(String name){
128         ConfigSnapshotHolder result = mock(ConfigSnapshotHolder.class);
129         doReturn("mock:" + name).when(result).toString();
130         return result;
131     }
132
133     private Persister mockPersister(String name){
134         Persister result = mock(Persister.class);
135         doReturn("mock:" + name).when(result).toString();
136         return result;
137     }
138
139     @Test
140     public void loadLastConfig() throws Exception {
141         List<PersisterWithConfiguration> persisterWithConfigurations = new ArrayList<>();
142         PersisterWithConfiguration first = new PersisterWithConfiguration(mock(Persister.class), false);
143
144         ConfigSnapshotHolder ignored = mockHolder("ignored");
145         doReturn(Arrays.asList(ignored)).when(first.getStorage()).loadLastConfigs(); // should be ignored
146
147
148         ConfigSnapshotHolder used = mockHolder("used");
149         PersisterWithConfiguration second = new PersisterWithConfiguration(mockPersister("p1"), false);
150         doReturn(Arrays.asList(used)).when(second.getStorage()).loadLastConfigs(); // should be used
151
152         PersisterWithConfiguration third = new PersisterWithConfiguration(mockPersister("p2"), false);
153         doReturn(Arrays.asList()).when(third.getStorage()).loadLastConfigs();
154
155         persisterWithConfigurations.add(first);
156         persisterWithConfigurations.add(second);
157         persisterWithConfigurations.add(third);
158
159         PersisterAggregator persisterAggregator = new PersisterAggregator(persisterWithConfigurations);
160         List<ConfigSnapshotHolder> configSnapshotHolderOptional = persisterAggregator.loadLastConfigs();
161         assertEquals(1, configSnapshotHolderOptional.size());
162         assertEquals(used, configSnapshotHolderOptional.get(0));
163     }
164 }