public void SetUp() { this.dalOutputs = new List <Thing>(); this.sieSiteDirectoryDto = new CDP4Common.DTO.SiteDirectory(Guid.NewGuid(), 22); this.person = new CDP4Common.DTO.Person(Guid.NewGuid(), 22) { ShortName = "John", GivenName = "John", Password = "******", IsActive = true }; var phone1 = new CDP4Common.DTO.TelephoneNumber(Guid.NewGuid(), 22) { Value = "123" }; phone1.VcardType.Add(VcardTelephoneNumberKind.HOME); var phone2 = new CDP4Common.DTO.TelephoneNumber(Guid.NewGuid(), 22) { Value = "456" }; phone2.VcardType.Add(VcardTelephoneNumberKind.WORK); var phone3 = new CDP4Common.DTO.TelephoneNumber(Guid.NewGuid(), 22) { Value = "789" }; phone3.VcardType.Add(VcardTelephoneNumberKind.FAX); this.sieSiteDirectoryDto.Person.Add(this.person.Iid); this.person.TelephoneNumber.Add(phone1.Iid); this.person.TelephoneNumber.Add(phone2.Iid); this.person.TelephoneNumber.Add(phone3.Iid); this.dalOutputs.Add(this.sieSiteDirectoryDto); this.dalOutputs.Add(this.person); this.dalOutputs.Add(phone1); this.dalOutputs.Add(phone2); this.dalOutputs.Add(phone3); this.uri = new Uri("http://www.rheagroup.com/"); var credentials = new Credentials("John", "Doe", this.uri); this.mockedDal = new Mock <IDal>(); this.mockedDal.SetupProperty(d => d.Session); this.session = new Session(this.mockedDal.Object, credentials); var openTaskCompletionSource = new TaskCompletionSource <IEnumerable <Thing> >(); openTaskCompletionSource.SetResult(this.dalOutputs); this.mockedDal.Setup(x => x.Open(It.IsAny <Credentials>(), It.IsAny <CancellationToken>())).Returns(openTaskCompletionSource.Task); }
/// <summary> /// Update a database record from the supplied data transfer object. /// </summary> /// <param name="transaction"> /// The current <see cref="NpgsqlTransaction"/> to the database. /// </param> /// <param name="partition"> /// The database partition (schema) where the requested resource will be updated. /// </param> /// <param name="person"> /// The Person DTO that is to be updated. /// </param> /// <param name="container"> /// The container of the DTO to be updated. /// </param> /// <returns> /// True if the concept was successfully updated. /// </returns> public virtual bool Update(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.Person person, CDP4Common.DTO.Thing container = null) { bool isHandled; var valueTypeDictionaryAdditions = new Dictionary <string, string>(); var beforeUpdate = this.BeforeUpdate(transaction, partition, person, container, out isHandled, valueTypeDictionaryAdditions); if (!isHandled) { beforeUpdate = beforeUpdate && base.Update(transaction, partition, person, container); var valueTypeDictionaryContents = new Dictionary <string, string> { { "GivenName", !this.IsDerived(person, "GivenName") ? person.GivenName.Escape() : string.Empty }, { "IsActive", !this.IsDerived(person, "IsActive") ? person.IsActive.ToString() : string.Empty }, { "IsDeprecated", !this.IsDerived(person, "IsDeprecated") ? person.IsDeprecated.ToString() : string.Empty }, { "OrganizationalUnit", !this.IsDerived(person, "OrganizationalUnit") ? person.OrganizationalUnit.Escape() : null }, { "Password", !this.IsDerived(person, "Password") ? person.Password.Escape() : null }, { "ShortName", !this.IsDerived(person, "ShortName") ? person.ShortName.Escape() : string.Empty }, { "Surname", !this.IsDerived(person, "Surname") ? person.Surname.Escape() : string.Empty }, }.Concat(valueTypeDictionaryAdditions).ToDictionary(kvp => kvp.Key, kvp => kvp.Value); using (var command = new NpgsqlCommand()) { var sqlBuilder = new System.Text.StringBuilder(); sqlBuilder.AppendFormat("UPDATE \"{0}\".\"Person\"", partition); sqlBuilder.AppendFormat(" SET (\"ValueTypeDictionary\", \"Container\", \"DefaultDomain\", \"DefaultEmailAddress\", \"DefaultTelephoneNumber\", \"Organization\", \"Role\")"); sqlBuilder.AppendFormat(" = (:valueTypeDictionary, :container, :defaultDomain, :defaultEmailAddress, :defaultTelephoneNumber, :organization, :role)"); sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;"); command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = person.Iid; command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents; command.Parameters.Add("container", NpgsqlDbType.Uuid).Value = container.Iid; command.Parameters.Add("defaultDomain", NpgsqlDbType.Uuid).Value = !this.IsDerived(person, "DefaultDomain") ? Utils.NullableValue(person.DefaultDomain) : Utils.NullableValue(null); command.Parameters.Add("defaultEmailAddress", NpgsqlDbType.Uuid).Value = !this.IsDerived(person, "DefaultEmailAddress") ? Utils.NullableValue(person.DefaultEmailAddress) : Utils.NullableValue(null); command.Parameters.Add("defaultTelephoneNumber", NpgsqlDbType.Uuid).Value = !this.IsDerived(person, "DefaultTelephoneNumber") ? Utils.NullableValue(person.DefaultTelephoneNumber) : Utils.NullableValue(null); command.Parameters.Add("organization", NpgsqlDbType.Uuid).Value = !this.IsDerived(person, "Organization") ? Utils.NullableValue(person.Organization) : Utils.NullableValue(null); command.Parameters.Add("role", NpgsqlDbType.Uuid).Value = !this.IsDerived(person, "Role") ? Utils.NullableValue(person.Role) : Utils.NullableValue(null); command.CommandText = sqlBuilder.ToString(); command.Connection = transaction.Connection; command.Transaction = transaction; this.ExecuteAndLogCommand(command); } } return(this.AfterUpdate(beforeUpdate, transaction, partition, person, container)); }
public void VerifyThatOpenCallMightBeCancelled() { var tasks = new List <Task>(); var credentials = new Credentials("admin", "pass", new Uri("https://cdp4services-public.cdp4.org")); var adminPerson = new CDP4Common.DTO.Person(Guid.NewGuid(), 22) { ShortName = "admin", GivenName = "admin", Password = "******", IsActive = true }; this.sieSiteDirectoryDto.Person.Add(adminPerson.Iid); this.dalOutputs.Add(adminPerson); this.session = new Session(this.mockedDal.Object, credentials); for (var i = 0; i < 50; i++) { var timeout = i; tasks.Add(Task.Run(async() => { Thread.Sleep(timeout); await this.session.Open(); })); } for (var i = 0; i < 50; i++) { var timeout = i; tasks.Add(Task.Run(() => { Thread.Sleep(timeout); if (this.session.CanCancel()) { this.session.Cancel(); } })); } Assert.DoesNotThrowAsync(async() => { await Task.WhenAll(tasks.ToArray()); }); }
/// <summary> /// Update user credentials after migration /// </summary> /// <param name="transaction">The database transaction.</param> /// <param name="partition">The database schema</param> /// <param name="person">The person <see cref="CDP4Common.DTO.Person" /></param> /// <param name="credentials">The new credentials from migration.json <see cref="MigrationPasswordCredentials" /></param> /// <returns> /// The true if operation finished with success /// </returns> public bool UpdateCredentials(NpgsqlTransaction transaction, string partition, CDP4Common.DTO.Person person, MigrationPasswordCredentials credentials) { var operationSuccess = true; var valueTypeDictionaryContents = new Dictionary <string, string> { { "GivenName", !this.IsDerived(person, "GivenName") ? person.GivenName.Escape() : string.Empty }, { "IsActive", !this.IsDerived(person, "IsActive") ? person.IsActive.ToString() : string.Empty }, { "IsDeprecated", !this.IsDerived(person, "IsDeprecated") ? person.IsDeprecated.ToString() : string.Empty }, { "OrganizationalUnit", !this.IsDerived(person, "OrganizationalUnit") ? person.OrganizationalUnit.Escape() : null }, { "Password", !this.IsDerived(person, "Password") ? credentials.Password.Escape() : null }, { "Salt", !this.IsDerived(person, "Salt") ? credentials.Salt.Escape() : null }, { "ShortName", !this.IsDerived(person, "ShortName") ? person.ShortName.Escape() : string.Empty }, { "Surname", !this.IsDerived(person, "Surname") ? person.Surname.Escape() : string.Empty }, }; try { using (var command = new NpgsqlCommand()) { var sqlBuilder = new System.Text.StringBuilder(); sqlBuilder.AppendFormat("UPDATE \"{0}\".\"Person\"", partition); sqlBuilder.AppendFormat(" SET \"ValueTypeDictionary\" = :valueTypeDictionary"); sqlBuilder.AppendFormat(" WHERE \"Iid\" = :iid;"); command.Parameters.Add("iid", NpgsqlDbType.Uuid).Value = credentials.Iid; command.Parameters.Add("valueTypeDictionary", NpgsqlDbType.Hstore).Value = valueTypeDictionaryContents; command.CommandText = sqlBuilder.ToString(); command.Connection = transaction.Connection; command.Transaction = transaction; this.ExecuteAndLogCommand(command); } } catch { operationSuccess = false; } return(operationSuccess); }
/// <summary> /// The mapping from a database record to data transfer object. /// </summary> /// <param name="reader"> /// An instance of the SQL reader. /// </param> /// <returns> /// A deserialized instance of <see cref="CDP4Common.DTO.Person"/>. /// </returns> public virtual CDP4Common.DTO.Person MapToDto(NpgsqlDataReader reader) { string tempGivenName; string tempIsActive; string tempIsDeprecated; string tempModifiedOn; string tempOrganizationalUnit; string tempPassword; string tempShortName; string tempSurname; string tempThingPreference; var valueDict = (Dictionary <string, string>)reader["ValueTypeSet"]; var iid = Guid.Parse(reader["Iid"].ToString()); var revisionNumber = int.Parse(valueDict["RevisionNumber"]); var dto = new CDP4Common.DTO.Person(iid, revisionNumber); dto.DefaultDomain = reader["DefaultDomain"] is DBNull ? (Guid?)null : Guid.Parse(reader["DefaultDomain"].ToString()); dto.DefaultEmailAddress = reader["DefaultEmailAddress"] is DBNull ? (Guid?)null : Guid.Parse(reader["DefaultEmailAddress"].ToString()); dto.DefaultTelephoneNumber = reader["DefaultTelephoneNumber"] is DBNull ? (Guid?)null : Guid.Parse(reader["DefaultTelephoneNumber"].ToString()); dto.EmailAddress.AddRange(Array.ConvertAll((string[])reader["EmailAddress"], Guid.Parse)); dto.ExcludedDomain.AddRange(Array.ConvertAll((string[])reader["ExcludedDomain"], Guid.Parse)); dto.ExcludedPerson.AddRange(Array.ConvertAll((string[])reader["ExcludedPerson"], Guid.Parse)); dto.Organization = reader["Organization"] is DBNull ? (Guid?)null : Guid.Parse(reader["Organization"].ToString()); dto.Role = reader["Role"] is DBNull ? (Guid?)null : Guid.Parse(reader["Role"].ToString()); dto.TelephoneNumber.AddRange(Array.ConvertAll((string[])reader["TelephoneNumber"], Guid.Parse)); dto.UserPreference.AddRange(Array.ConvertAll((string[])reader["UserPreference"], Guid.Parse)); if (valueDict.TryGetValue("GivenName", out tempGivenName)) { dto.GivenName = tempGivenName.UnEscape(); } if (valueDict.TryGetValue("IsActive", out tempIsActive)) { dto.IsActive = bool.Parse(tempIsActive); } if (valueDict.TryGetValue("IsDeprecated", out tempIsDeprecated)) { dto.IsDeprecated = bool.Parse(tempIsDeprecated); } if (valueDict.TryGetValue("ModifiedOn", out tempModifiedOn)) { dto.ModifiedOn = Utils.ParseUtcDate(tempModifiedOn); } if (valueDict.TryGetValue("OrganizationalUnit", out tempOrganizationalUnit) && tempOrganizationalUnit != null) { dto.OrganizationalUnit = tempOrganizationalUnit.UnEscape(); } if (valueDict.TryGetValue("Password", out tempPassword) && tempPassword != null) { dto.Password = tempPassword.UnEscape(); } if (valueDict.TryGetValue("ShortName", out tempShortName)) { dto.ShortName = tempShortName.UnEscape(); } if (valueDict.TryGetValue("Surname", out tempSurname)) { dto.Surname = tempSurname.UnEscape(); } if (valueDict.TryGetValue("ThingPreference", out tempThingPreference) && tempThingPreference != null) { dto.ThingPreference = tempThingPreference.UnEscape(); } return(dto); }