/*
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you 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.apache.wiki.util;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import javax.mail.MessagingException;
import javax.net.ssl.SSLHandshakeException;
import java.net.ConnectException;
import java.util.Properties;

/**
 * This test is not integrated into any TestSuite yet, because I don't know how
 * to really assert if a mail was sent etc. (setup would be to complicated yet). Therefore
 * replace the static TEST variables with your configuration and test by hand: execute
 * testSendMail and see if you get the mail at the adress you indicate in "TEST_RECEIVER".
 *
 *
 */
class MailUtilTest  {
    
    static Properties m_props = new Properties();

    @BeforeAll
    static void setUp() {
        m_props = PropertyReader.getCombinedProperties( PropertyReader.CUSTOM_JSPWIKI_CONFIG );
    }

    /**
     * Verifies that the properties loaded by tests/etc/jspwiki.properties are the ones we expect.
     * Three of them (account, password, jndi name) are commented out, so we expect null.
     */
    @Test
    void testProperties() {
        Assertions.assertEquals( "127.0.0.1",                   m_props.getProperty( MailUtil.PROP_MAIL_HOST ) );
        Assertions.assertEquals( "25",                          m_props.getProperty( MailUtil.PROP_MAIL_PORT ) );
        Assertions.assertEquals( "JSPWiki <JSPWiki@localhost>", m_props.getProperty( MailUtil.PROP_MAIL_SENDER ) );
        Assertions.assertNull( m_props.getProperty( MailUtil.PROP_MAIL_ACCOUNT ) );
        Assertions.assertNull( m_props.getProperty( MailUtil.PROP_MAIL_PASSWORD ) );
        Assertions.assertNull( m_props.getProperty( MailUtil.PROP_MAIL_JNDI_NAME ) );
    }

    /**
     * This test sends a message to the local user's mailbox on this host. It assumes that
     * there is a locally-listening SMTP server on port 25, and that the current runtime
     * user has a mailbox on the local machine. For Unix-based systems such as Linux and
     * OS X, you can verify that this test ran successfully simply by typing "mail" at
     * the command line.
     */
    @Test
    void testSendMail() {
        final String user = System.getProperty( "user.name" ) + "@localhost";

        try {
            MailUtil.sendMessage( m_props, user, "Mail test", "This is a test mail generated by MailUtilTest." );
        } catch( final MessagingException e ) {
            if( e.getCause() instanceof ConnectException ) {
                // This can occur if you do not have an SMTP server set up.  We just log this and don't Assertions.fail.
                System.out.println("I could not test whether mail sending works, since I could not connect to your SMTP server.");
                System.out.println("Reason: "+e.getMessage());
                return;
            }
            if( e.getCause() instanceof SSLHandshakeException ) {
                // This can occur if you do not have the required cert in the JVM's keystore.  We just log this
                // and don't Assertions.fail.
                System.out.println("I could not test whether mail sending works, since I don't have the required cert in my keystore.");
                System.out.println("Reason: "+e.getMessage());
                return;
            }
            if( e.getCause() == null ) {
                System.out.println("Reason: "+e.getMessage());
                System.out.println("I could not test whether mail sending works, we let the test pass anyway.");
                return;
            }
            e.printStackTrace();
            Assertions.fail( "Unknown problem, cause=" + e.getCause() + " (check the console for error report)", e );
        } catch (final Exception e) {
            e.printStackTrace();
            Assertions.fail( "Could not send mail: " + e.getMessage(), e );
        }
    }

}
