Exemple #1
0
        public void TrigramInstanceTest()
        {
            var triGram = new Trigram("abc");

            Assert.AreEqual(3, triGram.Length);
            Assert.AreEqual("abc", triGram.Value);
        }
 /// <summary>
 /// Method: ReadFromCSV
 /// Goal: Supporting function that is able to read a csv file line by line and adds it to the Trigram memory list object
 /// </summary>
 /// <param name="storageConnectionString">The azure storage account connection string to access the azure storage account</param>
 /// <param name="tableName">The azure table name to be created</param>
 /// <returns>trigramList -> a list of Model.Trigram objects</returns>
 /// <exception cref="IOException"> Represents an exception thrown by IO system</exception>
 /// <exception cref="StorageException"> Represents an exception thrown by the Azure Storage service</exception>
 public static List <Trigram> ReadFromCSVToTrigramList(string csvPath)
 {
     try
     {
         string[] lines       = File.ReadAllLines(csvPath);
         var      trigramList = new List <AzureStorage.Model.Trigram>();
         int      lineNumber  = 0;
         foreach (string line in lines)
         {
             //Ignoring the first header line
             if (lineNumber == 0)
             {
                 lineNumber++;
                 continue;
             }
             string[] columns = line.Split(',');
             var      trigram = new Trigram(columns[0], columns[1], columns[2]);
             trigramList.Add(trigram);
         }
         return(trigramList);
     }
     catch (IOException e)
     {
         Console.WriteLine($"ReadFromCSV : IOException : {e.Message}");
         Console.ReadLine();
         throw;
     }
     catch (StorageException e)
     {
         Console.WriteLine($"ReadFromCSV : StorageException : {e.Message}");
         Console.ReadLine();
         throw;
     }
 }
Exemple #3
0
        public void CompareToTest()
        {
            var instance       = new Trigram("xyz");
            var higherInstance = new Trigram("abc");
            var equalInstance  = new Trigram("xyz");

            HelpersTest.AssertCompareTo(instance, higherInstance, equalInstance);
        }
Exemple #4
0
 public Hexagram(
     int number,
     string name,
     Trigram innerTrigram,
     Trigram outerTrigram,
     IEnumerable <HexagramLine> lines)
 {
     Number       = number.Check(x => x >= 1 && x <= 64);
     Name         = name.NotNull(nameof(name));
     InnerTrigram = innerTrigram;
     OuterTrigram = outerTrigram;
     Lines        = lines.Check(x => x.Count() == 6).ToList().AsReadOnly();
 }
Exemple #5
0
        public void Trigram_ShouldCreateAnInstanceOfThisType_WhenProperArgument()
        {
            // Arrange
            // Act
            Trigram actual1
                = new Trigram(
                      new TokenizationStrategy(),
                      ObjectMother.LabeledExample_Text1_TrigramValue1
                      );
            Trigram actual2
                = new Trigram(
                      ObjectMother.LabeledExample_Text1_TrigramValue1
                      );

            // Assert
            Assert.IsInstanceOf <Trigram>(actual1);
            Assert.IsInstanceOf <Trigram>(actual2);
        }
        /// <summary>
        /// Method: RetrieveEntityUsingPointQueryAsync
        /// Goal: Retrieve trigram entry from Azure table storage.
        /// </summary>
        /// <param name="table">The azure storage table to query
        /// <param name="partitionKey">The partition key to query the table</param>
        /// <param name="rowKey">The row key to query the table</param>
        /// <param name="log">The ILogger object to log information and errors</param>
        /// <returns>Task<emp_trigram></returns>
        /// <exception cref="StorageException"> Represents an exception thrown by the Azure Storage service</exception>
        public static async Task <Trigram> RetrieveEntityUsingPointQueryAsync(CloudTable table, string partitionKey, string rowKey, ILogger log)
        {
            try
            {
                TableOperation retrieveOperation = TableOperation.Retrieve <Trigram>(partitionKey, rowKey);
                TableResult    result            = await table.ExecuteAsync(retrieveOperation);

                Trigram trigram = result.Result as Trigram;
                string  infoMessage;
                if (trigram != null)
                {
                    infoMessage = $"Triagram Exists: {trigram.RowKey}";
                    log?.LogInformation(infoMessage);
                }
                return(trigram);
            }
            catch (StorageException e)
            {
                log?.LogError($"RetrieveEntityUsingPointQueryAsync: StorageException : {e.Message}");
                throw;
            }
        }
        /// <summary>
        /// Method: InsertOrMergeEntityAsync
        /// Goal: Insert a trigram object into an Azure table account
        /// </summary>
        /// <param name="table">The azure storage table object</param>
        /// <param name="entity">The trigram object to be inserted</param>
        /// <returns>insertedTrigram -> the inserted trigram object</returns>
        /// <exception cref="StorageException"> Represents an exception thrown by the Azure Storage service</exception>
        public static async Task <Trigram> InsertOrMergeEntityAsync(CloudTable table, Trigram entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("Trigram");
            }
            try
            {
                // Create the InsertOrReplace table operation
                TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(entity);
                // Execute the operation.
                TableResult result = await table.ExecuteAsync(insertOrMergeOperation);

                Trigram insertedTrigram = result.Result as Trigram;
                return(insertedTrigram);
            }
            catch (StorageException e)
            {
                Console.WriteLine($"InsertOrMergeEntityAsync : StorageException : {e.Message}");
                Console.ReadLine();
                throw;
            }
        }
Exemple #8
0
        public static async Task Run([EventHubTrigger("%EMP_EVENT_HUB_NAME%", Connection = "EMP_EVENTHUB_CONNECTION_STRING")] EventData[] events, ILogger log, ExecutionContext executionContext)
        {
            var    exceptions = new List <Exception>();
            string infoMessage;
            string errorMessage;

            string jsonSettingsPath = executionContext.FunctionAppDirectory;

            //App Settings from local.settings.json
            string storageConnectionString = ApSettings.LoadAppSettings(jsonSettingsPath, log).EMP_STORAGE_ACCOUNT_CONNECTION_STRING;
            string teamsQueueName          = ApSettings.LoadAppSettings(jsonSettingsPath, log).EMP_STORAGE_ACCOUNT_TEAMS_QUEUE_NAME;
            string errorQueueName          = ApSettings.LoadAppSettings(jsonSettingsPath, log).EMP_STORAGE_ACCOUNT_ERROR_QUEUE_NAME;
            string trigramTableName        = ApSettings.LoadAppSettings(jsonSettingsPath, log).EMP_TRIGRAM_TABLE_NAME;
            string elasticSearchClusterURI = ApSettings.LoadAppSettings(jsonSettingsPath, log).EMP_ELASTIC_SEARCH_CLUSTER_URI;
            string elasticSearchAPIId      = ApSettings.LoadAppSettings(jsonSettingsPath, log).EMP_ELASTIC_SEARCH_API_ID;
            string elasticSearchAPIKey     = ApSettings.LoadAppSettings(jsonSettingsPath, log).EMP_ELASTIC_SEARCH_API_KEY;

            foreach (EventData eventData in events)
            {
                JsonLogEntry logEntry     = null;
                string       webhookUrl   = null;
                bool         validTrigram = false;
                string       messageBody  = null;

                try
                {
                    messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
                    log?.LogInformation($"FROM EVENTHUB {messageBody}");
                    if (string.IsNullOrEmpty(messageBody))
                    {
                        infoMessage = $"Task Run: Event is null";
                        log?.LogInformation(infoMessage);
                    }
                    else
                    {
                        // Validate semantically the json schema
                        logEntry = JsonLogEntry.DeserializeJsonLogEntry(messageBody, log);
                        var(validJson, errorValidation) = JsonLogEntry.ValidateJsonLogEntry(messageBody, log);
                        //Validate Trigram - compare entry from the json log with the trigram azure table storage
                        (validTrigram, webhookUrl) = Trigram.ValidateTrigram(storageConnectionString, logEntry, trigramTableName, log);

                        if (validJson)
                        {
                            infoMessage = $"Task Run: Json Schema valid: {messageBody}";
                            log?.LogInformation(infoMessage);
                            if (validTrigram)
                            {
                                //Insert into ElasticSearch Cluster
                                var elasticLowLevelClient = emp_elastic_operations.ElasticConnect(elasticSearchClusterURI, elasticSearchAPIId, elasticSearchAPIKey, log);
                                await emp_elastic_operations.ElasticPutAsync(elasticLowLevelClient, logEntry, log);
                            }
                            else
                            {
                                // If not successful send to the Azure Storage  Teams queue
                                log?.LogInformation($"Task Run: Application Trigram NOT valid: {messageBody}");
                                CloudQueue cloudQueue = AzureStorageQueueOperations.CreateAzureQueue(storageConnectionString, teamsQueueName, log);
                                var        logQueue   = new QueueLog()
                                {
                                    ErrorMessage = $"Invalid trigram", LogEntry = logEntry, WebhookUrl = webhookUrl
                                };
                                AzureStorageQueueOperations.InsertMessageQueue(cloudQueue, JsonConvert.SerializeObject(logQueue), log);
                            }
                        }
                        else
                        {
                            // If not valid  send to the Azure Storage Teams queue
                            infoMessage = $"Task Run: Json Schema NOT valid: {messageBody}";
                            log?.LogInformation(infoMessage);
                            CloudQueue cloudQueue = AzureStorageQueueOperations.CreateAzureQueue(storageConnectionString, teamsQueueName, log);
                            var        logQueue   = new QueueLog()
                            {
                                ErrorMessage = errorValidation, LogEntry = logEntry, WebhookUrl = webhookUrl
                            };
                            AzureStorageQueueOperations.InsertMessageQueue(cloudQueue, JsonConvert.SerializeObject(logQueue), log);
                        }
                        await Task.Yield();
                    }
                }
                catch (Exception ex)
                {
                    if (logEntry != null)
                    {
                        // send to the Azure Storage teams queue
                        log?.LogInformation($"Task Run: exception raised {ex}");
                        CloudQueue cloudQueue = AzureStorageQueueOperations.CreateAzureQueue(storageConnectionString, teamsQueueName, log);
                        var        logQueue   = new QueueLog()
                        {
                            ErrorMessage = $"{ex}", LogEntry = logEntry, WebhookUrl = webhookUrl
                        };
                        AzureStorageQueueOperations.InsertMessageQueue(cloudQueue, JsonConvert.SerializeObject(logQueue), log);

                        // send to the Azure Storage Error queue
                        log?.LogInformation($"Task Run: exception raised {ex}");
                        cloudQueue = AzureStorageQueueOperations.CreateAzureQueue(storageConnectionString, errorQueueName, log);
                        AzureStorageQueueOperations.InsertMessageQueue(cloudQueue, messageBody, log);
                    }
                }
            }
            // Once processing of the batch is complete, if any messages in the batch failed processing throw an exception so that there is a record of the failure.
            if (exceptions.Count > 1)
            {
                throw new AggregateException(exceptions);
            }

            if (exceptions.Count == 1)
            {
                throw exceptions.Single();
            }
        }
Exemple #9
0
        public void ToStringTest()
        {
            var triGram = new Trigram("abc");

            Assert.AreEqual("abc", triGram.ToString());
        }
Exemple #10
0
 public void TrigramExceptionTest()
 {
     var triGram = new Trigram("abcd");
 }
Exemple #11
0
 public static TrigramElement ToElement(this Trigram trigram) =>
 (TrigramElement)(int)trigram;