using Google.Apis.PeopleService.v1;
using Google.Apis.PeopleService.v1.Data;
using NUnit.Framework;
using Serilog;
using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace GoContactSyncMod.UnitTests
{
[TestFixture]
public class SyncContactsTests
{
private Synchronizer sync;
private const int defaultWait = 5000;
private const int defaultWaitTries = 10;
//Constants for test contact
internal const string TEST_CONTACT_NAME = "AN_OUTLOOK_TEST_CONTACT";
internal const string TEST_SAVE_AS = "SaveAs";
internal const string TEST_CONTACT_EMAIL = "email00@outlook.com";
private const string TEST_GROUP = "A TEST GROUP";
//private ContactGroup defaultContactGroup;
private int initialGoogleGroupCount;
private int initialGoogleContactCount;
[OneTimeSetUp]
public void Init()
{
//string timestamp = DateTime.Now.Ticks.ToString();
GoogleAPITests.LoadSettings(out var gmailUsername, out var syncProfile, out var syncContactsFolder, out var syncAppointmentsFolder);
sync = new Synchronizer
{
SyncContacts = true,
SyncAppointments = false,
};
Synchronizer.SyncProfile = syncProfile;
Synchronizer.SyncContactsFolder = syncContactsFolder;
sync.LoginToGoogle(gmailUsername);
sync.LoginToOutlook();
//Only load Google Contacts in My Contacts group (to avoid syncing accounts added automatically to "Weitere Kontakte"/"Further Contacts")
sync.LoadGoogleGroups();
//defaultContactGroup = sync.GetGoogleGroupByName(Synchronizer.myContactsGroup);
initialGoogleGroupCount = CountGoogleGroups();
}
[SetUp]
public void SetUp()
{
// delete previously failed test contacts
DeleteTestContacts();
DeleteTestContactGroups();
initialGoogleGroupCount = CountGoogleGroups();
sync.UseFileAs = true;
}
[OneTimeTearDown]
public void TearDown()
{
sync.LogoffOutlook();
sync.LogoffGoogle();
}
[Test]
public void TestSync_Structured()
{
Log.Information($"*** TestSync_Structured ***");
// create new contact to sync
var oc1 = new OutlookContactBuilder().Build();
oc1.FileAs = TEST_CONTACT_NAME;
oc1.Email1Address = TEST_CONTACT_EMAIL;
oc1.Email2Address = TEST_CONTACT_EMAIL.Replace("00", "01");
oc1.Email3Address = TEST_CONTACT_EMAIL.Replace("00", "02");
oc1.HomeAddressStreet = "Street";
oc1.HomeAddressCity = "City";
oc1.HomeAddressPostalCode = "1234";
oc1.HomeAddressCountry = "Country";
oc1.HomeAddressPostOfficeBox = "PO1";
oc1.HomeAddressState = "State1";
oc1.BusinessAddressStreet = "Street2";
oc1.BusinessAddressCity = "City2";
oc1.BusinessAddressPostalCode = "5678";
oc1.BusinessAddressCountry = "Country2";
oc1.BusinessAddressPostOfficeBox = "PO2";
oc1.BusinessAddressState = "State2";
oc1.OtherAddressStreet = "Street3";
oc1.OtherAddressCity = "City3";
oc1.OtherAddressPostalCode = "8012";
oc1.OtherAddressCountry = "Country3";
oc1.OtherAddressPostOfficeBox = "PO3";
oc1.OtherAddressState = "State3";
#region phones
//First delete the destination phone numbers
oc1.PrimaryTelephoneNumber = "123";
oc1.HomeTelephoneNumber = "456";
oc1.Home2TelephoneNumber = "4567";
oc1.BusinessTelephoneNumber = "45678";
oc1.Business2TelephoneNumber = "456789";
oc1.MobileTelephoneNumber = "123";
oc1.BusinessFaxNumber = "1234";
oc1.HomeFaxNumber = "12345";
oc1.PagerNumber = "123456";
oc1.OtherTelephoneNumber = "12345678";
//oc1.CarTelephoneNumber = "123456789";
//oc1.AssistantTelephoneNumber = "987";
#endregion phones
#region Name
oc1.Title = "Title";
oc1.FirstName = "Firstname";
oc1.MiddleName = "Middlename";
oc1.LastName = "Lastname";
oc1.Suffix = "Suffix";
#endregion Name
oc1.Birthday = new DateTime(1999, 1, 1);
oc1.NickName = "Nickname";
oc1.OfficeLocation = "Location";
oc1.Initials = "IN";
oc1.Language = "German";
oc1.CompanyName = "CompanyName";
oc1.JobTitle = "Position";
oc1.Department = "Department";
oc1.IMAddress = "IMs";
oc1.Anniversary = new DateTime(2000, 1, 1);
oc1.Children = "Children";
oc1.Spouse = "Spouse";
oc1.AssistantName = "Assi";
oc1.ManagerName = "Chef";
oc1.WebPage = "http://www.test.de";
oc1.Body = "<sn>Content & other stuff</sn>\r\n<![CDATA[ \r\n...\r\n&stuff in CDATA < >\r\n ]]>";
oc1.Save();
sync.SyncOption = SyncOption.OutlookToGoogleOnly;
var gc = new GoogleContactBuilder().Build();
sync.UpdateContact(oc1, gc);
var match = new ContactMatch(new OutlookContactInfo(oc1, sync), gc);
match.GoogleContactDirty = true;
//save contact to google.
sync.SaveGoogleContact(match);
Assert.IsTrue(EnsureGoogleContactSaved(match.GoogleContact));
sync.SyncOption = SyncOption.GoogleToOutlookOnly;
//load the same contact from google.
sync.MatchContacts();
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
//ContactsMatcher.SyncContact(match, sync);
var oc2 = new OutlookContactBuilder().Build();
ContactSync.UpdateContact(match.GoogleContact, oc2, sync.UseFileAs);
// match oc2 with oc1
Assert.AreEqual(oc1.FileAs, oc2.FileAs);
Assert.AreEqual(oc1.Email1Address, oc2.Email1Address);
Assert.AreEqual(oc1.Email2Address, oc2.Email2Address);
Assert.AreEqual(oc1.Email3Address, oc2.Email3Address);
Assert.AreEqual(oc1.PrimaryTelephoneNumber, oc2.PrimaryTelephoneNumber);
Assert.AreEqual(oc1.HomeTelephoneNumber, oc2.HomeTelephoneNumber);
Assert.AreEqual(oc1.Home2TelephoneNumber, oc2.Home2TelephoneNumber);
Assert.AreEqual(oc1.BusinessTelephoneNumber, oc2.BusinessTelephoneNumber);
Assert.AreEqual(oc1.Business2TelephoneNumber, oc2.Business2TelephoneNumber);
Assert.AreEqual(oc1.MobileTelephoneNumber, oc2.MobileTelephoneNumber);
Assert.AreEqual(oc1.BusinessFaxNumber, oc2.BusinessFaxNumber);
Assert.AreEqual(oc1.HomeFaxNumber, oc2.HomeFaxNumber);
Assert.AreEqual(oc1.PagerNumber, oc2.PagerNumber);
Assert.AreEqual(oc1.OtherTelephoneNumber, oc2.OtherTelephoneNumber);
//Assert.AreEqual(oc1.CarTelephoneNumber, oc2.CarTelephoneNumber);
//Assert.AreEqual(oc1.AssistantTelephoneNumber, oc2.AssistantTelephoneNumber);
Assert.AreEqual(oc1.HomeAddressStreet, oc2.HomeAddressStreet);
Assert.AreEqual(oc1.HomeAddressCity, oc2.HomeAddressCity);
Assert.AreEqual(oc1.HomeAddressCountry, oc2.HomeAddressCountry);
Assert.AreEqual(oc1.HomeAddressPostalCode, oc2.HomeAddressPostalCode);
Assert.AreEqual(oc1.HomeAddressPostOfficeBox, oc2.HomeAddressPostOfficeBox);
Assert.AreEqual(oc1.HomeAddressState, oc2.HomeAddressState);
Assert.AreEqual(oc1.BusinessAddressStreet, oc2.BusinessAddressStreet);
Assert.AreEqual(oc1.BusinessAddressCity, oc2.BusinessAddressCity);
Assert.AreEqual(oc1.BusinessAddressCountry, oc2.BusinessAddressCountry);
Assert.AreEqual(oc1.BusinessAddressPostalCode, oc2.BusinessAddressPostalCode);
Assert.AreEqual(oc1.BusinessAddressPostOfficeBox, oc2.BusinessAddressPostOfficeBox);
Assert.AreEqual(oc1.BusinessAddressState, oc2.BusinessAddressState);
Assert.AreEqual(oc1.OtherAddressStreet, oc2.OtherAddressStreet);
Assert.AreEqual(oc1.OtherAddressCity, oc2.OtherAddressCity);
Assert.AreEqual(oc1.OtherAddressCountry, oc2.OtherAddressCountry);
Assert.AreEqual(oc1.OtherAddressPostalCode, oc2.OtherAddressPostalCode);
Assert.AreEqual(oc1.OtherAddressPostOfficeBox, oc2.OtherAddressPostOfficeBox);
Assert.AreEqual(oc1.OtherAddressState, oc2.OtherAddressState);
Assert.AreEqual(oc1.FullName, oc2.FullName);
Assert.AreEqual(oc1.MiddleName, oc2.MiddleName);
Assert.AreEqual(oc1.LastName, oc2.LastName);
Assert.AreEqual(oc1.FirstName, oc2.FirstName);
Assert.AreEqual(oc1.Title, oc2.Title);
Assert.AreEqual(oc1.Suffix, oc2.Suffix);
Assert.AreEqual(oc1.Birthday, oc2.Birthday);
Assert.AreEqual(oc1.NickName, oc2.NickName);
Assert.AreEqual(oc1.OfficeLocation, oc2.OfficeLocation);
Assert.AreEqual(oc1.Initials, oc2.Initials);
Assert.AreEqual(oc1.Language, oc2.Language);
Assert.AreEqual(oc1.IMAddress, oc2.IMAddress);
Assert.AreEqual(oc1.Anniversary, oc2.Anniversary);
Assert.AreEqual(oc1.Children, oc2.Children);
Assert.AreEqual(oc1.Spouse, oc2.Spouse);
Assert.AreEqual(oc1.ManagerName, oc2.ManagerName);
Assert.AreEqual(oc1.AssistantName, oc2.AssistantName);
Assert.AreEqual(oc1.WebPage, oc2.WebPage);
Assert.AreEqual(oc1.Body, oc2.Body);
Assert.AreEqual(oc1.CompanyName, oc2.CompanyName);
Assert.AreEqual(oc1.JobTitle, oc2.JobTitle);
Assert.AreEqual(oc1.Department, oc2.Department);
DeleteAppointmentsForTestContacts();
oc1.Delete();
oc2.Delete();
DeleteTestContact(match.GoogleContact);
Log.Information($"*** TestSync_Structured ***");
}
[Test]
public void TestSync_Unstructured()
{
Log.Information($"*** TestSync_Unstructured ***");
sync.SyncOption = SyncOption.MergeOutlookWins;
// create new contact to sync
var oc1 = new OutlookContactBuilder().Build();
oc1.FileAs = TEST_CONTACT_NAME;
oc1.HomeAddress = "10 Parades";
oc1.BusinessAddress = "11 Parades";
oc1.OtherAddress = "12 Parades";
oc1.IMAddress = " "; //Test empty IMAddress
oc1.Email2Address = " "; //Test empty Email Address
oc1.FullName = TEST_CONTACT_NAME;
oc1.Save();
sync.SyncOption = SyncOption.OutlookToGoogleOnly;
var gc = new GoogleContactBuilder().Build();
sync.UpdateContact(oc1, gc);
var match = new ContactMatch(new OutlookContactInfo(oc1, sync), gc);
match.GoogleContactDirty = true;
//save contact to google.
sync.SaveGoogleContact(match);
Assert.IsTrue(EnsureGoogleContactSaved(match.GoogleContact));
sync.SyncOption = SyncOption.GoogleToOutlookOnly;
//load the same contact from google.
sync.MatchContacts();
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
//ContactsMatcher.SyncContact(match, sync);
Assert.IsNotNull(match.GoogleContact);
var oc2 = new OutlookContactBuilder().Build();
ContactSync.UpdateContact(match.GoogleContact, oc2, sync.UseFileAs);
// match oc2 with oc1
Assert.AreEqual(oc1.FileAs, oc2.FileAs);
Assert.AreEqual(oc1.HomeAddress, oc2.HomeAddress);
Assert.AreEqual(oc1.BusinessAddress, oc2.BusinessAddress);
Assert.AreEqual(oc1.OtherAddress, oc2.OtherAddress);
Assert.AreEqual(oc1.FullName, oc2.FullName);
oc1.Delete();
oc2.Delete();
DeleteTestContact(match.GoogleContact);
Log.Information($"*** TestSync_Unstructured ***");
}
[Test]
public void TestSync_CompanyOnly()
{
Log.Information($"*** TestSync_CompanyOnly ***");
sync.SyncOption = SyncOption.MergeOutlookWins;
// create new contact to sync
var oc1 = new OutlookContactBuilder().Build();
oc1.CompanyName = TEST_CONTACT_NAME;
oc1.BusinessAddress = "11 Parades";
oc1.Save();
Assert.IsNull(oc1.FullName);
Assert.IsNull(oc1.Email1Address);
sync.SyncOption = SyncOption.OutlookToGoogleOnly;
var gc = new GoogleContactBuilder().Build();
sync.UpdateContact(oc1, gc);
var match = new ContactMatch(new OutlookContactInfo(oc1, sync), gc);
match.GoogleContactDirty = true;
//save contact to google.
sync.SaveGoogleContact(match);
Assert.IsTrue(EnsureGoogleContactSaved(match.GoogleContact));
sync.SyncOption = SyncOption.GoogleToOutlookOnly;
//load the same contact from google.
sync.MatchContacts();
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
//ContactsMatcher.SyncContact(match, sync);
var oc2 = new OutlookContactBuilder().Build();
ContactSync.UpdateContact(match.GoogleContact, oc2, sync.UseFileAs);
// match oc2 with oc1
Assert.AreEqual(oc1.FileAs, oc2.FileAs);
Assert.AreEqual(oc1.CompanyName, oc2.CompanyName);
Assert.AreEqual(oc1.BusinessAddress, oc2.BusinessAddress);
Assert.IsNull(oc2.FullName);
Assert.IsNull(oc1.Email1Address);
oc1.Delete();
oc2.Delete();
DeleteTestContact(match.GoogleContact);
Log.Information($"*** TestSync_CompanyOnly ***");
}
[Test]
public void TestSync_EmailOnly()
{
Log.Information($"*** TestSync_EmailOnly ***");
sync.SyncOption = SyncOption.MergeOutlookWins;
// create new contact to sync
var oc1 = new OutlookContactBuilder().Build();
oc1.FileAs = TEST_CONTACT_EMAIL;
oc1.Email1Address = TEST_CONTACT_EMAIL;
oc1.Save();
Assert.IsNull(oc1.FullName);
Assert.IsNull(oc1.CompanyName);
sync.SyncOption = SyncOption.OutlookToGoogleOnly;
var gc = new GoogleContactBuilder().Build();
sync.UpdateContact(oc1, gc);
var match = new ContactMatch(new OutlookContactInfo(oc1, sync), gc);
match.GoogleContactDirty = true;
//save contact to google.
sync.SaveGoogleContact(match);
Assert.IsTrue(EnsureGoogleContactSaved(match.GoogleContact));
sync.SyncOption = SyncOption.GoogleToOutlookOnly;
//load the same contact from google.
sync.MatchContacts();
match = sync.ContactByProperty(TEST_CONTACT_EMAIL, TEST_CONTACT_EMAIL);
//ContactsMatcher.SyncContact(match, sync);
var oc2 = new OutlookContactBuilder().Build();
ContactSync.UpdateContact(match.GoogleContact, oc2, sync.UseFileAs);
// match oc2 with oc1
Assert.AreEqual(oc1.FileAs, oc2.FileAs);
Assert.AreEqual(oc1.Email1Address, oc2.Email1Address);
Assert.IsNull(oc2.FullName);
Assert.IsNull(oc1.CompanyName);
oc1.Delete();
oc2.Delete();
DeleteTestContact(match.GoogleContact);
Log.Information($"*** TestSync_EmailOnly ***");
}
[Test]
public void TestSync_UseFileAs()
{
Log.Information($"*** TestSync_UseFileAs ***");
sync.SyncOption = SyncOption.MergeOutlookWins;
sync.UseFileAs = true;
// create new contact to sync
var oc1 = new OutlookContactBuilder().Build();
oc1.FullName = TEST_CONTACT_NAME;
oc1.FileAs = TEST_SAVE_AS;
oc1.Save();
Assert.AreNotEqual(oc1.FullName, oc1.FileAs);
sync.SyncOption = SyncOption.OutlookToGoogleOnly;
var gc = new GoogleContactBuilder().Build();
sync.UpdateContact(oc1, gc);
var match = new ContactMatch(new OutlookContactInfo(oc1, sync), gc);
match.GoogleContactDirty = true;
//save contact to google.
sync.SaveGoogleContact(match);
Assert.IsTrue(EnsureGoogleContactSaved(match.GoogleContact));
sync.SyncOption = SyncOption.GoogleToOutlookOnly;
//load the same contact from google.
sync.MatchContacts();
match = sync.ContactByProperty(TEST_SAVE_AS, string.Empty);
//ContactsMatcher.SyncContact(match, sync);
var oc2 = new OutlookContactBuilder().Build();
Assert.IsNotNull(match.GoogleContact);
ContactSync.UpdateContact(match.GoogleContact, oc2, sync.UseFileAs);
// match oc2 with oc1
var googleName = ContactPropertiesUtils.GetGooglePrimaryName(match.GoogleContact);
Assert.IsNotNull(googleName);
var googleFileAs = ContactPropertiesUtils.GetGoogleFileAs(match.GoogleContact);
Assert.IsNotNull(googleFileAs);
Assert.AreEqual(oc2.FileAs, googleFileAs.Value);
Assert.AreEqual(oc2.FileAs, googleName.UnstructuredName);
Assert.AreEqual(oc1.FileAs, oc2.FileAs);
oc2.FileAs = TEST_CONTACT_NAME;
Assert.AreNotEqual(oc1.FileAs, oc2.FileAs);
Assert.AreNotEqual(oc2.FileAs, googleFileAs.Value);
ContactSync.UpdateContact(match.GoogleContact, oc2, sync.UseFileAs);
Assert.AreEqual(googleName.FamilyName, oc2.FileAs);
oc1.Delete();
oc2.Delete();
DeleteTestContact(match.GoogleContact);
Log.Information($"*** TestSync_UseFileAs ***");
}
[Test]
public void TestSync_UseFullName()
{
Log.Information($"*** TestSync_UseFullName ***");
sync.SyncOption = SyncOption.MergeOutlookWins;
sync.UseFileAs = false;
// create new contact to sync
var oc1 = new OutlookContactBuilder().Build();
oc1.FullName = TEST_CONTACT_NAME;
oc1.FileAs = TEST_SAVE_AS;
oc1.Save();
Assert.AreNotEqual(oc1.FullName, oc1.FileAs);
sync.SyncOption = SyncOption.OutlookToGoogleOnly;
var gc = new GoogleContactBuilder().Build();
sync.UpdateContact(oc1, gc);
var match = new ContactMatch(new OutlookContactInfo(oc1, sync), gc);
match.GoogleContactDirty = true;
//save contact to google.
sync.SaveGoogleContact(match);
Assert.IsTrue(EnsureGoogleContactSaved(match.GoogleContact));
sync.SyncOption = SyncOption.GoogleToOutlookOnly;
//load the same contact from google.
Assert.IsTrue(EnsureGoogleContactSaved(match.GoogleContact));
sync.MatchContacts();
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
Assert.IsNotNull(match);
var oc2 = new OutlookContactBuilder().Build();
ContactSync.UpdateContact(match.GoogleContact, oc2, sync.UseFileAs);
// match oc2 with oc1
var googleName = ContactPropertiesUtils.GetGooglePrimaryName(match.GoogleContact);
Assert.IsNotNull(googleName);
Assert.IsNotEmpty(googleName.UnstructuredName);
var googleFileAs = ContactPropertiesUtils.GetGoogleFileAs(match.GoogleContact);
Assert.IsNull(googleFileAs);
Assert.AreEqual(googleName.UnstructuredName, OutlookContactInfo.GetTitleFirstLastAndSuffix(oc2).Trim().Replace(" ", " "));
//Assert.AreNotEqual(oc1.FileAs, googleFileAs.Value);
Assert.AreNotEqual(oc1.FileAs, googleName.UnstructuredName);
Assert.AreNotEqual(oc1.FileAs, oc2.FileAs);
oc2.FileAs = TEST_SAVE_AS;
Assert.AreEqual(oc1.FileAs, oc2.FileAs);
ContactSync.UpdateContact(match.GoogleContact, oc2, sync.UseFileAs);
Assert.AreEqual(oc1.FileAs, oc2.FileAs);
oc1.Delete();
oc2.Delete();
DeleteTestContact(match.GoogleContact);
Log.Information($"*** TestSync_UseFullName ***");
}
[Test]
public void TestExtendedProps()
{
Log.Information($"*** TestExtendedProps ***");
sync.SyncOption = SyncOption.MergeOutlookWins;
sync.UseFileAs = true;
// create new contact to sync
var oc = new OutlookContactBuilder().Build();
oc.FullName = TEST_CONTACT_NAME;
oc.FileAs = TEST_CONTACT_NAME;
oc.Email1Address = TEST_CONTACT_EMAIL;
oc.Email2Address = TEST_CONTACT_EMAIL.Replace("00", "01");
oc.Email3Address = TEST_CONTACT_EMAIL.Replace("00", "02");
oc.HomeAddress = "10 Parades";
oc.PrimaryTelephoneNumber = "123";
oc.Save();
var gc = new GoogleContactBuilder().Build();
sync.UpdateContact(oc, gc);
var m = new ContactMatch(new OutlookContactInfo(oc, sync), gc);
m.GoogleContactDirty = true;
sync.SaveGoogleContact(m);
var googleFileAs = ContactPropertiesUtils.GetGoogleFileAs(m.GoogleContact);
Assert.IsNotNull(googleFileAs);
Assert.AreEqual(TEST_CONTACT_NAME, googleFileAs.Value);
// read contact from google
Assert.IsTrue(EnsureGoogleContactSaved(m.GoogleContact));
sync.MatchContacts();
ContactsMatcher.SyncContacts(sync);
m = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
Assert.IsNotNull(m);
Assert.IsNotNull(m.GoogleContact);
// get extended prop
var oid = ContactPropertiesUtils.GetOutlookId(oc);
var gid = ContactPropertiesUtils.GetGoogleOutlookContactId(m.GoogleContact);
Assert.AreEqual(oid, gid);
oc.Delete();
DeleteTestContact(m.GoogleContact);
Log.Information($"*** TestExtendedProps ***");
}
[Test]
public void TestSyncDeletedOulook()
{
Log.Information($"*** TestSyncDeletedOulook ***");
sync.LoadContacts();
//ToDo: Check for each SyncOption and SyncDelete combination
sync.SyncOption = SyncOption.MergeOutlookWins;
sync.SyncDelete = true;
// create new contact to sync
var oc = new OutlookContactBuilder().Build();
oc.FullName = TEST_CONTACT_NAME;
oc.FileAs = TEST_CONTACT_NAME;
oc.Email1Address = TEST_CONTACT_EMAIL;
oc.Email2Address = TEST_CONTACT_EMAIL.Replace("00", "01");
oc.Email3Address = TEST_CONTACT_EMAIL.Replace("00", "02");
oc.HomeAddress = "10 Parades";
oc.PrimaryTelephoneNumber = "123";
oc.Save();
var gc = new GoogleContactBuilder().Build();
sync.UpdateContact(oc, gc);
var match = new ContactMatch(new OutlookContactInfo(oc, sync), gc);
match.GoogleContactDirty = true;
//save contacts
sync.SaveContact(match);
Assert.IsTrue(EnsureGoogleContactSaved(match.GoogleContact));
// delete outlook contact
oc.Delete();
// sync
sync.MatchContacts();
ContactsMatcher.SyncContacts(sync);
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
Assert.IsNotNull(match);
Assert.IsNotNull(match.GoogleContact);
Assert.IsNull(match.OutlookContact);
// delete
sync.SaveContact(match);
Assert.IsTrue(EnsureGoogleContactDeleted(match.GoogleContact));
// sync
sync.MatchContacts();
ContactsMatcher.SyncContacts(sync);
// check if google contact still exists
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
Assert.IsNull(match);
Log.Information($"*** TestSyncDeletedOulook ***");
}
[Test]
public void TestSyncDeletedGoogle()
{
Log.Information($"*** TestSyncDeletedGoogle ***");
//ToDo: Check for each SyncOption and SyncDelete combination
sync.SyncOption = SyncOption.MergeOutlookWins;
sync.SyncDelete = true;
// create new contact to sync
var oc = new OutlookContactBuilder().Build();
oc.FullName = TEST_CONTACT_NAME;
oc.FileAs = TEST_CONTACT_NAME;
oc.Email1Address = TEST_CONTACT_EMAIL;
oc.Email2Address = TEST_CONTACT_EMAIL.Replace("00", "01");
oc.Email3Address = TEST_CONTACT_EMAIL.Replace("00", "02");
oc.HomeAddress = "10 Parades";
oc.PrimaryTelephoneNumber = "123";
oc.Save();
var gc = new GoogleContactBuilder().Build();
sync.UpdateContact(oc, gc);
var match = new ContactMatch(new OutlookContactInfo(oc, sync), gc);
match.GoogleContactDirty = true;
//save contacts
sync.SaveContact(match);
// delete google contact
sync.GooglePeopleResource.DeleteContact(match.GoogleContact.ResourceName).Execute();
// sync
sync.MatchContacts();
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
ContactsMatcher.SyncContact(match, sync);
// delete
sync.SaveContact(match);
// sync
sync.MatchContacts();
ContactsMatcher.SyncContacts(sync);
_ = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
Log.Information($"*** TestSyncDeletedGoogle ***");
}
[Test]
public void TestGooglePhoto()
{
Log.Information($"*** TestGooglePhoto ***");
sync.SyncOption = SyncOption.MergeOutlookWins;
Assert.IsTrue(File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\SamplePic.jpg"));
// create new contact to sync
var oc = new OutlookContactBuilder().Build();
oc.FullName = TEST_CONTACT_NAME;
oc.FileAs = TEST_CONTACT_NAME;
oc.Email1Address = TEST_CONTACT_EMAIL;
oc.Email2Address = TEST_CONTACT_EMAIL.Replace("00", "01");
oc.Email3Address = TEST_CONTACT_EMAIL.Replace("00", "02");
oc.HomeAddress = "10 Parades";
oc.PrimaryTelephoneNumber = "123";
oc.Save();
var gc = new GoogleContactBuilder().Build();
sync.UpdateContact(oc, gc);
var match = new ContactMatch(new OutlookContactInfo(oc, sync), gc);
match.GoogleContactDirty = true;
//save contact to google.
sync.SaveGoogleContact(match);
Assert.IsTrue(EnsureGoogleContactSaved(match.GoogleContact));
Assert.IsNotNull(match.GoogleContact.ResourceName);
//load the same contact from google.
sync.MatchContacts();
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
Assert.IsNotNull(match.GoogleContact);
Assert.IsNotNull(match.GoogleContact.ResourceName);
ContactsMatcher.SyncContact(match, sync);
var pic = Utilities.CropImageGoogleFormat(Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "\\SamplePic.jpg"));
Assert.IsNotNull(match.GoogleContact.ResourceName);
var saved = sync.SaveGooglePhoto(match.GoogleContact, new Bitmap(pic));
Assert.IsTrue(saved);
Assert.IsTrue(EnsureGoogleContactHasPhoto(match.GoogleContact));
sync.MatchContacts();
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
ContactsMatcher.SyncContact(match, sync);
var image = Utilities.GetGoogleContactPhoto(sync, match.GoogleContact);
Assert.IsNotNull(image);
oc.Delete();
if (oc != null)
{
Marshal.ReleaseComObject(oc);
}
DeleteTestContact(match.GoogleContact);
Log.Information($"*** TestGooglePhoto ***");
}
[Test]
public void TestOutlookPhoto()
{
Log.Information($"*** TestOutlookPhoto ***");
sync.SyncOption = SyncOption.MergeOutlookWins;
Assert.IsTrue(File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\SamplePic.jpg"));
// create new contact to sync
var oc = new OutlookContactBuilder().Build();
oc.FullName = TEST_CONTACT_NAME;
oc.FileAs = TEST_CONTACT_NAME;
oc.Email1Address = TEST_CONTACT_EMAIL;
oc.Email2Address = TEST_CONTACT_EMAIL.Replace("00", "01");
oc.Email3Address = TEST_CONTACT_EMAIL.Replace("00", "02");
oc.HomeAddress = "10 Parades";
oc.PrimaryTelephoneNumber = "123";
oc.Save();
var saved = oc.SetOutlookPhoto(AppDomain.CurrentDomain.BaseDirectory + "\\SamplePic.jpg");
Assert.IsTrue(saved);
oc.Save();
var image = oc.GetOutlookPhoto();
Assert.IsNotNull(image);
oc.Delete();
Log.Information($"*** TestOutlookPhoto ***");
}
[Test]
public void TestSyncPhoto()
{
Log.Information($"*** TestSyncPhoto ***");
sync.LoadContacts();
sync.SyncOption = SyncOption.MergeOutlookWins;
Assert.IsTrue(File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\SamplePic.jpg"));
// create new contact to sync
var oc = new OutlookContactBuilder().Build();
oc.FullName = TEST_CONTACT_NAME;
oc.FileAs = TEST_CONTACT_NAME;
oc.Email1Address = TEST_CONTACT_EMAIL;
oc.Email2Address = TEST_CONTACT_EMAIL.Replace("00", "01");
oc.Email3Address = TEST_CONTACT_EMAIL.Replace("00", "02");
oc.HomeAddress = "10 Parades";
oc.PrimaryTelephoneNumber = "123";
oc.SetOutlookPhoto(AppDomain.CurrentDomain.BaseDirectory + "\\SamplePic.jpg");
oc.Save();
// outlook contact should now have a photo
Assert.IsNotNull(oc.GetOutlookPhoto());
var gc = new GoogleContactBuilder().Build();
sync.UpdateContact(oc, gc);
var match = new ContactMatch(new OutlookContactInfo(oc, sync), gc);
match.GoogleContactDirty = true;
//save contact to google.
sync.SaveContact(match);
Assert.IsTrue(EnsureGoogleContactSaved(match.GoogleContact));
//load the same contact from google.
sync.MatchContacts();
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
ContactsMatcher.SyncContact(match, sync);
// google contact should now have a photo
Assert.IsNotNull(Utilities.GetGoogleContactPhoto(sync, match.GoogleContact));
// delete outlook contact
oc.Delete();
if (oc != null)
{
Marshal.ReleaseComObject(oc);
}
//DeleteTestPerson(match.GoogleContact);
// recreate outlook contact
oc = new OutlookContactBuilder().Build();
// outlook contact should now have no photo
Assert.IsNull(oc.GetOutlookPhoto());
sync.UpdateContact(match.GoogleContact, oc);
match = new ContactMatch(new OutlookContactInfo(oc, sync), match.GoogleContact);
//match.OutlookContact.Save();
//save contact to outlook
sync.SaveContact(match);
// outlook contact should now have a photo
Assert.IsNotNull(oc.GetOutlookPhoto());
oc.Delete();
if (oc != null)
{
Marshal.ReleaseComObject(oc);
}
DeleteTestContact(match.GoogleContact);
Log.Information($"*** TestSyncPhoto ***");
}
[Test]
public void TestSyncLargePerson()
{
Log.Information($"*** TestSyncLargePerson ***");
sync.LoadContacts();
sync.SyncOption = SyncOption.MergeOutlookWins;
// create new contact to sync
var oc = new OutlookContactBuilder().Build();
oc.FullName = TEST_CONTACT_NAME;
oc.FileAs = TEST_CONTACT_NAME;
oc.Email1Address = TEST_CONTACT_EMAIL;
oc.Email2Address = TEST_CONTACT_EMAIL.Replace("00", "01");
oc.Email3Address = TEST_CONTACT_EMAIL.Replace("00", "02");
oc.HomeAddress = "10 Parades";
oc.PrimaryTelephoneNumber = "123";
oc.Body = new string('*', 150000);
oc.Save();
var gc = new GoogleContactBuilder().Build();
sync.UpdateContact(oc, gc);
var match = new ContactMatch(new OutlookContactInfo(oc, sync), gc);
match.GoogleContactDirty = true;
//save contact to google.
sync.SaveContact(match);
Assert.IsNull(match.GoogleContact);
oc.Delete();
if (oc != null)
{
Marshal.ReleaseComObject(oc);
}
Log.Information($"*** TestSyncLargePerson ***");
}
[Test]
public void TestSyncContactModifiedToBeLarge()
{
Log.Information($"*** TestSyncContactModifiedToBeLarge ***");
//sync.LoadContacts();
//Arrange
sync.SyncOption = SyncOption.MergeOutlookWins;
// create new contact to sync
var oc = new OutlookContactBuilder().Build();
oc.FullName = TEST_CONTACT_NAME;
oc.FileAs = TEST_CONTACT_NAME;
oc.Email1Address = TEST_CONTACT_EMAIL;
oc.Email2Address = TEST_CONTACT_EMAIL.Replace("00", "01");
oc.Email3Address = TEST_CONTACT_EMAIL.Replace("00", "02");
oc.HomeAddress = "10 Parades";
oc.PrimaryTelephoneNumber = "123";
oc.Save();
var oid = ContactPropertiesUtils.GetOutlookId(oc);
if (oc != null)
{
Marshal.ReleaseComObject(oc);
}
sync.MatchContacts();
ContactsMatcher.SyncContacts(sync);
sync.SaveContacts(sync.Contacts);
DelayBetweenSync();
oc = Synchronizer.OutlookNameSpace.GetItemFromID(oid);
oc.Body = new string('*', 63000);
oc.Save();
if (oc != null)
{
Marshal.ReleaseComObject(oc);
}
DelayBetweenSync();
//Act
sync.MatchContacts();
ContactsMatcher.SyncContacts(sync);
sync.SaveContacts(sync.Contacts);
Assert.AreEqual(1, sync.Contacts.Count);
Assert.AreEqual(0, sync.GoogleContacts.Count);
//Assert
Assert.IsNotNull(oid);
oc = Synchronizer.OutlookNameSpace.GetItemFromID(oid);
Assert.IsNotNull(oc);
Assert.AreEqual(new string('*', 63000), oc.Body);
var gid = ContactPropertiesUtils.GetOutlookGoogleId(oc);
Assert.IsNotNull(gid);
var gc = RetrievePerson(gid);
Assert.IsNotNull(gc);
var bio = ContactPropertiesUtils.GetGoogleBiography(gc);
Assert.IsNotNull(bio);
Assert.AreEqual(new string('*', 63000), bio.Value);
DeleteTestContact(oc);
if (oc != null)
{
Marshal.ReleaseComObject(oc);
}
DeleteTestContact(gc);
Log.Information($"*** TestSyncContactModifiedToBeLarge ***");
}
[Test]
public void TestSyncContactModifiedToBeLargeAndFail()
{
Log.Information($"*** TestSyncContactModifiedToBeLargeAndFail ***");
//sync.LoadContacts();
//Arrange
sync.SyncOption = SyncOption.MergeOutlookWins;
// create new contact to sync
var oc = new OutlookContactBuilder().Build();
oc.FullName = TEST_CONTACT_NAME;
oc.FileAs = TEST_CONTACT_NAME;
oc.Email1Address = TEST_CONTACT_EMAIL;
oc.Email2Address = TEST_CONTACT_EMAIL.Replace("00", "01");
oc.Email3Address = TEST_CONTACT_EMAIL.Replace("00", "02");
oc.HomeAddress = "10 Parades";
oc.PrimaryTelephoneNumber = "123";
oc.Save();
var oid = ContactPropertiesUtils.GetOutlookId(oc);
if (oc != null)
{
Marshal.ReleaseComObject(oc);
}
sync.MatchContacts();
ContactsMatcher.SyncContacts(sync);
sync.SaveContacts(sync.Contacts);
DelayBetweenSync();
oc = Synchronizer.OutlookNameSpace.GetItemFromID(oid);
oc.Body = new string('*', 150000);
oc.Save();
if (oc != null)
{
Marshal.ReleaseComObject(oc);
}
DelayBetweenSync();
//Act
sync.MatchContacts();
ContactsMatcher.SyncContacts(sync);
sync.SaveContacts(sync.Contacts);
Assert.AreEqual(1, sync.Contacts.Count);
Assert.AreEqual(0, sync.GoogleContacts.Count);
//Assert
Assert.IsNotNull(oid);
oc = Synchronizer.OutlookNameSpace.GetItemFromID(oid);
Assert.IsNotNull(oc);
Assert.AreEqual(new string('*', 150000), oc.Body);
var gid = ContactPropertiesUtils.GetOutlookGoogleId(oc);
Assert.IsNotNull(gid);
var gc = RetrievePerson(gid);
Assert.IsNotNull(gc);
var bio = ContactPropertiesUtils.GetGoogleBiography(gc);
Assert.IsNull(bio);
DeleteTestContact(oc);
if (oc != null)
{
Marshal.ReleaseComObject(oc);
}
DeleteTestContact(gc);
Log.Information($"*** TestSyncContactModifiedToBeLargeAndFail ***");
}
[Test]
public void TestSyncGroups()
{
Log.Information($"*** TestSyncGroups ***");
Assert.IsTrue(EnsureGoogleGroupsCount(initialGoogleGroupCount));
sync.SyncOption = SyncOption.MergeOutlookWins;
// create new contact to sync
var oc = new OutlookContactBuilder().Build();
oc.FullName = TEST_CONTACT_NAME;
oc.FileAs = TEST_CONTACT_NAME;
oc.Email1Address = TEST_CONTACT_EMAIL;
oc.Email2Address = TEST_CONTACT_EMAIL.Replace("00", "01");
oc.Email3Address = TEST_CONTACT_EMAIL.Replace("00", "02");
oc.HomeAddress = "10 Parades";
oc.PrimaryTelephoneNumber = "123";
oc.Categories = TEST_GROUP;
oc.Save();
//Outlook contact should now have a group
Assert.AreEqual(TEST_GROUP, oc.Categories);
//Sync GoogleGroups first
sync.MatchContacts();
Assert.AreEqual(initialGoogleContactCount + 1, sync.Contacts.Count);
Assert.AreEqual(initialGoogleGroupCount, CountGoogleGroups());
ContactsMatcher.SyncGroups(sync);
Assert.AreEqual(initialGoogleGroupCount + 1, CountGoogleGroups());
Assert.IsTrue(EnsureGoogleGroupExist(TEST_GROUP));
var gc = new GoogleContactBuilder().Build();
sync.UpdateContact(oc, gc);
Assert.AreEqual(1, gc.Memberships.Count);
var match = new ContactMatch(new OutlookContactInfo(oc, sync), gc);
match.GoogleContactDirty = true;
//sync and save contact to google.
ContactsMatcher.SyncContact(match, sync);
Assert.AreEqual(1, match.GoogleContact.Memberships.Count);
sync.SaveContact(match);
Assert.IsTrue(EnsureGoogleContactHasGroups(match.GoogleContact, 1));
//load the same contact from google.
sync.MatchContacts();
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
ContactsMatcher.SyncContact(match, sync);
// google contact should now have the same group
Assert.IsTrue(EnsureGoogleContactHasGroups(match.GoogleContact, 1));
var googleContactGroups = Utilities.GetGoogleGroups(sync, match.GoogleContact);
Assert.AreEqual(1, googleContactGroups.Count);
Assert.Contains(sync.GetGoogleGroupByName(TEST_GROUP), googleContactGroups);
//Assert.Contains(sync.GetGoogleGroupByName(Synchronizer.myContactsGroup), googleContactGroups);
// delete outlook contact
oc.Delete();
DeleteTestContact(match.GoogleContact);
Log.Information($"*** TestSyncGroups ***");
}
[Test]
public void TestSyncDeletedGoogleContactGroup()
{
Log.Information($"*** TestSyncDeletedGoogleContactGroup ***");
//ToDo: Check for each SyncOption and SyncDelete combination
sync.SyncOption = SyncOption.MergeOutlookWins;
sync.SyncDelete = true;
// create new contact to sync
var oc = new OutlookContactBuilder().Build();
oc.FullName = TEST_CONTACT_NAME;
oc.FileAs = TEST_CONTACT_NAME;
oc.Email1Address = TEST_CONTACT_EMAIL;
oc.Email2Address = TEST_CONTACT_EMAIL.Replace("00", "01");
oc.Email3Address = TEST_CONTACT_EMAIL.Replace("00", "02");
oc.HomeAddress = "10 Parades";
oc.PrimaryTelephoneNumber = "123";
oc.Categories = TEST_GROUP;
oc.Save();
//Outlook contact should now have a group
Assert.AreEqual(TEST_GROUP, oc.Categories);
//Sync ContactGroups first
sync.MatchContacts();
ContactsMatcher.SyncGroups(sync);
Assert.IsTrue(EnsureGoogleGroupExist(TEST_GROUP));
//Create now Google Person and assing new ContactGroup
var gc = new GoogleContactBuilder().Build();
sync.UpdateContact(oc, gc);
var match = new ContactMatch(new OutlookContactInfo(oc, sync), gc);
match.GoogleContactDirty = true;
//save contact to google.
sync.SaveContact(match);
Assert.IsTrue(EnsureGoogleContactSaved(match.GoogleContact));
Assert.AreEqual(2, match.GoogleContact.Memberships.Count);
//load the same contact from google.
sync.MatchContacts();
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
Assert.IsNotNull(match.GoogleContact);
Assert.IsNotNull(match.OutlookContact);
Assert.AreEqual(2, match.GoogleContact.Memberships.Count);
ContactsMatcher.SyncContact(match, sync);
// google contact should now have the same group
var googleContactGroups = Utilities.GetGoogleGroups(sync, match.GoogleContact);
Assert.AreEqual(1, googleContactGroups.Count);
var group = sync.GetGoogleGroupByName(TEST_GROUP);
Assert.Contains(group, googleContactGroups);
//Assert.Contains(sync.GetGoogleGroupByName(Synchronizer.myContactsGroup), googleContactGroups);
// delete group from google contact
Utilities.RemoveGoogleGroup(match.GoogleContact, group);
googleContactGroups = Utilities.GetGoogleGroups(sync, match.GoogleContact);
Assert.AreEqual(0, googleContactGroups.Count);
//Assert.Contains(sync.GetGoogleGroupByName(Synchronizer.myContactsGroup), googleContactGroups);
//save contact to google.
sync.SaveGoogleContact(match.GoogleContact);
Assert.IsTrue(EnsureGoogleContactSaved(match.GoogleContact));
Assert.IsTrue(EnsureGoogleContactHasGroups(match.GoogleContact, 0));
sync.SyncOption = SyncOption.GoogleToOutlookOnly;
//Sync ContactGroups first
sync.MatchContacts();
ContactsMatcher.SyncGroups(sync);
//sync and save contact to outlook.
var etag = match.GoogleContact.ETag;
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
sync.UpdateContact(match.GoogleContact, oc);
sync.SaveContact(match);
Assert.IsTrue(EnsureGoogleContactUpdated(match.GoogleContact, etag));
Assert.AreEqual(1, match.GoogleContact.Memberships.Count);
// google and outlook should now have no category except for the System ContactGroup: My Contacts
googleContactGroups = Utilities.GetGoogleGroups(sync, match.GoogleContact);
Assert.AreEqual(0, googleContactGroups.Count);
Assert.AreEqual(null, oc.Categories);
//Assert.Contains(sync.GetGoogleGroupByName(Synchronizer.myContactsGroup), googleContactGroups);
// delete test group
if (group != null)
{
var groupsResource = new ContactGroupsResource(sync.GooglePeopleService);
groupsResource.Delete(group.ResourceName);
}
oc.Delete();
DeleteTestContact(match.GoogleContact);
Log.Information($"*** TestSyncDeletedGoogleContactGroup ***");
}
[Test]
public void TestSyncDeletedOutlookContactGroup()
{
Log.Information($"*** TestSyncDeletedOutlookContactGroup ***");
//ToDo: Check for eache SyncOption and SyncDelete combination
sync.SyncOption = SyncOption.MergeOutlookWins;
sync.SyncDelete = true;
// create new contact to sync
var oc = new OutlookContactBuilder().Build();
oc.FullName = TEST_CONTACT_NAME;
oc.FileAs = TEST_CONTACT_NAME;
oc.Email1Address = TEST_CONTACT_EMAIL;
oc.Email2Address = TEST_CONTACT_EMAIL.Replace("00", "01");
oc.Email3Address = TEST_CONTACT_EMAIL.Replace("00", "02");
oc.HomeAddress = "10 Parades";
oc.PrimaryTelephoneNumber = "123";
oc.Categories = TEST_GROUP;
oc.Save();
//Outlook contact should now have a group
Assert.AreEqual(TEST_GROUP, oc.Categories);
//Now sync ContactGroups
sync.MatchContacts();
ContactsMatcher.SyncGroups(sync);
var gc = new GoogleContactBuilder().Build();
sync.UpdateContact(oc, gc);
var match = new ContactMatch(new OutlookContactInfo(oc, sync), gc);
match.GoogleContactDirty = true;
//save contact to google.
sync.SaveContact(match);
Assert.IsTrue(EnsureGoogleContactSaved(match.GoogleContact));
//load the same contact from google.
sync.MatchContacts();
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
ContactsMatcher.SyncContact(match, sync);
// google contact should now have the same group
var googleContactGroups = Utilities.GetGoogleGroups(sync, match.GoogleContact);
var group = sync.GetGoogleGroupByName(TEST_GROUP);
Assert.AreEqual(1, googleContactGroups.Count);
//Assert.Contains(sync.GetGoogleGroupByName(Synchronizer.myContactsGroup), googleContactGroups);
Assert.Contains(group, googleContactGroups);
// delete group from outlook
Utilities.RemoveOutlookGroup(oc, TEST_GROUP);
//save contact to google.
sync.SaveContact(match);
//load the same contact from google.
sync.MatchContacts();
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
sync.UpdateContact(oc, match.GoogleContact, match);
// google and outlook should now have no category
googleContactGroups = Utilities.GetGoogleGroups(sync, match.GoogleContact);
Assert.AreEqual(null, oc.Categories);
Assert.AreEqual(0, googleContactGroups.Count);
//Assert.Contains(sync.GetGoogleGroupByName(Synchronizer.myContactsGroup), googleContactGroups);
// delete test group
if (group != null)
{
var groupsResource = new ContactGroupsResource(sync.GooglePeopleService);
groupsResource.Delete(group.ResourceName);
}
oc.Delete();
DeleteTestContact(match.GoogleContact);
Log.Information($"*** TestSyncDeletedOutlookContactGroup ***");
}
[Test]
public void TestResetMatches()
{
Log.Information($"*** TestResetMatches ***");
sync.SyncOption = SyncOption.MergeOutlookWins;
// create new contact to sync
var oc = new OutlookContactBuilder().Build();
oc.FullName = TEST_CONTACT_NAME;
oc.FileAs = TEST_CONTACT_NAME;
oc.Email1Address = TEST_CONTACT_EMAIL;
oc.Email2Address = TEST_CONTACT_EMAIL.Replace("00", "01");
oc.Email3Address = TEST_CONTACT_EMAIL.Replace("00", "02");
oc.HomeAddress = "10 Parades";
oc.PrimaryTelephoneNumber = "123";
oc.Save();
var gc = new GoogleContactBuilder().Build();
sync.UpdateContact(oc, gc);
var match = new ContactMatch(new OutlookContactInfo(oc, sync), gc);
match.GoogleContactDirty = true;
//save contact to google.
sync.SaveContact(match);
Assert.IsTrue(EnsureGoogleContactSaved(match.GoogleContact));
//load the same contact from google.
sync.MatchContacts();
Assert.IsNotNull(sync.GoogleContacts);
Assert.AreEqual(initialGoogleContactCount, sync.GoogleContacts.Count);
Assert.IsNotNull(sync.OutlookContacts);
Assert.AreEqual(1, sync.OutlookContacts.Count);
Assert.IsNotNull(sync.Contacts);
Assert.AreEqual(initialGoogleContactCount + 1, sync.Contacts.Count);
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
Assert.IsNotNull(match.GoogleContact);
Assert.IsNotNull(match.OutlookContact);
ContactsMatcher.SyncContact(match, sync);
// delete outlook contact
oc.Delete();
//load the same contact from google
sync.MatchContacts();
Assert.IsNotNull(sync.GoogleContacts);
Assert.AreEqual(initialGoogleContactCount + 1, sync.GoogleContacts.Count);
Assert.IsNotNull(sync.OutlookContacts);
Assert.AreEqual(0, sync.OutlookContacts.Count);
Assert.IsNotNull(sync.Contacts);
Assert.AreEqual(initialGoogleContactCount + 1, sync.Contacts.Count);
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
Assert.IsNull(match.OutlookContact);
Assert.IsNotNull(match.GoogleContact);
ContactsMatcher.SyncContact(match, sync);
// reset matches
var etag = match.GoogleContact.ETag;
sync.ResetMatch(match.GoogleContact);
Assert.IsTrue(EnsureGoogleContactUpdated(match.GoogleContact, etag));
// load same contact match
sync.MatchContacts();
Assert.IsNotNull(sync.GoogleContacts);
Assert.AreEqual(initialGoogleContactCount + 1, sync.GoogleContacts.Count);
Assert.IsNotNull(sync.OutlookContacts);
Assert.AreEqual(0, sync.OutlookContacts.Count);
Assert.IsNotNull(sync.Contacts);
Assert.AreEqual(initialGoogleContactCount + 1, sync.Contacts.Count);
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
Assert.IsNull(match.OutlookContact);
Assert.IsNotNull(match.GoogleContact);
ContactsMatcher.SyncContact(match, sync);
Assert.IsNotNull(match.GoogleContact, "google contact should still be present");
Assert.IsNotNull(match.OutlookContact, "OutlookContact should be recreated");
DeleteTestContacts(match);
Assert.IsTrue(EnsureGoogleContactDeleted(match.GoogleContact));
// create new contact to sync
oc = new OutlookContactBuilder().Build();
oc.FullName = TEST_CONTACT_NAME;
oc.FileAs = TEST_CONTACT_NAME;
oc.Email1Address = TEST_CONTACT_EMAIL;
oc.Email2Address = TEST_CONTACT_EMAIL.Replace("00", "01");
oc.Email3Address = TEST_CONTACT_EMAIL.Replace("00", "02");
oc.HomeAddress = "10 Parades";
oc.PrimaryTelephoneNumber = "123";
oc.Save();
// same test for delete google contact...
gc = new GoogleContactBuilder().Build();
sync.UpdateContact(oc, gc);
match = new ContactMatch(new OutlookContactInfo(oc, sync), gc);
match.GoogleContactDirty = true;
//save contact to google.
sync.SaveContact(match);
Assert.IsTrue(EnsureGoogleContactSaved(match.GoogleContact));
//load the same contact from google.
sync.MatchContacts();
Assert.IsNotNull(sync.GoogleContacts);
Assert.AreEqual(initialGoogleContactCount, sync.GoogleContacts.Count);
Assert.IsNotNull(sync.OutlookContacts);
Assert.AreEqual(1, sync.OutlookContacts.Count);
Assert.IsNotNull(sync.Contacts);
Assert.AreEqual(initialGoogleContactCount + 1, sync.Contacts.Count);
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
Assert.IsNotNull(match.OutlookContact);
Assert.IsNotNull(match.GoogleContact);
ContactsMatcher.SyncContact(match, sync);
Assert.IsNotNull(match.OutlookContact);
Assert.IsNotNull(match.GoogleContact);
// delete google contact
sync.GooglePeopleResource.DeleteContact(match.GoogleContact.ResourceName).Execute();
Assert.IsTrue(EnsureGoogleContactDeleted(match.GoogleContact));
//load the same contact from google.
sync.MatchContacts();
Assert.IsNotNull(sync.GoogleContacts);
Assert.AreEqual(initialGoogleContactCount, sync.GoogleContacts.Count);
Assert.IsNotNull(sync.OutlookContacts);
Assert.AreEqual(1, sync.OutlookContacts.Count);
Assert.IsNotNull(sync.Contacts);
Assert.AreEqual(initialGoogleContactCount + 1, sync.Contacts.Count);
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
ContactsMatcher.SyncContact(match, sync);
Assert.IsNotNull(match.OutlookContact);
Assert.IsNull(match.GoogleContact);
// reset matches
//Not, because null: sync.ResetMatch(match.GoogleContact);
sync.ResetMatch(match.OutlookContact.GetOriginalItemFromOutlook());
// load same contact match
sync.MatchContacts();
Assert.IsNotNull(sync.GoogleContacts);
Assert.AreEqual(initialGoogleContactCount, sync.GoogleContacts.Count);
Assert.IsNotNull(sync.OutlookContacts);
Assert.AreEqual(1, sync.OutlookContacts.Count);
Assert.IsNotNull(sync.Contacts);
Assert.AreEqual(initialGoogleContactCount + 1, sync.Contacts.Count);
match = sync.ContactByProperty(TEST_CONTACT_NAME, TEST_CONTACT_EMAIL);
Assert.IsNotNull(match.OutlookContact);
Assert.IsNull(match.GoogleContact);
ContactsMatcher.SyncContact(match, sync);
// Outlook contact should still be present and GooglePerson should be filled
Assert.IsNotNull(match.OutlookContact);
Assert.IsNotNull(match.GoogleContact);
oc.Delete();
DeleteTestContact(match.GoogleContact);
Log.Information($"*** TestResetMatches ***");
}
private void DeleteTestContacts(ContactMatch match)
{
if (match != null)
{
DeleteTestContact(match.GoogleContact);
Assert.IsTrue(EnsureGoogleContactDeleted(match.GoogleContact));
DeleteTestContact(match.OutlookContact);
}
}
private void DeleteTestContact(Outlook.ContactItem oc)
{
if (oc != null)
{
var name = oc.FileAs;
oc.Delete();
Log.Information($"Deleted Outlook test contact: {name}");
}
}
private void DeleteTestContact(OutlookContactInfo oc)
{
if (oc != null)
{
DeleteTestContact(oc.GetOriginalItemFromOutlook());
}
}
private void DeleteTestContact(Person gc1)
{
if (gc1 != null && gc1.Metadata != null && !(gc1.Metadata.Deleted ?? false) && !string.IsNullOrEmpty(gc1.ResourceName))
{
try
{
sync.GooglePeopleResource.DeleteContact(gc1.ResourceName).Execute();
var googleUniqueIdentifierName = ContactPropertiesUtils.GetGoogleUniqueIdentifierName(gc1);
Log.Information($"Deleted Google test contact: {googleUniqueIdentifierName}");
}
catch (Google.GoogleApiException)
{
try
{
var request = sync.GooglePeopleResource.Get(gc1.ResourceName);
request.PersonFields = Synchronizer.GET_PERSON_FIELDS;
var gc2 = request.Execute();
if (gc2 != null && !(gc2.Metadata.Deleted ?? false))
{
sync.GooglePeopleResource.DeleteContact(gc2.ResourceName).Execute();
Log.Information($"Deleted Google test contact: {ContactPropertiesUtils.GetGoogleUniqueIdentifierName(gc1)}");
}
}
catch (Exception e1)
{
Log.Information(e1, "Exception");
}
}
catch (Exception e2)
{
Log.Information(e2, "Exception");
}
}
}
private void DeleteTestContactGroup(ContactGroup g)
{
if (g != null && g.Metadata != null && !(g.Metadata.Deleted ?? false))
{
try
{
var groupsResource = new ContactGroupsResource(sync.GooglePeopleService);
groupsResource.Delete(g.ResourceName).Execute();
Log.Information($"Deleted Google test group: {g.Name}");
}
catch (Exception e1)
{
Log.Information(e1, "Exception");
}
}
}
internal ContactMatch FindMatch(Outlook.ContactItem oc)
{
foreach (var match in sync.Contacts)
{
if (match.OutlookContact != null && match.OutlookContact.EntryID == oc.EntryID)
{
return match;
}
}
return null;
}
private void DeleteGoogleTestContacts()
{
foreach (var gc in sync.GoogleContacts)
{
var pm = ContactPropertiesUtils.GetGooglePrimaryEmailValue(gc);
var unstructuredName = ContactPropertiesUtils.GetGoogleUnstructuredName(gc);
var googleFileAs = ContactPropertiesUtils.GetGoogleFileAsValue(gc);
var org = ContactPropertiesUtils.GetGooglePrimaryOrganizationName(gc);
if (gc != null &&
((pm == TEST_CONTACT_EMAIL) ||
googleFileAs == TEST_CONTACT_NAME ||
unstructuredName == TEST_CONTACT_NAME ||
org == TEST_CONTACT_NAME))
{
DeleteTestContact(gc);
Assert.IsTrue(EnsureGoogleContactDeleted(gc));
}
}
}
private void DeleteOutlookTestContacts()
{
var oc = sync.OutlookContacts.Find($"[Email1Address] = '{TEST_CONTACT_EMAIL}'") as Outlook.ContactItem;
while (oc != null)
{
DeleteTestContact(oc);
oc = sync.OutlookContacts.Find($"[Email1Address] = '{TEST_CONTACT_EMAIL}'") as Outlook.ContactItem;
}
oc = sync.OutlookContacts.Find($"[FileAs] = '{TEST_CONTACT_NAME}'") as Outlook.ContactItem;
while (oc != null)
{
DeleteTestContact(oc);
oc = sync.OutlookContacts.Find($"[FileAs] = '{TEST_CONTACT_NAME}'") as Outlook.ContactItem;
}
oc = sync.OutlookContacts.Find($"[FileAs] = '{TEST_SAVE_AS}'") as Outlook.ContactItem;
while (oc != null)
{
DeleteTestContact(oc);
oc = sync.OutlookContacts.Find($"[FileAs] = '{TEST_SAVE_AS}'") as Outlook.ContactItem;
}
}
private void DeleteTestContacts()
{
sync.LoadContacts();
initialGoogleContactCount = sync.GoogleContacts.Count;
DeleteOutlookTestContacts();
//for (var i = 1; i < defaultWaitTries; i++)
//{
DeleteGoogleTestContacts();
//Assert.IsTrue(EnsureAllGoogleContactsDeleted())
// break;
//}
sync.LoadContacts();
initialGoogleContactCount = sync.GoogleContacts.Count;
// Assert.AreEqual(0, sync.GoogleContacts.Count);
Assert.AreEqual(0, sync.OutlookContacts.Count);
//To be on the safe side: Call again
//DeleteGoogleTestContacts();
//sync.LoadContacts();
initialGoogleContactCount = sync.GoogleContacts.Count;
}
private bool EnsureGoogleContactHasPhoto(Person gc)
{
for (var i = 1; i < defaultWaitTries; i++)
{
//var query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"))
//{
// NumberToRetrieve = 256,
// StartIndex = 0,
// ContactGroup = defaultContactGroup.Id
//};
var id = gc.ResourceName;
//sync.GooglePeopleRequest.PageToken = null;
//do
//{
// var response = sync.GooglePeopleRequest.Execute();
// foreach (var a in response.Connections)
// {
// if (id.Equals(a.ResourceName))
// {
// if (Utilities.HasContactPhoto(a))
// {
// return true;
// }
// }
// }
// sync.GooglePeopleRequest.PageToken = response.NextPageToken;
//} while (!string.IsNullOrEmpty(sync.GooglePeopleRequest.PageToken));
var request = sync.GooglePeopleResource.Get(id);
request.PersonFields = Synchronizer.GET_PERSON_FIELDS;
var a = request.Execute();
if (a != null && Utilities.HasContactPhoto(a))
return true;
var t = (int)(Math.Pow(2.0, i - 1) * defaultWait);
Log.Information($"EnsureGoogleContactHasPhoto: sleeping for {t}ms");
Thread.Sleep(t);
}
return false;
}
private bool EnsureGoogleContactHasGroups(Person gc, int groups)
{
for (var i = 1; i < defaultWaitTries; i++)
{
var contact = RetrievePerson(ContactPropertiesUtils.GetGoogleId(gc));
if (contact != null)
{
if (CountGoogleGroups(contact) == groups)
{
return true;
}
else
{
Log.Information($"Found {contact.Memberships.Count} groups");
}
}
var t = (int)(Math.Pow(2.0, i - 1) * defaultWait);
Log.Information($"EnsureGoogleContactHasContactGroups: sleeping for {t}ms");
Thread.Sleep(t);
}
return false;
}
private bool EnsureGoogleGroupExist(string gn)
{
for (var i = 1; i < defaultWaitTries; i++)
{
if (RetrieveContactGroup(gn))
{
return true;
}
var t = (int)(Math.Pow(2.0, i - 1) * defaultWait);
Log.Information($"EnsureGoogleContactGroupSaved: sleeping for {t}ms");
Thread.Sleep(t);
}
return false;
}
private bool EnsureGoogleContactSaved(Person gc)
{
for (var i = 1; i < defaultWaitTries; i++)
{
if (RetrievePerson(ContactPropertiesUtils.GetGoogleId(gc)) != null)
{
return true;
}
var t = (int)(Math.Pow(2.0, i - 1) * defaultWait);
Log.Information($"EnsureGoogleContactSaved: sleeping for {t}ms");
Thread.Sleep(t);
}
return false;
}
private bool EnsureGoogleContactDeleted(Person gc)
{
for (var i = 1; i < defaultWaitTries; i++)
{
if (RetrievePerson(ContactPropertiesUtils.GetGoogleId(gc)) == null)
{
return true;
}
var t = (int)(Math.Pow(2.0, i - 1) * defaultWait);
Log.Information($"EnsureGoogleContactDeleted: sleeping for {t}ms");
Thread.Sleep(t);
}
return false;
}
//private bool EnsureAllGoogleContactsDeleted()
//{
// foreach (var gc in sync.GoogleContacts)
// {
// //for (var i = 1; i < defaultWaitTries; i++)
// //{
// if (RetrievePerson(gc.ResourceName) != null)
// {
// return false;
// }
// //var t = (int)(Math.Pow(2.0, i - 1) * defaultWait);
// //Log.Information($"EnsureGoogleContactUpdated: sleeping for {t}ms");
// //Thread.Sleep(t);
// //}
// }
// return true;
//}
private bool EnsureGoogleContactUpdated(Person gc, string etag)
{
for (var i = 1; i < defaultWaitTries; i++)
{
if (RetrieveContactIfUpdated(gc, etag))
{
return true;
}
var t = (int)(Math.Pow(2.0, i - 1) * defaultWait);
Log.Information($"EnsureGoogleContactUpdated: sleeping for {t}ms");
Thread.Sleep(t);
}
return false;
}
private bool EnsureGoogleGroupsCount(int count)
{
for (var i = 1; i < defaultWaitTries; i++)
{
if (CountGoogleGroups() == count)
{
return true;
}
var t = (int)(Math.Pow(2.0, i - 1) * defaultWait);
Log.Information($"EnsureGoogleGroupsCount: sleeping for {t}ms");
Thread.Sleep(t);
}
return false;
}
private bool RetrieveContactIfUpdated(Person gc, string etag)
{
//var query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"))
//{
// NumberToRetrieve = 256,
// StartIndex = 0,
// ContactGroup = defaultContactGroup.Id
//};
var id = gc.ResourceName;
//ToDo:Unfortunately not the same as the Alternative below, because same Resource is returned, but different eTag
//var request = sync.GooglePeopleResource.Get(id);
//request.PersonFields = Synchronizer.GET_PERSON_FIELDS;
//var a = request.Execute();
//if (a != null && a.ETag != etag)
// return true;
//Alternative, safer but also slower: Load all Google Contacts and go thru to check eTag
sync.GooglePeopleRequest.PageToken = null;
do
{
var response = sync.GooglePeopleRequest.Execute();
if (response.Connections != null)
{
foreach (var a in response.Connections)
{
if (id.Equals(a.ResourceName))
if (a.ETag != etag)
return true;
}
}
sync.GooglePeopleRequest.PageToken = response.NextPageToken;
} while (!string.IsNullOrEmpty(sync.GooglePeopleRequest.PageToken));
return false;
}
//private bool RetrievePerson(Person gc)
//{
// //var query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"))
// //{
// // NumberToRetrieve = 256,
// // StartIndex = 0,
// // ContactGroup = defaultContactGroup.Id
// //};
// var id = gc.ResourceName;
// var feed = sync.GooglePeopleResource.Connections.List("people/me").Execute();
// //while (feed != null)
// //{
// foreach (var a in feed.Connections)
// {
// if (id.Equals(a.ResourceName))
// {
// return true;
// }
// }
// //query.StartIndex += query.NumberToRetrieve;
// //feed = sync.ContactsRequest.Get(feed, FeedRequestType.Next);
// //}
// return false;
//}
private Person RetrievePerson(string id)
{
var request = sync.GooglePeopleRequest;
request.PersonFields = Synchronizer.GET_PERSON_FIELDS;
request.PageToken = null;
do
{
var response = request.Execute();
if (response.Connections != null)
{
foreach (var a in response.Connections)
{
if (a.Metadata != null && !(a.Metadata.Deleted ?? false) )
{
var a_id = ContactPropertiesUtils.GetGoogleId(a);
if (!string.IsNullOrEmpty(id) && id.Equals(a_id, StringComparison.InvariantCultureIgnoreCase))
{
return a;
}
}
}
}
request.PageToken = response.NextPageToken;
} while (!string.IsNullOrEmpty(request.PageToken));
return null;
}
/*private Person RetrievePerson(string resourceName)
{
try
{
//var u = new Uri(gid.Replace("http://", "https://"));
var request = sync.GooglePeopleResource.Get(resourceName);
request.PersonFields = Synchronizer.GET_PERSON_FIELDS;
var response = request.Execute();
if (response == null || (response.Metadata != null && (response.Metadata.Deleted??false)))
return null;
return response;
}
catch (Exception ex)
{
Log.Information(ex, "Exception");
return null;
}
}*/
private bool RetrieveContactGroup(string gn)
{
//var query = new ContactGroupsQuery(ContactGroupsQuery.CreateContactGroupsUri("default"))
//{
// NumberToRetrieve = 256,
// StartIndex = 0
//};
var groupsResource = new ContactGroupsResource(sync.GooglePeopleService);
var groupsRequest = groupsResource.List();
groupsRequest.PageToken = null;
do
{
var response = groupsRequest.Execute();
if (response.ContactGroups != null)
{
foreach (var a in response.ContactGroups)
{
if (a.Name == gn)
{
return true;
}
}
}
groupsRequest.PageToken = response.NextPageToken;
} while (!string.IsNullOrEmpty(groupsRequest.PageToken));
return false;
}
private void DeleteTestContactGroups()
{
//var query = new ContactGroupsQuery(ContactGroupsQuery.CreateContactGroupsUri("default"))
//{
// NumberToRetrieve = 256,
// StartIndex = 0
//};
var groupsResource = new ContactGroupsResource(sync.GooglePeopleService);
var groupsRequest = groupsResource.List();
groupsRequest.PageToken = null;
do
{
var response = groupsRequest.Execute();
if (response.ContactGroups != null)
{
foreach (var a in response.ContactGroups)
{
if (IsTestContactGroup(a))
{
DeleteTestContactGroup(a);
}
}
}
groupsRequest.PageToken = response.NextPageToken;
} while (!string.IsNullOrEmpty(groupsRequest.PageToken));
}
private int CountGoogleGroups()
{
//var query = new ContactGroupsQuery(ContactGroupsQuery.CreateContactGroupsUri("default"))
//{
// NumberToRetrieve = 256,
// StartIndex = 0
//};
var count = 0;
var groupsResource = new ContactGroupsResource(sync.GooglePeopleService);
var groupsRequest = groupsResource.List();
groupsRequest.PageToken = null;
do
{
var response = groupsRequest.Execute();
if (response.ContactGroups != null)
{
foreach (var a in response.ContactGroups)
{
if (a.Metadata != null && !(a.Metadata.Deleted ?? false) && a.ResourceName != Synchronizer.myContactsGroup)
{
count++;
}
}
}
groupsRequest.PageToken = response.NextPageToken;
} while (!string.IsNullOrEmpty(groupsRequest.PageToken));
return count;
}
private int CountGoogleGroups(Person a)
{
var ret = 0;
if (a.Memberships != null)
foreach (var group in a.Memberships)
if (group.ContactGroupMembership.ContactGroupResourceName != Synchronizer.myContactsGroup)
ret++;
return ret;
}
private void DeleteAppointmentsForTestContacts()
{
//Also delete the birthday/anniversary entries in Outlook calendar
Log.Information("Deleting Outlook calendar TEST entries (birthday, anniversary) ...");
try
{
var outlookNamespace = Synchronizer.OutlookApplication.GetNamespace("mapi");
var calendarFolder = outlookNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
var outlookCalendarItems = calendarFolder.Items;
for (var i = outlookCalendarItems.Count; i > 0; i--)
{
var item = outlookCalendarItems[i] as Outlook.AppointmentItem;
if (item.Subject.Contains(TEST_CONTACT_NAME))
{
var subject = item.Subject;
item.Delete();
Log.Information($"Deleted Outlook calendar TEST entry: {subject}");
}
}
}
catch (Exception ex)
{
Log.Information($"Could not delete Outlook calendar TEST entries: {ex.Message}");
}
}
private bool IsTestContactGroup(ContactGroup g)
{
return g.Name == TEST_GROUP;
}
private void DelayBetweenSync()
{
//we need to wait at least 3 minutes, so next synchronization will not ignore updates
Thread.Sleep(3 * 60 * 1000);
}
}
}