Skip to content

Commit a90eb55

Browse files
committed
Added async tests
1 parent ebacc11 commit a90eb55

File tree

6 files changed

+618
-0
lines changed

6 files changed

+618
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Redmine.Net.Api;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using Redmine.Net.Api.Extensions;
9+
using System.Diagnostics;
10+
using Redmine.Net.Api.Types;
11+
using System.Collections.Specialized;
12+
13+
namespace UnitTest_redmine_net40_api
14+
{
15+
[TestClass]
16+
public class IssueTestsAsync
17+
{
18+
private RedmineManager redmineManager;
19+
20+
private const int ISSUE_ID = 1;
21+
private const int USER_ID = 5;
22+
23+
[TestInitialize]
24+
public void Initialize()
25+
{
26+
SetMimeTypeXML();
27+
SetMimeTypeJSON();
28+
}
29+
30+
[Conditional("JSON")]
31+
private void SetMimeTypeJSON()
32+
{
33+
redmineManager = new RedmineManager(Helper.Uri, Helper.ApiKey, MimeFormat.json);
34+
}
35+
36+
[Conditional("XML")]
37+
private void SetMimeTypeXML()
38+
{
39+
redmineManager = new RedmineManager(Helper.Uri, Helper.ApiKey);
40+
}
41+
42+
[TestMethod]
43+
public async Task Should_Add_Watcher_To_Issue()
44+
{
45+
await redmineManager.AddWatcherToIssueAsync(ISSUE_ID, USER_ID);
46+
47+
Issue issue = await redmineManager.GetObjectAsync<Issue>(ISSUE_ID.ToString(), new NameValueCollection { { RedmineKeys.INCLUDE, RedmineKeys.WATCHERS} });
48+
49+
Assert.IsNotNull(issue, "Get issue returned null.");
50+
Assert.IsTrue(issue.Watchers.Count == 1, "Number of watchers != 1");
51+
CollectionAssert.AllItemsAreNotNull(issue.Watchers.ToList(), "Watchers contains null items.");
52+
CollectionAssert.AllItemsAreUnique(issue.Watchers.ToList(), "Watcher items are not unique.");
53+
Assert.IsTrue(((List<Watcher>)issue.Watchers).Find(w => w.Id == USER_ID) != null, "Watcher not added to issue.");
54+
}
55+
56+
[TestMethod]
57+
public async Task Should_Remove_Watcher_From_Issue()
58+
{
59+
await redmineManager.RemoveWatcherFromIssueAsync(ISSUE_ID, USER_ID);
60+
61+
Issue issue = await redmineManager.GetObjectAsync<Issue>(ISSUE_ID.ToString(), new NameValueCollection { { RedmineKeys.INCLUDE, RedmineKeys.WATCHERS } });
62+
63+
Assert.IsTrue(issue.Watchers == null || ((List<Watcher>)issue.Watchers).Find(w => w.Id == USER_ID) == null);
64+
}
65+
}
66+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
12+
namespace UnitTest_redmine_net40_api
13+
{
14+
[TestClass]
15+
public class ProjectMembershipTestsAsync
16+
{
17+
private RedmineManager redmineManager;
18+
19+
private const string PROJECT_ID = "redmine-test";
20+
private const int USER_ID = 5;
21+
private const int ROLE_ID = 36;
22+
23+
[TestInitialize]
24+
public void Initialize()
25+
{
26+
SetMimeTypeXML();
27+
SetMimeTypeJSON();
28+
}
29+
30+
[Conditional("JSON")]
31+
private void SetMimeTypeJSON()
32+
{
33+
redmineManager = new RedmineManager(Helper.Uri, Helper.ApiKey, MimeFormat.json);
34+
}
35+
36+
[Conditional("XML")]
37+
private void SetMimeTypeXML()
38+
{
39+
redmineManager = new RedmineManager(Helper.Uri, Helper.ApiKey);
40+
}
41+
42+
[TestMethod]
43+
public async Task Should_Add_Project_Membership()
44+
{
45+
ProjectMembership pm = new ProjectMembership();
46+
pm.User = new IdentifiableName { Id = USER_ID };
47+
pm.Roles = new List<MembershipRole>();
48+
pm.Roles.Add(new MembershipRole { Id = ROLE_ID });
49+
50+
ProjectMembership updatedPM = await redmineManager.CreateObjectAsync<ProjectMembership>(pm, PROJECT_ID);
51+
52+
Assert.IsNotNull(updatedPM, "Project membership is null.");
53+
}
54+
}
55+
}

UnitTest-redmine-net40-api/UnitTest-redmine-net40-api.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
</Choose>
7575
<ItemGroup>
7676
<Compile Include="AttachmentTests.cs" />
77+
<Compile Include="AttachmentTestsAsync.cs" />
7778
<Compile Include="CustomFieldTests.cs" />
7879
<Compile Include="GroupTests.cs" />
7980
<Compile Include="Helper.cs" />
@@ -82,7 +83,9 @@
8283
<Compile Include="IssueRelationTests.cs" />
8384
<Compile Include="IssueStatusTests.cs" />
8485
<Compile Include="IssueTests.cs" />
86+
<Compile Include="IssueTestsAsync.cs" />
8587
<Compile Include="ProjectMembershipTests.cs" />
88+
<Compile Include="ProjectMembershipTestsAsync.cs" />
8689
<Compile Include="ProjectTests.cs" />
8790
<Compile Include="Properties\AssemblyInfo.cs" />
8891
<Compile Include="NewsTests.cs" />
@@ -91,9 +94,16 @@
9194
<Compile Include="TimeEntryActivityTests.cs" />
9295
<Compile Include="TimeEntryTests.cs" />
9396
<Compile Include="TrackerTests.cs" />
97+
<Compile Include="UserTestsAsync.cs" />
9498
<Compile Include="UserTests.cs" />
9599
<Compile Include="VersionTests.cs" />
96100
<Compile Include="WikiPageTests.cs" />
101+
<Compile Include="WikiPageTestsAsync.cs" />
102+
</ItemGroup>
103+
<ItemGroup>
104+
<None Include="App.config">
105+
<SubType>Designer</SubType>
106+
</None>
97107
</ItemGroup>
98108
<ItemGroup>
99109
<ProjectReference Include="..\redmine-net40-api\redmine-net40-api.csproj">

0 commit comments

Comments
 (0)