public void ensureFromModelViewCreatesInstance() { double minWidth = 25; double maxWidth = 50; double recommendedWidth = 35; string unit = "cm"; AddProductSlotWidthsModelView productSlotWidthsModelView = new AddProductSlotWidthsModelView(); productSlotWidthsModelView.minWidth = minWidth; productSlotWidthsModelView.maxWidth = maxWidth; productSlotWidthsModelView.recommendedWidth = recommendedWidth; productSlotWidthsModelView.unit = unit; ProductSlotWidths slotWidths = ProductSlotWidthsModelViewService.fromModelView(productSlotWidthsModelView); double expectedMinWidth = MeasurementUnitService.convertFromUnit(minWidth, unit); double expectedMaxWidth = MeasurementUnitService.convertFromUnit(maxWidth, unit); double expectedRecommendedWidth = MeasurementUnitService.convertFromUnit(recommendedWidth, unit); Assert.Equal(expectedMinWidth, slotWidths.minWidth); Assert.Equal(expectedMaxWidth, slotWidths.maxWidth); Assert.Equal(expectedRecommendedWidth, slotWidths.recommendedWidth); }
//TODO: use ProductBuilder here /// <summary> /// Creates a new instance of Product and saves it to the Repository. /// </summary> /// <param name="addProductMV">AddProductModelView containing the new Product's information.</param> /// <returns>Created instance of Product.</returns> /// <exception cref="System.ArgumentException">Throw </exception> public static Product create(AddProductModelView addProductMV) { string reference = addProductMV.reference; string designation = addProductMV.designation; string modelFilename = addProductMV.modelFilename; long productCategoryId = addProductMV.categoryId; List <AddMeasurementModelView> measurementModelViews = addProductMV.measurements; //NOTE: these checks are made here in order to avoid making requests to repositories unnecessarily if (measurementModelViews == null || !measurementModelViews.Any()) { throw new ArgumentException(ERROR_NO_MEASUREMENTS_DEFINED); } List <AddProductMaterialModelView> materialViews = addProductMV.materials; //NOTE: these checks are made here in order to avoid making requests to repositories unnecessarily if (materialViews == null || !materialViews.Any()) { throw new ArgumentException(ERROR_NO_MATERIALS_DEFINED); } ProductCategoryRepository categoryRepository = PersistenceContext.repositories().createProductCategoryRepository(); ProductCategory category = categoryRepository.find(productCategoryId); if (category == null) { throw new ArgumentException(ERROR_CATEGORY_NOT_FOUND); } if (!categoryRepository.isLeaf(category)) { throw new ArgumentException(ERROR_CATEGORY_NOT_LEAF); } List <Material> materials = new List <Material>(); MaterialRepository materialRepository = PersistenceContext.repositories().createMaterialRepository(); foreach (AddProductMaterialModelView materialModelView in materialViews) { if (materialModelView == null) { throw new ArgumentException(ERROR_NULL_MATERIAL); } long materialId = materialModelView.materialId; Material material = materialRepository.find(materialId); if (material == null) { throw new ArgumentException(string.Format(ERROR_MATERIAL_NOT_FOUND, materialId)); } materials.Add(material); } IEnumerable <Measurement> measurements = MeasurementModelViewService.fromModelViews(addProductMV.measurements); List <AddComponentModelView> componentModelViews = addProductMV.components; AddProductSlotWidthsModelView slotWidthsModelView = addProductMV.slotWidths; bool hasComponents = componentModelViews != null && componentModelViews.Any(); bool hasSlots = slotWidthsModelView != null; Product product = null; if (hasSlots) { ProductSlotWidths slotWidths = ProductSlotWidthsModelViewService.fromModelView(slotWidthsModelView); if (hasComponents) { product = new Product(reference, designation, modelFilename, category, materials, measurements, slotWidths); return(addComplementaryProducts(product, componentModelViews)); } else { return(new Product(reference, designation, modelFilename, category, materials, measurements, slotWidths)); } } else { if (hasComponents) { product = new Product(reference, designation, modelFilename, category, materials, measurements); return(addComplementaryProducts(product, componentModelViews)); } else { return(new Product(reference, designation, modelFilename, category, materials, measurements)); } } }