2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.persist.impl;
11 import com.google.common.base.Optional;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.mockito.Mock;
15 import org.mockito.MockitoAnnotations;
16 import org.opendaylight.controller.config.persist.api.Persister;
17 import org.opendaylight.controller.config.persist.api.storage.StorageAdapter;
18 import org.opendaylight.controller.config.persist.api.storage.StorageAdapter.PropertiesProvider;
19 import org.opendaylight.controller.config.persist.storage.file.FileStorageAdapter;
20 import org.opendaylight.controller.netconf.persist.impl.osgi.ConfigPersisterActivator;
23 import java.io.IOException;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertThat;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29 import static org.junit.matchers.JUnitMatchers.containsString;
30 import static org.mockito.Matchers.any;
31 import static org.mockito.Matchers.anyString;
32 import static org.mockito.Mockito.doCallRealMethod;
33 import static org.mockito.Mockito.doNothing;
34 import static org.mockito.Mockito.doReturn;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.verify;
38 public class PersisterImplTest {
40 TestingPropertiesProvider propertiesProvider;
42 class TestingPropertiesProvider implements PropertiesProvider {
44 public String getFullKeyForReporting(String key) {
45 return "prefix" + key;
49 public String getProperty(String key) {
50 throw new UnsupportedOperationException("should be mocked");
55 public void setUpMocks() {
56 MockitoAnnotations.initMocks(this);
57 doCallRealMethod().when(propertiesProvider).getFullKeyForReporting(anyString());
61 public void testFromProperties() throws Exception {
62 doReturn(MockAdapter.class.getName()).when(propertiesProvider).getProperty(
63 ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX);
65 PersisterImpl persisterImpl = PersisterImpl.createFromProperties(propertiesProvider);
66 persisterImpl.persistConfig(null);
67 persisterImpl.loadLastConfig();
68 persisterImpl.persistConfig(null);
69 persisterImpl.loadLastConfig();
71 assertEquals(2, MockAdapter.persist);
72 assertEquals(2, MockAdapter.load);
73 assertEquals(1, MockAdapter.props);
78 public void testFromProperties2() throws Exception {
80 doReturn(FileStorageAdapter.class.getName()).when(propertiesProvider).getProperty(
81 ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX);
83 doReturn("target" + File.separator + "generated-test-sources" + File.separator + "testFile").when(
84 propertiesProvider).getProperty(FileStorageAdapter.FILE_STORAGE_PROP);
85 doReturn("propertiesProvider").when(propertiesProvider).toString();
86 doReturn(null).when(propertiesProvider).getProperty("numberOfBackups");
88 PersisterImpl persisterImpl = PersisterImpl.createFromProperties(propertiesProvider);
89 assertTrue(persisterImpl.getStorage() instanceof FileStorageAdapter);
93 public void testFromProperties3() throws Exception {
95 doReturn(FileStorageAdapter.class.getName()).when(propertiesProvider).getProperty(
96 ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX);
97 doReturn("target" + File.separator + "generated-test-sources" + File.separator + "testFile").when(
98 propertiesProvider).getProperty(FileStorageAdapter.FILE_STORAGE_PROP);
99 doReturn("propertiesProvider").when(propertiesProvider).toString();
100 doReturn("0").when(propertiesProvider).getProperty("numberOfBackups");
102 PersisterImpl.createFromProperties(propertiesProvider);
104 } catch (RuntimeException e) {
107 containsString("numberOfBackups property should be either set to positive value, or ommited. Can not be set to 0."));
112 public void test() throws Exception {
113 StorageAdapter storage = mock(StorageAdapter.class);
114 doReturn(null).when(storage).loadLastConfig();
115 doNothing().when(storage).persistConfig(any(Persister.ConfigSnapshotHolder.class));
116 PersisterImpl persister = new PersisterImpl(storage);
117 persister.loadLastConfig();
118 persister.persistConfig(null);
120 verify(storage).loadLastConfig();
121 verify(storage).persistConfig(any(Persister.ConfigSnapshotHolder.class));
124 public static class MockAdapter implements StorageAdapter {
126 static int persist = 0;
129 public void persistConfig(ConfigSnapshotHolder holder) throws IOException {
136 public Optional<ConfigSnapshotHolder> loadLastConfig() throws IOException {
141 static int props = 0;
144 public void setProperties(PropertiesProvider propertiesProvider) {
149 public void close() throws IOException {
150 // TODO Auto-generated method stub