Esempio n. 1
0
        public void GetAssets_UsingSearchandFilter()
        {
            int pageSizeInt;
            int pageNumberInt;

            _controllerUtilities.ValidatePageParameters(Arg.Any <string>(), Arg.Any <string>(), out pageSizeInt, out pageNumberInt);
            _assetServices.GetCustomersForApplication(Arg.Any <string>()).Returns(x => null);

            _controllerUtilities.ValidateAssetUIDParameters(Arg.Any <string[]>()).Returns(x => new Guid[] { Guid.NewGuid() });
            _searchAndFilterClient.QueryFleet(Arg.Any <AssetFleetQueryParameters>()).Returns(x => new AssetFleetResponseParameters()
            {
                AssetUIDs = new Guid[] { Guid.NewGuid() }
            });

            _assetServices.GetAssets(Arg.Any <Guid[]>(), Arg.Any <Guid>()).Returns(x =>
                                                                                   new List <VSS.MasterData.WebAPI.DbModel.Asset>()
            {
                new VSS.MasterData.WebAPI.DbModel.Asset {
                    AssetUID = "ceea76f5-84de-49c2-b3fb-7463b9697f9c", SerialNumber = "2FMPK4J96HBB29959", MakeCode = "FA1", AssetName = "", AssetTypeName = "qwe", EquipmentVIN = "45", IconKey = 1, LegacyAssetID = 0, Model = "rt", ModelYear = 2017, StatusInd = true
                },
                new VSS.MasterData.WebAPI.DbModel.Asset {
                    AssetUID = "f29ec652-7e35-464a-a246-d869ad233e69", SerialNumber = "2FMPK4J96HBB29950", MakeCode = "FA2", AssetName = "", AssetTypeName = "erw", EquipmentVIN = "345", IconKey = 1, LegacyAssetID = 0, Model = "rtt", ModelYear = 2017, StatusInd = true
                }
            });

            var response = _controller.GetAssets(null, "7a0d9d17-b55c-4a59-af43-579e2588d9ba", null, null, null, null, "57d", "10", "1", null, null);

            Assert.Equal(200, ((Microsoft.AspNetCore.Mvc.ObjectResult)response).StatusCode);


            _controllerUtilities.ValidateAssetUIDParameters(Arg.Any <string[]>()).Returns(x => new Guid[] { });
            response = _controller.GetAssets(null, "7a0d9d17-b55c-4a59-af43-579e2588d9ba", null, null, null, null, "57d", "10", "1", null, null);
            Assert.Equal(200, ((Microsoft.AspNetCore.Mvc.ObjectResult)response).StatusCode);
        }
Esempio n. 2
0
        public void GetAssetDeviceDetails_BadRequest()
        {
            var assetUIDs = new AssetDeviceRequest()
            {
                AssetUIDs = new List <string>()
                {
                    "a162eb79-0317-11e9-a988-029d68d36a0i", "a162eb79-0317-11e9-a988-029d68d36a0g"
                }
            };
            var response = _controller.GetAssetDeviceDetails(assetUIDs);

            Assert.Equal(400, ((Microsoft.AspNetCore.Mvc.ObjectResult)response).StatusCode);

            _controllerUtilities.ValidateAssetUIDParameters(Arg.Any <string[]>()).Returns(x => throw new Exception());

            response = _controller.GetAssetDeviceDetails(assetUIDs);
            Assert.Equal(400, ((Microsoft.AspNetCore.Mvc.ObjectResult)response).StatusCode);
        }
Esempio n. 3
0
        public ActionResult GetAssets(
            [FromQuery] string[] assetUIDs    = null, [FromQuery] string customerUid = null,
            [FromQuery] string[] assetType    = null, [FromQuery] string[] status    = null,
            [FromQuery] string[] manufacturer = null, [FromQuery] string[] model     = null,
            [FromQuery] string snContains     = null,
            [FromQuery] string pageSize       = "10", [FromQuery] string pageNumber = "1",
            Guid?userGuid = null, Guid?accountSelectionGuid = null)
        {
            bool isIntegrator = false;

            try
            {
                int         pageSizeInt;
                int         pageNumberInt;
                Guid?       customerGuid;
                List <Guid> customerGuids = new List <Guid>()
                {
                };

                _logger.LogDebug("Before JWT Call.Requestheaders:" + String.Join(",", Request.Headers.Select(x => x.Key + ":" + x.Value)));

                if (!Request.Headers.TryGetValue("X-JWT-Assertion", out StringValues headerValues))
                {
                    return(BadRequest("Could not validate X-VisionLink-UserUid or X-JWT-Assertion Headers in Request"));
                }

                TPaaSJWT jwt;
                try
                {
                    jwt = new TPaaSJWT(headerValues);
                }
                catch
                {
                    jwt = null;
                }


                if (jwt != null)
                {
                    if (userGuid == null || !userGuid.HasValue)
                    {
                        userGuid = jwt.UserUid;
                    }

                    _logger.LogDebug(string.Format("ApplicationName:{0},uuid:{1},encodedjwt:{2}", jwt.ApplicationName, jwt.UserUid, jwt.EncodedJWT));

                    if (!string.IsNullOrEmpty(jwt.ApplicationName))
                    {
                        _logger.LogInformation("JWT has an appname " + jwt.ApplicationName);
                        var customers = _assetService.GetCustomersForApplication(jwt.ApplicationName);                         // new List<Guid>() { new Guid("07D4A55244C5E311AA7700505688274D") };//

                        if (customers != null && customers.Count > 0)
                        {
                            _logger.LogInformation("CustomerCount>0" + new Guid(customers[0].ToString()));
                            customerGuid = !string.IsNullOrEmpty(customerUid) ? new Guid(customerUid) : (Guid?)null;
                            if (customerGuid.HasValue)
                            {
                                if (customers.Contains((Guid)customerGuid))
                                {
                                    customerGuids.Add((Guid)customerGuid);
                                    isIntegrator = true;
                                }
                                else
                                {
                                    return(BadRequest("Application does not have this customer mapped. Please contact your API administrator."));
                                }
                            }
                            else
                            {
                                customerGuids = customers;
                                isIntegrator  = true;
                            }
                        }
                        if (isIntegrator)
                        {
                            try
                            {
                                _controllerUtilities.ValidatePageParameters(pageSize, pageNumber, out pageSizeInt, out pageNumberInt);
                            }
                            catch (Exception ex)
                            {
                                _logger.LogError("Get Assets parameter validation threw an exception", ex);
                                return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message));                               //BadRequest("Assets parameter validation failed.");
                            }
                            if (customerGuids != null && customerGuids.Any())
                            {
                                CustomerAssetsListData assets = _assetService.GetAssetsForCustomer(customerGuids, pageNumberInt, pageSizeInt);
                                return(Ok(assets));
                            }
                        }
                        else
                        {
                            if (!userGuid.HasValue)
                            {
                                return(BadRequest("UserUID has not been provided"));
                            }

                            Guid[]          assetGuids;
                            WildcardMatches wildcardMatches;
                            try
                            {
                                assetGuids      = _controllerUtilities.ValidateAssetUIDParameters(assetUIDs);
                                wildcardMatches = ValidateWildcardMatches(null, snContains);
                                _controllerUtilities.ValidatePageParameters(pageSize, pageNumber, out pageSizeInt, out pageNumberInt);
                                customerGuid = !string.IsNullOrEmpty(customerUid) ? new Guid(customerUid) : (Guid?)null;
                            }
                            catch (Exception ex)
                            {
                                _logger.LogError("Get Assets parameter validation threw an exception", ex);
                                return(BadRequest(" Assets parameter validation " + ex.Message));                                //StatusCode((int)HttpStatusCode.InternalServerError, ex.Message);//
                            }

                            if (!assetGuids.Any())
                            {
                                assetGuids = GetAssetUIDsFromFilters(
                                    userGuid.Value, accountSelectionGuid, customerGuid, assetType, status, manufacturer, model,
                                    wildcardMatches, pageSizeInt, pageNumberInt);
                            }

                            if (assetGuids != null && assetGuids.Any())
                            {
                                var assets = GetAssetsFromAssetGuids(assetGuids, userGuid.Value);
                                return(Ok(assets));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Get AssetLists encountered an error", ex);
                return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message));
            }
            return(Ok(isIntegrator ? (object)new List <DataModels.CustomerAsset>() : (object)new List <DataModels.Asset>()));
        }
Esempio n. 4
0
        public ActionResult GetAssetDetail([FromQuery] Guid?assetUID = null, [FromQuery] Guid?deviceUID = null)
        {
            try
            {
                if (assetUID == null && deviceUID == null)
                {
                    return(BadRequest("AssetUID/DeviceUID has not been provided"));
                }
                try
                {
                    if (assetUID != null && deviceUID != null)
                    {
                        _controllerUtilities.ValidateAssetUIDParameters(new[] { assetUID.ToString(), deviceUID.ToString() });
                    }
                    else if (assetUID != null)
                    {
                        _controllerUtilities.ValidateAssetUIDParameters(new[] { assetUID.ToString() });
                    }
                    else if (deviceUID != null)
                    {
                        _controllerUtilities.ValidateAssetUIDParameters(new[] { deviceUID.ToString() });
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError("Get Assets parameter validation threw an exception", ex);
                    return(BadRequest(ex.Message));
                }

                if (assetUID != null || deviceUID != null)
                {
                    AssetSubscriptionModel           subscription      = null;
                    List <ClientModel.AssetCustomer> lstAssetCustomers = null;
                    List <AssetDetail> assetDetails = (List <AssetDetail>)_assetRepository.GetAssetDetail(assetUID, deviceUID);
                    if (assetDetails == null)
                    {
                        _logger.LogInformation($"No asset with UID {assetUID} exists");
                        return(StatusCode((int)HttpStatusCode.NoContent, assetUID));
                    }
                    List <AssetDeviceDetail> lstAssetDetails = new List <AssetDeviceDetail>();
                    foreach (AssetDetail assetDetail in assetDetails)
                    {
                        if (assetDetail.AssetUID != null)
                        {
                            subscription = new AssetSubscriptionModel();
                            subscription = GetSubscriptionfromAPI(new Guid(assetDetail.AssetUID));

                            lstAssetCustomers = new List <ClientModel.AssetCustomer>();
                            lstAssetCustomers = GetAssetCustomersfromAPI(new Guid(assetDetail.AssetUID));
                        }

                        AssetInfo assetInfo = null;
                        ClientModel.DeviceModel deviceInfo = null;

                        if (assetDetail != null)
                        {
                            if (assetDetail.AssetUID != null)
                            {
                                assetInfo = new AssetInfo
                                {
                                    AssetName    = assetDetail.AssetName,
                                    AssetType    = assetDetail.AssetTypeName,
                                    AssetUID     = assetDetail.AssetUID != null ? (Guid?)new Guid(assetDetail.AssetUID) : null,
                                    MakeCode     = assetDetail.MakeCode,
                                    Model        = assetDetail.Model,
                                    ModelYear    = assetDetail.ModelYear,
                                    SerialNumber = assetDetail.SerialNumber
                                };
                            }

                            deviceInfo = new ClientModel.DeviceModel
                            {
                                DeviceSerialNumber = assetDetail.DeviceSerialNumber,
                                DeviceState        = ((DeviceStateEnum)Enum.Parse(typeof(DeviceStateEnum), assetDetail.DeviceState)).ToString(),                          //assetDetail.DeviceState,
                                DeviceType         = assetDetail.DeviceType,
                                DeviceUID          = assetDetail.DeviceUID != null ? (Guid?)new Guid(assetDetail.DeviceUID) : null
                            };
                        }
                        AssetDeviceDetail assetDet = new AssetDeviceDetail
                        {
                            AssetInfo    = assetInfo,
                            DeviceInfo   = deviceInfo,
                            Subscription = subscription,
                            AccountInfo  = lstAssetCustomers
                        };
                        lstAssetDetails.Add(assetDet);
                    }
                    return(Ok(lstAssetDetails));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("GetAssetDetail encountered an error", ex);
                return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message));
            }
            return(Ok(new List <VSS.MasterData.WebAPI.DbModel.Asset>()));
        }