public void recv_file_happy_case() { var saved = new SavedPath { device_folder = "fff", relative_file_path = "rrr" }; temp.Setup(x => x.EndWrite()).Verifiable(); temp.Setup(x => x.Path).Returns("path1").Verifiable(); storage.Setup(x => x.MoveToStorage("path1", ctx.fileCtx)).Returns(saved).Verifiable(); util.Setup(x => x.GetNextSeq()).Returns(112345).Verifiable(); util.Setup(x => x.SaveFileRecord(It.Is <FileAsset>( f => f.device_id == ctx.device_id && f.event_time == ctx.fileCtx.datetime && !f.file_id.Equals(Guid.Empty) && f.file_name == ctx.fileCtx.file_name && f.file_path == Path.Combine(ctx.fileCtx.folder, ctx.fileCtx.file_name) && f.file_size == ctx.fileCtx.file_size && f.type == (int)ctx.fileCtx.type && f.saved_path == saved.relative_file_path && f.parent_folder == Path.GetDirectoryName(saved.relative_file_path) && f.seq == 112345) )).Verifiable(); var state = new TransmitStartedState() { Util = util.Object }; ctx.SetState(state); string sentData = ""; ctx.SendFunc = (x) => { sentData = x; }; bool called = false; ctx.OnFileReceived += (s, e) => { called = true; }; ctx.handleFileEndCmd(new TextCommand { action = "file-end", file_name = "f.jpg" }); Assert.AreEqual(1, ctx.recved_files); Assert.IsTrue(ctx.GetState() is TransmitInitState); util.VerifyAll(); temp.VerifyAll(); storage.VerifyAll(); Assert.IsTrue(called); var o = JsonConvert.DeserializeObject <TextCommand>(sentData); Assert.AreEqual("file-exist", o.action); Assert.AreEqual(ctx.fileCtx.file_name, o.file_name); }
public void unconnected_connect_conntected() { var connectMsg = new TextCommand { action = "connect", device_name = "dev", device_id = "guid1" }; var tempFactory = new Mock <ITempFileFactory>(); var fileStorage = new Mock <IFileStorage>(); var connectHandler = new Mock <IConnectMsgHandler>(); var retState = new Mock <AbstractProtocolState>(); var initState = new UnconnectedState(); initState.handler = connectHandler.Object; var ctx = new ProtocolContext(tempFactory.Object, storage.Object, initState); connectHandler.Setup(x => x.HandleConnectMsg(connectMsg, It.IsAny <ProtocolContext>())).Callback(() => { ctx.SetState(retState.Object); }).Verifiable(); ctx.handleConnectCmd(connectMsg); connectHandler.VerifyAll(); Assert.AreEqual(retState.Object, ctx.GetState()); Assert.AreEqual("dev", ctx.device_name); Assert.AreEqual("guid1", ctx.device_id); }