public void RelationshipController_InitiateUserRelationship_Returns_Status_Accepted_When_Default_Relationship_Action_Is_Accepted()
        {
            //Arrange
            var initiatingUser = new UserInfo { UserID = Constants.USER_TenId, PortalID = Constants.PORTAL_Zero };
            var targetUser = new UserInfo { UserID = Constants.USER_ElevenId, PortalID = Constants.PORTAL_Zero };
            var relationship = new Relationship { RelationshipId = Constants.SOCIAL_FollowerRelationshipID, RelationshipTypeId = Constants.SOCIAL_FollowerRelationshipTypeID, DefaultResponse = RelationshipStatus.Accepted };
           
            dtUserRelationships.Rows.Clear();
            dtUserRelationshipPreferences.Rows.Clear();

            //setup mock DataService
            var mockDataService = new Mock<IDataService>();
            mockDataService.Setup(md => md.GetUserRelationship(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<RelationshipDirection>())).Returns(dtUserRelationships.CreateDataReader());
            mockDataService.Setup(md => md.GetUserRelationshipPreference(It.IsAny<int>(), It.IsAny<int>())).Returns(dtUserRelationshipPreferences.CreateDataReader());
            mockDataService.Setup(md => md.GetAllRelationshipTypes()).Returns(dtRelationshipTypes.CreateDataReader());

            var relationshipController = CreateRelationshipController(mockDataService);

            //Act
            var userRelationship = relationshipController.InitiateUserRelationship(initiatingUser, targetUser, relationship);

            //Assert
            Assert.AreEqual(userRelationship.Status, RelationshipStatus.Accepted);
        }
        public void RelationshipController_SaveRelationship_Calls_EventLogController_AddLog()
        {
            //Arrange
            var mockEventLogController = new Mock<IEventLogController>();
            mockEventLogController.Setup(c => c.AddLog(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<EventLogController.EventLogType>()));
            CreateLocalizationProvider();

            var relationshipController = CreateRelationshipController(mockEventLogController);
            var relationship = new Relationship
                                        {
                                            RelationshipId = Constants.SOCIAL_FollowerRelationshipID,
                                            Name = Constants.SOCIAL_RelationshipName
                                        };

            //Act
            relationshipController.SaveRelationship(relationship);

            //Assert
            var logContent = string.Format(Constants.LOCALIZATION_Relationship_Updated, Constants.SOCIAL_RelationshipName);
            mockEventLogController.Verify(e => e.AddLog("Message", logContent, EventLogController.EventLogType.ADMIN_ALERT));
        }
        public void RelationshipController_InitiateUserRelationship_Throws_On_Negative_RelationshipID()
        {
            //Arrange
            var relationshipController = CreateRelationshipController();
            var initiatingUser = new UserInfo { UserID = Constants.USER_TenId, PortalID = Constants.PORTAL_Zero };
            var targetUser = new UserInfo {UserID = Constants.USER_ElevenId, PortalID = Constants.PORTAL_Zero};
            var relationship = new Relationship();

            //Act, Assert
            relationshipController.InitiateUserRelationship(initiatingUser, targetUser, relationship);
        }     
        public void RelationshipController_DeleteRelationship_Calls_DataCache_RemoveCache()
        {
            //Arrange
            var portalId = 1;
            var relationshipController = CreateRelationshipController();
            var cacheKey = CachingProvider.GetCacheKey(string.Format(DataCache.RelationshipByPortalIDCacheKey, portalId));
            var relationship = new Relationship()
                                    {
                                        RelationshipId = Constants.SOCIAL_FollowerRelationshipID,
                                        PortalId = portalId,
                                        UserId = -1
                                    };

            //Act
            relationshipController.DeleteRelationship(relationship);

            //Assert
            mockCachingProvider.Verify(e => e.Remove(cacheKey));
        }
        public void RelationshipController_SaveRelationship_Calls_DataService()
        {
            //Arrange
            var mockDataService = CreateMockDataServiceWithRelationshipTypes();
            var relationshipController = CreateRelationshipController(mockDataService);
            var relationship = new Relationship
                                        {
                                            RelationshipId = Constants.SOCIAL_FollowerRelationshipID
                                        };

            //Act
            relationshipController.SaveRelationship(relationship);

            //Assert
            mockDataService.Verify(d => d.SaveRelationship(relationship, It.IsAny<int>()));
        }
 private void ClearRelationshipCache(Relationship relationship)
 {
     if (relationship.UserId == Null.NullInteger)
     {
         DataCache.RemoveCache(string.Format(DataCache.RelationshipByPortalIDCacheKey, relationship.PortalId));
     }
 }
        public void RelationshipController_DeleteRelationship_Calls_DataService()
        {
            //Arrange
            var mockDataService = new Mock<IDataService>();
            var relationshipController = CreateRelationshipController(mockDataService);
            var relationship = new Relationship()
                                        {
                                            RelationshipId = Constants.SOCIAL_FollowerRelationshipID
                                        };

            //Act
            relationshipController.DeleteRelationship(relationship);

            //Assert
            mockDataService.Verify(d => d.DeleteRelationship(Constants.SOCIAL_FollowerRelationshipID));
        }
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Initiate an UserRelationship Request
        /// </summary>
        /// <param name="initiatingUser">UserInfo of the user initiating the request</param>        
        /// <param name="targetUser">UserInfo of the user being solicited for initiating the request</param>        
        /// <param name="relationship">Relationship to associate this request to (Portal-Level Relationship or User-Level Relationship)</param>        
        /// <remarks>
        /// If all conditions are met UserRelationship object belonging to Initiating User is returned.
        /// </remarks>
        /// <returns>
        /// Relationship object belonging to the initiating user
        /// </returns>
        /// <exception cref="UserRelationshipBlockedException">Target user has Blocked any relationship request from Initiating user</exception>
        /// <exception cref="InvalidRelationshipTypeException">Relationship type does not exist</exception>
        /// -----------------------------------------------------------------------------
        public UserRelationship InitiateUserRelationship(UserInfo initiatingUser, UserInfo targetUser,
                                                         Relationship relationship)
        {
            Requires.NotNull("user1", initiatingUser);
            Requires.NotNull("user2", targetUser);
            Requires.NotNull("relationship", relationship);

            Requires.PropertyNotNegative("user1", "UserID", initiatingUser.UserID);
            Requires.PropertyNotNegative("user2", "UserID", targetUser.UserID);

            Requires.PropertyNotNegative("user1", "PortalID", initiatingUser.PortalID);
            Requires.PropertyNotNegative("user2", "PortalID", targetUser.PortalID);

            Requires.PropertyNotNegative("relationship", "RelationshipId", relationship.RelationshipId);

            //cannot be same user
            if (initiatingUser.UserID == targetUser.UserID)
            {
                throw new UserRelationshipForSameUsersException(
                    Localization.GetExceptionMessage("UserRelationshipForSameUsersError",
                                                     "Initiating and Target Users cannot have same UserID '{0}'.",
                                                     initiatingUser.UserID));
            }

            //users must be from same portal
            if (initiatingUser.PortalID != targetUser.PortalID)
            {
                throw new UserRelationshipForDifferentPortalException(
                    Localization.GetExceptionMessage("UserRelationshipForDifferentPortalError",
                                                     "Portal ID '{0}' of Initiating User is different from Portal ID '{1}' of Target  User.",
                                                     initiatingUser.PortalID, targetUser.PortalID));
            }

            //check for existing UserRelationship record
            UserRelationship existingRelationship = GetUserRelationship(initiatingUser, targetUser, relationship);
            if (existingRelationship != null)
            {
                throw new UserRelationshipExistsException(Localization.GetExceptionMessage(
                    "UserRelationshipExistsError",
                    "Relationship already exists for Initiating User '{0}' Target User '{1}' RelationshipID '{2}'.",
                    initiatingUser.UserID, targetUser.UserID, relationship.RelationshipId));
            }

            //no existing UserRelationship record found 


            //use Relationship DefaultResponse as status
            RelationshipStatus status = relationship.DefaultResponse;

            //check if there is a custom relationship status setting for the user. 
            //TODO - Is this check only applicable for portal or host list
            //if (relationship.IsPortalList || relationship.IsHostList)
            {
                UserRelationshipPreference preference = GetUserRelationshipPreference(targetUser.UserID,
                                                                                      relationship.RelationshipId);
                if (preference != null)
                {
                    status = preference.DefaultResponse;
                }
            }

            if (status == RelationshipStatus.None)
            {
                status = RelationshipStatus.Pending;
            }

            var userRelationship = new UserRelationship
                                       {
                                           UserRelationshipId = Null.NullInteger,
                                           UserId = initiatingUser.UserID,
                                           RelatedUserId = targetUser.UserID,
                                           RelationshipId = relationship.RelationshipId,
                                           Status = status
                                       };

            SaveUserRelationship(userRelationship);

            return userRelationship;
        }
        public void CreateDefaultRelationshipsForPortal(int portalId)
        {
            //create default Friend Relationship
            if (GetFriendsRelationshipByPortal(portalId) == null)
            {
                var friendRelationship = new Relationship
                                             {
                                                 RelationshipId = Null.NullInteger,
                                                 Name = DefaultRelationshipTypes.Friends.ToString(),
                                                 Description = DefaultRelationshipTypes.Friends.ToString(),
                                                 PortalId = portalId,
                                                 UserId = Null.NullInteger,
                                                 DefaultResponse = RelationshipStatus.None,
                                                 //default response is None
                                                 RelationshipTypeId = (int) DefaultRelationshipTypes.Friends
                                             };
                SaveRelationship(friendRelationship);
            }

            //create default Follower Relationship
            if (GetFollowersRelationshipByPortal(portalId) == null)
            {
                var followerRelationship = new Relationship
                                               {
                                                   RelationshipId = Null.NullInteger,
                                                   Name = DefaultRelationshipTypes.Followers.ToString(),
                                                   Description = DefaultRelationshipTypes.Followers.ToString(),
                                                   PortalId = portalId,
                                                   UserId = Null.NullInteger,
                                                   DefaultResponse = RelationshipStatus.Accepted,
                                                   //default response is Accepted
                                                   RelationshipTypeId = (int) DefaultRelationshipTypes.Followers
                                               };
                SaveRelationship(followerRelationship);
            }
        }
        public UserRelationship GetUserRelationship(UserInfo user, UserInfo relatedUser, Relationship relationship)
        {
            UserRelationship userRelationship = null;
            if (relationship != null)
            {
                userRelationship = CBO.FillObject<UserRelationship>(_dataService.GetUserRelationship(user.UserID, relatedUser.UserID,
                                                                                  relationship.RelationshipId,
                                                                                  GetRelationshipType(
                                                                                      relationship.RelationshipTypeId).
                                                                                      Direction));
            }
            return userRelationship;

        }
        public void SaveRelationship(Relationship relationship)
        {
            Requires.NotNull("relationship", relationship);

            string localizationKey = (relationship.RelationshipId == Null.NullInteger)
                                         ? "Relationship_Added"
                                         : "Relationship_Updated";

            relationship.RelationshipId = _dataService.SaveRelationship(relationship,
                                                                        UserController.GetCurrentUserInfo().UserID);

            //log event
            string logContent = string.Format(Localization.GetString(localizationKey, Localization.GlobalResourceFile),
                                              relationship.Name);
            AddLog(logContent);

            //clear cache
            ClearRelationshipCache(relationship);
        }
        public void DeleteRelationship(Relationship relationship)
        {
            Requires.NotNull("relationship", relationship);

            _dataService.DeleteRelationship(relationship.RelationshipId);

            //log event
            string logContent =
                string.Format(Localization.GetString("Relationship_Deleted", Localization.GlobalResourceFile),
                              relationship.Name, relationship.RelationshipId);
            AddLog(logContent);

            //clear cache
            ClearRelationshipCache(relationship);
        }
Exemple #13
0
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Initiate an UserRelationship Request
        /// </summary>
        /// <param name="initiatingUser">UserInfo of the user initiating the request</param>
        /// <param name="targetUser">UserInfo of the user being solicited for initiating the request</param>
        /// <param name="relationship">Relationship to associate this request to (Portal-Level Relationship or User-Level Relationship)</param>
        /// <remarks>
        /// If all conditions are met UserRelationship object belonging to Initiating User is returned.
        /// </remarks>
        /// <returns>
        /// Relationship object belonging to the initiating user
        /// </returns>
        /// <exception cref="UserRelationshipBlockedException">Target user has Blocked any relationship request from Initiating user</exception>
        /// <exception cref="InvalidRelationshipTypeException">Relationship type does not exist</exception>
        /// -----------------------------------------------------------------------------
        public UserRelationship InitiateUserRelationship(UserInfo initiatingUser, UserInfo targetUser,
                                                         Relationship relationship)
        {
            Requires.NotNull("user1", initiatingUser);
            Requires.NotNull("user2", targetUser);
            Requires.NotNull("relationship", relationship);

            Requires.PropertyNotNegative("user1", "UserID", initiatingUser.UserID);
            Requires.PropertyNotNegative("user2", "UserID", targetUser.UserID);

            Requires.PropertyNotNegative("user1", "PortalID", initiatingUser.PortalID);
            Requires.PropertyNotNegative("user2", "PortalID", targetUser.PortalID);

            Requires.PropertyNotNegative("relationship", "RelationshipId", relationship.RelationshipId);

            //cannot be same user
            if (initiatingUser.UserID == targetUser.UserID)
            {
                throw new UserRelationshipForSameUsersException(
                          Localization.GetExceptionMessage("UserRelationshipForSameUsersError",
                                                           "Initiating and Target Users cannot have same UserID '{0}'.",
                                                           initiatingUser.UserID));
            }

            //users must be from same portal
            if (initiatingUser.PortalID != targetUser.PortalID)
            {
                throw new UserRelationshipForDifferentPortalException(
                          Localization.GetExceptionMessage("UserRelationshipForDifferentPortalError",
                                                           "Portal ID '{0}' of Initiating User is different from Portal ID '{1}' of Target  User.",
                                                           initiatingUser.PortalID, targetUser.PortalID));
            }

            //check for existing UserRelationship record
            UserRelationship existingRelationship = GetUserRelationship(initiatingUser, targetUser, relationship);

            if (existingRelationship != null)
            {
                throw new UserRelationshipExistsException(Localization.GetExceptionMessage(
                                                              "UserRelationshipExistsError",
                                                              "Relationship already exists for Initiating User '{0}' Target User '{1}' RelationshipID '{2}'.",
                                                              initiatingUser.UserID, targetUser.UserID, relationship.RelationshipId));
            }

            //no existing UserRelationship record found


            //use Relationship DefaultResponse as status
            RelationshipStatus status = relationship.DefaultResponse;

            //check if there is a custom relationship status setting for the user.
            //TODO - Is this check only applicable for portal or host list
            //if (relationship.IsPortalList || relationship.IsHostList)
            {
                UserRelationshipPreference preference = GetUserRelationshipPreference(targetUser.UserID,
                                                                                      relationship.RelationshipId);
                if (preference != null)
                {
                    status = preference.DefaultResponse;
                }
            }

            if (status == RelationshipStatus.None)
            {
                status = RelationshipStatus.Pending;
            }

            var userRelationship = new UserRelationship
            {
                UserRelationshipId = Null.NullInteger,
                UserId             = initiatingUser.UserID,
                RelatedUserId      = targetUser.UserID,
                RelationshipId     = relationship.RelationshipId,
                Status             = status
            };

            SaveUserRelationship(userRelationship);

            return(userRelationship);
        }
Exemple #14
0
        public UserRelationship GetUserRelationship(UserInfo user, UserInfo relatedUser, Relationship relationship)
        {
            UserRelationship userRelationship = null;

            if (relationship != null)
            {
                userRelationship = CBO.FillObject <UserRelationship>(_dataService.GetUserRelationship(user.UserID, relatedUser.UserID,
                                                                                                      relationship.RelationshipId,
                                                                                                      GetRelationshipType(
                                                                                                          relationship.RelationshipTypeId).
                                                                                                      Direction));
            }
            return(userRelationship);
        }