/*
 * Copyright 2012 s_wolff.
 *
 * 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 org.aegee.runanddine.mail;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.aegee.runanddine.mail.Mail;
import org.aegee.runanddine.mail.MailRenderContext;
import org.aegee.runanddine.mail.RenderException;
import org.junit.*;

/**
 *
 * @author s_wolff
 */
public class MailRenderContextTest {

    MailRenderContext instance;
    Map<String, String> map;

    public MailRenderContextTest() {
        map = new HashMap<String, String>(10);
        map.put("test_eins", "result1");
        map.put("test_zwei", "result2");
        map.put("test_drei", "result3");
        map.put("test_vier", "result4");
        map.put("test_funf", "result5");
        map.put("test_sechs", "result6");
        this.instance = new MailRenderContext(map) {
        };
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    /**
     * Test of renderKey method, of class MailRenderContext.
     */
    @Test
    public void testRenderKey() {
        System.out.println(" - renderKey");
        for (String key : map.keySet()) {
            assert instance.renderKey(key).equals(map.get(key));
        }
    }

    /**
     * Test of getKeys method, of class MailRenderContext.
     */
    @Test
    public void testGetKeys() {
        System.out.println(" - getKeys");
        Collection<String> expResult = map.keySet();
        Collection<String> result = instance.getKeys();
        assert expResult.size() == result.size();
        // contains with ignore case
        for (String s : expResult) {
            assert result.contains(s.toUpperCase()) || result.contains(s.toLowerCase());
        }
        for (String s : result) {
            assert expResult.contains(s.toLowerCase()) || expResult.contains(s.toUpperCase());
        }
    }

    /**
     * Test of render method, of class MailRenderContext.
     */
    @Test
    public void testRender_String() {
        System.out.println(" - render");
        String test = "lore ipsum {{%s}} dolor sit amet consectetur";
        for (String key : map.keySet()) {
            String input = String.format(test, key);
            String output = instance.render(input);
            String expOutput = String.format(test, map.get(key)).replace("{{", "").replace("}}", "");
            assertEquals(expOutput, output);
        }
        assert true;
    }

    /**
     * Test of render method, of class MailRenderContext.
     */
    @Test
    public void testRender_String2() {
        System.out.println(" - render (failure)");
        String test = "lore ipsum {{ %s}} dolor sit amet consectetur";
        try {
            for (String key : map.keySet()) {
                String input = String.format(test, key);
                instance.render(input);
                fail("input is malformed -> should throw exception");
            }
        } catch (RenderException e) {
            assert true;
        }
    }

    /**
     * Test of render method, of class MailRenderContext.
     */
    @Test
    public void testRender_Mail_boolean() {
        System.out.println(" - render");

        String subject = "subject {{test_eins}}";
        String content = "content {{test_zwei}}";
        Mail mail = new Mail(subject, "", "", content);
        Mail result = instance.render(mail, true);

        assertEquals(result.getContent(), "content result2");
        assertEquals(result.getSubject(), "subject result1");

        result = instance.render(mail, false);

        assertEquals(result.getContent(), "content result2");
        assertEquals(result.getSubject(), "subject {{test_eins}}");
    }
}
