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.responses;
019
020import org.junit.Test;
021import org.junit.runner.RunWith;
022import org.junit.runners.Parameterized;
023import org.openrdf.rio.WriterConfig;
024import org.openrdf.rio.helpers.JSONLDMode;
025
026import javax.ws.rs.core.MediaType;
027import java.util.Arrays;
028
029import static java.util.Collections.singletonMap;
030import static org.fcrepo.http.commons.responses.WriterConfigHelper.PROFILE_COMPACT;
031import static org.fcrepo.http.commons.responses.WriterConfigHelper.PROFILE_EXPAND;
032import static org.fcrepo.http.commons.responses.WriterConfigHelper.PROFILE_FLATTEN;
033import static org.fcrepo.http.commons.responses.WriterConfigHelper.PROFILE_KEY;
034import static org.junit.Assert.assertEquals;
035import static org.junit.Assert.assertFalse;
036import static org.junit.runners.Parameterized.Parameter;
037import static org.junit.runners.Parameterized.Parameters;
038import static org.openrdf.rio.helpers.JSONLDMode.COMPACT;
039import static org.openrdf.rio.helpers.JSONLDMode.EXPAND;
040import static org.openrdf.rio.helpers.JSONLDMode.FLATTEN;
041import static org.openrdf.rio.helpers.JSONLDSettings.JSONLD_MODE;
042
043/**
044 * @author awoods
045 * @since 7/7/2015.
046 */
047@RunWith(Parameterized.class)
048public class WriterConfigHelperTest {
049
050    @Parameter(value = 0)
051    public MediaType mediaType;
052
053    @Parameter(value = 1)
054    public JSONLDMode profile;
055
056    @Parameters(name = "{index}: mediaType:{0} => {1}")
057    public static Iterable<Object[]> data() {
058        return Arrays.asList(new Object[][]{
059                {new MediaType(null, null), null},
060                {new MediaType(null, "ld+json"), null},
061                {new MediaType("application", null), null},
062                {new MediaType("", ""), null},
063                {new MediaType("*", "*"), null},
064                {new MediaType("*", "pdf"), null},
065                {new MediaType("*", "ld+json"), null},
066                {new MediaType("application", "*"), null},
067                {new MediaType("application", "pdf"), null},
068                {new MediaType("application", "ld+json"), null},
069                {new MediaType("application", "ld+json", singletonMap(PROFILE_KEY, PROFILE_COMPACT)), COMPACT},
070                {new MediaType("application", "ld+json", singletonMap(PROFILE_KEY, PROFILE_FLATTEN)), FLATTEN},
071                {new MediaType("application", "ld+json", singletonMap(PROFILE_KEY, PROFILE_EXPAND)), EXPAND}
072        });
073    }
074
075    @Test
076    public void testApply() {
077        final WriterConfig config = WriterConfigHelper.apply(mediaType);
078        if (null == profile) {
079            assertFalse("JSONLD_MODE should not be set!", config.isSet(JSONLD_MODE));
080        } else {
081            assertEquals("JSONLD_MODE should have been: " + profile, config.get(JSONLD_MODE), profile);
082        }
083    }
084}