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