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.storage.file.FileStorageAdapter;
19 import org.osgi.framework.BundleContext;
22 import java.io.IOException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28 import static org.junit.matchers.JUnitMatchers.containsString;
29 import static org.mockito.Matchers.any;
30 import static org.mockito.Mockito.doNothing;
31 import static org.mockito.Mockito.doReturn;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.verify;
35 public class PersisterImplTest {
37 BundleContext mockedContext;
40 public void setUpMocks() {
41 MockitoAnnotations.initMocks(this);
45 public void testFromProperties() throws Exception {
46 doReturn(MockAdapter.class.getName()).when(mockedContext).getProperty(
47 PersisterImpl.STORAGE_ADAPTER_CLASS_PROP);
49 PersisterImpl persisterImpl = PersisterImpl.createFromProperties(mockedContext).get();
50 persisterImpl.persistConfig(null);
51 persisterImpl.loadLastConfig();
52 persisterImpl.persistConfig(null);
53 persisterImpl.loadLastConfig();
55 assertEquals(2, MockAdapter.persist);
56 assertEquals(2, MockAdapter.load);
57 assertEquals(1, MockAdapter.props);
61 public void testFromProperties2() throws Exception {
62 mockedContext = mock(BundleContext.class);
63 doReturn(FileStorageAdapter.class.getName()).when(mockedContext).getProperty(
64 PersisterImpl.STORAGE_ADAPTER_CLASS_PROP);
65 doReturn("target" + File.separator + "generated-test-sources" + File.separator + "testFile").when(
66 mockedContext).getProperty(FileStorageAdapter.FILE_STORAGE_PROP);
67 doReturn("mockedContext").when(mockedContext).toString();
68 doReturn(null).when(mockedContext).getProperty("numberOfBackups");
70 PersisterImpl persisterImpl = PersisterImpl.createFromProperties(mockedContext).get();
71 assertTrue(persisterImpl.getStorage() instanceof FileStorageAdapter);
75 public void testFromProperties3() throws Exception {
76 mockedContext = mock(BundleContext.class);
77 doReturn(FileStorageAdapter.class.getName()).when(mockedContext).getProperty(
78 PersisterImpl.STORAGE_ADAPTER_CLASS_PROP);
79 doReturn("target" + File.separator + "generated-test-sources" + File.separator + "testFile").when(
80 mockedContext).getProperty(FileStorageAdapter.FILE_STORAGE_PROP);
81 doReturn("mockedContext").when(mockedContext).toString();
82 doReturn("0").when(mockedContext).getProperty("numberOfBackups");
84 PersisterImpl.createFromProperties(mockedContext).get();
86 } catch (RuntimeException e) {
89 containsString("numberOfBackups property should be either set to positive value, or ommited. Can not be set to 0."));
94 public void test() throws Exception {
95 StorageAdapter storage = mock(StorageAdapter.class);
96 doReturn(null).when(storage).loadLastConfig();
97 doNothing().when(storage).persistConfig(any(Persister.ConfigSnapshotHolder.class));
98 PersisterImpl persister = new PersisterImpl(storage);
99 persister.loadLastConfig();
100 persister.persistConfig(null);
102 verify(storage).loadLastConfig();
103 verify(storage).persistConfig(any(Persister.ConfigSnapshotHolder.class));
106 public static class MockAdapter implements StorageAdapter {
108 static int persist = 0;
111 public void persistConfig(ConfigSnapshotHolder holder) throws IOException {
118 public Optional<ConfigSnapshotHolder> loadLastConfig() throws IOException {
123 static int props = 0;
126 public void setProperties(BundleContext configProvider) {
131 public void close() throws IOException {
132 // TODO Auto-generated method stub