PageRenderTime 21ms CodeModel.GetById 2ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/EventStore.Core.Tests/Http/Streams/feed.cs

https://github.com/elainte/EventStore
C# | 435 lines | 374 code | 61 blank | 0 comment | 11 complexity | 1fe1ac36e9c037c62582fa89a65a95ad MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Net;
  4using System.Xml;
  5using System.Xml.Linq;
  6using EventStore.Core.Tests.Helpers;
  7using EventStore.Core.TransactionLog.Chunks;
  8using EventStore.Transport.Http;
  9using NUnit.Framework;
 10using Newtonsoft.Json.Linq;
 11using System.Linq;
 12using HttpStatusCode = System.Net.HttpStatusCode;
 13
 14namespace EventStore.Core.Tests.Http.Streams
 15{
 16    namespace feed
 17    {
 18        public abstract class SpecificationWithLongFeed : HttpBehaviorSpecification
 19        {
 20            private int _numberOfEvents;
 21
 22            protected override void Given()
 23            {
 24                _numberOfEvents = 25;
 25                for (var i = 0; i < _numberOfEvents; i++)
 26                {
 27                    PostEvent(i);
 28                }
 29            }
 30
 31            protected string PostEvent(int i)
 32            {
 33                var response = MakeArrayEventsPost(
 34                    TestStream, new[] {new {EventId = Guid.NewGuid(), EventType = "event-type", Data = new {Number = i}}});
 35                Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
 36                return response.Headers[HttpResponseHeader.Location];
 37            }
 38
 39            protected string GetLink(JObject feed, string relation)
 40            {
 41                var rel = (from JObject link in feed["links"]
 42                           from JProperty attr in link
 43                           where attr.Name == "relation" && (string) attr.Value == relation
 44                           select link).SingleOrDefault();
 45                return (rel == null) ? (string)null : (string)rel["uri"];
 46            }
 47        }
 48
 49        [TestFixture, Category("LongRunning")]
 50        public class when_posting_multiple_events : SpecificationWithLongFeed
 51        {
 52            protected override void When()
 53            {
 54                GetJson<JObject>(TestStream, ContentType.AtomJson);
 55            }
 56
 57            [Test]
 58            public void returns_ok_status_code()
 59            {
 60                Assert.AreEqual(HttpStatusCode.OK, _lastResponse.StatusCode);
 61            }
 62        }
 63
 64        [TestFixture, Category("LongRunning")]
 65        public class when_retrieving_feed_head : SpecificationWithLongFeed
 66        {
 67            private JObject _feed;
 68
 69            protected override void When()
 70            {
 71                _feed = GetJson<JObject>(TestStream, ContentType.AtomJson);
 72            }
 73
 74            [Test]
 75            public void returns_ok_status_code()
 76            {
 77                Assert.AreEqual(HttpStatusCode.OK, _lastResponse.StatusCode);
 78            }
 79
 80            [Test]
 81            public void contains_a_link_rel_previous()
 82            {
 83                var rel = GetLink(_feed, "previous");
 84                Assert.IsNotEmpty(rel);
 85            }
 86
 87            [Test]
 88            public void contains_a_link_rel_next()
 89            {
 90                var rel = GetLink(_feed, "next");
 91                Assert.IsNotEmpty(rel);
 92            }
 93
 94            [Test]
 95            public void contains_a_link_rel_self()
 96            {
 97                var rel = GetLink(_feed, "self");
 98                Assert.IsNotEmpty(rel);
 99            }
100
101            [Test]
102            public void contains_a_link_rel_first()
103            {
104                var rel = GetLink(_feed, "first");
105                Assert.IsNotEmpty(rel);
106            }
107
108            [Test]
109            public void contains_a_link_rel_last()
110            {
111                var rel = GetLink(_feed, "last");
112                Assert.IsNotEmpty(rel);
113            }
114        }
115
116
117        [TestFixture, Category("LongRunning")]
118        public class when_retrieving_the_previous_link_of_the_feed_head: SpecificationWithLongFeed
119        {
120            private JObject _feed;
121            private JObject _head;
122            private string _previous;
123
124            protected override void Given()
125            {
126                base.Given();
127                _head = GetJson<JObject>(TestStream, ContentType.AtomJson);
128                _previous = GetLink(_head, "previous");
129            }
130
131            protected override void When()
132            {
133                _feed = GetJson<JObject>(_previous, ContentType.AtomJson);
134            }
135
136            [Test]
137            public void returns_200_response()
138            {
139                Assert.AreEqual(HttpStatusCode.OK, _lastResponse.StatusCode);
140            }
141
142            [Test]
143            public void there_is_no_prev_link()
144            {
145                var rel = GetLink(_feed, "prev");
146                Assert.IsNull(rel);
147            }
148
149            [Test]
150            public void there_is_a_next_link()
151            {
152                var rel = GetLink(_feed, "next");
153                Assert.IsNotEmpty(rel);                
154            }
155
156            [Test]
157            public void there_is_a_self_link()
158            {
159                var rel = GetLink(_feed, "self");
160                Assert.IsNotEmpty(rel);
161            }
162
163            [Test]
164            public void there_is_a_first_link()
165            {
166                var rel = GetLink(_feed, "first");
167                Assert.IsNotEmpty(rel);
168            }
169
170            [Test]
171            public void there_is_a_last_link()
172            {
173                var rel = GetLink(_feed, "last");
174                Assert.IsNotEmpty(rel);
175            }
176
177            [Test]
178            public void the_feed_is_empty()
179            {
180                Assert.AreEqual(0, _feed["entries"].Count());
181            }
182
183            [Test]
184            public void the_response_is_not_cachable()
185            {
186                Assert.AreEqual("max-age=0, no-cache, must-revalidate", _lastResponse.Headers["Cache-Control"]);
187            }
188        }
189
190
191        [TestFixture, Category("LongRunning")]
192        public class when_reading_a_stream_forward_with_deleted_linktos : HttpSpecificationWithLinkToToDeletedEvents
193        {
194            private JObject _feed;
195            private List<JToken> _entries;
196            protected override void When()
197            {
198                _feed = GetJson<JObject>("/streams/" + LinkedStreamName + "/0/forward/10", accept: ContentType.Json);
199                _entries = _feed != null ? _feed["entries"].ToList() : new List<JToken>();
200            }
201
202            [Test]
203            public void the_feed_has_one_event()
204            {
205                Assert.AreEqual(1, _entries.Count());
206            }
207
208            [Test]
209            public void the_edit_link_to_is_to_correct_uri()
210            {
211                var foo = _entries[0]["links"][0];
212                Assert.AreEqual("edit", foo["relation"].ToString());
213                Assert.AreEqual(MakeUrl("/streams/" + DeletedStreamName + "/0"), foo["uri"].ToString());
214            }
215
216            [Test]
217            public void the_alt_link_to_is_to_correct_uri()
218            {
219                var foo = _entries[0]["links"][1];
220                Assert.AreEqual("alternate", foo["relation"].ToString());
221                Assert.AreEqual(MakeUrl("/streams/" + DeletedStreamName + "/0"), foo["uri"].ToString());
222            }
223        }
224
225        [TestFixture, Category("LongRunning")]
226        public class when_reading_a_stream_forward_with_maxcount_deleted_linktos : SpecificationWithLinkToToMaxCountDeletedEvents
227        {
228            private JObject _feed;
229            private List<JToken> _entries;
230            protected override void When()
231            {
232                _feed = GetJson<JObject>("/streams/" + LinkedStreamName + "/0/forward/10", accept: ContentType.Json);
233                _entries = _feed != null ? _feed["entries"].ToList() : new List<JToken>();
234            }
235
236            [Test]
237            public void the_feed_has_no_events()
238            {
239                Assert.AreEqual(1, _entries.Count());
240            }
241        }
242
243        [TestFixture, Category("LongRunning")][Explicit("Failing test for Greg demonstrating NullReferenceException in Convert.cs")]
244        public class when_reading_a_stream_forward_with_maxcount_deleted_linktos_with_rich_entry : SpecificationWithLinkToToMaxCountDeletedEvents
245        {
246            private JObject _feed;
247            private List<JToken> _entries;
248            protected override void When()
249            {
250                _feed = GetJson<JObject>("/streams/" + LinkedStreamName + "/0/forward/10?embed=rich", accept: ContentType.Json); 
251                _entries = _feed != null ? _feed["entries"].ToList() : new List<JToken>();
252            }
253
254            [Test]
255            public void the_feed_has_some_events()
256            {
257                Assert.AreEqual(1, _entries.Count());
258            }
259        }
260
261        [TestFixture, Category("LongRunning")]
262        public class when_reading_a_stream_forward_with_deleted_linktos_with_content_enabled_as_xml :
263            HttpSpecificationWithLinkToToDeletedEvents
264        {
265            private XDocument _feed;
266            private XElement[] _entries;
267
268            protected override void When()
269            {
270                _feed = GetXml("/streams/" + LinkedStreamName + "/0/forward/10?embed=content");
271                _entries = _feed.GetEntries();
272            }
273
274            [Test]
275            public void the_feed_has_one_event()
276            {
277                Assert.AreEqual(1, _entries.Length);
278            }
279
280            [Test]
281            public void the_edit_link_is_to_correct_uri()
282            {
283                var link = _entries[0].GetLink("edit");
284                Assert.AreEqual(MakeUrl("/streams/" + DeletedStreamName + "/0"), link);
285            }
286
287	    [Test]
288            public void the_alternate_link_is_to_correct_uri()
289            {
290                var link = _entries[0].GetLink("alternate");
291                Assert.AreEqual(MakeUrl("/streams/" + DeletedStreamName + "/0"), link);
292            }
293        }
294
295
296        [TestFixture, Category("LongRunning")]
297        public class when_reading_a_stream_forward_with_deleted_linktos_with_content_enabled : HttpSpecificationWithLinkToToDeletedEvents
298        {
299            private JObject _feed;
300            private List<JToken> _entries;
301            protected override void When()
302            {
303                _feed = GetJson<JObject>("/streams/" + LinkedStreamName + "/0/forward/10?embed=content", accept: ContentType.Json);
304                _entries = _feed != null ? _feed["entries"].ToList() : new List<JToken>();
305            }
306
307            [Test]
308            public void the_feed_has_one_event()
309            {
310                Assert.AreEqual(1, _entries.Count());
311            }
312
313            [Test]
314            public void the_edit_link_to_is_to_correct_uri()
315            {
316                var foo = _entries[0]["links"][0];
317                Assert.AreEqual("edit", foo["relation"].ToString());
318                Assert.AreEqual(MakeUrl("/streams/" + DeletedStreamName + "/0"), foo["uri"].ToString());
319            }
320
321            [Test]
322            public void the_alt_link_to_is_to_correct_uri()
323            {
324                var foo = _entries[0]["links"][1];
325                Assert.AreEqual("alternate", foo["relation"].ToString());
326                Assert.AreEqual(MakeUrl("/streams/" + DeletedStreamName + "/0"), foo["uri"].ToString());
327            }
328        }
329
330
331        [TestFixture, Category("LongRunning")]
332        public class when_reading_a_stream_backward_with_deleted_linktos : HttpSpecificationWithLinkToToDeletedEvents
333        {
334            private JObject _feed;
335            private List<JToken> _entries;
336            protected override void When()
337            {
338                _feed = GetJson<JObject>("/streams/" + LinkedStreamName + "/0/backward/1", accept: ContentType.Json);
339                _entries = _feed != null? _feed["entries"].ToList() : new List<JToken>();
340            }
341
342            [Test]
343            public void the_feed_has_one_event()
344            {
345                Assert.AreEqual(1, _entries.Count());
346            }
347
348            [Test]
349            public void the_edit_link_to_is_to_correct_uri()
350            {
351                var foo = _entries[0]["links"][0];
352                Assert.AreEqual("edit", foo["relation"].ToString());
353                Assert.AreEqual(MakeUrl("/streams/" + DeletedStreamName + "/0"), foo["uri"].ToString());
354            }
355
356            [Test]
357            public void the_alt_link_to_is_to_correct_uri()
358            {
359                var foo = _entries[0]["links"][1];
360                Assert.AreEqual("alternate", foo["relation"].ToString());
361                Assert.AreEqual(MakeUrl("/streams/" + DeletedStreamName + "/0"), foo["uri"].ToString());
362            }
363        }
364
365
366        [TestFixture, Category("LongRunning")]
367        public class when_reading_a_stream_backward_with_deleted_linktos_and_embed_of_content : HttpSpecificationWithLinkToToDeletedEvents
368        {
369            private JObject _feed;
370            private List<JToken> _entries;
371            protected override void When()
372            {
373                _feed = GetJson<JObject>("/streams/" + LinkedStreamName + "/0/backward/1", accept: ContentType.Json);
374                _entries = _feed != null ? _feed["entries"].ToList() : new List<JToken>();
375            }
376
377            [Test]
378            public void the_feed_has_one_event()
379            {
380                Assert.AreEqual(1, _entries.Count());
381            }
382
383            [Test]
384            public void the_edit_link_to_is_to_correct_uri()
385            {
386                var foo = _entries[0]["links"][0];
387                Assert.AreEqual("edit", foo["relation"].ToString());
388                Assert.AreEqual(MakeUrl("/streams/" + DeletedStreamName + "/0"), foo["uri"].ToString());
389            }
390
391            [Test]
392            public void the_alt_link_to_is_to_correct_uri()
393            {
394                var foo = _entries[0]["links"][1];
395                Assert.AreEqual("alternate", foo["relation"].ToString());
396                Assert.AreEqual(MakeUrl("/streams/" + DeletedStreamName + "/0"), foo["uri"].ToString());
397            }
398        }
399
400        [TestFixture, Category("LongRunning")]
401        public class when_polling_the_head_forward_and_a_new_event_appears: SpecificationWithLongFeed
402        {
403            private JObject _feed;
404            private JObject _head;
405            private string _previous;
406            private string _lastEventLocation;
407
408            protected override void Given()
409            {
410                base.Given();
411                _head = GetJson<JObject>(TestStream, ContentType.AtomJson);
412                _previous = GetLink(_head, "previous");
413                _lastEventLocation = PostEvent(-1);
414            }
415
416            protected override void When()
417            {
418                _feed = GetJson<JObject>(_previous, ContentType.AtomJson);
419            }
420
421            [Test]
422            public void returns_ok_status_code()
423            {
424                Assert.AreEqual(HttpStatusCode.OK, _lastResponse.StatusCode);
425            }
426
427            [Test]
428            public void returns_a_feed_with_a_single_entry_referring_to_the_last_event()
429            {
430                HelperExtensions.AssertJson(new {entries = new[] {new {Id = _lastEventLocation}}}, _feed);
431            }
432        }
433
434    }
435}