Consumer Driven Contracts and Your Microservice Architecture by Marcin Grzejszczak and Josh Long

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right good morning okay let's get it started jingle in jindo buddy hello we are very important business to take care of yeah definitely before we get started here I want you to all when we say open source you need to all smile pretend like you're happy and then we're to take a selfie yeah picture down there okay okay okay oh line like you're happy you're so small yeah yeah okay ready on our marks one two three open source and then and then over here now okay my goodness is gonna be us come on come on you're not you're not doing a good job fulfilling there on the other side so I'm going to count on you and this I decide to do a better job okay one two three open sores that was terrible just terrible crap Fagin ready open sores yeah it was just crap well very good so we can go now right yeah yeah we can go it's all over in the highlight and I've got nothing now Oh welcome everybody very good come on in have a seat please do if you can there are some places here yeah they're spots right there on the stage we've got a lot to cover today don't we a lot a lot so if you are trying to follow along that's fine grab that git repository on the slide there right now take a photo of that keep it for reference for later we're going to go through so much and I don't expect everybody to remember everything that's okay you don't have to remember every line of code that we type and just just instead remember that it's possible and then to there yeah so here we go to the main topic if yours about us so my name is Matt seems a shock as you can see by looking my name I'm polish so I work at pivotal who knows pivotal whoa okay quite a few people so for those who don't know we're doing for example spring and spring cloud and I'm the in spring boot I'm the member of the spring cloud team focusing mostly on those free projects or smooth contract and pipelines today we'll be working with sprinkle contract my twitter handle is a times a shock for those who can't pronounce it go to my blog too much coding calm the Twitter handle is somewhere there a little bit about me my name is Josh long I'm so happy to be here I'm a spring developer advocate on the spring team and I've been a as part of that as part of my work on the spring team for last eight plus years I have been a contributor to a lot of the don't open source projects I have the distinguished record of being the number one number one top ranked most visible most highly acclaimed most lauded most prolific contributor of a bugs but still number one number one number one contributor across all the projects on which I commit more bugs per Clement thank you thank you so you know there's that and I'm a double champion and I do training videos and books and so on so I just finished up the second edition is building micro services with spring boot live lessons with my friend the one the only the amazing spring boot co-founder Phil Webb when I just finished my fifth book with my friend Kenny Bustani who by the by is here at devoxx Poland and I encourage you to go find his talk when it's on and we just finished our book it's called cloud native Java and among many other things of course it talks about how to build applications that survive and thrive in the cloud with spring boot and spring cloud and cloud binder and of course we talked about spring cloud contract in that book as well and for those of you are wondering and I can seat your eyes I can see the curiosity welling up in your eyes for those of you are wondering that bird on the cover is a blue eared Kingfisher anybody who knows anything knows that nobody cares what's in the book at an O'Reilly book it's the cover that makes the book people buy the book or don't buy the book based on the animal so we we did a lot of work that bird is a blue eared Kingfisher it's a bird that is indigenous or in English we would say native to the Indonesian Java Islands it's a bird that is native to Java and birds fly often through the clouds so it's a bird that flies through the clouds and that is native to Java it's a cloud native java bird to bird that it's never mind it's fine give it time and so so there's that and we both have the distinction the privilege of working at pivotal and we have a lot of video Panero lot of great open-source software and we care very much about the open source software you may recognize some of the icons from the slide who's art by the way it was contributed by my teammate ashley mckenna mara some of the icons are here but there's other projects like Apache Tomcat right a rabbim queue etc right lots of different technologies at pivotal and we care about that but let's be very clear it's not the thing that drives us it's not what motivates us it's not the reason we spring out of bed what we care about more than anything else is helping customers and community members and organizations and so on deliver value faster and safer to production and we see that a lot of organizations struggle with this we try and help them as best as we can and part of that move seems to be this sort of exists to this micro services architecture because it allows organizations to decompose larger batches of work into smaller batches micro services and that in turn makes it easier for teams to sort of stay small and lean and reduce the cost of synchronization right instead of having a large group of people with which with whom you need to communicate you have a small team and the cost of preparing and deciding about change is very very small as compared to a large team right micro services enable organizations to go faster and this is not a new or controversial idea right this is sort of the natural natural evolution of economy law right and no Conway's law says that organizations are a mirror image of the communication structures that build them right of the teams that have the communication structures of the teams that build it so if you have an organization that doesn't do a great job of communicating then you are necessarily going to do a poor job of building software that talks to each other and this has been borne out in lots of different studies over the intervening decades and the last 30 or so years since Mel Conway had this insight the one example was a study that looked at open source often compared to proprietary software something of analogous form and function imagine you had the opportunity to look at the source code to stare into the abyss into the Oblivion imagine you had the opportunity to look at the source code from Microsoft Office what would you see stare back at you after stifling the considerable endless amount of vomit imagine what that code would reveal to you as if you compared it to something of analogous form and function like OpenOffice what they realize in this study is that open-source software as developed by open source teams working in different time zones in different languages in different hours at different capacities tends to be much more modular than equivalent software developed by the same team in the same office in the same time zone in the same hours working in the same capacity and the reason is obvious right the cost of communication is cheaper so this far less need to have modularity if I want to make a change and Marcin and I are in the same room I can just say hey buddy would you think of this and we can have a discussion about it we can refactor but if you're in different time zones it's very hard for ever to get on the same phone call let alone the same room right the cost of communication is much harder so they optimize their ability to go faster by stabilizing their API boundaries and that became that becomes a communication channel the teams speak to each other through API that's how they get us get past the need to to have lots of discussions as long as they stay within their API boundary and they keep that stable everything is fine the problem comes of course when they don't keep that API boundaries stable then you need to do that very costly communication and it's much more difficult right because now you've got different pieces of code so today we're going to talk about a way to get around that problem right we've talked about spring he and I both done countless falkland spring cloud which helps you build robust services that do the right thing at runtime but now let's talk about building robust communications right let's try a lease it's trying we'll give it a go right hopefully the demo gods will help us oh yeah so we're going to build a sample application as we do we're going to build a hypothetical sort of car rental service that talks to another service a consumer that consumes an API from a producer right and that's really what we care about we don't care all that much about the domain of building a car rentals we don't care about the domain of building a fraud detection service right these are the examples but let's not pay too much attention to that what we care about is a consumer consuming an API from producers so I'll go ahead and start off so whenever josh is writing some code he's going to either send a request or receive a muse message for me so whenever I am coding it's going to be the second app the producer out so we're going to build a car rental service we're going to build any what is this page so this is my second favorite place on the internet anybody who knows me knows that this is my second favorite place on the Internet can anybody tell me what the first favorite place is production I love production you should love production you should bring your friends bring your family goes early and often as possible production is the happiest place on earth it's better than Disneyland I love production and so should you you should go as early and often as possible but you're if you're not already there then you begin your journey here and start that spring that I own if you need inspiration in the early morning before a cup of tea or coffee start that spring that aisle if your children are restless and can't sleep start that spring that I owe and if you suffer from indigestion after a long night of alcohol abuse in PHP start that spring that I own so we're gonna build a new application here that takes advantage of Springs web support we're going to use the actuator for observability and we're going to use the stub runner and the wire mach dependency yeah well book I think they give the benefits for free so Sigma yeah absolutely let stream rather babies will have them queue of stream processing good points a rabbit stream very good so we're gonna hit generate that'll give us a new project that we can open up in our ID and it doesn't really matter which IDE right we talk about this all the time doesn't matter we're going to use IntelliJ here but you can use whatever you want quick show of hands how many of using IntelliJ just curious very cool how many of using clips right on very good stuff as well what about NetBeans NetBeans yeah do people know that's cool what about Tmax are you here sir MX guy over here there you go there is C every time every talk I do every city country and continent I go to I ask who uses Emacs and he raises his hand it's the same human being the same object identity who uses Emacs I do and he leaves presumably to go to the next conference now okay so we've got a we're going to write a test right in the test-driven style maybe I should change the font can you all see that in the background ctrl alt s on go computer go bigger okay very good so there we're gonna write a test I'm going to write a test that talks to our fraud detection service so we're going to say public come on public void test should return all frauds right and our test is going to just call the rest API using the rest template which is a spring framework HTTP climb it's a workhorse that makes it easy for us to talk to other HTTP based services and so let's assume that we've got the service running on port 8081 and we're going to call the fraud the endpoint and we'll say string duck class and we're going to sign the intervening value to a variable here response entity and then we're going to use that we're gonna use the B via certain from a certain J to assert that entity dot dot git status code value is equal to is equal to 201 and then we're going to do the same thing for the the body when it asserts something about the body that gets returned as well and so we're going to say that the body is equal to this JSON string here so JSON and we'll create some JSON strings Java's perfectly and we also to write JSON that is perfect it's the best language but I mean JavaScript contains Java so it has to be like related so we use that and there we go so there's my test now of course this isn't gonna work is it I don't have that service running so a natural thing to do here would be to use wire mock and I can just say Auto configure who knows why or why our models water more efficient so for those who don't know it wire mark is a stub of an HTTP server so as you do is go Quito like given you execute some method on the class please return this you can do exactly the same thing with the API HTTP so josh is doing is that whenever a request where the method get' is sent to europe frauds please return the status visit any response of status or body with the JSON and status 201 right and we'll use this body here the JSON there we go so there's my wire mock endpoint and we've got our test we're basically mocking out the the a tie to which we want to communicate we're going to hit run okay so I'll run this and let's see I guess you need to be able to write code for this to work okay here we go take two all right you have to set the port still oh yeah a fragment port port equals 80 81 yeah these are I mean this is why write this because something doesn't work which is great right today we're going to do the only talk I think I think we have the privilege of giving the only talk where every single bug we went into is a feature so there we go there's our there's our code seems to be okay it seems to be fine we think of that I mean okay it seems to be working I mean I wasn't listening to what you're saying yeah that's boring so I mean I heard something I'm gonna take this knowledge and try to write the other a meanest you're gonna build the implementing like move aside and give space for the professional right of course okay so I'm going to write the fraud service app I don't need the stub runner I need a verifier and n I need res Docs res dogs generate that for service and now I'm going to open a new screen here unzip fraud service let's open it feets gonna work yep so because actually I don't trust this guy too much he's known for making bugs so I'm going to formalize our agreement right because he said that if I send this request then you will reply in this way so I don't trust him let's formalize that so under source test I'm going to create a directory resources contract so I contract assuming that this will work okay so a contract is an agreement between the consumer and the producer on how the API should look like so let us define this should return a list of frauds I'm using groovy who loves groovy okay not many people who hate gooey okay stop it so I'm going to use groovy but the good would worry because it's statically typed so the IntelliJ the IDE you hook me all the time and writing the code so you need a description it's like should return a list of threads oops and then we have a request and a response in HTTP you send a message you block until you get the response right so he mentioned something like URL should be fraud right yeah Frobisher good and that's what get right yeah so like this sounds like status 101 the body was like an array marching and Josh I guess ok fair enough so what did you just do you create a contract what is you just described so I describe the communication between us but it's not a schema because we don't care about all the possible requests fields and all the possible response fields we care about a certain scenario that for given this particular request please respond like this so that meant to actually implement the services just meant to provide the minimum sort of possible response precisely ok so now I'm going to add a plug-in because the fact that I only wrote the contract does the meanest meanest thing the contract needs to always speak the truth so how you verify it through the truth by running a test so we're going to add the sprinkle contract maven plugin not just artifact idea amazing plug-in version is one one one one release extensions is true in configuration I'm going to add something called a base class for tests and with that do so let me just first articles I can't so you can write at the same time so we need what will happen is that tests will get generated so we need those generated tests to extend from a base class if we wanted to set up some configuration for the for the generated tests so I'm going to add the base class over here in our test directory yep base class okay and now I'm removing this the test that was by default created here oh so it lives the typo so it wouldn't work days base base class ok and now let me run for service let me run maven clean install I had noticed right I remove the test so we're running the stuff and suddenly I had a breaking test and what happened so if I go to target we have generated the sources contract verifier and when I said that if you have a contract in which you say that if you should I get requested for the endpoint return 201 and certain body I'm actually checking that so I'm creating a get request I'm sending that to your endpoint and I'm expecting certain response but you don't have it so it's failing so what I need to do I need to do two things first is to add the missing controller so where is controller class throat controller and we have it was fraud right yeah so that's all right the response entity of list of string so this is just a basic spring MVC solar for yep let us add the missing dependencies response entity status will be 200 this would serve ya but write and erase as list is marching and all right that's good okay and now I also need since when that generated test we're using a rest assure a rest assured we need to set it up well so we do spring runner spring boot he's using this spring MVC test framework and the spring boot testing framework which makes it easy to stand up a context and then talk to objects you can mock out the things that are expensive and you can isolate the object on which you want to act as well by using mock beans this is very convenient because now we can just focus on the thing that we care about yep so we have rest assured walk MVC and I say please set it up for the foot controller that right so of course I'm the very essence of this this talk is that I mean we never make mistakes never is I haven't yet so these days so fast I'm sure of it of course well okay oh oh yeah because it was supposed to be alighting me it was supposed to be 200 201 the status so my I didn't pass the contract I stated right so already some bugs minor things are caught here so like in the implementation I was supposed to do 201 yeah my bad sorry so if we rerun it hopefully it's going to be built so the first thing that the plugin does it converts the groovy base dsl into h test and the second thing it does is that apart from this sub jar of the app we're creating also a stub jar so you see with a classifier stubs what is that if you're using wire mark you can use it like josh did so with code but also you can use the JSON notation so if you go here you'll see that from the DSL we've created also a JSON wire mock definition so definition of a stub this is the perfect bit I mean everybody loves embedding a JSON in a JSON so we're doing that for you you don't have to do it and we're installing it in a jar locally in an hour made em too so the last thing I'm going to do because everything seems to be working I'm going to start this at port 8080 to maybe and off we go there you go okay so what did we do we we were to contract and the contract describes the interactions with our downstream service the a flawed service and now that you've got that service up and running you've made it you've made the test work on your local instance you're on your fraud service implementation your contract is actually talking to your fraud service yep so within that module everything's green yeah I mean it's also agree on your side so I mean gonna work right any work we can charge them up and yeah be able to talk to each other now right so let's go back to the car rental service and so actually we don't really need to even test it because the test has passed right right but let's do it a formality really but let's go ahead and create a integration test yeah yeah okay so I'll say integration I'm just going to create an I'm going to copy that old test that I had before and this would be an integration test that doesn't have wire mock in place right we're just going to talk to the actual service that's running on that other port and that's on a DAT 280 you said so that should all that should be all I need to do I don't actually need this either so just go ahead and nominate it where's it at so thinks is gonna pass should pass who thinks it's not going to pass who doesn't care okay there is put in raise their hand okay a lot of spirit so here we go let's run both of them we should see green all the way oh I mean that it's impossible you had tests on both sides green so what happened we got a 404 that tells me something I mean is it right port 80 82 I mean did you should say like fraud oh so there's a typo should be throwed North roads let's fix that but we had tests right there were great tests the best okay there we are that's green seems consistent we should that's embarrassing that is a little embarrassing that must be very awkward for you yeah I find some way to fix that I don't want that to happen again what so we can use maybe we can use that contract you just build sort of safely talk to that service and get an idea of what that service looks like because I mentioned that we are installing stubs from the producer and the stubs were tested like so to speak against that producer right so they're actually saying the truth agent maybe they reflect the truth as far as that producer knows okay right let's take advantage of that instead let's go ahead and build an application that stands up stubs its then it runs the stubs for us right I'm going to do that using a rule here a JUnit rule rule instance is going to be stubbed when a rule and the stubborn a rule is going to download the stub which we've installed under the following maven coordinates right is a fraud service yeah I guess so okay and we're gonna work offline that means we're not going to try and resolve them against maven public repository and then we're going to use the following port which 883 maybe 80 83 I'll I'll take it 88 83 okay there's our stubborn a rule so when when jane it runs it's going to start that application up and we can now change this and run it again okay now remember the other one the real one is running on port 8080 too so if this is if all these tests pass and they did it's talking to the mock not to the real yeah so if we change this to frauds and run again what will happen I'm hoping that'll catch our mistake so we don't run into this again oh look at that so our our ma our stubs gave us an API against which we could talk but it's a cheap API that we didn't have to deploy the whole service we didn't have to do an integration test basically we don't have to deploy virtual machines or containers or or anything like that right we've got the ability to stand up a very very lightweight HTTP service that responds as we expect it to and that reflects the reality of the service to which we're connecting but there's still you know I'm I'm just gonna go ahead and say it I this is nice and all but we don't need this if we were using something truly decoupled and robust I mean obviously the product it wasn't our fault it was the fault of the protocol right absolutely and I think actually nobody uses HTTP anymore right now it's not you know fun to do it we need something a couple decouple in a synchronous asynchronous asynchronously developers I wanted to be decoupled later right I mean obviously this was the reason for the whole problem right the protocols a problem let's try something else let's do something more robust more decoupled more asynchronous so let have little bit like this I'm gonna send you a message okay send me a message so I'll write a code I'll write some tests here - ok - sort of right through work through how I want that message to be fair enough right okay so we'll try that again this time I'm going to be used do messaging and we can use spring cloud stream spring cloud stream is an abstraction that lets us talk to other services in terms of message queues spring cloud stream builds atong builds upon spring integration and it supports communication thanks to something called a binder and there's lots of different binders out there there's a binder for item queue for patchy cough cough or for JMS coming up there's one for you know all sorts of different technologies and these binders they let us write code in terms of channels and a channel is just basically a pipe through which data flows in and out writes a Java util Q basically in memory but those cues are connected to actual brokers like RabbitMQ so you you put something in the queue and it gets sent out to the broker you expect a message to pop out of the queue and you can process it in a code we don't worry about connecting those queues those channels rather to the broker that's what spring code stream will do for us it becomes just a matter of convention and configuration so let's go to our code here and let's update the code to accept a message that comes in off the wire we'll create a POJO here called a fraud entity and we'll use Lombok here to say that this is going to have all arguments and no arguing constructor and you know can have get into setters and the father can have a field here called name okay so there's our field and what I want to do is overwrite some code that listens for messages coming in on a channel and now I have to tell sprinkled stream about that channel I have to tell it the definition of that channel and we do this using an interface now that interface you know you can provide your own but for the common case of taking data in from a channel and for sending data out from a channel there are some predefined definitions here's one for the input Channel right data coming in on an inbound sort of request this interface has a channel definition all of this other stuff is extra you don't really need it but in our case it makes it easier to say and to distinguish that we want this particular channel in our code so we're going to say it's a sprinkle stream create a reference create a hydrated object that looks like this and then connect it to our to our broker and we'll look at the configuration for that here alright so we'll say spring clad stream bindings dot input which is input is the name of the channel that destination equals fraud so this is the queue or like this topic somewhere in the in the broker yeah this is the broker this is you know a random queue or Patrick Kafka or whatever this is the agreed-upon rendezvous point in the broker against which both producer and consumer must agree and then the thing over here is a channel definition in the interface alright so that's that's going to change from one code base to another so with that in place we can now write a component that listens for messages now right now I don't want to talk to the actual broker I want to do testing right so we're going to test using the the messaging support in spring cloud stream so stream test okay and we'll say scope is equal to test okay now I want to write some code I'm gonna say public void on fraud message fraud okay and what we need to do is to tell sprinkled stream to listen for messages coming in from the input channel and I could have just said input since that's the name it has right but I'm using that convenient variable in the interface itself and what I want to do is just for our purpose is just to get something working into prove it's working is we're going to observe the incoming message and then note the name that's observed so this dot name equals F dot get name right and just for convenience and just for ease of understanding will also log the incoming message so we'll say this that log that are just log dot info this name is that this dot name okay so there we go that looks yeah that's okay we should write China all right so let's write a test we'll say control shift key create a new test and what we're going to do here is we're going to talk to our test infrastructure using spring boots so spring boots and we'll say run with spring runner dot class and we're going to create a test that sends a message into the channel and then asserts that we that it behaves as we expect it to on arrival so test should consume fraud message okay throws throwable and to confirm this we need to inspect the state of the fraud listener right the resulting state and we need to have reference to that to that sink right so we can send a message into the sink definition so we're going to say we're going to say message builder dot with payload we're going to create a new object a new message that we can send into the channel right we're going to send a message of type fraud into the channel so I'll say message equals message builder and build and then of course we need to provide a payload so we're create an instance of this fraud and we need a name can you think of a good name we can use for this something easy precise and easy to easier to remember and type on the stage you can pick one of the two the first one is a stack and the other is interesti-- kids ah so which one I'm you can also type in festive oh so if you want to the first one let's see the first one I stack just spell it throws geez a stack is it say just say yep that's it yeah okay good okay I didn't like this part that's about right right it's that right yeah suddenly like this okay there's a name so we're going to we're going to have a message that comes that we're going to send into the channel so we'll say sync thought that input we're going to create a definition when V reference that Channel and send it into that so we'll say input that send message okay there we are there's our our code we need to assert the Oh anybody right firm the results right so we say be V assertions dot then this dot this inner dot name is equal to gooey chocolate vision yeah I'll just use the variable okay mind thank you okay today we go that should be okay right looks okay to you do we need to know anything here no other so doing that's fine for now let's go ahead and run it here we go so you've built a message using the PO jury of you you have there you send it in memory to yourself you check the listener got it and you asserted that the name is okay right and we can even see that reflected here on the console as well everything seems fine yep yeah so so it's the builders green so yeah so let me write my part I'm gonna send you that message it's just wrap this up okay we're winning so again I don't trust him okay let me formalize this should send a fraud message groovy so we define a contract make you have a description should send a fraud message and now we something called a label so something that can let's say we can reference this particular message via this label so let's say trigger fraud is okay make sense yes and now with HTTP you have a blocking call you send a request and you wait for the response with messaging it's not the case suddenly something happened that triggered a message a message there is there on the topic or nq-- so there's going to be an input something that triggers a message in our case is going to be a method a trigger message that's going to be in the base class itself right exactly so in the generated test class you'll see in a second that we call a method called trigger message so the con will code won't compile because we didn't define this method we have to define it in the base class now we will send it to all the frogs yeah sounds about right okay and the body there was my surname there okay can you type it in oh it please are you I'm okay I just want to text you fair enough okay so if I if I run this again it would like the plugin we'll see there's a new contract it will regenerate the test and say that there is no method called trigger message so if we go to the tool into here and check out the class we'll see there is a new test that triggers the message and expect the message to be there on destination fraud and a surname should be my name right okay so how can we fix that we need to add the missing method to the base class public void trigger message now actually we need to trigger that message first what we need to do is add a magical out of configure message with our annotation it takes to tell spring cloud to set about the messaging machinery for the test the second thing that we need to do to add the test dependency as Josh did on his side a stream test support and with scope scope test there enough and now we need to somehow trigger that message so let's say that we're going to have it post nothing to the message URL void message and we need to send it somehow so you're using sync right for the input I'm going to use something called a source for the output so going to Purdue you're going to be the producer of a message you're the source I as a consumer I expected messages to come in so think like a faucet the water goes into the sink right exactly so I have a naval the binding now we need to do the same thing that the binding is you have so for the output channel because you are the input on the output right destination is frauds right and the content type is application JSON so we do some sort of like technical configuration on tentacles here okay so now in the foot controller we need to send the message so we the source output send message builder with payload build so we need the the payload so let me do similar stuff to what she did no arcs constructor class fraud and there was discerning right right okay now let me do new fraud erased okay so it's frozen at the time so if we run this hopefully I didn't make any mistakes so that should I have not no need to be a person making course of course mmm-hmm something is not great but that's good tests are like to check it out so what we have here in the base class I saw in the contract we said we're sending froze oh I know what's wrong I have to reverse this project okay so we said let's check it out so we said that we're going to send the message to fraud in the application dot properties we are listening to fraud so that should be okay let's run it again hopefully demo gods please okay work they work on my machine there you go there's the fail okay let's check it out again yeah under running let's close the app Stream test support is here let me refresh it again you can take to the joke in the meantime maybe that one joke I know yes it's into fraud I check the like the area no pointer will point I like my fingers almost vertical kind of pointers surname hmm this is interesting trigger message label concerning source that looks good it's round again for sure it's now you have to run it a couple of times give it a lot of chances to really take out the bugs okay so in the meantime I know what we can do because yeah we have a special folder on the desktop which is called well let's see you want to try and pick it back we can try to figure it back up it's when actually happen oh yes true okay does anybody see a mistake here come again it's missing where no it's not it's not here it's it's in the message and point source outputs and message we stay load oh yes I forgot to call off cars thank you that's call so that's a perfect Oh so what happened is we did nothing that's why we get got no message right thank you if I had a like price for you I will give you up but I don't we have a copy of the spring frameworks you can have open source so we need to call the message yes thank you so much demo gods are evil but that's actually something that only catches all sort of mistakes like stupid ones that I make or for example the content type is wrong or there are typos and stuff okay it's beautiful finally it's beautiful so what I'm I'm gonna run both apps yeah and just check if things as think it'll working so okay we have 80 82 over here now let's run this thing the app itself not to test yeah if either you had a running rabbit mq instance right on my local machine yeah so both apps have let's say bound to the same broker so now if I curl a request over here to this local host local host 8080 to to the message endpoint just work oh I need opposed here that should work right I believe so right now fine why don't even check it we know it's going okay it's not working what you do no what did you do what did you just worked on my machine what that my name is hidden but then the guy who helped me you know it's working yeah exactly okay so mine was first did you say for other frauds cuz I don't lend Raj it's in the code that's the data let's check it out oh you should fraud fraud yeah and I take it in the fraud okay okay see the microwave my verdict sorry sorry so let us we start it and by the way his tests pass right yeah they were fine so we wear microphones when we work together even if it's just a tool up in the office okay so let's curl again and let's check it out oh we got it but the name is nil why is the name nil it gave up on your name what did you do man so let's check it out or you put name or surname name name ah come on so I put surname oh never mind but I mean your turn on English yeah you tell you your English is bad so I didn't get yes so let's run it yet again this time okay I don't work it's kind of work and yeah I mean yeah and by the way I don't have to remind you that your test has passed yeah okay true enough okay let me run this curl again yeah yeah okay the name is good the name is good okay so we made a lot of mistakes and the test has passed so by running the integrations between both of them were able to see the mistakes but that took a lot of time where they rerun everything each time so what I'm doing right now I'm reverting the change with the with a name right and George how can we make things happen in such a way that we fail fast well just as we did before let's take advantage of the stubs and instead of standing up by a fake or a mock sort of HTTP API what we're going to do is going to set up a mock messaging API I'm going to use the stubs again we're going to use the stub runner that we used before but this time we use the annotation so last time I used a rule in my test to stand up the step runner but this time I'll say Auto configure stub Runner and hello whoa easy let's say that we want to work offline that's true and that the ID of the stub is called that example : fraud - service and that's going to bring in the the stub that I need and then what I can do now is instead of me sending all of this stuff manually and talking and exercising the interactions manually mic my stub the stuff that he created for me already knows the kind of message I want to send and how to send it so I'm going to just rely upon the stub trigger okay private stub trigger that's a bit of machinery from spring cloud contract and it's gonna it's connected to the trigger to the sub definition to the contract itself so I can say stub trigger trigger and the thing it's expecting here is the contract label all right so I'm going to grab that invoke that and then that should send a message that the name should be gracious enough you fix it right it's isn't that what I you know really nice okay fine that right yeah this is the one we should read that before okay so there we are so there's the name and let's see what that gives us I'm expecting it to fail that would be successful yes it is about the bugs we reverted that instead of a surname we have a name right so it's better to actually run the test instead of running the app but I assume that kinda smells good okay okay here we go good okay failed and the error is indicative yeah it says no except the actual was not expected was a shock right so perfect right good so what I need to do now is to go back to the pojo here in my fraud pojo why don't I put that here car rental application fraud and I need to change that to what what did you have discerning surname I knew that just testing you so here we go sir good so there we go there's the updated code if we run the test again things should work right we caught the error we didn't have to run the actual downstream service and we got here saying that we can let's say break the build locally on our laptops without even starting any other applications unless you're Chuck Norris better to break it in your test than in production in the country and it's better the test in local machines than to test in production all right so that's awesome but I know that in trying to fix that we had to introduce that new rest endpoint that message endpoint oh yeah I'll go ahead and write a contract for that no no no leave it to professionals okay I mean apart from not writing by implementations in the test I'm doing stuff properly I guess okay fine so you write the contract for the message and let's not write the contract no contract no no no who's using a mock MVC here who knows mock NBC oh it's quite a busy test framework yeah quite a few people so there's a framework of mock in DC that you can use to test your API so let's use it so the endpoint we have here is post mapping for message so let's write a test for that okay so we're going to start with string runner obviously well thank you for killing me fraud service application class and also we're going to use something called the test slice so instead of starting up the whole machinery we can pick only parses or we're interested in in terms of our writing our tests so we're going to use a mock MVC that's the mock HP client that we can use exercise interactions with our spring MVC based one server and the tester should have a post message message whatever endpoint welcome VC perform mock and the C request builders so we send a post to message we throw exceptions and we expect mock MVC result matters status is equal to of status status status is 200 right so that's our case right let's run this so if we have such a such an endpoint but that should pass right now we're going to add how to configure res Docs red dogs is a library that allows you to automatically from your test generate documentation which means that instead of putting all the annotations to define how your interactions look like you can take it out from the test so the test the interactions with the API themselves generate the documentation not annotations on the API prescience so if the semantics are clear it's what we expect them to do that generates the documentation so we have a manual rest smoke NVC Red Sox there's documentation now mark MVC where's documentation well MVC yes has documentation document message so that's the name of what we're this is the thing on your tests that we want to document right so if we run this what's going to happen is if we check out the target we'll see snippets message so this is the named of this identifier I wrote here and you can see that let's see the definition of the request and the response in HTTP that we got from the test or the curl message the Kirkham and you would have to execute to replicate this particular request so it was post factum and for all of you who are already using mock MVC and for example if Docs it's enough for you to add the sprinkles contract wire mug dependency and out of the box you get the JSON stub so I can use that now if I wanted to my client to now talk to that that's the contract ultimately generates that stuff in a way you know I've got the stuff I can use that for my client to test that endpoint as web sites now I've got a question it's just just give me out this is just a crazy thing you know it's Wednesday and I'm feeling squirrely so like I'm curious and I guess this is just a hypothetical I don't I don't want you to get freaked out okay but I've heard on the internet and they don't like they can't it's impossible I've heard that there are other technologies and again don't get weird we don't like from their friends here we're okay we're friends so I've heard that there are other technologies besides spring and Java now again no again hobble this not true again as a hypothetical okay hypothetically let's say that it there is something like I'm going to just let's say say a couple of arbitrary words like letters nodejs for example let's say there is a node.js app and the node.js I would like to profit from the stubs sure it's possible so I'll go to the special folder on the desktop which is called bootiful contracts and there is an up there called stubborn reboots so I can do is execute that and say stop runner work offline work lock work offline as well true and stop runner IDs come example fraud service ring if I run what now jar jar makes no sense it's better to run Java door so we execute that the jar starts and the stubs are downloaded and started in a separate app so if I go to localhost 8080 free stubs I get no stops because I made a typo and of course back for a long example fraud - service ok I didn't know it's octave demo gods is why ok now it looks better so now it's better so I have a running stop on certain port if I go to localhost 8080 3 triggers I will seal the messaging triggers and if I go to localhost localhost / fraud I get 404 because there's no such endpoint got fraud there is so this is the stub that was created from the producers tests and it got you know started aside and now in my node.js app I can call it love even knowing that it's stopped right that's it that's awesome so now if I have a lot of different clamouring services whether their messaging base or HTTP based I can just run these stubs and that works for other languages and protocols yeah we've just made it this makes it much simpler to integrate with other micro services I don't have to run everything a metal scene you've just solved a big problem that Apple could not solve I don't know if you all saw the the QT release of the Apple MacBook Pros that they released last year but there's not enough RAM in that thing for Kev's division distributed systems programming ring 6 my gigs of ram my flock success good that's a joke so this solved that now you can run without anything they were super small on your local machine you can get the fast feedback and you get you know into pressing like semantics I'm going to learn more about this you can go to my talk and Friday about spring club pipelines where we leverage that in a deployment pipeline to discontinue the delivery in deployment right and this sets the foundation for consumer-driven contract testing which is when I hope you got out of this thanks buddy this is fun was also you like some of this stuff did you see the potential here whatever care thank you thank you thank you
Info
Channel: Devoxx
Views: 16,531
Rating: 4.90099 out of 5
Keywords: DVPL17
Id: MDydAqL4mYE
Channel Id: undefined
Length: 51min 1sec (3061 seconds)
Published: Wed Aug 02 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.