Exemple #1
0
        public void TestAttributeSchemaConstructorMock()
        {
            MockRecordsetIntegration integration = new MockRecordsetIntegration(new MockDataRecordset());

            Dictionary <int, AttributeSchema> attrschdict = integration.BuildAttributeSchemaDictionary();

            AttributeSchema ability     = attrschdict[1];
            AttributeSchema bonus       = attrschdict[2];
            AttributeSchema description = attrschdict[3];

            Assert.AreEqual <int>(1, ability.Id);
            Assert.AreEqual <string>("ability", ability.Name);
            Assert.AreEqual <bool>(true, ability.IsRequired);
            Assert.AreEqual <int>(1, ability.Multiplicity);

            Assert.AreEqual <int>(2, bonus.Id);
            Assert.AreEqual <string>("bonus", bonus.Name);
            Assert.AreEqual <bool>(true, bonus.IsRequired);
            Assert.AreEqual <int>(1, bonus.Multiplicity);

            Assert.AreEqual <int>(3, description.Id);
            Assert.AreEqual <string>("description", description.Name);
            Assert.AreEqual <bool>(false, description.IsRequired);
            Assert.AreEqual <int>(1, description.Multiplicity);
        }
Exemple #2
0
        public void TestAttributeSchemaUpsert()
        {
            String                  conn              = GMGameManager.Properties.Settings.Default.ConnString;
            SqlDataIntegration      dataIntegration   = null;
            SqlDataRecordset        records           = null;
            SqlRecordsetIntegration recordIntegration = null;
            AttributeSchema         Schema;
            bool result;

            try
            {
                dataIntegration = new SqlDataIntegration(conn);
            }
            catch (Exception ex)
            {
                Assert.Fail("Fail at connection: " + ex.Message);
            }

            Schema                   = new AttributeSchema();
            Schema.Name              = "weight";
            Schema.IsRequired        = true;
            Schema.Multiplicity      = 1;
            Schema.PropertySchema.Id = 1;

            result = dataIntegration.SendActionRequest(SqlDataIntegration.RecordType.AttributeSchema, SqlDataIntegration.RequestType.Upsert,
                                                       SqlRecordsetIntegration.ParameterizeAttributeSchema(Schema));

            Assert.IsTrue(result);
        }
Exemple #3
0
        internal void AddAttribute(string attributeName, string attributeType)
        {
            if (string.IsNullOrEmpty(attributeType))
            {
                throw new ArgumentException(string.Format("Attribute type must be specified", "AttributeType"));
            }

            var existingAttr = AttributeSchema.FirstOrDefault(attr => attr.AttributeName.Equals(attributeName, StringComparison.OrdinalIgnoreCase));

            if (existingAttr != null)
            {
                if (!attributeType.Equals(existingAttr.AttributeType, StringComparison.OrdinalIgnoreCase))
                {
                    throw new ArgumentException(string.Format("An attribute with name {0} had been defined previously but conflicting type ({1} vs {2}) has already been defined.",
                                                              attributeName, existingAttr.AttributeType, attributeType),
                                                "AttributeName");
                }

                return;
            }

            AttributeSchema.Add(new AttributeDefinition {
                AttributeName = attributeName, AttributeType = attributeType.ToUpper()
            });
        }
Exemple #4
0
        public void TestUnknownAttribute()
        {
            var             typeFullName = typeof(SensitiveAttribute).AssemblyQualifiedName;
            string          json         = @"
                {
                  ""TypeName"": """ + typeFullName + @""",
                  ""Data"": {
                    ""TypeId"": """ + typeFullName + @"""
                  }
                }";
            AttributeSchema attr         = JsonConvert.DeserializeObject <AttributeSchema>(json);

            Assert.IsNotNull(attr.Attribute);

            json = @"
                {
                  ""TypeName"": ""Gigya.Microdot.ServiceContract.UnitTests.HttpService.SensitiveAttribute2, Gigya.Microdot.ServiceContract.UnitTests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"",
                  ""Data"": {
                    ""TypeId"": """ + typeFullName + @"""
                  }
                }";
            attr = JsonConvert.DeserializeObject <AttributeSchema>(json);
            Assert.IsNull(attr.Attribute);
            Assert.IsTrue(attr.TypeName == "Gigya.Microdot.ServiceContract.UnitTests.HttpService.SensitiveAttribute2, Gigya.Microdot.ServiceContract.UnitTests, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");
        }
Exemple #5
0
        private Action <TCase> GenerateSetter(AttributeSchema attributeSchema, AttributeDefinition attributeDefinition)
        {
            Func <Delegate, Action <TCase> > factory = SetterFactory <int>;

            var setter = DynamicCodeUtils.GenerateSetter <TCase>(attributeDefinition);

            return(DynamicCodeUtils.DelegateInvoke(factory, attributeDefinition.Type, setter, this));

            Action <TCase> SetterFactory <T>(Delegate generatedSetter)
            {
                if (!(generatedSetter is Setter <TCase, T> setterDelegate))
                {
                    throw new InvalidOperationException("Encountered incorrect setter type.");
                }

                var getter = cursor.GetAttributeGetter <T>(attributeSchema);

                return(target =>
                {
                    T value = default;
                    getter(ref value);
                    setterDelegate(target, value);
                });
            }
        }
Exemple #6
0
 private void CloneAttributeSchema(IEnumerable <AttributeDefinition> sourceSchema)
 {
     AttributeSchema.AddRange(sourceSchema.Select(attr => new AttributeDefinition
     {
         AttributeName = new string(attr.AttributeName.ToCharArray()),
         AttributeType = new DynamoDBv2.ScalarAttributeType(attr.AttributeType)
     }));
 }
Exemple #7
0
        public static void CheckCompatibility(this ICase targetCase, AttributeSchema attribute)
        {
            var caseAttribute = targetCase.Schema.TryGetAttribute(attribute.Name) ??
                                throw new ArgumentException($"Cases are incompatible, attribute {attribute.Name} is missing.");

            if (caseAttribute.Type != attribute.Type)
            {
                throw new ArgumentException($"Cases are incompatible, expected {attribute.Type} type for attribute {attribute.Name}.");
            }
        }
 /// <summary>
 /// Constructs an array of SqlDataParameter objects representing the
 /// attributes
 /// </summary>
 /// <param name="Schema"></param>
 /// <returns></returns>
 public static SqlDataParameter[] ParameterizeAttributeSchema(AttributeSchema Schema)
 {
     return(new SqlDataParameter[] {
         new SqlDataParameter("@AttributeSchemaId", Schema.Id),
         new SqlDataParameter("@Name", Schema.Name),
         new SqlDataParameter("@IsRequired", Schema.IsRequired),
         new SqlDataParameter("@Multiplicity", Schema.Multiplicity),
         new SqlDataParameter("@PropertySchemaId", Schema.PropertySchema.Id)
     });
 }
Exemple #9
0
        /// <summary>
        /// Adds a new local secondary index or updates an index if it has been defined already
        /// </summary>
        /// <param name="indexName"></param>
        /// <param name="rangeKeyName"></param>
        /// <param name="rangeKeyDataType"></param>
        /// <param name="projectionType"></param>
        /// <param name="nonKeyAttributes"></param>
        internal void SetLocalSecondaryIndex(string indexName, string rangeKeyName, string rangeKeyDataType, string projectionType = null, string[] nonKeyAttributes = null)
        {
            // to add additional range keys, the user invokes this cmdlet multiple times in the
            // pipeline
            bool creatingNewIndex = false;
            var  lsi = LocalSecondaryIndexSchema.FirstOrDefault(l => l.IndexName.Equals(indexName, StringComparison.OrdinalIgnoreCase));

            if (lsi == null)
            {
                creatingNewIndex = true;
                lsi = new LocalSecondaryIndex
                {
                    IndexName = indexName,
                    KeySchema = new List <KeySchemaElement>()
                };
            }

            if (AttributeSchema.FirstOrDefault(a => a.AttributeName.Equals(rangeKeyName, StringComparison.Ordinal)) == null)
            {
                // could validate that data type matches here
                AttributeSchema.Add(new AttributeDefinition {
                    AttributeName = rangeKeyName, AttributeType = rangeKeyDataType
                });
            }

            lsi.KeySchema.Add(new KeySchemaElement {
                AttributeName = rangeKeyName, KeyType = Amazon.DynamoDBv2.KeyType.RANGE
            });

            if (!string.IsNullOrEmpty(projectionType))
            {
                lsi.Projection = new Projection();

                if (_projectionTypes.Contains(projectionType, StringComparer.OrdinalIgnoreCase))
                {
                    lsi.Projection.ProjectionType = projectionType.ToUpper();
                    if (nonKeyAttributes != null && nonKeyAttributes.Length > 0)
                    {
                        lsi.Projection.NonKeyAttributes.AddRange(nonKeyAttributes);
                    }
                }
                else
                {
                    throw new ArgumentException(string.Format("Invalid ProjectionType '{0}'; allowed values: {1}.",
                                                              projectionType,
                                                              string.Join(", ", _projectionTypes)),
                                                "ProjectionType");
                }
            }

            if (creatingNewIndex)
            {
                LocalSecondaryIndexSchema.Add(lsi);
            }
        }
Exemple #10
0
        public AttributeGetter <T> GetAttributeGetter <T>(AttributeSchema attribute)
        {
            var getter = getters[attribute.Index];

            if (!(getter is AttributeGetter <T> getterDelegate))
            {
                throw new InvalidOperationException("Encountered incorrect getter type.");
            }

            return(getterDelegate);
        }
        private AttributeSchema FillAttributeSchema(Dictionary <string, string> Fields)
        {
            AttributeSchema attrsch;

            attrsch              = new AttributeSchema();
            attrsch.Id           = Int32.Parse(Fields[AttributeSchemaNames.Id]);
            attrsch.Name         = Fields[AttributeSchemaNames.Name];
            attrsch.IsRequired   = Boolean.Parse(Fields[AttributeSchemaNames.IsRequired]);
            attrsch.Multiplicity = Int32.Parse(Fields[AttributeSchemaNames.Multiplicity]);

            return(attrsch);
        }
        /// <summary>
        /// Fills an attribute schema object with values stored in the stored
        /// SQL recordset.
        /// </summary>
        /// <param name="AttrSchema">The attribute schema to hydrate.</param>
        /// <returns>The hydrated attribute schema.</returns>
        private AttributeSchema FillAttributeSchema(DataRow Fields)
        {
            AttributeSchema attrsch = new AttributeSchema();

            attrsch.Id                = (int)Fields[AttributeSchemaNames.Id];
            attrsch.Name              = Fields[AttributeSchemaNames.Name].ToString();
            attrsch.IsRequired        = (bool)Fields[AttributeSchemaNames.IsRequired];
            attrsch.Multiplicity      = (int)Fields[AttributeSchemaNames.Multiplicity];
            attrsch.PropertySchema.Id = (int)Fields[AttributeSchemaNames.PropertySchemaId];

            return(attrsch);
        }
Exemple #13
0
        public static T GetAttribute <T>(this ICase targetCase, AttributeSchema attribute)
        {
            if (targetCase == null)
            {
                throw new ArgumentNullException(nameof(targetCase));
            }

            T   value  = default;
            var getter = targetCase.GetAttributeGetter <T>(attribute);

            getter(ref value);
            return(value);
        }
        public override Dictionary <int, AttributeSchema> BuildAttributeSchemaDictionary()
        {
            MockDataRecordset           rst = (MockDataRecordset)_rst;
            Dictionary <string, string> attrsch;

            for (int i = 0; i < rst.AttributeSchemaRecords.Values.Count; i++)
            {
                attrsch = rst.AttributeSchemaRecords.Values.ElementAt <Dictionary <string, string> >(i);
                AttributeSchema a = FillAttributeSchema(attrsch);
                _attrschlist.Add(a.Id, a);
            }

            return(_attrschlist);
        }
Exemple #15
0
        public override AttributeGetter <T> GetAttributeGetter <T>(AttributeSchema attribute)
        {
            if (!binding.TryGetBinding(attribute.Index, out var index))
            {
                return(cursor.GetAttributeGetter <T>(attribute));
            }

            var getter = getters[index];

            if (!(getter is AttributeGetter <T> getterDelegate))
            {
                throw new InvalidOperationException("Encountered incorrect getter type.");
            }

            return(getterDelegate);
        }
Exemple #16
0
        internal void AddKey(string keyName, string keyType, string dataType)
        {
            var keyElement = KeySchema.FirstOrDefault(key => key.AttributeName.Equals(keyName, StringComparison.OrdinalIgnoreCase));

            if (keyElement != null)
            {
                throw new ArgumentException(string.Format("A key with name '{0}' has already been defined in the schema.", keyName), "KeyName");
            }

            // if the key doesn't already exist as an attribute, add it
            var attribute = AttributeSchema.FirstOrDefault(a => a.AttributeName.Equals(keyName, StringComparison.OrdinalIgnoreCase));

            if (attribute == null)
            {
                if (string.IsNullOrEmpty(dataType))
                {
                    throw new ArgumentException("An attribute for the key was not found in the supplied schema. The data type is needed before it can be added automatically.", "DataType");
                }

                AttributeSchema.Add(new AttributeDefinition {
                    AttributeName = keyName, AttributeType = dataType
                });
            }

            keyElement = new KeySchemaElement
            {
                AttributeName = keyName,
                KeyType       = keyType
            };

            // allow for user possibly defining keys in any order; DDB requires the primary hash key to be first
            // and there may be multiple hash keys allowed in future.
            if (!HasDefinedKeys || keyType.Equals(Amazon.DynamoDBv2.KeyType.RANGE, StringComparison.OrdinalIgnoreCase))
            {
                KeySchema.Add(keyElement);
            }
            else if (KeySchema[0].KeyType.Equals(Amazon.DynamoDBv2.KeyType.HASH))
            {
                KeySchema.Add(keyElement);
            }
            else
            {
                KeySchema.Insert(0, keyElement);
            }
        }
        public void InitializeTest()
        {
            ISchemaInfo target = new AdsSchemaInfo();

            using (LdapConnection conn = new LdapConnection("localhost:20389"))
            {
                conn.Bind(System.Net.CredentialCache.DefaultNetworkCredentials);
                target.Initialize(conn);
            }

            int ocs = 0;

            foreach (ObjectClassSchema o in target.ObjectClasses)
            {
                System.Console.WriteLine("oc: {0}", o);
                foreach (AttributeSchema a in o.MustHave)
                {
                    System.Console.WriteLine("  must: {0} as {1}", a, a.LangType);
                }

                foreach (AttributeSchema a in o.MayHave)
                {
                    System.Console.WriteLine("  may : {0} as {1}", a, a.LangType);
                }

                ocs++;
            }

            Assert.IsTrue(ocs >= 10, "At least 10 object classes found");

            ObjectClassSchema user = target.GetObjectClass("USER");

            Assert.IsTrue(user != null, "Found 'USER' (mixed case) objectclass");

            user = target.GetObjectClass("NO-SUCH-THING");
            Assert.IsNull(user);

            AttributeSchema attr = target.GetAttribute("cn");

            Assert.IsNotNull(attr);

            attr = target.GetAttribute("NO-ATTRIBUTE");
            Assert.IsNull(attr);
        }
Exemple #18
0
 public override AttributeGetter <T> GetAttributeGetter <T>(AttributeSchema attribute) => cursor.GetAttributeGetter <T>(attribute);
Exemple #19
0
        public void TestAttributeSchemaConstructorSql()
        {
            String                            conn              = GMGameManager.Properties.Settings.Default.ConnString;
            SqlDataIntegration                dataIntegration   = null;
            SqlDataRecordset                  records           = null;
            SqlRecordsetIntegration           recordIntegration = null;
            Dictionary <int, AttributeSchema> schemas           = null;

            try
            {
                dataIntegration = new SqlDataIntegration(conn);
            }
            catch (Exception ex)
            {
                Assert.Fail("Fail at connection: " + ex.Message);
            }

            try
            {
                records = (SqlDataRecordset)dataIntegration.SendDataRequest(DataIntegration.RecordType.AttributeSchema, null);
                if (records.Dataset.Tables.Count == 0)
                {
                    Assert.Fail("No tables retrieved");
                }
                for (int i = 0; i < records.Dataset.Tables.Count; i++)
                {
                    DataTable table = records.Dataset.Tables[i];
                    if (records.Dataset.Tables[i].Rows.Count == 0)
                    {
                        Assert.Fail(String.Format("Table {0} return no results.",
                                                  records.Dataset.Tables[i].TableName));
                    }
                }
            }
            catch (Exception ex)
            {
                Assert.Fail("Fail at request: " + ex.Message);
            }

            recordIntegration = new SqlRecordsetIntegration(records);
            Assert.IsNotNull(recordIntegration, "Fail - record integration is null.");

            try
            {
                schemas = recordIntegration.BuildAttributeSchemaDictionary();
                Assert.IsNotNull(schemas, "Fail - schemas result is null.");
                if (schemas.Count == 0)
                {
                    Assert.Fail("No records retrieved");
                }
            }
            catch (Exception ex)
            {
                Assert.Fail("Fail at object build: " + ex.Message);
            }

            AttributeSchema ability = schemas[1];
            AttributeSchema bonus   = schemas[2];
            AttributeSchema desc    = schemas[3];

            Assert.AreEqual <string>("ability", ability.Name);
            Assert.AreEqual <string>("bonus", bonus.Name);
            Assert.AreEqual <string>("description", desc.Name);
        }
Exemple #20
0
        /// <summary>
        /// Adds a new global secondary index or updates an index if it has been defined already.
        /// </summary>
        /// <param name="indexName"></param>
        /// <param name="hashKeyName"></param>
        /// <param name="hashKeyDataType"></param>
        /// <param name="rangeKeyName"></param>
        /// <param name="rangeKeyDataType"></param>
        /// <param name="readCapacityUnits"></param>
        /// <param name="writeCapacityUnits"></param>
        /// <param name="projectionType"></param>
        /// <param name="nonKeyAttributes"></param>
        internal void SetGlobalSecondaryIndex(string indexName,
                                              string hashKeyName,
                                              string hashKeyDataType,
                                              string rangeKeyName,
                                              string rangeKeyDataType,
                                              Int64 readCapacityUnits,
                                              Int64 writeCapacityUnits,
                                              string projectionType     = null,
                                              string[] nonKeyAttributes = null)
        {
            // to add additional range keys, the user invokes this cmdlet multiple times in the
            // pipeline
            bool creatingNewIndex = false;
            var  gsi = GlobalSecondaryIndexSchema.FirstOrDefault(g => g.IndexName.Equals(indexName, StringComparison.OrdinalIgnoreCase));

            if (gsi == null)
            {
                creatingNewIndex = true;
                gsi = new GlobalSecondaryIndex
                {
                    IndexName = indexName,
                    KeySchema = new List <KeySchemaElement>()
                };
            }

            // for a GSI, an additional hash key can be defined that does not have to match that used by the table
            if (!string.IsNullOrEmpty(hashKeyName))
            {
                if (AttributeSchema.FirstOrDefault(a => a.AttributeName.Equals(hashKeyName, StringComparison.Ordinal)) == null)
                {
                    // could validate that data type matches here
                    AttributeSchema.Add(new AttributeDefinition {
                        AttributeName = hashKeyName, AttributeType = hashKeyDataType
                    });
                }

                gsi.KeySchema.Add(new KeySchemaElement {
                    AttributeName = hashKeyName, KeyType = Amazon.DynamoDBv2.KeyType.HASH
                });
            }

            // for global indexes, range key is optional (assuming a hash key has been specified); for local indexes the range key is
            // mandatory
            if (!string.IsNullOrEmpty(rangeKeyName))
            {
                if (AttributeSchema.FirstOrDefault(a => a.AttributeName.Equals(rangeKeyName, StringComparison.Ordinal)) == null)
                {
                    // could validate that data type matches here
                    AttributeSchema.Add(new AttributeDefinition {
                        AttributeName = rangeKeyName, AttributeType = rangeKeyDataType
                    });
                }

                gsi.KeySchema.Add(new KeySchemaElement {
                    AttributeName = rangeKeyName, KeyType = Amazon.DynamoDBv2.KeyType.RANGE
                });
            }

            gsi.ProvisionedThroughput = new ProvisionedThroughput
            {
                ReadCapacityUnits  = readCapacityUnits,
                WriteCapacityUnits = writeCapacityUnits
            };

            if (!string.IsNullOrEmpty(projectionType))
            {
                gsi.Projection = new Projection();

                if (_projectionTypes.Contains(projectionType, StringComparer.OrdinalIgnoreCase))
                {
                    gsi.Projection.ProjectionType = projectionType.ToUpper();
                    if (nonKeyAttributes != null && nonKeyAttributes.Length > 0)
                    {
                        gsi.Projection.NonKeyAttributes.AddRange(nonKeyAttributes);
                    }
                }
                else
                {
                    throw new ArgumentException(string.Format("Invalid ProjectionType '{0}'; allowed values: {1}.",
                                                              projectionType,
                                                              string.Join(", ", _projectionTypes)),
                                                "ProjectionType");
                }
            }

            if (creatingNewIndex)
            {
                GlobalSecondaryIndexSchema.Add(gsi);
            }
        }
Exemple #21
0
 public abstract AttributeGetter <T> GetAttributeGetter <T>(AttributeSchema attribute);