public async Task CreateHero(CreateHeroDTO hero, bool skipMethodForTest = false) { string imgUrl = ""; string coverImgUrl = ""; if (string.IsNullOrEmpty(hero.Description) || string.IsNullOrEmpty(hero.Gender) || string.IsNullOrEmpty(hero.Name) || string.IsNullOrEmpty(hero.RealName) || hero.Image == null || hero.CoverImage == null) { throw new InvalidOperationException("Fields cannot be null or empty."); } //Skip this method for unit testing if (!skipMethodForTest) { imgUrl = this._imageService.AddToCloudinaryAndReturnHeroImageUrl(hero.Image); coverImgUrl = this._imageService.AddToCloudinaryAndReturnHeroImageUrl(hero.CoverImage); } var heroObj = new Hero { Name = hero.Name, Description = hero.Description, Image = imgUrl, CoverImage = coverImgUrl, RealName = hero.RealName, Birthday = hero.Birthday.Date, Gender = hero.Gender }; var movieTitleList = new List <Movie>(); if (hero.MovieTitle.Length > 0 && hero.MovieTitle != null) { //Replacing and removing all the symbols that prevent the 'movie title' come how its expected. var movieTitles = hero.MovieTitle[0].Replace("\"", "").Replace("\\", "").TrimStart(' ', '"', ']', '\\', '/', '[').TrimEnd(' ', '"', ']', '\\', '/', '[').Split(",", StringSplitOptions.RemoveEmptyEntries); foreach (var title in movieTitles) { var movieTitle = new Movie { Title = title, HeroId = heroObj.Id }; movieTitleList.Add(movieTitle); } heroObj.Movies = movieTitleList; } await this._heroRepository.AddAsync(heroObj); await this._heroRepository.SaveChangesAsync(); }
public async Task <ActionResult <CreateHeroDTO> > CreateHero([FromForm] CreateHeroDTO hero) { if (!ModelState.IsValid) { return(this.NoContent()); } _logger.LogInfo("Creating a new hero..."); await this._heroService.CreateHero(hero); _logger.LogInfo($"Hero with name {hero.Name} successfully created."); return(this.CreatedAtAction("GetAllHeroes", new { name = hero.Name })); }
public async Task CreateHero_WithIncorrectData_ShouldThrowInvalidOperationException(string name, string realName, string description, string gender) { var repo = new Mock <IRepository <Hero> >(); var fileMock = new Mock <IFormFile>(); this._heroService = new HeroService(repo.Object, new ImageService(), null, null); var createHeroDto = new CreateHeroDTO { Name = name, RealName = realName, Birthday = DateTime.Now, Description = description, Image = fileMock.Object, CoverImage = fileMock.Object, Gender = gender, MovieTitle = Array.Empty <string>() }; await Assert.ThrowsAsync <InvalidOperationException>(() => this._heroService.CreateHero(createHeroDto)); }
public void CreateHero_WithCorrectData_ShouldCreateHeroSuccessfully() { string errorMessagePrefix = "HeroService CreateHero() method does not work properly."; var repo = new Mock <IRepository <Hero> >(); var fileMock = new Mock <IFormFile>(); const string content = "Hello World from a Fake File"; const string fileName = "profileImg.jpg"; var ms = new MemoryStream(); var writer = new StreamWriter(ms); writer.Write(content); writer.Flush(); ms.Position = 0; fileMock.Setup(_ => _.OpenReadStream()).Returns(ms); fileMock.Setup(_ => _.FileName).Returns(fileName); fileMock.Setup(_ => _.Length).Returns(ms.Length); repo.Setup(r => r.All()).Returns(this.GetTestData().AsQueryable); this._heroService = new HeroService(repo.Object, new ImageService(), null, null); var createHeroDto = new CreateHeroDTO { Name = "11", RealName = "123", Birthday = DateTime.Now, Description = "123123123123123", Image = fileMock.Object, CoverImage = fileMock.Object, Gender = "Male", MovieTitle = Array.Empty <string>() }; var actual = this._heroService.CreateHero(createHeroDto, true).IsCompletedSuccessfully; Assert.True(actual, errorMessagePrefix); }