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.serialization;
017
018import com.google.common.collect.ImmutableMap;
019import com.google.common.collect.ImmutableSet;
020import org.junit.Before;
021import org.junit.Test;
022import org.springframework.context.ApplicationContext;
023
024import static org.junit.Assert.assertEquals;
025import static org.mockito.Mockito.mock;
026import static org.mockito.Mockito.when;
027
028/**
029 * <p>SerializerUtilTest class.</p>
030 *
031 * @author cbeer
032 */
033public class SerializerUtilTest {
034
035    private ApplicationContext mockContext;
036
037    private SerializerUtil testObj;
038
039    @Before
040    public void setUp() {
041        mockContext = mock(ApplicationContext.class);
042
043        testObj = new SerializerUtil();
044        testObj.setApplicationContext(mockContext);
045
046        final FedoraObjectSerializer mockA = mock(FedoraObjectSerializer.class);
047        when(mockA.getKey()).thenReturn("a");
048        final FedoraObjectSerializer mockB = mock(FedoraObjectSerializer.class);
049        when(mockB.getKey()).thenReturn("b");
050
051        when(mockContext.getBeansOfType(FedoraObjectSerializer.class))
052                .thenReturn(ImmutableMap.of("mockA", mockA, "mockB", mockB));
053
054        testObj.buildFedoraObjectSerializersMap();
055
056    }
057
058    @Test
059    public void shouldProvideSetOfFormatsItCanSerialize() {
060        assertEquals(ImmutableSet.of("a", "b"), testObj.keySet());
061    }
062
063    @Test
064    public void shouldRetrieveSerializerByKey() {
065        final FedoraObjectSerializer mockA = mock(FedoraObjectSerializer.class);
066        when(mockA.getKey()).thenReturn("a");
067        final FedoraObjectSerializer mockB = mock(FedoraObjectSerializer.class);
068        when(mockB.getKey()).thenReturn("b");
069
070        when(mockContext.getBeansOfType(FedoraObjectSerializer.class))
071                .thenReturn(ImmutableMap.of("mockA", mockA, "mockB", mockB));
072
073        testObj.buildFedoraObjectSerializersMap();
074
075        assertEquals(mockA, testObj.getSerializer("a"));
076    }
077
078    @Test
079    public void shouldListAllSerializers() {
080        final FedoraObjectSerializer mockA = mock(FedoraObjectSerializer.class);
081        when(mockA.getKey()).thenReturn("a");
082        final FedoraObjectSerializer mockB = mock(FedoraObjectSerializer.class);
083        when(mockB.getKey()).thenReturn("b");
084
085        when(mockContext.getBeansOfType(FedoraObjectSerializer.class))
086                .thenReturn(ImmutableMap.of("mockA", mockA, "mockB", mockB));
087
088        testObj.buildFedoraObjectSerializersMap();
089
090        assertEquals(ImmutableMap.of("a", mockA, "b", mockB), testObj
091                .getFedoraObjectSerializers());
092
093    }
094}