de.elomagic.mag.ConfigurationTest.java Source code

Java tutorial

Introduction

Here is the source code for de.elomagic.mag.ConfigurationTest.java

Source

/*
 * Mail Attachment Gateway
 * Copyright (c) 2016-2017 Carsten Rambow
 * mailto:developer AT elomagic DOT de
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.elomagic.mag;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

import com.google.common.io.Files;

import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import de.elomagic.mag.Configuration.ConfigurationKey;
import de.elomagic.mag.utils.SimpleCrypt;

public class ConfigurationTest {

    private static File file;

    @BeforeClass
    public static void beforeClassConfigurationTest() throws Exception {
        file = File.createTempFile("test_", ".properties");
        file.deleteOnExit();
    }

    @Before
    public void beforeConfigurationTest() throws Exception {
        String text = IOUtils.toString(getClass().getResourceAsStream("/configuration.properties"), "utf-8");
        Files.write(text, file, StandardCharsets.UTF_8);
    }

    @Test
    public void testEncryption() throws Exception {
        Configuration.loadConfiguration(file.toPath());
        Assert.assertEquals("secret", Configuration.get(ConfigurationKey.MailReceivePassword));

        Configuration.encryptConfiguration(file.toPath());

        Properties p = new Properties();
        p.load(Files.newReader(file, StandardCharsets.UTF_8));

        String value = p.getProperty(ConfigurationKey.MailReceivePassword.getKey());

        Assert.assertTrue(SimpleCrypt.isEncryptedValue(value));
        Assert.assertEquals("secret", SimpleCrypt.decrypt(value));
    }

}