public void CreateAsset() { _assetServices.GetAssetUid(Arg.Any <Guid>(), Arg.Any <string>(), Arg.Any <string>()).Returns(x => null); _assetServices.GetCustomersForApplication(Arg.Any <string>()).Returns(x => new List <Guid>() { new Guid("8abcf851-44c5-e311-aa77-00505688274d") }); _assetServices.CreateAsset(Arg.Any <CreateAssetEvent>()).Returns(x => true); _assetServices.IsValidMakeCode(Arg.Any <String>()).Returns(x => true); var response = _controller.CreateAsset(GetCreateAssetEvent()); Assert.Equal(200, ((Microsoft.AspNetCore.Mvc.ObjectResult)response).StatusCode); }
public void Create_Asset() { //Arrange for Create var assetObject = new CreateAssetEvent() { AssetName = "TestAssetName", AssetType = "loader", SerialNumber = "TestSerialNumber", AssetUID = Guid.NewGuid(), MakeCode = "TestMake", Model = "model", EquipmentVIN = "equipmentVIN", IconKey = 1, LegacyAssetID = 1, ActionUTC = DateTime.UtcNow, ModelYear = 2016, ReceivedUTC = DateTime.UtcNow, OwningCustomerUID = Guid.NewGuid(), ObjectType = "test", Category = "test", ProjectStatus = "test", SortField = "test", Source = "test", Classification = "test", PlanningGroup = "test", UserEnteredRuntimeHours = "1234" }; _transaction.Execute(Arg.Any <List <Action> >()).Returns(x => { foreach (var action in x.Arg <List <Action> >()) { action(); } return(true); }); //Act Assert.True(_assetServices.CreateAsset(assetObject)); }
public ActionResult CreateAsset([FromBody] CreateAssetEvent asset) { try { //var jwt = GetSampleJwt(); //use this for testing locally 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 (Exception ex) { jwt = null; } if (jwt != null) { _logger.LogInformation("Creating asset for user: {0}", jwt.UserUid); } else { return(BadRequest("no jwt token")); } asset.ReceivedUTC = DateTime.UtcNow; _logger.LogDebug($"CreateAsset - Calling Application Name: {jwt.ApplicationName}, UserType: {jwt.UserType}"); bool isCGIntegrator = jwt.UserType == "APPLICATION" && jwt.ApplicationName == _configuration["CGIntegratorAppName"]; if (isCGIntegrator) { if (asset.AssetUID == null || asset.AssetUID.Equals(Guid.Empty) || !Guid.TryParse(asset.AssetUID.ToString(), out Guid g)) { return(BadRequest("AssetUID must be given for VisionLink integrator")); } if (asset.OwningCustomerUID == null) { asset.OwningCustomerUID = new Guid(); } } else if (!string.IsNullOrEmpty(jwt.ApplicationName)) { asset.AssetUID = Guid.NewGuid(); var customers = _assetService.GetCustomersForApplication(jwt.ApplicationName); if (customers?.Count > 0) { if (asset.OwningCustomerUID == null) { asset.OwningCustomerUID = customers.First(); } } else { return(BadRequest("Application does not have any customers mapped. Please contact your API administrator.")); } Guid?existingAssetGuid = _assetService.GetAssetUid(asset.AssetUID.Value, asset.MakeCode, asset.SerialNumber); if (existingAssetGuid.HasValue) { return(StatusCode((int)HttpStatusCode.Conflict, (existingAssetGuid.Value.ToString()))); } } else { return(BadRequest("jwt application name is empty")); } Guid?existingAsset = _assetService.GetAssetUid(asset.AssetUID.Value, asset.MakeCode, asset.SerialNumber); if (existingAsset.HasValue) { if (isCGIntegrator) { var updatePayload = new UpdateAssetEvent { AssetUID = existingAsset.Value, OwningCustomerUID = asset.OwningCustomerUID, LegacyAssetID = asset.LegacyAssetID, AssetName = asset.AssetName, Model = asset.Model, AssetType = asset.AssetType, IconKey = asset.IconKey, EquipmentVIN = asset.EquipmentVIN, ModelYear = asset.ModelYear, ActionUTC = DateTime.UtcNow, ObjectType = asset.ObjectType, Category = asset.Category, ProjectStatus = asset.ProjectStatus, SortField = asset.SortField, Source = asset.Source, UserEnteredRuntimeHours = asset.UserEnteredRuntimeHours, Classification = asset.Classification, PlanningGroup = asset.PlanningGroup }; var updateResult = UpdateAssetInfo(updatePayload); return(updateResult.GetType().Name == "OkObjectResult" ? StatusCode((int)HttpStatusCode.Conflict, (existingAsset.Value.ToString())) : updateResult); } return(Conflict(new { message = $"Asset already exists" })); } if (!_assetService.IsValidMakeCode(asset.MakeCode.ToUpper())) { return(BadRequest($"Asset make code '{asset.MakeCode.ToUpper()}' is not valid.")); } if (_assetService.CreateAsset(asset)) { return(Ok(asset.AssetUID.ToString())); } return(BadRequest("Unable to save to db. Make sure request is not duplicated and all keys exist")); } catch (Exception ex) { _logger.LogError("Create Asset Exception: " + ex.ToString()); return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message)); } }