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); } }
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); }
public bool IsSame(ICollectableBase itemToCompare, bool useTitleAuthor) { if (itemToCompare == null) { throw new CollectableException("Cannot compare to a null item"); } Type itemType = itemToCompare.CollectableType; if (itemType != this.CollectableType) { throw new CollectableException($"Invalid type {itemType}, expected type {this.CollectableType}"); } BookBase bookDef = (BookBase)itemToCompare; if (!useTitleAuthor) { if (ISBN != bookDef.ISBN) { return(false); } return(true); } else { if (Title != bookDef.Title) { return(false); } if (Author != bookDef.Author) { return(false); } return(true); } }
public void controller_initialized_with_null_collectable_base_object_fails() { ICollectableBase nullBase = null; controller = new CollectableBaseController(nullBase); Assert.IsFalse(true, "Expected the test to fail when initialized with a null object"); }
public CollectableBaseController(ICollectableBase collectableBase) { if (collectableBase == null) { throw new CollectableException("Controller must be initialized with a collectable base object"); } _collectableBase = collectableBase; }
public void create_new_collectable_from_interface_type_throws_exception() { Type invalidType = typeof(IStampBase); ICollectableBase newItem = CollectableBaseFactory.CreateCollectableBase(invalidType); Assert.IsFalse(true, "Expected test to fail if passed an interface instead of valid collectable base type"); }
public void create_new_collectable_from_empty_string_throws_exception() { string invalidTypeName = ""; ICollectableBase newItem = CollectableBaseFactory.CreateCollectableBase(invalidTypeName); Assert.IsFalse(true, "Expected test to fail if passed a null type"); }
public void create_new_stamp_from_factory_returns_stampbase_type() { Type stampType = CollectableBaseFactory.StampType; ICollectableBase newStamp = CollectableBaseFactory.CreateCollectableBase(stampType); Assert.IsTrue(stampType == newStamp.CollectableType); }
public void createandaddcollectableitem_null_collectable_throws_exception() { ICollectableBase nullCollectable = null; CollectableBaseFactory.CreateAndAddCollectableItem(nullCollectable); Assert.Fail("Expected CreateAndAddCollectableItem to fail when passed a null collectable"); }
public void create_new_factory_instance_from_valid_collectable_base_name_case_insensitive_succeeds() { string validTypeName = "BOOkBaSe"; // implements ICollectableBase ICollectableBase newItem = CollectableBaseFactory.CreateCollectableBase(validTypeName); Assert.AreEqual(validTypeName.ToUpper(), newItem.CollectableType.Name.ToUpper(), "Expected to get instance of a book base type"); }
public void create_new_collectable_from_null_type_throws_exception() { Type invalidType = null; ICollectableBase newItem = CollectableBaseFactory.CreateCollectableBase(invalidType); Assert.IsFalse(true, "Expected test to fail if passed a null type"); }
public void create_new_collectable_from_interface_type_string_throws_exception() { string validTypeName = "ICollectableBase"; ICollectableBase newItem = CollectableBaseFactory.CreateCollectableBase(validTypeName); Assert.IsFalse(true, "Expected test to fail if passed an interface type name"); }
public void create_new_factory_instance_from_valid_collectable_base_succeeds() { foreach (Type validType in CollectableBaseFactory.CollectableTypes) { ICollectableBase newCollectable = CollectableBaseFactory.CreateCollectableBase(validType); Assert.AreEqual(validType, newCollectable.CollectableType, $"Expected to get instance of a {validType.Name} base type"); } }
public void createandaddcollectableitem_valid_type_returns_new_item_with_type_set() { foreach (Type collectableType in CollectableBaseFactory.CollectableTypes) { ICollectableBase collectable = CollectableBaseFactory.CreateCollectableBase(collectableType); ICollectableItem newItem = CollectableBaseFactory.CreateAndAddCollectableItem(collectable); Assert.AreEqual(collectableType, newItem.CollectableType); } }
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"); }
public void RemoveFromCollection(ICollectableBase collectableToRemove) { try { _collection.Remove(collectableToRemove); } catch (Exception ex) { throw new CollectionException("Error removing item from collection", ex); } }
public void create_new_factory_instance_from_valid_collectable_base_name_succeeds() { foreach (Type validType in CollectableBaseFactory.CollectableTypes) { string validTypeName = validType.Name; // implements ICollectableBase ICollectableBase newItem = CollectableBaseFactory.CreateCollectableBase(validTypeName); Assert.AreEqual(validTypeName, newItem.CollectableType.Name, $"Expected to get instance of a {validTypeName} base type"); } }
public void AddToCollection(ICollectableBase collectableToAdd) { try { _homeCollection.AddToCollection(collectableToAdd); } catch (Exception ex) { throw new CollectionException("Error adding item to collection", ex); } }
public void createandaddcollectableitem_valid_type_adds_new_item_to_collectable() { foreach (Type collectableType in CollectableBaseFactory.CollectableTypes) { ICollectableBase collectable = CollectableBaseFactory.CreateCollectableBase(collectableType); ICollectableItem newItem = CollectableBaseFactory.CreateAndAddCollectableItem(collectable); ICollectableItem fromCollectable = collectable.ItemInstances[0]; Assert.AreEqual(newItem, fromCollectable); } }
public void getitems_returns_empty_list_when_there_are_no_members() { foreach (Type collectableType in CollectableBaseFactory.CollectableTypes) { ICollectableBase collectable = GetTestBase(collectableType, 0); IList <ICollectableItem> items = collectable.ItemInstances; Assert.AreEqual(0, items.Count); } }
public static ICollectableItem CreateAndAddCollectableItem(ICollectableBase collectable) { if (collectable == null) { throw new CollectableException("Collectable cannot be null when adding an instance of it"); } ICollectableItem newItem = CreateCollectableItem(collectable.CollectableType); collectable.AddItem(newItem); return(newItem); }
public void controller_collectabletype_returns_initial_collectable_base_type() { Type objType = typeof(ICollectableBase); ICollectableBase collectableBase = mockCollectableBase.Object; mockCollectableBase.Setup(b => b.CollectableType).Returns(objType); controller = new CollectableBaseController(collectableBase); Type objTestType = controller.CollectableType; Assert.AreEqual(objType, objTestType); }
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); } }
public void additem_inserts_new_item_into_empty_list() { foreach (Type collectableType in CollectableBaseFactory.CollectableTypes) { ICollectableBase collectable = GetTestBase(collectableType, 0); Mock <ICollectableItem> mockItem = GetMockItem(collectableType); collectable.AddItem(mockItem.Object); IList <ICollectableItem> list = collectable.ItemInstances; Assert.AreEqual(1, list.Count); } }
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); }
public void clearitems_from_empty_list_success() { int N = 0; foreach (Type collectableType in CollectableBaseFactory.CollectableTypes) { ICollectableBase collectable = GetTestBase(collectableType, N); collectable.ClearItems(); IList <ICollectableItem> list = collectable.ItemInstances; Assert.AreEqual(0, list.Count); } }
public void controller_initialized_with_collectable_base_object_returns_controller_instance() { try { ICollectableBase collectableBase = mockCollectableBase.Object; controller = new CollectableBaseController(collectableBase); Assert.IsNotNull(controller); } catch { Assert.IsFalse(true, "Test should not fail when initialized with an object"); } }
public void removeitem_from_existing_list_success() { int N = 3; foreach (Type collectableType in CollectableBaseFactory.CollectableTypes) { ICollectableBase collectable = GetTestBase(collectableType, N); ICollectableItem mockItem = collectable.ItemInstances[N - 1]; collectable.RemoveItem(mockItem); IList <ICollectableItem> list = collectable.ItemInstances; Assert.AreEqual(N - 1, list.Count); } }
public void additem_insert_into_empty_list_fails_for_null() { foreach (Type collectableType in CollectableBaseFactory.CollectableTypes) { ICollectableBase collectable = GetTestBase(collectableType, 0); try { collectable.AddItem(null); Assert.IsFalse(true, "Expected to fail adding a null member"); } catch (CollectableException) { Assert.IsTrue(true); } } }
public void getcollectablefromjson_deserialize_bad_format_throws_exception() { string jsonCollectable = "invalid JSON"; foreach (Type collectableType in CollectableBaseFactory.CollectableTypes) { bool fail = false; try { ICollectableBase newCollectable = HomeCollectionRepository.GetCollectableFromJson(jsonCollectable, collectableType); } catch (CollectableParseException) { fail = true; Assert.IsTrue(fail, "Expected exception to be thrown when JSON is invalid and cannot be parsed to an object"); } } }