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