Refactor persister.
[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.api.storage.StorageAdapter.PropertiesProvider;
19 import org.opendaylight.controller.config.persist.storage.file.FileStorageAdapter;
20 import org.opendaylight.controller.netconf.persist.impl.osgi.ConfigPersisterActivator;
21
22 import java.io.File;
23 import java.io.IOException;
24
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;
37
38 public class PersisterImplTest {
39     @Mock
40     TestingPropertiesProvider propertiesProvider;
41
42     class TestingPropertiesProvider implements PropertiesProvider {
43         @Override
44         public String getFullKeyForReporting(String key) {
45             return "prefix" + key;
46         }
47
48         @Override
49         public String getProperty(String key) {
50             throw new UnsupportedOperationException("should be mocked");
51         }
52     }
53
54     @Before
55     public void setUpMocks() {
56         MockitoAnnotations.initMocks(this);
57         doCallRealMethod().when(propertiesProvider).getFullKeyForReporting(anyString());
58     }
59
60     @Test
61     public void testFromProperties() throws Exception {
62         doReturn(MockAdapter.class.getName()).when(propertiesProvider).getProperty(
63                 ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX);
64
65         PersisterImpl persisterImpl = PersisterImpl.createFromProperties(propertiesProvider);
66         persisterImpl.persistConfig(null);
67         persisterImpl.loadLastConfig();
68         persisterImpl.persistConfig(null);
69         persisterImpl.loadLastConfig();
70
71         assertEquals(2, MockAdapter.persist);
72         assertEquals(2, MockAdapter.load);
73         assertEquals(1, MockAdapter.props);
74     }
75
76
77     @Test
78     public void testFromProperties2() throws Exception {
79
80         doReturn(FileStorageAdapter.class.getName()).when(propertiesProvider).getProperty(
81                 ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX);
82
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");
87
88         PersisterImpl persisterImpl = PersisterImpl.createFromProperties(propertiesProvider);
89         assertTrue(persisterImpl.getStorage() instanceof FileStorageAdapter);
90     }
91
92     @Test
93     public void testFromProperties3() throws Exception {
94
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");
101         try {
102             PersisterImpl.createFromProperties(propertiesProvider);
103             fail();
104         } catch (RuntimeException e) {
105             assertThat(
106                     e.getMessage(),
107                     containsString("numberOfBackups property should be either set to positive value, or ommited. Can not be set to 0."));
108         }
109     }
110
111     @Test
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);
119
120         verify(storage).loadLastConfig();
121         verify(storage).persistConfig(any(Persister.ConfigSnapshotHolder.class));
122     }
123
124     public static class MockAdapter implements StorageAdapter {
125
126         static int persist = 0;
127
128         @Override
129         public void persistConfig(ConfigSnapshotHolder holder) throws IOException {
130             persist++;
131         }
132
133         static int load = 0;
134
135         @Override
136         public Optional<ConfigSnapshotHolder> loadLastConfig() throws IOException {
137             load++;
138             return null;// ?
139         }
140
141         static int props = 0;
142
143         @Override
144         public void setProperties(PropertiesProvider propertiesProvider) {
145             props++;
146         }
147
148         @Override
149         public void close() throws IOException {
150             // TODO Auto-generated method stub
151
152         }
153
154     }
155
156 }

©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.