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 static javax.jcr.ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW;
019import static org.fcrepo.serialization.FedoraObjectSerializer.JCR_XML;
020import static org.junit.Assert.assertEquals;
021import static org.mockito.Matchers.any;
022import static org.mockito.Matchers.eq;
023import static org.mockito.Mockito.mock;
024import static org.mockito.Mockito.verify;
025import static org.mockito.Mockito.when;
026import static org.mockito.MockitoAnnotations.initMocks;
027
028import java.io.ByteArrayOutputStream;
029import java.io.IOException;
030import java.io.InputStream;
031import java.io.OutputStream;
032
033import javax.jcr.Node;
034import javax.jcr.RepositoryException;
035import javax.jcr.Session;
036
037import org.fcrepo.kernel.api.models.FedoraResource;
038import org.junit.Before;
039import org.junit.Test;
040import org.mockito.Mock;
041
042/**
043 * <p>JcrXmlSerializerTest class.</p>
044 *
045 * @author lsitu
046 */
047public class JcrXmlSerializerTest {
048
049    @Mock
050    private Session mockSession;
051
052    @Mock
053    private Node mockNode;
054
055    @Mock
056    private FedoraResource mockResource;
057
058    private String testPath = "/path/to/node";
059
060    @Before
061    public void setUp() throws Exception {
062        initMocks(this);
063        when(mockResource.getPath()).thenReturn(testPath);
064        when(mockResource.getNode()).thenReturn(mockNode);
065        when(mockNode.getSession()).thenReturn(mockSession);
066    }
067
068    @Test
069    public void testSerialize() throws Exception {
070
071        final OutputStream os = new ByteArrayOutputStream();
072
073        new JcrXmlSerializer().serialize(mockResource, os, false, false);
074
075        verify(mockSession).exportSystemView(testPath, os, false, !false);
076    }
077
078    @Test
079    public void testSerializeWithSkipBinary() throws Exception {
080
081        final boolean skipBinary = true;
082        final OutputStream os = new ByteArrayOutputStream();
083
084        new JcrXmlSerializer().serialize(mockResource, os, skipBinary, false);
085
086        verify(mockSession).exportSystemView(testPath, os, skipBinary, !false);
087    }
088
089    @Test
090    public void testSerializeWithOptions() throws Exception {
091
092        final boolean skipBinary = true;
093        final boolean recurse = true;
094        final OutputStream os = new ByteArrayOutputStream();
095
096        new JcrXmlSerializer().serialize(mockResource, os, skipBinary, recurse);
097
098        verify(mockSession).exportSystemView(testPath, os, skipBinary, !recurse);
099    }
100
101    @Test
102    public void testDeserialize() throws IOException, RepositoryException, InvalidSerializationFormatException {
103        final InputStream is = getClass().getClassLoader().getResourceAsStream("valid-jcr-xml.xml");
104        final Session mockSession = mock(Session.class);
105        new JcrXmlSerializer().deserialize(mockSession, "/objects", is);
106        verify(mockSession).importXML(eq("/objects"), any(InputStream.class), eq(IMPORT_UUID_COLLISION_THROW));
107    }
108
109    @Test
110    public void testGetKey() {
111        assertEquals(JCR_XML, new JcrXmlSerializer().getKey());
112    }
113
114    @Test
115    public void testGetMediaType() {
116        assertEquals("application/xml", new JcrXmlSerializer().getMediaType());
117    }
118
119    @Test
120    public void testValidJCRXMLValidation() throws IOException,
121            InvalidSerializationFormatException, RepositoryException {
122        final Session mockSession = mock(Session.class);
123        new JcrXmlSerializer().deserialize(mockSession, "/objects",
124                getClass().getClassLoader().getResourceAsStream("valid-jcr-xml.xml"));
125    }
126
127    @Test (expected = InvalidSerializationFormatException.class)
128    public void testInvalidJCRXMLValidation() throws IOException,
129            InvalidSerializationFormatException, RepositoryException {
130        final Session mockSession = mock(Session.class);
131        new JcrXmlSerializer().deserialize(mockSession, "/objects",
132                getClass().getClassLoader().getResourceAsStream("invalid-jcr-xml.xml"));
133    }
134
135    @Test (expected = InvalidSerializationFormatException.class)
136    public void testNonRDFContentJCRXMLValidation() throws IOException,
137            InvalidSerializationFormatException, RepositoryException {
138        final Session mockSession = mock(Session.class);
139        new JcrXmlSerializer().deserialize(mockSession, "/objects",
140                getClass().getClassLoader().getResourceAsStream("invalid-jcr-xml-2.xml"));
141    }
142
143    @Test (expected = InvalidSerializationFormatException.class)
144    public void testFrozenResourceJCRXMLValidation() throws IOException,
145            InvalidSerializationFormatException, RepositoryException {
146        final Session mockSession = mock(Session.class);
147        new JcrXmlSerializer().deserialize(mockSession, "/objects",
148                getClass().getClassLoader().getResourceAsStream("invalid-jcr-xml-3.xml"));
149    }
150
151
152}