using FluentNHibernate.Cfg;
using FluentNHibernate.Mapping;
using NHibernate.Cfg;
using Rhino.Mocks;
using Xunit;

namespace USD259.Test
{
    public class RepositoryBuildUpExample_2
    {
        public RepositoryBuildUpExample_2()
        {
            // SimpleSessionStorage exists in our base library and is based on the
            // S#arp Architecture project.  Including the SharpArch.* libraries
            // should resolve the dependency.
            ExampleConfiguration.Init(new SimpleSessionStorage());
        }

        [Fact]
        public void Fetch_A_Report_By_Id()
        {
            IReportRepository stubRepository =
                MockRepository.GenerateStub<IReportRepository>();

            Report stubReport =
                MockRepository.GenerateStub<Report>();

            stubReport.Id = 140;

            stubRepository
                .Expect(x => x.Get(140))
                .Return(stubReport);

            var report = stubRepository.Get(140);

            report.Id.ShouldBeEqualTo(140);
        }

        [Fact]

        public void Fetch_A_Report_By_Id_From_Database()
        {
            var repository = new ReportRepository();
            var report = repository.Get(140);
            report.Id.ShouldBeEqualTo(140);
        }

    }
    // INHibernateRepository exists in our base library and is based on the
    // S#arp Architecture project.  Including the SharpArch.* libraries
    // should resolve the dependency.
    public interface IReportRepository : INHibernateRepository<Report>
    {
    }

    // PersistentObject exists in our base library and is based on the
    // S#arp Architecture project.  Including the SharpArch.* libraries
    // should resolve the dependency.
    public class Report : PersistentObject
    {
    }

    public class ReportMap : ClassMap<Report>
    {
        public ReportMap()
        {
            CreateMap();
        }

        private void CreateMap()
        {
            WithTable("Reports");
            Id(x => x.Id);
        }
    }

    // NHibernateRepository exists in our base library and is based on the
    // S#arp Architecture project.  Including the SharpArch.* libraries
    // should resolve the dependency.
    public class ReportRepository : NHibernateRepository<Report>
    {
    }

    public static class ExampleConfiguration
    {
        public static Configuration Default
        {
            get { return Production; }
        }

        public static Configuration Production
        {
            get
            {
                var config = new Configuration();

                MsSqlConfiguration.MsSql2005
                    .ConnectionString
                        .Database("ExampleDB")
                        .Server("DBServer")
                        .Username("DBLogin")
                        .Password("DBPassword")
                        .Create
                    .ConfigureProperties(config);
                return config;
            }
        }

        public static void Init(ISessionStorage storage)
        {
            Init(Default, storage);
        }

        // NHibernateSession exists in our base library and is based on the
        // S#arp Architecture project.  Including the SharpArch.* libraries
        // should resolve the dependency.
        public static void Init(Configuration configuration, ISessionStorage storage)
        {
            // Any of the maps will do for the assembly mapping.
            NHibernateSession.Init(typeof(ReportMap).Assembly, configuration, storage);
        }
    }


}
