View Javadoc
1   /*
2    * Copyright (C) 2003-2015 eXo Platform SAS.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program. If not, see <http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.wiki.jpa.dao;
18  
19  import org.exoplatform.commons.file.model.FileInfo;
20  import org.exoplatform.commons.file.model.FileItem;
21  import org.exoplatform.commons.file.services.FileStorageException;
22  import org.exoplatform.wiki.jpa.BaseWikiJPAIntegrationTest;
23  import org.exoplatform.wiki.jpa.JPADataStorage;
24  import org.exoplatform.wiki.jpa.entity.*;
25  import org.exoplatform.wiki.mow.api.PermissionType;
26  
27  import java.io.ByteArrayInputStream;
28  import java.io.File;
29  import java.io.IOException;
30  import java.net.URISyntaxException;
31  import java.net.URL;
32  import java.nio.file.Files;
33  import java.nio.file.Paths;
34  import java.util.ArrayList;
35  import java.util.Date;
36  import java.util.List;
37  
38  /**
39   * Created by The eXo Platform SAS
40   * Author : eXoPlatform
41   *          exo@exoplatform.com
42   * Jun 25, 2015  
43   */
44  public class PageAttachmentDAOTest extends BaseWikiJPAIntegrationTest {
45  
46    public void testInsertDelete() throws IOException, URISyntaxException {
47      //Given
48      WikiEntity wiki = new WikiEntity();
49      wiki.setType("portal");
50      wiki.setOwner("wiki1");
51      wiki = wikiDAO.create(wiki);
52      PageEntity page = new PageEntity();
53      page.setName("page1");
54      page.setWiki(wiki);
55      page.setCreatedDate(new Date());
56      page.setUpdatedDate(new Date());
57      page = pageDAO.create(page);
58      URL fileResource = this.getClass().getClassLoader().getResource("AGT2010.DimitriBaeli.EnterpriseScrum-V1.2.pdf");
59      PageAttachmentEntity att = new PageAttachmentEntity();
60      FileItem fileItem = null;
61      try {
62         fileItem = new FileItem(null,
63                "AGT2010.DimitriBaeli.EnterpriseScrum-V1.2.pdf",
64                null,
65                JPADataStorage.WIKI_FILES_NAMESPACE_NAME,
66                Files.readAllBytes(Paths.get(fileResource.toURI())).length,
67                new Date(),
68                "marry",
69                false,
70                new ByteArrayInputStream(Files.readAllBytes(Paths.get(fileResource.toURI()))));
71        fileItem = fileService.writeFile(fileItem);
72      } catch (Exception e) {
73        fail();
74      }
75      att.setAttachmentFileID(fileItem.getFileInfo().getId());
76      att.setPage(page);
77      att.setCreatedDate(new Date());
78      //When
79      pageAttachmentDAO.create(att);
80      Long id = att.getId();
81      //Then
82      AttachmentEntity got = pageAttachmentDAO.find(id);
83      try {
84        assertNotNull(fileService.getFile(got.getAttachmentFileID()).getAsByte());
85        assertEquals(new File(fileResource.toURI()).length(), fileService.getFile(got.getAttachmentFileID()).getFileInfo().getSize());
86      } catch (FileStorageException e) {
87        fail();
88      }
89      //Delete
90      pageAttachmentDAO.delete(att);
91      assertNull(pageAttachmentDAO.find(id));
92    }
93  
94    public void testUpdate() throws IOException, URISyntaxException {
95      //Given
96      WikiEntity wiki = new WikiEntity();
97      wiki.setType("portal");
98      wiki.setOwner("wiki1");
99      wiki = wikiDAO.create(wiki);
100     PageEntity page = new PageEntity();
101     page.setName("page1");
102     page.setWiki(wiki);
103     page.setCreatedDate(new Date());
104     page.setUpdatedDate(new Date());
105     page = pageDAO.create(page);
106     URL fileResource = this.getClass().getClassLoader().getResource("AGT2010.DimitriBaeli.EnterpriseScrum-V1.2.pdf");
107     PageAttachmentEntity att = new PageAttachmentEntity();
108     FileItem fileItem = null;
109     try {
110       fileItem = new FileItem(null,
111               "AGT2010.DimitriBaeli.EnterpriseScrum-V1.2.pdf",
112               null,
113               JPADataStorage.WIKI_FILES_NAMESPACE_NAME,
114               Files.readAllBytes(Paths.get(fileResource.toURI())).length,
115               new Date(),
116               "marry",
117               false,
118               new ByteArrayInputStream(Files.readAllBytes(Paths.get(fileResource.toURI()))));
119       fileItem = fileService.writeFile(fileItem);
120     } catch (Exception e) {
121       fail();
122     }
123     att.setAttachmentFileID(fileItem.getFileInfo().getId());
124     att.setPage(page);
125     att.setCreatedDate(new Date());
126     //When
127     pageAttachmentDAO.create(att);
128     Long id = att.getId();
129     PermissionEntity per = new PermissionEntity();
130     per.setIdentity("user");
131     per.setIdentityType("User");
132     per.setPermissionType(PermissionType.ADMINPAGE);
133     List<PermissionEntity> permissions = new ArrayList<>();
134     permissions.add(per);
135     Date date = new Date();
136     fileItem.getFileInfo().setUpdater("creator");
137     fileItem.getFileInfo().setUpdatedDate(date);
138 
139     try {
140       fileService.updateFile(fileItem);
141     } catch (FileStorageException e) {
142       fail();
143     }
144     //Then
145     pageAttachmentDAO.update(att);
146     AttachmentEntity got = pageAttachmentDAO.find(id);
147 
148     FileInfo fileInfo=fileService.getFileInfo(got.getAttachmentFileID());
149     assertEquals("creator", fileInfo.getUpdater());
150     assertEquals(date, fileInfo.getUpdatedDate());
151   }
152 }