Java tutorial
/* * Copyright 2012-2014 glodon paas All right reserved. This software is the confidential and proprietary information of * glodon paas ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only * in accordance with the terms of the license agreement you entered into with glodon paas. */ package com.glodon.paas.document.api; import static org.junit.Assert.*; import java.io.IOException; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import net.sf.json.JSONObject; import org.apache.commons.collections.CollectionUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.FileEntity; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.message.BasicHeader; import org.codehaus.jackson.type.TypeReference; import org.junit.Test; import org.springframework.core.io.ClassPathResource; import com.glodon.paas.consts.StringConst; import com.glodon.paas.document.api.bean.File; import com.glodon.paas.dto.DataTransitionObject; /** * FileRestAPITest.java??file and project file rest api UT test * * @author admin 2013-4-12 ?4:01:15 */ public class FileRestAPITest extends AbstractDocumentAPITest { private String urlPrefix = "http://" + host + ":" + port + prefix; @Test public void createFolder() throws Exception { String str = simpleCall(createPost("/file/a/b/c?folder")); File c = getFile(str, null); assertEquals("c", c.getName()); } @Test public void uploadFileForMultiPart() throws IOException { File parentFile = getFile(simpleCall(createPost("/file/1?folder")), null); java.io.File file = new ClassPathResource("file/testupload.file").getFile(); String uploadUrl = "/file/" + file.getName() + "?upload&position=0&type=path"; HttpPost post = createPost(uploadUrl); FileBody fileBody = new FileBody(file); StringBody sbId = new StringBody(parentFile.getId()); StringBody sbSize = new StringBody(String.valueOf(file.length())); StringBody sbName = new StringBody(file.getName()); StringBody sbPosition = new StringBody("0"); MultipartEntity entity = new MultipartEntity(); entity.addPart("fileId", sbId); entity.addPart("size", sbSize); entity.addPart("fileName", sbName); entity.addPart("position", sbPosition); entity.addPart("file", fileBody); post.setEntity(entity); File uploadFile = getFile(simpleCall(post), null); assertEquals(file.getName(), uploadFile.getFullName()); assertTrue(file.length() == uploadFile.getSize()); assertEquals(parentFile.getId(), uploadFile.getParentId()); } @Test public void uploadFolderForMultiPart() throws IOException { File parentFile = getFile(simpleCall(createPost("/file/1?folder")), null); java.io.File file = new ClassPathResource("file/testupload.file").getFile(); String uploadUrl = "/file/" + file.getName() + "?upload&position=0&type=path"; HttpPost post = createPost(uploadUrl); FileBody fileBody = new FileBody(file); StringBody sbId = new StringBody(parentFile.getId()); StringBody sbSize = new StringBody(String.valueOf(file.length())); StringBody sbName = new StringBody("aa/" + file.getName()); StringBody sbPosition = new StringBody("0"); MultipartEntity entity = new MultipartEntity(); entity.addPart("fileId", sbId); entity.addPart("size", sbSize); entity.addPart("fileName", sbName); entity.addPart("position", sbPosition); entity.addPart("file", fileBody); post.setEntity(entity); File uploadFile = getFile(simpleCall(post), null); assertEquals(file.getName(), uploadFile.getFullName()); assertFalse(uploadFile.isFolder()); File aaFolder = getFile(simpleCall(createGet("/file/1/aa?meta")), "meta"); assertEquals(aaFolder.getId(), uploadFile.getParentId()); } @Test public void testUploadFileForOctet() throws IOException { java.io.File file = new ClassPathResource("file/testupload.file").getFile(); String uploadUrl = "/file/" + file.getName() + "?upload&position=0&type=path"; HttpPost post = createPost(uploadUrl); post.setHeader("content-type", "application/octet-stream"); FileEntity entity = new FileEntity(file); post.setEntity(entity); String response = simpleCall(post); assertNotNull(response); File resultFile = convertString2Obj(response, new TypeReference<File>() { }); assertEquals(file.getName(), resultFile.getFullName()); assertTrue(file.length() == resultFile.getSize()); } @Test public void getMeta() throws Exception { simpleCall(createPost("/file/a/b/c?folder")); String str = simpleCall(createGet("/file/a/b/c?meta")); File c = getFile(str, "meta"); assertEquals("c", c.getName()); } @Test public void getChild() throws Exception { simpleCall(createPost("/file/a/b/c?folder")); String str = simpleCall(createGet("/file/a?child")); List<File> fs = getFiles(str); assertTrue(fs.size() > 0); } @Test public void getUpTree() throws Exception { uploadFile("/file/a/b/c.log"); simpleCall(createPost("/file/a/b/c1?folder")); simpleCall(createPost("/file/a/b/c2?folder")); simpleCall(createPost("/file/a/b1?folder")); simpleCall(createPost("/file/a/b2?folder")); simpleCall(createPost("/file/a1?folder")); simpleCall(createPost("/file/a2?folder")); String str = simpleCall(createGet("/file/a/b/c.log?upTree")); File tree = getFile(str, "upTree"); assertEquals("", tree.getName()); List<File> aFiles = tree.getFiles(); boolean hasFolderA = false; boolean hasFolderB = false; for (File a : aFiles) { if (a.getFullName().equals("a")) { hasFolderA = true; assertTrue(a.isHasFolder()); assertEquals(a.getFolderCount(), 3); assertTrue(a.getFiles().size() >= 3); List<File> bFiles = a.getFiles(); for (File b : bFiles) { if (b.getFullName().equals("b")) { hasFolderB = true; assertTrue(b.isHasFolder()); assertTrue(b.getFolderCount() >= 2); assertTrue(b.getFiles().size() >= 3); List<File> cFiles = b.getFiles(); for (File c : cFiles) { assertTrue(c.getFullName().startsWith("c")); assertTrue(!c.isHasFolder()); assertEquals(c.getFolderCount(), 0); assertEquals(0, c.getFiles().size()); if (c.getFullName().equals("c.log")) { assertTrue(!c.isHasFolder()); assertEquals(c.getFolderCount(), 0); assertEquals(0, c.getFiles().size()); } } } } } } assertTrue(hasFolderA); assertTrue(hasFolderB); } @Test public void getPath() throws Exception { File c = getFile(simpleCall(createPost("/file/1/2/3?folder")), null); String str = simpleCall(createGet("/file/" + c.getId() + "?path")); Map<String, List<DataTransitionObject>> map = this.convertString2Obj(str, new TypeReference<Map<String, List<DataTransitionObject>>>() { }); List<DataTransitionObject> nvs = map.get("path"); assertEquals("/1/2/3", "/" + nvs.get(0).getName() + "/" + nvs.get(1).getName() + "/" + nvs.get(2).getName()); } @Test public void getPrivilege() throws Exception { File c = getFile(simpleCall(createPost("/file/1/2/3?folder")), null); String str = simpleCall(createGet("/file/" + c.getId() + "?privilege=owner")); Map<String, String> map = this.convertString2Obj(str, new TypeReference<Map<String, String>>() { }); assertEquals(StringConst.TRUE, map.get("privilege")); str = simpleCall(createGet("/file/" + c.getId() + "?privilege=sharer")); map = this.convertString2Obj(str, new TypeReference<Map<String, String>>() { }); assertEquals(StringConst.FALSE, map.get("privilege")); // simpleCall(createPost("/share/"+c.getId()+"?sharer=1@1.com")); shareFile("1@1.com", c.getId()); str = simpleCall( createGet("/file/" + c.getId() + "?privilege=sharer", new BasicHeader("userId", "1@1.com"))); map = this.convertString2Obj(str, new TypeReference<Map<String, String>>() { }); assertEquals(StringConst.TRUE, map.get("privilege")); } private void shareFile(String sharer, String fileId) { JSONObject params = new JSONObject(); params.put("sharer", sharer); String url = urlPrefix + "/share/" + fileId; JSONObject result = this.callPost(url, null, params, JSONObject.class); assertNotNull(result); String urlAccept = urlPrefix + "/incoming?accept&id=" + fileId + "&shareStatus=ACCEPT"; Map<String, String> header = new HashMap<String, String>(); header.put("userId", "1@1.com"); JSONObject acceptResult = this.callPut(urlAccept, header, null, JSONObject.class); assertNotNull(acceptResult); } @Test public void getProjectFilePrivilege() throws Exception { File pFile = createSimpleProject(""); File c = getFile( simpleCall(createPost("/project/" + pFile.getProjectId() + "/file/1/2/3?folder")), null); String str = simpleCall(createGet("/file/" + pFile.getId() + "?privilege")); Map<String, String[]> map = this.convertString2Obj(str, new TypeReference<Map<String, String[]>>() { }); Set<String> pv = new HashSet<String>(); CollectionUtils.addAll(pv, map.get("privilege")); assertEquals(4, pv.size()); assertTrue(pv.contains("x")); assertTrue(pv.contains("r")); assertTrue(pv.contains("w")); assertTrue(pv.contains("d")); str = simpleCall(createGet("/file/" + c.getId() + "?privilege")); map = this.convertString2Obj(str, new TypeReference<Map<String, String[]>>() { }); pv = new HashSet<String>(); CollectionUtils.addAll(pv, map.get("privilege")); assertEquals(3, pv.size()); assertTrue(pv.contains("i-r")); assertTrue(pv.contains("i-w")); assertTrue(pv.contains("i-d")); } @Test public void getBytes() throws Exception { //TODO ? ?? } @Test public void downloadFile() throws Exception { uploadFile("/file/a/b.log"); HttpResponse responseResult = call(createGet("/file/a/b.log?content")); HttpEntity entityResult = responseResult.getEntity(); assertTrue(entityResult.getContentLength() > 0); assertEquals("Content-Disposition: attachment; filename=\"b.log\"", responseResult.getHeaders("Content-Disposition")[0].toString()); } @Test public void downloadFileRange() throws Exception { uploadFile("/file/a/b.log"); java.io.File file = new ClassPathResource("file/testupload.file").getFile(); HttpGet get = createGet("/file/a/b.log?content"); get.addHeader("Range", "bytes=2-" + file.length()); HttpResponse responseResult = call(get); HttpEntity entityResult = responseResult.getEntity(); assertTrue(entityResult.getContentLength() > 0); assertEquals("Content-Disposition: attachment; filename=\"b.log\"", responseResult.getHeaders("Content-Disposition")[0].toString()); } @Test public void downloadFolder() throws Exception { uploadFile("/file/a/b.log"); HttpResponse responseResult = call(createGet("/file/a?content")); assertEquals("Content-Disposition: attachment; filename=\"a.zip\"", responseResult.getHeaders("Content-Disposition")[0].toString()); } @Test public void rename() throws Exception { File c = getFile(simpleCall(createPost("/file/1/2/3?folder")), null); File newFile = getFile(simpleCall(createPut("/file/" + c.getId() + "?rename&name=4")), null); assertEquals("4", newFile.getName()); } @Test public void move() throws Exception { File m1 = getFile(simpleCall(createPost("/file/1/2/1?folder")), null); File m2 = getFile(simpleCall(createPost("/file/1/2/2?folder")), null); File m3 = getFile(simpleCall(createPost("/file/1/2/3?folder")), null); simpleCall(createPut("/file/" + m1.getId() + "," + m2.getId() + "?move&target=" + m3.getId())); m1 = getFile(simpleCall(createGet("/file/" + m1.getId() + "?meta")), "meta"); m2 = getFile(simpleCall(createGet("/file/" + m2.getId() + "?meta")), "meta"); assertEquals(m3.getId(), m1.getParentId()); assertEquals(m3.getId(), m2.getParentId()); } @Test public void restore() throws Exception { File m1 = getFile(simpleCall(createPost("/file/1?folder")), null); File m2 = getFile(simpleCall(createPost("/file/1/2?folder")), null); File m3 = getFile(simpleCall(createPost("/file/1/2/3?folder")), null); File m4 = getFile(simpleCall(createPost("/file/1/2/4?folder")), null); simpleCall(createDelete("/file/" + m1.getId())); m1 = getFile(simpleCall(createGet("/file/" + m1.getId() + "?meta")), "meta"); m2 = getFile(simpleCall(createGet("/file/" + m2.getId() + "?meta")), "meta"); assertTrue(m1.isDeleted()); assertTrue(m2.isDeleted()); simpleCall(createPut("/file/" + m3.getId() + "?restore")); m1 = getFile(simpleCall(createGet("/file/" + m1.getId() + "?meta")), "meta"); m2 = getFile(simpleCall(createGet("/file/" + m2.getId() + "?meta")), "meta"); m3 = getFile(simpleCall(createGet("/file/" + m3.getId() + "?meta")), "meta"); m4 = getFile(simpleCall(createGet("/file/" + m4.getId() + "?meta")), "meta"); assertFalse(m1.isDeleted()); assertFalse(m2.isDeleted()); assertFalse(m3.isDeleted()); assertTrue(m4.isDeleted()); } @Test public void lock() throws Exception { File p = createSimpleProject(""); File lockFile = getFile(uploadFile("/project/" + p.getProjectId() + "/file/lock.log"), null); assertNull(lockFile.getLock()); simpleCall(createPut("/project/" + p.getProjectId() + "/file/" + lockFile.getId() + "?lock")); lockFile = getFile(simpleCall(createGet("/project/" + p.getProjectId() + "/file/lock.log?meta")), "meta"); assertNotNull(lockFile.getLock()); assertEquals("c@c.com", lockFile.getLock().getOwner().getEmail()); } @Test public void unLock() throws Exception { File p = createSimpleProject(""); File lockFile = getFile(uploadFile("/project/" + p.getProjectId() + "/file/lock.log"), null); assertNull(lockFile.getLock()); simpleCall(createPut("/project/" + p.getProjectId() + "/file/" + lockFile.getId() + "?lock")); lockFile = getFile(simpleCall(createGet("/project/" + p.getProjectId() + "/file/lock.log?meta")), "meta"); assertNotNull(lockFile.getLock()); assertEquals("c@c.com", lockFile.getLock().getOwner().getEmail()); simpleCall(createPut("/project/" + p.getProjectId() + "/file/" + lockFile.getId() + "?unlock")); lockFile = getFile(simpleCall(createGet("/project/" + p.getProjectId() + "/file/lock.log?meta")), "meta"); assertNull(lockFile.getLock()); } @Test public void remove() throws Exception { File t1 = getFile(uploadFile("/file/test1.log"), null); File t2 = getFile(uploadFile("/file/test2.log"), null); File t3 = getFile(uploadFile("/file/test3.log"), null); assertFalse(t1.isDeleted()); assertFalse(t2.isDeleted()); assertFalse(t3.isDeleted()); String str = simpleCall(createDelete("/file/" + t1.getId() + "," + t2.getId() + "," + t3.getId())); Map<String, List<String>> map = this.convertString2Obj(str, new TypeReference<Map<String, List<String>>>() { }); assertEquals(3, (map.get(StringConst.SUCCESS_ITEMS)).size()); assertEquals(0, (map.get(StringConst.FAILED_ITEMS)).size()); File dt1 = getFile(simpleCall(createGet("/file/test1.log?meta")), "meta"); File dt2 = getFile(simpleCall(createGet("/file/test2.log?meta")), "meta"); File dt3 = getFile(simpleCall(createGet("/file/test3.log?meta")), "meta"); assertEquals(t1.getId(), dt1.getId()); assertTrue(dt1.isDeleted()); assertEquals(t2.getId(), dt2.getId()); assertTrue(dt2.isDeleted()); assertEquals(t3.getId(), dt3.getId()); assertTrue(dt3.isDeleted()); } @Test public void purge() throws Exception { File t1 = getFile(uploadFile("/file/test1.log"), null); File t2 = getFile(uploadFile("/file/test2.log"), null); File t3 = getFile(uploadFile("/file/test3.log"), null); assertFalse(t1.isDeleted()); assertFalse(t2.isDeleted()); assertFalse(t3.isDeleted()); String str = simpleCall( createDelete("/file/" + t1.getId() + "," + t2.getId() + "," + t3.getId() + "?purge")); Map<String, List<String>> map = this.convertString2Obj(str, new TypeReference<Map<String, List<String>>>() { }); assertEquals(3, (map.get(StringConst.SUCCESS_ITEMS)).size()); assertEquals(0, (map.get(StringConst.FAILED_ITEMS)).size()); str = simpleCall(createGet("/file/test1.log?meta")); assertNull(str); } }