Hi, guys, my name is Karthik and I am from Automation Dotcom, and welcome to Part 21 of our Appia
testing with the rest assured.
And of course and in this video, we are going to be talking about contact API testing with builder
patterns.
And again, guys, this video is not completely related to our previous videos that we have discussed.
You can also watch this video in isolation, even if you have not watched the previous videos.
But watching the previous videos is going to give you more confidence off the core that we have discussed.
But in this video, we are going to talk about a separate concept, which is how you can maintain your
Podio classes of our API and also how you maintain the contracts of the poggio classes that we have
built all these days and our previous videos using builder patterns.
So this is going to be a completely new topic that you're talking about, and we have fused that within
our own API testing.
The rest assured, of course, just to ensure that we don't miss this particular concept in our course.
All right, let's get started.
So before talking about the builder pattern itself, we are going to talk about the contract testing.
And I have discussed about contract testing a lot in our weekly video series of YouTube.
So you can go and watch that particular video.
But I have discussed about contract testing and I can contract testing is immediately applicable anywhere
that you have to, services that need to communicate that as an API client and a Web front end.
Although a similar service is a common use case, contract testing really shines an environment with
many services.
So this is something very, very important for our kids as well, because as we know in our previous
videos we discussed about the fake design server, which generates the JSON and we try to D.C. realize
that and serialize those jasen files into a class file and from classifier to jasen file and all those
things we did.
But all these days, we just had an assumption that the APIs over that we have is always going to return
as only this particular format of the jasen response.
But what if something changes in the server side and our test will start to fail automatically because
we don't really have any contract with the Jasen server, which is nothing.
But the fact is answer in our case with the code that we are writing.
So we need to make a contract between a fake design server and with our test score so that our test
will always works without any problem.
And if anything fails, then probably we'll know that which particular property of our Web server has
changed and why our test is failing in this particular area.
So these are something that is very, very important that we could probably understand while doing with
the contract testing.
So there are many different tools available in the market for doing contract testing.
I don't know.
The popular contract testing tool is packed.
And once again, guys, I have discussed about Pact and how to use Pact Darknet in the EIA weekly video
so you can go ahead and watch there.
And again, I'm not going to be talking about the contract testing in this place because the whole topic
is around builder patterns.
So that's what we'll be discussing.
But just to make you understand that there is a concept of contract testing, that's why I'm introducing
you this in here.
But building a pattern is what we are going to be focusing on, on how are we going to achieve contract
testing with the builder patterns.
So the builder is a design pattern designed to provide a flexible solution to various object creation
problems in object oriented programming.
The Builder Department allows the creation of different representations of an object using the same
construction code.
That is very, very important.
And we'll talk about that in a minute in our next slide.
But as you can see, the builder design patterns solves the problems, like how can a class creates
different representations of a complex object and how can a class that includes creating a complex object
be simplified?
So if you don't really understand what these theories are talking about, I'll quickly show you a quote
example so that you can understand what it is.
So as you can see, we have a Podio class again.
Poggio is nothing but a plain old Jawa object class of the post.
Again, this particular class, we have already discussed no previous videos of this class.
So please go and watch that.
If you have not lost that, as you can see in here, we have this particular post class, which does
have properties like private properties, and it has a constructor called Post and it has a go getter
and of course, it has a setter.
And if you could see here, there are just three properties available, private properties and only
three getters are available.
And there will be three separate methods available.
And as you can see, there is only one construction.
But you can still see there are a lot of permutations and the combination of constructors can be created,
something like a default constructor and a constructor with just one parameter and a constructor with
two parameters and the constructor with three parameters, as you are seeing here.
And similarly, there are constructors that you can mix and match the.
Accommodations on the combination, something like a constructor just with Idy and a constructor with
ID and author and a constructor with an idea and title and then a contractor with title and other things
of that nature.
So you can see that for a particular given type in this case post, we can have a different kinds of
telescopic constructors and these telescopic constructors can be complex and even more vague if our
object is getting very, very complex and in order to resolve these kinds of situations, builder pattern
comes in the picture.
So as you can see, this is a static class called a builder class.
And again, you can name it anything, but it is a standard way of calling it.
And you can see here it tries to simplify the way we create the constructors.
So you can see I'm still calling the Post's class, which we saw in our previous slide, and I'm parsing
in all the properties which is required within a constructor for this particular builder class.
And in here I'm setting all the values, something like this, which is pretty cool.
So whichever property that you to be setting it, you only call that particular set operation so that
these values will be set for it automatically.
And you don't really have to worry about how these people values are being set because they're going
to be calling only that particular properties method to just set that particular value.
And the getter is going to be sitting within the Post's class, not within the middle class, which
is going to oversimplify the way that we're are creating a constructor in here.
And you can see that that is only one constructor spin car, which is something about the Post's class,
three parameters based constructor.
And this reduce our code dramatically.
So how to call this particular builder class?
Basically, it's going to be something like this.
As you can see.
I'm just going to call the postcard builder Duck-billed.
That's it.
And I can create a type of post and then I can get the value out from it.
But if you want to set any value, then I'm going to do that.
Builder Dot set it or set title and set order and then you can do that as well, which is going to be
very, very easy in the builder pattern side.
So essentially builder partners get rid of the problems with a complex object which has telescopic constructor
in much easier way through squeakily see everything in action and understand how things work so far
that I'm gonna flip to my intelligent idee.
And again, for this particular video, I'm going to be using the source code, which is available below
here.
All right.
Now in my intelligent idee, and you can see that this is the same project that we have already discussed
in all our videos and our earlier videos.
And what I'm going to do is this time and I'm going to be working with the builder pattern based class.
So I'm just going to convert only this particular class, which is nothing but the POS class.
And I'm going to convert that to the builder pattern in here.
So what I'm going to do, if I remember in our earlier videos, we created this class with two constructors,
something like this.
And we also had a problem that without having the default constructor, the code was throwing us an
error while we trying to execute this.
But now, in order to resolve all these problems, that's where we are going to be calling our builder
patterns, basically.
So I'm going to get rid of these SEDAR at least from in here.
So I don't really require this.
And again, it's gone and I'm going to be calling a public static.
Class of builder and witness builder, I'm going to set the same three properties that we created in
our earlier videos over here, and then I'm going to create default constructors, which is going to
be public builder, and then I need all the sitter, basically.
So I'm going to generate once again, just get all the setters over here, which is pretty awesome.
But the change in here for the SATs, instead of returning it to the wide, I'm going to return it as
a builder.
That's the creation of a design pattern, basically does.
You're going to return all the builder type over here for each and every set operation that you're going
to be doing.
I just done this, not the builder knew this and that I wanted to done this and this way.
This is going to be builder as well, and this is going to be builder.
And I'm going to be returning this over here so you can see the builder patterns.
I could able to create a set operation with returning all of the this type schismatic with a builder
type.
And this way I can also ensure that I have an isolated object for each and every properties that I'm
actually working with.
And finally, I'm going to be calling or post a certain type for the build method and I'm going to return
the new posts where I'm going to pass the Idy title and author.
So this is the builder pattern, guys.
This is what I'm talking about.
So this way, what will happen eventually is will produce so much, of course, that we are writing
all these days and know how to call this particular builder pattern or code.
So for that, I'm going to go to my get post that feature.
And if I go all the way to the I think this one, then I should see the author name as well.
Where here.
This is where I have actually called the post of this one.
So this is a common piece of code maybe.
And I don't really have the common duplicate piece of code.
So this code is without builder pattern.
Right, and this time I want to call with Miller, Palin is going to be Vare of Posts, a sequel to
New Post Dot.
I'm going to call the builder constructor in here, and then I just call the builder.
So basically, you can also do the starter, set it and title over here, because I don't really have
to set any value, at least at this particular point.
I'm just going to call the bird method over here.
And finally, I'm going to do the post operation.
So the post operation is going to be pretty much like how I did in here.
So maybe I can just call this.
Something like this.
Right.
So this can be a bar type and instead of the post start class, I'm just going to do the posts dot get
class so that I can make this as a class and now I can just call the post dot get Arthur.
Which is this one, and because this picture post actually has all the GETER operation, I get author
title and idy, I can easily call the author or hear that's it.
So this way it reduces dramatically a lot of ways that we can reduce the constructor.
But now you may have a question like what difference that this particular code has made.
Eventually this has even increased the number of lines of code here then compared to before.
And again, guys don't think of this particular situation over here that this particular code is actually
very, very simple.
It has only three properties, but in real time situation with any of the other classes, you would
see there will be a lot of different properties and you need to have a different permutations and combinations
of constructor.
But in here, we're not even worried about all those constructor.
We just call in here and it just works on that only problem.
So as I said, I'm just going to run this piece of code and see what's going to happen.
So if I just run this particular selector scenarios, so let's see what's going to happen.
There we go.
The test has got failed, so let's quickly see what has gone wrong.
Oops is because of this token, because we have removed that token in our earlier videos to be a static
token.
So I'm just going to include that over here.
So that's the problem.
I'm just gonna run this again.
And you can see that the court has successfully completed the execution and the test has not passed,
so this way we can see that we could be able to use the builder pattern to perform the operation, and
it increases so much of our coding practice much, much better.
And now you have a question like this particular piece of code has introduced are the builder pattern
has really introduced so many different lines of code than before.
So how to reduce this particular lines of code and how we can make our code even more neater than before,
even though without builder patterns we can at least see the getter and setter.
But then we also see a static class, which is something increasing some more lines of code in here.
So be sure to have some way to reduce this particular lines of code.
So how to get rid of all this problem you have going?
We're talking about Leimbach plugging in our next video to see how we can reduce the number of lines
of code.
Thank you.