CDS: Add stress test RPC to the cars model
[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.Before;
27 import org.junit.Test;
28 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
29 import org.opendaylight.controller.config.persist.api.Persister;
30 import org.opendaylight.controller.config.persist.storage.file.xml.XmlFileStorageAdapter;
31 import org.opendaylight.controller.netconf.persist.impl.osgi.ConfigPersisterActivator;
32 import org.opendaylight.controller.netconf.persist.impl.osgi.PropertiesProviderBaseImpl;
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     @Before
72     public void setUp() throws Exception {
73         if(XmlFileStorageAdapter.getInstance().isPresent()) {
74             XmlFileStorageAdapter.getInstance().get().reset();
75         }
76     }
77
78     @Test
79     public void testDummyAdapter() throws Exception {
80         PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(TestingPropertiesProvider.loadFile("test1.properties"));
81         List<PersisterWithConfiguration> persisters = persisterAggregator.getPersisterWithConfigurations();
82         assertEquals(1, persisters.size());
83         PersisterWithConfiguration persister = persisters.get(0);
84         assertEquals(DummyAdapter.class.getName(), persister.getStorage().getClass().getName());
85         assertFalse(persister.isReadOnly());
86
87         persisterAggregator.persistConfig(null);
88         persisterAggregator.loadLastConfigs();
89         persisterAggregator.persistConfig(null);
90         persisterAggregator.loadLastConfigs();
91
92         assertEquals(2, DummyAdapter.persist);
93         assertEquals(2, DummyAdapter.load);
94         assertEquals(1, DummyAdapter.props);
95     }
96
97     @Test
98     public void testNoopAdapter() throws Exception {
99         final NoOpStorageAdapter noOpStorageAdapter = new NoOpStorageAdapter();
100         final PersisterAggregator persisterAggregator =
101                 new PersisterAggregator(Lists.newArrayList(new PersisterWithConfiguration(noOpStorageAdapter, false)));
102
103         noOpStorageAdapter.instantiate(null);
104
105         persisterAggregator.persistConfig(null);
106         persisterAggregator.loadLastConfigs();
107         persisterAggregator.persistConfig(null);
108         persisterAggregator.loadLastConfigs();
109
110         noOpStorageAdapter.close();
111     }
112
113     @Test
114     public void testLoadFromPropertyFile() throws Exception {
115         PersisterAggregator persisterAggregator = PersisterAggregator.createFromProperties(TestingPropertiesProvider.loadFile("test2.properties"));
116         List<PersisterWithConfiguration> persisters = persisterAggregator.getPersisterWithConfigurations();
117         assertEquals(1, persisters.size());
118         PersisterWithConfiguration persister = persisters.get(0);
119         assertEquals(XmlFileStorageAdapter.class.getName() ,persister.getStorage().getClass().getName());
120         assertFalse(persister.isReadOnly());
121     }
122
123     @Test
124     public void testFileStorageNumberOfBackups() throws Exception {
125         try {
126             PersisterAggregator.createFromProperties(TestingPropertiesProvider.loadFile("test3.properties"));
127             fail();
128         } catch (RuntimeException e) {
129             assertThat(
130                     e.getMessage(),
131                     containsString("numberOfBackups property should be either set to positive value, or ommited. Can not be set to 0."));
132         }
133     }
134
135     private ConfigSnapshotHolder mockHolder(String name){
136         ConfigSnapshotHolder result = mock(ConfigSnapshotHolder.class);
137         doReturn("mock:" + name).when(result).toString();
138         return result;
139     }
140
141     private Persister mockPersister(String name){
142         Persister result = mock(Persister.class);
143         doReturn("mock:" + name).when(result).toString();
144         return result;
145     }
146
147     @Test
148     public void loadLastConfig() throws Exception {
149         List<PersisterWithConfiguration> persisterWithConfigurations = new ArrayList<>();
150         PersisterWithConfiguration first = new PersisterWithConfiguration(mock(Persister.class), false);
151
152         ConfigSnapshotHolder ignored = mockHolder("ignored");
153         doReturn(Arrays.asList(ignored)).when(first.getStorage()).loadLastConfigs(); // should be ignored
154
155
156         ConfigSnapshotHolder used = mockHolder("used");
157         PersisterWithConfiguration second = new PersisterWithConfiguration(mockPersister("p1"), false);
158         doReturn(Arrays.asList(used)).when(second.getStorage()).loadLastConfigs(); // should be used
159
160         PersisterWithConfiguration third = new PersisterWithConfiguration(mockPersister("p2"), false);
161         doReturn(Arrays.asList()).when(third.getStorage()).loadLastConfigs();
162
163         persisterWithConfigurations.add(first);
164         persisterWithConfigurations.add(second);
165         persisterWithConfigurations.add(third);
166
167         PersisterAggregator persisterAggregator = new PersisterAggregator(persisterWithConfigurations);
168         List<ConfigSnapshotHolder> configSnapshotHolderOptional = persisterAggregator.loadLastConfigs();
169         assertEquals(1, configSnapshotHolderOptional.size());
170         assertEquals(used, configSnapshotHolderOptional.get(0));
171     }
172 }