This repository has been archived by the owner on Jan 14, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathErrorAttachmentLogTest.cs
187 lines (166 loc) · 6.61 KB
/
ErrorAttachmentLogTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Text;
using Microsoft.AppCenter.Ingestion.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Microsoft.AppCenter.Crashes.Test.Windows
{
[TestClass]
public class ErrorAttachmentLogTest
{
[TestMethod]
public void TestCreateBinaryAttachmentLog()
{
// Create an error attachment log.
var contentType = "mime/type";
var data = new byte[] { 0, 3, 2, 1, 0 };
var fileName = "binary attachment";
var attachment = ErrorAttachmentLog.AttachmentWithBinary(data, fileName, contentType);
// Verify the contents.
Assert.AreEqual(contentType, attachment.ContentType);
Assert.AreEqual(fileName, attachment.FileName);
CollectionAssert.AreEqual(data, attachment.Data);
}
[TestMethod]
public void TestCreateTextAttachmentLog()
{
// Create an error attachment log with text.
var text = "This is the text.";
var fileName = "text attachment";
var attachment = ErrorAttachmentLog.AttachmentWithText(text, fileName);
// Verify the contents.
Assert.AreEqual("text/plain", attachment.ContentType);
Assert.AreEqual(fileName, attachment.FileName);
Assert.AreEqual(text, Encoding.UTF8.GetString(attachment.Data));
}
[TestMethod]
public void TestNullFileNameIsAllowed()
{
// Create an error attachment log.
var contentType = "mime/type";
var data = new byte[] { 0, 3, 2, 1, 0 };
string fileName = null;
var attachment = ErrorAttachmentLog.AttachmentWithBinary(data, fileName, contentType);
// Verify the contents.
Assert.AreEqual(contentType, attachment.ContentType);
Assert.AreEqual(fileName, attachment.FileName);
CollectionAssert.AreEqual(data, attachment.Data);
}
[TestMethod]
public void TestCreateNullBinaryAttachmentLog()
{
// Attempt to create an error attachment log with null data.
var contentType = "mime/type";
byte[] data = null;
var fileName = "binary attachment";
var attachment = ErrorAttachmentLog.AttachmentWithBinary(data, fileName, contentType);
// Verify the result is null.
Assert.IsNull(attachment);
}
[TestMethod]
public void TestCreateNullTextAttachmentLog()
{
// Attempt to create an error attachment log with null text.
string text = null;
var fileName = "text attachment";
var attachment = ErrorAttachmentLog.AttachmentWithText(text, fileName);
// Verify the result is null.
Assert.IsNull(attachment);
}
[TestMethod]
public void TestValidateDoesNotThrowForValidLog()
{
var validErrorAttachmentLog = new ErrorAttachmentLog
{
ContentType = "ContentType",
Data = new byte[] { 1, 2, 3, 4 },
Device = GetValidDevice()
};
// Pass if validate does not throw.
validErrorAttachmentLog.Validate();
}
[TestMethod]
public void TestValidateThrowsIfMissingData()
{
var validErrorAttachmentLog = new ErrorAttachmentLog
{
ContentType = "ContentType",
Device = GetValidDevice()
};
Assert.ThrowsException<ValidationException>(() => validErrorAttachmentLog.Validate());
}
[TestMethod]
public void TestValidateThrowsIfMissingContentType()
{
var validErrorAttachmentLog = new ErrorAttachmentLog
{
Data = new byte[] { 1, 2, 3, 4 },
Device = GetValidDevice()
};
Assert.ThrowsException<ValidationException>(() => validErrorAttachmentLog.Validate());
}
[TestMethod]
public void TestValidatePropertiesReturnsTrueIfValidData()
{
var validErrorAttachmentLog = new ErrorAttachmentLog
{
ContentType = "ContentType",
ErrorId = Guid.NewGuid(),
Data = new byte[] { 1, 2, 3, 4 },
Id = Guid.NewGuid()
};
Assert.IsTrue(validErrorAttachmentLog.ValidatePropertiesForAttachment());
}
[TestMethod]
public void TestValidatePropertiesReturnsFalseIfMissingData()
{
var validErrorAttachmentLog = new ErrorAttachmentLog
{
ContentType = "ContentType",
ErrorId = Guid.NewGuid(),
Id = Guid.NewGuid()
};
Assert.IsFalse(validErrorAttachmentLog.ValidatePropertiesForAttachment());
}
[TestMethod]
public void TestValidatePropertiesReturnsFalseIfMissingContentType()
{
var validErrorAttachmentLog = new ErrorAttachmentLog
{
Data = new byte[] { 1, 2, 3, 4 },
Id = Guid.NewGuid(),
ErrorId = Guid.NewGuid(),
};
Assert.IsFalse(validErrorAttachmentLog.ValidatePropertiesForAttachment());
}
[TestMethod]
public void TestValidatePropertiesReturnsFalseIfInvalidErrorId()
{
var validErrorAttachmentLog = new ErrorAttachmentLog
{
Data = new byte[] { 1, 2, 3, 4 },
ContentType = "ContentType",
Id = Guid.NewGuid()
};
Assert.IsFalse(validErrorAttachmentLog.ValidatePropertiesForAttachment());
}
[TestMethod]
public void TestValidatePropertiesReturnsFalseIfInvalidAttachId()
{
var validErrorAttachmentLog = new ErrorAttachmentLog
{
Data = new byte[] { 1, 2, 3, 4 },
ErrorId = Guid.NewGuid(),
ContentType = "ContentType",
};
Assert.IsFalse(validErrorAttachmentLog.ValidatePropertiesForAttachment());
}
private Microsoft.AppCenter.Ingestion.Models.Device GetValidDevice()
{
return new Microsoft.AppCenter.Ingestion.Models.Device("sdkName", "sdkVersion", "osName", "osVersion",
"locale", 1,"appVersion", "appBuild", null, null, "model", "oemName", "osBuild", null, "screenSize",
null, null, "appNamespace", null, null, null, null);
}
}
}