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