Merge "Increase timeout for waiting for broker service in sal-binding-it."
[controller.git] / opendaylight / config / config-persister-directory-autodetect-adapter / src / test / java / org / opendaylight / controller / config / persist / storage / directory / autodetect / AutodetectDirectoryPersisterTest.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 package org.opendaylight.controller.config.persist.storage.directory.autodetect;
9
10 import java.io.File;
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;
20
21 public class AutodetectDirectoryPersisterTest {
22
23     @Test
24     public void testCombined() throws Exception {
25         File resourcePath = FileTypeTest.getResourceAsFile("/combined/1controller.txt.config");
26         File parentFile = resourcePath.getParentFile();
27
28         AutodetectDirectoryStorageAdapter adapter = new AutodetectDirectoryStorageAdapter();
29
30         PropertiesProviderTest pp = new PropertiesProviderTest();
31         pp.addProperty("directoryStorage",parentFile.getPath());
32         AutodetectDirectoryPersister persister = (AutodetectDirectoryPersister) adapter.instantiate(pp);
33         List<ConfigSnapshotHolder> configs = persister.loadLastConfigs();
34
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>"));
39
40         String snapFromXml = configs.get(1).getConfigSnapshot();
41         org.junit.Assert.assertThat(snapFromXml, JUnitMatchers.containsString("<config>xml</config>"));
42
43         Assert.assertEquals(configs.get(0).getCapabilities(), configs.get(1).getCapabilities());
44     }
45
46     @Test
47     public void testInvalidXml() throws Exception {
48         File resourcePath = FileTypeTest.getResourceAsFile("/bad_controller.xml.config");
49         File parentFile = resourcePath.getParentFile();
50
51         AutodetectDirectoryStorageAdapter adapter = new AutodetectDirectoryStorageAdapter();
52
53         PropertiesProviderTest pp = new PropertiesProviderTest();
54         pp.addProperty("directoryStorage",parentFile.getPath());
55         AutodetectDirectoryPersister persister = (AutodetectDirectoryPersister) adapter.instantiate(pp);
56         try {
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 "));
62         }
63
64     }
65
66     @Test
67     public void testPersistConfig() throws Exception {
68         File resourcePath = FileTypeTest.getResourceAsFile("/combined/1controller.txt.config");
69         File parentFile = resourcePath.getParentFile();
70
71         AutodetectDirectoryStorageAdapter adapter = new AutodetectDirectoryStorageAdapter();
72
73         PropertiesProviderTest pp = new PropertiesProviderTest();
74         pp.addProperty("directoryStorage",parentFile.getPath());
75         AutodetectDirectoryPersister persister = (AutodetectDirectoryPersister) adapter.instantiate(pp);
76         List<ConfigSnapshotHolder> configs = null;
77         try {
78             configs = persister.loadLastConfigs();
79         } catch (IOException e) {
80             fail("An exception of type " + UnsupportedOperationException.class + " was expected");
81         }
82         Assert.assertEquals(2, configs.size());
83         try {
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"));
88         }
89     }
90
91 }