/// <summary>
        /// Insert a new machine and/or machine group into the database
        /// </summary>
        /// <param name="applicationName">The name of the machine to insert</param>
        /// <param name="applicationGroupName">The name of the machine group</param>
        public void AddApplication(string applicationName, string applicationGroupName)
        {
            Check.ArgumentIsNullOrEmpty(applicationName, "applicationName");
            Check.ArgumentIsNullOrEmpty(applicationGroupName, "applicationGroupName");

            var application = Uow.Applications.GetApplicationByName(applicationName.Trim());
            if (application != null)
                throw new InvalidOperationException("Application already exists!");

            application = new Application(applicationName.Trim());
            Uow.Applications.Add(application);

            // Let's see if this group exists before adding a new one
            var group = Uow.ApplicationGroups.GetApplicationGroupByGroupName(applicationGroupName.Trim());
            if (group == null)
            {
                group = new ApplicationGroup(applicationGroupName.Trim());
                Uow.ApplicationGroups.Add(group);
            }

            var applicationGroupMap = new ApplicationGroupMap
            {
                iApplicationID = application.iApplicationID,
                iApplicationGroupID = group.iApplicationGroupID
            };
            Uow.ApplicationGroupMaps.Add(applicationGroupMap);

            Uow.Commit();
        }
        public void DeleteApplicationById_WithGroupThatHasMoreAssociations_DeletesGroupMapAndApplication()
        {
            // arrange
            const int id = 4;
            var group = new ApplicationGroup
            {
                iApplicationGroupID = 1,
                vchApplicationGroupName = "Some group"
            };

            var map = new ApplicationGroupMap
            {
                iApplicationGroupID = group.iApplicationGroupID,
                iApplicationID = id
            };

            MockRepoApplicationGroupMap.Setup(x => x.GetApplicationGroupMapsByGroupId(group.iApplicationGroupID)).Returns(new List<ApplicationGroupMap> {new ApplicationGroupMap()});
            MockRepoApplicationGroup.Setup(x => x.GetApplicationGroupByApplicationId(id)).Returns(group);
            MockRepoApplicationGroupMap.Setup(x => x.GetApplicationGroupMapByApplicationIdAndGroupId(id, group.iApplicationGroupID)).Returns(map);

            // act
            ApplicationService.DeleteApplicationById(id);

            // assert
            MockRepoApplicationGroup.Verify(x => x.Delete(group.iApplicationGroupID), Times.Never);
            MockRepoApplicationGroupMap.Verify(x => x.Delete(map));
            MockRepoApplication.Verify(x => x.Delete(id));
            MockUow.Verify(x => x.Commit());
        }
        public void UpdateApplication_WithPreexistingGroup_UpdatesApplicationAndAddsGroupMap()
        {
            // arrange
            const string oldName = "old application name";
            const string newName = "new application name";
            const string groupName = "some group";

            var application = new Application
            {
                iApplicationID = 1,
                vchAppName = oldName
            };

            var group = new ApplicationGroup
            {
                iApplicationGroupID = 1,
                vchApplicationGroupName = "group name"
            };

            var groupMap = new ApplicationGroupMap
            {
                iApplicationGroupID = group.iApplicationGroupID,
                iApplicationID = application.iApplicationID
            };

            var preexistingGroup = new ApplicationGroup
            {
                iApplicationGroupID = 2,
                vchApplicationGroupName = groupName
            };

            MockRepoApplication.Setup(x => x.GetApplicationByName(newName)).Returns((Application)null);
            MockRepoApplication.Setup(x => x.GetApplicationByName(oldName)).Returns(application);
            MockRepoApplicationGroup.Setup(x => x.GetApplicationGroupByApplicationName(oldName)).Returns(group);
            MockRepoApplicationGroupMap.Setup(x => x.GetApplicationGroupMapByApplicationIdAndGroupId(application.iApplicationID, group.iApplicationGroupID)).Returns(groupMap);
            MockRepoApplicationGroup.Setup(x => x.GetApplicationGroupByGroupName(groupName)).Returns(preexistingGroup);

            // act
            ApplicationService.UpdateApplication(oldName, newName, groupName);

            // assert
            Assert.AreEqual(newName, application.vchAppName);
            MockRepoApplication.Verify(x => x.Update(application));
            MockRepoApplicationGroupMap.Verify(x => x.Delete(groupMap));
            MockRepoApplicationGroup.Verify(x => x.Add(It.IsAny<ApplicationGroup>()), Times.Never);
            MockRepoApplicationGroupMap.Verify(x => x.Add(It.Is<ApplicationGroupMap>(map => map.iApplicationGroupID == 2 && map.iApplicationID == 1)));
            MockUow.Verify(x => x.Commit());
        }
        public void Init()
        {
            // Only keep all database calls within this transaction scope
            _scope = new TransactionScope();

            // arrange test data
            _application = new Application
            {
                vchAppName = "TESTAPP"
            };

            _group = new ApplicationGroup
            {
                vchApplicationGroupName = "TESTGROUP"
            };

            _uow.ApplicationGroups.Add(_group);
            _uow.Applications.Add(_application);
            _uow.Commit();

            _map = new ApplicationGroupMap
            {
                iApplicationID = _application.iApplicationID,
                iApplicationGroupID = _group.iApplicationGroupID
            };
            _uow.ApplicationGroupMaps.Add(_map);
            _uow.Commit();
        }
        public void UpdateApplication(string oldApplicationName, string newApplicationName, string applicationGroupName)
        {
            Check.ArgumentIsNullOrEmpty(oldApplicationName, "oldApplicationName");
            Check.ArgumentIsNullOrEmpty(newApplicationName, "newApplicationName");
            Check.ArgumentIsNullOrEmpty(applicationGroupName, "applicationGroupName");

            applicationGroupName = applicationGroupName.Trim();
            newApplicationName = newApplicationName.Trim();

            // If the new application name already exists, throw an exception
            if (oldApplicationName != newApplicationName)
            {
                var newApplication = Uow.Applications.GetApplicationByName(newApplicationName);
                if (newApplication != null)
                    throw new InvalidOperationException("Application already exists!");
            }

            // Get the application to update
            var application = Uow.Applications.GetApplicationByName(oldApplicationName);
            application.vchAppName = newApplicationName;
            Uow.Applications.Update(application);

            // Get the current group for the selected application
            var currentGroup = Uow.ApplicationGroups.GetApplicationGroupByApplicationName(oldApplicationName);
            if (currentGroup.vchApplicationGroupName != applicationGroupName)
            {
                // Application groups are different, delete the map and make a new map
                Uow.ApplicationGroupMaps.Delete(Uow.ApplicationGroupMaps.GetApplicationGroupMapByApplicationIdAndGroupId(application.iApplicationID, currentGroup.iApplicationGroupID));

                var group = Uow.ApplicationGroups.GetApplicationGroupByGroupName(applicationGroupName);
                if (group == null)
                {
                    group = new ApplicationGroup(applicationGroupName);
                    Uow.ApplicationGroups.Add(group);
                }

                var applicationGroupMap = new ApplicationGroupMap
                {
                    iApplicationID = application.iApplicationID,
                    iApplicationGroupID = group.iApplicationGroupID
                };

                Uow.ApplicationGroupMaps.Add(applicationGroupMap);
            }

            Uow.Commit();
        }