private async Task RegisterCallsignUpdate(string imei, string callsign, VehicleType? type, IMEIToCallsign linkedObject, bool shouldUpdate = true)
        {
            var oldValues = new IMEIToCallsign
            {
                CallSign = linkedObject.CallSign,
                Id = linkedObject.Id,
                IMEI = linkedObject.IMEI,
                Type = linkedObject.Type
            };

            var imeis = CreateMockIMEIDbSet();
            var context = CreateMockIMEIContext(imeis.Object);
            var locationService = CreateMockLocationService();

            var service = new IMEIService(context.Object, locationService.Object);

            await service.RegisterCallsign(imei, callsign, type);

            if (string.IsNullOrWhiteSpace(callsign))
            {
                locationService.Verify(l => l.ExpireLocation(It.IsAny<string>()), Times.Never);
            }
            else
            {
                locationService.Verify(l => l.ExpireLocation(oldValues.CallSign));
            }

            if (shouldUpdate)
            {
                Assert.Equal(imei, linkedObject.IMEI);
                Assert.Equal(string.IsNullOrWhiteSpace(callsign) ? oldValues.CallSign : callsign, linkedObject.CallSign);
                Assert.Equal(type ?? oldValues.Type, linkedObject.Type);
                context.Verify(c => c.SaveChangesAsync());
            }
            else
            {
                Assert.Equal(imei, linkedObject.IMEI);
                Assert.Equal(oldValues.CallSign, linkedObject.CallSign);
                Assert.Equal(oldValues.Type, linkedObject.Type);
                context.Verify(c => c.SaveChangesAsync(), Times.Never);
            }
        }
        private async Task GetFromIMEI(string imei, IMEIToCallsign expectedResult = null, bool shouldReturn = true, bool shouldRegister = false)
        {
            var imeis = CreateMockIMEIDbSet();
            var context = CreateMockIMEIContext(imeis.Object);
            var locationService = CreateMockLocationService();

            var service = new IMEIService(context.Object, locationService.Object);

            var res = await service.GetFromIMEI(imei);

            if (shouldRegister)
            {
                imeis.Verify(i => i.Add(It.Is<IMEIToCallsign>(c => c.IMEI == imei && c.CallSign == IMEIService.DefaultCallsign && c.Type == VehicleType.Unknown)));
                locationService.Verify(s => s.ExpireLocation(IMEIService.DefaultCallsign), Times.Never);
                context.Verify(c => c.SaveChangesAsync());
            }
            else
            {
                imeis.Verify(i => i.Add(It.IsAny<IMEIToCallsign>()), Times.Never);
                locationService.Verify(s => s.ExpireLocation(It.IsAny<string>()), Times.Never);
                context.Verify(c => c.SaveChangesAsync(), Times.Never);
            }

            if (shouldReturn)
            {
                Assert.NotNull(res);
                Assert.NotNull(expectedResult);

                if (!shouldRegister)
                {
                    // If the system is expected to register the item, ID won't have been generated
                    Assert.Equal(expectedResult.Id, res.Id);
                }
                Assert.Equal(expectedResult.CallSign, res.CallSign);
                Assert.Equal(expectedResult.IMEI, res.IMEI);
                Assert.Equal(expectedResult.Type, res.Type);
            }
        }
        private async Task RegisterCallsignCreate(string imei, string callsign, VehicleType? type, bool shouldCreate = true)
        {
            var imeis = CreateMockIMEIDbSet();
            var context = CreateMockIMEIContext(imeis.Object);
            var locationService = CreateMockLocationService();

            var service = new IMEIService(context.Object, locationService.Object);

            await service.RegisterCallsign(imei, callsign, type);

            if (shouldCreate)
            {
                var expectedItem = new IMEIToCallsign
                {
                    IMEI = imei,
                    CallSign = callsign ?? IMEIService.DefaultCallsign,
                    Type = type ?? VehicleType.Unknown
                };

                imeis.Verify(i => i.Add(It.Is<IMEIToCallsign>(c => c.IMEI == expectedItem.IMEI && c.CallSign == expectedItem.CallSign && c.Type == expectedItem.Type)));
                context.Verify(c => c.SaveChangesAsync());
            }
            else
            {
                imeis.Verify(i => i.Add(It.IsAny<IMEIToCallsign>()), Times.Never);
                context.Verify(c => c.SaveChangesAsync(), Times.Never);
            }
        }
        private async Task GetFromIdQueryable(int id, IMEIToCallsign expectedResult)
        {
            var imeis = CreateMockIMEIDbSet();
            var context = CreateMockIMEIContext(imeis.Object);
            var locationService = CreateMockLocationService();

            var service = new IMEIService(context.Object, locationService.Object);

            var res = await service.GetFromIdQueryable(id);

            Assert.IsAssignableFrom<IQueryable<IMEIToCallsign>>(res);

            if (expectedResult == null)
            {
                Assert.False(res.Any());
            }
            else
            {
                Assert.Equal(1, res.Count());
                var actual = res.First();
                Assert.Equal(expectedResult.Id, actual.Id);
                Assert.Equal(expectedResult.CallSign, actual.CallSign);
                Assert.Equal(expectedResult.IMEI, actual.IMEI);
                Assert.Equal(expectedResult.Type, actual.Type);
            }
        }
        private async Task GetFromId(int id, IMEIToCallsign expectedResult)
        {
            var imeis = CreateMockIMEIDbSet();
            var context = CreateMockIMEIContext(imeis.Object);
            var locationService = CreateMockLocationService();

            var service = new IMEIService(context.Object, locationService.Object);

            var res = await service.GetFromId(id);

            if (expectedResult == null)
            {
                Assert.Null(res);
            }
            else
            {
                Assert.Equal(expectedResult.Id, res.Id);
                Assert.Equal(expectedResult.CallSign, res.CallSign);
                Assert.Equal(expectedResult.IMEI, res.IMEI);
                Assert.Equal(expectedResult.Type, res.Type);
            }
        }
        private async Task DeleteIMEIByID(int id, IMEIToCallsign imeiObj = null, bool tryDelete = true)
        {
            var imeis = CreateMockIMEIDbSet();
            var context = CreateMockIMEIContext(imeis.Object);
            var locationService = CreateMockLocationService();

            var service = new IMEIService(context.Object, locationService.Object);

            await service.DeleteIMEIById(id);
            ConfirmDelete(imeis, context, locationService, imeiObj, tryDelete);
        }
        private async Task DeleteIMEI(string imei, IMEIToCallsign imeiObj = null, bool tryDelete = true)
        {
            var imeis = CreateMockIMEIDbSet();
            var context = CreateMockIMEIContext(imeis.Object);
            var locationService = CreateMockLocationService();

            var container = new UnityContainer();

            container.RegisterInstance(locationService.Object);

            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

            var service = new IMEIService(context.Object);

            await service.DeleteIMEI(imei);

            ConfirmDelete(imeis, context, locationService, imeiObj, tryDelete);
        }
        public async Task GetAll()
        {
            var imeis = CreateMockIMEIDbSet();
            var context = CreateMockIMEIContext(imeis.Object);
            var locationService = CreateMockLocationService();

            var service = new IMEIService(context.Object, locationService.Object);

            var result = await service.GetAllAsync();

            Assert.True(result.OrderBy(r => r.Id).SequenceEqual(GoodCallsigns.OrderBy(r => r.Id)));
        }