/**********************************************************************************************//** * \fn public ADHCServiceJsonResult RunQuery() * * \brief Executes the query operation * * \author Delmiro Paes * * \returns An ADHCServiceJsonResult. **************************************************************************************************/ public ADHCServiceJsonResult RunQuery() { ADHCServiceJsonResult adhcServiceJsonResult = new ADHCServiceJsonResult(); string urlGitHubBridgeWebAPI = string.Empty; bool isRunningOnAzure = false; //TODO: Replace GitHubBridgeWebAPI by ADHCServiceJsonResult. isRunningOnAzure = ConfigurationManager.AppSettings["IsRunningOnAzure"] == "true"; urlGitHubBridgeWebAPI = ConfigurationManager.AppSettings["GitHubBridgeWebAPI"]; if (string.IsNullOrEmpty(urlGitHubBridgeWebAPI)) { return(adhcServiceJsonResult); } else if (!isRunningOnAzure) { if (CheckByPing(urlGitHubBridgeWebAPI, 1000) == false) { return(null); } } //TODO: Create a test for servive offline. using (HttpClient httpClient = new HttpClient()) { string queryResultString = string.Empty; httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); httpClient.DefaultRequestHeaders.Add("user-agent", "Nothing"); try { // Just another way to query a URL. if (string.IsNullOrEmpty(queryResultString = Task .Run(() => httpClient.GetStringAsync(urlGitHubBridgeWebAPI)) .Result)) { // Just for fun, It's not a crime! Is it? goto RunQueryEnd; } adhcServiceJsonResult = JsonConvert.DeserializeObject <ADHCServiceJsonResult>(queryResultString); } catch (Exception) { adhcServiceJsonResult = null; } } RunQueryEnd :; return(adhcServiceJsonResult); }
public void GitHubQuery_QueryResult_Test() { //Arrange GitHubQuery gitHubQuery = new GitHubQuery(); //Act ADHCServiceJsonResult adhcServiceJsonResult = gitHubQuery.RunQuery(); //Assert Assert.IsTrue(adhcServiceJsonResult != null); }
public void ADHCServiceJsonResultTests_JustToCoverability_Test() { // arrange ADHCServiceJsonResult adhcServiceJsonResult = new ADHCServiceJsonResult(); bool incomplete_results = false; int total_count = 0; List <LanguageItem> languageItems = new List <LanguageItem>(); // Act adhcServiceJsonResult.incomplete_results = incomplete_results; adhcServiceJsonResult.total_count = total_count; adhcServiceJsonResult.LanguageItems = languageItems; // Assert Assert.IsTrue(adhcServiceJsonResult.incomplete_results == incomplete_results || adhcServiceJsonResult.total_count == total_count || adhcServiceJsonResult.LanguageItems == languageItems); }
/**********************************************************************************************//** * \fn public ActionResult QueryResult() * * \brief Queries the result * * \author Delmiro Paes * * \returns A response stream to send to the QueryResult View. **************************************************************************************************/ public ActionResult QueryResult() { ViewBag.Message = "Resultado da consulta ao GitHub."; GitHubQuery gitHubQuery = new GitHubQuery(); //TODO: Check query result. ADHCServiceJsonResult adhcServiceJsonResult = gitHubQuery.RunQuery(); // Fail. if (null == adhcServiceJsonResult) { return(View("Error")); } ViewBag.ADHCServiceJsonResult = adhcServiceJsonResult; return(View()); }
/**********************************************************************************************//** * \fn public string GetJsonFromSql() * * \brief Gets JSON from SQL * * \author Delmiro Paes * * \returns The JSON from SQL. **************************************************************************************************/ public string GetJsonFromSql() { string jsonResult = string.Empty; List <Item> items = new List <Item>(); if (CheckSqlServerAccess() == false) { //TODO: SQL Error, what to do? return(""); } using (GitHubDataContext dbContext = new GitHubDataContext()) { try { //TODO: Just speed this up with Sql store procedure. items = dbContext.Items .Join ( dbContext.Items.GroupBy(g => new { g.Language }) .Select(c => new { Language = c.Key.Language, SUM = c.Sum(s => s.Stargazers_count) }) //TODO: Remove this, not necessary. .OrderByDescending(obd => obd.SUM), itemIn => itemIn.Language, itemOuter => itemOuter.Language, (itemOuter, itemIn) => new { itemOuter, itemIn.SUM } ) .OrderByDescending(obd => obd.SUM) .ThenByDescending(obd => obd.itemOuter.Stargazers_count) .Select(s => s.itemOuter) .Include("Owner") .Include("License") .ToList(); //TODO: Check items count = 0. // All local. ADHCServiceJsonResult adhcServiceJsonResult = new ADHCServiceJsonResult(); int totalCount = 0; adhcServiceJsonResult.total_count = 0; adhcServiceJsonResult.incomplete_results = false; adhcServiceJsonResult.LanguageItems = new List <LanguageItem>(); List <LanguageItem> languageItems = new List <LanguageItem>(); LanguageItem languageItem = new LanguageItem(); for (int iCount = 0; iCount < items.Count; ++iCount) { languageItem.Name = items[iCount].Language; languageItem.TotalStars += items[iCount].Stargazers_count; languageItem.TotalProjects++; totalCount++; if (iCount + 1 == items.Count) { languageItem.Items.Add(items[iCount]); adhcServiceJsonResult.LanguageItems.Add(languageItem); break; } else if (languageItem.Name != items[iCount + 1].Language) { languageItem.Items.Add(items[iCount]); adhcServiceJsonResult.LanguageItems.Add(languageItem); languageItem = new LanguageItem(); } else { languageItem.Items.Add(items[iCount]); } } adhcServiceJsonResult.incomplete_results = false; adhcServiceJsonResult.total_count = totalCount; jsonResult = JsonConvert.SerializeObject(adhcServiceJsonResult); } catch (Exception) { return(string.Empty); } } return(jsonResult); }