View Javadoc

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.tck.tests.crud;
20  
21  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.FAILURE;
22  
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.chemistry.opencmis.client.api.Document;
30  import org.apache.chemistry.opencmis.client.api.Folder;
31  import org.apache.chemistry.opencmis.client.api.ObjectId;
32  import org.apache.chemistry.opencmis.client.api.Session;
33  import org.apache.chemistry.opencmis.commons.PropertyIds;
34  import org.apache.chemistry.opencmis.commons.data.ContentStream;
35  import org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition;
36  import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
37  import org.apache.chemistry.opencmis.commons.enums.VersioningState;
38  import org.apache.chemistry.opencmis.tck.CmisTestResult;
39  import static org.apache.chemistry.opencmis.tck.CmisTestResultStatus.FAILURE;
40  import org.apache.chemistry.opencmis.tck.impl.AbstractSessionTest;
41  
42  /**
43   * Big document test.
44   */
45  public class CreateBigDocument extends AbstractSessionTest {
46  
47      @Override
48      public void init(Map<String, String> parameters) {
49          super.init(parameters);
50          setName("Create Big Document Test");
51          setDescription("Creates a 10 MiB document and deletes it.");
52      }
53  
54      @Override
55      public void run(Session session) {
56          CmisTestResult f;
57  
58          // create a test folder
59          Folder testFolder = createTestFolder(session);
60  
61          try {
62              String name = "bigdoc";
63              String objectTypeId = getDocumentTestTypeId();
64              String mimetype = "application/octet-stream";
65  
66              final long size = 10 * 1024 * 1024; // 10 MiB
67              InputStream in = new InputStream() {
68                  private int counter = -1;
69  
70                  @Override
71                  public int read() throws IOException {
72                      counter++;
73                      if (counter >= size) {
74                          return -1;
75                      }
76  
77                      return '0' + (counter / 10);
78                  }
79              };
80  
81              // create stream and properties
82              ContentStream contentStream = session.getObjectFactory().createContentStream(name, size, mimetype, in);
83  
84              Map<String, Object> properties = new HashMap<String, Object>();
85              properties.put(PropertyIds.NAME, name);
86              properties.put(PropertyIds.OBJECT_TYPE_ID, objectTypeId);
87  
88              // check type
89              TypeDefinition type = session.getTypeDefinition(objectTypeId);
90              if (!(type instanceof DocumentTypeDefinition)) {
91                  addResult(createResult(FAILURE, "Type is not a document type! Type: " + objectTypeId, true));
92                  return;
93              }
94  
95              DocumentTypeDefinition docType = (DocumentTypeDefinition) type;
96              VersioningState versioningState = (Boolean.TRUE.equals(docType.isVersionable()) ? VersioningState.MAJOR
97                      : VersioningState.NONE);
98  
99              // create and fetch the document
100             ObjectId id = session.createDocument(properties, testFolder, contentStream, versioningState);
101             Document doc = (Document) session.getObject(id, SELECT_ALL_NO_CACHE_OC);
102 
103             // check the new document
104             addResult(checkObject(session, doc, getAllProperties(doc), "New document object spec compliance"));
105 
106             // check the size
107             f = createResult(FAILURE, "Content stream length doesn't match the uploaded content!", true);
108             assertEquals(size, doc.getContentStreamLength(), null, f);
109 
110             // delete it by path
111             List<String> paths = doc.getPaths();
112 
113             f = createResult(FAILURE,
114                     "The document must have at least one path because it was created in a folder! Id: " + doc.getId());
115             addResult(assertIsTrue(paths != null && paths.size() > 0, null, f));
116 
117             session.deleteByPath(paths.get(0));
118         } finally {
119             // delete the test folder
120             deleteTestFolder();
121         }
122     }
123 }