Exemplo n.º 1
0
        public async Task CanConnectAndListen()
        {
            var client = new FortnoxWebSocketClient(this.connectionSettings.ClientSecret);
            await client.Connect();

            await client.AddTenant(this.connectionSettings.AccessToken);

            await client.AddTopic(WebSocketTopic.Articles);

            await client.Subscribe();

            var updatedDescription = $"TestArtikel {DateTime.UtcNow}";
            var article            = new Article {
                Description = updatedDescription, ArticleNumber = "100370"
            };
            var request        = new FortnoxApiRequest(this.connectionSettings.AccessToken, this.connectionSettings.ClientSecret);
            var updatedArticle = ArticleService.UpdateArticleAsync(request, article).GetAwaiter().GetResult();

            Assert.AreEqual(updatedDescription, updatedArticle.Description);

            var response = await client.Receive();

            while (response.Type != WebSocketResponseType.EventResponse)
            {
                response = await client.Receive();
            }

            Assert.IsTrue(response.Topic == WebSocketTopic.Articles.ToString().ToLower());
            Assert.IsTrue(response.EventType == WebSocketEventType.ArticleUpdated);
            Assert.IsTrue(response.EntityId == "100370");

            await client.Close();
        }
Exemplo n.º 2
0
        public async Task CanRemoveTenant()
        {
            var client = new FortnoxWebSocketClient(this.connectionSettings.ClientSecret);
            await client.Connect();

            await client.AddTenant(this.connectionSettings.AccessToken);

            var addTenantResponse = await client.Receive();

            Assert.IsTrue(addTenantResponse.Type == WebSocketResponseType.CommandResponse);
            Assert.IsTrue(addTenantResponse.Result.Equals("ok"));
            Assert.IsTrue(addTenantResponse.Response == WebSocketCommands.AddTenants);
            Assert.IsTrue(client.AccessTokenTenantId.Keys.Count == 1);
            Assert.IsTrue(client.AccessTokenTenantId.Values.Count == 1);

            await client.RemoveTenant(client.AccessTokenTenantId[this.connectionSettings.AccessToken]);

            var removeTenantResponse = await client.Receive();

            Assert.IsTrue(removeTenantResponse.Type == WebSocketResponseType.CommandResponse);
            Assert.IsTrue(removeTenantResponse.Result.Equals("ok"));
            Assert.IsTrue(client.AccessTokenTenantId.Keys.Count == 0);
            Assert.IsTrue(client.AccessTokenTenantId.Values.Count == 0);

            await client.Close();
        }
Exemplo n.º 3
0
        public async Task CanHandleCommandResponses()
        {
            var client = new FortnoxWebSocketClient(this.connectionSettings.ClientSecret);
            await client.Connect();

            await client.AddTenant(this.connectionSettings.AccessToken);

            var addTenantResponse = await client.Receive();

            Assert.IsTrue(addTenantResponse.Type == WebSocketResponseType.CommandResponse);
            Assert.IsTrue(addTenantResponse.Result.Equals("ok"));
            Assert.IsTrue(addTenantResponse.Response == WebSocketCommands.AddTenants);


            await client.AddTopic(WebSocketTopic.Articles);

            var addTopicResponse = await client.Receive();

            Assert.IsTrue(addTopicResponse.Type == WebSocketResponseType.CommandResponse);
            Assert.IsTrue(addTopicResponse.Result.Equals("ok"));
            Assert.IsTrue(addTopicResponse.Response == WebSocketCommands.AddTopics);

            await client.Subscribe();

            var subscribeResponse = await client.Receive();

            Assert.IsTrue(subscribeResponse.Type == WebSocketResponseType.CommandResponse);
            Assert.IsTrue(subscribeResponse.Result.Equals("ok"));
            Assert.IsTrue(subscribeResponse.Response == WebSocketCommands.Subscribe);

            await client.Close();
        }
Exemplo n.º 4
0
        public async Task CancellationTokenTest()
        {
            var client = new FortnoxWebSocketClient(this.connectionSettings.ClientSecret);
            await client.Connect();

            await client.AddTenant(this.connectionSettings.AccessToken);

            await client.Receive();

            await client.AddTopic(WebSocketTopic.Articles);

            await client.Receive();

            await client.Subscribe();

            await client.Receive();

            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

            cancellationTokenSource.CancelAfter(2500);
            CancellationToken token = cancellationTokenSource.Token;

            // Receive that will be cancelled after 2,5 seconds.
            await Assert.ThrowsExceptionAsync <TaskCanceledException>(() => client.Receive(token));

            // Underlying ClientWebSocket state will be set to "Aborted" once cancelled so we cannot re-use the socket now.
            await Assert.ThrowsExceptionAsync <WebSocketException>(() => client.Receive(token));

            await client.Close();
        }
Exemplo n.º 5
0
        public async Task CannotConnectTwice()
        {
            var client = new FortnoxWebSocketClient(this.connectionSettings.ClientSecret);
            await client.Connect();

            await Assert.ThrowsExceptionAsync <InvalidOperationException>(() => client.Connect());

            await client.Close();
        }
Exemplo n.º 6
0
        public async Task CanSubscribeToMultipleTopics()
        {
            var listeningTask = new Task(async() =>
            {
                var client = new FortnoxWebSocketClient(this.connectionSettings.ClientSecret);
                await client.Connect();
                await client.AddTenant(this.connectionSettings.AccessToken);
                await client.AddTopic(WebSocketTopic.Articles);
                await client.AddTopic(WebSocketTopic.Orders);
                await client.Subscribe();

                var receivedArticleUpdate = false;
                var receivedOrderUpdate   = false;

                while (!receivedArticleUpdate && !receivedOrderUpdate)
                {
                    var response = await client.Receive();
                    if (response.Type == WebSocketResponseType.EventResponse)
                    {
                        Assert.IsTrue(response.Topic == WebSocketTopic.Articles.ToString() || response.Topic == WebSocketTopic.Orders.ToString());

                        if (response.Topic == WebSocketTopic.Articles.ToString())
                        {
                            receivedArticleUpdate = true;
                        }
                        else if (response.Topic == WebSocketTopic.Orders.ToString())
                        {
                            receivedOrderUpdate = true;
                        }

                        Assert.IsTrue(response.EventType == WebSocketEventType.ArticleUpdated || response.EventType == WebSocketEventType.OrderUpdated);
                        Assert.IsTrue(response.EntityId == "100370" || response.EntityId == "1");

                        if (receivedArticleUpdate && receivedOrderUpdate)
                        {
                            return;
                        }
                    }
                }

                await client.Close();
            }, TaskCreationOptions.LongRunning);

            listeningTask.Start();

            var comment = $"Comment: {DateTime.Now}";
            var request = new OrderListRequest(this.connectionSettings.AccessToken, this.connectionSettings.ClientSecret);
            var order   = new Order {
                DocumentNumber = 1, Comments = comment
            };
            var updatedOrder = OrderService.UpdateOrderAsync(request, order).GetAwaiter().GetResult();

            Assert.AreEqual(comment, updatedOrder.Comments);

            var updatedDescription = $"TestArtikel {DateTime.UtcNow}";
            var article            = new Article {
                Description = updatedDescription, ArticleNumber = "100370"
            };
            var articleRequest = new FortnoxApiRequest(this.connectionSettings.AccessToken, this.connectionSettings.ClientSecret);
            var updatedArticle = ArticleService.UpdateArticleAsync(articleRequest, article).GetAwaiter().GetResult();

            Assert.AreEqual(updatedDescription, updatedArticle.Description);

            await listeningTask;
        }