Ejemplo n.º 1
0
        public async Task <NameCheckBatchModel> NameCheckBatchAsync(string value, string separator, EndpointType endpointType = EndpointType.NotSet, string userIp = null)
        {
            Guard.ArgumentNotNullOrWhiteSpace(value, "value");
            Stopwatch timer = new Stopwatch();

            timer.Start();
            IList <string> parsedBatch = NameCheckHelper.ParseBatch(value, String.IsNullOrWhiteSpace(separator) ? Constants.DefaultBatchSeparator : separator);

            var result = new NameCheckBatchModel();

            if (parsedBatch == null)
            {
                return(result);
            }
            result.Value        = value;
            result.NameChecks   = new List <NameCheckModel>();
            result.Id           = DescendingSortedGuid.NewSortedGuid();
            result.DateUtc      = DateTime.UtcNow;
            result.EndpointType = endpointType;
            result.UserIp       = userIp;


            foreach (var item in parsedBatch)
            {
                result.NameChecks.AddIfNotNull(await NameCheckAsync(item, endpointType));
            }
            timer.Stop();
            result.BatchDurationMs = timer.ElapsedMilliseconds;
            return(result);
        }
Ejemplo n.º 2
0
        public void DescendingSortedGuid_TryParse_With_Valid_DescendingSortedGuid_Returns_True()
        {
            DescendingSortedGuid guid;

            Assert.IsTrue(DescendingSortedGuid.TryParse("0635318522499400050_B77AD6F9624A4C2896E8545923E56502", out guid));
            Assert.AreEqual("0635318522499400050_b77ad6f9624a4c2896e8545923e56502", guid.ToString());
        }
Ejemplo n.º 3
0
        public void DescendingSortedGuid_CreateNewSortedGuid_Returns_New_DescendingSortedGuid()
        {
            var desc = DescendingSortedGuid.NewSortedGuid();

            Assert.IsNotNull(desc);
            Assert.IsFalse(string.IsNullOrWhiteSpace(desc.ToString()));
        }
Ejemplo n.º 4
0
        public void DescendingSortedGuid_Parse_With_ValidFormat_Returns_ParsedItem()
        {
            DescendingSortedGuid expected = DescendingSortedGuid.NewSortedGuid();
            DescendingSortedGuid actual   = DescendingSortedGuid.Parse(expected.ToString());

            AssertCompare(expected, actual);
        }
Ejemplo n.º 5
0
        private static Fake <DescendingSortedGuid> NewItemDesc()
        {
            Fake <DescendingSortedGuid> item = new Fake <DescendingSortedGuid>();

            item.Id   = DescendingSortedGuid.NewSortedGuid();
            item.Name = Randomizer.GetRandomString();
            return(item);
        }
Ejemplo n.º 6
0
        public void DescendingSortedGuid_Operator_Equals_Returns_True()
        {
            var guid      = Guid.NewGuid();
            var timestamp = DateTimeOffset.UtcNow;

            var value1 = new DescendingSortedGuid(timestamp, guid);
            var value2 = new DescendingSortedGuid(timestamp, guid);

            Assert.IsTrue(value1 == value2);
        }
Ejemplo n.º 7
0
        public void DescendingSortedGuid_OperatorOverload_DifferentThan_Returns_True()
        {
            var guid             = Guid.NewGuid();
            var timestamp        = DateTimeOffset.UtcNow;
            var smallerTimestamp = timestamp.AddMilliseconds(-1);

            Assert.IsTrue(new DescendingSortedGuid(timestamp, guid) != new DescendingSortedGuid(smallerTimestamp, guid));
            Assert.IsTrue(new DescendingSortedGuid(smallerTimestamp, guid) != new DescendingSortedGuid(timestamp, guid));
            Assert.IsTrue(DescendingSortedGuid.NewSortedGuid() != DescendingSortedGuid.NewSortedGuid());
        }
Ejemplo n.º 8
0
        public void DescendingSortedGuid_GetHasCode_WithFastIteration_Creates_UniqueHasCode()
        {
            HashSet <int> hashSet = new HashSet <int>();

            for (int i = 0; i < 1000; i++)
            {
                int hashCode = DescendingSortedGuid.NewSortedGuid().GetHashCode();
                Assert.IsTrue(hashSet.Add(hashCode), $"Duplicate hashcode: {hashCode}");
            }
        }
Ejemplo n.º 9
0
        public void DescendingSortedGuid_Operator_Equals_Returns_False()
        {
            var guid       = Guid.NewGuid();
            var guid2      = Guid.NewGuid();
            var timestamp  = DateTimeOffset.UtcNow;
            var timestamp2 = DateTimeOffset.MaxValue;

            var value1 = new DescendingSortedGuid(timestamp, guid);
            var value2 = new DescendingSortedGuid(timestamp, guid);

            Assert.IsFalse(new DescendingSortedGuid(timestamp, guid) == new DescendingSortedGuid(timestamp, guid2));
            Assert.IsFalse(new DescendingSortedGuid(timestamp2, guid) == new DescendingSortedGuid(timestamp, guid));
        }
Ejemplo n.º 10
0
        public NameCheckBatchModel ToModel(NameCheckBatchEntity entity)
        {
            if (entity == null)
            {
                return(null);
            }

            List <NameCheckModel> nameChecks = JsonConvert.DeserializeObject <List <NameCheckModel> >(entity.NameChecksJson);

            return(new NameCheckBatchModel
            {
                Id = DescendingSortedGuid.Parse(entity.RowKey),
                Value = entity.Value,
                UserIp = entity.UserIp,
                DateUtc = entity.DateUtc,
                EndpointType = (EndpointType)Enum.Parse(typeof(EndpointType), entity.EndpointType),
                NameChecks = nameChecks,
                BatchDurationMs = entity.BatchDurationMs
            });
        }
Ejemplo n.º 11
0
        public async Task <NameCheckModel> NameCheckAsync(string name, EndpointType endpointType = EndpointType.NotSet, string userIp = null)
        {
            Guard.ArgumentNotNullOrWhiteSpace(name, "name");
            Stopwatch timer = new Stopwatch();

            timer.Start();

            var key    = NameCheckHelper.FormatKey(name);
            var result = Cache.GetItem(key);

            if (result != null)
            {
                return(result);
            }

            result              = new NameCheckModel();
            result.Id           = DescendingSortedGuid.NewSortedGuid();
            result.Key          = key;
            result.DateUtc      = DateTime.UtcNow;
            result.EndpointType = endpointType;
            result.Name         = NameCheckHelper.FormatName(name);
            result.Query        = NameCheckHelper.FormatQuery(name);
            result.UserIp       = userIp;

            var twitterResult = await TwitterApiManager.IsNameAvailable(result.Query);

            var gandiResult = GandiApiManager.CheckDomains(result.Query, new string[] { "com", "net", "org" });

            result.SocialNetworks = new Dictionary <string, bool>();
            result.SocialNetworks.Add("twitter", twitterResult.Content);
            result.Domains = gandiResult;

            timer.Stop();
            result.QueryDurationMs = timer.ElapsedMilliseconds;
            Cache.AddItem(key, result);
            return(result);
        }
Ejemplo n.º 12
0
        public NameCheckModel ToModel(NameCheckEntity entity)
        {
            if (entity == null)
            {
                return(null);
            }

            Dictionary <string, bool> socialNetworks = JsonConvert.DeserializeObject <Dictionary <string, bool> >(entity.SocialNetworksJson);
            Dictionary <string, bool> domains        = JsonConvert.DeserializeObject <Dictionary <string, bool> >(entity.DomainsJson);

            return(new NameCheckModel
            {
                Id = DescendingSortedGuid.Parse(entity.RowKey),
                UserIp = entity.UserIp,
                Key = entity.Key,
                Name = entity.Name,
                Query = entity.Query,
                QueryDurationMs = entity.QueryDurationMs,
                DateUtc = entity.DateUtc,
                EndpointType = (EndpointType)Enum.Parse(typeof(EndpointType), entity.EndpointType),
                SocialNetworks = socialNetworks,
                Domains = domains
            });
        }
Ejemplo n.º 13
0
 public void DescendingSortedGuid_Equals_With_NotEqualItems_Returns_False()
 {
     Assert.IsFalse(new DescendingSortedGuid().Equals(null));
     Assert.IsFalse(new DescendingSortedGuid().Equals(new { Toto = "toto" }));
     Assert.IsFalse(DescendingSortedGuid.NewSortedGuid().Equals(DescendingSortedGuid.NewSortedGuid()));
 }
Ejemplo n.º 14
0
 public void DescendingSortedGuid_Parse_With_InvalidFormat_Throws_ArgumentException(string value)
 {
     Assert.Throws <ArgumentException>(() => DescendingSortedGuid.Parse(value));
 }
Ejemplo n.º 15
0
 public void DescendingSortedGuid_Parse_With_NullArgument_Throws_ArgumentNullException()
 {
     Assert.Throws <ArgumentNullException>(() => DescendingSortedGuid.Parse(null));
 }
Ejemplo n.º 16
0
 private static void AssertCompare(DescendingSortedGuid expected, DescendingSortedGuid actual)
 {
     Assert.AreEqual(expected.Guid, actual.Guid);
     Assert.AreEqual(expected.Timestamp, actual.Timestamp);
 }
Ejemplo n.º 17
0
        public void DescendingSortedGuid_TryParse_With_NotValid_DescendingSortedGuid_Returns_False(string value)
        {
            DescendingSortedGuid guid;

            Assert.IsFalse(DescendingSortedGuid.TryParse(value, out guid));
        }