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;
030
031import static org.fcrepo.kernel.api.RdfLexicon.CONSTRAINED_BY;
032import static org.junit.Assert.assertEquals;
033
034/**
035 * @author cabeer
036 */
037public class MalformedRdfExceptionMapperTest {
038
039    private MalformedRdfExceptionMapper testObj;
040
041    @Before
042    public void setUp() {
043        testObj = new MalformedRdfExceptionMapper();
044
045    }
046    @Test
047    public void testToResponse() throws IOException {
048        final Response response = testObj.toResponse(new MalformedRdfException("xyz"));
049
050        final Link link = response.getLink(CONSTRAINED_BY.getURI());
051        assertEquals(CONSTRAINED_BY.getURI(), link.getRel());
052        assertEquals("data", link.getUri().getScheme());
053        final String[] split = link.getUri().toString().split(",", 2);
054        assertEquals("Constraint data appears malformed", 2, split.length);
055        assertEquals("xyz", IOUtils.toString(Base64.decodeBase64(split[1].getBytes()), "UTF-8"));
056    }
057
058    @Test
059    public void testToResponseError() {
060        final String errorPrefix = "org.modeshape.jcr.value.ValueFormatException: ";
061        final String errorSuffix = "Error converting ...";
062        final Response response = testObj.toResponse(new MalformedRdfException(errorPrefix + errorSuffix));
063
064        assertEquals(errorSuffix, response.getEntity().toString());
065    }
066
067    @Test
068    public void testToResponseError2() {
069        final String errorPrefix = "org.modeshape.jcr.value.ValueFormat: ";
070        final String errorSuffix = "Error converting ...";
071        final Response response = testObj.toResponse(new MalformedRdfException(errorPrefix + errorSuffix));
072
073        assertEquals(errorPrefix + errorSuffix, response.getEntity().toString());
074    }
075}