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.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Map; 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.codehaus.jackson.type.TypeReference; import org.junit.Test; import com.glodon.paas.document.api.bean.File; import com.glodon.paas.document.api.bean.StringUtil; import com.glodon.paas.document.api.bean.User; import com.glodon.paas.util.UUIDUtil; /** * Test restful api */ public class PerformanceRestAPITest extends AbstractDocumentAPITest { private String shareTo = "1@1.com"; private String tempUploadFile = "c://default.properties"; /** * ? */ @Test public void testGetQuotaOauth() { String url = "/quota"; String response = this.simpleCall(createGet(url)); assertNotNull(response); } /** * ???? */ @Test public void testGetMeta() throws IOException { File file = this.createFile("test1"); String response = simpleCall(createGet("/file/" + file.getId() + "?meta&type=id")); assertNotNull(response); Map<String, File> map = this.convertString2Obj(response, new TypeReference<Map<String, File>>() { }); File resultFile = map.get("meta"); assertEquals(file.getName(), resultFile.getName()); } /** * */ @Test public void testCreateFolder() throws IOException { // create folder test1 String fileName = "test" + UUIDUtil.getUUIDStr(); String url = "/file/" + fileName + "?folder&type=path"; String response = this.simpleCall(this.createPost(url)); assertNotNull(response); File file = this.convertString2Obj(response, new TypeReference<File>() { }); assertTrue(fileName.equalsIgnoreCase(file.getName())); } /** * multipart */ @Test public void testUploadFileForMultiPart() throws IOException { File parentFile = createFile("testUpload"); java.io.File file = new java.io.File("src/test/resources/file/testupload.file"); if (!file.exists()) { file = new java.io.File(tempUploadFile); } 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); String response = simpleCall(post); assertNotNull(response); File resultFile = convertString2Obj(response, new TypeReference<File>() { }); assertEquals(file.getName(), resultFile.getFullName()); assertTrue(file.length() == resultFile.getSize()); } /** * octet-stream */ @Test public void testUploadFileForOctet() throws IOException { // File tempFile=createFile("testUpload"); java.io.File file = new java.io.File("src/test/resources/file/testupload.file"); if (!file.exists()) { file = new java.io.File(tempUploadFile); } 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 getUploadBytes() throws IOException { File tempFile = createFile("testUpload"); java.io.File file = new java.io.File("src/test/resources/file/testupload.file"); if (!file.exists()) { file = new java.io.File(tempUploadFile); } 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>() { }); //get bytes String url = "/file/" + resultFile.getId() + "?bytes&name=" + resultFile.getName() + "&size=" + resultFile.getSize() + "&type=id]"; response = simpleCall(createGet(url)); //assertEquals("{\"0\":0}",response.trim()); assertNotNull(response); } /** * */ @Test public void testDownloadFile() throws IOException { File tempFile = createFile("testUpload"); java.io.File file = new java.io.File("src/test/resources/file/testupload.file"); if (!file.exists()) { file = new java.io.File(tempUploadFile); } 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>() { }); //download String url = "/file/" + resultFile.getId() + "?content&type=id"; HttpGet get = createGet(url); HttpResponse responseResult = call(get); HttpEntity entityResult = responseResult.getEntity(); assertTrue(resultFile.getSize() == entityResult.getContentLength()); } /** * */ @SuppressWarnings("unchecked") @Test public void testDeleteFolder() throws IOException { // create folder test2 File tempFile = createFile("test2"); // delete folder String url = "/file/" + tempFile.getName() + "?purge&type=path"; String response = this.simpleCall(this.createDelete(url)); assertNotNull(response); Map<String, Object> map = this.convertString2Obj(response, new TypeReference<Map<String, Object>>() { }); assertEquals(1, ((ArrayList<String>) map.get("successItems")).size()); } /** * ? */ @Test public void testGetDeleteFileList() throws IOException { // File tempFile = this.createFile("test1"); // this.deleteFileWithoutPurge(tempFile.getName()); // String response=this.simpleCall(this.createGet("/recycler")); // Map<String, File[]> map = this.convertString2Obj(response, new TypeReference<Map<String, File[]>>(){} ); // assertTrue(map.get("items").length>0); } /** * ?? */ @Test public void testGetChildFile() throws IOException { // create parent and child folder File tempFile = this.createFile("parent/child"); // get children String response = this.simpleCall(this.createGet("/file/parent?child&type=path")); assertNotNull(response); Map<String, File[]> map = convertString2Obj(response, new TypeReference<Map<String, File[]>>() { }); assertTrue(map.get("items").length >= 1); } /** * ? */ @Test public void testGetFilePath() throws IOException { String filePath = "parent/child/test"; File temFile = createFile(filePath); String response = this.simpleCall(this.createGet("/file/parent/child?path&root=parent&type=path")); assertNotNull(response); Map<String, String[]> map = convertString2Obj(response, new TypeReference<Map<String, String[]>>() { }); assertTrue(map.get("path").length > 1); } /** * ? */ @Test public void testGetDeleteFile() throws IOException { // File file = this.createFile("test1"); // this.deleteFileWithoutPurge(file.getName()); // String response=this.simpleCall(this.createGet("/recycler/"+file.getName()+"?type=path")); // Map<String, File[]> map = this.convertString2Obj(response, new TypeReference<Map<String, File[]>>(){} ); // assertTrue(map.get("items").length>0); // response=this.simpleCall(this.createGet("/recycler/"+file.getId()+"?type=id")); // Map<String, File> meta = this.convertString2Obj(response, new TypeReference<Map<String, File>>(){} ); // assertEquals(file.getName(),meta.get("meta").getName()); } /** * */ @Test public void testDeleteFromRecycler() throws IOException { // File file = this.createFile("test1"); // this.deleteFileWithoutPurge(file.getName()); // String response=this.simpleCall(this.createGet("/recycler/"+file.getId()+"?type=id")); // Map<String, File> meta = this.convertString2Obj(response, new TypeReference<Map<String, File>>(){} ); // assertEquals(file.getName(),meta.get("meta").getName()); // response = this.simpleCall(this.createDelete("/recycler/"+file.getId()+"?type=id")); // assertNotNull(response); // Map<String, String[]> map=convertString2Obj(response,new TypeReference<Map<String, String[]>>(){}); // String[] sucess = map.get("successItems"); // String[] failed = map.get("failedItems"); // if(sucess!=null&&sucess.length>0){ // assertEquals(file.getId(),sucess[0]); // }else if(failed!=null&&failed.length>0){ // assertEquals(file.getId(),failed[0]); // }else{ // fail(); // } } /** * */ @Test public void testRestoreFromRecycler() throws IOException { // File file = this.createFile("test1"); // this.deleteFileWithoutPurge(file.getName()); // String response = this.simpleCall(this.createPut("/recycler/"+file.getId()+"?type=id")); // Map<String, Object> meta = this.convertString2Obj(response, new TypeReference<Map<String, Object>>(){} ); // assertEquals(file.getId(),((ArrayList<String>)meta.get("successItems")).get(0)); } /** * */ @Test public void testClearCycler() throws IOException { // File file = this.createFile("test1"); // this.deleteFileWithoutPurge(file.getName()); // String response = this.simpleCall(this.createDelete("/recycler")); // Map<String, String> map = convertString2Obj(response, new TypeReference<Map<String, String>>(){}); // assertEquals("success",map.get("result")); // response = simpleCall(createGet("/file/"+file.getId()+"?meta&type=id")); // assertNull(response); } /** * ?? */ @Test public void testRename() throws IOException { String destName = "test2" + UUIDUtil.getUUIDStr(); File tempFile = createFile("test1"); String response = this.simpleCall( this.createPut("/file/" + tempFile.getName() + "?rename&name=" + destName + "&type=path")); assertNotNull(response); File file = new File(); file = this.convertString2Obj(response, new TypeReference<File>() { }); assertEquals(destName, file.getName()); } /** * */ @Test public void testShareFile() throws IOException { File file = createFile("testshare"); String shareUrl = "/share/" + file.getId() + "?sharer=" + shareTo; String response = simpleCall(createPost(shareUrl)); assertNotNull(response); Map<String, String[]> map = convertString2Obj(response, new TypeReference<Map<String, String[]>>() { }); String[] success = map.get("successItems"); String[] failed = map.get("failedItems"); if (success != null && success.length > 0) { assertEquals(shareTo, success[0]); } if (failed != null && failed.length > 0) { assertEquals(shareTo, failed[0]); } } /** * ? */ @Test public void testDeleteShareFile() throws IOException { File file = createFile("share"); String shareUrl = "/share/" + file.getId() + "?sharer=" + shareTo; String response = simpleCall(createPost(shareUrl)); assertNotNull(response); Map<String, String[]> map = convertString2Obj(response, new TypeReference<Map<String, String[]>>() { }); assertTrue(shareTo.equals(map.get("successItems")[0]) || shareTo.equals(map.get("failedItems")[0])); //delete share response = simpleCall(createDelete(shareUrl)); Map<String, String[]> mapResult = convertString2Obj(response, new TypeReference<Map<String, String[]>>() { }); assertTrue(shareTo.equals(mapResult.get("successItems")[0]) || shareTo.equals(mapResult.get("failedItems")[0])); } /** * ? */ @Test public void testOutcomingShareFileList() throws IOException { File file = createFile("testshare"); String shareUrl = "/share/" + file.getId() + "?sharer=" + shareTo; String response = simpleCall(createPost(shareUrl)); assertNotNull(response); Map<String, String[]> map = convertString2Obj(response, new TypeReference<Map<String, String[]>>() { }); String[] success = map.get("successItems"); if (success != null) { assertEquals(shareTo, success[0]); } String shareListUrl = "/share"; response = simpleCall(createGet(shareListUrl)); Map<String, File[]> mapFile = convertString2Obj(response, new TypeReference<Map<String, File[]>>() { }); assertEquals(file.getId(), mapFile.get("items")[0].getId()); } /** * ? */ @Test public void testIncomingShareFileList() throws IOException { //create folder by shareTo String fileName = "file_test_" + UUIDUtil.getUUIDStr(); String url = "/file/" + fileName + "?folder&type=path"; HttpPost post = new HttpPost(prefix + url); post.setHeader("UserId", shareTo); String response = this.simpleCall(post); File file = convertString2Obj(response, new TypeReference<File>() { }); //share file to login user String sharer = user; String shareUrl = "/share/" + file.getId() + "?sharer=" + sharer; post = new HttpPost(prefix + shareUrl); post.setHeader("UserId", shareTo); response = simpleCall(post); assertNotNull(response); //Get share list String inShareUrl = "/incoming"; response = simpleCall(createGet(inShareUrl)); assertNotNull(response); Map<String, File[]> mapFile = convertString2Obj(response, new TypeReference<Map<String, File[]>>() { }); assertEquals(file.getId(), mapFile.get("items")[0].getId()); } /** * ? */ @Test public void testSharer() throws IOException { //create folder by shareTo String fileName = "file_test_" + UUIDUtil.getUUIDStr(); String url = "/file/" + fileName + "?folder&type=path"; HttpPost post = new HttpPost(prefix + url); post.setHeader("UserId", shareTo); String response = this.simpleCall(post); File file = convertString2Obj(response, new TypeReference<File>() { }); //share file to login user String sharer = user; String shareUrl = "/share/" + file.getId() + "?sharer=" + sharer; post = new HttpPost(prefix + shareUrl); post.setHeader("UserId", shareTo); response = simpleCall(post); //get sharer url = "/share/" + file.getId() + "?sharer"; HttpGet get = new HttpGet(prefix + url); get.setHeader("UserId", shareTo); response = simpleCall(get); assertNotNull(response); Map<String, User[]> mapFile = convertString2Obj(response, new TypeReference<Map<String, User[]>>() { }); assertEquals(sharer, mapFile.get("users")[0].getEmail()); } /** * ? */ @Test public void testDeleteIncomingShare() throws IOException { //create folder by shareTo String fileName = "file_test_" + UUIDUtil.getUUIDStr(); String url = "/file/" + fileName + "?folder&type=path"; HttpPost post = new HttpPost(prefix + url); post.setHeader("UserId", shareTo); String response = this.simpleCall(post); File file = convertString2Obj(response, new TypeReference<File>() { }); //share file to login user String sharer = user; String shareUrl = "/share/" + file.getId() + "?sharer=" + sharer; post = new HttpPost(prefix + shareUrl); post.setHeader("UserId", shareTo); response = simpleCall(post); //Delete share url = "/incoming/" + file.getId(); response = simpleCall(createDelete(url)); assertNotNull(response); Map<String, String[]> map = convertString2Obj(response, new TypeReference<Map<String, String[]>>() { }); assertTrue(!map.isEmpty()); assertTrue(map.containsKey("successItems")); assertTrue(map.containsKey("failedItems")); } /** * */ @Test public void testPublicFile() throws IOException { File file = createFile("testPublic"); String url = "/public/" + file.getId(); String response = this.simpleCall(createPost(url)); assertNotNull(response); Map<String, String> map = convertString2Obj(response, new TypeReference<Map<String, String>>() { }); assertTrue(!StringUtil.isBlank(map.get("publicToken"))); assertTrue(!StringUtil.isBlank(map.get("result"))); } /** * ? */ @Test public void testGetPublicFileList() throws IOException { File file = createFile("testPublic"); String url = "/public/" + file.getId(); call(createPost(url)); //get public list url = "/public"; String response = simpleCall(createGet(url)); assertNotNull(response); Map<String, File[]> map = convertString2Obj(response, new TypeReference<Map<String, File[]>>() { }); assertTrue(map.get("items").length > 0); } /** * ? */ @Test public void testCancelPublic() throws IOException { File file = createFile("testPublic"); String url = "/public/" + file.getId(); call(createPost(url)); //cancel Public url = "/public/" + file.getId(); String response = simpleCall(createDelete(url)); assertNotNull(response); Map<String, String> map = convertString2Obj(response, new TypeReference<Map<String, String>>() { }); assertEquals("success", map.get("result")); } /** * ? */ @Test public void testLockFile() throws IOException { //create project String projectName = "project_" + UUIDUtil.getUUIDStr(); File project = null; try { project = createSimpleProject(projectName); } catch (UnsupportedEncodingException e1) { fail(); } //upload file java.io.File file = new java.io.File("src/test/resources/file/testupload.file"); if (!file.exists()) { file = new java.io.File(tempUploadFile); } String uploadUrl = "/file/" + project.getId() + "?upload&position=0&type=id&fileName=" + file.getName(); 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>() { }); //lock file String url = "/file/" + resultFile.getId() + "?lock&type=id"; response = simpleCall(createPut(url)); assertNotNull(response); Map<String, Object> map = convertString2Obj(response, new TypeReference<Map<String, Object>>() { }); assertTrue(map.containsKey("lockTime")); assertTrue(map.containsKey("owner")); } /** * ? */ @Test public void testUnlockFile() throws IOException { //create project String projectName = "project_" + UUIDUtil.getUUIDStr(); File project = null; try { project = createSimpleProject(projectName); } catch (UnsupportedEncodingException e1) { fail(); } //upload file java.io.File file = new java.io.File("src/test/resources/file/testupload.file"); if (!file.exists()) { file = new java.io.File(tempUploadFile); } String uploadUrl = "/file/" + project.getId() + "?upload&position=0&type=id&fileName=" + file.getName(); 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>() { }); //lock file String url = "/file/" + resultFile.getId() + "?lock&type=id"; simpleCall(createPut(url)); //unlock file url = "/file/" + resultFile.getId() + "?unlock&type=id"; response = simpleCall(createPut(url)); assertNotNull(response); Map<String, String> map = convertString2Obj(response, new TypeReference<Map<String, String>>() { }); assertEquals("success", map.get("result")); } /** * ??? */ @Test public void testGetFilePrivilege() throws IOException { //create project String projectName = "project_" + UUIDUtil.getUUIDStr(); File project = null; try { project = createSimpleProject(projectName); } catch (UnsupportedEncodingException e1) { fail(); } //Get privilege String url = "/file/" + project.getId() + "?privilege&type=id"; String response = simpleCall(createGet(url)); Map<String, String[]> map = convertString2Obj(response, new TypeReference<Map<String, String[]>>() { }); assertTrue(map.get("privilege").length == 4); } }