Exemplo n.º 1
0
        /// <inheritdoc />
        public RequestResult <IEnumerable <UserComment> > GetEntryComments(string hdId, string parentEntryId)
        {
            UserProfile profile = this.profileDelegate.GetUserProfile(hdId).Payload;
            string?     key     = profile.EncryptionKey;

            // Check that the key has been set
            if (key == null)
            {
                this.logger.LogError($"User does not have a key: ${hdId}");
                return(new RequestResult <IEnumerable <UserComment> >()
                {
                    ResultStatus = ResultType.Error,
                    ResultError = new RequestResultError()
                    {
                        ResultMessage = "Profile Key not set", ErrorCode = ErrorTranslator.InternalError(ErrorType.InvalidState)
                    },
                });
            }

            DBResult <IEnumerable <Comment> >          dbComments = this.commentDelegate.GetByParentEntry(hdId, parentEntryId);
            RequestResult <IEnumerable <UserComment> > result     = new RequestResult <IEnumerable <UserComment> >()
            {
                ResourcePayload  = UserComment.CreateListFromDbModel(dbComments.Payload, this.cryptoDelegate, key),
                TotalResultCount = dbComments.Payload.Count(),
                PageIndex        = 0,
                PageSize         = dbComments.Payload.Count(),
                ResultStatus     = dbComments.Status == DBStatusCode.Read ? ResultType.Success : ResultType.Error,
                ResultError      = dbComments.Status != DBStatusCode.Read ? new RequestResultError()
                {
                    ResultMessage = dbComments.Message, ErrorCode = ErrorTranslator.ServiceError(ErrorType.CommunicationInternal, ServiceType.Database)
                } : null,
            };

            return(result);
        }
Exemplo n.º 2
0
        public void ShouldGetComments()
        {
            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));

            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  = DBStatusCode.Read
            };

            Mock <ICommentDelegate> commentDelegateMock = new Mock <ICommentDelegate>();

            commentDelegateMock.Setup(s => s.GetList(hdid, parentEntryId)).Returns(commentsDBResult);

            ICommentService service = new CommentService(
                new Mock <ILogger <CommentService> >().Object,
                commentDelegateMock.Object,
                profileDelegateMock.Object,
                cryptoDelegateMock.Object
                );

            RequestResult <IEnumerable <UserComment> > actualResult = service.GetList(hdid, parentEntryId);

            Assert.Equal(Common.Constants.ResultType.Success, actualResult.ResultStatus);
            Assert.True(actualResult.ResourcePayload.IsDeepEqual(userCommentList));
        }
Exemplo n.º 3
0
        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));
        }