public void testParse_throwParserExceptionOnInvalidDataFormat() { var mapper = new DummyMapper(); var parser = new MessageParser(mapper); parser.Parse("event: put\ndata: "); }
public void testParse_shouldExitIfEmpty() { var mapper = new DummyMapper(); var parser = new MessageParser(mapper); parser.Parse(null); Assert.IsNull(mapper.eventData); parser.Parse(""); Assert.IsNull(mapper.eventData); }
public void testParse_shouldParseErrorEvent() { var mapper = new DummyMapper(); var parser = new MessageParser(mapper); string error = "{\"error\":"; parser.Parse(error); Assert.IsNotNull(mapper.eventData); Assert.AreEqual(mapper.eventData.EventType, "error"); Assert.AreEqual(mapper.eventData.Message, error); }
public void TestCheckParameter() { try { var person2 = DummyMapper.ToAnotherPerson(null); } catch (Exception ex) { if (!ex.Message.Equals("El parámetro es nulo y no puede ser mapeado. El parámetro entregado es del tipo DummyPerson, en la clase DummyMapper, en el método ToAnotherPerson")) { throw new Exception("Prueba no superada"); } } }
public void testParse_shouldSkipMalformedMessages() { var mapper = new DummyMapper(); var parser = new MessageParser(mapper); parser.Parse("garbage test"); Assert.IsNull(mapper.eventData); parser.Parse("event: dummy"); Assert.IsNull(mapper.eventData); parser.Parse("event: put"); Assert.IsNull(mapper.eventData); }
public void testParse_shouldParsePutRevokeEvents() { var mapper = new DummyMapper(); var parser = new MessageParser(mapper); string data = "{data}"; parser.Parse("event: put\ndata: " + data); Assert.IsNotNull(mapper.eventData); Assert.AreEqual(mapper.eventData.EventType, "put"); Assert.AreEqual(mapper.eventData.Message, data); parser.Parse("event: auth_revoked\ndata: " + data); Assert.IsNotNull(mapper.eventData); Assert.AreEqual(mapper.eventData.EventType, "auth_revoked"); Assert.AreEqual(mapper.eventData.Message, data); }
public void TestErrorHandler() { try { var person = new DummyPerson() { BirthAt = new DateTime(1985, 4, 5), Email = "*****@*****.**", Name = "Some Name" }; var person2 = DummyMapper.ToAnotherPerson(person); } catch (Exception ex) { if (!ex.Message.Equals("Se ha producido un error inesperado al mapear el objeto. El error se produjo en la clase DummyMapper, método ToAnotherPerson. Revise la pila de excepciones para mayor información")) { throw new Exception("Prueba no superada"); } } }
public static void GetDataByVirtualTableName(SqlInt32 accountID, SqlString virtualTableName, SqlString deliveryOutputID, SqlDateTime dateCreated) { string dbtableName = string.Empty; string sqlAssembly = typeof(Creative).Assembly.FullName; string classNamespace = typeof(Creative).Namespace; DummyMapper mapper = new DummyMapper(); bool isMetaProperty = false; #region Getting Type from table name /****************************************************************/ Type type = GetTypeByTableName(virtualTableName.Value); //Type type = Type.GetType(string.Format("{0}.{1},{2}", classNamespace, virtualTableName.Value, sqlAssembly)); if (type != null) { //Check if subclass of creative / Target / EdgeObject if (type.IsSubclassOf(typeof(Creative))) { dbtableName = typeof(Creative).Name; } else if (type.IsSubclassOf(typeof(Target))) { dbtableName = typeof(Target).Name; } else if (type.IsSubclassOf(typeof(EdgeObject))) { dbtableName = typeof(EdgeObject).Name; } } else // EdgeObject Type { isMetaProperty = true; dbtableName = typeof(EdgeObject).Name; } /****************************************************************/ #endregion //Creating Select by Dummy table name StringBuilder col = new StringBuilder(); col.Append("SELECT "); #region Members by Type Mapper /*******************************************************/ foreach (var mapItem in mapper.Mapping[typeof(Edge.Data.Objects.EdgeObject)]) { col.Append(dbtableName + ".["); col.Append(mapItem.Value); col.Append("] as "); col.Append("["); col.Append(mapItem.Key); col.Append("],"); } if (type != null) { foreach (var mapItem in mapper.Mapping[type]) { col.Append(dbtableName + ".["); col.Append(mapItem.Value); col.Append("] as "); col.Append("["); col.Append(mapItem.Key); col.Append("],"); } } col.Remove(col.Length - 1, 1); //col.Append("[ObjectTracking_Table].[DeliveryOutputID]"); col.Append(string.Format(" From {0}", dbtableName)); //JOIN WITH Meta Property Table //col.Append(" INNER JOIN ObjectTracking [ObjectTracking_Table]"); //col.Append(string.Format(" ON [ObjectTracking_Table].ObjectGK = {0}.GK", dbtableName)); #region Where query string /*****************************************************************/ Int32 metaPropertyID = -1; string baseValueType = string.Empty; col.Append(" WHERE "); if (!isMetaProperty) { col.Append(" [ObjectType] = @objectType "); } //Get data from Meta Property Table else { baseValueType = GetMetaPropertyBaseValueType(virtualTableName.Value, accountID.IsNull == true ? SqlInt32.Null : accountID, out metaPropertyID); if (string.IsNullOrEmpty(baseValueType)) { return; } Type baseType = Type.GetType(string.Format("{0}.{1},{2}", classNamespace, baseValueType, sqlAssembly)); string metaPropertyFieldName = mapper.Mapping[baseType]["MetaPropertyID"]; col.Append(" ObjectType = @ObjectType"); col.Append(string.Format(" AND {0} = @MetaPropertyID ", metaPropertyFieldName)); } col.Append(" AND AccountID in( @accountID ,-1) "); if (!deliveryOutputID.IsNull) { col.Append(" AND deliveryOutputID = @deliveryOutputID"); } if (!dateCreated.IsNull) { col.Append(" AND dateCreated = @dateCreated"); } /*****************************************************************/ #endregion /****************************************************************/ #endregion #region Sql Command and parameters /*******************************************************************************/ SqlCommand cmd = new SqlCommand(col.ToString()); SqlParameter sql_account; SqlParameter sql_OutputID; SqlParameter sql_dateCreated; if (!isMetaProperty) { SqlParameter sql_objectType = new SqlParameter("@objectType", type.Name); cmd.Parameters.Add(sql_objectType); } else { SqlParameter sql_objectBaseType = new SqlParameter("@objectType", baseValueType); cmd.Parameters.Add(sql_objectBaseType); SqlParameter sql_metaPropertyID = new SqlParameter("@MetaPropertyID", metaPropertyID); cmd.Parameters.Add(sql_metaPropertyID); } sql_account = new SqlParameter("@accountID", accountID); cmd.Parameters.Add(sql_account); if (!deliveryOutputID.IsNull) { sql_OutputID = new SqlParameter("@deliveryOutputID", deliveryOutputID); cmd.Parameters.Add(sql_OutputID); } if (!dateCreated.IsNull) { sql_dateCreated = new SqlParameter("@dateCreated", dateCreated); cmd.Parameters.Add(sql_dateCreated); } /*******************************************************************************/ #endregion try { using (SqlConnection conn = new SqlConnection("context connection=true")) { conn.Open(); cmd.Connection = conn; using (SqlDataReader reader = cmd.ExecuteReader()) { SqlContext.Pipe.Send(reader); } } } catch (Exception e) { throw new Exception("Could not get table data", e); } }
public static void GetTableStructureByName(SqlString virtualTableName) { DummyMapper mapper = new DummyMapper(); string classNamespace = typeof(Creative).Namespace; Type type = GetTypeByTableName(virtualTableName.Value); //Creating Select by Dummy table name StringBuilder col = new StringBuilder(); #region Members /*******************************************************/ Dictionary <string, string> tableStructure = GetSqlTableStructure(type); foreach (MemberInfo member in type.GetMembers()) { if (IsRelevant(member)) { string sql_name = string.Empty; string sql_type = string.Empty; string dotNet_name = string.Empty; string dotNet_type = string.Empty; bool isEnum = false; //Verify that memeber is class member of Edge.Data.Object if (((FieldInfo)(member)).FieldType.FullName.Contains(classNamespace)) //Memeber is class member from Edge.Data.Object { //Getting Enum Types if ((((FieldInfo)(member)).FieldType).BaseType == typeof(Enum)) { sql_name = member.Name; sql_type = tableStructure[mapper.GetMap(type, member.Name)]; dotNet_name = member.Name; dotNet_type = ((MemberInfo)(((FieldInfo)(member)).FieldType)).Name; isEnum = true; } //Getting Types that are not Enum if ((((FieldInfo)(member)).FieldType).BaseType != typeof(Enum)) { sql_name = member.Name + "ID"; sql_type = tableStructure[sql_name]; dotNet_name = member.Name + ".ID"; dotNet_type = ((MemberInfo)(((FieldInfo)(member)).FieldType)).Name; } } else { sql_name = mapper.GetMap(type, member.Name); sql_type = tableStructure[sql_name]; dotNet_name = member.Name; dotNet_type = ((MemberInfo)(((FieldInfo)(member)).FieldType)).Name; } //Creating sql select query col.Append(string.Format(" Select '{0}' as 'SQL Name', '{1}' as 'SQL Type', '{2}' as '.Net Name', '{3}' as '.Net Type', '{4}' as 'IsEnum' Union ", sql_name, sql_type, dotNet_name, dotNet_type, isEnum ) ); } } //Removing last "union string" col.Remove(col.Length - 6, 6); //Creating SQL command SqlCommand cmd = new SqlCommand(col.ToString()); try { using (SqlConnection conn = new SqlConnection("Data Source=BI_RND;Initial Catalog=EdgeObjects;Integrated Security=True;Pooling=False")) { conn.Open(); cmd.Connection = conn; using (SqlDataReader reader = cmd.ExecuteReader()) { SqlContext.Pipe.Send(reader); } } } catch (Exception e) { throw new Exception("Could not get table data", e); } /****************************************************************/ #endregion }
public static void GetTableStructureByName(SqlString virtualTableName) { string sqlAssembly = typeof(Creative).Assembly.FullName; string classNamespace = typeof(Creative).Namespace; DummyMapper mapper = new DummyMapper(); #region Getting Type from table name /****************************************************************/ Type type = Type.GetType(string.Format("{0}.{1},{2}", classNamespace, virtualTableName.Value, sqlAssembly)); if (type == null) { Int32 metaPropertyID = 0; string baseValueType = GetMetaPropertyBaseValueType(virtualTableName.Value, SqlInt32.Null, out metaPropertyID); if (string.IsNullOrEmpty(baseValueType)) { return; } type = Type.GetType(string.Format("{0}.{1},{2}", classNamespace, baseValueType, sqlAssembly)); } /****************************************************************/ #endregion //Creating Select by Dummy table name StringBuilder col = new StringBuilder(); #region Members /*******************************************************/ Dictionary <string, string> tableStructure = GetTableStructure(type); foreach (MemberInfo member in type.GetMembers()) { if (IsRelevant(member)) { //Get Params and types string sql_name = string.Empty; string sql_type = string.Empty; string dotNet_name = string.Empty; string dotNet_type = string.Empty; bool isEnum = false; if (((FieldInfo)(member)).FieldType.FullName.Contains(classNamespace)) //Memeber is class member from Edge.Data.Object { //Getting Enum Types if ((((FieldInfo)(member)).FieldType).BaseType == typeof(Enum)) { sql_name = member.Name; sql_type = tableStructure[mapper.GetMap(type, member.Name)]; dotNet_name = member.Name; dotNet_type = ((MemberInfo)(((FieldInfo)(member)).FieldType)).Name; isEnum = true; } //Getting Types that are not Enum if ((((FieldInfo)(member)).FieldType).BaseType != typeof(Enum)) { sql_name = member.Name + "ID"; sql_type = tableStructure[sql_name]; dotNet_name = member.Name + ".ID"; dotNet_type = ((MemberInfo)(((FieldInfo)(member)).FieldType)).Name; } } else { sql_name = mapper.GetMap(type, member.Name); sql_type = tableStructure[sql_name]; dotNet_name = member.Name; dotNet_type = ((MemberInfo)(((FieldInfo)(member)).FieldType)).Name; } //Creating sql select query col.Append(string.Format(" Select '{0}', '{1}', '{2}', '{3}', '{4}' Union ", sql_name, sql_type, dotNet_name, dotNet_type, isEnum ) ); } } //Removing last "union string" col.Remove(col.Length - 5, 5); //Creating SQL command SqlCommand cmd = new SqlCommand(col.ToString()); try { using (SqlConnection conn = new SqlConnection("context connection=true")) { conn.Open(); cmd.Connection = conn; using (SqlDataReader reader = cmd.ExecuteReader()) { SqlContext.Pipe.Send(reader); } } } catch (Exception e) { throw new Exception("Could not get table data", e); } /****************************************************************/ #endregion }
public static void CLR_GetTablesList(SqlInt32 accountID, SqlInt32 channelID) { try { StringBuilder sb = new StringBuilder(); DummyMapper mapper = new DummyMapper(); #region Getting EdgeObjectTypes Tables //**************************************************// foreach (Type type in typeof(Creative).Assembly.GetTypes()) { if (!type.Equals(typeof(Segment)) && !type.IsAbstract) { #region Type of Edge Object if (type.IsSubclassOf(typeof(EdgeObject))) { StringBuilder select = new StringBuilder(); string from = string.Empty; StringBuilder where = new StringBuilder(); //Setting Select statement foreach (var field in mapper.GetFieldsMapping(type)) { select.Append(string.Format("[{0}] as [{1}],", field.Value, field.Key)); } select.Remove(select.Length - 1, 1); //Get table name from class attribute string tableName = ((TableInfoAttribute)Attribute.GetCustomAttribute(type, typeof(TableInfoAttribute))).Name; where.Append(string.Format("AccountID ={0} ", accountID, type.Name)); //Add channel to statement only if type inherit from ChannelSpecificObject class if (type.IsSubclassOf(typeof(ChannelSpecificObject))) { if (channelID.Value != null) { where.Append(string.Format(" AND ChannelID =''{0}'' ", channelID.Value)); } else { where.Append(" AND ChannelID != NULL "); } } if (type != typeof(Ad)) { where.Append(string.Format(" AND ObjectType=''{0}''", type.Name)); } //setting From statement from = GetTableSource(type); sb.Append(string.Format("SELECT '{0}' as TableName,'{1}' as [select],'{2}' as [From], '{3}' as [Where] Union ", tableName, select, from, where)); } #endregion #region non EdgeObject Types with Attribute /*================================================*/ //Getting Other tables such as - Channel, Account ... else if (((TableInfoAttribute)Attribute.GetCustomAttribute(type, typeof(TableInfoAttribute))) != null) { StringBuilder select = new StringBuilder(); string from = string.Empty; StringBuilder where = new StringBuilder(); //Setting Select statement foreach (var field in mapper.GetFieldsMapping(type)) { select.Append(string.Format("[{0}] as [{1}],", field.Value, field.Key)); } select.Remove(select.Length - 1, 1); //setting Where statement where = where.Append(string.Format("AccountID in (-1,{0})", accountID, type.Name)); // where = where.Append(string.Format("AccountID in (-1,{0}) AND ObjectType=''{1}''", accountID, type.Name)); string tableName = ((TableInfoAttribute)Attribute.GetCustomAttribute(type, typeof(TableInfoAttribute))).Name; from = tableName; sb.Append(string.Format("SELECT '{0}' as TableName,'{1}' as [select],'{2}' as [From], '{3}' as [Where] Union ", tableName, select, from, where)); } /*================================================*/ #endregion } } //**************************************************// #endregion #region Getting Tables list from Connection Definition Table /********************************************************************/ //Getting connections from data base List <ConnectionDef> accountConnections = GetConnections(accountID.Value, channelID.Value); foreach (ConnectionDef connection in accountConnections) { StringBuilder select = new StringBuilder(); string fromTable = string.Empty; StringBuilder where = new StringBuilder(); Type type = GetDotNetTypeByName(connection.BaseValue); #region Setting Select statement foreach (var field in mapper.GetFieldsMapping(type)) { if (field.Key != "ConnectionDefinitionID") { select.Append(string.Format("[{0}] as [{1}],", field.Value, field.Key)); } } select.Remove(select.Length - 1, 1); #endregion #region Setting Where statement if (type.IsSubclassOf(typeof(Segment)) || type == typeof(Segment)) { string ConnectionDefinitionIDField = mapper.GetSqlTargetMapFieldName(type, "ConnectionDefinitionID"); where = where.Append(string.Format("AccountID in (-1,{0})", accountID)); //Add channel to statement only if type inherit from ChannelSpecificObject class if (type.IsSubclassOf(typeof(ChannelSpecificObject))) { if (channelID.Value != null) { where.Append(string.Format(" AND ChannelID =''{0}'' ", channelID.Value)); } else { where.Append(" AND ChannelID != NULL "); } where = where.Append(string.Format(" AND ObjectType =''{0}'' AND {1} = {2}", typeof(Segment).Name, ConnectionDefinitionIDField, connection.ID)); } } else { where = where.Append(string.Format("AccountID = {0} ", accountID)); if (channelID.Value != null) { where.Append(string.Format(" AND ChannelID = ''{0}'' ", channelID.Value)); } else { where.Append(" AND ChannelID != NULL "); } where = where.Append(string.Format(" AND ObjectType = ''{0}''", type.Name)); } #endregion fromTable = GetTableSource(type); sb.Append(string.Format("SELECT '{0}' as TableName,'{1}' as [select],'{2}' as [From], '{3}' as [Where] Union ", connection.Name, select, fromTable, where)); } sb.Remove(sb.Length - 6, 6); /********************************************************************/ #endregion SqlCommand cmd = new SqlCommand(sb.ToString()); using (SqlConnection conn = new SqlConnection("context connection=true")) { conn.Open(); cmd.Connection = conn; using (SqlDataReader reader = cmd.ExecuteReader()) { SqlContext.Pipe.Send(reader); } } } catch (Exception e) { throw new Exception("Could not get table list from data object data base " + e.ToString(), e); } }
public static void CLR_GetTableStructure(SqlString virtualTableName) { try { DummyMapper mapper = new DummyMapper(); string classNamespace = typeof(Creative).Namespace; Type type = GetDotNetTypeByName(virtualTableName.Value); //Creating Select by Dummy table name StringBuilder col = new StringBuilder(); #region Members /*******************************************************/ Dictionary <string, string> tableStructure = GetSqlTableStructure(type); foreach (MemberInfo member in type.GetMembers()) { if (IsRelevant(member)) { string sql_name = string.Empty; string sql_type = string.Empty; string dotNet_name = string.Empty; string dotNet_type = string.Empty; string display = string.Empty; bool isEnum = false; string memberNamespace = string.Empty; MemberTypes memberType = member.MemberType; //Get MemberName if (member.MemberType.Equals(MemberTypes.Field)) { memberNamespace = ((FieldInfo)(member)).FieldType.Namespace; } else { memberNamespace = ((PropertyInfo)(member)).PropertyType.Namespace; } //Verify that memeber is class member of Edge.Data.Object if (memberNamespace == typeof(EdgeObject).Namespace || memberNamespace.StartsWith(classNamespace + ".")) //Memeber is class member from Edge.Data.Object { //Getting Enum Types if ((((FieldInfo)(member)).FieldType).BaseType == typeof(Enum)) { sql_name = member.Name; sql_type = tableStructure[mapper.GetSqlTargetMapFieldName(type, member.Name)]; dotNet_name = member.Name; //dotNet_type = ((MemberInfo)(((FieldInfo)(member)).FieldType)).Name; dotNet_type = member.ReflectedType.Name; display = string.Format("{0} {1}", dotNet_name, sql_type); isEnum = true; } //Getting Types that are not Enum but member of Edge.Data.Object if ((((FieldInfo)(member)).FieldType).BaseType != typeof(Enum)) { sql_name = member.Name + "ID"; dotNet_name = member.Name + ".ID"; if (!tableStructure.TryGetValue(sql_name, out sql_type)) { sql_name = member.Name + "GK"; dotNet_name = member.Name + ".GK"; sql_type = tableStructure[sql_name]; } //sql_type = tableStructure.TryGetValue(sql_name); //dotNet_type = ((MemberInfo)(((FieldInfo)(member)).FieldType)).Name; dotNet_type = member.ReflectedType.Name; display = string.Format("{0} {1}", sql_name, sql_type); } } else { //Incase that memeber is not class member of Edge.Data.Object for ex. Budget sql_name = mapper.GetSqlTargetMapFieldName(type, member.Name); sql_type = tableStructure[sql_name]; dotNet_name = member.Name; //dotNet_type = ((MemberInfo)(((FieldInfo)(member)).FieldType)).Name; dotNet_type = member.ReflectedType.Name; display = string.Format("{0} {1}", dotNet_name, sql_type); } //Creating sql select query if (!string.IsNullOrEmpty(sql_name)) { col.Append(string.Format(" Select '{0}' as 'Display Name' Union ", display)); } } } if (col.Length > 0) { //Removing last "union string" col.Remove(col.Length - 6, 6); //Creating SQL command SqlCommand cmd = new SqlCommand(col.ToString()); SqlContext.Pipe.Send(col.ToString()); try { using (SqlConnection conn = new SqlConnection("context connection=true")) { conn.Open(); cmd.Connection = conn; using (SqlDataReader reader = cmd.ExecuteReader()) { SqlContext.Pipe.Send(reader); } } } catch (Exception e) { throw new Exception("Could not get table data - GetTableStructureByName() " + e.ToString(), e); } } /****************************************************************/ #endregion } catch (Exception ex) { SqlContext.Pipe.Send(ex.Message); } }