/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.controller.config.persist.storage.file.xml; import com.google.common.base.Charsets; import java.io.File; import java.nio.file.Files; import java.util.List; import java.util.SortedSet; import java.util.TreeSet; import junit.framework.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder; import org.opendaylight.controller.config.persist.test.PropertiesProviderTest; import static junit.framework.Assert.assertFalse; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; public class FileStorageAdapterTest { private static int i; private File file; @Before public void setUp() throws Exception { file = Files.createTempFile("testFilePersist", ".txt").toFile(); if (!file.exists()) return; com.google.common.io.Files.write("", file, Charsets.UTF_8); i = 1; } @Test public void testFileAdapter() throws Exception { XmlFileStorageAdapter storage = new XmlFileStorageAdapter(); PropertiesProviderTest pp = new PropertiesProviderTest(); pp.addProperty("fileStorage",file.getPath()); pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE)); storage.instantiate(pp); final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() { @Override public String getConfigSnapshot() { return createConfig(); } @Override public SortedSet getCapabilities() { return createCaps(); } }; storage.persistConfig(holder); storage.persistConfig(holder); assertEquals(27, com.google.common.io.Files.readLines(file, Charsets.UTF_8).size()); List lastConf = storage.loadLastConfigs(); assertEquals(1, lastConf.size()); ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0); assertEquals("2", configSnapshotHolder.getConfigSnapshot().replaceAll("\\s", "")); assertEquals(createCaps(), configSnapshotHolder.getCapabilities()); storage = new XmlFileStorageAdapter(); storage.setFileStorage(file); storage.setNumberOfBackups(Integer.MAX_VALUE); List last = storage.loadLastConfigs(); Assert.assertEquals(createCaps(), last.get(0).getCapabilities()); } private SortedSet createCaps() { SortedSet caps = new TreeSet<>(); caps.add("cap1" + i); caps.add("cap2" + i); caps.add("urn:opendaylight:params:xml:ns:yang:controller:netty?module=netty&revision=2013-11-19" + i); caps.add("capaaaa as dasfasdf s2" + i); return caps; } @Test public void testFileAdapterOneBackup() throws Exception { XmlFileStorageAdapter storage = new XmlFileStorageAdapter(); PropertiesProviderTest pp = new PropertiesProviderTest(); pp.addProperty("fileStorage",file.getPath()); pp.addProperty("numberOfBackups",Integer.toString(Integer.MAX_VALUE)); storage.instantiate(pp); final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() { @Override public String getConfigSnapshot() { return createConfig(); } @Override public SortedSet getCapabilities() { return createCaps(); } }; storage.persistConfig(holder); storage.persistConfig(holder); assertEquals(27, com.google.common.io.Files.readLines(file, Charsets.UTF_8).size()); List lastConf = storage.loadLastConfigs(); assertEquals(1, lastConf.size()); ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0); assertEquals("2", configSnapshotHolder.getConfigSnapshot().replaceAll("\\s", "")); } @Test public void testFileAdapterOnlyTwoBackups() throws Exception { XmlFileStorageAdapter storage = new XmlFileStorageAdapter(); storage.setFileStorage(file); storage.setNumberOfBackups(2); final ConfigSnapshotHolder holder = new ConfigSnapshotHolder() { @Override public String getConfigSnapshot() { return createConfig(); } @Override public SortedSet getCapabilities() { return createCaps(); } }; storage.persistConfig(holder); storage.persistConfig(holder); storage.persistConfig(holder); List readLines = com.google.common.io.Files.readLines(file, Charsets.UTF_8); assertEquals(27, readLines.size()); List lastConf = storage.loadLastConfigs(); assertEquals(1, lastConf.size()); ConfigSnapshotHolder configSnapshotHolder = lastConf.get(0); assertEquals("3", configSnapshotHolder.getConfigSnapshot().replaceAll("\\s", "")); assertFalse(readLines.contains(holder.getConfigSnapshot())); } @Test public void testNoLastConfig() throws Exception { File file = Files.createTempFile("testFilePersist", ".txt").toFile(); if (!file.exists()) return; XmlFileStorageAdapter storage = new XmlFileStorageAdapter(); storage.setFileStorage(file); List elementOptional = storage.loadLastConfigs(); assertThat(elementOptional.size(), is(0)); } @Test(expected = NullPointerException.class) public void testNoProperties() throws Exception { XmlFileStorageAdapter storage = new XmlFileStorageAdapter(); storage.loadLastConfigs(); } @Test(expected = NullPointerException.class) public void testNoProperties2() throws Exception { XmlFileStorageAdapter storage = new XmlFileStorageAdapter(); storage.persistConfig(new ConfigSnapshotHolder() { @Override public String getConfigSnapshot() { return Mockito.mock(String.class); } @Override public SortedSet getCapabilities() { return new TreeSet<>(); } } ); } static String createConfig() { return "" + i++ + ""; } }