This project has retired. For details please refer to its Attic page.
AtomEntryParserTest xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.server.impl;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertNull;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.io.ByteArrayInputStream;
27  import java.io.ByteArrayOutputStream;
28  
29  import org.apache.chemistry.opencmis.commons.PropertyIds;
30  import org.apache.chemistry.opencmis.commons.data.ContentStream;
31  import org.apache.chemistry.opencmis.commons.data.PropertyString;
32  import org.apache.chemistry.opencmis.server.impl.atompub.AtomEntryParser;
33  import org.apache.commons.codec.binary.Base64;
34  import org.junit.Test;
35  
36  /**
37   * AtomEntryParser test.
38   */
39  public class AtomEntryParserTest {
40  
41      private static final int THRESHOLD = 4 * 1024 * 1024;
42  
43      private static final String CMIS_ENTRY_CONTENT = "This is my content!";
44      private static final String CMIS_ENTRY = "<?xml version='1.0' encoding='utf-8'?>"
45              + "<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:cmis=\"http://docs.oasis-open.org/ns/cmis/core/200908/\" xmlns:cmisra=\"http://docs.oasis-open.org/ns/cmis/restatom/200908/\">"
46              + "<atom:author><atom:name>test</atom:name></atom:author>" + "<atom:id>http://test/id</atom:id>"
47              + "<atom:published>2009-12-31T12:53:37Z</atom:published>" + "<atom:title>test.txt</atom:title>"
48              + "<atom:updated>2010-01-01T00:00:00Z</atom:updated>"
49              + "<cmisra:object xmlns:ns3=\"http://docs.oasis-open.org/ns/cmis/messaging/200908/\">"
50              + "  <cmis:properties>" + "    <cmis:propertyId propertyDefinitionId=\"cmis:objectId\">"
51              + "      <cmis:value>id</cmis:value>" + "    </cmis:propertyId>"
52              + "    <cmis:propertyString propertyDefinitionId=\"cmis:name\">"
53              + "      <cmis:value>test.txt</cmis:value>" + "    </cmis:propertyString>"
54              + "    <cmis:propertyId propertyDefinitionId=\"cmis:objectTypeId\">"
55              + "      <cmis:value>cmis:document</cmis:value>" + "    </cmis:propertyId>" + "  </cmis:properties>"
56              + "</cmisra:object>" + "<cmisra:content>" + "  <cmisra:mediatype>text/plain</cmisra:mediatype>"
57              + "  <cmisra:base64>" + Base64.encodeBase64String(CMIS_ENTRY_CONTENT.getBytes()) + "</cmisra:base64>"
58              + "</cmisra:content>" + "</atom:entry>";
59  
60      private static final String ATOM_ENTRY_TEXT_CONTENT = "This is plain text!";
61      private static final String ATOM_ENTRY_TEXT = "<?xml version='1.0' encoding='utf-8'?>"
62              + "<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:cmis=\"http://docs.oasis-open.org/ns/cmis/core/200908/\" xmlns:cmisra=\"http://docs.oasis-open.org/ns/cmis/restatom/200908/\">"
63              + "<atom:author><atom:name>test</atom:name></atom:author>" + "<atom:id>http://test/id</atom:id>"
64              + "<atom:published>2009-12-31T12:53:37Z</atom:published>" + "<atom:title>test.txt</atom:title>"
65              + "<atom:updated>2010-01-01T00:00:00Z</atom:updated>"
66              + "<cmisra:object xmlns:ns3=\"http://docs.oasis-open.org/ns/cmis/messaging/200908/\">"
67              + "  <cmis:properties>" + "    <cmis:propertyId propertyDefinitionId=\"cmis:objectId\">"
68              + "      <cmis:value>id</cmis:value>" + "    </cmis:propertyId>"
69              + "    <cmis:propertyString propertyDefinitionId=\"cmis:name\">"
70              + "      <cmis:value>test.txt</cmis:value>" + "    </cmis:propertyString>"
71              + "    <cmis:propertyId propertyDefinitionId=\"cmis:objectTypeId\">"
72              + "      <cmis:value>cmis:document</cmis:value>" + "    </cmis:propertyId>" + "  </cmis:properties>"
73              + "</cmisra:object>" + "<atom:content type=\"text\">" + ATOM_ENTRY_TEXT_CONTENT + "</atom:content>"
74              + "</atom:entry>";
75  
76      private static final String ATOM_ENTRY_XML_CONTENT = "<first xmlns=\"http://test/1\"><second myattr=\"Cool, a value!\">hey, this is text</second><myns:third xmlns:myns=\"http://test/2\">guess what's here ... more text</myns:third></first>";
77      private static final String ATOM_ENTRY_XML = "<?xml version='1.0' encoding='utf-8'?>"
78              + "<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:cmis=\"http://docs.oasis-open.org/ns/cmis/core/200908/\" xmlns:cmisra=\"http://docs.oasis-open.org/ns/cmis/restatom/200908/\">"
79              + "<atom:author><atom:name>test</atom:name></atom:author>" + "<atom:id>http://test/id</atom:id>"
80              + "<atom:published>2009-12-31T12:53:37Z</atom:published>" + "<atom:title>test.txt</atom:title>"
81              + "<atom:updated>2010-01-01T00:00:00Z</atom:updated>"
82              + "<cmisra:object xmlns:ns3=\"http://docs.oasis-open.org/ns/cmis/messaging/200908/\">"
83              + "  <cmis:properties>" + "    <cmis:propertyId propertyDefinitionId=\"cmis:objectId\">"
84              + "      <cmis:value>id</cmis:value>" + "    </cmis:propertyId>"
85              + "    <cmis:propertyString propertyDefinitionId=\"cmis:name\">"
86              + "      <cmis:value>test.txt</cmis:value>" + "    </cmis:propertyString>"
87              + "    <cmis:propertyId propertyDefinitionId=\"cmis:objectTypeId\">"
88              + "      <cmis:value>cmis:document</cmis:value>" + "    </cmis:propertyId>" + "  </cmis:properties>"
89              + "</cmisra:object>" + "<atom:content type=\"text/xml\">" + ATOM_ENTRY_XML_CONTENT + "</atom:content>"
90              + "</atom:entry>";
91  
92      private static final String ATOM_ENTRY_XHTML_CONTENT = "<div xmlns=\"http://www.w3.org/1999/xhtml\">This is <b>XHTML</b> content.</div>";
93      private static final String ATOM_ENTRY_XHTML = "<?xml version='1.0' encoding='utf-8'?>"
94              + "<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:cmis=\"http://docs.oasis-open.org/ns/cmis/core/200908/\" xmlns:cmisra=\"http://docs.oasis-open.org/ns/cmis/restatom/200908/\">"
95              + "<atom:author><atom:name>test</atom:name></atom:author>" + "<atom:id>http://test/id</atom:id>"
96              + "<atom:published>2009-12-31T12:53:37Z</atom:published>" + "<atom:title>test.txt</atom:title>"
97              + "<atom:updated>2010-01-01T00:00:00Z</atom:updated>"
98              + "<cmisra:object xmlns:ns3=\"http://docs.oasis-open.org/ns/cmis/messaging/200908/\">"
99              + "  <cmis:properties>" + "    <cmis:propertyId propertyDefinitionId=\"cmis:objectId\">"
100             + "      <cmis:value>id</cmis:value>" + "    </cmis:propertyId>"
101             + "    <cmis:propertyString propertyDefinitionId=\"cmis:name\">"
102             + "      <cmis:value>test.txt</cmis:value>" + "    </cmis:propertyString>"
103             + "    <cmis:propertyId propertyDefinitionId=\"cmis:objectTypeId\">"
104             + "      <cmis:value>cmis:document</cmis:value>" + "    </cmis:propertyId>" + "  </cmis:properties>"
105             + "</cmisra:object>" + "<atom:content type=\"xhtml\">" + ATOM_ENTRY_XHTML_CONTENT + "</atom:content>"
106             + "</atom:entry>";
107 
108     private static final String ATOM_ENTRY_BASE64_CONTENT = "This is another content!";
109     private static final String ATOM_ENTRY_BASE64 = "<?xml version='1.0' encoding='utf-8'?>"
110             + "<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:cmis=\"http://docs.oasis-open.org/ns/cmis/core/200908/\" xmlns:cmisra=\"http://docs.oasis-open.org/ns/cmis/restatom/200908/\">"
111             + "<atom:author><atom:name>test</atom:name></atom:author>" + "<atom:id>http://test/id</atom:id>"
112             + "<atom:published>2009-12-31T12:53:37Z</atom:published>" + "<atom:title>test.txt</atom:title>"
113             + "<atom:updated>2010-01-01T00:00:00Z</atom:updated>"
114             + "<cmisra:object xmlns:ns3=\"http://docs.oasis-open.org/ns/cmis/messaging/200908/\">"
115             + "  <cmis:properties>" + "    <cmis:propertyId propertyDefinitionId=\"cmis:objectId\">"
116             + "      <cmis:value>id</cmis:value>" + "    </cmis:propertyId>"
117             + "    <cmis:propertyString propertyDefinitionId=\"cmis:name\">"
118             + "      <cmis:value>test.txt</cmis:value>" + "    </cmis:propertyString>"
119             + "    <cmis:propertyId propertyDefinitionId=\"cmis:objectTypeId\">"
120             + "      <cmis:value>cmis:document</cmis:value>" + "    </cmis:propertyId>" + "  </cmis:properties>"
121             + "</cmisra:object>" + "<atom:content type=\"application/something\">"
122             + Base64.encodeBase64String(ATOM_ENTRY_BASE64_CONTENT.getBytes()) + "</atom:content>" + "</atom:entry>";
123 
124     private static final String ATOM_ENTRY_NAME = "<?xml version='1.0' encoding='utf-8'?>"
125             + "<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:cmis=\"http://docs.oasis-open.org/ns/cmis/core/200908/\" xmlns:cmisra=\"http://docs.oasis-open.org/ns/cmis/restatom/200908/\">"
126             + "<atom:author><atom:name>test</atom:name></atom:author>" + "<atom:id>http://test/id</atom:id>"
127             + "<atom:published>2009-12-31T12:53:37Z</atom:published>" + "<atom:title>atom.title</atom:title>"
128             + "<atom:updated>2010-01-01T00:00:00Z</atom:updated>"
129             + "<cmisra:object xmlns:ns3=\"http://docs.oasis-open.org/ns/cmis/messaging/200908/\">"
130             + "  <cmis:properties>" + "    <cmis:propertyId propertyDefinitionId=\"cmis:objectId\">"
131             + "      <cmis:value>id</cmis:value>" + "    </cmis:propertyId>"
132             + "    <cmis:propertyString propertyDefinitionId=\"cmis:name\">"
133             + "      <cmis:value>cmis.name</cmis:value>" + "    </cmis:propertyString>"
134             + "    <cmis:propertyId propertyDefinitionId=\"cmis:objectTypeId\">"
135             + "      <cmis:value>cmis:document</cmis:value>" + "    </cmis:propertyId>" + "  </cmis:properties>"
136             + "</cmisra:object>" + "</atom:entry>";
137 
138     @Test
139     public void testCmisContent() throws Exception {
140         byte[] content = parse(CMIS_ENTRY.getBytes());
141         assertEquals(CMIS_ENTRY_CONTENT, new String(content));
142     }
143 
144     @Test
145     public void testAtomContentText() throws Exception {
146         byte[] content = parse(ATOM_ENTRY_TEXT.getBytes());
147         assertEquals(ATOM_ENTRY_TEXT_CONTENT, new String(content));
148     }
149 
150     @Test
151     public void testAtomContentXml() throws Exception {
152         byte[] content = parse(ATOM_ENTRY_XML.getBytes());
153         String xmlContent = new String(content);
154         assertTrue(xmlContent.indexOf('>') > -1);
155         assertEquals(ATOM_ENTRY_XML_CONTENT, xmlContent.substring(xmlContent.indexOf('>') + 1));
156     }
157 
158     @Test
159     public void testAtomContentXHtml() throws Exception {
160         byte[] content = parse(ATOM_ENTRY_XHTML.getBytes());
161         String xmlContent = new String(content);
162         assertTrue(xmlContent.indexOf('>') > -1);
163         assertEquals(ATOM_ENTRY_XHTML_CONTENT, xmlContent.substring(xmlContent.indexOf('>') + 1));
164     }
165 
166     @Test
167     public void testAtomContentBase64() throws Exception {
168         byte[] content = parse(ATOM_ENTRY_BASE64.getBytes());
169         assertEquals(ATOM_ENTRY_BASE64_CONTENT, new String(content));
170     }
171 
172     @Test
173     public void testAtomTitle() throws Exception {
174         AtomEntryParser aep = new AtomEntryParser(new ByteArrayInputStream(ATOM_ENTRY_NAME.getBytes()), null, THRESHOLD);
175 
176         assertNotNull(aep);
177         assertNotNull(aep.getObject());
178         assertNotNull(aep.getObject().getProperties());
179         assertNotNull(aep.getObject().getProperties().getProperties());
180         assertNotNull(aep.getObject().getProperties().getProperties().get(PropertyIds.NAME) instanceof PropertyString);
181 
182         PropertyString nameProperty = (PropertyString) aep.getObject().getProperties().getProperties()
183                 .get(PropertyIds.NAME);
184 
185         assertEquals("atom.title", nameProperty.getFirstValue());
186     }
187 
188     @Test
189     public void testNullStream() throws Exception {
190         AtomEntryParser aep = new AtomEntryParser(null, null, THRESHOLD);
191 
192         assertNotNull(aep);
193         assertNull(aep.getId());
194         assertNull(aep.getObject());
195         assertNull(aep.getContentStream());
196         assertNull(aep.getProperties());
197     }
198 
199     @Test
200     public void testEmptyStream() throws Exception {
201         AtomEntryParser aep = new AtomEntryParser(new ByteArrayInputStream(new byte[0]), null, THRESHOLD);
202 
203         assertNotNull(aep);
204         assertNull(aep.getId());
205         assertNull(aep.getObject());
206         assertNull(aep.getContentStream());
207         assertNull(aep.getProperties());
208     }
209 
210     private static byte[] parse(byte[] entry) throws Exception {
211         AtomEntryParser aep = new AtomEntryParser(new ByteArrayInputStream(entry), null, THRESHOLD);
212         ContentStream contentStream = aep.getContentStream();
213 
214         assertNotNull(contentStream);
215         assertNotNull(contentStream.getStream());
216 
217         ByteArrayOutputStream baos = new ByteArrayOutputStream();
218 
219         byte[] buffer = new byte[4096];
220         int b;
221 
222         while ((b = contentStream.getStream().read(buffer)) > -1) {
223             baos.write(buffer, 0, b);
224         }
225 
226         return baos.toByteArray();
227     }
228 }