Ejemplo n.º 1
0
        private ICollectionBase GetTestCollection(string collectionName, Type collectableType, int numberOfCollectables, int numberOfItemsPerCollectable = 0)
        {
            ICollectableBase collectable    = null;
            ICollectableItem item           = null;
            Type             collectionType = collectableType;
            ICollectionBase  testCollection = new HomeCollection(collectionName, collectionType);

            for (int i = 0; i < numberOfCollectables; i++)
            {
                if (collectionType == CollectableBaseFactory.BookType)
                {
                    collectable = GetTestBookBase(i);
                }
                else if (collectionType == CollectableBaseFactory.StampType)
                {
                    collectable = GetTestStampBase(i);
                }
                testCollection.AddToCollection(collectable);

                for (int j = 0; j < numberOfItemsPerCollectable; j++)
                {
                    if (collectionType == CollectableBaseFactory.BookType)
                    {
                        item = GetTestBookItem(i);
                    }
                    else if (collectionType == CollectableBaseFactory.StampType)
                    {
                        item = GetTestStampItem(i);
                    }
                    collectable.AddItem(item);
                }
            }
            return(testCollection);
        }
Ejemplo n.º 2
0
        internal static ICollectionBase ConvertJsonToCollection(string jsonCollection)
        {
            string collectionTypeName = null;

            try
            {
                dynamic collection = JsonConvert.DeserializeObject(jsonCollection);

                // This is not working as expected when it has child members
                //ICollectionBase collection = JsonConvert.DeserializeObject<HomeCollection>(jsonCollection);

                string collectionName = collection.CollectionName;
                collectionTypeName = collection.CollectionType;
                Type            collectionType = CollectableBaseFactory.GetTypeFromFullName(collectionTypeName);
                ICollectionBase newCollection  = new HomeCollection(collectionName, collectionType);

                var collectables = collection.Collectables;
                foreach (var c in collectables)
                {
                    string jsonCollectable = c.ToString();
                    // add custom try/catch??
                    ICollectableBase collectable = GetCollectableFromJson(jsonCollectable, collectionType);

                    newCollection.AddToCollection(collectable);
                }
                return(newCollection);
            }
            catch (Exception ex)
            {
                throw new CollectionParseException($"Unable to parse Json into a collection object.  Type={collectionTypeName}, Json={jsonCollection}", ex);
            }
        }
Ejemplo n.º 3
0
        public void add_invalid_type_to_book_collection_throws_exception()
        {
            ICollectionBase bookCollection = new HomeCollection("initial", CollectableBaseFactory.CollectableTypes[0]);

            bookCollection.AddToCollection(new StampBase());

            Assert.IsFalse(true, "Expected that an exception is thrown when an invalid type is added to the collection");
        }
Ejemplo n.º 4
0
        public void add_null_collectable_to_collection_throws_exception()
        {
            ICollectionBase testCollection = new HomeCollection("initial", CollectableBaseFactory.CollectableTypes[0]);

            ICollectableBase nullCollectable = null;

            testCollection.AddToCollection(nullCollectable);

            Assert.IsFalse(true, "Expected that an exception is thrown when a null value is added to the collection");
        }
Ejemplo n.º 5
0
        public void add_valid_type_to_collection_succeeds()
        {
            foreach (Type collectableType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectionBase  collection  = new HomeCollection("initial", collectableType);
                ICollectableBase collectable = GetMockCollectableObject(collectableType);

                collection.AddToCollection(collectable);

                Assert.AreEqual(1, collection.Collectables.Count);
            }
        }
Ejemplo n.º 6
0
        private ICollectionBase GetMockCollection(int N, Type collectableType)
        {
            Type            collectionType = collectableType;
            ICollectionBase testCollection = new HomeCollection("initial", collectionType);

            for (int i = 0; i < N; i++)
            {
                ICollectableBase collectable = GetMockCollectableObject(collectableType);
                testCollection.AddToCollection(collectable);
            }

            return(testCollection);
        }
Ejemplo n.º 7
0
        public void add_invalid_type_to_collection_throws_exception()
        {
            bool threwException = false;

            foreach (Type collectableType in CollectableBaseFactory.CollectableTypes)
            {
                threwException = false;
                ICollectionBase  collection  = new HomeCollection("initial", collectableType);
                ICollectableBase collectable = GetDifferentTypeMockCollectable(collectableType).Object; //CollectableBaseFactory.CreateCollectableBase(collectableType);

                try
                {
                    collection.AddToCollection(collectable);
                }
                catch (CollectionException)
                {
                    threwException = true;
                }

                Assert.IsTrue(threwException, "Expected that an exception is thrown when an invalid type is added to the collection");
            }
        }
Ejemplo n.º 8
0
        public void getcollection_returns_all_collectables_added_to_collection()
        {
            int N = 3;

            foreach (Type collectableType in CollectableBaseFactory.CollectableTypes)
            {
                Type collectionType = collectableType;
                IList <ICollectableBase> collectables   = new List <ICollectableBase>();
                ICollectionBase          testCollection = new HomeCollection("initial", collectionType);

                for (int i = 0; i < N; i++)
                {
                    ICollectableBase collectable = GetMockCollectableObject(collectionType);
                    collectables.Add(collectable);

                    testCollection.AddToCollection(collectable);
                }

                for (int i = 0; i < N; i++)
                {
                    Assert.AreEqual(collectables[i], testCollection.Collectables[i]);
                }
            }
        }