public async Task SetThing_Should_ReturnOk_When_SetWrite2()
        {
            var source = new CancellationTokenSource();

            source.CancelAfter(s_timeout);


            var value   = _fixture.Create <bool>();
            var message = new WebSocketRequest
            {
                MessageType = "setProperty",
                Data        = new
                {
                    Write2 = value
                }
            };

            using var socket = await _socketClient.ConnectAsync(_baseUrl, source.Token);

            socket.State.Should().Be(WebSocketState.Open);

            source = new CancellationTokenSource();
            source.CancelAfter(s_timeout);
            await socket.SendAsync(Serializer.Serialize(message), WebSocketMessageType.Text, true, source.Token);

            await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
        }
        public async Task RequestAction_Should_ReturnNotFound_When_ActionNotFound()
        {
            var source = new CancellationTokenSource();

            source.CancelAfter(s_timeout);

            var message = new WebSocketRequest
            {
                MessageType = "requestAction",
                Data        = new
                {
                    Abc = new
                    {
                        Input = new object()
                    }
                }
            };

            using var socket = await _socketClient.ConnectAsync(_baseUrl, source.Token);

            socket.State.Should().Be(WebSocketState.Open);

            source = new CancellationTokenSource();
            source.CancelAfter(s_timeout);
            await socket.SendAsync(Serializer.Serialize(message), WebSocketMessageType.Text, true, source.Token);

            var counter = 0;

            while (true)
            {
                source = new CancellationTokenSource();
                source.CancelAfter(s_timeout);

                try
                {
                    var array  = new ArraySegment <byte>(new byte[4096]);
                    var result = await socket.ReceiveAsync(array, source.Token);

                    var json = Serializer.Deserialize(array, result);
                    json["messageType"].Value <string>().Should().Be("error");
                    json["data"].Type.Should().Be(JTokenType.Object);
                    json["data"]["status"].Type.Should().Be(JTokenType.String);
                    json["data"]["status"].Value <string>().Should().StartWith("404");
                    break;
                }
                catch (Exception)
                {
                    counter++;

                    if (counter >= s_retries)
                    {
                        throw;
                    }
                }
            }

            await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
        }
        public async Task SetThing_Should_ReturnOk_When_SetText()
        {
            var source = new CancellationTokenSource();

            source.CancelAfter(s_timeout);


            var value   = _fixture.Create <string>();
            var message = new WebSocketRequest
            {
                MessageType = "setProperty",
                Data        = new
                {
                    Text = value
                }
            };

            using var socket = await _socketClient.ConnectAsync(_baseUrl, source.Token);

            socket.State.Should().Be(WebSocketState.Open);

            source = new CancellationTokenSource();
            source.CancelAfter(s_timeout);
            await socket.SendAsync(Serializer.Serialize(message), WebSocketMessageType.Text, true, source.Token);

            var counter = 0;

            while (true)
            {
                source = new CancellationTokenSource();
                source.CancelAfter(s_timeout);

                try
                {
                    var array  = new ArraySegment <byte>(new byte[4096]);
                    var result = await socket.ReceiveAsync(array, source.Token);

                    var json = Serializer.Deserialize(array, result);
                    json["messageType"].Value <string>().Should().Be("propertyStatus");
                    json["data"].Type.Should().Be(JTokenType.Object);
                    json["data"]["text"].Type.Should().Be(JTokenType.String);
                    json["data"]["text"].Value <string>().Should().Be(value);
                    break;
                }
                catch (Exception)
                {
                    counter++;

                    if (counter >= s_retries)
                    {
                        throw;
                    }
                }
            }

            await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
        }
Exemple #4
0
        public async Task AddEventSubscriptionForLevel_Should_ReturnOk()
        {
            var source = new CancellationTokenSource();

            source.CancelAfter(s_timeout);

            var message = new WebSocketRequest
            {
                MessageType = "addEventSubscription",
                Data        = new
                {
                    Level = new object()
                }
            };

            using var socket = await _socketClient.ConnectAsync(_baseUrl, source.Token);

            socket.State.Should().Be(WebSocketState.Open);

            source = new CancellationTokenSource();
            source.CancelAfter(s_timeout);
            await socket.SendAsync(Serializer.Serialize(message), WebSocketMessageType.Text, true, source.Token);

            var counter = 0;

            while (true)
            {
                source = new CancellationTokenSource();
                source.CancelAfter(s_timeout);

                try
                {
                    var array  = new ArraySegment <byte>(new byte[4096]);
                    var result = await socket.ReceiveAsync(array, source.Token);

                    var json = Serializer.Deserialize(array, result);
                    json["messageType"].Value <string>().Should().Be("event");
                    json["data"]["level"].Type.Should().Be(JTokenType.Object);
                    json["data"]["level"]["data"].Type.Should().Be(JTokenType.Integer);
                    json["data"]["level"]["timestamp"].Type.Should().Be(JTokenType.Date);
                    break;
                }
                catch (Exception)
                {
                    counter++;

                    if (counter >= s_retries)
                    {
                        throw;
                    }
                }
            }

            await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
        }
        public async Task RequestLongRunningAction_Should_ReturnOk()
        {
            var source = new CancellationTokenSource();

            source.CancelAfter(s_timeout);


            var message = new WebSocketRequest
            {
                MessageType = "requestAction",
                Data        = new
                {
                    LongRunning = new
                    {
                        Input = new object()
                    }
                }
            };

            using var socket = await _socketClient.ConnectAsync(_baseUrl, source.Token);

            socket.State.Should().Be(WebSocketState.Open);

            source = new CancellationTokenSource();
            source.CancelAfter(s_timeout);
            await socket.SendAsync(Serializer.Serialize(message), WebSocketMessageType.Text, true, source.Token);

            var counterError = 0;
            var status       = 0;

            while (status < 3)
            {
                source = new CancellationTokenSource();
                source.CancelAfter(s_timeout);

                try
                {
                    var array  = new ArraySegment <byte>(new byte[4096]);
                    var result = await socket.ReceiveAsync(array, source.Token);

                    var json = Serializer.Deserialize(array, result);
                    json["messageType"].Value <string>().Should().Be("actionStatus");
                    json["data"].Type.Should().Be(JTokenType.Object);
                    json["data"]["longRunning"].Type.Should().Be(JTokenType.Object);

                    json["data"]["longRunning"]["status"].Type.Should().Be(JTokenType.String);
                    json["data"]["longRunning"]["href"].Type.Should().Be(JTokenType.String);

                    if (status == 0)
                    {
                        json["data"]["longRunning"]["status"].Value <string>().Should().BeOneOf("created");
                        status++;
                        counterError = 0;
                    }

                    if (status == 1)
                    {
                        json["data"]["longRunning"]["status"].Value <string>().Should().BeOneOf("pending");
                        status++;
                        counterError = 0;
                    }

                    if (status == 2)
                    {
                        json["data"]["longRunning"]["status"].Value <string>().Should().BeOneOf("completed");
                        status++;
                        counterError = 0;
                    }
                }
                catch (Exception)
                {
                    counterError++;

                    if (counterError >= s_retries)
                    {
                        throw;
                    }
                }
            }

            await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
        }
        public async Task RequestWithRestrictionAction_Should_ReturnOk(int level, string id)
        {
            var source = new CancellationTokenSource();

            source.CancelAfter(s_timeout);

            var value  = _fixture.Create <string>();
            var active = _fixture.Create <bool>();

            var message = new WebSocketRequest
            {
                MessageType = "requestAction",
                Data        = new
                {
                    WithRestriction = new
                    {
                        Input = new
                        {
                            Value  = value,
                            Level  = level,
                            Active = active,
                            Id     = Guid.Parse(id)
                        }
                    }
                }
            };

            using var socket = await _socketClient.ConnectAsync(_baseUrl, source.Token);

            socket.State.Should().Be(WebSocketState.Open);

            source = new CancellationTokenSource();
            source.CancelAfter(s_timeout);
            await socket.SendAsync(Serializer.Serialize(message), WebSocketMessageType.Text, true, source.Token);

            var counterError = 0;
            var status       = 0;

            while (status < 3)
            {
                source = new CancellationTokenSource();
                source.CancelAfter(s_timeout);

                try
                {
                    var array  = new ArraySegment <byte>(new byte[4096]);
                    var result = await socket.ReceiveAsync(array, source.Token);

                    var json = Serializer.Deserialize(array, result);
                    json["messageType"].Value <string>().Should().Be("actionStatus");
                    json["data"].Type.Should().Be(JTokenType.Object);
                    json["data"]["withRestriction"].Type.Should().Be(JTokenType.Object);
                    json["data"]["withRestriction"]["input"].Type.Should().Be(JTokenType.Object);

                    json["data"]["withRestriction"]["input"]["level"].Type.Should().Be(JTokenType.Integer);
                    json["data"]["withRestriction"]["input"]["level"].Value <int>().Should().Be(level);

                    json["data"]["withRestriction"]["input"]["active"].Type.Should().Be(JTokenType.Boolean);
                    json["data"]["withRestriction"]["input"]["active"].Value <bool>().Should().Be(active);

                    json["data"]["withRestriction"]["input"]["id"].Type.Should().Be(JTokenType.String);
                    json["data"]["withRestriction"]["input"]["id"].Value <string>().ToUpper().Should().Be(id.ToString().ToUpper());

                    json["data"]["withRestriction"]["status"].Type.Should().Be(JTokenType.String);
                    json["data"]["withRestriction"]["href"].Type.Should().Be(JTokenType.String);

                    if (status == 0)
                    {
                        json["data"]["withRestriction"]["status"].Value <string>().Should().BeOneOf("created");
                        status++;
                        counterError = 0;
                    }

                    if (status == 1)
                    {
                        json["data"]["withRestriction"]["status"].Value <string>().Should().BeOneOf("pending");
                        status++;
                        counterError = 0;
                    }

                    if (status == 2)
                    {
                        json["data"]["withRestriction"]["status"].Value <string>().Should().BeOneOf("completed");
                        status++;
                        counterError = 0;
                    }
                }
                catch (Exception)
                {
                    counterError++;

                    if (counterError >= s_retries)
                    {
                        throw;
                    }
                }
            }

            await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
        }