context: //
  // DO NOT REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  //
  // @Authors:
  //       timop
  //
  // Copyright 2004-2013 by OM International
  //
  // This file is part of OpenPetra.org.
  //
  // OpenPetra.org is free software: you can redistribute it and/or modify
  // it under the terms of the GNU General Public License as published by
  // the Free Software Foundation, either version 3 of the License, or
  // (at your option) any later version.
  //
  // OpenPetra.org is distributed in the hope that it will be useful,
  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  // GNU General Public License for more details.
  //
  // You should have received a copy of the GNU General Public License
  // along with OpenPetra.org.  If not, see <http://www.gnu.org/licenses/>.
  //
  using System;
  using System.Data;
  using System.Configuration;
  using System.IO;
  using NUnit.Framework;
  using NUnit.Framework.Constraints;
  using Tests.MPartner.shared.CreateTestPartnerData;
  using Ict.Testing.NUnitPetraServer;
  using Ict.Common;
  using Ict.Common.DB;
  using Ict.Common.Data;
  using Ict.Common.IO;
  using Ict.Common.Verification;
  using Ict.Common.Remoting.Shared;
  using Ict.Common.Remoting.Server;
  using Ict.Petra.Server.App.Core;
  using Ict.Petra.Server.MCommon.Data.Access;
  using Ict.Petra.Server.MCommon.WebConnectors;
  using Ict.Petra.Server.MHospitality.Data.Access;
  using Ict.Petra.Server.MPartner.Common;
  using Ict.Petra.Server.MPartner.Mailroom.Data.Access;
  using Ict.Petra.Server.MPartner.Partner.Data.Access;
  using Ict.Petra.Server.MPartner.Partner.ServerLookups.WebConnectors;
  using Ict.Petra.Server.MPartner.Partner.UIConnectors;
  using Ict.Petra.Server.MPartner.Partner.WebConnectors;
  using Ict.Petra.Server.MPersonnel.Personnel.Data.Access;
  using Ict.Petra.Shared;
  using Ict.Petra.Shared.MCommon.Data;
  using Ict.Petra.Shared.MPartner;
  using Ict.Petra.Shared.MPartner.Mailroom.Data;
  using Ict.Petra.Shared.MPartner.Partner.Data;
  using Ict.Petra.Shared.MPersonnel.Personnel.Data;
  using Ict.Petra.Shared.MHospitality.Data;
  using Ict.Testing.NUnitTools;
  
  namespace Tests.MPartner.Server.PartnerEdit
  {
      /// This will test the business logic directly on the server
      [TestFixture]
      public class TPartnerEditTest
      {
          /// <summary>
          /// open database connection or prepare other things for this test
          /// </summary>
          [TestFixtureSetUp]
          public void Init()
          {
              new TLogging("../../log/TestServer.log");
              TPetraServerConnector.Connect("../../etc/TestServer.config");
          }
  
          /// <summary>
          /// cleaning up everything that was set up for this test
          /// </summary>
          [TestFixtureTearDown]
          public void TearDown()
          {
              TPetraServerConnector.Disconnect();
          }
  
          /// <summary>
          /// create a new partner and save it with a new location
          /// </summary>
          [Test]
          public void TestSaveNewPartnerWithLocation()
          {
              TPartnerEditUIConnector connector = new TPartnerEditUIConnector();
  
              PartnerEditTDS MainDS = new PartnerEditTDS();
  
              PPartnerRow PartnerRow = TCreateTestPartnerData.CreateNewFamilyPartner(MainDS);
  
              TCreateTestPartnerData.CreateNewLocation(PartnerRow.PartnerKey, MainDS);
  
              DataSet ResponseDS = new PartnerEditTDS();
              TVerificationResultCollection VerificationResult;
  
              TSubmitChangesResult result = connector.SubmitChanges(ref MainDS, ref ResponseDS, out VerificationResult);
  
              CommonNUnitFunctions.EnsureNullOrOnlyNonCriticalVerificationResults(VerificationResult,
                  "There was a critical error when saving:");
  
              Assert.AreEqual(TSubmitChangesResult.scrOK, result, "TPartnerEditUIConnector SubmitChanges return value");
  
              // check the location key for this partner. should not be negative
              Assert.AreEqual(1, MainDS.PPartnerLocation.Rows.Count, "TPartnerEditUIConnector SubmitChanges returns one location");
              Assert.Greater(MainDS.PPartnerLocation[0].LocationKey, 0, "TPartnerEditUIConnector SubmitChanges returns valid location key");
          }
  
          /// <summary>
          /// new partner with location 0.
          /// first save the partner with location 0, then add a new location, and save again
          /// </summary>
          [Test]
          public void TestNewPartnerWithLocation0()
          {
              TPartnerEditUIConnector connector = new TPartnerEditUIConnector();
  
              PartnerEditTDS MainDS = new PartnerEditTDS();
  
              PPartnerRow PartnerRow = TCreateTestPartnerData.CreateNewFamilyPartner(MainDS);
  
              PPartnerLocationRow PartnerLocationRow = MainDS.PPartnerLocation.NewRowTyped();
  
              PartnerLocationRow.SiteKey = DomainManager.GSiteKey;
              PartnerLocationRow.PartnerKey = PartnerRow.PartnerKey;
              PartnerLocationRow.LocationKey = 0;
              MainDS.PPartnerLocation.Rows.Add(PartnerLocationRow);
  
              DataSet ResponseDS = new PartnerEditTDS();
              TVerificationResultCollection VerificationResult;
  
              TSubmitChangesResult result = connector.SubmitChanges(ref MainDS, ref ResponseDS, out VerificationResult);
  
              CommonNUnitFunctions.EnsureNullOrOnlyNonCriticalVerificationResults(VerificationResult,
                  "There was a critical error when saving:");
  
              Assert.AreEqual(TSubmitChangesResult.scrOK, result, "Create a partner with location 0");
  
              TCreateTestPartnerData.CreateNewLocation(PartnerRow.PartnerKey, MainDS);
  
              // remove location 0, same is done in csharp\ICT\Petra\Client\MCommon\logic\UC_PartnerAddresses.cs TUCPartnerAddressesLogic::AddRecord
              // Check if record with PartnerLocation.LocationKey = 0 is around > delete it
              DataRow PartnerLocationRecordZero =
                  MainDS.PPartnerLocation.Rows.Find(new object[] { PartnerRow.PartnerKey, DomainManager.GSiteKey, 0 });
  
              if (PartnerLocationRecordZero != null)
              {
                  DataRow LocationRecordZero = MainDS.PLocation.Rows.Find(new object[] { DomainManager.GSiteKey, 0 });
  
                  if (LocationRecordZero != null)
                  {
                      LocationRecordZero.Delete();
                  }
  
                  PartnerLocationRecordZero.Delete();
              }
  
              ResponseDS = new PartnerEditTDS();
              result = connector.SubmitChanges(ref MainDS, ref ResponseDS, out VerificationResult);
  
              CommonNUnitFunctions.EnsureNullOrOnlyNonCriticalVerificationResults(VerificationResult,
                  "There was a critical error when saving:");
  
              Assert.AreEqual(TSubmitChangesResult.scrOK, result, "Replace location 0 of partner");
  
              Assert.AreEqual(1, MainDS.PPartnerLocation.Rows.Count, "the partner should only have one location in the dataset");
  
              // get all addresses of the partner
              TDBTransaction ReadTransaction = null;
              DBAccess.GDBAccessObj.GetNewOrExistingAutoReadTransaction(IsolationLevel.ReadCommitted, TEnforceIsolationLevel.eilMinimum,
                  ref ReadTransaction,
                  delegate
                  {
                      PPartnerLocationTable testPartnerLocations = PPartnerLocationAccess.LoadViaPPartner(PartnerRow.PartnerKey, ReadTransaction);
                      Assert.AreEqual(1, testPartnerLocations.Rows.Count, "the partner should only have one location");
                      Assert.Greater(testPartnerLocations[0].LocationKey, 0, "TPartnerEditUIConnector SubmitChanges returns valid location key");
                  });
          }
  
          /// <summary>
          /// create a new partner and save it with an existing location
          /// </summary>
          [Test]
          public void TestSaveNewPartnerWithExistingLocation()
          {
              TPartnerEditUIConnector connector = new TPartnerEditUIConnector();
  
              PartnerEditTDS MainDS = new PartnerEditTDS();
  
              PPartnerRow PartnerRow = TCreateTestPartnerData.CreateNewFamilyPartner(MainDS);
  
              TCreateTestPartnerData.CreateNewLocation(PartnerRow.PartnerKey, MainDS);
  
              DataSet ResponseDS = new PartnerEditTDS();
              TVerificationResultCollection VerificationResult;
  
              TSubmitChangesResult result = connector.SubmitChanges(ref MainDS, ref ResponseDS, out VerificationResult);
  
              CommonNUnitFunctions.EnsureNullOrOnlyNonCriticalVerificationResults(VerificationResult,
                  "There was a critical error when saving:");
  
              Assert.AreEqual(TSubmitChangesResult.scrOK, result, "saving the first partner with a location");
  
              Int32 LocationKey = MainDS.PLocation[0].LocationKey;
  
              MainDS = new PartnerEditTDS();
  
              PartnerRow = TCreateTestPartnerData.CreateNewFamilyPartner(MainDS);
  
              PPartnerLocationRow PartnerLocationRow = MainDS.PPartnerLocation.NewRowTyped();
              PartnerLocationRow.SiteKey = DomainManager.GSiteKey;
              PartnerLocationRow.PartnerKey = PartnerRow.PartnerKey;
              PartnerLocationRow.LocationKey = LocationKey;
              MainDS.PPartnerLocation.Rows.Add(PartnerLocationRow);
  
              ResponseDS = new PartnerEditTDS();
  
              result = connector.SubmitChanges(ref MainDS, ref ResponseDS, out VerificationResult);
  
              CommonNUnitFunctions.EnsureNullOrOnlyNonCriticalVerificationResults(VerificationResult,
                  "There was a critical error when saving:");
  
              TDBTransaction ReadTransaction = null;
              DBAccess.GDBAccessObj.GetNewOrExistingAutoReadTransaction(IsolationLevel.ReadCommitted, TEnforceIsolationLevel.eilMinimum,
                  ref ReadTransaction,
                  delegate
                  {
                      PPartnerTable PartnerAtAddress = PPartnerAccess.LoadViaPLocation(
                          DomainManager.GSiteKey, LocationKey, ReadTransaction);
                      Assert.AreEqual(2, PartnerAtAddress.Rows.Count, "there should be two partners at this location");
                  });
          }
  
          /// <summary>
          /// add a new location for a family, and propagate this to the members of the family
          /// </summary>
          [Test]
          public void TestFamilyPropagateNewLocation()
          {
              TPartnerEditUIConnector connector = new TPartnerEditUIConnector();
  
              PartnerEditTDS MainDS = new PartnerEditTDS();
  
              TCreateTestPartnerData.CreateFamilyWithTwoPersonRecords(MainDS);
  
              DataSet ResponseDS = new PartnerEditTDS();
              TVerificationResultCollection VerificationResult;
  
              TSubmitChangesResult result = connector.SubmitChanges(ref MainDS, ref ResponseDS, out VerificationResult);
  
              CommonNUnitFunctions.EnsureNullOrOnlyNonCriticalVerificationResults(VerificationResult,
  
gt:                 "There was a critical error when saving:");