/// <inheritdoc /> public RequestResult <UserNote> CreateNote(UserNote userNote) { UserProfile profile = this.profileDelegate.GetUserProfile(userNote.HdId).Payload; string? key = profile.EncryptionKey; if (key == null) { this.logger.LogError($"User does not have a key: ${userNote.HdId}"); return(new RequestResult <UserNote>() { ResultStatus = ResultType.Error, ResultError = new RequestResultError() { ResultMessage = "Profile Key not set", ErrorCode = ErrorTranslator.InternalError(ErrorType.InvalidState) }, }); } Note note = userNote.ToDbModel(this.cryptoDelegate, key); DBResult <Note> dbNote = this.noteDelegate.AddNote(note); RequestResult <UserNote> result = new RequestResult <UserNote>() { ResourcePayload = UserNote.CreateFromDbModel(dbNote.Payload, this.cryptoDelegate, key), ResultStatus = dbNote.Status == DBStatusCode.Created ? ResultType.Success : ResultType.Error, ResultError = dbNote.Status == DBStatusCode.Created ? null : new RequestResultError() { ResultMessage = dbNote.Message, ErrorCode = ErrorTranslator.ServiceError(ErrorType.CommunicationInternal, ServiceType.Database) }, }; return(result); }
public void ShouldUpdateNote() { 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 = Database.Constants.DBStatusCode.Updated }; 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); Assert.Equal(Common.Constants.ResultType.Success, actualResult.ResultStatus); Assert.True(actualResult.ResourcePayload.IsDeepEqual(userNote)); }
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)); }