d9fa7ba4d82dbfa931106d93389d83837f105daf
[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.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;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Properties;
23
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;
34
35 public class PersisterAggregatorTest {
36
37     static class TestingPropertiesProvider extends PropertiesProviderBaseImpl {
38
39         private final Properties prop;
40
41         public TestingPropertiesProvider(Properties prop) {
42             super(null);
43             this.prop = prop;
44         }
45
46         public static TestingPropertiesProvider loadFile(String fileName) {
47             Properties prop = new Properties();
48             try {
49                 prop.load(TestingPropertiesProvider.class.getClassLoader().getResourceAsStream(fileName));
50             } catch (IOException e) {
51                 throw new RuntimeException(e);
52             }
53             return new TestingPropertiesProvider(prop);
54         }
55
56         @Override
57         public String getFullKeyForReporting(String key) {
58             return ConfigPersisterActivator.NETCONF_CONFIG_PERSISTER + "." + key;
59         }
60
61         @Override
62         public String getProperty(String key) {
63             return prop.getProperty(getFullKeyForReporting(key));
64         }
65
66         @Override
67         public String getPropertyWithoutPrefix(String fullKey){
68             return prop.getProperty(fullKey);
69         }
70     }
71
72     @Test
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());
80
81         persisterAggregator.persistConfig(null);
82         persisterAggregator.loadLastConfig();
83         persisterAggregator.persistConfig(null);
84         persisterAggregator.loadLastConfig();
85
86         assertEquals(2, DummyAdapter.persist);
87         assertEquals(2, DummyAdapter.load);
88         assertEquals(1, DummyAdapter.props);
89     }
90
91     @Test
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());
99     }
100
101     @Test
102     public void testFileStorageNumberOfBackups() throws Exception {
103         try {
104             PersisterAggregator.createFromProperties(loadFile("test3.properties"));
105             fail();
106         } catch (RuntimeException e) {
107             assertThat(
108                     e.getMessage(),
109                     containsString("numberOfBackups property should be either set to positive value, or ommited. Can not be set to 0."));
110         }
111     }
112
113     @Test
114     public void loadLastConfig() throws Exception {
115         List<PersisterWithConfiguration> persisterWithConfigurations = new ArrayList<>();
116         PersisterWithConfiguration first = new PersisterWithConfiguration(mock(Persister.class), false);
117
118         ConfigSnapshotHolder ignored = mock(ConfigSnapshotHolder.class);
119         doReturn(Optional.of(ignored)).when(first.getStorage()).loadLastConfig(); // should be ignored
120
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
124
125         PersisterWithConfiguration third = new PersisterWithConfiguration(mock(Persister.class), false);
126         doReturn(Optional.absent()).when(third.getStorage()).loadLastConfig();
127
128         persisterWithConfigurations.add(first);
129         persisterWithConfigurations.add(second);
130         persisterWithConfigurations.add(third);
131
132         PersisterAggregator persisterAggregator = new PersisterAggregator(persisterWithConfigurations);
133         Optional<ConfigSnapshotHolder> configSnapshotHolderOptional = persisterAggregator.loadLastConfig();
134         assertTrue(configSnapshotHolderOptional.isPresent());
135         assertEquals(used, configSnapshotHolderOptional.get());
136     }
137
138 }