view Agendas/trunk/src/Agendas.Repositories.Tests/PatrocinadorEventoTests.cs @ 300:48ab8788bd19

#200: Patrocinadores: Agregar link al sitio web.
author juanjose.montesdeocaarbos
date Mon, 27 Feb 2012 14:45:12 -0300
parents 9bc60d166c8a
children
line wrap: on
line source

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using Agendas.Repositories.Tests.Infraestructure;
using AltNetHispano.Agendas.Domain;
using AltNetHispano.Agendas.Domain.Repositories;
using Moq;
using NUnit.Framework;

namespace Agendas.Repositories.Tests
{
	public abstract class PatrocinadorEventoTests<T> where T : IInfraestrutureFactory, new()
	{
		private readonly IPersonaRepository _personaRepository;
		private readonly IEventoRepository _eventoRepository;
		private readonly IPatrocinadorRepository _patrocinadorRepository;
		private readonly Func<IDisposable> _requestEmulator;

		private Guid _apressId;
		private Guid _eventoUnoId;
		private Guid _eventoDosId;
		private Agenda _agenda;
		private readonly TestsHelper _testsHelper;

		private static byte[] LoadLogo(string path)
		{
			using (var image = Image.FromFile(path))
			{
				var ms = new MemoryStream();
				image.Save(ms, image.RawFormat);
				return ms.ToArray();
			}
		}

		protected PatrocinadorEventoTests()
		{
			var infraestrutureFactory = new T();

			_eventoRepository = infraestrutureFactory.GetEventoRepository();
			_personaRepository = infraestrutureFactory.GetPonenteRepository();
			_patrocinadorRepository = infraestrutureFactory.GetPatrocinadorRepository();
			_requestEmulator = infraestrutureFactory.GetRequestEmulator();

			_testsHelper = new TestsHelper(_personaRepository, _patrocinadorRepository);
		}

		[SetUp]
		public void SetearUsuario()
		{
			var seguridad = new Mock<ISeguridad>();
			seguridad.Setup(s => s.GetUserName()).Returns("neluz");
			IdentityContext.Init(seguridad.Object, _personaRepository);
		}

		private Guid CrearEvento(string[] nombrePonentes, string nombreEvento, DateTime fechaInicio, DateTime fechaTermino)
		{
			Evento evento;
			var ponentes = new Guid[nombrePonentes.Length];
			for (var nroPonente = 0; nroPonente < nombrePonentes.Length; nroPonente++)
			{
				using (_requestEmulator.Invoke())
				{
					ponentes[nroPonente] = _testsHelper.GetOrCreatePonente(nombrePonentes[nroPonente]);
				}
			}

			using (_requestEmulator.Invoke())
			{
				_agenda.Agendar(nombreEvento, ponentes, fechaInicio, fechaTermino, null, TipoEvento.Van);
			}
			using (_requestEmulator.Invoke())
			{
				evento = _eventoRepository.GetActivos().First(e => e.Titulo == nombreEvento);
			}
			return evento.Id;
		}

		private Guid CrearPatrocinador(string nombre, string webSite, string images)
		{
			var patrocinador = new Patrocinador(nombre, webSite)
			                   	{
									Logo = LoadLogo(images)
			                   	};
			using (_requestEmulator.Invoke())
			{
				_patrocinadorRepository.Save(patrocinador);
			}
			return patrocinador.Id;
		}

		protected void CrearDatos()
		{
			var fechaInicio = DateTime.Today.AddDays(7).ToUniversalTime();
			var fechaTermino = fechaInicio.AddHours(2);
			_agenda = new Agenda(null, _eventoRepository, _personaRepository, _patrocinadorRepository);

			_apressId = CrearPatrocinador("Apress", "http://apress.com/", "images/apress.gif");
			_eventoUnoId = CrearEvento(new[] { "Carlos Peix", "Nelo Pauselli" }, "ADFS", fechaInicio, fechaTermino);
			_eventoDosId = CrearEvento(new[] { "Carlos Peix", "Nelo Pauselli" }, "ADFS: Segunda parte.", fechaInicio.AddDays(3), fechaTermino.AddDays(3));
		}

		[Test]
		public void MismoPatrocinadorEnDosEventosDiferentes()
		{
			CrearDatos();
			Evento eventoUno, eventoDos;
			{
				using (_requestEmulator.Invoke())
				{
					_agenda.IndicarPatrocinadores(_eventoUnoId, new[] {_apressId});
				}
				using (_requestEmulator.Invoke())
				{
					eventoUno = _eventoRepository.GetActivos().First(e => e.Id == _eventoUnoId);
					Assert.AreEqual(1, eventoUno.Patrocinadores.Count());
				}
			}

			{
				using (_requestEmulator.Invoke())
				{
					_agenda.IndicarPatrocinadores(_eventoDosId, new[] {_apressId});
				}
				using (_requestEmulator.Invoke())
				{
					eventoDos = _eventoRepository.GetActivos().First(e => e.Id == _eventoDosId);
					Assert.AreEqual(1, eventoDos.Patrocinadores.Count());
				}
			}
			using (_requestEmulator.Invoke())
			{
				eventoDos = _eventoRepository.GetActivos().First(e => e.Id == _eventoDosId);
				Assert.AreEqual(1, eventoDos.Patrocinadores.Count());
			}
			using (_requestEmulator.Invoke())
			{
				eventoUno = _eventoRepository.GetActivos().First(e => e.Id == _eventoUnoId);
				Assert.AreEqual(1, eventoUno.Patrocinadores.Count());
			}
		}
	}
}