-
Notifications
You must be signed in to change notification settings - Fork 3
/
OrientWebManagerTests.cs
252 lines (201 loc) · 8.34 KB
/
OrientWebManagerTests.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xunit;
using ConsoleApp1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Net;
using System.Net.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Moq;
namespace ConsoleApp1.Tests
{
public class WebResponseUnitTest
{
Mock<IWebManager> webManager = new Mock<IWebManager>();
public WebResponseUnitTest()
{
webManager.Setup(m => m.GetResponse(It.IsAny<string>(), It.IsAny<string>()));
}
[Fact]
public void GetResponseReturnsNullTest()
{
webManager.Object.GetResponse(@"localhost", "GET");
WebResponse res = webManager.Object.GetResponse(@"localhost", "GET");
Assert.Null(res);
}
}
public class WebResponseReaderUnitTest
{
Mock<IResponseReader> webResponseReader;
Mock<WebResponse> webResp;
Mock<HttpWebResponse> httpWebResp;
Mock<HttpResponseMessage> httpRespMsg;
public WebResponseReaderUnitTest()
{
webResponseReader = new Mock<IResponseReader>();
webResponseReader.Setup(s=> s.ReadResponse(It.IsAny<WebResponse>())).Returns( @"OK" ) ;
webResponseReader.Setup(s => s.ReadResponse(It.IsAny<HttpWebResponse>())).Returns(@"OK");
webResponseReader.Setup(s => s.ReadResponse(It.IsAny<HttpResponseMessage>())).Returns(@"OK");
webResp = new Mock<WebResponse>();
httpWebResp = new Mock<HttpWebResponse>();
httpRespMsg = new Mock<HttpResponseMessage>();
}
[Fact]
public void GetWebResponseRturnsOKstring()
{
string res = webResponseReader.Object.ReadResponse(webResp.Object);
Assert.Equal(@"OK", res);
}
[Fact]
public void GetHttpWebResponseRturnsOKstring()
{
string res = webResponseReader.Object.ReadResponse(httpWebResp.Object);
Assert.Equal(@"OK", res);
}
[Fact]
public void GetHttpResponseMsgRturnsOKstring()
{
string res = webResponseReader.Object.ReadResponse(httpRespMsg.Object);
Assert.Equal(@"OK", res);
}
}
public class OrientWebManagerIntegrationTests
{
OrientWebManager orietWebManager;
string authUrl, funcUrl, batchUrl, commandUrl, root, password;
NetworkCredential nc;
public OrientWebManagerIntegrationTests()
{
orietWebManager = new OrientWebManager();
authUrl = ConfigurationManager.AppSettings["orient_auth_host"];
funcUrl = ConfigurationManager.AppSettings["orient_func_host"];
batchUrl = ConfigurationManager.AppSettings["orient_batch_host"];
commandUrl = ConfigurationManager.AppSettings["orient_command_host"];
root = ConfigurationManager.AppSettings["ChildLogin"];
password = ConfigurationManager.AppSettings["ChildPassword"];
nc = new NetworkCredential(root, password);
}
[Fact]
public void GetResponseAnUthorizedReturnsNullIntegrationTest()
{
WebResponse response;
response = orietWebManager.GetResponse(authUrl, "GET");
Assert.Equal(response, null);
}
[Fact]
public void AuthenticateIntegrationTest()
{
string result = orietWebManager.Authenticate(authUrl, nc).Headers.Get("Set-Cookie");
bool check = result != string.Empty && result != null;
Assert.True(check);
}
}
public class WebResponseReaderIntegrationTest
{
WebResponseReader webResponseReader;
WebRequest webRequest;
HttpWebRequest httpWebRequest;
string webRequestsCheckURL;
//arrange
public WebResponseReaderIntegrationTest()
{
webResponseReader = new WebResponseReader();
webRequestsCheckURL = @"https://www.google.ru";
webRequest = WebRequest.Create(webRequestsCheckURL);
httpWebRequest = HttpWebRequest.CreateHttp(webRequestsCheckURL);
}
[Fact]
public void GetWebResponseReturns()
{
string result = webResponseReader.ReadResponse(webRequest.GetResponse());
Assert.NotNull(result);
Assert.NotEqual(string.Empty, result);
}
[Fact]
public void GetHttpWebResponse()
{
string result = webResponseReader.ReadResponse(httpWebRequest.GetResponse());
Assert.NotNull(result);
Assert.NotEqual(string.Empty, result);
}
}
public class JSONmanagerIntegrationTests
{
JSONManager2 jm;
string str0 ,act0,str1,act1,str4;
//arrange
public JSONmanagerIntegrationTests()
{
str0 = "{\"result\":[{\"Name\":\"value1\",\"sAMAccountName\":\"acc1\"},{\"Name\":\"value2\",\"sAMAccountName\":\"acc2\"}]}";
act0 = "[{\"sAMAccountName\":\"acc1\",\"Name\":\"value1\"},{\"sAMAccountName\":\"acc2\",\"Name\":\"value2\"}]";
str1 =
"{\"news\":[{\"Title\":\"value1\",\"Article\":\"value3\"},{\"Title\":\"value2\",\"Article\":\"value4\",\"tags\":[{\"Name\":\"value7\"},{\"Name\":\"value8\"}]}]}";
act1 = "[\"value1\",\"value2\"]";
str4 = "[{\"Name\":\"value1\"},{\"Name\":\"value2\"}]";
jm = new JSONManager2();
}
[Fact]
public void JSONmanagerParseJSONParentColectiontoClassReturnsString()
{
//Extract tokens from JSON response parent Node, convert to collection of model objects
IJEnumerable<JToken> jte = jm.ExtractFromParentNode(str0, "result");
//Extract + convert JSON to collection of model objects
IEnumerable<Person> res = jm.DeserializeFromParentNode<Person>(str0, "result");
//to string Selectable -> ignore nulls, no intending
string resp = jm.CollectionToStringFormat<Person>(res,
new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None });
Assert.Equal(resp, act0);
}
[Fact]
public void JSONmanagerParseJSONParentChildColectiontoStringReturnsString()
{
//extract from JSON parent node
IJEnumerable<JToken> jte = jm.ExtractFromParentChildNode(str1, "news", "Title");
//convert to collection of strings
IEnumerable<string> res = jm.DeserializeFromParentChildNode<string>(str1, "news", "Title");
//to string Selectable -> ignore nulls, no intending
string resp = jm.CollectionToStringFormat<string>(res
, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Include, Formatting = Formatting.None });
Assert.Equal(resp, act1);
}
[Fact]
public void JSONmngParsefromChildCollectionreturnsString()
{
//extract from child nodes
IJEnumerable<JToken> jte = jm.ExtractFromChildNode(str4, "Name");
//to collection
IEnumerable<string> res = jm.DeserializeFromChildNode<string>(str4, "Name");
//to string Selectable -> ignore nulls, no intending
string resp = jm.CollectionToStringFormat<string>(res
, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Include, Formatting = Formatting.None });
Assert.Equal(resp, act1);
}
}
//collection of test cases with expected and result
public class TestCase
{
public int CaseNumber { get ; private set; }
public string Input { get; set; }
public string Expected { get; set; }
public string Actual { get; private set; }
public bool Equal { get; private set; }
public TestCase()
{
this.CaseNumber += 1;
this.Actual = string.Empty;
this.Expected = string.Empty;
this.Equal = false;
}
public void Check(string Actual_)
{
this.Actual = Actual_;
if (this.Expected == this.Actual) { this.Equal = true; }
else { this.Equal = false; }
}
}
}