コード例 #1
0
        public void CreateNew_WhenLocationIsNotValid_ShouldThrowUseCaseException()
        {
            //ARRANGE
            const string userName         = "******";
            const string eventDescription = "event description";
            const string eventName        = "event name";
            const string locationName     = "Skolas iela, Riga, Latvia";
            const double latitude         = 22;
            const double longitude        = 33;
            DateTime?    eventStart       = new DateTime(1999, 12, 1);
            DateTime?    eventEnd         = new DateTime(1999, 12, 2);
            var          memberId         = new Guid("41FEB6C1-DA81-4E9D-8ABA-63665DF0B9CC");
            var          categoryId       = new Guid("7BC0C011-7FA3-4AA4-B260-257465831D65");

            var coordServiceResult = new CoordServiceResult
            {
                Latitude  = latitude,
                Longitude = longitude,
                Message   = "location message",
                Success   = false
            };

            var createEventRequest = new CreateEventRequest
            {
                Categories = new List <CategoryDto> {
                    new CategoryDto {
                        CategoryId = categoryId
                    }
                },
                Locations = new List <LocationDto>
                {
                    new LocationDto {
                        EventStart = eventStart, EventEnd = eventEnd, Name = locationName
                    }
                },
                Members = new List <MemberDto> {
                    new MemberDto {
                        Id = memberId
                    }
                },
                Name        = eventName,
                Description = eventDescription
            };

            _coordServiceMock.Setup(cs => cs.Lookup(locationName))
            .ReturnsAsync(coordServiceResult);

            //ACT & ASSERT
            Assert.ThrowsAsync <UseCaseException>(() => _eventService.Create(createEventRequest, userName));
        }
コード例 #2
0
ファイル: CoordService.cs プロジェクト: pawelby/pawelby
        /// <summary>
        /// Lookup coordinates
        /// </summary>
        /// <param name="location">Location</param>
        /// <returns>Coordinate service result</returns>
        public async Task<CoordServiceResult> Lookup(string location)
        {
            var result = new CoordServiceResult()
            {
                Success = false,
                Message = "Undeterminated failure while looking up coordinates"
            };

            //Lookup Coordinates
            var bingKey = Startup.Configuration["AppSettings:BingKey"];
            var encodedName = WebUtility.UrlEncode(location);
            var url = $"http://dev.virtualearth.net/REST/v1/Locations?q={encodedName}&key={bingKey}";

            var client = new HttpClient();

            var json = await client.GetStringAsync(url);

            var results = JObject.Parse(json);
            var resources = results["resourceSets"][0]["resources"];
            if (!resources.HasValues)
            {
                result.Message = $"Could not find '{location}' as a location";
            }
            else
            {
                var confidence = (string) resources[0]["confidence"];
                if (confidence != "High")
                {
                    result.Message = $"Could not find a confident match for '{location}' as a location";
                }
                else
                {
                    var coords = resources[0]["geocodePoints"][0]["coordinates"];
                    result.Latitude = (double) coords[0];
                    result.Longitude = (double) coords[1];
                    result.Success = true;
                    result.Message = "Success";
                }
            }

            return result;
        }
コード例 #3
0
        public async Task CreateNew_ShouldSuccess()
        {
            //ARRANGE
            const string userName         = "******";
            const string eventDescription = "event description";
            const string eventName        = "event name";
            const string locationName     = "Skolas iela 1, Riga, Latvia";
            const double latitude         = 22;
            const double longitude        = 33;
            DateTime?    eventStart       = new DateTime(1999, 12, 1);
            DateTime?    eventEnd         = new DateTime(1999, 12, 2);
            var          memberId         = new Guid("41FEB6C1-DA81-4E9D-8ABA-63665DF0B9CC");
            var          categoryId       = new Guid("7BC0C011-7FA3-4AA4-B260-257465831D65");

            var coordServiceResult = new CoordServiceResult
            {
                Latitude  = latitude,
                Longitude = longitude,
                Message   = "location message",
                Success   = true
            };

            var createEventRequest = new CreateEventRequest
            {
                Categories = new List <CategoryDto> {
                    new CategoryDto {
                        CategoryId = categoryId
                    }
                },
                Locations = new List <LocationDto>
                {
                    new LocationDto {
                        EventStart = eventStart, EventEnd = eventEnd, Name = locationName
                    }
                },
                Members = new List <MemberDto> {
                    new MemberDto {
                        Id = memberId
                    }
                },
                Name        = eventName,
                Description = eventDescription
            };

            Event @event = null;

            _eventRepositoryMock.Setup(er => er.AddEvent(It.IsNotNull <Event>()))
            .Callback <Event>(e => @event = e);

            _eventRepositoryMock.Setup(er => er.SaveAll()).ReturnsAsync(true);

            _coordServiceMock.Setup(cs => cs.Lookup(locationName))
            .ReturnsAsync(coordServiceResult);

            //ACT
            await _eventService.Create(createEventRequest, userName);

            //ASSERT
            var expected = new Event
            {
                UserName     = userName,
                EventMembers = new List <EventMember>
                {
                    new EventMember
                    {
                        MemberId = memberId
                    }
                },
                EventCategories = new List <EventCategory>
                {
                    new EventCategory
                    {
                        CategoryId = categoryId
                    }
                },
                Locations = new List <Location>
                {
                    new Location
                    {
                        EventEnd   = eventEnd.Value,
                        EventStart = eventStart.Value,
                        Latitude   = latitude,
                        Longitude  = longitude,
                        Name       = locationName
                    }
                },
                Description = eventDescription,
                Name        = eventName
            };

            ContentAssert.AreEqual(expected, @event);
        }