package uk.ac.ox.zoo.seeg.abraid.mp.common.config; import org.apache.commons.io.FileUtils; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import static org.assertj.core.api.Assertions.assertThat; /** * Tests the ConfigurationService class. * Copyright (c) 2014 University of Oxford */ public class ConfigurationServiceTest { @Rule public TemporaryFolder testFolder = new TemporaryFolder(); ///CHECKSTYLE:SUPPRESS VisibilityModifier private void writeStandardSimpleProperties(File testFile, String defaultRepoUrl, String defaultModelVersion) throws FileNotFoundException, UnsupportedEncodingException { PrintWriter writer = new PrintWriter(testFile, "UTF-8"); writer.println("model.repo.url = " + defaultRepoUrl); writer.println("model.repo.version = " + defaultModelVersion); writer.close(); } @Test public void setModelRepositoryUrlUpdatesFile() throws Exception { // Arrange File testFile = testFolder.newFile(); writeStandardSimpleProperties(testFile, "initialValue3", "initialValue4"); ConfigurationService target = new ConfigurationServiceImpl(testFile); String expectedUrl = "foo"; // Act target.setModelRepositoryUrl(expectedUrl); // Assert assertThat(FileUtils.readFileToString(testFile)).contains("model.repo.url = " + expectedUrl); } @Test public void setModelRepositoryVersionUpdatesFile() throws Exception { // Arrange File testFile = testFolder.newFile(); writeStandardSimpleProperties(testFile, "initialValue3", "initialValue4"); ConfigurationService target = new ConfigurationServiceImpl(testFile); String expectedVersion = "foo"; // Act target.setModelRepositoryVersion(expectedVersion); // Assert assertThat(FileUtils.readFileToString(testFile)).contains("model.repo.version = " + expectedVersion); } @Test public void getModelRepositoryUrlReturnsCorrectValue() throws Exception { // Arrange File testFile = testFolder.newFile(); String expectedUrl = "foo"; writeStandardSimpleProperties(testFile, expectedUrl, "initialValue4"); ConfigurationService target = new ConfigurationServiceImpl(testFile); // Act String result = target.getModelRepositoryUrl(); // Assert assertThat(result).isEqualTo(expectedUrl); } @Test public void getModelRepositoryVersionReturnsCorrectValue() throws Exception { // Arrange File testFile = testFolder.newFile(); String expectedVersion = "foo"; writeStandardSimpleProperties(testFile, "initialValue3", expectedVersion); ConfigurationService target = new ConfigurationServiceImpl(testFile); // Act String result = target.getModelRepositoryVersion(); // Assert assertThat(result).isEqualTo(expectedVersion); } }