Пример #1
0
        public async Task CreateSmartUrlShouldGenerateUniqueKey()
        {
            var managedConfig    = ConfigureManagedConfig();
            var smartUriResponse = new SmartUrlEntity {
                Url = _url
            };
            var dataProviderMock = new Mock <IDataProvider>();

            dataProviderMock.Setup(a => a.GetSmartUrlByHash(It.IsAny <string>()))
            .Returns(Task.FromResult((SmartUrlEntity)null));
            dataProviderMock.Setup(a => a.GetSmartUrlByKey(It.IsAny <string>()))
            .Returns(Task.FromResult((SmartUrlEntity)null));
            dataProviderMock.Setup(a => a.Add(It.IsAny <SmartUrlEntity>()))
            .Returns(Task.FromResult(0));

            var sut = new HashKeyService(managedConfig, dataProviderMock.Object);


            // Act
            var result = await sut.CreateSmartUrl(string.Empty);


            // Assert
            result.ShortUrl.ShouldContain("https://localhost:44390");
            dataProviderMock.Verify(a => a.GetSmartUrlByHash(It.IsAny <string>()), Times.Once);
            dataProviderMock.Verify(a => a.Add(It.IsAny <SmartUrlEntity>()), Times.Once);
        }
Пример #2
0
        public async Task CreateSmartUrlShouldCreateNewRecord()
        {
            var managedConfig    = ConfigureManagedConfig();
            var smartUriResponse = new SmartUrlEntity {
                Id = 1
            };
            var dataProviderMock = new Mock <IDataProvider>();

            dataProviderMock.Setup(a => a.GetSmartUrlByHash(It.IsAny <string>()))
            .Returns(Task.FromResult((SmartUrlEntity)null));
            dataProviderMock.Setup(a => a.GetSmartUrlByKey(It.IsAny <string>()))
            .Returns(Task.FromResult(smartUriResponse));

            var sut = new HashKeyService(managedConfig, dataProviderMock.Object);


            // Act
            var result = await sut.CreateSmartUrl(_url);


            // Assert
            result.Id.ShouldBeEqualTo(smartUriResponse.Id);
            dataProviderMock.Verify(a => a.GetSmartUrlByHash(It.IsAny <string>()), Times.Once);
            dataProviderMock.Verify(a => a.GetSmartUrlByKey(It.IsAny <string>()), Times.Once);
        }
Пример #3
0
 public async Task Add(SmartUrlEntity smartUrl)
 {
     try
     {
         await db.SmartUrl.InsertOneAsync(smartUrl);
     }
     catch
     {
         throw;
     }
 }
Пример #4
0
        public async Task <SmartUrlEntity> CreateSmartUrl(string url)
        {
            SmartUrlEntity objSmartUrlEntity = null;
            string         urlHash           = CalculateMD5Hash(url);
            string         uniqueUrlKey      = string.Empty;

            objSmartUrlEntity = await _dataProvider.GetSmartUrlByHash(urlHash);

            if (objSmartUrlEntity != null)
            {
                objSmartUrlEntity.ShortUrl  = new Uri(string.Format(_managedConfig.ApplicationPath, uniqueUrlKey)).ToString();
                objSmartUrlEntity.IsSuccess = true;
                return(objSmartUrlEntity);
            }

            uniqueUrlKey = GetUniqueKeyFromUrl(url);
            if (!string.IsNullOrEmpty(uniqueUrlKey))
            {
                objSmartUrlEntity = await _dataProvider.GetSmartUrlByKey(uniqueUrlKey);

                if (objSmartUrlEntity != null)
                {
                    objSmartUrlEntity.ShortUrl   = new Uri(string.Format(_managedConfig.ApplicationPath, uniqueUrlKey)).ToString();
                    objSmartUrlEntity.IsSuccess  = true;
                    objSmartUrlEntity.IsShortUrl = true;
                    return(objSmartUrlEntity);
                }
            }

            uniqueUrlKey = string.Empty;
            uniqueUrlKey = GenerateUniqueUrlKey().Result;

            if (!string.IsNullOrEmpty(uniqueUrlKey))
            {
                objSmartUrlEntity          = new SmartUrlEntity();
                objSmartUrlEntity.UrlKey   = uniqueUrlKey;
                objSmartUrlEntity.Url      = url;
                objSmartUrlEntity.UrlHash  = urlHash;
                objSmartUrlEntity.ShortUrl = new Uri(string.Format(_managedConfig.ApplicationPath, uniqueUrlKey)).ToString();

                await _dataProvider.Add(objSmartUrlEntity);

                objSmartUrlEntity.IsSuccess = true;
                return(objSmartUrlEntity);
            }

            objSmartUrlEntity           = new SmartUrlEntity();
            objSmartUrlEntity.IsSuccess = false;
            return(objSmartUrlEntity);
        }
Пример #5
0
 public async Task Add(SmartUrlEntity smartUrl)
 {
     _db.Add(smartUrl);
     await _db.SaveChangesAsync();
 }