public void UpdateNotConfirmedUser() { var mocker = new RouteServiceMocker(); RouteInfoEntity entity = new RouteInfoEntity(); mocker.RepoMock.Setup(r => r.Update(It.IsAny <RouteInfoEntity>())).Callback(() => Console.WriteLine("Unit test update")); mocker.RepoMock.Setup(r => r.GetSingle(It.IsAny <int>())).Returns(() => new RouteInfoEntity() { ID = 1, Name = "test", UserId = 2 }); mocker.RepoMock.Setup(r => r.Commit()).Throws(new Exception("testexception")); mocker.UserResolverMock.Setup(r => r.GetUser()) .Returns(() => Task.FromResult <UserModel>(new UserModel() { Email = "*****@*****.**", Id = 2, Name = "testowy" })); mocker.UserResolverMock.Setup(r => r.IsConfirmed()).Returns(() => Task.FromResult <bool>(false)); IRouteBLService service = mocker.GetService(); var result = service.Update(0, new RouteUpdateModel()); Assert.Equal(ErrorStatus.Forbidden, result.Result.Error.Status); Assert.Equal("User is not confirmed", result.Result.Error.Message); }
public void RemoveUnhandledException() { var mocker = new RouteServiceMocker(); RouteInfoEntity entity = new RouteInfoEntity(); mocker.RepoMock.Setup(r => r.Delete(It.IsAny <RouteInfoEntity>())).Callback(() => Console.WriteLine("Unit test delete")); mocker.RepoMock.Setup(r => r.GetSingle(It.IsAny <int>())).Returns(() => new RouteInfoEntity() { ID = 1, Name = "test", UserId = 2 }); mocker.RepoMock.Setup(r => r.Commit()).Throws(new Exception("testexception")); mocker.UserResolverMock.Setup(r => r.GetUser()).Returns(() => Task.FromResult <UserModel>(new UserModel() { Email = "*****@*****.**", Id = 2, Name = "testowy" })); mocker.UserResolverMock.Setup(r => r.IsConfirmed()).Returns(() => Task.FromResult <bool>(true)); IRouteBLService service = mocker.GetService(); var result = service.Remove(new IdModel()); Assert.Equal(ErrorStatus.InternalServer, result.Result.Error.Status); Assert.Equal("testexception", result.Result.Error.Message); }
public async Task <Result <int> > Create(RouteCreateModel item) { try { if (await _userResolver.IsConfirmed()) { string line = $"LINESTRING({item.Line})"; bool isValidLine = _routeRepository.IsValidLine(line); if (isValidLine) { string point = RouteHelper.GetFirstPoint(item.Line); var user = await _userResolver.GetUser(); var infoEntity = new RouteInfoEntity() { Difficulty = item.Difficulty, Name = item.Name, Geometry = new RouteGeomEntity() { Line = line, Status = 1 }, UserId = user.Id, Point = new RoutePointEntity() { Point = $"POINT({point})", Status = 1 } }; _routeRepository.Add(infoEntity); await _routeRepository.Commit(); return(new Result <int>(infoEntity.ID)); } else { ErrorResult error = GenerateError("Invalid route line", "Line", "Invalid geometry", ErrorStatus.InvalidModel); return(new Result <int>(error)); } } else { ErrorResult error = GenerateError("User is not confirmed", "", "", ErrorStatus.Forbidden); return(new Result <int>(error)); } } catch (Exception ex) { ErrorResult error = GenerateError(ex); return(new Result <int>(error)); } }
public void GetGeometriesEmptyRoutes() { var mocker = new RouteServiceMocker(); RouteInfoEntity entity = new RouteInfoEntity(); mocker.RepoMock.Setup(r => r.GetGeometries(It.IsAny <int[]>())).Callback(() => new List <RouteGeomEntity>()); IRouteBLService service = mocker.GetService(); var result = service.GetGeometries(string.Empty); Assert.Equal(ErrorStatus.ObjectNotFound, result.Result.Error.Status); Assert.Equal("Route not found", result.Result.Error.Message); }
public void UpdateWrongId() { var mocker = new RouteServiceMocker(); RouteInfoEntity entity = new RouteInfoEntity(); mocker.RepoMock.Setup(r => r.Update(It.IsAny <RouteInfoEntity>())).Callback(() => Console.WriteLine("Unit test delete")); mocker.RepoMock.Setup(r => r.GetSingle(It.IsAny <int>())).Returns(() => null); IRouteBLService service = mocker.GetService(); var result = service.Update(0, new RouteUpdateModel()); Assert.Equal("Id", result.Result.Error.Errors.First().Field); Assert.Equal(ErrorStatus.ObjectNotFound, result.Result.Error.Status); }
public void GetAllRoutesUnhandledException() { var request = new CollectionRequestModel(); var mocker = new RouteServiceMocker(); RouteInfoEntity entity = new RouteInfoEntity(); mocker.RepoMock.Setup(r => r.Search(It.IsAny <SearchQuery <RouteInfoEntity> >())).Throws(new Exception("testexception")); IRouteBLService service = mocker.GetService(); var result = service.GetAllRoutes(request); Assert.Equal(ErrorStatus.InternalServer, result.Result.Error.Status); Assert.Equal("testexception", result.Result.Error.Message); }
public void GetRouteByIdInvalidId() { var request = new CollectionRequestModel(); var mocker = new RouteServiceMocker(); RouteInfoEntity entity = new RouteInfoEntity(); mocker.RepoMock.Setup(r => r.GetSingleWithDependencies(It.IsAny <int>())).Returns((() => Task.FromResult((RouteInfoEntity)null))); IRouteBLService service = mocker.GetService(); var result = service.GetRouteById(new IdModel() { Id = 1 }); Assert.Equal(ErrorStatus.ObjectNotFound, result.Result.Error.Status); Assert.Equal("Route not found", result.Result.Error.Message); }
public void GetRouteByIdUnhandledException() { var request = new CollectionRequestModel(); var mocker = new RouteServiceMocker(); RouteInfoEntity entity = new RouteInfoEntity(); mocker.RepoMock.Setup(r => r.GetSingleWithDependencies(It.IsAny <int>())).Throws(new Exception("testexception")); IRouteBLService service = mocker.GetService(); var result = service.GetRouteById(new IdModel() { Id = 1 }); Assert.Equal(ErrorStatus.InternalServer, result.Result.Error.Status); Assert.Equal("testexception", result.Result.Error.Message); }
public void GetGeometriesUnhandledException() { int[] ids = new int[2] { 32, 42 }; var mocker = new RouteServiceMocker(); RouteInfoEntity entity = new RouteInfoEntity(); mocker.RepoMock.Setup(r => r.GetGeometries(It.IsAny <int[]>())).Throws(new Exception("testexception")); IRouteBLService service = mocker.GetService(); var result = service.GetGeometries("32,42"); Assert.Equal(ErrorStatus.InternalServer, result.Result.Error.Status); Assert.Equal("testexception", result.Result.Error.Message); }
public void CreateUnhandledException() { var mocker = new RouteServiceMocker(); RouteInfoEntity entity = new RouteInfoEntity(); mocker.RepoMock.Setup(r => r.Add(It.IsAny <RouteInfoEntity>())).Callback(() => Console.WriteLine("Unit test create")); mocker.RepoMock.Setup(r => r.IsValidLine(It.IsAny <string>())).Returns(() => true); mocker.RepoMock.Setup(r => r.Commit()).Throws(new Exception("testexception")); mocker.UserResolverMock.Setup(r => r.IsConfirmed()).Returns(() => Task.FromResult <bool>(true)); IRouteBLService service = mocker.GetService(); var result = service.Create(new RouteCreateModel()); Assert.Equal(ErrorStatus.InternalServer, result.Result.Error.Status); Assert.Equal("testexception", result.Result.Error.Message); }
public async Task <Result <RouteDetailsViewModel> > GetRouteById(IdModel model) { try { var query = new SearchQuery <RouteInfoEntity>(); RouteInfoEntity result = await _routeRepository.GetSingleWithDependencies(model.Id); if (result == null) { ErrorResult error = GenerateError("Route not found", "Id", "Invalid identifier", ErrorStatus.ObjectNotFound); return(new Result <RouteDetailsViewModel>(error)); } var viewModel = new RouteDetailsViewModel() { Difficulty = result.Difficulty, Id = result.ID, Name = result.Name, UserId = result.UserId, User = new UserListItemModel() { Email = result.User.Email, Id = result.User.ID, Name = result.User.Name, RoutesCount = result.User.Routes.Count }, Geometry = new RouteGeometry() { Id = result.Geometry.ID, Points = GeoPointConverter.GetPoints(result.Geometry.Line), }, MainPoint = GeoPointConverter.GetPoint(result.Point.Point) }; return(new Result <RouteDetailsViewModel>(viewModel)); } catch (Exception ex) { ErrorResult error = GenerateError(ex); return(new Result <RouteDetailsViewModel>(error)); } }
public void GetAllRoutesValidResult() { var request = new CollectionRequestModel(); var mocker = new RouteServiceMocker(); RouteInfoEntity entity = new RouteInfoEntity(); mocker.RepoMock.Setup(r => r.Search(It.IsAny <SearchQuery <RouteInfoEntity> >())) .Returns(() => Task.FromResult(new PagingResult <RouteInfoEntity>() { Count = 2, HasNext = false, Results = new List <RouteInfoEntity>() { new RouteInfoEntity() { Difficulty = 2, ID = 1, Name = "First" }, new RouteInfoEntity() { Difficulty = 3, ID = 2, Name = "Second" } } })); IRouteBLService service = mocker.GetService(); var result = service.GetAllRoutes(request); Assert.Equal(true, result.Result.IsOk); Assert.Equal(2, result.Result.Content.Count); Assert.Equal(false, result.Result.Content.HasNext); }
public void RemoveValidResult() { var mocker = new RouteServiceMocker(); RouteInfoEntity entity = new RouteInfoEntity(); mocker.RepoMock.Setup(r => r.Delete(It.IsAny <RouteInfoEntity>())).Callback(() => Console.WriteLine("Unit test delete")); mocker.RepoMock.Setup(r => r.GetSingle(It.IsAny <int>())).Returns(() => new RouteInfoEntity() { ID = 1, Name = "test", UserId = 2 }); mocker.UserResolverMock.Setup(r => r.GetUser()).Returns(() => Task.FromResult <UserModel>(new UserModel() { Email = "*****@*****.**", Id = 2, Name = "testowy" })); mocker.UserResolverMock.Setup(r => r.IsConfirmed()).Returns(() => Task.FromResult <bool>(true)); IRouteBLService service = mocker.GetService(); var result = service.Remove(new IdModel()); Assert.True(result.Result.IsOk); }
private async Task <bool> CheckUsersPermission(RouteInfoEntity entity) { var user = await _userResolver.GetUser(); return(user != null && (user.Id == entity.UserId || user.Role == Role.Admin)); }