public void TestJsonEncoder_EncodeSuccess() { /* PRECONDITION */ Debug.Assert(encoder != null); Debug.Assert(fileSystem != null); Debug.Assert(config != null); Debug.Assert(container != null); Debug.Assert(input != null); container.ComposeExportedValue <IEncodableEventQueue <JsonIntermediateFormatSample> >(input); container.ComposeExportedValue(config); container.ComposeParts(encoder); /* GIVEN */ var random = new Random(); var eventList = new List <TestEvent> { new TestEvent(random.Next()), new TestEvent(random.Next()) }; var serializedEvent = JsonManualSerialization.GenerateSerializedEvents(eventList); config.RelativeFilePath = new FilePath(fileLocation, true); /* WHEN */ encoder.Encode(new DirectoryPath(recordingFolderLocation, true)); // As the input queue is correctly attached we can now queue our events. Assert.IsTrue(input.ConsumerAttachedEvent.WaitOne(maxWaitTime), "Encoder did not attach to input queue in time!"); // We enqueue now all events we want to serialize. foreach (var @event in eventList) { var sample = new JsonIntermediateFormatSample() { Data = JsonSerializer.SerializeToUtf8Bytes(@event), IssuingModule = @event.IssuingModule, Timestamp = @event.Timestamp, Type = @event.GetType() }; input.Enqueue(sample); } input.Close(); Assert.IsTrue(encoder.EncodeFinished.WaitOne(maxWaitTime), "Encoder did not finish in time!"); /* THEN */ // We get the encoded string which the encoder produced. const string path = recordingFolderLocation + fileLocation; Assert.IsTrue(fileSystem.File.Exists(path)); var encodedString = fileSystem.File.ReadAllText(recordingFolderLocation + fileLocation, Encoding.Default); var plusResolvedString = encodedString.Replace(@"\u002B", "+"); Assert.AreEqual(serializedEvent, plusResolvedString); }
public void TestJsonDecoder_ParseSuccess() { /* PRECONDITION */ Debug.Assert(decoder != null); Debug.Assert(fileSystem != null); Debug.Assert(config != null); Debug.Assert(container != null); config.RelativeFilePath = new FilePath(fileLocation, true); container.ComposeExportedValue(config); container.ComposeParts(decoder); /* GIVEN */ var random = new Random(); var eventList = new List <TestEvent> { new TestEvent(random.Next()), new TestEvent(random.Next()) }; var serializedString = JsonManualSerialization.GenerateSerializedEvents(eventList); var mockFileData = new MockFileData(serializedString, Encoding.UTF8); fileSystem.AddFile(recordingFolderLocation + fileLocation, mockFileData); using var awaitThreadEvent = new ManualResetEvent(false); using var openEvent = new ManualResetEvent(false); using var didDecodeAllEvents = new ManualResetEvent(false); /* WHEN */ var thread = new Thread(async() => { await foreach (var @event in Awaitable.Await(decoder.GetEvents(), awaitThreadEvent)) { var deserialized = JsonSerializer.Deserialize(@event.Data, @event.Type); if ((!(deserialized is TestEvent receivedEvent))) { continue; } var index = eventList.FindIndex((compareEvent) => compareEvent.Identifier == receivedEvent.Identifier); if (index == -1) { continue; } eventList.RemoveAt(index); if (eventList.Count == 0) { didDecodeAllEvents.Set(); } } }); decoder.Open(); thread.Start(); Assert.IsTrue(awaitThreadEvent.WaitOne(maxWaitTime), "Thread did not start in time!"); decoder.Decode(new DirectoryPath(recordingFolderLocation, true)); /* THEN */ Assert.IsTrue(decoder.DecodeFinished.WaitOne(maxWaitTime), "Decoder did not finish in time!"); Assert.IsTrue(didDecodeAllEvents.WaitOne(maxWaitTime), "Could not find all encoded events."); }