public void Test_Method_GetRaitingListAnimeXML()
        {
            AnimeNNEncyclopediaConnector con = new AnimeNNEncyclopediaConnector();
            XElement result = con.GetRatingListAnimeXML(0, 1000);
            int actual =
                (from r in result.Elements("item")
                 select r).Count();


            Assert.AreEqual<int>(1000, actual);
        }
 public void Test_Method_GetTitleListXML()
 {
     //Build Connector
     AnimeNNEncyclopediaConnector con = new AnimeNNEncyclopediaConnector();
     //Retrieve data
     XElement result = con.GetTitleListXML(0, 1000, 0);
     int actual =
         (from r in result.Elements("item")
          select r).Count();
     //Assert behaviour
     Assert.AreEqual<int>(1000, actual);
 }
        public void Test_Method_GetAnimeDetail()
        {
            //Create Connector
            AnimeNNEncyclopediaConnector con = new AnimeNNEncyclopediaConnector();

            //Get list of Anime to retrieve details from
            XElement expected = con.GetTitleListXML(0, 20, APIRequestType.ANIME);
            
            //Build Request
            List<AniMoConsole.src.API.APIRequestDesc> descColl = 
                (from r in expected.Elements("item")
                  select new APIRequestDesc() { RequestType = APIRequestType.ANIME, RequestValue = r.Element("id").Value }).ToList();
            //Request Data
            XElement actual = con.GetItemDetailsXML(descColl);

            //Check data
            var results = from r in expected.Elements("item")
                          join a in actual.Elements("anime") on r.Element("id").Value equals a.Attribute("id").Value
                          where r.Element("name").Value == a.Attribute("name").Value
                          select new { NameExpected = r.Element("name").Value, NameActual = a.Attribute("name").Value };
            //See if method behaved as expected
            Assert.AreEqual(results.Count(), 20);
        }
        public static List<Anime>  Transform(int elements, int skip)
        {
            //Build Connector
            AnimeNNEncyclopediaConnector conn = new AnimeNNEncyclopediaConnector();

            //Gather Raw API Data
            XElement result = conn.GetRatingListAnimeXML(skip, elements);
            List<APIRequestDesc> descList =
                (from r in result.Elements("item")
                 select new APIRequestDesc() { RequestType = APIRequestType.ANIME, RequestValue = r.Attribute("id").Value }).ToList();

            //Map Data
            List<Anime> mappedData = new List<Anime>(elements);
            int totalAmout = descList.Count;
            while (descList.Count > 0)
            {
                Console.WriteLine((totalAmout-descList.Count)+ "/"+totalAmout);
                //Get API Request Descriptors
                int end = descList.Count >= 50 ? 50 : descList.Count;
                List<APIRequestDesc> temp = descList.GetRange(0, end);
                descList.RemoveRange(0, end);

                XElement temp_result = null;
                APIRequestDesc current = null;
                List<Anime> temporaryMapping = null;
                //Download API Raw Data
                try
                {
                    temp_result = conn.GetItemDetailsXML(temp);
                    temporaryMapping = API2ModelTransformer.TransformXML2Model(temp_result);
                    mappedData.AddRange(temporaryMapping);

                } catch (XmlException x)
                {

                    foreach (APIRequestDesc t in temp)
                    {
                        try
                        {
                            current = t;
                            temp_result = conn.GetItemDetailsXML(temp);
                            temporaryMapping = API2ModelTransformer.TransformXML2Model(temp_result);
                            mappedData.AddRange(temporaryMapping);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(current.RequestValue + ": Failed");
                        }
                    }


                }   
               

            }

            BuildRelations(mappedData);

            return mappedData;

        }