public void TestProperties()
        {
            Genre genre = new Genre() { Id = TestId, Name = TestName };

            Assert.AreEqual(TestId, genre.Id, "Expected the property to persist");
            Assert.AreEqual(TestName, genre.Name, "Expected the property to persist");
        }
        public void TestJsonParsing()
        {
            Genre genre = new Genre() { Id = "Metal", Name = "Metal" };
            JObject json = JObject.Parse("{\"id\":\"Metal\",\"name\":\"Metal\"}");
            Genre genreFromJson = Genre.FromJToken(json) as Genre;

            Assert.IsNotNull(genreFromJson, "Expected a genre object");

            Assert.IsTrue(genre.Equals(genreFromJson), "Expected the same genre");
        }
 public void HashCodeCanBeRetrievedWhenIdIsNull()
 {
     Genre genre = new Genre();
     Assert.IsNotNull(genre.GetHashCode(), "Expected a hash code");
 }
 public void TestOverrides()
 {
     Genre genre = new Genre() { Id = TestId, Name = TestName };
     Assert.IsNotNull(genre.GetHashCode(), "Expected a hash code");
     Assert.IsFalse(genre.Equals(TestId), "Expected inequality");
 }
Example #5
0
        /// <summary>
        /// Creates a Product from a JSON Object
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns>A Product object</returns>
        internal static Product FromJToken(JToken item)
        {
            if (item == null)
            {
                return(null);
            }

            // Extract category...
            Category category     = Category.Unknown;
            JToken   jsonCategory = item["category"];

            if (jsonCategory != null)
            {
                category = ParseHelper.ParseEnumOrDefault <Category>(jsonCategory.Value <string>("id"));
            }

            // Extract genres...
            Genre[] genres     = null;
            JArray  jsonGenres = item.Value <JArray>("genres");

            if (jsonGenres != null)
            {
                List <Genre> list = new List <Genre>();
                foreach (JToken jsonGenre in jsonGenres)
                {
                    list.Add((Genre)Genre.FromJToken(jsonGenre));
                }

                genres = list.ToArray();
            }

            // Extract takenfrom...
            Product takenFrom     = null;
            JToken  jsonTakenFrom = item["takenfrom"];

            if (jsonTakenFrom != null)
            {
                takenFrom = (Product)FromJToken(jsonTakenFrom);
            }

            // Extract price...
            Price  price      = null;
            JToken jsonPrices = item["prices"];

            if (jsonPrices != null)
            {
                JToken jsonPermDownload = jsonPrices["permanentdownload"];
                if (jsonPermDownload != null)
                {
                    price = Price.FromJToken(jsonPermDownload);
                }
            }

            // Extract Artists...
            Artist[] performers = null;

            if (item["creators"] != null)
            {
                JArray jsonArtists = item["creators"].Value <JArray>("performers");
                if (jsonArtists != null)
                {
                    List <Artist> list = new List <Artist>();
                    foreach (JToken jsonArtist in jsonArtists)
                    {
                        list.Add((Artist)Artist.FromJToken(jsonArtist));
                    }

                    performers = list.ToArray();
                }
            }

            // Extract trackcount...
            int?   trackCount     = null;
            JToken jsonTrackCount = item["trackcount"];

            if (jsonTrackCount != null)
            {
                trackCount = item.Value <int>("trackcount");
            }

            // Extract thumbnails...
            Uri square50  = null;
            Uri square100 = null;
            Uri square200 = null;
            Uri square320 = null;

            MusicItem.ExtractThumbs(item["thumbnails"], out square50, out square100, out square200, out square320);

            // Create the resulting Product object...
            var product = new Product()
            {
                Id                = item.Value <string>("id"),
                Name              = item.Value <string>("name"),
                Thumb50Uri        = square50,
                Thumb100Uri       = square100,
                Thumb200Uri       = square200,
                Thumb320Uri       = square320,
                Category          = category,
                Genres            = genres,
                TakenFrom         = takenFrom,
                Price             = price,
                TrackCount        = trackCount,
                Tracks            = ExtractTracks(item["tracks"]),
                Performers        = performers,
                Duration          = item.Value <int>("duration"),
                VariousArtists    = item.Value <bool>("variousartists"),
                StreetReleaseDate = item.Value <DateTime>("streetreleasedate"),
                SellerStatement   = item.Value <string>("sellerstatement"),
                Label             = item.Value <string>("label")
            };

            var sequence = item.Value <int>("sequence");

            if (sequence >= 1)
            {
                product.Sequence = sequence;
            }

            return(product);
        }