public void Instanciation_With_IP_OK() { var client = new NikoClient("127.0.0.1"); client.Should().NotBeNull(); client.IsConnected.Should().BeFalse(); }
public async Task Event_Received() { TaskCompletionSource <int> tcs = null; var tcpclientmock = new Mock <ITcpClient>(); var(jsonbytes, jsonmodel) = GetJson <EventImp>(@"Json\Event.json", true, new EventConverter()); // ReSharper disable once AccessToModifiedClosure tcpclientmock.SetupGet(c => c.IsConnected).Returns(() => false); tcpclientmock.Setup(c => c.ReadAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())) .Callback <byte[], int, int>((b, o, l) => { jsonbytes.CopyTo(b, o); }) .Returns(() => (tcs = new TaskCompletionSource <int>()).Task); var client = new NikoClient(tcpclientmock.Object); client.StartClient(); using (var minitoredclient = client.Monitor()) { tcs.SetResult(jsonbytes.Length); minitoredclient.Should().Raise(nameof(NikoClient.OnValueChanged)).WithArgs <IEvent>(d => d.Data.Count == jsonmodel.Data.Count); await Task.Delay(1000); } }
public async Task Invalid_JSON() { TaskCompletionSource <int> tcs = null; var tcpclientmock = new Mock <ITcpClient>(); var(jsonbytes, _) = GetJson <NikoResponse <IReadOnlyList <IAction> > >(@"Json\Invalid_Json.json", false, new SystemInfoConverter()); // ReSharper disable once AccessToModifiedClosure tcpclientmock.SetupGet(c => c.IsConnected).Returns(() => false); tcpclientmock.Setup(c => c.ReadAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())) .Callback <byte[], int, int>((b, o, l) => { jsonbytes.CopyTo(b, o); }) .Returns(() => (tcs = new TaskCompletionSource <int>()).Task); var client = new NikoClient(tcpclientmock.Object); client.StartClient(); var sendtask = client.SendCommand <ISystemInfo>(new GetSystemInfoCommand()); tcs.SetResult(jsonbytes.Length); Func <Task> act = async() => await sendtask; await act.Should().NotThrowAsync <JsonReaderException>(); }
public async Task Get_Locations_OK() { TaskCompletionSource <int> tcs = null; var tcpclientmock = new Mock <ITcpClient>(); var(jsonbytes, responsemodel) = GetJson <NikoResponse <IReadOnlyList <ILocation> > >(@"Json\GetLocations_OK.json", true, new LocationsConverter()); // ReSharper disable once AccessToModifiedClosure tcpclientmock.SetupGet(c => c.IsConnected).Returns(() => false); tcpclientmock.Setup(c => c.ReadAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())) .Callback <byte[], int, int>((b, o, l) => { jsonbytes.CopyTo(b, o); }) .Returns(() => (tcs = new TaskCompletionSource <int>()).Task); var client = new NikoClient(tcpclientmock.Object); client.StartClient(); var responsetask = client.GetLocations(); tcs.SetResult(jsonbytes.Length); var response = await responsetask; response.Should().NotBeNull(); response.Command.Should().Be(response.Command); response.Data.Should().NotBeNull(); response.Data.Should().HaveCount(responsemodel.Data.Count); response.Data.Should().Equal(responsemodel.Data); }
public async Task ExecuteCommand_Error() { TaskCompletionSource <int> tcs = null; var tcpclientmock = new Mock <ITcpClient>(); var(jsonbytes, responsemodel) = GetJson <NikoResponse <ErrorImp> >(@"Json\ExecuteCommand_Error.json", true, new BaseResponseConverter()); // ReSharper disable once AccessToModifiedClosure tcpclientmock.SetupGet(c => c.IsConnected).Returns(() => false); tcpclientmock.Setup(c => c.ReadAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())) .Callback <byte[], int, int>((b, o, l) => { jsonbytes.CopyTo(b, o); }) .Returns(() => (tcs = new TaskCompletionSource <int>()).Task); var client = new NikoClient(tcpclientmock.Object); client.StartClient(); var responsetask = client.ExecuteCommand(31, 100); tcs.SetResult(jsonbytes.Length); var response = await responsetask; response.Should().NotBeNull(); response.Command.Should().Be(response.Command); response.Error.Should().Be(responsemodel.Data.Error); }
public void Instanciation_With_ITcpClient_OK() { var tcpclientmock = new Mock <ITcpClient>(); var client = new NikoClient(tcpclientmock.Object); client.Should().NotBeNull(); client.IsConnected.Should().BeFalse(); }
public void Instanciation_With_Null_ITcpClient() { try { // ReSharper disable once UnusedVariable var client = new NikoClient((ITcpClient)null); } catch (Exception e) { e.Should().BeOfType <ArgumentNullException>(); return; } Assert.Fail("Exception should have been raised."); }
public void Instanciation_With_Wrong_IP_Address() { try { // ReSharper disable once UnusedVariable var client = new NikoClient("@@@@@"); } catch (Exception e) { e.Should().BeOfType <ArgumentException>(); return; } Assert.Fail("Exception should have been raised."); }
public void Stop_Client_Without_Start_First_OK() { var tcpclientmock = new Mock <ITcpClient>(); tcpclientmock.SetupGet(c => c.IsConnected).Returns(() => false); var client = new NikoClient(tcpclientmock.Object); client.StopClient(); client.Should().NotBeNull(); tcpclientmock.Verify(c => c.Start(), Times.Never); tcpclientmock.Verify(c => c.Stop(), Times.Never); tcpclientmock.VerifyGet(c => c.IsConnected, Times.Once); client.IsConnected.Should().BeFalse(); }
public async Task ExecuteCommand_Exception() { var tcpclientmock = new Mock <ITcpClient>(); // ReSharper disable once AccessToModifiedClosure tcpclientmock.SetupGet(c => c.IsConnected).Returns(() => false); tcpclientmock.Setup(c => c.ReadAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())) .Returns(() => new TaskCompletionSource <int>().Task); tcpclientmock.Setup(c => c.WriteAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())) .Throws <ArgumentNullException>(); var client = new NikoClient(tcpclientmock.Object); client.StartClient(); Func <Task> act = async() => await client.ExecuteCommand(31, 0); await act.Should().ThrowAsync <ArgumentNullException>(); }
public void Get_Client_IP() { var tcs = new TaskCompletionSource <int>(); var isconnected = false; var tcpclientmock = new Mock <ITcpClient>(); // ReSharper disable once AccessToModifiedClosure tcpclientmock.SetupGet(c => c.IsConnected).Returns(() => isconnected); tcpclientmock.Setup(c => c.ReadAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())) .Returns(() => tcs.Task); tcpclientmock.SetupGet(c => c.IpAddress).Returns(IPAddress.Loopback); var client = new NikoClient(tcpclientmock.Object); client.StartClient(); isconnected = true; client.Should().NotBeNull(); client.IpAddress.Should().Be(IPAddress.Loopback); }
public void Start_Client_Connects() { var tcs = new TaskCompletionSource <int>(); var isconnected = false; var tcpclientmock = new Mock <ITcpClient>(); // ReSharper disable once AccessToModifiedClosure tcpclientmock.SetupGet(c => c.IsConnected).Returns(() => isconnected); tcpclientmock.Setup(c => c.ReadAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())) .Returns(() => tcs.Task); var client = new NikoClient(tcpclientmock.Object); client.StartClient(); isconnected = true; client.Should().NotBeNull(); tcpclientmock.Verify(c => c.Start(), Times.Once); tcpclientmock.VerifyGet(c => c.IsConnected, Times.Once); client.IsConnected.Should().BeTrue(); }
public ActionsController(INikoService service) { _nikoClient = service.Client; _nikoClient.StartClient(); _nikoClient.StartEvents(); }
public Task Initialize() { return(NikoClient.AutoDetect().ContinueWith(t => { Client = t.Result; })); }