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