Exemplo n.º 1
0
        public void ValidData_SuccessfullyParsesAndMaps()
        {
            UrlData inputData = new UrlData();

            inputData.Url  = "https://www.google.com/";
            inputData.Path = "PathValue1";
            inputData.Size = 10;
            UrlOutputData expectedOutputData = new UrlOutputData();

            expectedOutputData.Path = new Dictionary <string, UrlInfo>();
            expectedOutputData.Path.Add(inputData.Path, new UrlInfo()
            {
                Url  = inputData.Url,
                Size = inputData.Size
            });

            HttpResponseMessage responseMessage = new HttpResponseMessage();

            responseMessage.Content = new StringContent("response");
            responseMessage.Content.Headers.ContentLength = inputData.Size;

            _mockHttpClientWrapper.Setup(h => h.GetAsync(It.IsAny <string>())).ReturnsAsync(responseMessage);
            UrlDataParser parser = new UrlDataParser(_mockHttpClientWrapper.Object);
            UrlOutputData result = parser.Parse(inputData);

            Assert.AreEqual(expectedOutputData.Path.Keys.Count, result.Path.Keys.Count);
            Assert.IsTrue(result.Path.ContainsKey(inputData.Path));

            expectedOutputData.Path.TryGetValue(inputData.Path, out UrlInfo expectedUrlData);
            result.Path.TryGetValue(inputData.Path, out UrlInfo resultUrlData);

            Assert.AreEqual(expectedUrlData.Url, resultUrlData.Url);
            Assert.AreEqual(expectedUrlData.Size, resultUrlData.Size);
        }
Exemplo n.º 2
0
        private async static void WriteToFile()
        {
            using (StreamWriter streamWriter = new StreamWriter(outputFilePath))
            {
                streamWriter.WriteLine("{");

                while (dataLeftInFile || dataToParse.Count > 0)
                {
                    if (dataToParse.Count > 0)
                    {
                        UrlData       urlInfo    = (UrlData)(dataToParse.Dequeue());
                        UrlOutputData outputData = null;
                        try
                        {
                            outputData = parser.Parse(urlInfo);
                        }
                        catch (UriFormatException ex)
                        {
                            Console.WriteLine("Error! " + ex.Message);
                            Environment.Exit(1);
                        }
                        catch (InvalidDataException ex)
                        {
                            Console.WriteLine("Error! " + ex.Message);
                            Environment.Exit(1);
                        }
                        catch (HttpRequestException)
                        {
                            Console.WriteLine("Error! HTTP request was not successful");
                        }

                        //Write the objects as we go so we don't build a single massive object in memory
                        string jsonOutputData = JsonConvert.SerializeObject(outputData.Path);

                        //remove the first and last brackets from the json object { myobject }
                        streamWriter.Write(jsonOutputData.Substring(1, jsonOutputData.Length - 2));

                        //only write the comma if there is data left to parse, either remaining in the file or on the queue
                        //i.e. don't write the comma if this is the last element
                        if (dataLeftInFile || dataToParse.Count > 0)
                        {
                            streamWriter.WriteLine(",");
                        }
                    }
                }

                streamWriter.WriteLine("}");
            }

            Console.WriteLine($"Success! All data has been written to {outputFilePath}");
        }