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