public async Task GetLatestAsync_WhenFound_ReturnsShortUrl() { var expectedAdverb = GenerateWord <Adverb>(false, false); var expectedAdjective = GenerateWord <Adjective>(false, false); var expectedNoun = GenerateWord <Noun>(false, false); var expectedUrl = new Url { DateCreated = DateTimeOffset.UtcNow, TargetUrl = "https://localhost/" }; _wordsServiceMock.Setup(m => m.GetWordByIdAsync <Adverb>(It.IsAny <int>())) .ReturnsAsync(expectedAdverb); _wordsServiceMock.Setup(m => m.GetWordByIdAsync <Adjective>(It.IsAny <int>())) .ReturnsAsync(expectedAdjective); _wordsServiceMock.Setup(m => m.GetWordByIdAsync <Noun>(It.IsAny <int>())) .ReturnsAsync(expectedNoun); _urlServiceMock.Setup(m => m.GetLatestAsync(It.IsAny <string>())).ReturnsAsync(expectedUrl); var repository = new ShortUrlRepository(_urlServiceMock.Object, _wordsServiceMock.Object, _configuration); var actual = await repository.GetLatestAsync("en"); Assert.Equal(expectedAdverb.Id, actual.Adverb.Id); Assert.Equal(expectedAdverb.Value, actual.Adverb.Value); Assert.Equal(expectedAdjective.Id, actual.Adjective.Id); Assert.Equal(expectedAdjective.Value, actual.Adjective.Value); Assert.Equal(expectedNoun.Id, actual.Noun.Id); Assert.Equal(expectedNoun.Value, actual.Noun.Value); Assert.Equal("https://localhost/", actual.BaseDomain); Assert.Equal(expectedUrl.TargetUrl, actual.TargetUrl); Assert.Equal(expectedUrl.DateCreated, actual.DateCreated); Assert.Equal($"https://localhost/{expectedAdverb.Value}{expectedAdjective.Value}{expectedNoun.Value}", actual.FullUri.ToString()); }
static void Main(string[] args) { ShortUrlRepository das = new ShortUrlRepository(); string str = Console.ReadLine(); Console.WriteLine(das.lengthenUrl(str)); }
public void TestInitalise() { var Urls = new List<ShortUrlEntity>(); Urls.Add(new ShortUrlEntity { Id = 0, Nsfw = false, RedirectionUrl = "http://www.google.com/", ShortCode = "google", Views = 0 }); Urls.Add(new ShortUrlEntity { Id = 1, Nsfw = true, RedirectionUrl = "http://www.google2.com/", ShortCode = "google2", Views = 20 }); var dbSet = new Mock<IEntitySet<ShortUrlEntity>>(); dbSet.Setup(s => s.GetEnumerator()).Returns(Urls.GetEnumerator()); var context = new Mock<IShortUrlContext>(); context.Setup(r => r.EntitySet<ShortUrlEntity>()).Returns(dbSet.Object); repo = new ShortUrlRepository(context.Object); }
public void TestInitialize() { //Arrange _mockShortUrls = new Mock <DbSet <ShortUrl> >(); _mockContext = new Mock <IDataContext>(); _mockContext.Setup(x => x.ShortUrls).Returns(_mockShortUrls.Object); _shortUrlRepository = new ShortUrlRepository(_mockContext.Object); }
public void GetValidUrls_WhenCalled_ReturnsValidOnly() { var shortUrlsList = new List <ShortUrl> { new ShortUrl() { OriginalUrl = "www.DeltaTre1.com", ShortenUrl = "12345678", InsertDate = DateTime.Now.AddMonths(-1) }, new ShortUrl() { OriginalUrl = "www.DeltaTre2.com", ShortenUrl = "12345677", InsertDate = DateTime.Now.AddMonths(-2) }, new ShortUrl() { OriginalUrl = "www.DeltaTre3.com", ShortenUrl = "12345676", InsertDate = DateTime.Now.AddMonths(-3) }, new ShortUrl() { OriginalUrl = "www.DeltaTre4.com", ShortenUrl = "12345675", InsertDate = DateTime.Now.AddMonths(-4) }, new ShortUrl() { OriginalUrl = "www.DeltaTre5.com", ShortenUrl = "12345674", InsertDate = DateTime.Now.AddMonths(-5) }, new ShortUrl() { OriginalUrl = "www.DeltaTre6.com", ShortenUrl = "12345673", InsertDate = DateTime.Now.AddMonths(-6) } }; _context.ShortUrls.AddRange(shortUrlsList); _context.SaveChanges(); var shortUrlRepository = new ShortUrlRepository(_context); var validUrl = shortUrlRepository.GetValidUrls(3).Count; Assert.That(validUrl, Is.EqualTo(3)); }
public async Task CreateAsync_WhenUrlExistsForTargetUrl_UrlIsReused() { var expectedUrl = GenerateUrl(); _urlServiceMock.Setup(m => m.FindWithTargetUrlAsync(It.IsAny <string>(), It.IsAny <string>())) .ReturnsAsync(expectedUrl); var repository = new ShortUrlRepository(_urlServiceMock.Object, _wordsServiceMock.Object, _configuration); var actual = await repository.CreateAsync("https://localhost/", "en", PartOfSpeech.Adjective); Assert.Equal(expectedUrl.DateCreated, actual.DateCreated); Assert.Equal(expectedUrl.TargetUrl, actual.TargetUrl); _urlServiceMock.Verify(m => m.FindWithTargetUrlAsync(It.IsAny <string>(), It.IsAny <string>()), Times.Once); _wordsServiceMock.Verify(m => m.GetWordByIdAsync <Adverb>(expectedUrl.AdverbId), Times.Once); _wordsServiceMock.Verify(m => m.GetWordByIdAsync <Adjective>(expectedUrl.AdjectiveId), Times.Once); _wordsServiceMock.Verify(m => m.GetWordByIdAsync <Noun>(expectedUrl.NounId), Times.Once); }
public JsonResult <string> LengthenUrl(string url) { try { ShortUrlRepository dal = new ShortUrlRepository(); var fullUrl = dal.lengthenUrl(url); return(Json <string>(fullUrl)); } catch (Exception ex) { var response = new HttpResponseMessage(HttpStatusCode.GatewayTimeout) { Content = new StringContent("Unable to connect to database", System.Text.Encoding.UTF8, "text/plain"), StatusCode = HttpStatusCode.GatewayTimeout }; throw new HttpResponseException(response); } }
public async Task CreateAsync_WhenCannotReuse_UsesRandomWordsUntilAvailable() { var expectedAdverb = GenerateWord <Adverb>(false, false); var expectedAdjective = GenerateWord <Adjective>(false, false); var expectedNoun = GenerateWord <Noun>(false, false); _wordsServiceMock.Setup(m => m.GetRandomWordAsync <Adverb>(It.IsAny <string>(), It.IsAny <bool>())) .ReturnsAsync(expectedAdverb); _wordsServiceMock.Setup(m => m.GetRandomWordAsync <Adjective>(It.IsAny <string>(), It.IsAny <bool>())) .ReturnsAsync(expectedAdjective); _wordsServiceMock.Setup(m => m.GetRandomWordAsync <Noun>(It.IsAny <string>(), It.IsAny <bool>())) .ReturnsAsync(expectedNoun); _urlServiceMock.SetupSequence(m => m.IsAvailableAsync(It.IsAny <Url>())) .ReturnsAsync(false).ReturnsAsync(false).ReturnsAsync(false).ReturnsAsync(false).ReturnsAsync(false) .ReturnsAsync(true); const string targetUrl = "https://localhost/"; const string languageCode = "en"; var repository = new ShortUrlRepository(_urlServiceMock.Object, _wordsServiceMock.Object, _configuration); var actual = await repository.CreateAsync(targetUrl, languageCode, PartOfSpeech.Noun); Assert.Equal(expectedAdverb.Id, actual.Adverb.Id); Assert.Equal(expectedAdverb.Value, actual.Adverb.Value); Assert.Equal(expectedAdjective.Id, actual.Adjective.Id); Assert.Equal(expectedAdjective.Value, actual.Adjective.Value); Assert.Equal(expectedNoun.Id, actual.Noun.Id); Assert.Equal(expectedNoun.Value, actual.Noun.Value); Assert.Equal("https://localhost/", actual.BaseDomain); Assert.Equal(targetUrl, actual.TargetUrl); Assert.Equal($"https://localhost/{expectedAdverb.Value}{expectedAdjective.Value}{expectedNoun.Value}", actual.FullUri.ToString()); _urlServiceMock.Verify(m => m.FindWithTargetUrlAsync(It.IsAny <string>(), It.IsAny <string>()), Times.Once); _urlServiceMock.Verify(m => m.IsAvailableAsync(It.IsAny <Url>()), Times.Exactly(6)); _urlServiceMock.Verify(m => m.SaveUrlAsync(It.IsAny <Url>()), Times.Once); _wordsServiceMock.Verify(m => m.GetRandomWordAsync <Adverb>(It.IsAny <string>(), It.IsAny <bool>()), Times.Exactly(6)); _wordsServiceMock.Verify(m => m.GetRandomWordAsync <Adjective>(It.IsAny <string>(), It.IsAny <bool>()), Times.Exactly(6)); _wordsServiceMock.Verify(m => m.GetRandomWordAsync <Noun>(It.IsAny <string>(), It.IsAny <bool>()), Times.Exactly(6)); }
public async Task GetRandomAsync_WhenNotFound_Throws() { var repository = new ShortUrlRepository(_urlServiceMock.Object, _wordsServiceMock.Object, _configuration); await Assert.ThrowsAsync <InvalidOperationException>(() => repository.GetRandomAsync("en")); }
public ShortenController(ShortUrlRepository shortUrlRepository) { _shortUrlRepository = shortUrlRepository; }