public void Correctly_creates_new_application_instance_if_existing_instance_does_not_exist()
		{
			ApplicationInstance instance = new ApplicationInstance();

			MockRepository mocks = new MockRepository();
			ISystemEnvironment environment = mocks.CreateMock<ISystemEnvironment>();
			IConfigurationReader configurationReader = mocks.CreateMock<IConfigurationReader>();
			IApplicationInstanceRepository repository = mocks.CreateMock<IApplicationInstanceRepository>();
			IApplicationInstanceFactory factory = mocks.CreateMock<IApplicationInstanceFactory>();

			using (mocks.Record())
			{
				Expect.Call(environment.GetMachineName()).Return("MyMachine");
				Expect.Call(configurationReader.GetRequiredSetting("TarantinoWebManagementHttpHost")).Return("www.myapp.com");
				Expect.Call(repository.GetByMaintenanceHostHeaderAndMachineName("www.myapp.com", "MyMachine")).Return(null);
				Expect.Call(factory.Create()).Return(instance);
				repository.Save(instance);
			}

			using (mocks.Playback())
			{
				ICurrentApplicationInstanceRetriever retriever = new CurrentApplicationInstanceRetriever(environment, configurationReader, repository, factory);
				Assert.That(retriever.GetApplicationInstance(), Is.SameAs(instance));
			}

			mocks.VerifyAll();
		}
		public void Set(string key, ApplicationInstance item)
		{
			DateTime expiration = _clock.GetCurrentDateTime().AddMinutes(1);

			_context.SetItem(key, item);
			_context.SetCacheItem(key, item, expiration, TimeSpan.Zero);
		}
		public void Should_enable_load_balancing_when_requested()
		{
			ApplicationInstance instance = new ApplicationInstance();
			instance.AvailableForLoadBalancing = false;

			MockRepository mocks = new MockRepository();
			IApplicationInstanceContext instanceContext = mocks.CreateMock<IApplicationInstanceContext>();
			IWebContext context = mocks.CreateMock<IWebContext>();
			ISecureAvailabilityStatusUpdater updater = mocks.CreateMock<ISecureAvailabilityStatusUpdater>();			

			using (mocks.Record())
			{
				Expect.Call(instanceContext.GetCurrent()).Return(instance);
				Expect.Call(context.GetRequestItem(LoadBalanceStatusManager.ENABLED_PARAM)).Return("True");
				Expect.Call(updater.SetStatus(true)).Return("My error message");
			}

			using (mocks.Playback())
			{
				ILoadBalanceStatusManager manager = new LoadBalanceStatusManager(instanceContext, context, updater);
				string errorMessage = manager.HandleLoadBalanceRequest();

				Assert.That(errorMessage, Is.EqualTo("My error message"));
			}
		}
Example #4
0
		string ListApplications(ApplicationInstance applicationInstance)
		{
			System.Text.StringBuilder output = new System.Text.StringBuilder();
			WriteCSS();
			if (_authenticated)
			{
				output.AppendFormat("<form method=post action=''>");
			}
			output.Append("<table >\n");
			output.AppendFormat("<tr><td><a href='Tarantino.WebManagement.Application.axd'>Back</a></td><td></td></tr>");
			output.AppendFormat("<tr><td>Machine Name</td><td>{0}</td></tr>", applicationInstance.MachineName);
			output.AppendFormat("<tr><td>Version</td><td>{0}</td></tr>", applicationInstance.Version);
			output.AppendFormat("<tr><td>Unique Hostname</td><td><input type=\"text\" name=\"hostname\" value=\"{0}\"></td></tr>", applicationInstance.UniqueHostHeader);
			output.AppendFormat("<tr><td>Shared Hostname</td><td>{0}</td></tr>", applicationInstance.MaintenanceHostHeader);
			output.AppendFormat("<tr><td>Load balanaced</td><td>{0}</td></tr>", applicationInstance.AvailableForLoadBalancing ? "Online" : "Offline");
			output.AppendFormat("<tr><td>Maintenance Mode</td><td>{0}</td></tr>", applicationInstance.DownForMaintenance ? "Down" : "Online");
			output.Append("</table>\n");

			if (_authenticated)
			{
				output.AppendFormat("<input type=submit value='Submit' />");
				output.AppendFormat("</form>");
			}

			return output.ToString();
		}
		public ApplicationInstance Create()
		{
			string hostHeader = _configurationReader.GetRequiredSetting("TarantinoWebManagementHttpHost");

			ApplicationInstance instance = new ApplicationInstance();
			instance.MachineName = _systemEnvironment.GetMachineName();
			instance.AvailableForLoadBalancing = true;
			instance.Version = _assemblyContext.GetAssemblyVersion();
			instance.MaintenanceHostHeader = hostHeader;
			instance.ApplicationDomain = hostHeader;

			return instance;
		}
 public string BuildMRowHtml(ApplicationInstance applicationInstance)
 {
     string template = fileLocator.ReadTextFile("Tarantino.Core", ApplicationListingBodyView.RowNFragment);
     tokenReplacer.Text = template;
     tokenReplacer.Replace("INSTANCE_ID", applicationInstance.Id.ToString());
     tokenReplacer.Replace("MAINTANANCE_HOST_HEADER", applicationInstance.MaintenanceHostHeader);
     tokenReplacer.Replace("MAINTANANCE_STATUS", applicationInstance.DownForMaintenance ? "Offline" : "Online");
     tokenReplacer.Replace("MACHINE_NAME", applicationInstance.MachineName);
     tokenReplacer.Replace("LOAD_BALANCE_STATUS", applicationInstance.AvailableForLoadBalancing ? "Online" : "Offline");
     tokenReplacer.Replace("NEW_LOAD_BALANCE_STATUS", (!applicationInstance.AvailableForLoadBalancing).ToString());
     tokenReplacer.Replace("VERSION", applicationInstance.Version);
     tokenReplacer.Replace("HOST_HEADER", applicationInstance.UniqueHostHeader);
     return tokenReplacer.Text;
 }
		public void Can_persist_application_instance()
		{
			var instance = new ApplicationInstance
			               	{
			               		AvailableForLoadBalancing = true,
			               		ApplicationDomain = "Domain...",
			               		CacheRefreshQueryString = "QueryString",
			               		DownForMaintenance = true,
			               		MachineName = "My Machine",
			               		MaintenanceHostHeader = "HostHeader",
			               		UniqueHostHeader = "Unique Host Header",
			               		Version = "Version"
			               	};

			AssertObjectCanBePersisted(instance);
		}
		public void Correctly_retrieves_application_instance_from_first_level_cache()
		{
			ApplicationInstance instance = new ApplicationInstance();

			MockRepository mocks = new MockRepository();
			IWebContext webContext = mocks.CreateMock<IWebContext>();

			using (mocks.Record())
			{
				Expect.Call(webContext.GetItem<ApplicationInstance>(ApplicationInstance.CacheKey)).Return(instance);
			}

			using (mocks.Playback())
			{
				IApplicationInstanceCache cache = new ApplicationInstanceCache(webContext, null);
				Assert.That(cache.GetCurrent(), Is.SameAs(instance));
			}
		}
		public void Can_correctly_save_application_instance()
		{
			var instance = new ApplicationInstance();

			var mocks = new MockRepository();
			var objectRepository = mocks.CreateMock<IPersistentObjectRepository>();

			using (mocks.Record())
			{
				objectRepository.ConfigurationFile = "webmanagement.hibernate.cfg.xml";
				objectRepository.Save(instance);
			}

			using (mocks.Playback())
			{
				IApplicationInstanceRepository repository = new ApplicationInstanceRepository(objectRepository);
				repository.Save(instance);
			}
		}
		public void Retrieves_application_instance_from_cache()
		{
			ApplicationInstance instance = new ApplicationInstance();

			MockRepository mocks = new MockRepository();
			IApplicationInstanceCache cache = mocks.CreateMock<IApplicationInstanceCache>();

			using (mocks.Record())
			{
				Expect.Call(cache.GetCurrent()).Return(instance);
			}

			using (mocks.Playback())
			{
				IApplicationInstanceContext instanceContext = new ApplicationInstanceContext(cache, null);
				Assert.That(instanceContext.GetCurrent(), Is.SameAs(instance));
			}

			mocks.VerifyAll();
		}
		public void Correctly_caches_application_instance()
		{
			ApplicationInstance instance = new ApplicationInstance();

			MockRepository mocks = new MockRepository();
			IWebContext context = mocks.CreateMock<IWebContext>();
			ISystemClock clock = mocks.CreateMock<ISystemClock>();

			using (mocks.Record())
			{
				Expect.Call(clock.GetCurrentDateTime()).Return(new DateTime(2007, 4, 15));
				context.SetItem(ApplicationInstance.CacheKey, instance);
				context.SetCacheItem(ApplicationInstance.CacheKey, instance, new DateTime(2007, 4, 15).AddMinutes(1), TimeSpan.Zero);
			}

			using (mocks.Playback())
			{
				IApplicationInstanceCache cache = new ApplicationInstanceCache(context, clock);
				cache.Set(ApplicationInstance.CacheKey, instance);
			}
		}
		public void Correctly_returns_html_for_error_non_authenticated_page_with_enabled_instance()
		{
			string errorMessages = "An error has occured";
		  
			string bodyTemplate = "body template";

			ApplicationInstance instance = new ApplicationInstance();
			instance.MachineName = "MyMachine";
			instance.AvailableForLoadBalancing = true;

			MockRepository mocks = new MockRepository();
			IApplicationInstanceContext context = mocks.CreateMock<IApplicationInstanceContext>();
			IResourceFileLocator fileLocator = mocks.CreateMock<IResourceFileLocator>();
			ITokenReplacer replacer = mocks.CreateMock<ITokenReplacer>();
			IAdministratorSecurityChecker securityChecker = mocks.CreateMock<IAdministratorSecurityChecker>();

			using (mocks.Record())
			{
				Expect.Call(securityChecker.IsCurrentUserAdministrator()).Return(false);

				Expect.Call(context.GetCurrent()).Return(instance);
				Expect.Call(fileLocator.ReadTextFile("Tarantino.Core", LoadBalancerBodyView.LoadBalancerBodyTemplate)).Return(bodyTemplate);
				replacer.Text = bodyTemplate;
				replacer.Replace("ERROR_MESSAGE", errorMessages);
				replacer.Replace("CURRENT_STATE", "enabled");
				replacer.Replace("MACHINE", "MyMachine");
				replacer.Replace("CHANGE_STATE_LINK", string.Empty);

				Expect.Call(replacer.Text).Return("formatted HTML");
			}

			using (mocks.Playback())
			{
				ILoadBalancerBodyView view = new LoadBalancerBodyView(context, fileLocator, replacer, securityChecker);
				string html = view.BuildHtml(errorMessages);

				Assert.That(html, Is.EqualTo("formatted HTML"));
			}
		}
		public void Can_correctly_gets_single_application_instance()
		{
			var id = Guid.NewGuid();
			var instance = new ApplicationInstance();

			var mocks = new MockRepository();
			var objectRepository = mocks.CreateMock<IPersistentObjectRepository>();

			using (mocks.Record())
			{
				objectRepository.ConfigurationFile = "webmanagement.hibernate.cfg.xml";
				Expect.Call(objectRepository.GetById<ApplicationInstance>(id)).Return(instance);
			}

			using (mocks.Playback())
			{
				IApplicationInstanceRepository repository = new ApplicationInstanceRepository(objectRepository);
				var actualInstance = repository.GetById(id);

				Assert.That(actualInstance, Is.SameAs(instance));
			}
		}
        public string BuildHtml()
        {
            
            if (checker.IsCurrentUserAdministrator())
            {
                IList<ApplicationInstance> applications =
                    new List<ApplicationInstance>(applicationInstanceRepository.GetAll());

                string bodyTemplate =
                    fileLocator.ReadTextFile("Tarantino.Core", ApplicationListingBodyView.BodyTemplate);
             
                replacer.Text = bodyTemplate;

                ApplicationInstance lastInstance = new ApplicationInstance();
                string rowHTMl = "";
                foreach(ApplicationInstance instance in applications)
                {
                    if(instance.MaintenanceHostHeader!=lastInstance.MaintenanceHostHeader)
                    {
                        int instanceCount= GetInstanceCount(applications,instance.MaintenanceHostHeader);
                        rowHTMl += applicationListingRowView.BuildFirstRowHtml(instance,instanceCount);
                    }
                    else
                    {
                        rowHTMl += applicationListingRowView.BuildMRowHtml(instance);
                    }
                    lastInstance = instance;
                }
                replacer.Replace("ROWS", rowHTMl);
            }
            else
            {
                replacer.Text = "Unauthorized request.";
            }
            return replacer.Text;


        }
        public void View_should_build_html_for_row_one()
        {
            ApplicationInstance instance = new ApplicationInstance();
            instance.MaintenanceHostHeader = "1";
            instance.MachineName = "2";
            instance.Id = Guid.NewGuid();

            MockRepository mocks = new MockRepository();
            IResourceFileLocator fileLocator = mocks.CreateMock<IResourceFileLocator>();
            ITokenReplacer tokenReplacer = mocks.CreateMock<ITokenReplacer>();

            using (mocks.Record())
            {
                Expect.Call(fileLocator.ReadTextFile("Tarantino.Core", ApplicationListingBodyView.Row1Fragment)).Return(
                    "template");
                tokenReplacer.Text = "template";
                tokenReplacer.Replace("INSTANCE_COUNT", "2");
                tokenReplacer.Replace("INSTANCE_ID", instance.Id.ToString());
                tokenReplacer.Replace("MAINTANANCE_HOST_HEADER", instance.MaintenanceHostHeader);
                tokenReplacer.Replace("MAINTANANCE_STATUS", instance.DownForMaintenance ? "Offline" : "Online");
                tokenReplacer.Replace("MACHINE_NAME", instance.MachineName);
                tokenReplacer.Replace("LOAD_BALANCE_STATUS", instance.AvailableForLoadBalancing ? "Online" : "Offline");
                tokenReplacer.Replace("NEW_LOAD_BALANCE_STATUS", (!instance.AvailableForLoadBalancing).ToString());
                tokenReplacer.Replace("VERSION", instance.Version);
                tokenReplacer.Replace("HOST_HEADER", instance.UniqueHostHeader);
                Expect.Call(tokenReplacer.Text).Return("html");
            }

            using (mocks.Playback())
            {
                ApplicationListingRowView view = new ApplicationListingRowView(fileLocator, tokenReplacer);

                string html = view.BuildFirstRowHtml(instance, 2);

                Assert.That(html, Is.EqualTo("html"));
            }
            mocks.VerifyAll();
        }
		public void Return_normal_status_200_message_if_instance_should_be_online_and_status_not_being_changed()
		{
			ApplicationInstance instance = new ApplicationInstance();
			instance.AvailableForLoadBalancing = true;

			MockRepository mocks = new MockRepository();
			IApplicationInstanceContext instanceContext = mocks.CreateMock<IApplicationInstanceContext>();
			IWebContext context = mocks.CreateMock<IWebContext>();

			using (mocks.Record())
			{
				Expect.Call(instanceContext.GetCurrent()).Return(instance);
				Expect.Call(context.GetRequestItem(LoadBalanceStatusManager.ENABLED_PARAM)).Return(null);
			}

			using (mocks.Playback())
			{
				ILoadBalanceStatusManager manager = new LoadBalanceStatusManager(instanceContext, context, null);
				string errorMessage = manager.HandleLoadBalanceRequest();

				Assert.That(errorMessage, Is.Empty);
			}
		}
		private void performTest(bool isRedirectableExtension, bool isExternalUrl, bool isDownForMaintenance, bool shouldBeRedirected)
		{
			ApplicationInstance instance = new ApplicationInstance();
			instance.DownForMaintenance = isDownForMaintenance;

			MockRepository mocks = new MockRepository();
			IFileExtensionChecker extensionChecker = mocks.CreateMock<IFileExtensionChecker>();
			IExternalUrlChecker urlChecker = mocks.CreateMock<IExternalUrlChecker>();
			IApplicationInstanceContext context = mocks.CreateMock<IApplicationInstanceContext>();

			using (mocks.Record())
			{
				Expect.Call(extensionChecker.CurrentUrlCanBeRedirected()).Return(isRedirectableExtension);
				Expect.Call(urlChecker.CurrentUrlIsExternal()).Return(isExternalUrl);
				Expect.Call(context.GetCurrent()).Return(instance);
			}

			using (mocks.Playback())
			{
				IMaintenanceRedirectionChecker checker = new MaintenanceRedirectionChecker(extensionChecker, urlChecker, context);
				Assert.That(checker.ShouldBeRedirectedToMaintenancePage(), Is.EqualTo(shouldBeRedirected));
			}
		}
		public void Can_correctly_gets_all_application_instances()
		{
			var instance1 = new ApplicationInstance();
			var instance2 = new ApplicationInstance();
			var instances = new[] { instance1, instance2 };

			var mocks = new MockRepository();
			var objectRepository = mocks.CreateMock<IPersistentObjectRepository>();

			using (mocks.Record())
			{
				objectRepository.ConfigurationFile = "webmanagement.hibernate.cfg.xml";
				Expect.Call(objectRepository.GetAll<ApplicationInstance>()).Return(instances);
			}

			using (mocks.Playback())
			{
				IApplicationInstanceRepository repository = new ApplicationInstanceRepository(objectRepository);
				IEnumerable<ApplicationInstance> actualInstances = repository.GetAll();
				
				EnumerableAssert.That(actualInstances, Is.EqualTo(instances));
			}
		}
		public void Retrieves_application_instance_when_not_found_in_cache()
		{
			ApplicationInstance instance = new ApplicationInstance();

			MockRepository mocks = new MockRepository();
			IApplicationInstanceCache cache = mocks.CreateMock<IApplicationInstanceCache>();
			ICurrentApplicationInstanceRetriever retriever = mocks.CreateMock<ICurrentApplicationInstanceRetriever>();

			using (mocks.Record())
			{
				Expect.Call(cache.GetCurrent()).Return(null);
				Expect.Call(retriever.GetApplicationInstance()).Return(instance);
				cache.Set(ApplicationInstance.CacheKey, instance);
			}

			using (mocks.Playback())
			{
				IApplicationInstanceContext instanceContext = new ApplicationInstanceContext(cache, retriever);
				Assert.That(instanceContext.GetCurrent(), Is.SameAs(instance));
			}

			mocks.VerifyAll();
		}
		public void Correctly_disables_application_instance()
		{
			ApplicationInstance instance = new ApplicationInstance();

			MockRepository mocks = new MockRepository();
			IApplicationInstanceRepository repository = mocks.CreateMock<IApplicationInstanceRepository>();
			IApplicationInstanceContext context = mocks.CreateMock<IApplicationInstanceContext>();

			using (mocks.Record())
			{
				Expect.Call(context.GetCurrent()).Return(instance);
				repository.Save(instance);
			}

			using (mocks.Playback())
			{
				IAvailabilityStatusUpdater updater = new AvailabilityStatusUpdater(repository, context);
				updater.SetAvailabilityStatus(false);
				
				Assert.That(instance.AvailableForLoadBalancing, Is.False);
			}

			mocks.VerifyAll();
		}
		public void Property_accessors_work()
		{
			ApplicationInstance instance = new ApplicationInstance();

			Assert.AreEqual(null, instance.MachineName);
			instance.MachineName = "MachineName";
			Assert.AreEqual("MachineName", instance.MachineName);

			Assert.AreEqual(null, instance.ApplicationDomain);
			instance.ApplicationDomain = "ApplicationDomain";
			Assert.AreEqual("ApplicationDomain", instance.ApplicationDomain);

			Assert.AreEqual(null, instance.UniqueHostHeader);
			instance.UniqueHostHeader = "UniqueHostHeader";
			Assert.AreEqual("UniqueHostHeader", instance.UniqueHostHeader);

			Assert.AreEqual(null, instance.CacheRefreshQueryString);
			instance.CacheRefreshQueryString = "CacheRefreshQueryString";
			Assert.AreEqual("CacheRefreshQueryString", instance.CacheRefreshQueryString);

			Assert.AreEqual(null, instance.MaintenanceHostHeader);
			instance.MaintenanceHostHeader = "MaintenanceHostHeader";
			Assert.AreEqual("MaintenanceHostHeader", instance.MaintenanceHostHeader);

			Assert.AreEqual(false, instance.DownForMaintenance);
			instance.DownForMaintenance = true;
			Assert.AreEqual(true, instance.DownForMaintenance);

			Assert.AreEqual(false, instance.AvailableForLoadBalancing);
			instance.AvailableForLoadBalancing = true;
			Assert.AreEqual(true, instance.AvailableForLoadBalancing);

			Assert.AreEqual(null, instance.Version);
			instance.Version = "Version";
			Assert.AreEqual("Version", instance.Version);
		}
		protected override void SetupDatabase()
		{
			_instance1 = new ApplicationInstance();
			_instance2 = new ApplicationInstance();
			_instance3 = new ApplicationInstance();
			_instance4 = new ApplicationInstance();

			_instance1.MaintenanceHostHeader = "Domain1";
			_instance2.MaintenanceHostHeader = "Domain1";

			_instance1.ApplicationDomain = "Domain1";
			_instance2.ApplicationDomain = "Domain2";

			_instance1.MachineName = "Machine1";
			_instance2.MachineName = "Machine2";

			_instance3.MachineName = "Machine1";
			_instance4.MachineName = "Machine2";

			_instance3.UniqueHostHeader = "HostHeader1";
			_instance4.UniqueHostHeader = "HostHeader2";

			Save(_instance1, _instance2, _instance3, _instance4);
		} 
		public void Save(ApplicationInstance instance)
		{
			_objectRepository.Save(instance);
		}
		public void Delete(ApplicationInstance instance)
		{
			_objectRepository.Delete(instance);
		}
		public void Can_correctly_gets_application_instances_by_host_header()
		{
			var instance1 = new ApplicationInstance();
			var instance2 = new ApplicationInstance();
			var instances = new[] { instance1, instance2 };

			var criteria = new CriterionSet();
			criteria.AddCriterion(new Criterion("UniqueHostHeader", "MyHostHeader"));

			var mocks = new MockRepository();
			var objectRepository = mocks.CreateMock<IPersistentObjectRepository>();

			using (mocks.Record())
			{
				objectRepository.ConfigurationFile = "webmanagement.hibernate.cfg.xml";
				Expect.Call(objectRepository.FindAll<ApplicationInstance>(criteria)).Return(instances);
			}

			using (mocks.Playback())
			{
				IApplicationInstanceRepository repository = new ApplicationInstanceRepository(objectRepository);
				IEnumerable<ApplicationInstance> actualInstances = repository.GetByHostHeader("MyHostHeader");

				EnumerableAssert.That(actualInstances, Is.EqualTo(instances));
			}
		}
		public void Can_correctly_gets_application_instances_by_machine_name_and_maintenance_host_header()
		{
			var instance = new ApplicationInstance();

			var criteria = new CriterionSet();
			criteria.AddCriterion(new Criterion("MachineName", "MyMachine"));
			criteria.AddCriterion(new Criterion("MaintenanceHostHeader", "MyDomain"));

			var mocks = new MockRepository();
			var objectRepository = mocks.CreateMock<IPersistentObjectRepository>();

			using (mocks.Record())
			{
				objectRepository.ConfigurationFile = "webmanagement.hibernate.cfg.xml";
				Expect.Call(objectRepository.FindFirst<ApplicationInstance>(criteria)).Return(instance);
			}

			using (mocks.Playback())
			{
				IApplicationInstanceRepository repository = new ApplicationInstanceRepository(objectRepository);
				var actualInstance = repository.GetByMaintenanceHostHeaderAndMachineName("MyDomain", "MyMachine");

				Assert.That(actualInstance, Is.SameAs(instance));
			}
		}