|
| 1 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 2 | +using Redmine.Net.Api; |
| 3 | +using Redmine.Net.Api.Types; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Redmine.Net.Api.Extensions; |
| 11 | +using System.IO; |
| 12 | +using System.Collections.Specialized; |
| 13 | + |
| 14 | +namespace UnitTest_redmine_net40_api |
| 15 | +{ |
| 16 | + [TestClass] |
| 17 | + public class AttachmentTestsAsync |
| 18 | + { |
| 19 | + private RedmineManager redmineManager; |
| 20 | + |
| 21 | + const string ATTACHMENT_LOCAL_PATH = "E:\\uploadAttachment.txt"; |
| 22 | + const string ATTACHMENT_NAME = "AttachmentUploaded.txt"; |
| 23 | + const string ATTACHMENT_DESCRIPTION = "File uploaded using REST API"; |
| 24 | + const string ATTACHMENT_CONTENT_TYPE = "text/plain"; |
| 25 | + const int PROJECT_ID = 1; |
| 26 | + const string ISSUE_SUBJECT = "Issue with attachments"; |
| 27 | + |
| 28 | + const string ATTACHMENT_ID = "1"; |
| 29 | + const string ATTACHMENT_FILE_NAME = "AttachmentUploaded.txt"; |
| 30 | + |
| 31 | + [TestInitialize] |
| 32 | + public void Initialize() |
| 33 | + { |
| 34 | + SetMimeTypeXML(); |
| 35 | + SetMimeTypeJSON(); |
| 36 | + } |
| 37 | + |
| 38 | + [Conditional("JSON")] |
| 39 | + private void SetMimeTypeJSON() |
| 40 | + { |
| 41 | + redmineManager = new RedmineManager(Helper.Uri, Helper.ApiKey, MimeFormat.json); |
| 42 | + } |
| 43 | + |
| 44 | + [Conditional("XML")] |
| 45 | + private void SetMimeTypeXML() |
| 46 | + { |
| 47 | + redmineManager = new RedmineManager(Helper.Uri, Helper.ApiKey); |
| 48 | + } |
| 49 | + |
| 50 | + [TestMethod] |
| 51 | + public async Task Should_Get_Attachment_By_Id() |
| 52 | + { |
| 53 | + var attachment = await redmineManager.GetObjectAsync<Attachment>(ATTACHMENT_ID, null); |
| 54 | + |
| 55 | + Assert.IsNotNull(attachment, "Get attachment returned null."); |
| 56 | + Assert.IsInstanceOfType(attachment, typeof(Attachment), "Downloaded object is not of type attachment."); |
| 57 | + Assert.IsTrue(attachment.FileName == ATTACHMENT_FILE_NAME, "Attachment file name is not the expected one."); |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + [TestMethod] |
| 62 | + public async Task Should_Upload_Attachment() |
| 63 | + { |
| 64 | + //read document from specified path |
| 65 | + byte[] documentData = File.ReadAllBytes(ATTACHMENT_LOCAL_PATH); |
| 66 | + |
| 67 | + //upload attachment to redmine |
| 68 | + Upload attachment = await redmineManager.UploadFileAsync(documentData); |
| 69 | + |
| 70 | + //set attachment properties |
| 71 | + attachment.FileName = ATTACHMENT_NAME; |
| 72 | + attachment.Description = ATTACHMENT_DESCRIPTION; |
| 73 | + attachment.ContentType = ATTACHMENT_CONTENT_TYPE; |
| 74 | + |
| 75 | + //create list of attachments to be added to issue |
| 76 | + IList<Upload> attachments = new List<Upload>(); |
| 77 | + attachments.Add(attachment); |
| 78 | + |
| 79 | + Issue issue = new Issue(); |
| 80 | + issue.Project = new Project { Id = PROJECT_ID }; |
| 81 | + issue.Subject = ISSUE_SUBJECT; |
| 82 | + issue.Uploads = attachments; |
| 83 | + |
| 84 | + //create issue and attach document |
| 85 | + Issue issueWithAttachment = await redmineManager.CreateObjectAsync<Issue>(issue); |
| 86 | + |
| 87 | + issue = await redmineManager.GetObjectAsync<Issue>(issueWithAttachment.Id.ToString(), new NameValueCollection { { RedmineKeys.INCLUDE, RedmineKeys.ATTACHMENTS } }); |
| 88 | + |
| 89 | + Assert.IsNotNull(issue, "Get created issue returned null."); |
| 90 | + Assert.IsNotNull(issue.Attachments, "Attachments list is null."); |
| 91 | + CollectionAssert.AllItemsAreNotNull(issue.Attachments.ToList(), "Attachments list contains null items."); |
| 92 | + CollectionAssert.AllItemsAreInstancesOfType(issue.Attachments.ToList(), typeof(Attachment), "Attachments contains items of unexpected type."); |
| 93 | + Assert.IsTrue(issue.Attachments.Count == 1, "Number of attachments != 1"); |
| 94 | + Assert.IsTrue(issue.Attachments[0].FileName == ATTACHMENT_NAME, "Attachment name is not correct."); |
| 95 | + Assert.IsTrue(issue.Attachments[0].Description == ATTACHMENT_DESCRIPTION, "Attachment description is not correct."); |
| 96 | + Assert.IsTrue(issue.Attachments[0].ContentType == ATTACHMENT_CONTENT_TYPE, "Attachment content type is not correct."); |
| 97 | + } |
| 98 | + |
| 99 | + [TestMethod] |
| 100 | + public async Task Sould_Download_Attachment() |
| 101 | + { |
| 102 | + var url = Helper.Uri + "/attachments/download/" + ATTACHMENT_ID + "/" + ATTACHMENT_FILE_NAME; |
| 103 | + |
| 104 | + var document = await redmineManager.DownloadFileAsync(url); |
| 105 | + |
| 106 | + Assert.IsNotNull(document, "Downloaded file is null."); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments