private ContactShortcut GetFilledRandomContactShortcut(string OmitPropName) { ContactShortcut contactShortcut = new ContactShortcut(); if (OmitPropName != "ContactID") { contactShortcut.ContactID = 1; } if (OmitPropName != "ShortCutText") { contactShortcut.ShortCutText = GetRandomString("", 5); } if (OmitPropName != "ShortCutAddress") { contactShortcut.ShortCutAddress = GetRandomString("", 5); } if (OmitPropName != "LastUpdateDate_UTC") { contactShortcut.LastUpdateDate_UTC = new DateTime(2005, 3, 6); } if (OmitPropName != "LastUpdateContactTVItemID") { contactShortcut.LastUpdateContactTVItemID = 2; } return(contactShortcut); }
public ContactShortcut GetContactShortcutWithContactShortcutIDDB(int ContactShortcutID) { ContactShortcut contactShortcut = (from c in db.ContactShortcuts where c.ContactShortcutID == ContactShortcutID select c).FirstOrDefault <ContactShortcut>(); return(contactShortcut); }
public void ContactShortcut_Controller_Put_Test() { foreach (LanguageEnum LanguageRequest in AllowableLanguages) { foreach (int ContactID in new List <int>() { AdminContactID }) //, TestEmailValidatedContactID, TestEmailNotValidatedContactID }) { ContactShortcutController contactShortcutController = new ContactShortcutController(DatabaseTypeEnum.SqlServerTestDB); Assert.IsNotNull(contactShortcutController); Assert.AreEqual(DatabaseTypeEnum.SqlServerTestDB, contactShortcutController.DatabaseType); ContactShortcut contactShortcutLast = new ContactShortcut(); using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { Query query = new Query(); query.Language = LanguageRequest; ContactShortcutService contactShortcutService = new ContactShortcutService(query, db, ContactID); contactShortcutLast = (from c in db.ContactShortcuts select c).FirstOrDefault(); } // ok with ContactShortcut info IHttpActionResult jsonRet = contactShortcutController.GetContactShortcutWithID(contactShortcutLast.ContactShortcutID); Assert.IsNotNull(jsonRet); OkNegotiatedContentResult <ContactShortcut> Ret = jsonRet as OkNegotiatedContentResult <ContactShortcut>; ContactShortcut contactShortcutRet = Ret.Content; Assert.AreEqual(contactShortcutLast.ContactShortcutID, contactShortcutRet.ContactShortcutID); BadRequestErrorMessageResult badRequest = jsonRet as BadRequestErrorMessageResult; Assert.IsNull(badRequest); // Put to return success IHttpActionResult jsonRet2 = contactShortcutController.Put(contactShortcutRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet2); OkNegotiatedContentResult <ContactShortcut> contactShortcutRet2 = jsonRet2 as OkNegotiatedContentResult <ContactShortcut>; Assert.IsNotNull(contactShortcutRet2); BadRequestErrorMessageResult badRequest2 = jsonRet2 as BadRequestErrorMessageResult; Assert.IsNull(badRequest2); // Put to return CSSPError because ContactShortcutID of 0 does not exist contactShortcutRet.ContactShortcutID = 0; IHttpActionResult jsonRet3 = contactShortcutController.Put(contactShortcutRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet3); OkNegotiatedContentResult <ContactShortcut> contactShortcutRet3 = jsonRet3 as OkNegotiatedContentResult <ContactShortcut>; Assert.IsNull(contactShortcutRet3); BadRequestErrorMessageResult badRequest3 = jsonRet3 as BadRequestErrorMessageResult; Assert.IsNotNull(badRequest3); } } }
public void ContactShortcut_CRUD_Test() { foreach (CultureInfo culture in AllowableCulture) { ChangeCulture(culture); using (CSSPDBContext dbTestDB = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { ContactShortcutService contactShortcutService = new ContactShortcutService(new Query() { Lang = culture.TwoLetterISOLanguageName }, dbTestDB, ContactID); int count = 0; if (count == 1) { // just so we don't get a warning during compile [The variable 'count' is assigned but its value is never used] } ContactShortcut contactShortcut = GetFilledRandomContactShortcut(""); // ------------------------------- // ------------------------------- // CRUD testing // ------------------------------- // ------------------------------- count = contactShortcutService.GetContactShortcutList().Count(); Assert.AreEqual(count, (from c in dbTestDB.ContactShortcuts select c).Count()); contactShortcutService.Add(contactShortcut); if (contactShortcut.HasErrors) { Assert.AreEqual("", contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); } Assert.AreEqual(true, contactShortcutService.GetContactShortcutList().Where(c => c == contactShortcut).Any()); contactShortcutService.Update(contactShortcut); if (contactShortcut.HasErrors) { Assert.AreEqual("", contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); } Assert.AreEqual(count + 1, contactShortcutService.GetContactShortcutList().Count()); contactShortcutService.Delete(contactShortcut); if (contactShortcut.HasErrors) { Assert.AreEqual("", contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); } Assert.AreEqual(count, contactShortcutService.GetContactShortcutList().Count()); } } }
public ContactShortcutModel PostAddContactShortcutDB(ContactShortcutModel contactShortcutModel) { string retStr = ContactShortcutModelOK(contactShortcutModel); if (!string.IsNullOrWhiteSpace(retStr)) { return(ReturnError(retStr)); } ContactOK contactOK = IsContactOK(); if (!string.IsNullOrEmpty(contactOK.Error)) { return(ReturnError(contactOK.Error)); } ContactModel contactModel = _ContactService.GetContactModelWithContactIDDB(contactShortcutModel.ContactID); if (!string.IsNullOrWhiteSpace(contactModel.Error)) { return(ReturnError(contactModel.Error)); } ContactShortcut contactShortcutNew = new ContactShortcut(); retStr = FillContactShortcut(contactShortcutNew, contactShortcutModel, contactOK); if (!string.IsNullOrWhiteSpace(retStr)) { return(ReturnError(retStr)); } using (TransactionScope ts = new TransactionScope()) { db.ContactShortcuts.Add(contactShortcutNew); retStr = DoAddChanges(); if (!string.IsNullOrWhiteSpace(retStr)) { return(ReturnError(retStr)); } LogModel logModel = _LogService.PostAddLogForObj("ContactShortcuts", contactShortcutNew.ContactShortcutID, LogCommandEnum.Add, contactShortcutNew); if (!string.IsNullOrWhiteSpace(logModel.Error)) { return(ReturnError(logModel.Error)); } ts.Complete(); } return(GetContactShortcutModelWithContactShortcutIDDB(contactShortcutNew.ContactShortcutID)); }
public IHttpActionResult GetContactShortcutWithID([FromUri] int ContactShortcutID, [FromUri] string lang = "en", [FromUri] string extra = "") { using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { ContactShortcutService contactShortcutService = new ContactShortcutService(new Query() { Language = (lang == "fr" ? LanguageEnum.fr : LanguageEnum.en) }, db, ContactID); contactShortcutService.Query = contactShortcutService.FillQuery(typeof(ContactShortcut), lang, 0, 1, "", "", extra); if (contactShortcutService.Query.Extra == "A") { ContactShortcutExtraA contactShortcutExtraA = new ContactShortcutExtraA(); contactShortcutExtraA = contactShortcutService.GetContactShortcutExtraAWithContactShortcutID(ContactShortcutID); if (contactShortcutExtraA == null) { return(NotFound()); } return(Ok(contactShortcutExtraA)); } else if (contactShortcutService.Query.Extra == "B") { ContactShortcutExtraB contactShortcutExtraB = new ContactShortcutExtraB(); contactShortcutExtraB = contactShortcutService.GetContactShortcutExtraBWithContactShortcutID(ContactShortcutID); if (contactShortcutExtraB == null) { return(NotFound()); } return(Ok(contactShortcutExtraB)); } else { ContactShortcut contactShortcut = new ContactShortcut(); contactShortcut = contactShortcutService.GetContactShortcutWithContactShortcutID(ContactShortcutID); if (contactShortcut == null) { return(NotFound()); } return(Ok(contactShortcut)); } } }
public ContactShortcutModel PostUpdateContactShortcutDB(ContactShortcutModel contactShortcutModel) { string retStr = ContactShortcutModelOK(contactShortcutModel); if (!string.IsNullOrWhiteSpace(retStr)) { return(ReturnError(retStr)); } ContactOK contactOK = IsContactOK(); if (!string.IsNullOrEmpty(contactOK.Error)) { return(ReturnError(contactOK.Error)); } ContactShortcut contactShortcutToUpdate = GetContactShortcutWithContactShortcutIDDB(contactShortcutModel.ContactShortcutID); if (contactShortcutToUpdate == null) { return(ReturnError(string.Format(ServiceRes.CouldNotFind_ToUpdate, ServiceRes.ContactShortcut))); } retStr = FillContactShortcut(contactShortcutToUpdate, contactShortcutModel, contactOK); if (!string.IsNullOrWhiteSpace(retStr)) { return(ReturnError(retStr)); } using (TransactionScope ts = new TransactionScope()) { retStr = DoUpdateChanges(); if (!string.IsNullOrWhiteSpace(retStr)) { return(ReturnError(retStr)); } LogModel logModel = _LogService.PostAddLogForObj("ContactShortcuts", contactShortcutToUpdate.ContactShortcutID, LogCommandEnum.Change, contactShortcutToUpdate); if (!string.IsNullOrWhiteSpace(logModel.Error)) { return(ReturnError(logModel.Error)); } ts.Complete(); } return(GetContactShortcutModelWithContactShortcutIDDB(contactShortcutToUpdate.ContactShortcutID)); }
private bool TryToSave(ContactShortcut contactShortcut) { try { db.SaveChanges(); } catch (DbUpdateException ex) { contactShortcut.ValidationResults = new List <ValidationResult>() { new ValidationResult(ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : "")) }.AsEnumerable(); return(false); } return(true); }
public bool Update(ContactShortcut contactShortcut) { contactShortcut.ValidationResults = Validate(new ValidationContext(contactShortcut), ActionDBTypeEnum.Update); if (contactShortcut.ValidationResults.Count() > 0) { return(false); } db.ContactShortcuts.Update(contactShortcut); if (!TryToSave(contactShortcut)) { return(false); } return(true); }
// Fill public string FillContactShortcut(ContactShortcut contactShortcut, ContactShortcutModel contactShortcutModel, ContactOK contactOK) { contactShortcut.DBCommand = (int)contactShortcutModel.DBCommand; contactShortcut.ContactShortcutID = contactShortcutModel.ContactShortcutID; contactShortcut.ContactID = (int)contactShortcutModel.ContactID; contactShortcut.ShortCutText = contactShortcutModel.ShortCutText; contactShortcut.ShortCutAddress = contactShortcutModel.ShortCutAddress; contactShortcut.LastUpdateDate_UTC = DateTime.UtcNow; if (contactOK == null) { contactShortcut.LastUpdateContactTVItemID = 2; } else { contactShortcut.LastUpdateContactTVItemID = contactOK.ContactTVItemID; } return(""); }
public void ContactShortcut_Controller_GetContactShortcutWithID_Test() { foreach (LanguageEnum LanguageRequest in AllowableLanguages) { foreach (int ContactID in new List <int>() { AdminContactID }) //, TestEmailValidatedContactID, TestEmailNotValidatedContactID }) { ContactShortcutController contactShortcutController = new ContactShortcutController(DatabaseTypeEnum.SqlServerTestDB); Assert.IsNotNull(contactShortcutController); Assert.AreEqual(DatabaseTypeEnum.SqlServerTestDB, contactShortcutController.DatabaseType); ContactShortcut contactShortcutFirst = new ContactShortcut(); using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { ContactShortcutService contactShortcutService = new ContactShortcutService(new Query(), db, ContactID); contactShortcutFirst = (from c in db.ContactShortcuts select c).FirstOrDefault(); } // ok with ContactShortcut info IHttpActionResult jsonRet = contactShortcutController.GetContactShortcutWithID(contactShortcutFirst.ContactShortcutID); Assert.IsNotNull(jsonRet); OkNegotiatedContentResult <ContactShortcut> Ret = jsonRet as OkNegotiatedContentResult <ContactShortcut>; ContactShortcut contactShortcutRet = Ret.Content; Assert.AreEqual(contactShortcutFirst.ContactShortcutID, contactShortcutRet.ContactShortcutID); BadRequestErrorMessageResult badRequest = jsonRet as BadRequestErrorMessageResult; Assert.IsNull(badRequest); // Not Found IHttpActionResult jsonRet2 = contactShortcutController.GetContactShortcutWithID(0); Assert.IsNotNull(jsonRet2); OkNegotiatedContentResult <ContactShortcut> contactShortcutRet2 = jsonRet2 as OkNegotiatedContentResult <ContactShortcut>; Assert.IsNull(contactShortcutRet2); NotFoundResult notFoundRequest = jsonRet2 as NotFoundResult; Assert.IsNotNull(notFoundRequest); } } }
public IHttpActionResult Delete([FromBody] ContactShortcut contactShortcut, [FromUri] string lang = "en") { using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { ContactShortcutService contactShortcutService = new ContactShortcutService(new Query() { Language = (lang == "fr" ? LanguageEnum.fr : LanguageEnum.en) }, db, ContactID); if (!contactShortcutService.Delete(contactShortcut)) { return(BadRequest(String.Join("|||", contactShortcut.ValidationResults))); } else { contactShortcut.ValidationResults = null; return(Ok(contactShortcut)); } } }
public void GetContactShortcutList_Test() { foreach (CultureInfo culture in AllowableCulture) { ChangeCulture(culture); using (CSSPDBContext dbTestDB = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { ContactShortcutService contactShortcutService = new ContactShortcutService(new Query() { Lang = culture.TwoLetterISOLanguageName }, dbTestDB, ContactID); ContactShortcut contactShortcut = (from c in dbTestDB.ContactShortcuts select c).FirstOrDefault(); Assert.IsNotNull(contactShortcut); List <ContactShortcut> contactShortcutDirectQueryList = new List <ContactShortcut>(); contactShortcutDirectQueryList = (from c in dbTestDB.ContactShortcuts select c).Take(200).ToList(); foreach (string extra in new List <string>() { null, "A", "B", "C", "D", "E" }) { contactShortcutService.Query.Extra = extra; if (string.IsNullOrWhiteSpace(extra)) { List <ContactShortcut> contactShortcutList = new List <ContactShortcut>(); contactShortcutList = contactShortcutService.GetContactShortcutList().ToList(); CheckContactShortcutFields(contactShortcutList); } else { //Assert.AreEqual(true, false); } } } } }
public ContactShortcutModel PostDeleteContactShortcutDB(int contactShortcutID) { ContactOK contactOK = IsContactOK(); if (!string.IsNullOrEmpty(contactOK.Error)) { return(ReturnError(contactOK.Error)); } ContactShortcut contactShortcutToDelete = GetContactShortcutWithContactShortcutIDDB(contactShortcutID); if (contactShortcutToDelete == null) { return(ReturnError(string.Format(ServiceRes.CouldNotFind_ToDelete, ServiceRes.ContactShortcut))); } using (TransactionScope ts = new TransactionScope()) { db.ContactShortcuts.Remove(contactShortcutToDelete); string retStr = DoDeleteChanges(); if (!string.IsNullOrWhiteSpace(retStr)) { return(ReturnError(retStr)); } LogModel logModel = _LogService.PostAddLogForObj("ContactShortcuts", contactShortcutToDelete.ContactShortcutID, LogCommandEnum.Delete, contactShortcutToDelete); if (!string.IsNullOrWhiteSpace(logModel.Error)) { return(ReturnError(logModel.Error)); } ts.Complete(); } return(ReturnError("")); }
public void ContactShortcut_Controller_GetContactShortcutList_Test() { foreach (LanguageEnum LanguageRequest in AllowableLanguages) { foreach (int ContactID in new List <int>() { AdminContactID }) //, TestEmailValidatedContactID, TestEmailNotValidatedContactID }) { ContactShortcutController contactShortcutController = new ContactShortcutController(DatabaseTypeEnum.SqlServerTestDB); Assert.IsNotNull(contactShortcutController); Assert.AreEqual(DatabaseTypeEnum.SqlServerTestDB, contactShortcutController.DatabaseType); ContactShortcut contactShortcutFirst = new ContactShortcut(); int count = -1; Query query = new Query(); using (CSSPDBContext db = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { ContactShortcutService contactShortcutService = new ContactShortcutService(query, db, ContactID); contactShortcutFirst = (from c in db.ContactShortcuts select c).FirstOrDefault(); count = (from c in db.ContactShortcuts select c).Count(); count = (query.Take > count ? count : query.Take); } // ok with ContactShortcut info IHttpActionResult jsonRet = contactShortcutController.GetContactShortcutList(); Assert.IsNotNull(jsonRet); OkNegotiatedContentResult <List <ContactShortcut> > ret = jsonRet as OkNegotiatedContentResult <List <ContactShortcut> >; Assert.AreEqual(contactShortcutFirst.ContactShortcutID, ret.Content[0].ContactShortcutID); Assert.AreEqual((count > query.Take ? query.Take : count), ret.Content.Count); List <ContactShortcut> contactShortcutList = new List <ContactShortcut>(); count = -1; query = new Query(); using (CSSPDBContext db = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { ContactShortcutService contactShortcutService = new ContactShortcutService(query, db, ContactID); contactShortcutList = (from c in db.ContactShortcuts select c).OrderBy(c => c.ContactShortcutID).Skip(0).Take(2).ToList(); count = (from c in db.ContactShortcuts select c).Count(); } if (count > 0) { query.Skip = 0; query.Take = 5; count = (query.Take > count ? query.Take : count); // ok with ContactShortcut info jsonRet = contactShortcutController.GetContactShortcutList(query.Language.ToString(), query.Skip, query.Take); Assert.IsNotNull(jsonRet); ret = jsonRet as OkNegotiatedContentResult <List <ContactShortcut> >; Assert.AreEqual(contactShortcutList[0].ContactShortcutID, ret.Content[0].ContactShortcutID); Assert.AreEqual((count > query.Take ? query.Take : count), ret.Content.Count); if (count > 1) { query.Skip = 1; query.Take = 5; count = (query.Take > count ? query.Take : count); // ok with ContactShortcut info IHttpActionResult jsonRet2 = contactShortcutController.GetContactShortcutList(query.Language.ToString(), query.Skip, query.Take); Assert.IsNotNull(jsonRet2); OkNegotiatedContentResult <List <ContactShortcut> > ret2 = jsonRet2 as OkNegotiatedContentResult <List <ContactShortcut> >; Assert.AreEqual(contactShortcutList[1].ContactShortcutID, ret2.Content[0].ContactShortcutID); Assert.AreEqual((count > query.Take ? query.Take : count), ret2.Content.Count); } } } } }
private IEnumerable <ValidationResult> Validate(ValidationContext validationContext, ActionDBTypeEnum actionDBType) { string retStr = ""; Enums enums = new Enums(LanguageRequest); ContactShortcut contactShortcut = validationContext.ObjectInstance as ContactShortcut; contactShortcut.HasErrors = false; if (actionDBType == ActionDBTypeEnum.Update || actionDBType == ActionDBTypeEnum.Delete) { if (contactShortcut.ContactShortcutID == 0) { contactShortcut.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "ContactShortcutID"), new[] { "ContactShortcutID" })); } if (!(from c in db.ContactShortcuts select c).Where(c => c.ContactShortcutID == contactShortcut.ContactShortcutID).Any()) { contactShortcut.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "ContactShortcut", "ContactShortcutID", contactShortcut.ContactShortcutID.ToString()), new[] { "ContactShortcutID" })); } } Contact ContactContactID = (from c in db.Contacts where c.ContactID == contactShortcut.ContactID select c).FirstOrDefault(); if (ContactContactID == null) { contactShortcut.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "Contact", "ContactID", contactShortcut.ContactID.ToString()), new[] { "ContactID" })); } if (string.IsNullOrWhiteSpace(contactShortcut.ShortCutText)) { contactShortcut.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "ShortCutText"), new[] { "ShortCutText" })); } if (!string.IsNullOrWhiteSpace(contactShortcut.ShortCutText) && contactShortcut.ShortCutText.Length > 100) { contactShortcut.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._MaxLengthIs_, "ShortCutText", "100"), new[] { "ShortCutText" })); } if (string.IsNullOrWhiteSpace(contactShortcut.ShortCutAddress)) { contactShortcut.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "ShortCutAddress"), new[] { "ShortCutAddress" })); } if (!string.IsNullOrWhiteSpace(contactShortcut.ShortCutAddress) && contactShortcut.ShortCutAddress.Length > 200) { contactShortcut.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._MaxLengthIs_, "ShortCutAddress", "200"), new[] { "ShortCutAddress" })); } if (contactShortcut.LastUpdateDate_UTC.Year == 1) { contactShortcut.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsRequired, "LastUpdateDate_UTC"), new[] { "LastUpdateDate_UTC" })); } else { if (contactShortcut.LastUpdateDate_UTC.Year < 1980) { contactShortcut.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._YearShouldBeBiggerThan_, "LastUpdateDate_UTC", "1980"), new[] { "LastUpdateDate_UTC" })); } } TVItem TVItemLastUpdateContactTVItemID = (from c in db.TVItems where c.TVItemID == contactShortcut.LastUpdateContactTVItemID select c).FirstOrDefault(); if (TVItemLastUpdateContactTVItemID == null) { contactShortcut.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TVItem", "LastUpdateContactTVItemID", contactShortcut.LastUpdateContactTVItemID.ToString()), new[] { "LastUpdateContactTVItemID" })); } else { List <TVTypeEnum> AllowableTVTypes = new List <TVTypeEnum>() { TVTypeEnum.Contact, }; if (!AllowableTVTypes.Contains(TVItemLastUpdateContactTVItemID.TVType)) { contactShortcut.HasErrors = true; yield return(new ValidationResult(string.Format(CSSPServicesRes._IsNotOfType_, "LastUpdateContactTVItemID", "Contact"), new[] { "LastUpdateContactTVItemID" })); } } retStr = ""; // added to stop compiling CSSPError if (retStr != "") // will never be true { contactShortcut.HasErrors = true; yield return(new ValidationResult("AAA", new[] { "AAA" })); } }
public void ContactShortcut_Controller_Post_Test() { foreach (LanguageEnum LanguageRequest in AllowableLanguages) { foreach (int ContactID in new List <int>() { AdminContactID }) //, TestEmailValidatedContactID, TestEmailNotValidatedContactID }) { ContactShortcutController contactShortcutController = new ContactShortcutController(DatabaseTypeEnum.SqlServerTestDB); Assert.IsNotNull(contactShortcutController); Assert.AreEqual(DatabaseTypeEnum.SqlServerTestDB, contactShortcutController.DatabaseType); ContactShortcut contactShortcutLast = new ContactShortcut(); using (CSSPDBContext db = new CSSPDBContext(DatabaseType)) { Query query = new Query(); query.Language = LanguageRequest; query.Asc = ""; query.Desc = ""; ContactShortcutService contactShortcutService = new ContactShortcutService(query, db, ContactID); contactShortcutLast = (from c in db.ContactShortcuts select c).FirstOrDefault(); } // ok with ContactShortcut info IHttpActionResult jsonRet = contactShortcutController.GetContactShortcutWithID(contactShortcutLast.ContactShortcutID); Assert.IsNotNull(jsonRet); OkNegotiatedContentResult <ContactShortcut> Ret = jsonRet as OkNegotiatedContentResult <ContactShortcut>; ContactShortcut contactShortcutRet = Ret.Content; Assert.AreEqual(contactShortcutLast.ContactShortcutID, contactShortcutRet.ContactShortcutID); BadRequestErrorMessageResult badRequest = jsonRet as BadRequestErrorMessageResult; Assert.IsNull(badRequest); // Post to return CSSPError because ContactShortcutID exist IHttpActionResult jsonRet2 = contactShortcutController.Post(contactShortcutRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet2); OkNegotiatedContentResult <ContactShortcut> contactShortcutRet2 = jsonRet2 as OkNegotiatedContentResult <ContactShortcut>; Assert.IsNull(contactShortcutRet2); BadRequestErrorMessageResult badRequest2 = jsonRet2 as BadRequestErrorMessageResult; Assert.IsNotNull(badRequest2); // Post to return newly added ContactShortcut contactShortcutRet.ContactShortcutID = 0; contactShortcutController.Request = new System.Net.Http.HttpRequestMessage(); contactShortcutController.Request.RequestUri = new System.Uri("http://localhost:5000/api/contactShortcut"); IHttpActionResult jsonRet3 = contactShortcutController.Post(contactShortcutRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet3); CreatedNegotiatedContentResult <ContactShortcut> contactShortcutRet3 = jsonRet3 as CreatedNegotiatedContentResult <ContactShortcut>; Assert.IsNotNull(contactShortcutRet3); BadRequestErrorMessageResult badRequest3 = jsonRet3 as BadRequestErrorMessageResult; Assert.IsNull(badRequest3); IHttpActionResult jsonRet4 = contactShortcutController.Delete(contactShortcutRet, LanguageRequest.ToString()); Assert.IsNotNull(jsonRet4); OkNegotiatedContentResult <ContactShortcut> contactShortcutRet4 = jsonRet4 as OkNegotiatedContentResult <ContactShortcut>; Assert.IsNotNull(contactShortcutRet4); BadRequestErrorMessageResult badRequest4 = jsonRet4 as BadRequestErrorMessageResult; Assert.IsNull(badRequest4); } } }
public ContactShortcutTest() { contactShortcut = new ContactShortcut(); contactShortcutExtraA = new ContactShortcutExtraA(); contactShortcutExtraB = new ContactShortcutExtraB(); }
public void ContactShortcut_Properties_Test() { foreach (CultureInfo culture in AllowableCulture) { ChangeCulture(culture); using (CSSPDBContext dbTestDB = new CSSPDBContext(DatabaseTypeEnum.SqlServerTestDB)) { ContactShortcutService contactShortcutService = new ContactShortcutService(new Query() { Lang = culture.TwoLetterISOLanguageName }, dbTestDB, ContactID); int count = 0; if (count == 1) { // just so we don't get a warning during compile [The variable 'count' is assigned but its value is never used] } count = contactShortcutService.GetContactShortcutList().Count(); ContactShortcut contactShortcut = GetFilledRandomContactShortcut(""); // ------------------------------- // ------------------------------- // Properties testing // ------------------------------- // ------------------------------- // ----------------------------------- // [Key] // Is NOT Nullable // contactShortcut.ContactShortcutID (Int32) // ----------------------------------- contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut(""); contactShortcut.ContactShortcutID = 0; contactShortcutService.Update(contactShortcut); Assert.AreEqual(string.Format(CSSPServicesRes._IsRequired, "ContactShortcutID"), contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut(""); contactShortcut.ContactShortcutID = 10000000; contactShortcutService.Update(contactShortcut); Assert.AreEqual(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "ContactShortcut", "ContactShortcutID", contactShortcut.ContactShortcutID.ToString()), contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [CSSPExist(ExistTypeName = "Contact", ExistPlurial = "s", ExistFieldID = "ContactID", AllowableTVtypeList = )] // contactShortcut.ContactID (Int32) // ----------------------------------- contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut(""); contactShortcut.ContactID = 0; contactShortcutService.Add(contactShortcut); Assert.AreEqual(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "Contact", "ContactID", contactShortcut.ContactID.ToString()), contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [StringLength(100))] // contactShortcut.ShortCutText (String) // ----------------------------------- contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut("ShortCutText"); Assert.AreEqual(false, contactShortcutService.Add(contactShortcut)); Assert.AreEqual(1, contactShortcut.ValidationResults.Count()); Assert.IsTrue(contactShortcut.ValidationResults.Where(c => c.ErrorMessage == string.Format(CSSPServicesRes._IsRequired, "ShortCutText")).Any()); Assert.AreEqual(null, contactShortcut.ShortCutText); Assert.AreEqual(count, contactShortcutService.GetContactShortcutList().Count()); contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut(""); contactShortcut.ShortCutText = GetRandomString("", 101); Assert.AreEqual(false, contactShortcutService.Add(contactShortcut)); Assert.AreEqual(string.Format(CSSPServicesRes._MaxLengthIs_, "ShortCutText", "100"), contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); Assert.AreEqual(count, contactShortcutService.GetContactShortcutList().Count()); // ----------------------------------- // Is NOT Nullable // [StringLength(200))] // contactShortcut.ShortCutAddress (String) // ----------------------------------- contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut("ShortCutAddress"); Assert.AreEqual(false, contactShortcutService.Add(contactShortcut)); Assert.AreEqual(1, contactShortcut.ValidationResults.Count()); Assert.IsTrue(contactShortcut.ValidationResults.Where(c => c.ErrorMessage == string.Format(CSSPServicesRes._IsRequired, "ShortCutAddress")).Any()); Assert.AreEqual(null, contactShortcut.ShortCutAddress); Assert.AreEqual(count, contactShortcutService.GetContactShortcutList().Count()); contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut(""); contactShortcut.ShortCutAddress = GetRandomString("", 201); Assert.AreEqual(false, contactShortcutService.Add(contactShortcut)); Assert.AreEqual(string.Format(CSSPServicesRes._MaxLengthIs_, "ShortCutAddress", "200"), contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); Assert.AreEqual(count, contactShortcutService.GetContactShortcutList().Count()); // ----------------------------------- // Is NOT Nullable // [CSSPAfter(Year = 1980)] // contactShortcut.LastUpdateDate_UTC (DateTime) // ----------------------------------- contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut(""); contactShortcut.LastUpdateDate_UTC = new DateTime(); contactShortcutService.Add(contactShortcut); Assert.AreEqual(string.Format(CSSPServicesRes._IsRequired, "LastUpdateDate_UTC"), contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut(""); contactShortcut.LastUpdateDate_UTC = new DateTime(1979, 1, 1); contactShortcutService.Add(contactShortcut); Assert.AreEqual(string.Format(CSSPServicesRes._YearShouldBeBiggerThan_, "LastUpdateDate_UTC", "1980"), contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [CSSPExist(ExistTypeName = "TVItem", ExistPlurial = "s", ExistFieldID = "TVItemID", AllowableTVtypeList = Contact)] // contactShortcut.LastUpdateContactTVItemID (Int32) // ----------------------------------- contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut(""); contactShortcut.LastUpdateContactTVItemID = 0; contactShortcutService.Add(contactShortcut); Assert.AreEqual(string.Format(CSSPServicesRes.CouldNotFind_With_Equal_, "TVItem", "LastUpdateContactTVItemID", contactShortcut.LastUpdateContactTVItemID.ToString()), contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); contactShortcut = null; contactShortcut = GetFilledRandomContactShortcut(""); contactShortcut.LastUpdateContactTVItemID = 1; contactShortcutService.Add(contactShortcut); Assert.AreEqual(string.Format(CSSPServicesRes._IsNotOfType_, "LastUpdateContactTVItemID", "Contact"), contactShortcut.ValidationResults.FirstOrDefault().ErrorMessage); // ----------------------------------- // Is NOT Nullable // [NotMapped] // contactShortcut.HasErrors (Boolean) // ----------------------------------- // No testing requied // ----------------------------------- // Is NOT Nullable // [NotMapped] // contactShortcut.ValidationResults (IEnumerable`1) // ----------------------------------- // No testing requied } } }