Update and Delete Api Calls

Okay. I'm still in the IDE and I'm still in the same basic Todo flow. We're going to add in a few more calls. This time we're going to just make a call to update a task and then to delete a task as well. Let's start with the update Todo. What we're going to do is we're just going to update the Todo that we capture up here like so. When we created this Todo, remember, we captured this ID. Let's just update that same Todo, to say given path of the ID, the ID that we captured because we're updating it Todo we need to add in another body as well. Let's say request title, the title first still then for this one, will say complete this update the complete to be True this time. Now we do when method Put. 


Remember before we did a Get and a Post. But when we do a Put, that's the HTTP verb when we want to update something to an endpoint. Let's check the response that we get back is a 200. And that's also check response status is now updated to True. So say, And match response.complete. Make sure that that equals to True. Looks good. Go ahead and run that quickly from Maven just to make sure it's working. Okay. That looks good. Finally, let's go ahead and delete the Todo as well, just so that we're making sure that we're doing all of our different API calls that are on offer. Let's just delete the same Todo that we captured. We say given path of ID. Again, Karate is going to append that ID to the URL.


This time we're going to say When method delete. It's not a Get not a Post, not a Put but at this time, we would expect to get a 200 response back when we run the Delete call. Just run that quickly. And that's passed. That's good. That looks like it's deleting as well. We're getting into a state now where we've got quite a lot of Todos in our applications. What might be good is if we make that call, that clears down all of the Todos at the end of this scenario. Let's just add in the API call that does that. I'll just put a comment in here. So say clear all tasks to clear all the tasks we have to call a certain endpoint. 


So this time I'm going to just type in the full URL of the endpoint just because it's slightly different to this one up here. It doesn't have API Todos, it's localhost:8080 it is an API, but it's just at resets this time, so it's not at this Todos endpoint, we just skip that we just call straightaway this reset endpoint. We need to make a Get call to that. We say one method Get checked with the status we get back is a 200. And I think when we call that, we also got a JSON response back as well. Let's just try and match on that. So just check that the response shows deleted and then it shows a certain number that got deleted as well. We're not going to be too sure what the number is going to be, just as it is going to be a number. To do that, we can just do hash number. So if we knew that it was going to be 10 deleted, for example, we could put 10 in. But for us, we just put in hash number. Now I'll just put in 10 and let's just see what happens. I suspect this will fail because we probably have 10 tasks, but let's just see what happens. I'm running this at the moment. Yes, we've actually got 15 tasks not 10, so that's fine.


 We know that this assertion is working. Let's change that now. This time to be hashtag number. I'll save that and then run it again. And this time it works. 


Okay, so now if I just jump back over to the UI, if I refresh now, just after running that Karate test, we can see all the tasks have gone. When we called the API now, it deleted all of the tasks at the end of our scenario in here. What's another enhancement that we can do in our scripts? So if we look here at these titles that we're calling for our Todos were always have to hard code in the whole string of the title. So here I have typed in first, first, first here, again. So if I ever wanted to change that title in this test for some reason I'd have to go through and I'd have to update the string in every different place. What we could do instead is we could just put this into a parameter. And just then, if we ever needed to update the name of the task, we just needed to update the parameter. Let me show you what I mean in the code. What I'm going to do at the top here of this basic to do. I'm just going to define, let's say, a task name. Let's just give it the task name of firstTask. I need to make sure I put a * in front to make sure we do * Def task name equals firstTask. So now instead of having the task hardcoded, I can put in that task name parameter.


So to do that, again, we use the hash open brackets and we put in the parameter name. In this case, it's task name. Let me copy that and then let me replace it every time. It says first down here for this title or replace that there like this one. Here, I'll leave the second because that's a different one. The same for here. For the title here instead of it being first, I just want it to call directly the parameter so directly this task name parameter that should come from here because it's not a JSON blob. We don't need to do the hash and the brackets. Replace this first with the task name here. And I think that's awesome. Looks good. Let me go ahead and save that and then we'll run it again. Cool. And that's working. Okay. So that's just refactored our code a little bit just to make it a little bit easier to maintain again, if we ever needed to change a task name, now we can just edit this variable here.

Comments are closed.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}