/// <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 AddApplication_WithApplicationNameInUse_ThrowsException()
        {
            const string name = "app name";
            var application = new Application
            {
                vchAppName = name
            };

            MockRepoApplication.Setup(x => x.GetApplicationByName(name)).Returns(application);

            try
            {
                ApplicationService.AddApplication(name, "group");
                Assert.Fail("Invalid Operation Exception should have been thrown!");
            }
            catch (Exception e)
            {
                Assert.IsInstanceOfType(e, typeof(InvalidOperationException));
            }
        }
        public void UpdateApplication_WithSameGroup_UpdatesApplication()
        {
            // arrange
            const string oldName = "old application name";
            const string newName = "new application name";
            const string groupName = "some group";

            var application = new Application
            {
                vchAppName = oldName
            };

            var group = new ApplicationGroup
            {
                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);

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

            // assert
            Assert.AreEqual(newName, application.vchAppName);
            MockRepoApplication.Verify(x => x.Update(application));
            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 UpdateApplication_WithApplicationNameInUse_ThrowsException()
        {
            // arrange
            const string name = "new application name";

            var application = new Application
            {
                vchAppName = name
            };

            MockRepoApplication.Setup(x => x.GetApplicationByName(name)).Returns(application);

            try
            {
                // act
                ApplicationService.UpdateApplication("application name", name, "some group");
                Assert.Fail("Invalid Operation Exception should have been thrown!");
            }
            catch (Exception e)
            {
                // assert
                Assert.IsInstanceOfType(e, typeof(InvalidOperationException));
                MockRepoApplication.Verify(x => x.GetApplicationByName(name));
            }
        }
        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();
        }