/// <summary> /// Returns a ProductSeries object from the database /// </summary> /// <param name="seriesId">Id of the product series to be retrieved</param> /// <returns>Returns a ProductSeries object populated with data</returns> internal static ProductSeries GetProductSeriesById(int seriesId) { ProductSeries series = null; //create and open connection using (OleDbConnection conn = DAL.GetConnection()) { //create command OleDbCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from ProductSeries where SeriesID = @seriesId"; cmd.Parameters.AddWithValue("@seriesId", seriesId); try { //execute a datareader, closing the connection when all the data is read from it using (OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { List <ProductSeries> productSeries = LoadProductSeriesListFromDataReader(dr); if (productSeries.Count >= 1) { series = productSeries[0]; } } } catch (Exception exception) { throw new Exception("Error while executing the following Sql statement:\n" + cmd.ToStringExtended(), exception); } } return(series); }
public void GetProductSeriesById_with_invalid_id_returns_null() { const int seriesId = -1; string errorMsg; ProductSeries actualSeries = WebMethods.ProductSeriesMethods.GetProductSeriesById(seriesId, out errorMsg); Assert.IsNull(actualSeries); Assert.IsTrue(errorMsg.StartsWith("error"), string.Format("Expected result should start with \"error: \". Unexpected return value: {0}", errorMsg)); }
/// <summary> /// Update a product series in the webshop /// </summary> /// <param name="series">The product series to be updated</param> /// <returns>Returns a string with "ok" or an error message</returns> internal static string UpdateProductSeries(ProductSeries series) { string data = series.ToXml(); string result = SendDataThroughWebMethod("productseries", "update", null, data); XElement xml = XElement.Parse(result); return(xml.ToString()); }
public void GetProductSeriesById_with_valid_id_returns_product_series() { const int seriesId = 1; // The Joy of... var expectedSeries = new ProductSeries { Id = seriesId, Name = "The Joy of..." }; string errorMsg; ProductSeries actualSeries = WebMethods.ProductSeriesMethods.GetProductSeriesById(seriesId, out errorMsg); Assert.AreEqual(expectedSeries.Id, actualSeries.Id); Assert.AreEqual(expectedSeries.Name, actualSeries.Name); }
public void Get_method_returns_valid_xml() { int seriesId = new PredefinedDataObjects().ProductSeries.Id; string xmlData = WebMethods.GetDataFromWebMethod("productseries", "getById", "id=" + seriesId); string errorMsg; Assert.IsFalse(WebMethods.WebMethodReturnedError(xmlData, out errorMsg), string.Format("Product Series with Id {0} could not be retrieved from the webshop. Unexpected return value: {1}", seriesId, errorMsg)); string validationError = ProductSeries.ValidateXmlStructure(xmlData); Assert.IsNull(validationError); }
/// <summary> /// Get a product series from the webshop /// </summary> /// <param name="seriesId">Id of the product series to be retrieved</param> /// <param name="errorMsg" type="output">Error message returned by the web method</param> /// <returns>Returns a ProductSeries object populated with data</returns> internal static ProductSeries GetProductSeriesById(int seriesId, out string errorMsg) { string xmlData = GetDataFromWebMethod("productseries", "getById", "id=" + seriesId); if (WebMethodReturnedError(xmlData, out errorMsg)) { return(null); } else { errorMsg = null; ProductSeries series = ProductSeriesRepository.LoadProductSeriesFromXml(xmlData); return(series); } }
private static List <ProductSeries> LoadProductSeriesListFromDataReader(OleDbDataReader reader) { var productSeries = new List <ProductSeries>(); while (reader.Read()) { var series = new ProductSeries(); series.Id = reader.GetInt32(reader.GetOrdinal("SeriesID")); series.Name = reader.GetStringSafe(reader.GetOrdinal("Naam")); series.CreatedDttm = reader.GetNullableDateTime(reader.GetOrdinal("CreateDttm")); series.UpdatedDttm = reader.GetNullableDateTime(reader.GetOrdinal("UpdateDttm")); series.DeletedDttm = reader.GetNullableDateTime(reader.GetOrdinal("DeleteDttm")); productSeries.Add(series); } return(productSeries); }
/// <summary> /// Converts a product series represented in an Xml string to a ProductSeries object /// </summary> /// <param name="xmlString">Xml definition of the product series</param> /// <returns>Returns a ProductSeries object populated with data</returns> internal static ProductSeries LoadProductSeriesFromXml(string xmlString) { var series = new ProductSeries(); XElement xml = XElement.Parse(xmlString); if (!Utility.XmlElementIsEmptyOrSpecialValue(xml.Element("id"))) { series.Id = Convert.ToInt32(xml.Element("id").Value); } if (!Utility.XmlElementIsEmptyOrSpecialValue(xml.Element("name"))) { series.Name = xml.Element("name").Value; } if (!Utility.XmlElementIsEmptyOrSpecialValue(xml.Element("test"))) { series.Test = xml.Element("test").Value.Equals("1"); } if (!Utility.XmlElementIsEmptyOrSpecialValue(xml.Element("created"))) { series.CreatedDttm = Convert.ToDateTime(xml.Element("created").Value); } if (!Utility.XmlElementIsEmptyOrSpecialValue(xml.Element("updated"))) { series.UpdatedDttm = Convert.ToDateTime(xml.Element("updated").Value); } if (!Utility.XmlElementIsEmptyOrSpecialValue(xml.Element("deleted"))) { series.DeletedDttm = Convert.ToDateTime(xml.Element("deleted").Value); } return(series); }
public void UpdateProductSeries_with_values_saves_all_data_correctly() { ProductSeries series = new AnonymousProductSeriesBuilder().build(); //save the product series to the webshop string result = WebMethods.ProductSeriesMethods.UpdateProductSeries(series); result = XElement.Parse(result).Value; Assert.IsTrue(result == "ok", string.Format("Product Series with id {0} could not be created/updated. Unexpected return value was: {1}", series.Id, result)); //retrieve the series from the webshop string errorMsg; ProductSeries seriesFromWS = WebMethods.ProductSeriesMethods.GetProductSeriesById(series.Id, out errorMsg); //compare all values Assert.AreEqual(series.Id, seriesFromWS.Id, "The field comparison for field \"id\" failed."); Assert.AreEqual(series.Name, seriesFromWS.Name, "The field comparison for field \"name\" failed."); Assert.AreEqual(series.Test, seriesFromWS.Test, "The field comparison for field \"test\" failed."); Assert.AreEqual(series.CreatedDttm, seriesFromWS.CreatedDttm, "The field comparison for field \"created\" failed."); Assert.AreEqual(series.UpdatedDttm, seriesFromWS.UpdatedDttm, "The field comparison for field \"updated\" failed."); Assert.AreEqual(series.DeletedDttm, seriesFromWS.DeletedDttm, "The field comparison for field \"deleted\" failed."); }