Search in sources :

Example 1 with ConfigLocation

use of org.motechproject.config.core.domain.ConfigLocation in project motech by motech.

the class ConfigPropertiesUtilsTest method shouldThrowExceptionIfNoneOfTheFilesInTheDefaultLocationIsReadable.

@Test(expected = MotechConfigurationException.class)
public void shouldThrowExceptionIfNoneOfTheFilesInTheDefaultLocationIsReadable() {
    List<ConfigLocation> configLocationList = new ArrayList<>();
    ConfigLocation configLocationMock = Mockito.mock(ConfigLocation.class);
    configLocationList.add(configLocationMock);
    when(configLocationMock.getLocation()).thenReturn("location");
    when(configLocationMock.getFile("fileName", ConfigLocation.FileAccessType.READABLE)).thenThrow(new MotechConfigurationException("File is not readable"));
    when(configLocationMock.getLocation()).thenReturn("location");
    ConfigPropertiesUtils.getDefaultPropertiesFile(ConfigLocation.FileAccessType.READABLE, configLocationList, "fileName");
}
Also used : ConfigLocation(org.motechproject.config.core.domain.ConfigLocation) ArrayList(java.util.ArrayList) MotechConfigurationException(org.motechproject.config.core.exception.MotechConfigurationException) Test(org.junit.Test)

Example 2 with ConfigLocation

use of org.motechproject.config.core.domain.ConfigLocation in project motech by motech.

the class ConfigPropertiesUtilsTest method getFile.

private File getFile() {
    String tempDir = new File(System.getProperty("java.io.tmpdir"), "config").getAbsolutePath();
    List<ConfigLocation> configLocationList = new ArrayList<>();
    configLocationList.add(new ConfigLocation(tempDir));
    File file = new File(tempDir, BootstrapManager.BOOTSTRAP_PROPERTIES);
    if (file.exists()) {
        file.delete();
    }
    return file;
}
Also used : ConfigLocation(org.motechproject.config.core.domain.ConfigLocation) ArrayList(java.util.ArrayList) File(java.io.File)

Example 3 with ConfigLocation

use of org.motechproject.config.core.domain.ConfigLocation in project motech by motech.

the class CoreConfigurationServiceImpl method getConfigLocation.

@Override
public ConfigLocation getConfigLocation() {
    Iterable<ConfigLocation> locations = configLocationFileStore.getAll();
    StringBuilder sb = new StringBuilder("");
    for (ConfigLocation configLocation : locations) {
        sb.append(configLocation.getLocation()).append(' ');
        Resource configLocationResource = configLocation.toResource();
        try {
            Resource motechSettings = configLocationResource.createRelative(ConfigurationConstants.SETTINGS_FILE_NAME);
            if (motechSettings.isReadable() && locations != null) {
                return configLocation;
            }
            LOGGER.warn("Could not read motech-settings.properties from: " + configLocationResource.toString());
        } catch (IOException e) {
            LOGGER.warn("Problem reading motech-settings.properties from location: " + configLocationResource.toString(), e);
        }
    }
    throw new MotechConfigurationException(String.format("Could not read settings from any of the config locations. Searched directories: %s.", sb));
}
Also used : ConfigLocation(org.motechproject.config.core.domain.ConfigLocation) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) MotechConfigurationException(org.motechproject.config.core.exception.MotechConfigurationException)

Example 4 with ConfigLocation

use of org.motechproject.config.core.domain.ConfigLocation in project motech by motech.

the class BootstrapManagerTest method shouldLoadPropertiesInTheCorrectOrder.

@PrepareForTest(ConfigPropertiesUtils.class)
@Test
public void shouldLoadPropertiesInTheCorrectOrder() throws IOException {
    PowerMockito.mockStatic(ConfigPropertiesUtils.class);
    Iterable<ConfigLocation> configLocations = new ArrayList<ConfigLocation>();
    Properties properties = createProperties();
    properties.put(SQL_URL, "");
    when(environment.getConfigDir()).thenReturn(null);
    when(environment.getBootstrapProperties()).thenReturn(properties);
    when(configLocationFileStore.getAll()).thenReturn(configLocations);
    when(ConfigPropertiesUtils.getDefaultPropertiesFile(ConfigLocation.FileAccessType.READABLE, configLocations, BootstrapManager.BOOTSTRAP_PROPERTIES)).thenThrow(new MotechConfigurationException("Error loading file from config locations"));
    try {
        bootstrapManager.loadBootstrapConfig();
    } catch (MotechConfigurationException e) {
    // Ignore error because invocation order is to be verified.
    }
    InOrder inOrder = Mockito.inOrder(environment, configLocationFileStore);
    inOrder.verify(environment).getConfigDir();
    inOrder.verify(environment).getBootstrapProperties();
    inOrder.verify(configLocationFileStore).getAll();
}
Also used : InOrder(org.mockito.InOrder) ConfigLocation(org.motechproject.config.core.domain.ConfigLocation) ArrayList(java.util.ArrayList) MotechConfigurationException(org.motechproject.config.core.exception.MotechConfigurationException) Properties(java.util.Properties) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with ConfigLocation

use of org.motechproject.config.core.domain.ConfigLocation in project motech by motech.

the class DbConfigManagerTest method shouldLoadPropertiesFromDefaultConfigLocation.

private void shouldLoadPropertiesFromDefaultConfigLocation(String filename) throws IOException {
    when(environment.getDatanucleusDataProperties()).thenReturn(new Properties());
    when(environment.getConfigDir()).thenReturn("");
    tempDir = Files.createTempDir();
    File file = new File(tempDir, filename);
    List<ConfigLocation> configLocationList = new ArrayList<>();
    Properties properties = getCompleteProperties(filename);
    try (FileOutputStream os = new FileOutputStream(file)) {
        properties.store(os, null);
    }
    configLocationList.add(new ConfigLocation(tempDir.getAbsolutePath()));
    when(configLocationFileStore.getAll()).thenReturn(configLocationList);
    properties = useDbManagerToGetCorrectFile(filename);
    assertEquals(getCompleteProperties(filename), properties);
}
Also used : ConfigLocation(org.motechproject.config.core.domain.ConfigLocation) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) Properties(java.util.Properties) File(java.io.File)

Aggregations

ConfigLocation (org.motechproject.config.core.domain.ConfigLocation)24 Test (org.junit.Test)17 File (java.io.File)9 ArrayList (java.util.ArrayList)7 Properties (java.util.Properties)4 MotechConfigurationException (org.motechproject.config.core.exception.MotechConfigurationException)4 InOrder (org.mockito.InOrder)3 BootstrapConfig (org.motechproject.config.core.domain.BootstrapConfig)3 SQLDBConfig (org.motechproject.config.core.domain.SQLDBConfig)3 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 FileObject (org.apache.commons.vfs2.FileObject)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Resource (org.springframework.core.io.Resource)2 FileOutputStream (java.io.FileOutputStream)1 MalformedURLException (java.net.MalformedURLException)1 Path (java.nio.file.Path)1 PropertiesConfiguration (org.apache.commons.configuration.PropertiesConfiguration)1 MotechSettings (org.motechproject.config.domain.MotechSettings)1 SettingsRecord (org.motechproject.config.domain.SettingsRecord)1