de.elomagic.mag.WebDAVTest.java Source code

Java tutorial

Introduction

Here is the source code for de.elomagic.mag.WebDAVTest.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.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import javax.mail.internet.MimeMessage;

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

/**
 *
 * @author Carsten Rambow
 */
public class WebDAVTest extends AbstractTest {

    @Before
    public void before() throws Exception {
        try (InputStream in = getClass().getResourceAsStream("/configuration.properties");
                Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
            Configuration.loadConfiguration(reader);
        }

        Configuration.set(Configuration.ConfigurationKey.TargetWebDAVEnabled, true);

        startEMailServer();

        startHttpServer();

        main = new MAG();
    }

    @Test
    public void testMain() throws Exception {

        greeUser.deliver(createMimeMessage("TestFile.pdf"));

        main.start();

        Future future = createPutServletFuture(putServlet);
        future.get(500, TimeUnit.SECONDS);

        InputStream in = getClass().getResourceAsStream("/TestFile.pdf");
        int fileSize = in.available();
        byte[] send = IOUtils.readFully(in, fileSize);

        Assert.assertEquals(fileSize, putServlet.lastContentLength);
        Assert.assertArrayEquals(send, putServlet.lastContent);
        Assert.assertTrue("Put filename doesn't match.",
                putServlet.requestedUri.startsWith("/inbox/unsorted/TestFile")
                        && putServlet.requestedUri.endsWith(".pdf"));
        Assert.assertNotEquals("/inbox/unsorted/TestFile", putServlet.requestedUri);
    }

    // I had to ignore the test because success mails will now be send asynchronously. 
    @Ignore
    @Test
    public void testSendSuccessMail() throws Exception {

        Configuration.set(Configuration.ConfigurationKey.MailNotificationSuccessRecipient,
                "mailuser@localhost.com");

        greeUser.deliver(createMimeMessage("TestFile.pdf"));

        main.start();

        Future future = createPutServletFuture(putServlet);
        future.get(5, TimeUnit.SECONDS);

        Thread.sleep(2000);

        MimeMessage[] messages = greenMail.getReceivedMessages();
        Assert.assertEquals(1, messages.length);

        Assert.assertEquals("An attachment was successful uploaded.", messages[0].getSubject());
        Assert.assertEquals(Bundle.get(Bundle.BundleKey.MailNotificationSuccessBody),
                messages[0].getContent().toString().trim());
    }

    @Test
    @Ignore
    public void testSendFailedMail() throws Exception {

        Configuration.set(Configuration.ConfigurationKey.MailNotificationFailedRecipient, "mailuser@localhost.com");
        Configuration.set(Configuration.ConfigurationKey.MailSMTPHostname, "localhost:3026");

        greeUser.deliver(createMimeMessage("TestFile.pdf"));

        main.start();

        Future future = createPutServletFuture(putServlet);
        future.get(5, TimeUnit.SECONDS);

        MimeMessage[] messages = greenMail.getReceivedMessages();
        Assert.assertEquals(1, messages.length);

        Assert.assertEquals("An error during processing of an email occur!", messages[0].getSubject());
        Assert.assertEquals(Bundle.get(Bundle.BundleKey.MailNotificationFailedBody),
                messages[0].getContent().toString());
    }

}