com.fujitsu.dc.test.jersey.cell.ErrorPageTest.java Source code

Java tutorial

Introduction

Here is the source code for com.fujitsu.dc.test.jersey.cell.ErrorPageTest.java

Source

/**
 * personium.io
 * Copyright 2014 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 com.fujitsu.dc.test.jersey.cell;

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

    import java.util.HashMap;

    import javax.ws.rs.core.MediaType;

    import org.apache.http.HttpHeaders;
    import org.apache.http.HttpStatus;
    import org.junit.Test;
    import org.junit.experimental.categories.Category;
    import org.junit.runner.RunWith;

    import com.fujitsu.dc.core.DcCoreException;
    import com.fujitsu.dc.core.DcCoreMessageUtils;
    import com.fujitsu.dc.test.categories.Integration;
    import com.fujitsu.dc.test.categories.Regression;
    import com.fujitsu.dc.test.categories.Unit;
    import com.fujitsu.dc.test.jersey.DaoException;
    import com.fujitsu.dc.test.jersey.DcException;
    import com.fujitsu.dc.test.jersey.DcResponse;
    import com.fujitsu.dc.test.jersey.DcRestAdapter;
    import com.fujitsu.dc.test.jersey.DcRunner;
    import com.fujitsu.dc.test.setup.Setup;
    import com.fujitsu.dc.test.unit.core.UrlUtils;
    import com.sun.jersey.test.framework.JerseyTest;

    /**
     * ImplicitFlow??.
     */
    @RunWith(DcRunner.class)
    @Category({ Unit.class, Integration.class, Regression.class })
    public class ErrorPageTest extends JerseyTest {

        /**
         * .
         */
        public ErrorPageTest() {
            super("com.fujitsu.dc.core.rs");
        }

        /**
         * ??GET??????????.
         */
        @Test
        public final void ??GET??????????()
        {

            String code = DcCoreException.OData.JSON_PARSE_ERROR.getCode();
            DcResponse res = requesttoErrorPage(code);

            assertEquals(HttpStatus.SC_OK, res.getStatusCode());

            // ???
            assertEquals(MediaType.TEXT_HTML + ";charset=UTF-8", res.getFirstHeader(HttpHeaders.CONTENT_TYPE));

            // ???
            checkResponseBody(res, code);

        }

        /**
         * personium??????????undefined????.
         */
        @Test
        public final void personium??????????undefined????()
        {

            String code = "dummyCode";
            DcResponse res = requesttoErrorPage(code);

            assertEquals(HttpStatus.SC_OK, res.getStatusCode());

            // ???
            assertEquals(MediaType.TEXT_HTML + ";charset=UTF-8", res.getFirstHeader(HttpHeaders.CONTENT_TYPE));

            // ???
            checkResponseBody(res, null);

        }

        /**
         * ??????undefined????.
         */
        @Test
        public final void ??????undefined????()
        {

        String code = "";
        DcResponse res = requesttoErrorPage(code);

        assertEquals(HttpStatus.SC_OK, res.getStatusCode());

        // ???
        assertEquals(MediaType.TEXT_HTML + ";charset=UTF-8", res.getFirstHeader(HttpHeaders.CONTENT_TYPE));

        // ???
        checkResponseBody(res, null);
    }

    /**
     * ?????undefined????.
     */
    @Test
    public final void ?????undefined????()
        {

            DcRestAdapter rest = new DcRestAdapter();
            DcResponse res = null;

            // 
            HashMap<String, String> requestheaders = new HashMap<String, String>();

            try {
                res = rest.getAcceptEncodingGzip(UrlUtils.cellRoot(Setup.TEST_CELL1) + "__html/error", requestheaders);
            } catch (DcException e) {
                e.printStackTrace();
            }

            assertEquals(HttpStatus.SC_OK, res.getStatusCode());

            // ???
            assertEquals(MediaType.TEXT_HTML + ";charset=UTF-8", res.getFirstHeader(HttpHeaders.CONTENT_TYPE));

            // ???
            checkResponseBody(res, null);

        }

    /**
     * ??POST?405????.
     */
    @Test
    public final void ??POST?405 ????()
        {

            String code = DcCoreException.OData.JSON_PARSE_ERROR.getCode();
            DcRestAdapter rest = new DcRestAdapter();
            DcResponse res = null;

            // 
            HashMap<String, String> requestheaders = new HashMap<String, String>();

            try {
                res = rest.post(UrlUtils.cellRoot(Setup.TEST_CELL1) + "__html/error?code=" + code, "", requestheaders);
            } catch (DcException e) {
                e.printStackTrace();
            }
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, res.getStatusCode());
        }

    /**
     * ??PUT?405????.
     */
    @Test
    public final void ??PUT?405 ????()
        {

            String code = DcCoreException.OData.JSON_PARSE_ERROR.getCode();
            DcRestAdapter rest = new DcRestAdapter();
            DcResponse res = null;

            // 
            HashMap<String, String> requestheaders = new HashMap<String, String>();

            try {
                res = rest.put(UrlUtils.cellRoot(Setup.TEST_CELL1) + "__html/error?code=" + code, "", requestheaders);
            } catch (DcException e) {
                e.printStackTrace();
            }
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, res.getStatusCode());
        }

    /**
     * ??DELETE?405????.
     */
    @Test
    public final void ??DELETE?405 ????()
        {

            String code = DcCoreException.OData.JSON_PARSE_ERROR.getCode();
            DcRestAdapter rest = new DcRestAdapter();
            DcResponse res = null;

            // 
            HashMap<String, String> requestheaders = new HashMap<String, String>();

            try {
                res = rest.del(UrlUtils.cellRoot(Setup.TEST_CELL1) + "__html/error?code=" + code, requestheaders);
            } catch (DcException e) {
                e.printStackTrace();
            }
            assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, res.getStatusCode());
        }

    /**
     * ??.
     * @return ?
     */
    private DcResponse requesttoErrorPage(String code) {
        DcRestAdapter rest = new DcRestAdapter();
        DcResponse res = null;

        // 
        HashMap<String, String> requestheaders = new HashMap<String, String>();

        try {
            res = rest.getAcceptEncodingGzip(UrlUtils.cellRoot(Setup.TEST_CELL1) + "__html/error?code="
                    + code, requestheaders);
        } catch (DcException e) {
            e.printStackTrace();
        }

        return res;
    }

    /**
     * ????.
     * @param res ?
     * @param expectedCode ?
     */
    public static void checkResponseBody(DcResponse res, String expectedCode) {
        String body = null;
        String expectedMessage = null;
        if (expectedCode == null) {
            expectedMessage = DcCoreMessageUtils.getMessage("PS-ER-0003");
        } else {
            expectedMessage = DcCoreMessageUtils.getMessage(expectedCode);
        }
        try {
            body = res.bodyAsString();
            System.out.println(body);
            assertEquals(
                    "<html><head><title>ERROR</title></head><body><h1>ERROR</h1></br><p>PCS ERROR</p><p>"
                            + expectedMessage + "</p></body></html>",
                    body.replaceFirst("<!-- .*-->", ""));
        } catch (DaoException e) {
            fail(e.getMessage());
            e.printStackTrace();
        }
    }
    }