public async Task CreateValidAction() { var session = await Login(); using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) }) { client.AcceptJson().AddSessionHeader(session.Id.ToString()); var newAction = await CreateTestAction(); var actionForm = new Action { Verb = "testerVerb", ActivityId = newAction.ActivityId, GoalId = newAction.GoalId }; var actionResponse = await client.PostAsJsonAsync("/api/actions", actionForm); Assert.Equal(HttpStatusCode.Created, actionResponse.StatusCode); var action = await actionResponse.Content.ReadAsJsonAsync <Action>(); Assert.IsType(typeof(Action), action); } }
protected async Task<Action> CreateTestAction(string verb = "Created for test goal and activity") { var session = await Login(); using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) }) { client.AcceptJson().AddSessionHeader(session.Id.ToString()); var action = new Action { Verb = verb, Goal = new Goal { Concern = new ConcernMatrix { Coordinates = new Matrix { X = 1, Y = 1 }, Category = 0 }, RewardResource = new RewardResourceMatrix { Coordinates = new Matrix { X = 2, Y = 2 }, Category = 0 }, Feedback = new GoalFeedback { Threshold = 0, Target = 0, Direction = 0 }, Description = "Created for test cases" }, Activity = new Activity { Name = "Testing" } }; var actionResponse = await client.PostAsJsonAsync("/api/actions", action); Assert.Equal(HttpStatusCode.Created, actionResponse.StatusCode); var created = await actionResponse.Content.ReadAsJsonAsync<Action>(); return created; } }
public async Task CreateActionWithInvalidGoalId() { var session = await Login(); using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) }) { client.AcceptJson().AddSessionHeader(session.Id.ToString()); var newAction = await CreateTestAction(); var actionForm = new Action { Verb = "testerVerb", ActivityId = newAction.ActivityId, GoalId = new Guid() }; var actionResponse = await client.PostAsJsonAsync("/api/actions", actionForm); Assert.Equal(HttpStatusCode.NotFound, actionResponse.StatusCode); var content = await actionResponse.Content.ReadAsJsonAsync <ApiError>(); Assert.Equal($"No Goal found", content.Error); } }
public async Task UpdateInvalidAction() { var session = await Login(); var invalidId = Guid.NewGuid(); using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) }) { client.AcceptJson().AddSessionHeader(session.Id.ToString()); var newAction = await CreateTestAction(); var actionForm = new Action { Verb = "testerVerb", ActivityId = newAction.ActivityId, GoalId = newAction.GoalId }; // Get action with Invalid Id var actionResponse = await client.PutAsJsonAsync($"/api/actions/{invalidId}", actionForm); Assert.Equal(HttpStatusCode.NotFound, actionResponse.StatusCode); var content = await actionResponse.Content.ReadAsJsonAsync <ApiError>(); Assert.Equal($"No such Action found.", content.Error); } }
public async Task <IActionResult> PutAction([FromRoute] Guid id, [FromBody] Action action) { if (!ModelState.IsValid) { return(HttpResponseHelper.BadRequest(ModelState)); } var actionMatch = await _context.Actions.Where(g => g.Id.Equals(id)).FirstOrDefaultAsync(); if (actionMatch == null) { return(HttpResponseHelper.NotFound("No such Action found.")); } _context.Entry(actionMatch).State = EntityState.Modified; var error = await SaveChangesAsync(); if (error != null) { return(error); } return(Ok(actionMatch)); }
protected async Task <Action> CreateTestAction(string verb = "Created for test goal and activity") { var session = await Login(); using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) }) { client.AcceptJson().AddSessionHeader(session.Id.ToString()); var action = new Action { Verb = verb, Goal = new Goal { Concern = new ConcernMatrix { Coordinates = new Matrix { X = 1, Y = 1 }, Category = 0 }, RewardResource = new RewardResourceMatrix { Coordinates = new Matrix { X = 2, Y = 2 }, Category = 0 }, Feedback = new GoalFeedback { Threshold = 0, Target = 0, Direction = 0 }, Description = "Created for test cases" }, Activity = new Activity { Name = "Testing" } }; var actionResponse = await client.PostAsJsonAsync("/api/actions", action); Assert.Equal(HttpStatusCode.Created, actionResponse.StatusCode); var created = await actionResponse.Content.ReadAsJsonAsync <Action>(); return(created); } }
public async Task <IActionResult> PostAction([FromBody] Action action) { if (!ModelState.IsValid) { return(HttpResponseHelper.BadRequest(ModelState)); } if (action.ActivityId != Guid.Empty) { var actTest = await _context.Activities.FindAsync(action.ActivityId); if (actTest == null) { return(HttpResponseHelper.NotFound("Invalid ActivityId.")); } } else if (action.Activity == null) { return(HttpResponseHelper.NotFound("No Activity found")); } if (action.GoalId != Guid.Empty) { var goalTest = await _context.Goals.FindAsync(action.GoalId); if (goalTest == null) { return(HttpResponseHelper.NotFound("Invalid GoalId.")); } } else if (action.Goal == null) { return(HttpResponseHelper.NotFound("No Goal found")); } _context.Actions.Add(action); var error = await SaveChangesAsync(); if (error != null) { return(error); } return(CreatedAtRoute("GetAction", new { id = action.Id }, action)); }
public async Task UpdateRewardByInvalidAction() { var session = await Login(); using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) }) { client.AcceptJson().AddSessionHeader(session.Id.ToString()); var actionForm = new Action { Verb = "invalidVerb" }; var actionResponse = await client.PostAsJsonAsync("/api/actions/send", actionForm); Assert.Equal(HttpStatusCode.NotFound, actionResponse.StatusCode); var content = await actionResponse.Content.ReadAsJsonAsync <ApiError>(); Assert.Equal($"Invalid action verb.", content.Error); } }
public async Task DeleteValidAction() { var session = await Login(); using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) }) { client.AcceptJson().AddSessionHeader(session.Id.ToString()); var newAction = await CreateTestAction(); var actionForm = new Action { Verb = "testerVerb", ActivityId = newAction.ActivityId, GoalId = newAction.GoalId }; var actionResponse = await client.PostAsJsonAsync("/api/actions", actionForm); Assert.Equal(HttpStatusCode.Created, actionResponse.StatusCode); var action = await actionResponse.Content.ReadAsJsonAsync<Action>(); Assert.IsType(typeof(Action), action); var actionResponse2 = await client.DeleteAsync($"/api/actions/{action.Id}"); Assert.Equal(HttpStatusCode.OK, actionResponse2.StatusCode); var actions = await actionResponse2.Content.ReadAsJsonAsync<Action>(); Assert.IsType(typeof(Action), actions); } }
public async Task UpdateRewardByValidAction() { var session = await Login(); var newAction = await CreateTestAction("Verb test" + Guid.NewGuid()); using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) }) { client.AcceptJson().AddSessionHeader(session.Id.ToString()); var newAT = new AttributeType { Name = "testAttribute", DefaultValue = 0f, Type = 0 }; var atResponse = await client.PostAsJsonAsync("/api/attributes/types", newAT); Assert.Equal(HttpStatusCode.Created, atResponse.StatusCode); var attribute = await atResponse.Content.ReadAsJsonAsync<AttributeType>(); var newAR = new ActionRelation { ActionId = newAction.Id, Relationship = 0, ConcernChange = new Matrix { X = 0, Y = 0 }, RewardResourceChange = new Matrix { X = 0, Y = 0 } }; var arResponse = await client.PostAsJsonAsync("/api/actions/relations", newAR); Assert.Equal(HttpStatusCode.Created, arResponse.StatusCode); var arReturn = await arResponse.Content.ReadAsJsonAsync<ActionRelation>(); Assert.IsType(typeof(ActionRelation), arReturn); var newReward = new Reward { AttributeTypeId = attribute.Id, TypeReward = RewardType.Store, Value = 1.5f, Status = 0, GoalId = newAction.GoalId }; var rewardResponse = await client.PostAsJsonAsync("/api/rewards", newReward); Assert.Equal(HttpStatusCode.Created, rewardResponse.StatusCode); var newReward2 = new Reward { AttributeTypeId = attribute.Id, TypeReward = RewardType.Modify, Value = 1f, Status = 0, GoalId = newAction.GoalId, ActionRelationId = arReturn.Id }; var rewardResponse2 = await client.PostAsJsonAsync("/api/rewards", newReward2); Assert.Equal(HttpStatusCode.Created, rewardResponse2.StatusCode); var actionForm = new Action { Verb = newAction.Verb }; var actionResponse = await client.PostAsJsonAsync("/api/actions/send", actionForm); Assert.Equal(HttpStatusCode.OK, actionResponse.StatusCode); var rewardReturn = await actionResponse.Content.ReadAsJsonAsync<Reward>(); Assert.IsType(typeof(Reward), rewardReturn); } }
public async Task UpdateRewardByInvalidAction() { var session = await Login(); using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) }) { client.AcceptJson().AddSessionHeader(session.Id.ToString()); var actionForm = new Action { Verb = "invalidVerb" }; var actionResponse = await client.PostAsJsonAsync("/api/actions/send", actionForm); Assert.Equal(HttpStatusCode.NotFound, actionResponse.StatusCode); var content = await actionResponse.Content.ReadAsJsonAsync<ApiError>(); Assert.Equal($"Invalid action verb.", content.Error); } }
public async Task CreateActionWithInvalidGoalId() { var session = await Login(); using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) }) { client.AcceptJson().AddSessionHeader(session.Id.ToString()); var newAction = await CreateTestAction(); var actionForm = new Action { Verb = "testerVerb", ActivityId = newAction.ActivityId, GoalId = new Guid() }; var actionResponse = await client.PostAsJsonAsync("/api/actions", actionForm); Assert.Equal(HttpStatusCode.NotFound, actionResponse.StatusCode); var content = await actionResponse.Content.ReadAsJsonAsync<ApiError>(); Assert.Equal($"No Goal found", content.Error); } }
public async Task UpdateInvalidAction() { var session = await Login(); var invalidId = Guid.NewGuid(); using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) }) { client.AcceptJson().AddSessionHeader(session.Id.ToString()); var newAction = await CreateTestAction(); var actionForm = new Action { Verb = "testerVerb", ActivityId = newAction.ActivityId, GoalId = newAction.GoalId }; // Get action with Invalid Id var actionResponse = await client.PutAsJsonAsync($"/api/actions/{invalidId}", actionForm); Assert.Equal(HttpStatusCode.NotFound, actionResponse.StatusCode); var content = await actionResponse.Content.ReadAsJsonAsync<ApiError>(); Assert.Equal($"No such Action found.", content.Error); } }
public async Task UpdateRewardByValidAction() { var session = await Login(); var newAction = await CreateTestAction("Verb test" + Guid.NewGuid()); using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) }) { client.AcceptJson().AddSessionHeader(session.Id.ToString()); var newAT = new AttributeType { Name = "testAttribute", DefaultValue = 0f, Type = 0 }; var atResponse = await client.PostAsJsonAsync("/api/attributes/types", newAT); Assert.Equal(HttpStatusCode.Created, atResponse.StatusCode); var attribute = await atResponse.Content.ReadAsJsonAsync <AttributeType>(); var newAR = new ActionRelation { ActionId = newAction.Id, Relationship = 0, ConcernChange = new Matrix { X = 0, Y = 0 }, RewardResourceChange = new Matrix { X = 0, Y = 0 } }; var arResponse = await client.PostAsJsonAsync("/api/actions/relations", newAR); Assert.Equal(HttpStatusCode.Created, arResponse.StatusCode); var arReturn = await arResponse.Content.ReadAsJsonAsync <ActionRelation>(); Assert.IsType(typeof(ActionRelation), arReturn); var newReward = new Reward { AttributeTypeId = attribute.Id, TypeReward = RewardType.Store, Value = 1.5f, Status = 0, GoalId = newAction.GoalId }; var rewardResponse = await client.PostAsJsonAsync("/api/rewards", newReward); Assert.Equal(HttpStatusCode.Created, rewardResponse.StatusCode); var newReward2 = new Reward { AttributeTypeId = attribute.Id, TypeReward = RewardType.Modify, Value = 1f, Status = 0, GoalId = newAction.GoalId, ActionRelationId = arReturn.Id }; var rewardResponse2 = await client.PostAsJsonAsync("/api/rewards", newReward2); Assert.Equal(HttpStatusCode.Created, rewardResponse2.StatusCode); var actionForm = new Action { Verb = newAction.Verb }; var actionResponse = await client.PostAsJsonAsync("/api/actions/send", actionForm); Assert.Equal(HttpStatusCode.OK, actionResponse.StatusCode); var rewardReturn = await actionResponse.Content.ReadAsJsonAsync <Reward>(); Assert.IsType(typeof(Reward), rewardReturn); } }