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
8 package org.opendaylight.controller.config.persist.storage.directory.autodetect;
11 import java.io.IOException;
12 import java.util.List;
13 import junit.framework.Assert;
14 import org.junit.Test;
15 import org.junit.matchers.JUnitMatchers;
16 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
17 import org.opendaylight.controller.config.persist.test.PropertiesProviderTest;
18 import static org.junit.Assert.assertThat;
19 import static org.junit.Assert.fail;
21 public class AutodetectDirectoryPersisterTest {
24 public void testCombined() throws Exception {
25 File resourcePath = FileTypeTest.getResourceAsFile("/combined/1controller.txt.config");
26 File parentFile = resourcePath.getParentFile();
28 AutodetectDirectoryStorageAdapter adapter = new AutodetectDirectoryStorageAdapter();
30 PropertiesProviderTest pp = new PropertiesProviderTest();
31 pp.addProperty("directoryStorage",parentFile.getPath());
32 AutodetectDirectoryPersister persister = (AutodetectDirectoryPersister) adapter.instantiate(pp);
33 List<ConfigSnapshotHolder> configs = persister.loadLastConfigs();
35 Assert.assertEquals(2, configs.size());
36 String snapFromTxt = configs.get(0).getConfigSnapshot();
37 org.junit.Assert.assertThat(snapFromTxt, JUnitMatchers.containsString("<config>txt</config>"));
38 org.junit.Assert.assertThat(snapFromTxt, JUnitMatchers.containsString("<service>txt</service>"));
40 String snapFromXml = configs.get(1).getConfigSnapshot();
41 org.junit.Assert.assertThat(snapFromXml, JUnitMatchers.containsString("<config>xml</config>"));
43 Assert.assertEquals(configs.get(0).getCapabilities(), configs.get(1).getCapabilities());
47 public void testInvalidXml() throws Exception {
48 File resourcePath = FileTypeTest.getResourceAsFile("/bad_controller.xml.config");
49 File parentFile = resourcePath.getParentFile();
51 AutodetectDirectoryStorageAdapter adapter = new AutodetectDirectoryStorageAdapter();
53 PropertiesProviderTest pp = new PropertiesProviderTest();
54 pp.addProperty("directoryStorage",parentFile.getPath());
55 AutodetectDirectoryPersister persister = (AutodetectDirectoryPersister) adapter.instantiate(pp);
57 List<ConfigSnapshotHolder> configs = persister.loadLastConfigs();
58 fail("An exception of type " + IllegalStateException.class + " was expected");
59 } catch (IllegalStateException ise){
60 String message = ise.getMessage();
61 assertThat(message, JUnitMatchers.containsString("Unable to restore configuration snapshot from "));
67 public void testPersistConfig() throws Exception {
68 File resourcePath = FileTypeTest.getResourceAsFile("/combined/1controller.txt.config");
69 File parentFile = resourcePath.getParentFile();
71 AutodetectDirectoryStorageAdapter adapter = new AutodetectDirectoryStorageAdapter();
73 PropertiesProviderTest pp = new PropertiesProviderTest();
74 pp.addProperty("directoryStorage",parentFile.getPath());
75 AutodetectDirectoryPersister persister = (AutodetectDirectoryPersister) adapter.instantiate(pp);
76 List<ConfigSnapshotHolder> configs = null;
78 configs = persister.loadLastConfigs();
79 } catch (IOException e) {
80 fail("An exception of type " + UnsupportedOperationException.class + " was expected");
82 Assert.assertEquals(2, configs.size());
84 persister.persistConfig(configs.get(0));
85 } catch (UnsupportedOperationException uoe){
86 String message = uoe.getMessage();
87 assertThat(message,JUnitMatchers.containsString("This adapter is read only. Please set readonly=true on class"));