44b3b610434038aba7cfb68a20c763e97f2f7cff
[controller.git] / opendaylight / netconf / config-persister-impl / src / test / java / org / opendaylight / controller / netconf / persist / impl / PersisterImplTest.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 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;
20
21 import java.io.File;
22 import java.io.IOException;
23
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;
34
35 public class PersisterImplTest {
36     @Mock
37     BundleContext mockedContext;
38
39     @Before
40     public void setUpMocks() {
41         MockitoAnnotations.initMocks(this);
42     }
43
44     @Test
45     public void testFromProperties() throws Exception {
46         doReturn(MockAdapter.class.getName()).when(mockedContext).getProperty(
47                 PersisterImpl.STORAGE_ADAPTER_CLASS_PROP);
48
49         PersisterImpl persisterImpl = PersisterImpl.createFromProperties(mockedContext).get();
50         persisterImpl.persistConfig(null);
51         persisterImpl.loadLastConfig();
52         persisterImpl.persistConfig(null);
53         persisterImpl.loadLastConfig();
54
55         assertEquals(2, MockAdapter.persist);
56         assertEquals(2, MockAdapter.load);
57         assertEquals(1, MockAdapter.props);
58     }
59
60     @Test
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");
69
70         PersisterImpl persisterImpl = PersisterImpl.createFromProperties(mockedContext).get();
71         assertTrue(persisterImpl.getStorage() instanceof FileStorageAdapter);
72     }
73
74     @Test
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");
83         try {
84             PersisterImpl.createFromProperties(mockedContext).get();
85             fail();
86         } catch (RuntimeException e) {
87             assertThat(
88                     e.getMessage(),
89                     containsString("numberOfBackups property should be either set to positive value, or ommited. Can not be set to 0."));
90         }
91     }
92
93     @Test
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);
101
102         verify(storage).loadLastConfig();
103         verify(storage).persistConfig(any(Persister.ConfigSnapshotHolder.class));
104     }
105
106     public static class MockAdapter implements StorageAdapter {
107
108         static int persist = 0;
109
110         @Override
111         public void persistConfig(ConfigSnapshotHolder holder) throws IOException {
112             persist++;
113         }
114
115         static int load = 0;
116
117         @Override
118         public Optional<ConfigSnapshotHolder> loadLastConfig() throws IOException {
119             load++;
120             return null;// ?
121         }
122
123         static int props = 0;
124
125         @Override
126         public void setProperties(BundleContext configProvider) {
127             props++;
128         }
129
130         @Override
131         public void close() throws IOException {
132             // TODO Auto-generated method stub
133
134         }
135
136     }
137
138 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.