001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.http.commons.exceptionhandlers;
019
020import org.apache.commons.codec.binary.Base64;
021import org.apache.commons.io.IOUtils;
022import org.fcrepo.kernel.api.exception.MalformedRdfException;
023import org.junit.Before;
024import org.junit.Test;
025
026import javax.ws.rs.core.Link;
027import javax.ws.rs.core.Response;
028
029import java.io.IOException;
030import java.util.Random;
031import java.util.stream.Stream;
032
033import static javax.ws.rs.core.HttpHeaders.LINK;
034import static org.fcrepo.kernel.api.RdfLexicon.CONSTRAINED_BY;
035import static org.junit.Assert.assertEquals;
036import static org.junit.Assert.assertNotNull;
037import static org.junit.Assert.assertTrue;
038
039/**
040 * @author cabeer
041 */
042public class MalformedRdfExceptionMapperTest {
043
044    private MalformedRdfExceptionMapper testObj;
045
046    @Before
047    public void setUp() {
048        testObj = new MalformedRdfExceptionMapper();
049
050    }
051    @Test
052    public void testToResponse() throws IOException {
053        final Response response = testObj.toResponse(new MalformedRdfException("xyz"));
054
055        final Link link = response.getLink(CONSTRAINED_BY.getURI());
056        assertEquals(CONSTRAINED_BY.getURI(), link.getRel());
057        assertEquals("data", link.getUri().getScheme());
058        final String[] split = link.getUri().toString().split(",", 2);
059        assertEquals("Constraint data appears malformed", 2, split.length);
060        assertEquals("xyz", IOUtils.toString(Base64.decodeBase64(split[1].getBytes()), "UTF-8"));
061    }
062
063    @Test
064    public void testToResponseError() {
065        final String errorPrefix = "org.modeshape.jcr.value.ValueFormatException: ";
066        final String errorSuffix = "Error converting ...";
067        final Response response = testObj.toResponse(new MalformedRdfException(errorPrefix + errorSuffix));
068
069        assertEquals(errorSuffix, response.getEntity().toString());
070    }
071
072    @Test
073    public void testToResponseError2() {
074        final String errorPrefix = "org.modeshape.jcr.value.ValueFormat: ";
075        final String errorSuffix = "Error converting ...";
076        final Response response = testObj.toResponse(new MalformedRdfException(errorPrefix + errorSuffix));
077
078        assertEquals(errorPrefix + errorSuffix, response.getEntity().toString());
079    }
080
081    @Test
082    public void testToResponseLongError() {
083        final StringBuilder error = new StringBuilder();
084        Stream.generate(new Random()::nextInt).limit(2000).forEach(error::append);
085        final Response response = testObj.toResponse(new MalformedRdfException(error.toString()));
086
087        final String linkHeader = response.getHeaderString(LINK);
088        assertNotNull(linkHeader);
089        assertTrue(linkHeader.length() < 1000);
090    }
091}