io.personium.jersey.engine.test.ExtensionScriptIntegrationTest.java Source code

Java tutorial

Introduction

Here is the source code for io.personium.jersey.engine.test.ExtensionScriptIntegrationTest.java

Source

    /**
     * Personium
     * Copyright 2014 - 2017 FUJITSU LIMITED
     *
     * 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 io.personium.jersey.engine.test;

    import java.io.IOException;
    import java.lang.reflect.Field;

    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.methods.HttpUriRequest;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.experimental.categories.Category;
    import org.junit.runner.RunWith;

    import com.dumbster.smtp.SimpleSmtpServer;
    import io.personium.client.DaoException;
    import io.personium.client.http.PersoniumRequestBuilder;
    import io.personium.client.http.PersoniumResponse;
    import io.personium.engine.extension.support.ExtensionJarLoader;
    import io.personium.jersey.engine.test.categories.Integration;

    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.fail;

    /**
     * Extension?Engine???. <br />
     * ???/personium/personium-engine/extensions????Extension?????.<br />
     * ???src/test/resources??extension-test-config.properties?????????.<br />
     * ?extension-test-config.properties.sample??????????
     */
@RunWith(PersoniumEngineRunner.class)
@Category({ Integration.class })
public class ExtensionScriptIntegrationTest extends ScriptTestBase {
    /**
     * .
     */
    public ExtensionScriptIntegrationTest() {
        super("io.personium.engine");
    }

    /**
     * ??????.
     * @throws Exception ??????????
     */
    @Before
    public void before() throws Exception {
        // ? singleton?????????????
        // ? singleton??????????
        Field field = ExtensionJarLoader.class.getDeclaredField("singleton");
        field.setAccessible(true);
        field.set(null, null);
    }

    /**
     * ?Ext_MailSender????. Extension? MailSender <br />
     * ???????Ext_MailSender? jar???<br />
     * <br />
     */
    @Test
    public final void ?Ext_MailSender????() {

        SimpleSmtpServer smtpServer = SimpleSmtpServer.start(1025);

        try {
            String url;
            String testSrc = "callExtensionMailSender.js";
            HttpUriRequest req = null;
            String requestJson = "{\"to\":[{\"address\":\"hoge1@hoge.com\",\"name\":\"\"},"
                    + "{\"address\":\"hoge2@hoge.com\",\"name\":\"\"}],"
                    + "\"cc\":[{\"address\":\"hoge3@hoge.com\",\"name\":\"\"}],"
                    + "\"bcc\":[{\"address\":\"hoge4@hoge.com\",\"name\":\"\"}],"
                    + "\"from\":{\"address\":\"hoge5@hoge.com\",\"name\":\"\"},"
                    + "\"reply-to\":[{\"address\":\"hoge6@hoge.com\",\"name\":\"\"}],"
                    + "\"subject\":\"\",\"text\":\"?\",\"charset\":\"ISO-2022-JP\","
                    + "\"envelope-from\":\"hoge7@hoge.com\",\"headers\":{\"Organization\":\"personium\"}}";

            try {
                if (isServiceTest) {
                    // ? Dav?put
                    putScript(testSrc, "test.js");
                    url = requestUrl();
                } else {
                    url = requestUrl(testSrc);
                }
                // ?
                req = new PersoniumRequestBuilder().url(url).method("POST").body(requestJson).token(token).build();
                req.setHeader(KEY_HEADER_BASEURL, baseUrl);
                String version = getVersion();
                if (version != null && !(version.equals(""))) {
                    req.setHeader("X-Personium-Version", version);
                }

                HttpResponse objResponse;
                objResponse = httpClient.execute(req);
                PersoniumResponse dcRes = new PersoniumResponse(objResponse);

                assertEquals(HttpStatus.SC_OK, dcRes.getStatusCode());
                assertEquals("Successfully sent a mail.", dcRes.bodyAsString());

                if (!isServiceTest) {
                    // IT????SMTP?????????
                    assertEquals(1, smtpServer.getReceivedEmailSize());
                }

            } catch (DaoException e) {
                fail(e.getMessage());
            } catch (ClientProtocolException e) {
                fail(e.getMessage());
            } catch (IOException e) {
                fail(e.getMessage());
            } finally {
                if (isServiceTest) {
                    // ?Dav?del
                    try {
                        testSvcCol.del("test.js");
                    } catch (DaoException e) {
                        fail(e.getMessage());
                    }
                }
            }
        } finally {
            smtpServer.stop();
        }
    }

}