private Tuple <RequestResult <UserNote>, UserNote> ExecuteUpdateNote(Database.Constants.DBStatusCode dBStatusCode = Database.Constants.DBStatusCode.Updated) { string encryptionKey = "abc"; DBResult <UserProfile> profileDBResult = new DBResult <UserProfile> { Payload = new UserProfile() { EncryptionKey = encryptionKey } }; Mock <IUserProfileDelegate> profileDelegateMock = new Mock <IUserProfileDelegate>(); profileDelegateMock.Setup(s => s.GetUserProfile(hdid)).Returns(profileDBResult); Mock <ICryptoDelegate> cryptoDelegateMock = new Mock <ICryptoDelegate>(); cryptoDelegateMock.Setup(s => s.Encrypt(It.IsAny <string>(), It.IsAny <string>())).Returns((string key, string text) => text + key); cryptoDelegateMock.Setup(s => s.Decrypt(It.IsAny <string>(), It.IsAny <string>())).Returns((string key, string text) => text.Remove(text.Length - key.Length)); UserNote userNote = new UserNote() { HdId = hdid, Title = "Updated Note", Text = "Updated Note text", CreatedDateTime = new DateTime(2020, 1, 1) }; Note note = userNote.ToDbModel(cryptoDelegateMock.Object, encryptionKey); DBResult <Note> updateResult = new DBResult <Note> { Payload = note, Status = dBStatusCode }; Mock <INoteDelegate> noteDelegateMock = new Mock <INoteDelegate>(); noteDelegateMock.Setup(s => s.UpdateNote(It.Is <Note>(x => x.Text == note.Text), true)).Returns(updateResult); INoteService service = new NoteService( new Mock <ILogger <NoteService> >().Object, noteDelegateMock.Object, profileDelegateMock.Object, cryptoDelegateMock.Object ); RequestResult <UserNote> actualResult = service.UpdateNote(userNote); return(new Tuple <RequestResult <UserNote>, UserNote>(actualResult, userNote)); }
private RequestResult <UserPreferenceModel> ExecuteUpdateUserPreference(Database.Constants.DBStatusCode dbResultStatus = Database.Constants.DBStatusCode.Updated) { UserPreferenceModel userPreferenceModel = new UserPreferenceModel { HdId = hdid, Preference = "TutorialPopover", Value = "mocked value", }; DBResult <UserPreference> readResult = new DBResult <UserPreference> { Payload = userPreferenceModel.ToDbModel(), Status = dbResultStatus }; Mock <IUserPreferenceDelegate> preferenceDelegateMock = new Mock <IUserPreferenceDelegate>(); preferenceDelegateMock.Setup(s => s.UpdateUserPreference(It.IsAny <UserPreference>(), It.IsAny <bool>())).Returns(readResult); Mock <IEmailQueueService> emailer = new Mock <IEmailQueueService>(); Mock <IUserProfileDelegate> profileDelegateMock = new Mock <IUserProfileDelegate>(); Mock <IEmailDelegate> emailDelegateMock = new Mock <IEmailDelegate>(); Mock <IMessagingVerificationDelegate> emailInviteDelegateMock = new Mock <IMessagingVerificationDelegate>(); Mock <IConfigurationService> configServiceMock = new Mock <IConfigurationService>(); Mock <ILegalAgreementDelegate> legalAgreementDelegateMock = new Mock <ILegalAgreementDelegate>(); Mock <ICryptoDelegate> cryptoDelegateMock = new Mock <ICryptoDelegate>(); Mock <INotificationSettingsService> notificationServiceMock = new Mock <INotificationSettingsService>(); Mock <IMessagingVerificationDelegate> messageVerificationDelegateMock = new Mock <IMessagingVerificationDelegate>(); IUserProfileService service = new UserProfileService( new Mock <ILogger <UserProfileService> >().Object, new Mock <IPatientService>().Object, new Mock <IUserEmailService>().Object, new Mock <IUserSMSService>().Object, configServiceMock.Object, new Mock <IEmailQueueService>().Object, notificationServiceMock.Object, profileDelegateMock.Object, preferenceDelegateMock.Object, new Mock <ILegalAgreementDelegate>().Object, messageVerificationDelegateMock.Object, cryptoDelegateMock.Object, new Mock <IHttpContextAccessor>().Object); return(service.UpdateUserPreference(userPreferenceModel)); }
private Tuple <RequestResult <UserProfileModel>, UserProfileModel> ExecuteRecoverUserProfile(UserProfile userProfile, Database.Constants.DBStatusCode dbResultStatus = Database.Constants.DBStatusCode.Read) { DBResult <UserProfile> userProfileDBResult = new DBResult <UserProfile> { Payload = userProfile, Status = dbResultStatus }; UserProfileModel expected = UserProfileModel.CreateFromDbModel(userProfile); Mock <IUserProfileDelegate> profileDelegateMock = new Mock <IUserProfileDelegate>(); profileDelegateMock.Setup(s => s.GetUserProfile(hdid)).Returns(userProfileDBResult); profileDelegateMock.Setup(s => s.Update(userProfile, true)).Returns(userProfileDBResult); Mock <IEmailQueueService> emailQueueServiceMock = new Mock <IEmailQueueService>(); emailQueueServiceMock .Setup(s => s.QueueNewEmail(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Dictionary <string, string> >(), false)); IHeaderDictionary headerDictionary = new HeaderDictionary(); headerDictionary.Add("referer", "http://localhost/"); Mock <HttpRequest> httpRequestMock = new Mock <HttpRequest>(); httpRequestMock.Setup(s => s.Headers).Returns(headerDictionary); Mock <HttpContext> httpContextMock = new Mock <HttpContext>(); httpContextMock.Setup(s => s.Request).Returns(httpRequestMock.Object); Mock <IHttpContextAccessor> httpContextAccessorMock = new Mock <IHttpContextAccessor>(); httpContextAccessorMock.Setup(s => s.HttpContext).Returns(httpContextMock.Object); IUserProfileService service = new UserProfileService( new Mock <ILogger <UserProfileService> >().Object, new Mock <IPatientService>().Object, new Mock <IUserEmailService>().Object, new Mock <IUserSMSService>().Object, new Mock <IConfigurationService>().Object, emailQueueServiceMock.Object, new Mock <INotificationSettingsService>().Object, profileDelegateMock.Object, new Mock <IUserPreferenceDelegate>().Object, new Mock <ILegalAgreementDelegate>().Object, new Mock <IMessagingVerificationDelegate>().Object, new Mock <ICryptoDelegate>().Object, httpContextAccessorMock.Object); RequestResult <UserProfileModel> actualResult = service.RecoverUserProfile(hdid); return(new Tuple <RequestResult <UserProfileModel>, UserProfileModel>(actualResult, expected)); }
// TODO: Remove helper methods private Tuple <RequestResult <UserProfileModel>, UserProfileModel> ExecuteGetUserProfile(Database.Constants.DBStatusCode dbResultStatus, DateTime lastLoginDateTime) { UserProfile userProfile = new UserProfile { HdId = hdid, AcceptedTermsOfService = true, LastLoginDateTime = lastLoginDateTime, }; DBResult <UserProfile> userProfileDBResult = new DBResult <UserProfile> { Payload = userProfile, Status = dbResultStatus }; UserProfileModel expected = UserProfileModel.CreateFromDbModel(userProfile); expected.HasTermsOfServiceUpdated = true; LegalAgreement termsOfService = new LegalAgreement() { Id = Guid.NewGuid(), LegalText = "", EffectiveDate = DateTime.Now }; Mock <IEmailQueueService> emailer = new Mock <IEmailQueueService>(); Mock <IUserProfileDelegate> profileDelegateMock = new Mock <IUserProfileDelegate>(); profileDelegateMock.Setup(s => s.GetUserProfile(hdid)).Returns(userProfileDBResult); profileDelegateMock.Setup(s => s.Update(userProfile, true)).Returns(userProfileDBResult); UserPreference dbUserPreference = new UserPreference { HdId = hdid, Preference = "TutorialPopover", Value = true.ToString(), }; List <UserPreference> userPreferences = new List <UserPreference>(); userPreferences.Add(dbUserPreference); DBResult <IEnumerable <UserPreference> > readResult = new DBResult <IEnumerable <UserPreference> > { Payload = userPreferences, Status = DBStatusCode.Read }; Mock <IUserPreferenceDelegate> preferenceDelegateMock = new Mock <IUserPreferenceDelegate>(); preferenceDelegateMock.Setup(s => s.GetUserPreferences(hdid)).Returns(readResult); Mock <IMessagingVerificationDelegate> emailInviteDelegateMock = new Mock <IMessagingVerificationDelegate>(); emailInviteDelegateMock.Setup(s => s.GetLastByInviteKey(It.IsAny <Guid>())).Returns(new MessagingVerification()); Mock <IConfigurationService> configServiceMock = new Mock <IConfigurationService>(); configServiceMock.Setup(s => s.GetConfiguration()).Returns(new ExternalConfiguration()); Mock <ILegalAgreementDelegate> legalAgreementDelegateMock = new Mock <ILegalAgreementDelegate>(); legalAgreementDelegateMock .Setup(s => s.GetActiveByAgreementType(LegalAgreementType.TermsofService)) .Returns(new DBResult <LegalAgreement>() { Payload = termsOfService }); Mock <ICryptoDelegate> cryptoDelegateMock = new Mock <ICryptoDelegate>(); Mock <INotificationSettingsService> notificationServiceMock = new Mock <INotificationSettingsService>(); Mock <IMessagingVerificationDelegate> messageVerificationDelegateMock = new Mock <IMessagingVerificationDelegate>(); IUserProfileService service = new UserProfileService( new Mock <ILogger <UserProfileService> >().Object, new Mock <IPatientService>().Object, new Mock <IUserEmailService>().Object, new Mock <IUserSMSService>().Object, configServiceMock.Object, new Mock <IEmailQueueService>().Object, notificationServiceMock.Object, profileDelegateMock.Object, preferenceDelegateMock.Object, legalAgreementDelegateMock.Object, messageVerificationDelegateMock.Object, cryptoDelegateMock.Object, new Mock <IHttpContextAccessor>().Object ); RequestResult <UserProfileModel> actualResult = service.GetUserProfile(hdid, DateTime.Now); return(new Tuple <RequestResult <UserProfileModel>, UserProfileModel>(actualResult, expected)); }
private async Task <Tuple <RequestResult <UserProfileModel>, UserProfileModel> > ExecuteInsertUserProfile(Database.Constants.DBStatusCode dbResultStatus, string registrationStatus, MessagingVerification messagingVerification = null) { UserProfile userProfile = new UserProfile { HdId = hdid, AcceptedTermsOfService = true, Email = "*****@*****.**" }; UserProfileModel expected = UserProfileModel.CreateFromDbModel(userProfile); Mock <IUserProfileDelegate> profileDelegateMock = new Mock <IUserProfileDelegate>(); profileDelegateMock.Setup(s => s.InsertUserProfile(It.Is <UserProfile>(x => x.HdId == userProfile.HdId))).Returns(new DBResult <UserProfile> { Payload = userProfile, Status = DBStatusCode.Created }); Mock <IConfigurationService> configServiceMock = new Mock <IConfigurationService>(); configServiceMock.Setup(s => s.GetConfiguration()).Returns(new ExternalConfiguration() { WebClient = new WebClientConfiguration() { RegistrationStatus = registrationStatus } }); Mock <ICryptoDelegate> cryptoDelegateMock = new Mock <ICryptoDelegate>(); cryptoDelegateMock.Setup(s => s.GenerateKey()).Returns("abc"); Mock <INotificationSettingsService> notificationServiceMock = new Mock <INotificationSettingsService>(); notificationServiceMock.Setup(s => s.QueueNotificationSettings(It.IsAny <NotificationSettingsRequest>())); IUserProfileService service = new UserProfileService( new Mock <ILogger <UserProfileService> >().Object, new Mock <IPatientService>().Object, new Mock <IUserEmailService>().Object, new Mock <IUserSMSService>().Object, configServiceMock.Object, new Mock <IEmailQueueService>().Object, notificationServiceMock.Object, profileDelegateMock.Object, new Mock <IUserPreferenceDelegate>().Object, new Mock <ILegalAgreementDelegate>().Object, new Mock <IMessagingVerificationDelegate>().Object, cryptoDelegateMock.Object, new Mock <IHttpContextAccessor>().Object); RequestResult <UserProfileModel> actualResult = await service.CreateUserProfile(new CreateUserRequest() { Profile = userProfile }, DateTime.Today); return(new Tuple <RequestResult <UserProfileModel>, UserProfileModel>(actualResult, expected)); }
private Tuple <RequestResult <Communication>, Communication> ExecuteGetActiveCommunication(Database.Constants.DBStatusCode dbResultStatus = Database.Constants.DBStatusCode.Read) { Communication communication = new Communication { Id = Guid.NewGuid(), EffectiveDateTime = DateTime.UtcNow.AddDays(-1), ExpiryDateTime = DateTime.UtcNow.AddDays(2) }; DBResult <Communication> dbResult = new DBResult <Communication> { Payload = communication, Status = dbResultStatus }; ServiceCollection services = new ServiceCollection(); services.AddMemoryCache(); ServiceProvider serviceProvider = services.BuildServiceProvider(); IMemoryCache memoryCache = serviceProvider.GetService <IMemoryCache>(); Mock <ICommunicationDelegate> communicationDelegateMock = new Mock <ICommunicationDelegate>(); communicationDelegateMock.Setup(s => s.GetActiveBanner()).Returns(dbResult); ICommunicationService service = new CommunicationService( new Mock <ILogger <CommunicationService> >().Object, communicationDelegateMock.Object, memoryCache ); RequestResult <Communication> actualResult = service.GetActiveBanner(); return(new Tuple <RequestResult <Communication>, Communication>(actualResult, communication)); }
private Tuple <RequestResult <IEnumerable <UserNote> >, List <UserNote> > ExecuteGetNotes(string encryptionKey = null, Database.Constants.DBStatusCode notesDBResultStatus = Database.Constants.DBStatusCode.Read) { DBResult <UserProfile> profileDBResult = new DBResult <UserProfile> { Payload = new UserProfile() { EncryptionKey = encryptionKey } }; Mock <IUserProfileDelegate> profileDelegateMock = new Mock <IUserProfileDelegate>(); profileDelegateMock.Setup(s => s.GetUserProfile(hdid)).Returns(profileDBResult); Mock <ICryptoDelegate> cryptoDelegateMock = new Mock <ICryptoDelegate>(); cryptoDelegateMock.Setup(s => s.Encrypt(It.IsAny <string>(), It.IsAny <string>())).Returns((string key, string text) => text + key); cryptoDelegateMock.Setup(s => s.Decrypt(It.IsAny <string>(), It.IsAny <string>())).Returns((string key, string text) => text.Remove(text.Length - key.Length)); List <Note> noteList = new List <Note>(); noteList.Add(new Note { HdId = hdid, Title = "First Note", Text = "First Note text", CreatedDateTime = new DateTime(2020, 1, 1) }); noteList.Add(new Note { HdId = hdid, Title = "Second Note", Text = "Second Note text", CreatedDateTime = new DateTime(2020, 2, 2) }); List <UserNote> userNoteList = null; if (encryptionKey != null) { userNoteList = UserNote.CreateListFromDbModel(noteList, cryptoDelegateMock.Object, encryptionKey).ToList(); } DBResult <IEnumerable <Note> > notesDBResult = new DBResult <IEnumerable <Note> > { Payload = noteList, Status = notesDBResultStatus }; Mock <INoteDelegate> noteDelegateMock = new Mock <INoteDelegate>(); noteDelegateMock.Setup(s => s.GetNotes(hdid, 0, 500)).Returns(notesDBResult); INoteService service = new NoteService( new Mock <ILogger <NoteService> >().Object, noteDelegateMock.Object, profileDelegateMock.Object, cryptoDelegateMock.Object ); var userNoteResult = service.GetNotes(hdid, 0, 500); return(new Tuple <RequestResult <IEnumerable <UserNote> >, List <UserNote> >(userNoteResult, userNoteList)); }
private void GetPatient(PatientIdentifierType identifierType, Dictionary <string, string> configDictionary, Database.Constants.DBStatusCode mockDBStatusCode, bool returnValidCache = false) { RequestResult <PatientModel> requestResult = new RequestResult <PatientModel>() { ResultStatus = Common.Constants.ResultType.Success, TotalResultCount = 1, PageSize = 1, ResourcePayload = new PatientModel() { FirstName = "John", LastName = "Doe", HdId = hdid, }, }; Mock <IClientRegistriesDelegate> patientDelegateMock = new Mock <IClientRegistriesDelegate>(); var config = new ConfigurationBuilder() .AddInMemoryCollection(configDictionary) .Build(); patientDelegateMock.Setup(p => p.GetDemographicsByHDIDAsync(It.IsAny <string>())).ReturnsAsync(requestResult); patientDelegateMock.Setup(p => p.GetDemographicsByPHNAsync(It.IsAny <string>())).ReturnsAsync(requestResult); DBResult <GenericCache> dbResult = new DBResult <GenericCache>() { Status = mockDBStatusCode, }; Mock <IGenericCacheDelegate> genericCacheDelegateMock = new Mock <IGenericCacheDelegate>(); genericCacheDelegateMock.Setup(p => p.CacheObject(It.IsAny <object>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>(), true)).Returns(dbResult); if (returnValidCache) { genericCacheDelegateMock.Setup(p => p.GetCacheObject <PatientModel>(It.IsAny <string>(), It.IsAny <string>())).Returns(requestResult.ResourcePayload); } IPatientService service = new PatientService( new Mock <ILogger <PatientService> >().Object, config, patientDelegateMock.Object, genericCacheDelegateMock.Object); // Act RequestResult <PatientModel> actual = Task.Run(async() => await service.GetPatient(hdid, identifierType).ConfigureAwait(true)).Result; // Verify Assert.Equal(Common.Constants.ResultType.Success, actual.ResultStatus); Assert.Equal(hdid, actual.ResourcePayload.HdId); }
private Tuple <RequestResult <IEnumerable <UserComment> >, List <UserComment> > ExecuteGetComments(string encryptionKey = null, Database.Constants.DBStatusCode dbResultStatus = Database.Constants.DBStatusCode.Read) { DBResult <UserProfile> profileDBResult = new DBResult <UserProfile> { Payload = new UserProfile() { EncryptionKey = encryptionKey } }; Mock <IUserProfileDelegate> profileDelegateMock = new Mock <IUserProfileDelegate>(); profileDelegateMock.Setup(s => s.GetUserProfile(hdid)).Returns(profileDBResult); Mock <ICryptoDelegate> cryptoDelegateMock = new Mock <ICryptoDelegate>(); cryptoDelegateMock.Setup(s => s.Encrypt(It.IsAny <string>(), It.IsAny <string>())).Returns((string key, string text) => text + key); cryptoDelegateMock.Setup(s => s.Decrypt(It.IsAny <string>(), It.IsAny <string>())).Returns((string key, string text) => text.Remove(text.Length - key.Length)); List <Comment> commentList = new List <Comment>(); commentList.Add(new Comment { UserProfileId = hdid, ParentEntryId = parentEntryId, Text = "First Comment", EntryTypeCode = CommentEntryType.Medication, CreatedDateTime = new DateTime(2020, 1, 1) }); commentList.Add(new Comment { UserProfileId = hdid, ParentEntryId = parentEntryId, Text = "Second Comment", EntryTypeCode = CommentEntryType.Medication, CreatedDateTime = new DateTime(2020, 2, 2) }); List <UserComment> userCommentList = UserComment.CreateListFromDbModel(commentList, cryptoDelegateMock.Object, encryptionKey).ToList(); DBResult <IEnumerable <Comment> > commentsDBResult = new DBResult <IEnumerable <Comment> > { Payload = commentList, Status = dbResultStatus }; Mock <ICommentDelegate> commentDelegateMock = new Mock <ICommentDelegate>(); commentDelegateMock.Setup(s => s.GetByParentEntry(hdid, parentEntryId)).Returns(commentsDBResult); ICommentService service = new CommentService( new Mock <ILogger <CommentService> >().Object, commentDelegateMock.Object, profileDelegateMock.Object, cryptoDelegateMock.Object ); RequestResult <IEnumerable <UserComment> > actualResult = service.GetEntryComments(hdid, parentEntryId); return(new Tuple <RequestResult <IEnumerable <UserComment> >, List <UserComment> >(actualResult, userCommentList)); }
private Tuple <RequestResult <UserComment>, UserComment> ExecuteDeleteComment(Database.Constants.DBStatusCode dBStatusCode = Database.Constants.DBStatusCode.Deleted) { string encryptionKey = "abc"; DBResult <UserProfile> profileDBResult = new DBResult <UserProfile> { Payload = new UserProfile() { EncryptionKey = encryptionKey } }; Mock <IUserProfileDelegate> profileDelegateMock = new Mock <IUserProfileDelegate>(); profileDelegateMock.Setup(s => s.GetUserProfile(hdid)).Returns(profileDBResult); Mock <ICryptoDelegate> cryptoDelegateMock = new Mock <ICryptoDelegate>(); cryptoDelegateMock.Setup(s => s.Encrypt(It.IsAny <string>(), It.IsAny <string>())).Returns((string key, string text) => text + key); cryptoDelegateMock.Setup(s => s.Decrypt(It.IsAny <string>(), It.IsAny <string>())).Returns((string key, string text) => text.Remove(text.Length - key.Length)); UserComment userComment = new UserComment() { UserProfileId = hdid, ParentEntryId = parentEntryId, Text = "Deleted Comment", EntryTypeCode = CommentEntryType.Medication, CreatedDateTime = new DateTime(2020, 1, 1) }; Comment comment = userComment.ToDbModel(cryptoDelegateMock.Object, encryptionKey); DBResult <Comment> deleteResult = new DBResult <Comment> { Payload = comment, Status = dBStatusCode }; Mock <ICommentDelegate> commentDelegateMock = new Mock <ICommentDelegate>(); commentDelegateMock.Setup(s => s.Delete(It.Is <Comment>(x => x.Text == comment.Text), true)).Returns(deleteResult); ICommentService service = new CommentService( new Mock <ILogger <CommentService> >().Object, commentDelegateMock.Object, profileDelegateMock.Object, cryptoDelegateMock.Object ); RequestResult <UserComment> actualResult = service.Delete(userComment); return(new Tuple <RequestResult <UserComment>, UserComment>(actualResult, userComment)); }