WcfTests.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // <copyright file="WcfTests.cs" company="Objective Advantage Europe Ltd.">
  2. //
  3. // Copyright (c) Objective Advantage Europe Ltd. 2011 - 2013 All Right Reserved.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. // </copyright>
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Data;
  21. using System.IO;
  22. using System.ServiceModel;
  23. using Excel;
  24. using Foghorn.Core;
  25. using Foghorn.WcfService;
  26. using NLog;
  27. using Xunit;
  28. namespace Foghorn.Test
  29. {
  30. public class WcfTests
  31. {
  32. private const string ApplicationTestName = "TestApplication";
  33. private const string ServiceUrl = @"http://localhost:8733/Design_Time_Addresses/Foghorn.WcfService/FoghornService/";
  34. private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
  35. [Fact]
  36. public void TestRegisterSendingApplication_LoadNotificationTypesFromExcel_AllLoaded()
  37. {
  38. var foghornClient =
  39. new ChannelFactory<IFoghornService>(new BasicHttpBinding(), new EndpointAddress(ServiceUrl)).CreateChannel();
  40. var notificationTypeDtos = new List<NotificationTypeDto>();
  41. LoadNotificationTypes(notificationTypeDtos);
  42. var sendingApplicationDto = new SendingApplicationDto {SendingApplicationName = ApplicationTestName};
  43. LoadApplicationIcon(sendingApplicationDto);
  44. foghornClient.RegisterSendingApplication(sendingApplicationDto, notificationTypeDtos);
  45. //TODO: Add Assertions
  46. }
  47. [Fact]
  48. public void TestRegisterSubscription_ApplicationExists_SubscriberRegistered()
  49. {
  50. var foghornClient =
  51. new ChannelFactory<IFoghornService>(new BasicHttpBinding(), new EndpointAddress(ServiceUrl)).CreateChannel();
  52. var subscriberDto = new SubscriberDto {HostName = "localhost", Password = "abc123!", SubscriberName = "jbloggs"};
  53. foghornClient.RegisterSubscription(subscriberDto, ApplicationTestName);
  54. //TODO: Add Assertions
  55. }
  56. [Fact]
  57. public void TestNotify_ApplicationExists_NotificationSent()
  58. {
  59. var foghornClient =
  60. new ChannelFactory<IFoghornService>(new BasicHttpBinding(), new EndpointAddress(ServiceUrl)).CreateChannel();
  61. var notificationDto = new NotificationDto
  62. {
  63. NotificationMessage = "This is the message of the test notification.",
  64. NotificationTypeName = "Error",
  65. NotificationTitle = "Notification Title",
  66. Priority = 2,
  67. Sticky = true
  68. };
  69. foghornClient.Notify(notificationDto, ApplicationTestName);
  70. //TODO: Add Assertions
  71. }
  72. private static void LoadApplicationIcon(SendingApplicationDto sendingApplicationDto)
  73. {
  74. Stream iconStream = null;
  75. try
  76. {
  77. iconStream = File.OpenRead(Path.Combine(@"..\..\NotificationIcons", "Icon.png"));
  78. var buffer = new byte[iconStream.Length];
  79. iconStream.Read(buffer, 0, (int) iconStream.Length);
  80. sendingApplicationDto.SendingApplicationIcon = buffer;
  81. }
  82. catch (Exception exception)
  83. {
  84. Logger.LogException(LogLevel.Error, "Exception Ignored", exception);
  85. throw;
  86. }
  87. finally
  88. {
  89. if (iconStream != null) iconStream.Close();
  90. }
  91. }
  92. private static void LoadNotificationTypes(ICollection<NotificationTypeDto> notificationTypeDtos)
  93. {
  94. Stream excelFile = null;
  95. try
  96. {
  97. excelFile = File.OpenRead(@"..\..\NotificationTypes.xlsx");
  98. var excel = ExcelReaderFactory.CreateOpenXmlReader(excelFile);
  99. excel.IsFirstRowAsColumnNames = true;
  100. var excelData = excel.AsDataSet();
  101. var sheet = excelData.Tables["NotificationTypes"];
  102. foreach (DataRow row in sheet.Rows)
  103. {
  104. var notificationTypeDto = new NotificationTypeDto
  105. {
  106. NotificationTypeName = row["NotificationTypeName"].ToString(),
  107. NotificationTypeDisplayName = row["NotificationTypeDescription"].ToString(),
  108. };
  109. Stream notificationIconStream = null;
  110. try
  111. {
  112. notificationIconStream =
  113. File.OpenRead(Path.Combine(@"..\..\NotificationIcons", row["IconFileName"].ToString()));
  114. var buffer = new byte[notificationIconStream.Length];
  115. notificationIconStream.Read(buffer, 0, (int) notificationIconStream.Length);
  116. notificationTypeDto.NotificationTypeIcon = buffer;
  117. notificationTypeDtos.Add(notificationTypeDto);
  118. }
  119. catch (Exception exception)
  120. {
  121. Logger.LogException(LogLevel.Error, "Exception Ignored", exception);
  122. throw;
  123. }
  124. finally
  125. {
  126. if (notificationIconStream != null) notificationIconStream.Close();
  127. }
  128. }
  129. }
  130. catch (Exception exception)
  131. {
  132. Logger.LogException(LogLevel.Error, "Exception Ignored", exception);
  133. }
  134. finally
  135. {
  136. if (excelFile != null) excelFile.Close();
  137. }
  138. }
  139. }
  140. }