Beispiel #1
0
        public async Task PublishAreaAsync_SucceedsOnValidInputs()
        {
            // Arrange
            NarrowcastMessage request = new NarrowcastMessage();

            request.Area = new NarrowcastArea
            {
                BeginTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
                EndTimestamp   = DateTimeOffset.UtcNow.AddHours(1).ToUnixTimeMilliseconds(),
                Location       = new Coordinates
                {
                    Latitude  = 10.1234,
                    Longitude = -10.1234
                },
                RadiusMeters = 100
            };
            request.UserMessage = "Test user message!";
            string repoResponse = "00000000-0000-0000-0000-0000000000001";

            this._repo
            .Setup(r => r.InsertAsync(It.IsAny <MessageContainer>(), It.IsAny <Region>(), CancellationToken.None))
            .Returns(Task.FromResult(repoResponse));

            // Act
            await this._service.PublishAreaAsync(request, CancellationToken.None);

            // Assert
            // No exceptions thrown
        }
Beispiel #2
0
        public async Task PublishAsync_ArgumentNullOnNullRegion()
        {
            // Arrange
            MessageContainer  request    = new MessageContainer();
            NarrowcastMessage narrowcast = new NarrowcastMessage
            {
                UserMessage = "Test user message"
            };

            narrowcast.Area = new NarrowcastArea
            {
                BeginTimestamp = 0,
                EndTimestamp   = 1,
                Location       = new Coordinates
                {
                    Latitude  = 10.1234,
                    Longitude = -10.1234
                },
                RadiusMeters = 100
            };
            request.Narrowcasts.Add(narrowcast);
            request.BluetoothSeeds.Add(new BluetoothSeedMessage
            {
                EndTimestamp   = 1,
                BeginTimestamp = 0,
                Seed           = "00000000-0000-0000-0000-000000000000"
            });

            // Act
            string result = await this._service
                            .PublishAsync(request, null, CancellationToken.None);

            // Assert
            // Exception caught by decorator
        }
        public async Task PutAsync_BadRequestWithNoUserMessage()
        {
            // Arrange
            NarrowcastMessage requestObj = new NarrowcastMessage();

            requestObj.Area = new Area
            {
                BeginTime = 0,
                EndTime   = 1,
                Location  = new Location
                {
                    Latitude  = 10.1234,
                    Longitude = 10.1234
                },
                RadiusMeters = 100
            };

            // Act
            ActionResult controllerResponse = await this._controller
                                              .PutAsync(requestObj, CancellationToken.None);

            // Assert
            Assert.IsNotNull(controllerResponse);
            Assert.IsInstanceOfType(controllerResponse, typeof(BadRequestObjectResult));
        }
        public async Task PutAsync_OkWithValidInputs()
        {
            // Arrange
            NarrowcastMessage requestObj = new NarrowcastMessage
            {
                UserMessage = "User message content"
            };

            requestObj.Area = new Area
            {
                BeginTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
                EndTime   = DateTimeOffset.UtcNow.AddHours(1).ToUnixTimeMilliseconds(),
                Location  = new Location
                {
                    Latitude  = 10.1234,
                    Longitude = 10.1234
                },
                RadiusMeters = 100
            };

            // Act
            ActionResult controllerResponse = await this._controller
                                              .PutAsync(requestObj, CancellationToken.None);

            // Assert
            Assert.IsNotNull(controllerResponse);
            Assert.IsInstanceOfType(controllerResponse, typeof(OkResult));
        }
Beispiel #5
0
        /// <inheritdoc/>
        public async Task PublishAreaAsync(NarrowcastMessage message, CancellationToken cancellationToken = default)
        {
            // Validate inputs
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            RequestValidationResult validationResult = message.Validate();

            if (validationResult.Passed)
            {
                // Build a MessageContainer for the submitted NarrowcastMessage
                MessageContainer container = new MessageContainer();
                container.Narrowcasts.Add(message);

                // Define regions for the published message
                IEnumerable <Region> messageRegions = RegionHelper.GetRegionsCoverage(message.Area, this.PrecisionMin, this.PrecisionMax);

                // Publish
                await this._reportRepo.InsertAsync(container, messageRegions, cancellationToken);
            }
            else
            {
                throw new RequestValidationFailedException(validationResult);
            }
        }
        public async Task PutAsync_BadRequestObjectWithNoAreas()
        {
            // Arrange
            NarrowcastMessage requestObj = new NarrowcastMessage
            {
                UserMessage = "This is a message"
            };

            // Act
            ActionResult controllerResponse = await this._controller
                                              .PutAsync(requestObj, CancellationToken.None);

            // Assert
            Assert.IsNotNull(controllerResponse);
            Assert.IsInstanceOfType(controllerResponse, typeof(BadRequestObjectResult));
        }
Beispiel #7
0
        public async Task PublishAsync_SucceedsOnValidMessage()
        {
            // Arrange
            string repoResponse = "00000000-0000-0000-0000-000000000002";
            Region region       = new Region(10, -10, 4);

            MessageContainer  request    = new MessageContainer();
            NarrowcastMessage narrowcast = new NarrowcastMessage
            {
                UserMessage = "Test user message"
            };

            narrowcast.Area = new NarrowcastArea
            {
                BeginTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
                EndTimestamp   = DateTimeOffset.UtcNow.AddHours(1).ToUnixTimeMilliseconds(),
                Location       = new Coordinates
                {
                    Latitude  = 10.1234,
                    Longitude = -10.1234
                },
                RadiusMeters = 100
            };
            request.Narrowcasts.Add(narrowcast);
            request.BluetoothSeeds.Add(new BluetoothSeedMessage
            {
                BeginTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
                EndTimestamp   = DateTimeOffset.UtcNow.AddHours(1).ToUnixTimeMilliseconds(),
                Seed           = "00000000-0000-0000-0000-000000000001"
            });

            this._repo
            .Setup(r => r.InsertAsync(It.IsAny <MessageContainer>(), It.IsAny <Region>(), CancellationToken.None))
            .Returns(Task.FromResult(repoResponse));

            // Act
            string result = await this._service
                            .PublishAsync(request, region, CancellationToken.None);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(repoResponse, result);
        }
        public async Task <ActionResult> PutAsync([Required] AreaMatch request, CancellationToken cancellationToken = default)
        {
            try
            {
                // Parse AreaMatch to AreaReport type
                NarrowcastMessage report = this._map.Map <NarrowcastMessage>(request);

                // Publish area
                await this._reportService.PublishAreaAsync(report, cancellationToken);

                return(Ok());
            }
            catch (RequestValidationFailedException ex)
            {
                // Only return validation issues
                return(BadRequest(ex.ValidationResult));
            }
            catch (ArgumentNullException)
            {
                return(BadRequest());
            }
        }
        /// <summary>
        /// Sends a push message to multiple users. You can specify recipients using attributes (such as age, gender, OS, and region) or by retargeting (audiences). Messages cannot be sent to groups or rooms.
        /// </summary>
        /// <param name="narrowcastMessage">Narrowcast message.</param>
        /// <param name="retryKey">
        /// * The retry key lets you retry a request while preventing the same request from being accepted in duplicate. For more information, see Retrying a failed API request.<br/>
        /// * The retry key is valid for 24 hours after attempting the first request.<br/>
        /// See <a href="https://developers.line.biz/en/reference/messaging-api/#retry-api-request">Here</a>.
        /// </param>
        /// <returns>Messaging API response.</returns>
        /// <remarks>See <a href="https://developers.line.biz/en/reference/messaging-api/#send-narrowcast-message">Here</a>.</remarks>
        public async Task <MessagingApiResponse> SendNarrowcastMessageAsync(NarrowcastMessage narrowcastMessage, Guid?retryKey = null)
        {
            var json = JsonConvert.SerializeObject(narrowcastMessage, _settings);

            return(await SendNarrowcastMessageAsync(json, retryKey));
        }