public void Test_UserOwner_AlreadyOwned()
        {
            var user = new User("username", "emailAddress");
            var item = new Item("Title", "Creator", "Edition");
            var expectedOwnership = new Ownership<User>(item, user);
            SaveEntities(user, item, expectedOwnership);

            CommitTransactionAndOpenNew();

            var request = new AddUserItemRequest()
            {
                OwnerId = user.Id,
                Creator = item.Creator,
                Edition = item.Edition,
                Title = item.Title
            };

            var expectedResponse = new BaseResponse(AddItemRequestHandler<User>.OwnershipAlreadyExists);

            var sut = new AddItemRequestHandler<User>(() => Session);
            BaseResponse actualResponse = sut.HandleRequest(request);

            actualResponse.ShouldEqual(expectedResponse);

            CommitTransactionAndOpenNew();

            Ownership<User> ownershipInDb = Session
                .QueryOver<Ownership>()
                .JoinQueryOver<Item>(x => x.Item)
                .Where(x => x.Id == item.Id)
                .SingleOrDefault<Ownership<User>>()
                ;

            ownershipInDb.ShouldEqual(expectedOwnership);
        }
        public void Test_Success()
        {
            //Arrange
            var fromUser = new User("from", "fromEmail");
            var toUser = new User("to", "toEmail");

            SaveEntities(fromUser, toUser);

            CommitTransactionAndOpenNew();

            var expectedConnection = new Connection(fromUser, toUser);
            var expectedResponse = new BaseResponse();
            var request = new ConnectRequest() {FromUserId = fromUser.Id, ToUserId = toUser.Id};

            //Act

            var sut = new ConnectRequestHandler(() => Session);
            BaseResponse actualResponse = sut.HandleRequest(request);

            //Assert

            actualResponse.ShouldEqual(expectedResponse);

            //Check that the connection was saved in the DB
            CommitTransactionAndOpenNew();

            Connection actualConnection = Session
                .QueryOver<Connection>()
                .SingleOrDefault()
                ;

            actualConnection.ShouldEqual(expectedConnection);
        }
        public void Test_AlreadyConnected()
        {
            //Arrange
            var fromUser = new User("from", "fromEmail");
            var toUser = new User("to", "toEmail");
            var existingConnection = new Connection(fromUser, toUser);

            SaveEntities(fromUser, toUser, existingConnection);

            CommitTransactionAndOpenNew();

            var expectedResponse = new BaseResponse(ConnectRequestHandler.AlreadyConnected);
            var request = new ConnectRequest() { FromUserId = fromUser.Id, ToUserId = toUser.Id };

            //Act

            var sut = new ConnectRequestHandler(() => Session);
            BaseResponse actualResponse = sut.HandleRequest(request);

            //Assert

            actualResponse.ShouldEqual(expectedResponse);

            //Check that the connection wasn't saved in the DB
            int numberOfConnections = Session
                .QueryOver<Connection>()
                .RowCount()
                ;

            Assert.That(numberOfConnections, Is.EqualTo(1));
        }
        public void Test_Failed_UsernameTaken()
        {
            //Arrange
            var request = new AddUserRequest() { EmailAddress = "*****@*****.**", UserName = "******"};
            var expectedResponse = new BaseResponse() { Success = false, FailureDescription = AddUserRequestHandler.UsernameTaken };

            var existingUser = new User(request.UserName, "new email address");
            Session.Save(existingUser);

            CommitTransactionAndOpenNew();

            //Act
            var sut = new AddUserRequestHandler(() => Session);
            BaseResponse actualResponseBase = sut.HandleRequest(request);

            //Assert
            actualResponseBase.ShouldEqual(expectedResponse);
        }
        public void Test_Success()
        {
            var requestor = DefaultTestData.ServiceStackUser1;
            var owner = DefaultTestData.ServiceStackUser2;
            var item = new Item("title", "creator", "edition");
            var ownership = new Ownership<User>(item, owner);

            SaveEntities(requestor, owner, item, ownership);

            CommitTransactionAndOpenNew();

            var request = new BorrowItemRequest(){OwnershipId = ownership.Id, RequestorId = requestor.Id};
            var expectedResponse = new BaseResponse();

            var expectedBorrowing = new Borrowing(requestor, ownership);

            var sut = new BorrowItemRequestHandler(() => Session);
            BaseResponse actualResponse = sut.HandleRequest(request);

            actualResponse.ShouldEqual(expectedResponse);

            //Check that the right Borrowing object is in the DB
            Borrowing borrowingAlias = null;
            User requestorAllias = null;
            Ownership ownershipAlias = null;

            Borrowing borrowingInDb = Session
                .QueryOver<Borrowing>(() => borrowingAlias)
                .JoinAlias(() => borrowingAlias.Borrower, () => requestorAllias)
                .JoinAlias(() => borrowingAlias.Ownership, () => ownershipAlias)
                .Where(() => requestorAllias.Id == requestor.Id)
                .And(() => ownershipAlias.Id == ownership.Id)
                .SingleOrDefault()
                ;

            borrowingInDb.ShouldEqual(expectedBorrowing);
        }
        public void Test_OrgOwner_ItemExists_Success()
        {
            var user = DefaultTestData.ServiceStackUser1;
            var organisation = new Organisation("organisation");
            var item = new Item("Title", "Creator", "Edition");
            SaveEntities(user, organisation, item);

            CommitTransactionAndOpenNew();

            var request = new AddOrganisationItemRequest()
            {
                OwnerId = organisation.Id,
                Creator = item.Creator,
                Edition = item.Edition,
                Title = item.Title
            };

            var expectedResponse = new BaseResponse();

            var expectedOwnership = new Ownership<Organisation>(item, organisation);

            var sut = new AddItemRequestHandler<Organisation>(() => Session);
            BaseResponse actualResponse = sut.HandleRequest(request, user.Id);

            actualResponse.ShouldEqual(expectedResponse);

            CommitTransactionAndOpenNew();

            Ownership<Organisation> ownershipInDb = Session
                .QueryOver<Ownership>()
                .JoinQueryOver<Item>(x => x.Item)
                .Where(x => x.Id == item.Id)
                .SingleOrDefault<Ownership<Organisation>>()
                ;

            ownershipInDb.ShouldEqual(expectedOwnership);
        }
        public void Test_Success()
        {
            //Arrange
            var request = new AddUserRequest() {EmailAddress = "*****@*****.**", UserName = "******"};
            var expectedResponse = new BaseResponse() {Success = true};

            //Act
            var sut = new AddUserRequestHandler(() => Session);
            BaseResponse actualResponseBase = sut.HandleRequest(request);

            //Assert
            actualResponseBase.ShouldEqual(expectedResponse);

            var expectedUser = new User(request.UserName, request.EmailAddress);

            CommitTransactionAndOpenNew();

            User userInDb = Session
                .QueryOver<User>()
                .SingleOrDefault()
                ;

            userInDb.ShouldEqual(expectedUser);
        }
        public static bool ShouldEqual(this BaseResponse actual, BaseResponse expected)
        {
            Assert.That(actual.Success, Is.EqualTo(expected.Success));
            Assert.That(actual.FailureDescription, Is.EqualTo(expected.FailureDescription));

            return true;
        }
        public void Test_UserOwner_NewItem_Success()
        {
            var user = DefaultTestData.ServiceStackUser1;
            var expectedItem = new Item("Title", "Creator", "Edition");
            SaveEntities(user);

            CommitTransactionAndOpenNew();

            var request = new AddUserItemRequest()
            {
                OwnerId = user.Id,
                Creator = expectedItem.Creator,
                Edition = expectedItem.Edition,
                Title = expectedItem.Title
            };

            var expectedResponse = new BaseResponse();

            var expectedOwnership = new Ownership<User>(expectedItem, user);

            var sut = new AddItemRequestHandler<User>(() => Session);
            BaseResponse actualResponse = sut.HandleRequest(request, user.Id);

            actualResponse.ShouldEqual(expectedResponse);

            CommitTransactionAndOpenNew();

            Item itemInDb = Session
                .QueryOver<Item>()
                .Where(x => x.Title == expectedItem.Title)
                .Where(x => x.Creator == expectedItem.Creator)
                .Where(x => x.Edition == expectedItem.Edition)
                .SingleOrDefault()
                ;

            itemInDb.ShouldEqual(expectedItem);

            Ownership<User> ownershipInDb = Session
                .QueryOver<Ownership>()
                .JoinQueryOver<Item>(x => x.Item)
                .Where(x => x.Id == itemInDb.Id)
                .SingleOrDefault<Ownership<User>>()
                ;

            ownershipInDb.ShouldEqual(expectedOwnership);
        }
        public void Test_SuccessWithExistingOtherConnection()
        {
            //Arrange
            var fromUser = new User("from", "fromEmail");
            var toUser = new User("to", "toEmail");
            var otherUser = new User("other", "otherEmail");
            var existingConnection = new Connection(otherUser, toUser);

            SaveEntities(fromUser, toUser, otherUser, existingConnection);

            CommitTransactionAndOpenNew();

            var expectedConnection = new Connection(fromUser, toUser);
            var request = new ConnectRequest() { FromUserId = fromUser.Id, ToUserId = toUser.Id };
            var expectedResponse = new BaseResponse();

            //Act

            var sut = new ConnectRequestHandler(() => Session);
            BaseResponse actualResponse = sut.HandleRequest(request);

            //Assert

            actualResponse.ShouldEqual(expectedResponse);

            //Check that the connection was saved in the DB
            CommitTransactionAndOpenNew();

            Connection connectionAlias = null;
            User user1Alias = null;
            User user2Alias = null;

            Connection actualConnection = Session
                .QueryOver<Connection>(() => connectionAlias)
                .JoinAlias(() => connectionAlias.User1, () => user1Alias)
                .JoinAlias(() => connectionAlias.User2, () => user2Alias)
                .Where(() => (user1Alias.Id == request.FromUserId && user2Alias.Id == request.ToUserId) ||
                             (user1Alias.Id == request.ToUserId && user2Alias.Id == request.FromUserId))
                .SingleOrDefault()
                ;

            actualConnection.ShouldEqual(expectedConnection);
        }