예제 #1
0
        public void GetDeviceBySerialNumberTest_UpperLower()
        {
            var          serialNumber         = (Guid.NewGuid().ToString()).ToLower();
            const string expectedDeviceId     = "trn::profilex:us-west-2:device:560c2a6c-6b7e-48d8-b1a5-e4009e2d4c97";
            const string expectedDeviceType   = "CB460";
            const string expectedDeviceName   = "The device Name";
            var          expectedSerialNumber = serialNumber.ToUpper();

            var deviceResponseModel = new DeviceResponseModel()
            {
                TRN = expectedDeviceId, DeviceType = expectedDeviceType, DeviceName = expectedDeviceName, SerialNumber = expectedSerialNumber
            };

            var route           = $"/devices/getDeviceWithSerialNumber";
            var queryParameters = new List <KeyValuePair <string, string> > {
                new KeyValuePair <string, string>("serialNumber", serialNumber.ToUpper())
            };
            var expectedUrl = $"{baseUrl}{route}?serialNumber={serialNumber.ToUpper()}";

            mockServiceResolution.Setup(m => m.ResolveRemoteServiceEndpoint(
                                            It.IsAny <string>(), It.IsAny <ApiType>(), It.IsAny <ApiVersion>(), route, queryParameters)).Returns(Task.FromResult(expectedUrl));

            MockUtilities.TestRequestSendsCorrectJson("Get device by serial number", mockWebRequest, null, expectedUrl, HttpMethod.Get, deviceResponseModel, async() =>
            {
                var client = ServiceProvider.GetRequiredService <ICwsDeviceClient>();
                var result = await client.GetDeviceBySerialNumber(serialNumber);

                Assert.NotNull(result);
                Assert.Equal(TRNHelper.ExtractGuidAsString(expectedDeviceId), result.Id);
                Assert.Equal(expectedDeviceType, result.DeviceType);
                Assert.Equal(expectedDeviceName, result.DeviceName);
                Assert.Equal(expectedSerialNumber, result.SerialNumber);
                return(true);
            });
        }
예제 #2
0
        public async Task GetDevice_NoAccountFound_UnhappyPath()
        {
            var cwsDevice = new DeviceResponseModel()
            {
                TRN        = TRNHelper.MakeTRN(_deviceUid, TRNHelper.TRN_DEVICE),
                DeviceName = _deviceName, SerialNumber = _serialNumber
            };
            var cwsDeviceClient = new Mock <ICwsDeviceClient>();

            cwsDeviceClient.Setup(pr => pr.GetDeviceBySerialNumber(It.IsAny <string>(), _customHeaders))
            .ReturnsAsync(cwsDevice);
            cwsDeviceClient.Setup(pr => pr.GetAccountsForDevice(It.IsAny <Guid>(), _customHeaders))
            .ReturnsAsync((DeviceAccountListResponseModel)null);

            var getDeviceBySerialExecutor = RequestExecutorContainerFactory.Build <GetDeviceBySerialExecutor>
                                                (_logger, _configStore, _serviceExceptionHandler,
                                                headers: _customHeaders, cwsDeviceClient: cwsDeviceClient.Object);
            var response = await getDeviceBySerialExecutor.ProcessAsync(new DeviceSerial(_serialNumber))
                           as DeviceDescriptorSingleResult;

            Assert.NotNull(response);
            Assert.Equal(102, response.Code);
            Assert.Equal("Unable to locate any account for the device in cws", response.Message);

            Assert.NotNull(response.DeviceDescriptor);
            Assert.Equal(102, response.DeviceDescriptor.Code);
            Assert.Null(response.DeviceDescriptor.CustomerUID);
            Assert.Equal(_deviceUid, response.DeviceDescriptor.DeviceUID);
            Assert.Equal(_deviceName, response.DeviceDescriptor.DeviceName);
            Assert.Equal(_serialNumber, response.DeviceDescriptor.SerialNumber);
            Assert.Equal("UNKNOWN", response.DeviceDescriptor.RelationStatus.ToString().ToUpper());
            Assert.Equal("Unknown", response.DeviceDescriptor.TccDeviceStatus.ToString());
            Assert.Null(response.DeviceDescriptor.ShortRaptorAssetId);
        }
예제 #3
0
        public void GetDeviceByDeviceUidTest()
        {
            const string DeviceId             = "trn::profilex:us-west-2:device:560c2a6c-6b7e-48d8-b1a5-e4009e2d4c97";
            const string expectedDeviceId     = DeviceId;
            const string expectedDeviceType   = "CB460";
            const string expectedDeviceName   = "The device Name";
            const string expectedSerialNumber = "2002J032SW";

            var deviceResponseModel = new DeviceResponseModel()
            {
                TRN = expectedDeviceId, DeviceType = expectedDeviceType, DeviceName = expectedDeviceName, SerialNumber = expectedSerialNumber
            };

            var route       = $"/devices/{DeviceId}";
            var expectedUrl = $"{baseUrl}{route}";

            mockServiceResolution.Setup(m => m.ResolveRemoteServiceEndpoint(
                                            It.IsAny <string>(), It.IsAny <ApiType>(), It.IsAny <ApiVersion>(), route, It.IsAny <IList <KeyValuePair <string, string> > >())).Returns(Task.FromResult(expectedUrl));

            MockUtilities.TestRequestSendsCorrectJson("Get device by deviceUid", mockWebRequest, null, expectedUrl, HttpMethod.Get, deviceResponseModel, async() =>
            {
                var client = ServiceProvider.GetRequiredService <ICwsDeviceClient>();
                var result = await client.GetDeviceByDeviceUid(TRNHelper.ExtractGuid(DeviceId).Value);

                Assert.NotNull(result);
                Assert.Equal(TRNHelper.ExtractGuidAsString(expectedDeviceId), result.Id);
                Assert.Equal(expectedDeviceType, result.DeviceType);
                Assert.Equal(expectedDeviceName, result.DeviceName);
                Assert.Equal(expectedSerialNumber, result.SerialNumber);
                return(true);
            });
        }
예제 #4
0
        public DeviceResponseModel DeviceById(string deviceId)
        {
            DeviceResponseModel summaryModel =
                CreateSummaryModel(_deviceManager.GetDevice(deviceId));

            summaryModel.Layers = CreateLayerSummaryModels(_deviceManager.GetDevice(deviceId).Layers.Values, deviceId);
            return(summaryModel);
        }
예제 #5
0
        public async Task <DeviceResponseModel> Post([FromBody] DeviceRequestModel model)
        {
            var device = model.ToDevice(_userService.GetProperUserId(User));
            await _deviceService.SaveAsync(device);

            var response = new DeviceResponseModel(device);

            return(response);
        }
예제 #6
0
        public async Task <DeviceResponseModel> GetByIdentifier(string identifier)
        {
            var device = await _deviceRepository.GetByIdentifierAsync(identifier, _userService.GetProperUserId(User).Value);

            if (device == null)
            {
                throw new NotFoundException();
            }

            var response = new DeviceResponseModel(device);

            return(response);
        }
예제 #7
0
        public DeviceResponseModel GetDeviceByDeviceUid(string deviceTrn)
        {
            var deviceResponseModel = new DeviceResponseModel()
            {
                TRN          = deviceTrn,
                DeviceType   = "EC520",
                DeviceName   = "this is a device",
                SerialNumber = "56556565"
            };

            Logger.LogInformation($"{nameof(GetDeviceByDeviceUid)}: deviceTrn {deviceTrn}. deviceResponseModel {JsonConvert.SerializeObject(deviceResponseModel)}");
            return(deviceResponseModel);
        }
예제 #8
0
        public DeviceResponseModel GetDeviceBySerialNumber([FromQuery] string serialNumber)
        {
            var deviceResponseModel = new DeviceResponseModel()
            {
                TRN          = TRNHelper.MakeTRN(Guid.NewGuid().ToString(), TRNHelper.TRN_DEVICE),
                DeviceType   = "EC520",
                DeviceName   = "this is a device",
                SerialNumber = serialNumber
            };

            Logger.LogInformation($"{nameof(GetDeviceBySerialNumber)}: serialNumber {serialNumber}. deviceResponseModel {JsonConvert.SerializeObject(deviceResponseModel)}");
            return(deviceResponseModel);
        }
예제 #9
0
        public async Task <DeviceResponseModel> GetByIdentifier(string identifier)
        {
            var device = await _deviceRepository.GetByIdentifierAsync(identifier, new Guid(_userManager.GetUserId(User)));

            if (device == null)
            {
                throw new NotFoundException();
            }

            var response = new DeviceResponseModel(device);

            return(response);
        }
예제 #10
0
        public async Task GetDevice_TooManyActiveAccounts_UnhappyPath()
        {
            var cwsDevice = new DeviceResponseModel()
            {
                TRN        = TRNHelper.MakeTRN(_deviceUid, TRNHelper.TRN_DEVICE),
                DeviceName = _deviceName, SerialNumber = _serialNumber
            };
            var cwsDeviceClient = new Mock <ICwsDeviceClient>();

            cwsDeviceClient.Setup(pr => pr.GetDeviceBySerialNumber(It.IsAny <string>(), _customHeaders))
            .ReturnsAsync(cwsDevice);
            var cwsDeviceAccountList = new DeviceAccountListResponseModel()
            {
                Accounts = new List <DeviceAccountResponseModel>()
                {
                    new DeviceAccountResponseModel()
                    {
                        TRN         = TRNHelper.MakeTRN(_customerUid, TRNHelper.TRN_ACCOUNT),
                        AccountName = "the customer name", RelationStatus = _relationStatus, TccDeviceStatus = _tccDeviceStatus
                    },
                    new DeviceAccountResponseModel()
                    {
                        TRN         = TRNHelper.MakeTRN(Guid.NewGuid().ToString(), TRNHelper.TRN_ACCOUNT),
                        AccountName = "the other customer name", RelationStatus = _relationStatus, TccDeviceStatus = _tccDeviceStatus
                    }
                }
            };

            cwsDeviceClient.Setup(pr => pr.GetAccountsForDevice(It.IsAny <Guid>(), _customHeaders))
            .ReturnsAsync(cwsDeviceAccountList);

            var getDeviceBySerialExecutor = RequestExecutorContainerFactory.Build <GetDeviceBySerialExecutor>
                                                (_logger, _configStore, _serviceExceptionHandler,
                                                headers: _customHeaders, cwsDeviceClient: cwsDeviceClient.Object);
            var response = await getDeviceBySerialExecutor.ProcessAsync(new DeviceSerial(_serialNumber))
                           as DeviceDescriptorSingleResult;

            Assert.NotNull(response);
            Assert.Equal(103, response.Code);
            Assert.Equal("There is >1 active account for the device in cws", response.Message);

            Assert.NotNull(response.DeviceDescriptor);
            Assert.Equal(103, response.DeviceDescriptor.Code);
            Assert.Null(response.DeviceDescriptor.CustomerUID);
            Assert.Equal(_deviceUid, response.DeviceDescriptor.DeviceUID);
            Assert.Equal(_deviceName, response.DeviceDescriptor.DeviceName);
            Assert.Equal(_serialNumber, response.DeviceDescriptor.SerialNumber);
            Assert.Equal("UNKNOWN", response.DeviceDescriptor.RelationStatus.ToString().ToUpper());
            Assert.Equal("Unknown", response.DeviceDescriptor.TccDeviceStatus.ToString());
            Assert.Null(response.DeviceDescriptor.ShortRaptorAssetId);
        }
예제 #11
0
        public Task <DeviceResponseModel> GetDeviceByDeviceUid(Guid deviceUid, IHeaderDictionary customHeaders = null)
        {
            log.LogDebug($"{nameof(GetDeviceByDeviceUid)} Mock: deviceUid {deviceUid}");

            var deviceResponseModel = new DeviceResponseModel()
            {
                TRN          = TRNHelper.MakeTRN(deviceUid.ToString(), TRNHelper.TRN_DEVICE),
                DeviceType   = "EC520",
                DeviceName   = "this is a device",
                SerialNumber = "56556565"
            };

            log.LogDebug($"{nameof(GetDeviceByDeviceUid)} Mock: deviceResponseModel {JsonConvert.SerializeObject(deviceResponseModel)}");
            return(Task.FromResult(deviceResponseModel));
        }
예제 #12
0
        public async Task <DeviceResponseModel> Put(string id, [FromBody] DeviceRequestModel model)
        {
            var device = await _deviceRepository.GetByIdAsync(new Guid(id), _userService.GetProperUserId(User).Value);

            if (device == null)
            {
                throw new NotFoundException();
            }

            await _deviceService.SaveAsync(model.ToDevice(device));

            var response = new DeviceResponseModel(device);

            return(response);
        }
예제 #13
0
        private async Task <List <DeviceResponseModel> > GetDevices(AssetEntity entity)
        {
            var locationCount = await repository.GetEntities <LocationEntity>()
                                .Where(x => x.AssetId == entity.Id)
                                .GroupBy(x => x.DeviceId)
                                .Select(x => new { DeviceId = x.Key, LocationsCount = x.Count() })
                                .ToListAsync();


            return(entity.Devices.Select(x =>
            {
                DeviceResponseModel deviceModel = mapper.Map <DeviceEntity, DeviceResponseModel>(x);
                deviceModel.LocationsCount = (locationCount.FirstOrDefault(y => y.DeviceId == x.Id)?.LocationsCount)
                                             .GetValueOrDefault();

                return deviceModel;
            }).ToList());
        }
예제 #14
0
        public static DeviceResponseModel DomainToResponse(this DeviceDomainModel @this)
        {
            DeviceResponseModel model = new DeviceResponseModel
            {
                device_id     = @this.device_id,
                name          = @this.name,
                serial_number = @this.serial_number,
                user_id       = @this.user_id,
                is_online     = @this.is_online,
                parameters    = new Dictionary <string, dynamic>()
            };

            foreach (var parameter in @this.DeviceParameters)
            {
                model.parameters.Add(parameter.parameter_name.ToLower(), parameter.value);
            }

            return(model);
        }