001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.http.commons.exceptionhandlers;
007
008import static org.junit.Assert.assertEquals;
009import static org.junit.Assert.assertNull;
010
011import javax.ws.rs.WebApplicationException;
012import javax.ws.rs.core.Response;
013
014import org.junit.Before;
015import org.junit.Test;
016
017import java.util.stream.Stream;
018
019/**
020 * <p>WebApplicationExceptionMapperTest class.</p>
021 *
022 * @author lsitu
023 * @author awoods
024 */
025public class WebApplicationExceptionMapperTest {
026
027    private WebApplicationExceptionMapper testObj;
028
029    @Before
030    public void setUp() {
031        testObj = new WebApplicationExceptionMapper();
032    }
033
034    @Test
035    public void testToResponse() {
036        final WebApplicationException input = new WebApplicationException();
037        final Response actual = testObj.toResponse(input);
038        assertEquals(input.getResponse().getStatus(), actual.getStatus());
039    }
040
041    /**
042     * Insures that the WebApplicationExceptionMapper does not provide an entity body to 204, 205, or 304 responses.
043     * Entity bodies on other responses are mapped appropriately.
044     */
045    @Test
046    public void testNoEntityBody() {
047        Stream.of(204, 205, 304).forEach(status -> {
048                    final WebApplicationException input = new WebApplicationException("Error message", status);
049                    final Response actual = testObj.toResponse(input);
050                    assertNull("Responses with a " + status + " status code MUST NOT carry an entity body.",
051                            actual.getEntity());
052                }
053        );
054
055        final WebApplicationException input = new WebApplicationException("Error message", 500);
056        final Response actual = testObj.toResponse(input);
057        assertEquals("Error message", actual.getEntity());
058    }
059}