public void AddRawMetric(string id, string date, string value) { IDictionary <string, Object> paramMap = new Dictionary <string, Object>(); paramMap[Constants.Context.PROVIDER_URL] = ConfigurationManager.AppSettings["jmsProviderUrl"].ToString(); IContext jmsContext = ContextFactory.CreateContext(paramMap); IConnectionFactory cf = jmsContext.LookupConnectionFactory(ConfigurationManager.AppSettings["jmsConnectionFactory"].ToString()); IQueue queue = (IQueue)jmsContext.LookupDestination(ConfigurationManager.AppSettings["jmsQueue"].ToString()); IConnection connection; try { if (date == null || date.Length == 0 || value == null || value.Length == 0) { throw new Exception("Missing raw metrics parameters."); } var device = DeviceDAO.Get(int.Parse(id)); if (device == null) { throw new Exception("Device doesn't exist."); } connection = cf.CreateConnection(); connection.Start(); ISession producerSession = connection.CreateSession(Constants.SessionMode.AUTO_ACKNOWLEDGE); IMessageProducer producer = producerSession.CreateProducer(queue); producer.DeliveryMode = Constants.DeliveryMode.PERSISTENT; MetricModel model = new MetricModel() { DeviceId = int.Parse(id), Date = date, Value = value }; ITextMessage jmsMessage = producerSession.CreateTextMessage(JsonConvert.SerializeObject(model)); producer.Send(jmsMessage); } catch (Exception ex) { throw new WebFaultException <string>(ex.Message, HttpStatusCode.MethodNotAllowed); } connection.Close(); }
public void GivenIdShouldReturnObject() { Device d1 = new Device() { Id = 0, Name = "A" }; Device d2 = new Device() { Id = 1, Name = "B" }; var mockContext = new Mock <UserDataContext>(); mockContext.Setup(c => c.Device.Find(0)).Returns(d1); var dao = new DeviceDAO(mockContext.Object); var result = dao.Get(0); Assert.AreEqual(d1, result); }