Merge "Fixed major sonar warnings in Binding Aware Broker"
[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 static Properties prop = new Properties();
40
41         public TestingPropertiesProvider() {
42             super(null);
43         }
44
45         public static TestingPropertiesProvider loadFile(String fileName) {
46             try {
47                 prop.load(TestingPropertiesProvider.class.getClassLoader().getResourceAsStream(fileName));
48             } catch (IOException e) {
49                 throw new RuntimeException(e);
50             }
51             return new TestingPropertiesProvider();
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(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.loadLastConfig();
81         persisterAggregator.persistConfig(null);
82         persisterAggregator.loadLastConfig();
83
84         assertEquals(2, DummyAdapter.persist);
85         assertEquals(2, DummyAdapter.load);
86         assertEquals(1, DummyAdapter.props);
87     }
88
89     @Test
90     public void testLoadFromPropertyFile() throws Exception {
91         PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(loadFile("test2.properties"));
92         List<PersisterWithConfiguration> persisters = persisterAggregator.getPersisterWithConfigurations();
93         assertEquals(1, persisters.size());
94         PersisterWithConfiguration persister = persisters.get(0);
95         assertEquals(FileStorageAdapter.class.getName() ,persister.getStorage().getClass().getName());
96         assertFalse(persister.isReadOnly());
97     }
98
99     @Test
100     public void testFileStorageNumberOfBackups() throws Exception {
101         try {
102             PersisterAggregator.createFromProperties(loadFile("test3.properties"));
103             fail();
104         } catch (RuntimeException e) {
105             assertThat(
106                     e.getMessage(),
107                     containsString("numberOfBackups property should be either set to positive value, or ommited. Can not be set to 0."));
108         }
109     }
110
111     @Test
112     public void loadLastConfig() throws Exception {
113         List<PersisterWithConfiguration> persisterWithConfigurations = new ArrayList<>();
114         PersisterWithConfiguration first = new PersisterWithConfiguration(mock(Persister.class), false);
115
116         ConfigSnapshotHolder ignored = mock(ConfigSnapshotHolder.class);
117         doReturn(Optional.of(ignored)).when(first.getStorage()).loadLastConfig(); // should be ignored
118
119         ConfigSnapshotHolder used = mock(ConfigSnapshotHolder.class);
120         PersisterWithConfiguration second = new PersisterWithConfiguration(mock(Persister.class), false);
121         doReturn(Optional.of(used)).when(second.getStorage()).loadLastConfig(); // should be used
122
123         PersisterWithConfiguration third = new PersisterWithConfiguration(mock(Persister.class), false);
124         doReturn(Optional.absent()).when(third.getStorage()).loadLastConfig();
125
126         persisterWithConfigurations.add(first);
127         persisterWithConfigurations.add(second);
128         persisterWithConfigurations.add(third);
129
130         PersisterAggregator persisterAggregator = new PersisterAggregator(persisterWithConfigurations);
131         Optional<ConfigSnapshotHolder> configSnapshotHolderOptional = persisterAggregator.loadLastConfig();
132         assertTrue(configSnapshotHolderOptional.isPresent());
133         assertEquals(used, configSnapshotHolderOptional.get());
134     }
135
136 }