public void ForwardBackwardRecipeConversionWithoutInformationLoss(DummyProductRecipe originalRecipe, RecipeClassification classification, RecipeState state, DummyProductType backupProductType, DummyWorkplan workplanInTargetRecipe, int testCaseCounter) { // Arrange // - Basic Workplan properties originalRecipe.Id = 42; originalRecipe.Name = "TestName"; originalRecipe.Revision = 1337; originalRecipe.Classification = classification; originalRecipe.State = state; // - Mock workplan requests if there could be a Workplan var originalWorkplanRecipe = originalRecipe as DummyProductWorkplanRecipe; if (originalWorkplanRecipe is not null) { _workplanManagementMock.Setup(wm => wm.LoadWorkplan(It.IsAny <long>())) .Returns((long id) => new DummyWorkplan() { Id = id }); } // - Create target object var targetDummyRecipe = (DummyProductRecipe)Activator.CreateInstance(originalRecipe.GetType()); targetDummyRecipe.Id = 42; if (originalWorkplanRecipe is not null) { ((DummyProductWorkplanRecipe)targetDummyRecipe).Workplan = workplanInTargetRecipe; } // - No change of classification on clones should be possible, thereby preset it if (originalRecipe.Classification.HasFlag(RecipeClassification.Clone)) { targetDummyRecipe.Classification = originalRecipe.Classification; } // Act var convertedModel = _productConverter.ConvertRecipe(originalRecipe); var recoveredOriginal = _productConverter.ConvertRecipeBack(convertedModel, targetDummyRecipe, backupProductType); // Assert // - Backup products are used for recipes without products if (originalRecipe.Product is null) { originalRecipe.Product = backupProductType; } Assert.AreEqual(originalRecipe, recoveredOriginal); // - If there is a workplan and it changed, reload it at backward conversion if (originalWorkplanRecipe?.Workplan is not null && originalWorkplanRecipe.Workplan.Id != workplanInTargetRecipe.Id) { _workplanManagementMock.Verify(wm => wm.LoadWorkplan(originalWorkplanRecipe.Workplan.Id), Times.Once); }
public void ForwardBackwardProductConversionWithoutInformationLoss(DummyProductType originalProductType, ProductState state, IIdentity identity, IReadOnlyList <IProductRecipe> recipes, bool flat, int counter) { // Arrange // - Basic ProductType properties originalProductType.Id = 42; originalProductType.Name = "TestName"; originalProductType.State = state; originalProductType.Identity = identity; // - Expected behavior from the RecipeManagement if (recipes.Any()) { ReflectionTool.TestMode = true; } _recipeManagementMock.Setup(rm => rm.GetAllByProduct(It.IsAny <IProductType>())).Returns(recipes); _recipeManagementMock.Setup(rm => rm.Get(It.IsAny <long>())).Returns((long id) => new DummyProductRecipe() { Id = id }); // - Create target ProductType object var targetDummyProductType = (DummyProductType)Activator.CreateInstance(originalProductType.GetType()); targetDummyProductType.Id = 42; // - Expected behavior from the RecipeManagement _productManagerMock.Setup(pm => pm.LoadType(It.IsAny <long>())).Returns((long id) => new DummyProductType() { Id = id }); // - Product PartsShould only by included with their id if they already exist var originalProductTypeWithParts = originalProductType as DummyProductTypeWithParts; if (originalProductTypeWithParts is not null && originalProductTypeWithParts.ProductPartLink is not null) { ((DummyProductTypeWithParts)targetDummyProductType).ProductPartLink = ((DummyProductTypeWithParts)originalProductType).ProductPartLink; ((DummyProductTypeWithParts)targetDummyProductType).ProductPartLinkEnumerable = ((DummyProductTypeWithParts)originalProductType).ProductPartLinkEnumerable.Take(1).ToList(); } // Act var convertedModel = _productConverter.ConvertProduct(originalProductType, flat); var recoveredOriginal = _productConverter.ConvertProductBack(convertedModel, targetDummyProductType); // Assert // - Non ProductIdentities will always be transformed into empty ProductIdentities in the Process if (identity is null) { originalProductType.Identity = new ProductIdentity("", 0); } // - Products originally converted with the flat flag will only affect certain properties if (flat) { Assert.AreEqual(originalProductType.Id, recoveredOriginal.Id); Assert.AreEqual(originalProductType.Name, recoveredOriginal.Name); Assert.AreEqual(originalProductType.State, recoveredOriginal.State); Assert.AreEqual(originalProductType.Identity, recoveredOriginal.Identity); _recipeManagementMock.VerifyNoOtherCalls(); _productManagerMock.VerifyNoOtherCalls(); return; } // - The following assert uses overwritten Equals methods to check for VALUE equality Assert.AreEqual(originalProductType, recoveredOriginal); // - If there are Recipes the RecipeManagement should be called if (recipes.Any()) { _recipeManagementMock.Verify(rm => rm.GetAllByProduct(originalProductType)); _recipeManagementMock.Verify(rm => rm.Save(originalProductType.Id, It.Is <List <IProductRecipe> >(list => list.Count == recipes.Count))); if (recipes.First().Id != 0) { _recipeManagementMock.Verify(rm => rm.Get(recipes.First().Id)); } } // - If there are ProductPartLinks the ProductManagement should be called var targetDummyTypeWithParts = recoveredOriginal as DummyProductTypeWithParts; if (targetDummyTypeWithParts?.ProductPartLink?.Product is not null) { _productManagerMock.Verify(pm => pm.LoadType(targetDummyTypeWithParts.ProductPartLink.Product.Id)); _productManagerMock.Verify(pm => pm.LoadType(targetDummyTypeWithParts.ProductPartLinkEnumerable.First().Product.Id)); } }