Alarm activete/deactivate tests and implementation 27/169827/3
authorMichal Kolodziejski <m.kolodziejs@samsung.com>
Fri, 9 Feb 2018 11:14:45 +0000 (12:14 +0100)
committerMichal Kolodziejski <m.kolodziejs@samsung.com>
Mon, 12 Feb 2018 13:53:07 +0000 (14:53 +0100)
Change-Id: I338f034775a24cd570fcbfc725e873a889f8d1ed
Signed-off-by: Michal Kolodziejski <m.kolodziejs@samsung.com>
Clock/Clock.Tests/AlarmManagerTests.cs [new file with mode: 0644]
Clock/Clock.Tests/Mock/AlarmFactory.Platform.cs
Clock/Clock.Tests/Mock/AlarmFactory.cs
Clock/Clock.Tests/Mock/MockFactory.cs [new file with mode: 0644]
Clock/Clock/Abstractions/IPlatformAlarm.cs
Clock/Clock/Abstractions/IPlatformAlarmsService.cs
Clock/Clock/Services/AlarmManager.cs
clock/Clock.Tests/Clock.Tests.csproj

diff --git a/Clock/Clock.Tests/AlarmManagerTests.cs b/Clock/Clock.Tests/AlarmManagerTests.cs
new file mode 100644 (file)
index 0000000..ae05c7e
--- /dev/null
@@ -0,0 +1,177 @@
+/*
+* Copyright 2017  Samsung Electronics Co., Ltd
+*
+* Licensed under the Flora License, Version 1.1 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://floralicense.org/license/
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+using System;
+using System.Linq;
+using Clock.Services;
+using Clock.Tests.Mock;
+using NUnit.Framework;
+
+
+namespace Clock.Tests
+{
+    [TestFixture]
+    public class AlarmManagerTests
+    {
+        [Test]
+        public void AlarmManager_GetAlarms()
+        {
+            //Arrange
+            var alarmManager = new AlarmManager(AlarmFactory.CreatePlatformAlarmWithItems(),
+                AlarmFactory.CreateAlarmRepositoryWithItems(), MockFactory.NavigationServiceMock);
+
+            //Act
+            var alarms = alarmManager.GetAlarms();
+
+            //Assert
+            Assert.That(alarms.ToList().Count, Is.EqualTo(3));
+        }
+
+        [Test]
+        public void AlarmManager_DeactivateAlarm()
+        {
+            //Arrange
+            var alarmRepository = AlarmFactory.CreateAlarmRepositoryWithItems();
+            var platformAlarmService = AlarmFactory.CreatePlatformAlarmWithItems();
+
+            var alarmManager =
+                new AlarmManager(platformAlarmService, alarmRepository, MockFactory.NavigationServiceMock);
+
+            var alarm = alarmRepository.Get(1);
+            if (alarm.PlatformAlarmId == null) return;
+
+            var platformId = (int)alarm.PlatformAlarmId;
+
+            //Act
+            alarmManager.Deactivate(alarm);
+
+            //Assert
+            Assert.That(alarm.PlatformAlarmId, Is.EqualTo(null));
+            Assert.That(platformAlarmService.GetPlatformAlarm(platformId), Is.EqualTo(null));
+        }
+
+        [Test]
+        public void AlarmManager_DeactivateNotActiveAlarm()
+        {
+            //Arrange
+            var alarmRepository = AlarmFactory.CreateAlarmRepositoryWithItems();
+            var platformAlarmService = AlarmFactory.CreatePlatformAlarmWithItems();
+
+            var alarmManager =
+                new AlarmManager(platformAlarmService, alarmRepository, MockFactory.NavigationServiceMock);
+
+            var alarm = alarmRepository.Get(3);
+
+
+            //Act
+            alarmManager.Deactivate(alarm);
+
+            //Assert
+            Assert.That(alarm.PlatformAlarmId, Is.EqualTo(null));
+        }
+
+        [Test]
+        public void AlarmManager_ActivateAlarm()
+        {
+            //Arrange
+            var alarmRepository = AlarmFactory.CreateAlarmRepositoryWithItems();
+            var platformAlarmService = AlarmFactory.CreatePlatformAlarmWithItems();
+
+            var alarmManager =
+                new AlarmManager(platformAlarmService, alarmRepository, MockFactory.NavigationServiceMock);
+
+            var alarm = alarmRepository.Get(3);
+
+            //Act
+            alarmManager.Activate(alarm);
+
+            //Assert
+            if (alarm.PlatformAlarmId == null) Assert.Fail("Platform id is null");
+
+            Assert.That(alarm.PlatformAlarmId, Is.Not.EqualTo(null));
+            Assert.That(platformAlarmService.GetPlatformAlarm((int)alarm.PlatformAlarmId), Is.Not.EqualTo(null));
+        }
+
+        [Test]
+        public void AlarmManager_ActivateActiveAlarm()
+        {
+            //Arrange
+            var alarmRepository = AlarmFactory.CreateAlarmRepositoryWithItems();
+            var platformAlarmService = AlarmFactory.CreatePlatformAlarmWithItems();
+
+            var alarmManager =
+                new AlarmManager(platformAlarmService, alarmRepository, MockFactory.NavigationServiceMock);
+
+            var alarm = alarmRepository.Get(1);
+
+            //Act
+            alarmManager.Activate(alarm);
+
+            //Assert
+            if (alarm.PlatformAlarmId == null) Assert.Fail("Platform id is null");
+
+            Assert.That(alarm.PlatformAlarmId, Is.Not.EqualTo(null));
+            Assert.That(platformAlarmService.GetPlatformAlarm((int)alarm.PlatformAlarmId), Is.Not.EqualTo(null));
+        }
+
+        [Test]
+        public void AlarmManager_ActivateNullAlarm()
+        {
+            //Arrange
+            var alarmRepository = AlarmFactory.CreateAlarmRepositoryWithItems();
+            var platformAlarmService = AlarmFactory.CreatePlatformAlarmWithItems();
+
+            var alarmManager =
+                new AlarmManager(platformAlarmService, alarmRepository, MockFactory.NavigationServiceMock);
+
+            //Assert
+            Assert.Throws(Is.TypeOf<ArgumentNullException>(), () => alarmManager.Activate(null));
+        }
+
+        [Test]
+        public void AlarmManager_DeactivateNullAlarm()
+        {
+            //Arrange
+            var alarmRepository = AlarmFactory.CreateAlarmRepositoryWithItems();
+            var platformAlarmService = AlarmFactory.CreatePlatformAlarmWithItems();
+
+            var alarmManager = new AlarmManager(platformAlarmService, alarmRepository, null);
+
+            //Assert
+            Assert.Throws(Is.TypeOf<ArgumentNullException>(), () => alarmManager.Activate(null));
+        }
+
+        [Test]
+        public void AlarmManager_DeactivateNotExistingPlatformAlarm()
+        {
+            //Arrange
+            var alarmRepository = AlarmFactory.CreateAlarmRepositoryWithItems();
+            var platformAlarmService = AlarmFactory.CreatePlatformAlarmWithItems();
+
+            var alarmManager =
+                new AlarmManager(platformAlarmService, alarmRepository, MockFactory.NavigationServiceMock);
+
+            var alarm = alarmRepository.Get(1);
+            alarm.PlatformAlarmId = 10;
+
+            //Act
+            alarmManager.Deactivate(alarm);
+
+            //Assert
+            Assert.That(alarm.PlatformAlarmId, Is.EqualTo(null));
+        }
+    }
+}
\ No newline at end of file
index f0fecaf..32cd21d 100644 (file)
@@ -25,30 +25,30 @@ namespace Clock.Tests.Mock
 {
     public partial class AlarmFactory
     {
-        private static Dictionary<int, PlatformAlarmFake> _platformAlarms;
-        private static int _platformIndex = 2;
-
         public static IPlatformAlarmsService CreatePlatformAlarmWithItems()
         {
-            _platformAlarms = new Dictionary<int, PlatformAlarmFake>
+            var platformIndex = 2;
+            var platformAlarms = new Dictionary<int, PlatformAlarmFake>
             {
-                {1, new PlatformAlarmFake(new DateTime(2000, 01, 01))},
-                {2, new PlatformAlarmFake(new DateTime(2000, 01, 01))}
+                {1, new PlatformAlarmFake(new DateTime(2000, 01, 01)) {Id = 1}},
+                {2, new PlatformAlarmFake(new DateTime(2000, 01, 01)) {Id = 2}}
             };
 
             var mock = new Mock<IPlatformAlarmsService>();
 
             mock.Setup(x => x.GetPlatformAlarm(It.IsAny<int>()))
                 .Returns<int>(i =>
-                    _platformAlarms.Keys.Contains(i) && _platformAlarms[i].IsActive ? _platformAlarms[i] : null);
+                    platformAlarms.Keys.Contains(i) && platformAlarms[i].IsActive ? platformAlarms[i] : null);
 
-            mock.Setup(x => x.GetPlatformAlarms()).Returns(_platformAlarms.Values.Where(fake => fake.IsActive).AsEnumerable());
+            mock.Setup(x => x.GetPlatformAlarms())
+                .Returns(platformAlarms.Values.Where(fake => fake.IsActive).AsEnumerable());
 
             mock.Setup(x => x.CreateNewPlatformAlarm(It.IsAny<DateTime>(), It.IsAny<DaysOfWeek>()))
                 .Callback<DateTime, DaysOfWeek>((time, days) =>
                 {
-                    _platformAlarms.Add(++_platformIndex, new PlatformAlarmFake(time));
-                }).Returns(_platformAlarms[_platformIndex]);
+                    platformIndex++;
+                    platformAlarms.Add(platformIndex, new PlatformAlarmFake(time));
+                }).Returns(platformAlarms[platformIndex]);
 
             return mock.Object;
         }
@@ -59,6 +59,8 @@ namespace Clock.Tests.Mock
 
             public bool IsActive { get; private set; }
 
+            public int Id { get; set; }
+
             public PlatformAlarmFake(DateTime time)
             {
                 _scheduledDateTime = time;
index 12bcfd9..36712f8 100644 (file)
@@ -24,31 +24,30 @@ namespace Clock.Tests.Mock
 {
     public partial class AlarmFactory
     {
-        private static int _index = 3;
-
         public static IAlarmRepository CreateAlarmRepositoryWithItems()
         {
-            var alarmRepo = new Mock<IAlarmRepository>();
+            var index = 3;
+            var alarmRepository = new Mock<IAlarmRepository>();
 
             var alarms = new List<Alarm>
             {
                 new Alarm {Id = 1, Name = "Alarm1", PlatformAlarmId = 1},
                 new Alarm {Id = 2, Name = "Alarm2", PlatformAlarmId = 2},
-                new Alarm {Id = 3, Name = "Alarm3"}
+                new Alarm {Id = 3, Name = "Alarm3", PlatformAlarmId = null}
             };
 
-            alarmRepo.Setup(x => x.Get(It.IsAny<int>()))
+            alarmRepository.Setup(x => x.Get(It.IsAny<int>()))
                 .Returns((int i) => alarms.FirstOrDefault(a => a.Id == i));
 
-            alarmRepo.Setup(x => x.Add(It.IsAny<Alarm>())).Callback((Alarm alarm) =>
+            alarmRepository.Setup(x => x.Add(It.IsAny<Alarm>())).Callback((Alarm alarm) =>
             {
-                alarm.Id = ++_index;
+                alarm.Id = ++index;
                 alarms.Add(alarm);
             }).Returns((Alarm alarm) => alarm.Id);
 
-            alarmRepo.Setup(x => x.GetAll()).Returns(alarms.AsEnumerable());
+            alarmRepository.Setup(x => x.GetAll()).Returns(alarms.AsEnumerable());
 
-            alarmRepo.Setup(x => x.Update(It.IsAny<Alarm>())).Callback((Alarm alarm) =>
+            alarmRepository.Setup(x => x.Update(It.IsAny<Alarm>())).Callback((Alarm alarm) =>
             {
                 var first = alarms.FirstOrDefault(a => a.Id == alarm.Id);
                 if (first != default(Alarm))
@@ -64,7 +63,7 @@ namespace Clock.Tests.Mock
                 }
             });
 
-            alarmRepo.Setup(x => x.Remove(It.IsAny<Alarm>())).Callback((Alarm alarm) =>
+            alarmRepository.Setup(x => x.Remove(It.IsAny<Alarm>())).Callback((Alarm alarm) =>
             {
                 var alarmToDelete = alarms.FirstOrDefault(a => a.Id == alarm.Id);
                 if (alarmToDelete != default(Alarm))
@@ -73,7 +72,7 @@ namespace Clock.Tests.Mock
                 }
             });
 
-            return alarmRepo.Object;
+            return alarmRepository.Object;
         }
     }
 }
\ No newline at end of file
diff --git a/Clock/Clock.Tests/Mock/MockFactory.cs b/Clock/Clock.Tests/Mock/MockFactory.cs
new file mode 100644 (file)
index 0000000..b0ffc7c
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+* Copyright 2017  Samsung Electronics Co., Ltd
+*
+* Licensed under the Flora License, Version 1.1 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://floralicense.org/license/
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+using Clock.Abstractions;
+using Moq;
+
+namespace Clock.Tests.Mock
+{
+    public static class MockFactory
+    {
+        public static INavigationService NavigationServiceMock => new Mock<INavigationService>().Object;
+    }
+}
\ No newline at end of file
index a16b177..950b006 100644 (file)
@@ -24,6 +24,11 @@ namespace Clock.Abstractions
     public interface IPlatformAlarm
     {
         /// <summary>
+        /// Unique id of the alarm.
+        /// </summary>
+        int Id { get; set; }
+
+        /// <summary>
         /// Cancels this alarm.
         /// </summary>
         void Cancel();
index 9c88c37..62c1e03 100644 (file)
@@ -40,7 +40,7 @@ namespace Clock.Abstractions
         /// </summary>
         /// <param name="time">Time of the alarm.</param>
         /// <param name="days">Flags that indicate days of the week.</param>
-        /// <returns>Platform alarm object.</returns>
+        /// <returns>New platform alarm object.</returns>
         IPlatformAlarm CreateNewPlatformAlarm(DateTime time, DaysOfWeek days);
 
         /// <summary>
index baafdcd..959f27b 100644 (file)
@@ -48,7 +48,8 @@ namespace Clock.Services
         /// <param name="platformAlarmsService">Service that will provides platform alarm functionalities.</param>
         /// <param name="alarmRepository">Repository with alarms.</param>
         /// <param name="navigationService">Application navigation service.</param>
-        public AlarmManager(IPlatformAlarmsService platformAlarmsService, IAlarmRepository alarmRepository, INavigationService navigationService)
+        public AlarmManager(IPlatformAlarmsService platformAlarmsService, IAlarmRepository alarmRepository,
+            INavigationService navigationService)
         {
             _platformAlarmsService = platformAlarmsService;
             _alarmRepository = alarmRepository;
@@ -119,18 +120,41 @@ namespace Clock.Services
         /// <param name="alarm">Alarm data.</param>
         public void Activate(Alarm alarm)
         {
-            // 1. Create platform alarm.
-            // 2. Change flag in alarm
-            // 3. Update alarm in db
-            throw new NotImplementedException();
+            if (alarm == null)
+            {
+                throw new ArgumentNullException();
+            }
+
+            if (alarm.PlatformAlarmId != null)
+            {
+                if (_platformAlarmsService.GetPlatformAlarm((int)alarm.PlatformAlarmId) != null)
+                {
+                    return;
+                }
+            }
+
+            var platformAlarm = _platformAlarmsService.CreateNewPlatformAlarm(alarm.Time, alarm.Days);
+            alarm.PlatformAlarmId = platformAlarm.Id;
+            _alarmRepository.Update(alarm);
         }
 
         public void Deactivate(Alarm alarm)
         {
-            // 1. Get platform alarm and cancel it
-            // 2. Change flag in alarm
-            // 3. Update alarm in db
-            throw new NotImplementedException();
+            if (alarm == null)
+            {
+                throw new ArgumentNullException();
+            }
+
+            if (alarm.PlatformAlarmId == null)
+            {
+                return;
+            }
+
+            var platformAlarm = _platformAlarmsService.GetPlatformAlarm((int)alarm.PlatformAlarmId);
+            platformAlarm?.Cancel();
+
+            alarm.PlatformAlarmId = null;
+            _alarmRepository.Update(alarm);
         }
 
         /// <summary>
index daedc7a..7b52f3d 100644 (file)
@@ -71,7 +71,9 @@
     </Otherwise>
   </Choose>
   <ItemGroup>
+    <Compile Include="AlarmManagerTests.cs" />
     <Compile Include="Mock\AlarmFactory.cs" />
+    <Compile Include="Mock\MockFactory.cs" />
     <Compile Include="TimezonesTest.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>