tag:blogger.com,1999:blog-195172922018-03-16T16:37:30.042+01:00One Jar To Rule Them AllAlexhttp://www.blogger.com/profile/11632964711752480304noreply@blogger.comBlogger177125tag:blogger.com,1999:blog-19517292.post-65138163603227239402018-02-12T17:27:00.001+01:002018-02-12T17:27:20.644+01:00Repeatable Annotations in Java 8<div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-aMKMfdupYlw/WnV4B4aNKAI/AAAAAAAAB_0/zoNsZudjj6M-xkz_z2-dysjl6er7QhaTwCLcBGAs/s1600/rinserepeat.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="319" data-original-width="666" height="95" src="https://2.bp.blogspot.com/-aMKMfdupYlw/WnV4B4aNKAI/AAAAAAAAB_0/zoNsZudjj6M-xkz_z2-dysjl6er7QhaTwCLcBGAs/s200/rinserepeat.png" width="200" /></a></div><br /><div style="text-align: justify;">With Java 8 you are able to repeat the same annotation to a declaration or type. For example, to register that one class should only be accessible at runtime by specific roles, you could write something like:</div><br /><div style="text-align: justify;"><script src="https://gist.github.com/lordofthejars/17f3f2905b0a5d954e11b4ddcda36959.js"></script> Notice that now&nbsp;<i>@Role</i> is repeated several times.&nbsp;For compatibility reasons, repeating annotations are stored in a container annotation, so instead of writing just one annotation you need to write two, so in the previous case, you need to create&nbsp;<i>@Role</i> and <i>@Roles</i> annotations.</div><br /><div style="text-align: justify;">Notice that you need to create two annotations, one which is the "plural" part of the annotation where you set return type of&nbsp;<i>value </i>method to be an array of the annotation that can be used multiple times. The other annotation can be used multiple time in the scope where it is defined and must be annotated with @<i>Repeatable</i> annotation.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">This is how I did all the time since Java 8 allows to do it. But last week, during a code review my mate George Gastaldi pointed me out how they are implementing these repeatable annotations in <i>javax.validation</i> spec.&nbsp; Of course, it is not completely different but I think that looks pretty much clear from point of view implementation since everything is implemented within the same archive and also, in my opinion, the name looks much natural.&nbsp;</div><div style="text-align: justify;"><script src="https://gist.github.com/lordofthejars/509e67ec93720589e7c005a6c01f6c17.js"></script></div><div style="text-align: justify;"><br />Notice that now everything is placed in the same archive. Since usually you only need to refer to&nbsp;<i>@Role </i>class, and not&nbsp;<i>@Roles</i> (now&nbsp;<i>@Role.List</i>) annotation you can hide this annotation as an inner annotation. Also in case of defining several annotations, this approach makes everything look more compact, instead of having of populating the hierarchy with "duplicated" classes serving the same purpose, you only create one.<br /><br />Of course, I am not saying that the approach of having two classes is wrong, at the end is about preferences since both are really similar. But after implementing repeatable annotations in this way, I think that it is cleaner and compact solution having everything defined in one class.<br /><br />We keep learning,<br />Alex.<br /><blockquote class="tr_bq">Jo sóc l'hipopòtam, i crec que&nbsp;el lleó, ha de refrescar-se per estar molt millor (El Lleó Vergonyós - El Pot Petit)</blockquote>Music:&nbsp;<a href="https://www.youtube.com/watch?v=lYriMzzMsUw">https://www.youtube.com/watch?v=lYriMzzMsUw</a><br /><br />Follow me at <a href="https://twitter.com/alexsotob">https://twitter.com/alexsotob</a><br /><br /><br /></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><br /></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com1tag:blogger.com,1999:blog-19517292.post-2398127236545904112018-01-08T15:26:00.001+01:002018-01-08T15:26:53.523+01:00Secret Rotation for JWT tokens<div class="separator" style="clear: both; text-align: center;"><a href="http://www.cardgamedb.com/forums/uploads/blog-0281295001389910674.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://www.cardgamedb.com/forums/uploads/blog-0281295001389910674.jpg" data-original-height="791" data-original-width="800" height="197" width="200" /></a></div><div style="text-align: center;"><br /></div><br /><div style="text-align: justify;">When you are using <b>JSON Web Token</b> (<b>JWT</b>), or any other token technology that requires to sign or encrypt payload information, it is important to set an expiration date to the token, so if the token expires, you can either assume that this might be considered a security breach and you refuse any communication using this token, or you decide to enable the token by updating it with new expiry date.</div><br /><div style="text-align: justify;">But it is also important to use some kind of secret rotation algorithm, so the secret used to sign or encrypt a token is periodically updated, so if the secret is compromised the tokens leaked by this key is less. Also in this way you are decreasing the probability of a secret&nbsp;being broken.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">There are several strategies for implementing this, but in this post, I am going to explain how I implemented secret rotation in one project I developed some years ago to sign <b>JWT</b> tokens with HMAC algorithm.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">I am going to show how to create a <b>JWT</b> token in Java.</div><br /><script src="https://gist.github.com/lordofthejars/64f531a64634110cdfac20c756602269.js"></script> <br /><div style="text-align: justify;">Notice that what you need to do here is creating an algorithm object setting HMAC algorithm and set a secret that is used to sign and verify instance.</div><br /><div style="text-align: justify;">So what we need is to rotate this algorithm instance every X minutes, so the probability of breaking the secret, and that the broken secret is still valid, becomes very low.</div><br /><div style="text-align: justify;">So how to rotate secrets? Well, with a really simple algorithm that everyone (even if you are not a crypto expert) can understand. Just using time.</div><br /><div style="text-align: justify;">So to generate the secret, you need a string, in the previous example was <i>secret </i>String, of course, this is not so secure, so the idea is to compose this secret string by a root (something we called the big bang part) + a shifted part time. In summary the secret is <i>&lt;bigbang&gt;</i>+<i>&lt;timeInMilliseconds&gt;</i></div><br />Bing bang part has no mystery, it is just a static part, for example, <i>my_super_secret</i>.<br /><br /><div style="text-align: justify;">The interesting part is the time part. Suppose you want to renew secret every second. You only need to do this:</div><br /><script src="https://gist.github.com/lordofthejars/1d011017d839b787e4b23891bae89fab.js"></script> I am just putting 0s to milliseconds part, so if I run this I get something like:<br /><br /><i>1515091335543</i><br /><i>1515091335500</i><br /><i>1515091335500</i><br /><br /><div style="text-align: justify;">Notice that although between second and third print, it has passed 50 milliseconds, the time part is exactly the same. And it will be the same during the same second.</div><br /><div style="text-align: justify;">Of course, this is an extreme example where the secret is changed every second but the idea is that you remove the part of the time that you want to ignore, and fill it with 0s. For this reason, first, you are dividing the time and then multiply by the same number.</div><br /><div style="text-align: justify;">For example, suppose that you want to rotate the&nbsp;secret every 10 minutes you just need to divide and multiply for 600000.</div><br /><div style="text-align: justify;">There are two problems with this approach that can be fixed although one of them is not really a big issue.</div><br /><div style="text-align: justify;">The first one is that since you are truncating the time if you want to change the secret every minute and for example, the first calculation occurs in the middle of a minute, then for just this initial case, the rotation will occur after 30 seconds and not 1 minute. Not a big problem and in our project, we did nothing to fix it.</div><br /><div style="text-align: justify;">The second one is what's happening with tokens that were signed just before the secret rotation, they are still valid and you need to be able to verify them too, not with the new secret but with previous one.</div><br /><div style="text-align: justify;">To fix this, what we did was to create a valid window, where the previous valid secret was also maintained. So when the system receives a token, it is verified with the current secret, if it passes then we can do any other checks and work with it, if not then the token is verified by the previous secret. If it passes, the token is recreated and signed with the new secret, and if not then obviously this token is invalid and must be refused.</div><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-v4X4hsmMwlk/WlJaeqQc3-I/AAAAAAAAB8w/ZGw-MuJmr5kLRpSb8EZpTDjnjaGu8SeCACLcBGAs/s1600/Verifying%2BJWT%2Btoken.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="642" data-original-width="372" height="400" src="https://3.bp.blogspot.com/-v4X4hsmMwlk/WlJaeqQc3-I/AAAAAAAAB8w/ZGw-MuJmr5kLRpSb8EZpTDjnjaGu8SeCACLcBGAs/s400/Verifying%2BJWT%2Btoken.png" width="231" /></a></div><br /><br /><div style="text-align: justify;">To create the algorithm object for <b>JWT</b> you only need to do something like:</div><div style="text-align: justify;"><br /></div><script src="https://gist.github.com/lordofthejars/8c2c8d43b761e810bd4a8c261a5017f4.js"></script> <br /><div style="text-align: justify;">What I really like about this solution is:</div><br /><ul><li style="text-align: justify;">It is clean, no need for extra elements on your system.</li><li style="text-align: justify;">No need for triggered threads that are run asynchronously to update the secret.</li><li style="text-align: justify;">It is really performant, you don't need to access an external system.</li><li style="text-align: justify;">Testing the service is really easy.</li><li style="text-align: justify;">The process of verifying is responsible for rotating the secret.</li><li style="text-align: justify;">It is really easy to scale, in fact, you don't need to do anything, you can add more and more instances of the same service and all of them will rotate the secret at the same time, and all of them will use the same secret, so the rotating process is really stateless, you can scale up or down your instances and all instances will continue to be able to verify tokens signed by other instances.</li></ul><br />But of course there are some drawbacks:<br /><br /><ul><li style="text-align: justify;">You still need to share a part of the secret (the big bang part) to each of the services in a secure way. Maybe using Kubernetes secrets, Vault from Hashicorp or if you are not using microservices, you can just copy a file into a concrete location and when the service is up and running, read the big bang part, and then just remove it.</li><li style="text-align: justify;">If your physical servers are in different time zones, then using this approach might be more problematic. Also, you need that the servers are more or less synchronized. Since you are storing the previous token and current token, it is not necessary that they are synced in the same second and some seconds delay is still possible without any issue.</li></ul><br /><div style="text-align: justify;"><br /></div><div style="text-align: justify;">So we have seen a really simple way of rotating secrets so you can keep your tokens safer. Of course, there are other ways of doing the same. In this post, I just have explained how I did it in a monolith application we developed three years ago, and it worked really well.</div><br />We keep learning,<br />Alex.<br /><blockquote class="tr_bq">You just want attention, you don't want my heart, Maybe you just hate the thought of me with someone new, Yeah, you just want attention, I knew from the start, You're just making sure I'm never gettin' over you (Attention - Charlie Puth)</blockquote>Music:&nbsp;<a href="https://www.youtube.com/watch?v=nfs8NYg7yQM">https://www.youtube.com/watch?v=nfs8NYg7yQM</a><br /><br />Follow me at <a href="https://twitter.com/alexsotob">https://twitter.com/alexsotob</a>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com1tag:blogger.com,1999:blog-19517292.post-24470496281845734102018-01-03T13:23:00.001+01:002018-01-08T15:42:39.773+01:00Cloud Native Applications with JWT<div class="separator" style="clear: both; text-align: center;"><a href="https://cdn.vox-cdn.com/thumbor/C1SdoDActSv8tPONx_OjwEobUjw=/0x0:1004x753/1200x800/filters:focal(0x0:1004x753)/cdn.vox-cdn.com/uploads/chorus_image/image/49523369/20150428-cloud-computing.0.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="533" data-original-width="800" height="213" src="https://cdn.vox-cdn.com/thumbor/C1SdoDActSv8tPONx_OjwEobUjw=/0x0:1004x753/1200x800/filters:focal(0x0:1004x753)/cdn.vox-cdn.com/uploads/chorus_image/image/49523369/20150428-cloud-computing.0.jpg" width="320" /></a></div><div style="text-align: center;"><br /></div><div style="text-align: left;"><div style="text-align: justify;">A <b>native cloud application </b>is an application that is developed for a cloud computing environment.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">There is no specific answer to the question "<i>what is a cloud-native application</i>" but different concepts that must be met.&nbsp;</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">One of the most important in my opinion is the ability to <i>scale up and down</i> at a rapid rate. And this means that our applications cannot have any state on each of the servers since if one server goes down or is scaled down, then the state stored in that server will be lost.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">This is very well summarized at <a href="https://www.youtube.com/watch?v=osz-MT3AxqA">https://www.youtube.com/watch?v=osz-MT3AxqA</a> where it is explained with a shopping cart example. In monolith approach, you store the products of the shopping cart in a server session, if the server went down then all products of shopping cart were lost as well. In a <b>cloud-native app</b>, where server instances can be scaled up and down quickly, it is important to not have this stateful behavior on your services and design them to be stateless.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">There are different approaches to achieve this goal of implementing a <i>stateless architecture</i>&nbsp;but they can be summarized into two categories:</div><div style="text-align: justify;"></div><ul><li>Use a distributed in-memory key/value data store like Infinispan.</li><li>Use a token which acts as a <i>session</i> between client and server using for example <b>JWT</b>.</li></ul><div style="text-align: justify;">In this post, I am going to introduce you the later approach.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">From <a href="https://jwt.io/introduction/">https://jwt.io/introduction/</a>&nbsp;site:</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><i><b>JSON Web Token (JWT)</b> is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object.&nbsp;&nbsp;</i></div><div style="text-align: justify;"><i><br /></i></div><div style="text-align: justify;"><i>This information can be verified and trusted because it is digitally signed. <b>JWTs</b> can be signed using a secret using HMAC or a public/private key pair using RSA.</i></div><div style="text-align: justify;"><i><br /></i></div><div style="text-align: justify;"><i><b>JSON Web Tokens</b> consist of three Base64Url strings separated by dots which are: Header.Payload.Signature</i></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">So the idea basic idea for implementing stateless architecture on backend using <b>JWT</b> is the next one:</div><div style="text-align: justify;"></div><ol><li>When the user adds the first product, the backend service generates a new <b>JWT</b> token with the product added and sent it back to the frontend.</li><li>When the user adds a new product, it sends the product to add and also the <b>JWT</b> token that was sent before by backend.</li><li>Then the backend verifies that the token has not been modified (verifying the signature), then it gets the products from <b>JWT</b> payload added previously and add the new one to the list. Finally, it creates a new token with previous and new products and it sent it back to the frontend.</li><li>The same process is repeated all the time.</li></ol><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-8-K6v25MYVs/WkyrRqoUdhI/AAAAAAAAB8g/8YGGjU5TqsA7_ITRwTKvEa7wNd18nrHlwCLcBGAs/s1600/Shoping%2BCart.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="531" data-original-width="564" height="376" src="https://2.bp.blogspot.com/-8-K6v25MYVs/WkyrRqoUdhI/AAAAAAAAB8g/8YGGjU5TqsA7_ITRwTKvEa7wNd18nrHlwCLcBGAs/s400/Shoping%2BCart.png" width="400" /></a></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">So as you can see, now it is not necessary to maintain any state or add any new database service on backend side, you just need to sent back and forward the <b>JWT</b> token with the products inside.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">I have recorded a video of a simple shopping cart example where I show the stateless nature of the solution. It can be seen at:</div><div style="text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="YOUTUBE-iframe-video" data-thumbnail-src="https://i.ytimg.com/vi/xx93_YswRtk/0.jpg" frameborder="0" height="266" src="https://www.youtube.com/embed/xx93_YswRtk?feature=player_embedded" width="320"></iframe></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Also if you want to check the project that I used for recording you can take a look at&nbsp;<a href="https://github.com/lordofthejars/shop-jwt">https://github.com/lordofthejars/shop-jwt</a>.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Notice that this is just a simple post so you can get the basic idea. But you need to take into consideration next things to use it in production:</div><div style="text-align: justify;"></div><ol><li>Use HTTPS instead of HTTP</li><li><b>JWT</b> just signs the token, if you want extra protection apart from HTTPS, use JWE to encrypt the payload of <b>JWT</b> token as well.</li><li>Fingerprinting the token to avoid any man-in-the-middle attack and use these parameters as authentication parameters for the token.</li><li><b>JWT</b> can be used for passing authentication and authorization things as well.</li></ol><div style="text-align: justify;">You can watch my talk at JavaZone where I introduce some of these techniques:</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><iframe allowfullscreen="" frameborder="0" height="360" mozallowfullscreen="" src="https://player.vimeo.com/video/138956646" webkitallowfullscreen="" width="640"></iframe> <a href="https://vimeo.com/138956646">Make your REST services attack proof - Alex Soto Bueno</a> from <a href="https://vimeo.com/javazone">JavaZone</a> on <a href="https://vimeo.com/">Vimeo</a>.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">The good part of <b>JWT</b> approach is that it simplifies a lot the deployment of the service, you don't need to deploy or configure any other distributed database to share the content across the cluster, which minimizes the problems related to the network for communicating to the distributed database or misconfiguring of any of the nodes.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">The drawback is that the client needs to be aware to receive and sent back the token and deal with it. In backend side, you need to sign and verify every token all the time.</div><div style="text-align: justify;"><br />Note that this approach might work in some cases and might get into some troubles in others (for example if there are parallel connections to backend all of them modifying the token). This post just shows an example of how I implemented this stateless thing in a project with specific requirements, but in other cases it might be wrong to do it. A real shopping cart implementation would have some problems, but for the sake of simplicity and having a business model that everyone undertands, I decided to implement it in this way.<br /><br /></div><div style="text-align: justify;">We keep learning,</div><div style="text-align: justify;">Alex.</div><div style="text-align: justify;"><blockquote class="tr_bq">Turn it up, it's your favorite song (hey), Dance, dance, dance to the distortion, Turn it up (turn it up), keep it on repeat, Stumbling around like a wasted zombie (like a wasted zombie) (Chained to the Rhythm - Katy Perry)</blockquote>Music:&nbsp;<a href="https://www.youtube.com/watch?v=Um7pMggPnug">https://www.youtube.com/watch?v=Um7pMggPnug</a><br /><br />Follow me at <a href="https://twitter.com/alexsotob">https://twitter.com/alexsotob</a><br /><br /></div></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com1tag:blogger.com,1999:blog-19517292.post-69658769797789061702018-01-02T12:50:00.002+01:002018-01-18T08:31:05.353+01:00Writing end to end test for a microservices architecture<div class="separator" style="clear: both; text-align: center;"><a href="http://www.parksun.com/images/Bocce_web_boundary_lo_800.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://www.parksun.com/images/Bocce_web_boundary_lo_800.png" data-original-height="533" data-original-width="800" height="213" width="320" /></a></div><div style="text-align: center;"><br /></div><div style="text-align: justify;">UPDATE: I was not really sure if I should call this post with e2e tests since I knew it can get some confusions, but I couldn0t think about any other name. After reading&nbsp;<a href="https://martinfowler.com/bliki/IntegrationTest.html">https://martinfowler.com/bliki/IntegrationTest.html</a> I can be sure that what I am describing here is how to do <b>Narrow Integration Test</b>.<br /><br />One of the main aspects of microservices architecture is that the application is formed as a collection of loosely coupled services each one deployable independently and communicated each other with some kind of light protocol.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">It is because of microservices architecture is a distributed system that makes writing end to end tests really hard. Suppose next simple example provided by Red Hat as an example of microservices architecture (<a href="https://github.com/jbossdemocentral/coolstore-microservice">https://github.com/jbossdemocentral/coolstore-microservice</a>):</div><div style="text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://raw.githubusercontent.com/lordofthejars/coolstore-microservice/1.2.x/docs/images/arch-diagram.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="412" data-original-width="800" height="205" src="https://raw.githubusercontent.com/lordofthejars/coolstore-microservice/1.2.x/docs/images/arch-diagram.png" width="400" /></a></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Now suppose that you want to write an end to end test for <i>Cart Service</i>. You will quickly see that it is not easy at all, let me enumerate some of the reasons:</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"></div><ul><li><i>Cart Service</i> needs to know how to boot up <i>Pricing Service</i>, <i>Catalog Service</i>, and <i>MongoDB</i> (and if you want to involve the front-end as well then <i>Coolstore GW</i> and <i>WebUI</i>).</li><li><i>Cart Service</i> needs to prepare some data (fixtures) for both of external services.</li><li>You communicate with services using a network. It might occur that some tests fail not because of a real failure but because of an infrastructure problem or because the other services have any bug. So the probability of these tests become flaky and start failing not because any changed introduced in current service is higher.</li><li>In more complex cases running these tests might be expensive, in terms of cost (deploying to the cloud), time (booting up all the infrastructure and services) and maintenance time.</li><li>Difficult to run them in developer machine, since you need all the pieces installed on the machine.</li></ul><br /><div style="text-align: justify;">For this reason, the end to end tests is not the best approach for testing a microservice, but you still need a way to test from the beginning to the end of the service.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">It is necessary to find a way to "simulate" these external dependencies without having to inject any mock object. What we need to do is cheat the service under test so it really thinks it is communicating with the real external services, when in reality it is not.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">The method that allows us to do it is Service Virtualiztion.&nbsp; Service virtualization is a method to emulate the behavior of component applications such as API based.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">You can think about service virtualization as mocking approach you used to implement in OOP but instead of simulating at the object level, you simulate at the service level. It is mocking for the enterprise.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">There are a lot of service virtualization tools out there, but in my experience, in the JVM ecosystem, one of the tools that work better is <b>Hoverfly</b>.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Let's see how an "end-to-end" test looks like for Cart Service.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><script src="https://gist.github.com/lordofthejars/f841b2dbe15984c572b3bc7615e3b8f7.js"></script></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">This service is implemented using <b>Spring Boot</b>, so we are using Spring Boot Test framework. The important part here is that the URL where Catalog service is deployed is specified by using <i>CATALOG_ENDPOINT</i> property.&nbsp; And for this test, it is set to <i>catalog</i>.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">The next important point is the Hoverfly class rule section. In that rule next things are specified:</div><div style="text-align: justify;"></div><ol><li>An Http proxy is started before the test and all outgoing traffic from JVM is redirected to that proxy.</li><li>It records that when a request to host <i>catalog</i> is done and the path is <i>/api/products</i> it must return a success result with given json document.</li></ol><br /><div style="text-align: justify;">The test itself just uses <i>TestRestTemplate</i> (it is a rest client) and validates that you can add some elements to the cart.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Notice that you don't need to configure where the Http proxy is started or configure any port because <b>Hoverfly</b> automatically configures JVM network parameters so any network communication goes through Hoverfly proxy.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">So notice that now you don't need to know how to boot up Catalog service nor how to configure it with correct data.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">You are testing the whole service within its boundaries, from incoming messages to outgoing messages to other services, without mocking any internal element.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Probably you are wondering "What's happening in case of current&nbsp; service has also a dependency on a database server?"</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">In this case, you do as usual since the service itself knows which database server is using and the kind of data it requires, you only need to boot up the database server, populate required data (fixtures) and execute tests. For this scenario I suggest you using <b>Arquillian Cube Docker</b> to bootup database service from a Docker container so you don't need to install it on each machine you need to run tests and <b>Arquillian Persistence Extension</b> for maintaining the database into a known state.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">In next example of rating service, you can see briefly how to use them for persistence tests:</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><script src="https://gist.github.com/lordofthejars/f0a615341ad901fc1a1d70ff0aa8f519.js"></script></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">With this approach, you are ensuring that all inner components of the service work together as expected and avoiding the flakiness nature of end to end tests in microservices.</div><div style="text-align: justify;"><br />So end to end tests in any microservice is not exactly the same of an end to end test in a monolith application, you are still testing the whole service, but just keeping a controlled environment, where test only depends on components within the boundary of service.<br /><br /></div><div style="text-align: justify;">How contract tests fit on this? Well actually everything showed here can be used in consumer and provider side of contract testing to avoid having to boot up any external service.&nbsp; In this way, as many authors conclude, if you are using contract tests, these are becoming the new end to end tests.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">You can see full projects where both tests are used at&nbsp;<a href="https://github.com/lordofthejars/coolstore-microservice/blob/a6fb45710f4830f77221ed3d47e808f7ef3f2790/cart-service/src/test/java/com/redhat/coolstore/CartServiceBoundaryTest.java">https://github.com/lordofthejars/coolstore-microservice/blob/a6fb45710f4830f77221ed3d47e808f7ef3f2790/cart-service/src/test/java/com/redhat/coolstore/CartServiceBoundaryTest.java</a> and&nbsp;<a href="https://github.com/lordofthejars/coolstore-microservice/blob/22779d9e70df2db244e6be694c7e35676fb1674f/rating-service/src/test/java/com/redhat/coolstore/rating/verticle/service/ApeCubeRatingServiceTest.java">https://github.com/lordofthejars/coolstore-microservice/blob/22779d9e70df2db244e6be694c7e35676fb1674f/rating-service/src/test/java/com/redhat/coolstore/rating/verticle/service/ApeCubeRatingServiceTest.java</a><br /><br />We keep learning,<br />Alex.<br /><blockquote class="tr_bq">When every star falls from the sky, And every last heart in the world breaks, It's gonna be OK. (Ok - Robin Schulz)</blockquote>Music:&nbsp;<a href="https://www.youtube.com/watch?v=P9-4xHVc7uk">https://www.youtube.com/watch?v=P9-4xHVc7uk</a><br /><br />Follow me at <a href="https://twitter.com/alexsotob">https://twitter.com/alexsotob</a><br /><br /><br /></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com1tag:blogger.com,1999:blog-19517292.post-71411788011388018312017-12-29T18:44:00.001+01:002017-12-29T18:52:31.303+01:00Java Champion<div class="separator" style="clear: both; text-align: center;"><a href="https://cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/279734aa-8672-41f3-b463-eada17c58abe/File/9f2d0435773ba3ae6e1a0cb24a4ef4b6/java_champions_cmyk_copy.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="164" data-original-width="395" height="132" src="https://cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/279734aa-8672-41f3-b463-eada17c58abe/File/9f2d0435773ba3ae6e1a0cb24a4ef4b6/java_champions_cmyk_copy.jpg" width="320" /></a></div><div style="text-align: center;"><br /></div><br /><div style="text-align: justify;">Before Christmas, I received the good news that I become a Java Champion. And then many memories came to my mind. My first computer, the ZX Spectrum that my parents bought me when I was 6, the first game I had, Desperado (<a href="http://www.worldofspectrum.org/infoseekid.cgi?id=0009331">http://www.worldofspectrum.org/infoseekid.cgi?id=0009331</a>)&nbsp; from TopoSoft. All programs that I copied from books in Basic language, all modifications I did and of course all the "pokers" that I copied from magazines to have infinite lives in games.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">After that, it started the PC era, where I remember creating many "bat" files for having different memory configurations for playing games, editing saved files with Hex editors to have millions of resources and the appearance of Turbo C in my computer.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Finally, the internet era, that for me started in 1997, and my first browser which it was Netscape. What good old time, and then my first web page and JavaScript.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">And this moves me towards the key point, I think it was at 1999 or 2000, I bought a magazine which it came with a CD-ROM with Java 1.2 there, I installed and I started programming in Java without IDE until today.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Probably this is the life of many of developers that were born in the 80s.&nbsp;</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">But this is only "scientific" facts, not feelings. These days I have been thinking about the feelings I had, what ruled to me all this time, and it came to my mind the opening theme of one of my favorite TV program called "This is Opera". Exactly the same words can be applied in my case for programming/computing/Java.</div><div style="text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="YOUTUBE-iframe-video" data-thumbnail-src="https://i.ytimg.com/vi/tGulfVpncTQ/0.jpg" frameborder="0" height="266" src="https://www.youtube.com/embed/tGulfVpncTQ?feature=player_embedded" width="320"></iframe></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">I cannot finish without thanking all the people who I met over the globe during conferences, starting with Dan Allen (the first one I met at a conference), all Arquillian team, Tomitribe team with David Belvins at head), Asciidoctor community, Barcelona JUG guys and a lot of people, organizers, attendees, ....&nbsp;every one thank you so much for being there.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Of course, all the workmates I had in University (La Salle), in Aventia, Grifols (thanks to Erytra team, I enjoyed so much during all years I was there), Everis, Scytl, CloudBees (LLAP to Jenkins) and finally Red Hat, a company that I really love and I feel like home.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Last but not least my family who has been there all the time.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Next year I will continue posting more posts, contributing more to open source projects and of course going to speak to as many events as possible.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">We keep learning,</div><div style="text-align: justify;">Alex.</div><blockquote class="tr_bq" style="text-align: justify;">Caminante son tus huellas&nbsp;el camino y nada más: caminante, no hay camino se hace el camino al andar (Caminante No Hay Camino - Joan Manuel Serrat)</blockquote><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Music: <a href="https://www.youtube.com/watch?v=2DA3pRht2MA">https://www.youtube.com/watch?v=2DA3pRht2MA</a></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Follow me at <a href="https://twitter.com/alexsotob">https://twitter.com/alexsotob</a></div><div style="text-align: justify;"><br /></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com3tag:blogger.com,1999:blog-19517292.post-13387004409623847862017-10-25T16:46:00.004+02:002017-10-25T16:46:51.330+02:00Adding Chaos on OpenShift cluster<div class="separator" style="clear: both; text-align: center;"><a href="http://www.altarandthrone.com/wp-content/uploads/2016/05/205057-chaos.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://www.altarandthrone.com/wp-content/uploads/2016/05/205057-chaos.jpg" data-original-height="535" data-original-width="800" height="214" width="320" /></a></div><br /><div style="text-align: justify;"><b><a href="https://github.com/gaia-adm/pumba">Pumba</a></b> is a chaos testing tool for Docker and <b>Kubernetes</b> (hence <b>OpenShift</b> too). It allows you to add some chaos on containers instances such as stopping, killing or removing containers randomly. But it also adding some network chaos such as delaying, packet loss or re-ordering.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">You can see in next screen recording how to add Pumba on OpenShift cluster and add some chaos on it.</div><br /><iframe allowfullscreen="" frameborder="0" gesture="media" height="315" src="https://www.youtube.com/embed/kA0P-V2JPTA?ecver=1" width="560"></iframe> <br /><br /><div style="text-align: justify;">The security calls that you need to run before deploying <b>Pumba</b> are the next ones:</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><script src="https://gist.github.com/lordofthejars/eab3db728df66ac44ce0d8d31b6224dc.js"></script> Also the Kubernetes script to deploy <b>Pumba</b> in <b>OpenShift</b> is:</div><div style="text-align: justify;"><br /></div><script src="https://gist.github.com/lordofthejars/3a342bc8ac7e253ccec35b7ff69f56a1.js"></script> <br /><div style="text-align: justify;">I have sent a PR to upstream <b>Pumba</b> project with this file but meanwhile is not accepted you can use it this one.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">I'd like to say thank you to Slava Semushin and Jorge Morales for helping me on understanding the <b>OpenShift</b> security model.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">We keep learning,</div><div style="text-align: justify;">Alex.</div><blockquote class="tr_bq">Ce joli rajolinet, que les oques tonifique, si le fique en une pique, mantindra le pompis net (El baró de Bidet - La Trinca)</blockquote>Music:&nbsp;<a href="https://www.youtube.com/watch?v=4JWIbKGe4gA">https://www.youtube.com/watch?v=4JWIbKGe4gA</a><br /><br />Follow me at <a href="https://twitter.com/alexsotob">https://twitter.com/alexsotob</a><br /><div><br /></div><br />Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com1tag:blogger.com,1999:blog-19517292.post-31853661200580481922017-10-24T17:10:00.003+02:002017-10-24T17:10:40.703+02:00Testing Code that requires a mail server<div class="separator" style="clear: both; text-align: center;"><a href="http://iconshow.me/media/images/Mixed/line-icon/png/512/mail-512.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://iconshow.me/media/images/Mixed/line-icon/png/512/mail-512.png" data-original-height="512" data-original-width="512" height="200" width="200" /></a></div><div style="text-align: justify;">Almost all applications has one common requirement, they need to send an email notifying something to a registered user. It might be an invoice, a confirmation of an action or a password recovery. How to test this use case might be challenging, using mocks and stubs are ok for unit tests, but having a component test that tests the whole stack.&nbsp;</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">In this post I am going to show you how <b>Docker</b> and <b>MailHog</b>, can help you on testing this part of code.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><b><a href="https://github.com/mailhog/MailHog">MailHog</a></b> is &nbsp;super simple SMTP server for email testing tool for developers:</div><ul><li style="text-align: justify;">Configure your application to use MailHog for SMTP delivery</li><li style="text-align: justify;">View messages in the web UI, or retrieve them with the JSON API</li><li style="text-align: justify;">Optionally release messages to real SMTP servers for delivery</li><li style="text-align: justify;">Docker image with MailHog installed</li></ul><div style="text-align: justify;">Notice that since you can retrieve any message sent to mail server using JSON API, it makes really simple to validate if the message has been really delivered and of course assert on any of message fields.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><a href="http://arquillian.org/arquillian-cube/"><b>Arquillian Cube</b></a> is an <b>Arquillian</b> extension that can be used to manager <b>Docker</b> containers in your tests. To use it you need a <b>Docker</b> daemon running on a computer (it can be local or not), but probably it will be at local.</div><br /><div style="text-align: justify;"><b>Arquillian Cube</b> offers three different ways to define container(s):</div><ul><li style="text-align: justify;">Defining a docker-compose file.</li><li style="text-align: justify;">Defining a Container Object.</li><li style="text-align: justify;">Using Container Object DSL.</li></ul><div style="text-align: justify;">In this example I am going to show you Container Object DSL approach, but any of the others works as well.</div><div><div style="text-align: justify;"><br /></div></div><div><div style="text-align: justify;">To use Container Object DSL you simply need to instantiate the <i>ContainerDslRule</i> (in case you are using JUnit Rules) or use <b>Arquillian</b> runner in case of using JUnit, TestNG or Spock. You can read more about Container Object DSL at&nbsp;<a href="http://arquillian.org/arquillian-cube/#_arquillian_cube_and_container_object_dsl">http://arquillian.org/arquillian-cube/#_arquillian_cube_and_container_object_dsl</a></div></div><div><br /></div><div><div style="text-align: justify;">As an example of definition of Redis container:</div></div><div><br /></div><div><script src="https://gist.github.com/lordofthejars/e2a3eeb683019f24d2d95c45ef9e3e46.js"></script></div><div><br /></div><div>When running this test, Redis Docker image is started, tests are executed and finally the Docker instance is stopped.<br /><br /><div style="text-align: justify;">So let's see how to do the same but instead of Redis using <b>Mailhog Docker</b> image.</div><div style="text-align: justify;">It is important to notice that <i>ContainerDslRule</i> is a generic class that&nbsp; can be extended to become more specific to a concrete use case. And this is what we are going to do for <b>Mailhog</b>.</div><br /><div style="text-align: justify;"><script src="https://gist.github.com/lordofthejars/d20406be8cb38fd59001fb3f1a37cdc9.js"></script> First of all we need to create a class extending from <i>ContainerDslRule</i>, so everything is still a JUnit rule, but a customized one. Then we create a factory method which creates the <i>MailhogContainer</i> object, setting the image name and the binding ports. Finally an assertion method is used to connect to Rest API of Mailhog server to check if there is any message with given subject.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Then we can write a test using this new rule.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><script src="https://gist.github.com/lordofthejars/3d2a766dddc9d1999e3c2b802ea5974f.js"></script></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">This test just configures <i>MailService</i> class with <b>Docker</b> container configuration, it sends an email and finally it delegates to container object to validate if the email has been received or not.<br /><br />Notice that putting everything into an object makes this object reusable in other tests and even in other projects. You can create an independent project with all your custom developed container objects and just reuse them importing them as test jar in hosted project.<br /><br />Code:&nbsp;<a href="https://github.com/lordofthejars/mailtest">https://github.com/lordofthejars/mailtest</a><br /><br />We keep learning,<br />Alex.<br /><blockquote class="tr_bq">'Cause I'm kind of like Han Solo always stroking my own wookie, I'm the root of all that's evil yeah but you can call me cookie (Fire Water Burn - Bloodhound Gang)</blockquote></div><div style="text-align: justify;">Music:&nbsp;<a href="https://www.youtube.com/watch?v=Adgx9wt63NY">https://www.youtube.com/watch?v=Adgx9wt63NY</a><br /><br />Follow me at <a href="https://twitter.com/alexsotob">https://twitter.com/alexsotob</a><br /><br /></div><br /><br /></div><div><br /></div><br />Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com1tag:blogger.com,1999:blog-19517292.post-67024306356101790892017-09-19T11:17:00.000+02:002017-09-19T11:17:24.567+02:00Testing code that uses Java System Properties<div class="separator" style="clear: both; text-align: center;"><a href="http://www.scottphillips.com/files/2011/01/Configuration.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://www.scottphillips.com/files/2011/01/Configuration.png" data-original-height="200" data-original-width="200" height="200" width="200" /></a></div><div class="separator" style="clear: both; text-align: justify;">Sometimes your code uses any <b>Java System Property</b> to configure itself. Usually these classes are configuration classes that can get properties from different sources and one of valid one is using Java System Properties.</div><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;">The problem is how to write tests for this code? Obviously to maintain your tests isolated you need to set and unset them for each test, restoring old value if for example you are dealing with a global property which is already set before executing tests, and of course in case of an error do not forget to unset/restore the value. So arrived at this point the test might look like:</div><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;"><script src="https://gist.github.com/lordofthejars/4588844e431ff58152a97a3b9b751515.js"></script></div><div class="separator" style="clear: both; text-align: justify;">Notice that this structure should be repeated for each test method that requires an specific system property, so this structure becomes a boilerplate code.</div><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;">To avoid having to repeat this code over and over again, you can use <b>System Rules</b> project which is a collection of JUnit rules for testing code that uses <i>java.lang.System</i>&nbsp;</div><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;">The first thing you need to do is add <b>System Rules</b> dependency as test scope in your build tool, which in this case is <i>com.github.stefanbirkner:system-rules:1.16.0.</i></div><div class="separator" style="clear: both; text-align: justify;"><i><br /></i></div><div class="separator" style="clear: both; text-align: justify;">Then you need to register the JUnit Rule into your test class.</div><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;"><script src="https://gist.github.com/lordofthejars/cf5f53c0556faa1a8b6b8b9393d448c4.js"></script></div><div class="separator" style="clear: both; text-align: justify;">In this case, you can freely set a System property in your test, the JUnit rule will take care of saving and restoring the System property.</div><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;">That's it, really easy, no boilerplate code and help you maintaining your tests clean.</div><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;">Just one notice, these kind of tests are not thread safe, so you need to be really cautious when you use for example surefire plugin with forks. One possible way to avoiding this is by using&nbsp;<i>net.jcip.annotations.NotThreadSafe.class</i> capabilities.</div><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;">We keep learning,</div><div class="separator" style="clear: both; text-align: justify;">Alex</div><div class="separator" style="clear: both; text-align: justify;"><br /></div><blockquote class="tr_bq" style="clear: both; text-align: justify;">Polly wants a cracker, I think I should get off her first, I think she wants some water, To put out the blow torch (Polly - Nirvana)</blockquote><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;">Music:&nbsp;<a href="https://www.youtube.com/watch?v=Qd3rpF9v-1o">https://www.youtube.com/watch?v=Qd3rpF9v-1o</a></div><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;">Follow me at&nbsp;<a href="https://twitter.com/alexsotob">https://twitter.com/alexsotob</a></div><div class="separator" style="clear: both; text-align: justify;"><br /></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com0tag:blogger.com,1999:blog-19517292.post-83331391255667204432017-06-27T12:34:00.000+02:002017-06-27T14:52:26.674+02:00Lifecycle of JUnit 5 Extension Model<div class="separator" style="clear: both; text-align: center;"><a href="http://junit.org/junit4/images/junit5-banner.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://junit.org/junit4/images/junit5-banner.png" data-original-height="244" data-original-width="800" height="60" width="200" /></a></div><br /><div style="text-align: justify;"><b>JUnit5</b> final release is around the corner (currently it is M4), and I have started playing with it a bit on how to write extensions.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">In <b>JUnit5</b>, instead of dealing with <i>Runners</i>, <i>Rules</i>, <i>ClassRules</i> and so on, you've got a single <b>Extension API</b> to implement your own extensions.</div><br /><div style="text-align: justify;"><b>JUnit5</b> provides several interfaces to hook in its lifecycle. For example you can hook to&nbsp;<i>Test Instance Post-processing </i>to&nbsp;invoke custom initialization methods on the test instance, or&nbsp;<i>Parameter Resolution</i> for dynamically resolving test method parameters at runtime. And of course the typical ones like hooking before all tests are executed, before a test is executed, after a test is executed and so on so far, a complete list can be found at&nbsp;<a href="http://junit.org/junit5/docs/current/user-guide/#extensions-lifecycle-callbacks">http://junit.org/junit5/docs/current/user-guide/#extensions-lifecycle-callbacks</a></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">But in which point of the process it is executed each of them? To test it I have just created an extension that implements all interfaces and each method prints who is it.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><script src="https://gist.github.com/lordofthejars/e86cede969e7012cc4ec257d4a2b58da.js"></script></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Then I have created a <b>JUnit5</b> Test suite containing two tests:<br /><br /><script src="https://gist.github.com/lordofthejars/af4155804208ad7efb7310c67d1995fe.js"></script> So after executing this suite, what it is the output? Let's see it. Notice that for sake of readability I have added some callouts on terminal output.<br /><br/><script src="https://gist.github.com/lordofthejars/d321a1deca9d58f3c6f346516a171422.js"></script><br /><b>&lt;1&gt;</b> First test that it is run is <i>AnotherLoggerExtensionTest</i>. In this case there is only one simple test, so the lifecycle of extension is <i>BeforeAll</i>, <i>Test Instance-Post-Processing</i>, <i>Before Each</i>, <i>Before Test Execution</i>, then the test itself is executed, and then all <i>After</i> callbacks.<br /><br /><b>&lt;2&gt; </b>Then the <i>LoggerExtensionTest</i>&nbsp;is executed. First test is not a parametrized test, so events related to <i>parameter resolution</i> are not called. Before the test method is executed, <i>test instance post-processing</i> is called, and after that all before events are thrown. Finally the test is executed with all after events.<br /><br /><b>&lt;3&gt;</b> Second test contains requires a parameter resolution. <i>Parameter resolvers</i> are run after <i>Before</i> events and before executing the test itself.<br /><br /><b>&lt;4&gt;</b> Last test throws an exception. <i>Test Execution Exception</i> is called after test is executed but before <i>After</i> events.<br /><br />Last thing to notice is that <i>BeforeAll</i> and <i>AfterAll</i>&nbsp;events are executed per test class and not suite.<br /><br />The <b>JUnit</b> version used in this example is&nbsp;<i>org.junit.jupiter:junit-jupiter-api:5.0.0-M4</i><br /><br />We keep learning,<br />Alex<br /><blockquote class="tr_bq">That's why we won't back down, We won't run and hide, 'Cause these are the things we can't deny, I'm passing over you like a satellite (Satellite - Rise Against)</blockquote>Music:&nbsp;<a href="https://www.youtube.com/watch?v=6nQCxwneUwA">https://www.youtube.com/watch?v=6nQCxwneUwA</a><br /><br />Follow me at <a href="http://www.twitter.com/alexsotob"><img alt="" src="http://www.logospike.com/wp-content/uploads/2014/11/Twitter_logo-12.png" title="" width="10%" /></a><br /><br /></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com0tag:blogger.com,1999:blog-19517292.post-77169154915244343672017-06-23T16:53:00.002+02:002017-06-23T16:53:52.657+02:00Test AWS cloud stack offline with Arquillian and LocalStack<div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-LFNzAzH690Q/WUztnCVPzlI/AAAAAAAABqI/mrTxnS7piGAtJhiQCgeX--G8U3FOqlUEwCLcBGAs/s1600/330981.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="200" data-original-width="200" height="150" src="https://4.bp.blogspot.com/-LFNzAzH690Q/WUztnCVPzlI/AAAAAAAABqI/mrTxnS7piGAtJhiQCgeX--G8U3FOqlUEwCLcBGAs/s200/330981.png" width="150" /></a><a href="https://2.bp.blogspot.com/-MWmlfP23wFg/WUztnORWBkI/AAAAAAAABqQ/YmQ0zq7WgOccltjZbTpn3XceA1WI4XmUQCLcBGAs/s1600/aws-graphic.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="273" data-original-width="459" height="118" src="https://2.bp.blogspot.com/-MWmlfP23wFg/WUztnORWBkI/AAAAAAAABqQ/YmQ0zq7WgOccltjZbTpn3XceA1WI4XmUQCLcBGAs/s200/aws-graphic.jpg" width="200" /></a><a href="https://4.bp.blogspot.com/-ATTvPE9JIm4/WUztnGtg9CI/AAAAAAAABqM/jiWtyoO-5T0HXWOqEzjS2t5Xtux4_zy6ACLcBGAs/s1600/localstack.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="157" data-original-width="300" height="104" src="https://4.bp.blogspot.com/-ATTvPE9JIm4/WUztnGtg9CI/AAAAAAAABqM/jiWtyoO-5T0HXWOqEzjS2t5Xtux4_zy6ACLcBGAs/s200/localstack.png" width="200" /></a></div><br /><div style="text-align: justify;">When you are building your applications on <b>AWS cloud stack</b> (such as DynamoDB, S3, ...), you need to write tests against these components. The first idea you might have is to have one environment for production and another one for testing, and run tests against it.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">This is fine for integration tests, deployment tests, end to end tests or performance tests, but for component tests it will be faster if you could run <b>AWS cloud stack</b> locally and offline.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><a href="https://github.com/atlassian/localstack"><b>Localstack</b></a> provides this feature. It &nbsp;provides a fully functional local <b>AWS cloud stack</b> so you can develop and test your cloud applications offline.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><b>Localstack</b> comes with different ways to start all stack, but the easiest one is by using Docker image. So if you run&nbsp;<i>atlassianlabs/localstack</i> then you get the stack up and running with next configuration:</div><ul><li>API Gateway at http://localhost:4567</li><li>Kinesis at http://localhost:4568</li><li>DynamoDB at http://localhost:4569</li><li>DynamoDB Streams at http://localhost:4570</li><li>Elasticsearch at http://localhost:4571</li><li>S3 at http://localhost:4572</li><li>Firehose at http://localhost:4573</li><li>Lambda at http://localhost:4574</li><li>SNS at http://localhost:4575</li><li>SQS at http://localhost:4576</li><li>Redshift at http://localhost:4577</li><li>ES (Elasticsearch Service) at http://localhost:4578</li><li>SES at http://localhost:4579</li><li>Route53 at http://localhost:4580</li><li>CloudFormation at http://localhost:4581</li><li>CloudWatch at http://localhost:4582</li></ul><div style="text-align: justify;">So the next question is how do you automate all the process of starting the container, run the tests and finally stop everything and make it portable, so you don't need to worry if you are using Docker in Linux or MacOS? The answer is using <b>Arquillian Cube</b>.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><div><b><a href="http://arquillian.org/arquillian-cube">Arquillian Cube</a></b> is an <i>Arquillian</i> extension that can be used to manager Docker containers in your tests. To use it you need a Docker daemon running on a computer (it can be local or not), but probably it will be at local.</div><div><br /></div><div><b>Arquillian Cube</b> offers three different ways to define container(s):</div><div><ul><li>Defining a <i>docker-compose</i> file.</li><li>Defining a Container Object.</li><li>Using Container Object DSL.</li></ul></div><div>In this example I am going to show you Container Object DSL approach, but any of the others works as well.</div></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">The first thing you need to do is add Arquillian and <b>Arquillian Cube</b> dependencies on your build tool.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><script src="https://gist.github.com/lordofthejars/f60f7ed54c9c0f0e9f055873b7350e5d.js"></script></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Then you can write the test which in this case tests that you can create a bucket and add some content using the S3 instance started in Docker host:<br /><br /><script src="https://gist.github.com/lordofthejars/9ef04ac606dc91880e6d12015d431286.js"></script> Important things to take into consideration:<br /><ol><li>You annotate your test with Arquillian runner.</li><li>Use <i>@DockerContainer</i> annotation to attribute used to define the container.</li><li>Container Object DSL is just a DSL that allows you to configure the container you want to use. In this case the <i>localstack</i> container with required port binding information.</li><li>The test just connects to Amazon S3 and creates a bucket and stores some content.</li></ol><div>Nothing else is required. When you run this test, <b>Arquillian Cube</b> will connect to installed Docker (Machine) host and start the <i>localstack</i> container. When it is up and running and services are able to receive requests, the tests are executed. After that container is stopped and destroyed.</div><div><br /></div><div><b>TIP1</b>: If you cannot use Arquillian runner you can also use a JUnit Class Rule to define the container as described here&nbsp;<a href="http://arquillian.org/arquillian-cube/#_junit_rule">http://arquillian.org/arquillian-cube/#_junit_rule</a></div><div><br /></div><div><b>TIP2:</b> If you are planning to use <i>localstack</i> in the whole organization, I suggest you to use <i>Container Object</i> approach instead of DSL because then you can pack the <i>localstack</i> Container Object into a jar file and import in all projects you need to use it. You can read at&nbsp;<a href="http://arquillian.org/arquillian-cube/#_arquillian_cube_and_container_object">http://arquillian.org/arquillian-cube/#_arquillian_cube_and_container_object</a></div><br />So now you can write tests for your application running on <b>AWS cloud </b>without having to connect to remote hosts, just using local environment.<br /><br />We keep learning,<br />Alex<br /><blockquote class="tr_bq">Tú, tú eres el imán y yo soy el metal , Me voy acercando y voy armando el plan , Solo con pensarlo se acelera el pulso (Oh yeah) (Despacito - Luis Fonsi)</blockquote>Music:&nbsp;<a href="https://www.youtube.com/watch?v=kJQP7kiw5Fk">https://www.youtube.com/watch?v=kJQP7kiw5Fk</a><br /><br /></div><div style="text-align: justify;"><br /></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com0tag:blogger.com,1999:blog-19517292.post-76523507808436901792017-06-22T11:29:00.000+02:002017-06-22T11:29:38.884+02:00Vert.X meets Service Virtualization with Hoverfly<div class="separator" style="clear: both; text-align: center;"><a href="http://vertx.io/vertx2/logo-white-big.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://vertx.io/vertx2/logo-white-big.png" data-original-height="127" data-original-width="422" height="60" width="200" /></a><a href="https://raw.githubusercontent.com/SpectoLabs/hoverfly/master/hoverfly_logo.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="199" data-original-width="650" height="97" src="https://raw.githubusercontent.com/SpectoLabs/hoverfly/master/hoverfly_logo.png" width="320" /></a></div><br /><div style="text-align: justify;"><b><br /></b></div><div style="text-align: justify;"><b>Service Virtualization</b> is a technique using to emulate the behaviour of dependencies of component-based applications.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><b>Hoverfly</b> is a service virtualisation tool written in Go which allows you to emulate HTTP(S) services. It is a proxy which responds to HTTP(S) requests with stored responses, pretending to be it’s real counterpart.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><b>Hoverfly Java</b> is a wrapper around <b>Hoverfly</b>, that let's you use it in Java world. It provides a native Java DSL to write expectations and a <b>JUnit</b> rule to use it together with <b>JUnit</b>.<br /><br />But apart from being able to program expectations, you can also use <b>Hoverfly</b> to capture traffic between both services (in both cases are real services) and persist it.<br /><br />Then in next runs <b>Hoverfly</b> will use these persisted scripts to emulate traffic and not touch the remote service. In this way, instead of programming expectations, which means that you are programming how you understand the system, you are using real communication data.<br /><br />This can be summarised in next figures:<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-Lr8ytwOcxx8/WUpXmpLmyTI/AAAAAAAABnQ/wQt5OaGnz5MoTvJkPEWFJEoEoVwpQFdPwCLcBGAs/s1600/Screen%2BShot%2B2017-06-21%2Bat%2B13.24.22.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="474" data-original-width="986" height="153" src="https://4.bp.blogspot.com/-Lr8ytwOcxx8/WUpXmpLmyTI/AAAAAAAABnQ/wQt5OaGnz5MoTvJkPEWFJEoEoVwpQFdPwCLcBGAs/s320/Screen%2BShot%2B2017-06-21%2Bat%2B13.24.22.png" width="320" /></a></div><br />First time the output traffic is sent though <b>Hoverfly</b> proxy, it is redirected to real service and it generates a response. Then when the response arrives to proxy, both request and response are stored, and the real response is sent back to caller.<br /><br />Then in next calls of same method:<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-gsDVMn5ByoU/WUpZOyC9F0I/AAAAAAAABnc/4Ns8qThr2B06rIjWu1D4EzAtyXXkBG1tQCLcBGAs/s1600/Screen%2BShot%2B2017-06-21%2Bat%2B13.24.29.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="470" data-original-width="984" height="152" src="https://1.bp.blogspot.com/-gsDVMn5ByoU/WUpZOyC9F0I/AAAAAAAABnc/4Ns8qThr2B06rIjWu1D4EzAtyXXkBG1tQCLcBGAs/s320/Screen%2BShot%2B2017-06-21%2Bat%2B13.24.29.png" width="320" /></a></div><br />The output traffic of Service A is still sent though <b>Hoverfly</b> proxy, but now the response is returned from previous stored responses instead of redirecting to real service.<br /><br />So, how to connect from HTTP client of Service A to <b>Hoverfly</b> proxy? The quick answer is nothing.<br /><br /><b>Hoverfly</b> just overrides Java network system properties (<a href="https://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html">https://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html</a>) so you don't need to do anything, all communications (independently of the host you put there) from HTTP client will &nbsp;go through <b>Hoverfly</b> proxy.<br /><br />The problem is what's happening if the API you are using as HTTP client does not honor these system properties? Then obviously all outgoing communications will not pass thorough proxy.<br /><br />One example is <b>Vert.X</b> and its HTTP client&nbsp;<i>io.vertx.rxjava.ext.web.client.WebClient</i>. Since WebClient does not honor these properties, you need to configure the client properly in order to use&nbsp;<b>Hoverfly</b>.<br /><br />The basic step you need to do is just configure <i>WebClient</i> with proxy options that are set as system properties.<br /><br /><script src="https://gist.github.com/lordofthejars/14abf7622164979fb1d44e0222305818.js"></script> Notice that the only thing that it done here is just checking if system properties regarding network proxy has been configured (by <b>Hoverfly</b> Java) and if it is the case just create a <b>Vert.X</b> <i>ProxyOptions</i> object to configure the HTTP client.<br /><br />With this change, you can write tests with <b>Hoverfly</b> and <b>Vert.X </b>without any problem:<br /><br /><script src="https://gist.github.com/lordofthejars/46b194870e4b295c95278b9e46070439.js"></script> In previous example <b>Hoverfly</b> is used as in simulate mode and the request/response definitions comes in form of DSL instead of an external JSON script.<br />Notice that in this case you are programming that when a request by current service (<i>VillainsVerticle</i>), is done to host <i>crimes</i> and port <i>9090</i>, using <i>GET</i> HTTP method at <i>/crimes/Gru</i> then the response is returned. For sake of simplicity of current post this method is enough.<br /><br />You can see source code at&nbsp;<a href="https://github.com/arquillian-testing-microservices/villains-service">https://github.com/arquillian-testing-microservices/villains-service</a>&nbsp;and read about <b>Hoverfly</b> Java at&nbsp;<a href="http://hoverfly-java.readthedocs.io/en/latest/">http://hoverfly-java.readthedocs.io/en/latest/</a><br /><br />We keep learning,<br />Alex<br /><blockquote class="tr_bq">No vull veure't, vull mirar-te. No vull imaginar-te, vull sentir-te. Vull compartir tot això que sents. No vull tenir-te a tu: vull, amb tu, tenir el temps. (Una LLuna a l'Aigua - Txarango)</blockquote>Music:&nbsp;<a href="https://www.youtube.com/watch?v=BeH2eH9iPw4">https://www.youtube.com/watch?v=BeH2eH9iPw4</a></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com0tag:blogger.com,1999:blog-19517292.post-90501843524592895302017-05-24T15:47:00.000+02:002017-05-24T15:47:46.736+02:00Deploying Docker Images to OpenShift<div style="text-align: justify;"><div class="separator" style="clear: both; text-align: center;"><a href="https://www.safaribooksonline.com/library/view/getting-started-with/9781491900468/images/gsos_0101.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="192" src="https://www.safaribooksonline.com/library/view/getting-started-with/9781491900468/images/gsos_0101.png" width="400" /></a></div><br /></div><div style="text-align: justify;"><b><br /></b><b>OpenShift</b> is RedHat's cloud development Platform as a Service (<i>PaaS</i>). It uses <b>Kubernetes</b> as container orchestration (so you can use <b>OpenShift</b> as <b>Kubernetes</b> implementation), but providing some features missed in <b>Kubernates</b> such as automation of the build process of the containers, health management, dynamic provision storage or multi-tenancy to cite a few.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">In this post I am going to explain how you can deploy a <b>Docker</b> image from <i>Docker Hub</i> into an <b>OpenShift</b> instance.&nbsp;</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">It is important to note that <b>OpenShift</b> offers other ways to create and deploy a container into its infrastructure, you can read more about at&nbsp;<a href="https://docs.openshift.com/enterprise/3.2/dev_guide/builds.html">https://docs.openshift.com/enterprise/3.2/dev_guide/builds.html</a>&nbsp;but as read in previous paragraph, in this case I am going to show you how to deploy already created <b>Docker</b> images from <i>Docker Hub</i>.</div><br /><div style="text-align: justify;">First thing to do is create an account in <a href="https://www.openshift.com/">OpenShift Online</a>. It is free and for the sake of this post is enough. Of course you can use any other <b>OpenShift</b> approach like<b> OpenShift Origin</b>.</div><br />After that you need to login into <b>OpenShift</b> cluster. In case of <b>OpenShift Online</b> using the token provided:<br /><br /><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;">oc login https://api.starter-us-east-1.openshift.com --token=xxxxxxx</span><br /><br />Then you need to create a new project inside <b>OpenShift</b>.<br /><br /><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;">oc new-project villains</span><br /><br /><div style="text-align: justify;">You can understand a project as a <b>Kubernetes</b> namespace with additional features.&nbsp;</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Then let's create a new application within previous project based on a <b>Docker</b> image published at <b>Docker Hub</b>. This example is a VertX application where you can get crimes from several fictional villains from Lex Luthor to Gru.<br /><br /></div><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;">oc new-app lordofthejars/crimes:1.0 --name crimes</span><br /><br />In this case a new app called <i>crimes</i> is created based on <i>lordofthejars/crimes:1.0</i> image. After running previous command, a new pod running previous image&nbsp;+ a service&nbsp;+ &nbsp;a replication controller is created.<br /><br />After that we need to create a route so the service is available to public internet.<br /><br /><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;">oc expose svc crimes --name=crimeswelcome</span><br /><br /><div style="text-align: justify;">The last step is just get the version of the service from the browser, in my case was: <i>http://crimeswelcome-villains.1d35.starter-us-east-1.openshiftapps.com/version</i> notice that you need to change the public host with the one generated by your router and then append <i>version</i>. You can find the public URL by going to <b>OpenShift</b> dashboard, at top of pods definition.</div><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-OSC1BM68-MU/WSRNkWSVsEI/AAAAAAAABio/wIiyLt11_2sblPncm_qceGRW9awlIXGZgCLcB/s1600/console_navigation.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="427" src="https://1.bp.blogspot.com/-OSC1BM68-MU/WSRNkWSVsEI/AAAAAAAABio/wIiyLt11_2sblPncm_qceGRW9awlIXGZgCLcB/s640/console_navigation.png" width="640" /></a></div><br /><div style="text-align: justify;">Ok now you'll get a <i>1.0</i> which is the version we have deployed. Now suppose you want to update to next version of the service, to version 1.1, so you need to run next commands to deploy next version of crimes service container, which is pushed at <b>Docker Hub</b>.</div><br /><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;">oc import-image crimes:1.1 --from=lordofthejars/crimes:1.1</span><br /><br /><div style="text-align: justify;">With previous command you are configuring internal <b>OpenShift</b> Docker Registry with next <b>Docker</b> image to release.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Then let's prepare the application so when next rollout command is applied, the new image is deployed:</div><br /><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;">oc patch dc/crimes -p '{"spec": { "triggers":[ {"type": "ConfigChange", "type": "ImageChange" , "imageChangeParams": {"automatic": true, "containerNames":["crimes"],"from": {"name":"crimes:1.1"}}}]}}'</span><br /><br />And finally you can do the rollout of the application by using:<br /><br /><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;">oc rollout latest dc/crimes</span><br /><br /><div style="text-align: justify;">After a few seconds you can go again to&nbsp;<i style="text-align: justify;">http://crimeswelcome-villains.1d35.starter-us-east-1.openshiftapps.com/version</i><span style="text-align: justify;"> (of course change the host with your host)</span><i style="text-align: justify;">&nbsp;</i><span style="text-align: justify;">and the version you'll get is 1.1.</span></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Finally what's happening if this new version contains a bug, and you want to do a rollback of the deployment to previous version? Easy just run next command:</div><br /><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;">oc rollback crimes-1</span><br /><br /><div style="text-align: justify;">And previous version is going to be deployed again, so after a few seconds you can go again to <i>/version </i>and you'll see 1.0 version again.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Finally if you want to delete the application to have a clean cluster run:</div><br /><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;">oc delete all --all</span><br /><br /><div style="text-align: justify;">So as you can see, it is really easy to deploy to deploy container images from <b>Docker Hub</b> to <b>OpenShift</b>. Notice that there are other ways to deploy our application into <b>OpenShift</b> (<a href="https://docs.openshift.com/enterprise/3.2/dev_guide/builds.html" style="text-align: justify;">https://docs.openshift.com/enterprise/3.2/dev_guide/builds.html</a>), in this post I have just shown you one.</div><br />Commands:&nbsp;<a href="https://gist.github.com/lordofthejars/9fb5f08e47775a185a9b1f80f4af7aff">https://gist.github.com/lordofthejars/9fb5f08e47775a185a9b1f80f4af7aff</a><br /><br />We keep learning,<br />Alex.<br /><blockquote class="tr_bq">Yo listen up here's a story, About a little guy that lives in a blue world, And all day and all night and, everything he sees is just blue, Like him inside and outside (Blue - Eiffel 65)</blockquote>Music:&nbsp;<a href="https://www.youtube.com/watch?v=68ugkg9RePc">https://www.youtube.com/watch?v=68ugkg9RePc</a><br /><br /><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;"><br /></span>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com0tag:blogger.com,1999:blog-19517292.post-73156273938847561062017-05-19T12:30:00.001+02:002017-05-19T12:30:12.463+02:00Running Parallel Tests in Docker<div style="text-align: justify;"><div class="separator" style="clear: both; text-align: center;"><a href="https://www.allaboutcircuits.com/uploads/articles/Parallel-Resistance-Calculator.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="95" src="https://www.allaboutcircuits.com/uploads/articles/Parallel-Resistance-Calculator.jpg" width="200" /></a></div><br /><br /></div><div style="text-align: justify;">Sometimes when you are running your tests on your CI environment, you want to run tests in parallel. This parallelism is programmed in build tool such as <i>Maven</i> or <i>Gradle</i> or by using <i>Jenkins</i> plugin.&nbsp;</div><br /><div style="text-align: justify;">If you are using <b>Docker</b> as a testing tool for providing external dependencies to the application (for example databases, mail servers, ftp servers, ....) you might find a big problem and it is that probably <b>Docker Hos</b>t used is one and when running tests in parallel, all of them are going to try to start a container with same name. So when you start the second test (in parallel) you will get a failure regarding that a conflict container name because of trying to start at the same<b> Docker Host</b> two containers with same name or having same binding port in two containers.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">So arrived at this point you can do two things:</div><div style="text-align: justify;"></div><ul><li style="text-align: justify;">You can have one <b>Docker Host</b> for each parallel test.</li><li style="text-align: justify;">You can reuse the same <b>Docker Host</b> and use <b>Arquillian Cube</b> <i>Star Operator</i>.</li></ul><br /><div style="text-align: justify;"><b>Arquillian Cube</b> is an Arquillian extension that can be used to manager <b>Docker</b> containers in your tests.</div><div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">To use <b>Arquillian Cube</b> you need a <b>Docker</b> daemon running on a computer (it can be local or not), but probably it will be at local.</div></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><div><b>Arquillian Cube</b> offers three different ways to define container(s):</div><div><br /></div><div><ul><li>Defining a <i>docker-compose</i> file.</li><li>Defining a Container Object.</li><li>Using Container Object DSL.</li></ul><div>In this example I am going to show you how to use <i>docker-compose</i> and <i>Container Object DSL</i>.</div></div><div><br /></div><div><div><i>Star operator</i> let’s you indicate to <b>Arquillian Cube</b> that you want to generate cube names randomly and can adapt links as well. In this way when you execute your tests in parallel there will be no conflicts because of names or binding ports.</div><div><br /></div><div>Let’s see an example:</div></div><div><br /></div><div><script src="https://gist.github.com/lordofthejars/de03ab760ccd19f8962f68632cd40b91.js"></script></div><div><br /></div><div>You can see in <i>docker-compose.yml</i> file an important change on a typical <i>docker-compose</i> file, and it is that the name ends up with star <b>(*)</b> operator [<b>redis*</b>]. This is how you are instructing <b>Arquillian Cube</b> that this name should be generated dynamically for each execution.</div><div><br /></div><div>Then there are three tests (here only showed the first one) that all of them looks like the same. Basically it prints to console the binding port to connect to the server.</div><div><br /></div><div>Finally there is <i>build.gradle </i>file, which executes two tests in parallel. So if you run the tests in Gradle (<i>./gradlew test</i>) you'll see that two tests are executed at the same time and when it finish one of them, the remaining test is executed. Then if you inspect the output you'll see next output:</div><div><br /></div><div><script src="https://gist.github.com/lordofthejars/df0e7afb5f4102a9138811a4b85d412a.js"></script></div><div><br /></div><div>So as you can see in the log, container name is not <i>redis</i> nor <i>redis*</i>, but <i>redis</i> followed by a <i>UUID</i>. Also you can see that when the output is printed the binding port is different in each case.<br /><br />But if you don't want to use <i>docker-compose</i> approach, you can also define container programmatically by using <i>Container Object DSL</i> which also supports <i>star</i> operator. &nbsp;In this case it the example looks like:<br /><br /><script src="https://gist.github.com/lordofthejars/47459a24d6a4a50694989d5b0bac8533.js"></script> The approach is the same, but using Container Objects (you need Arquillian Cube 1.4.0 to run it with Container Objects).</div><div><br /></div><div>Notice that thanks of this feature you can run the tests with any degree of parallel execution, since <b>Arquillian Cube</b> takes care of naming or port binding issues. Notice that in case of linking between containers, you still need to use the star operator, and it will be resolved at runtime.<br /><br />To read more about star operator just check&nbsp;<a href="http://arquillian.org/arquillian-cube/#_parallel_execution">http://arquillian.org/arquillian-cube/#_parallel_execution</a><br /><br />Source code:&nbsp;<a href="https://github.com/lordofthejars/parallel-docker">https://github.com/lordofthejars/parallel-docker</a><br /><br />We keep learning,<br />Alex.<br /><blockquote class="tr_bq">I can show you the world, Shining, shimmering, splendid, Tell me, princess, now when did, You last let your heart decide? (A Whole New World - Aladdin)</blockquote>Music:&nbsp;<a href="https://www.youtube.com/watch?v=sVxUUotm1P4">https://www.youtube.com/watch?v=sVxUUotm1P4</a><br /><br /><br /></div></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com0tag:blogger.com,1999:blog-19517292.post-84487635747078638602017-05-12T15:12:00.000+02:002017-05-12T15:12:30.255+02:00Testing Spring Data + Spring Boot applications with Arquillian (Part 2)<div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-Zy4cJrsu2BM/WP8PEd31JyI/AAAAAAAABfw/2_bMHwJPqygAMT-RTRA938nyGeGqDgXMgCPcB/s1600/boot%252Barq.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="80" src="https://3.bp.blogspot.com/-Zy4cJrsu2BM/WP8PEd31JyI/AAAAAAAABfw/2_bMHwJPqygAMT-RTRA938nyGeGqDgXMgCPcB/s200/boot%252Barq.png" width="200" /></a></div><br /><div style="text-align: justify;">In <a href="http://www.lordofthejars.com/2017/04/testing-spring-data-spring-boot.html">previous post</a>, I wrote about how to test<b> Spring Data</b> application using <b>Docker</b> with <b>Arquillian Cube</b>. The test looked like:</div><div style="text-align: justify;"><br /></div><script src="https://gist.github.com/lordofthejars/cf23e513d13fae30acff1cb8195ea6a4.js"></script> <br /><div style="text-align: justify;">This test just starts <b>Redis</b> container, then populate data using <i>restTemplate</i> and <i>post </i>method, then execute the logic under test (testing <i>GET HTTP</i> method) and finally stop the <b>Redis</b> container.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">It is good, it works but there are several problems there:</div><ul><li style="text-align: justify;">The first one is that we are using <i>REST API</i> to prepare data set of the test. The problem here is that the test might fail not because a failure on code under test but because of the preparation of the test (insertion of data).</li><li style="text-align: justify;">The second one is that if <i>POST</i> endpoint changes format/location, then you need to remember to change everywhere in the tests where it is used.</li><li style="text-align: justify;">The last one is that each test should leave the environment as found before execution, so the test is isolated from all executions. The problem is that to do it in this approach you need to delete the previous elements inserted by <i>POST</i>. This means to add <i>DELETE HTTP</i> method which might not be always implemented in endpoint, or it might be restricted to some concrete users so need to deal with special authentication things.</li></ul><div><div style="text-align: justify;">To avoid this problem <b>Arquillian Persistence Extension</b> (aka <i>APE</i>) was created. This extensions integrates with <b>DBUnit</b> and <b>Flyway</b> for <b>SQL</b> databases, <b>NoSQLUnit</b> for <b>No SQL</b> databases and <b>Postman collections</b> for <b>REST</b> services so you can populate your backend before testing the real test use case and clean the persistence storage after the test is executed.<br /><br />Also population data is stored inside a file, so this means that can be reused in all tests and easily changed in case of any schema update.</div><br />Let's see example of <a href="http://www.lordofthejars.com/2017/04/testing-spring-data-spring-boot.html">Part 1</a> of the post but updating to use <b>APE</b>.<br /><br /><script src="https://gist.github.com/lordofthejars/860417259d6f7c49eb9d49afdaa0a797.js"></script> And the file (<i>pings.json</i>) used for populating <b>Redis</b> instance with data looks like:<br /><br /><script src="https://gist.github.com/lordofthejars/8719ca319009015268e22f4e1f3ae5c1.js"></script> <br /><div style="text-align: justify;">Notice that in this test you have replaced the <i>POST</i> calls for something that directly inserts into the storage. In this way you avoid any failure that might occurs in the insertion logic (which is not the part under test). Finally after each test method, <b>Redis</b> instance is cleaned so other tests finds <b>Redis</b> clean and into known state.&nbsp;</div><br />Project can be found at&nbsp;<a href="https://github.com/arquillian-testing-microservices/pingpongbootredis">https://github.com/arquillian-testing-microservices/pingpongbootredis</a><br /><br />We keep learning,<br />Alex<br /><blockquote class="tr_bq">Y es que no puedo estar así, Las manecillas del reloj, Son el demonio que me tiene hablando solo (Tocado y Hundido - Melendi)</blockquote>Music:&nbsp;<a href="https://www.youtube.com/watch?v=1JwAr4ZxdMk">https://www.youtube.com/watch?v=1JwAr4ZxdMk</a><br /><br /><br /><br /></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com1tag:blogger.com,1999:blog-19517292.post-70997748425788521452017-05-02T18:21:00.001+02:002017-05-02T18:21:25.404+02:00Testing Dockerized SQL Databases<div class="separator" style="clear: both; text-align: center;"><a href="https://upload.wikimedia.org/wikipedia/commons/thumb/4/49/Ape_skeletons.png/510px-Ape_skeletons.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="172" src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/49/Ape_skeletons.png/510px-Ape_skeletons.png" width="320" /></a></div><br /><div style="text-align: justify;">One of the big advantages of using <b>Docker</b> for testing is that you don't need to install the required dependencies of code under tests in all machines where you are going to run these tests. This is really helpful for external services such as database servers, mail services, JMS queues, ... Also one of the big advantages of this approach is that the tests are going to use the same version used in production.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">So for persistence tests using <b>Docker</b> is a really good approach to follow. But as usually this approach comes with some drawbacks.&nbsp;</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><b>The first one</b> is that obviously you need to have <b>Docker</b> installed in all machines that needs to run the tests, not a big problem but something to take into consideration, as well as <b>Docker</b> inside <b>Docker</b> problem.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><b>The second one</b> is that you need to automate somehow the starting and stopping of the container.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><b>The third one</b> is that <b>Docker</b> containers are ephemeral. This means that when you start the container, in this case a container with a <b>SQL</b> server, then you need to migrate the database schema there.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><b>The fourth one</b>, and this is not only related to Docker, is that you need to maintain test method execution isolated from test to test execution, by providing known data before execution and cleaning data after the execution so other test finds the environment clean.</div><div style="text-align: justify;"><br /><b>First</b> and <b>second</b> problems are fixed with <b>Arquillian Cube</b> (<a href="http://arquillian.org/arquillian-cube/">http://arquillian.org/arquillian-cube/</a>). It manages lifecycle of containers by starting and stopping them automatically before and after test class execution. Also it detects when you are running into a <i>DinD</i> situation and configures started containers accordantly.<br /><br /><b>Arquillian Cube</b> offers three different ways to define container(s).<br /><br /><ul><li>Defining a docker-compose file.</li><li>Defining a Container Object.</li><li>Using Container Object DSL.</li></ul><br />For this post, Container Object DSL approach is the one used. To define a container to be started before executing tests and stopped after you only need to write next piece of code.<br /><div><br /></div><div><script src="https://gist.github.com/lordofthejars/5d2cc19349beafcd552a6eccb14b1468.js"></script></div><div><br /></div><div>In this case a JUnit Rule is used to define which image should be used in the test (<i>redis:3.2.</i>6) and add as binding port the Redis port (6379).</div><div><br /></div><div>The <b>third</b> one can be fixed using <b>Flyway</b>. It is an open-source database migration tool for <i>SQL</i> databases that allows you to automate the creation of database schemas.<br /><br /><b>Flyway</b> is useful here since you can start the <b>Docker</b> container and then apply all migrations to the empty database using <b>Flyway</b>.<br /><br />The <b>fourth</b> problem can be fixed by using tools like <b>DBUnit</b>. iI puts your database into a known state between test runs by populating database with known data, and cleaning it after the test execution.<br /><br /><b>Arquillian</b> integrates with both of these tools (<b>Flyway</b> and <b>DBUnit</b>) &nbsp;among others with its extension called <b>Arquillian Persistence Extension</b> (aka <b>APE</b>),<br /><br />An example on how to use <b>APE</b> with <b>DBUnit</b> is shown in next snippet:<br /><br /><script src="https://gist.github.com/lordofthejars/26e067e52fe3916a176385b4729d3e75.js"></script> You can use <i>Arquillian</i> runner as shown in&nbsp;<a href="https://github.com/arquillian/arquillian-extension-persistence/blob/2.0.0/arquillian-ape-sql/standalone/dbunit-ftest/src/test/java/org/arquillian/ape/rdbms/dbunit/PersonTest.java#L19">dbunit-ftest-example</a>&nbsp;or as shown in previous snippet using a JUnit Rule. Choosing one or other depends on your test requirements.<br /><br />So how everything fits together in <b>Arquillian</b> so you can boot up a <b>Docker</b> container with a SQL database, such as <i>PostgreSQL</i>, before test class execution, then migrate SQL schema and populate it with data, execute the test method, then clean the whole database so next test method finds a clean database and finally after test class execution, the <b>Docker</b> container is destroyed?<br /><br />Let's see it in the next example:<br /><br /><script src="https://gist.github.com/lordofthejars/11a1850ce48b9eed394647725938d3df.js"></script> Test is not so much complicated and it is pretty &nbsp;much self explanatory of what it is doing in each step . You are creating the <b>Docker</b> container using <b>Arquillian Cube</b>&nbsp;<i>DSL</i>, and also you are configuring the populators by just using <b>Arquillian APE</b>&nbsp;<i>DSL</i>.<br /><br />So thanks of <b>Arquillian Cube</b> and <b>Arquillian APE</b>&nbsp; you can make your test totally isolated from your runtime, it will be executed always agains the same PostgreSQL database version and each test method execution will be isolated.<br /><br />You can see full code at&nbsp;<a href="https://github.com/arquillian/arquillian-extension-persistence/tree/2.0.0/arquillian-ape-sql/standalone/dbunit-flyway-ftest">https://github.com/arquillian/arquillian-extension-persistence/tree/2.0.0/arquillian-ape-sql/standalone/dbunit-flyway-ftest</a><br /><br />We keep learning,<br />Alex<br /><blockquote class="tr_bq">Ya no me importa nada, Ni el día ni la hora, Si lo he perdido todo, Me has dejado en las sombras (Súbeme la Radio - Enrique Iglésias)</blockquote>Music:&nbsp;<a href="https://www.youtube.com/watch?v=9sg-A-eS6Ig">https://www.youtube.com/watch?v=9sg-A-eS6Ig</a><br /><br /></div></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com2tag:blogger.com,1999:blog-19517292.post-46993590898617781602017-04-26T14:58:00.000+02:002017-04-26T14:58:01.934+02:00Testing Spring Data + Spring Boot applications with Arquillian (Part 1)<div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-Zy4cJrsu2BM/WP8PEd31JyI/AAAAAAAABfs/vaJAP7ULoYUXLaadIUoVLqCtWTYZdPZKwCLcB/s1600/boot%252Barq.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="128" src="https://3.bp.blogspot.com/-Zy4cJrsu2BM/WP8PEd31JyI/AAAAAAAABfs/vaJAP7ULoYUXLaadIUoVLqCtWTYZdPZKwCLcB/s320/boot%252Barq.png" width="320" /></a></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: justify;"><b>Spring Data</b>’s mission is to provide a familiar and consistent, Spring-based programming model for data access while still retaining the special traits of the underlying data store. It provides integration with several backend technologies such as JPA, Rest, MongoDB, Neo4J or Redis to cite a few.</div><div class="separator" style="clear: both; text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: justify;">So if you are using <b>Spring (Boot)</b> then <b>Spring Data</b> is the right choice to deal with persistence layer.</div><br />In next example you can see how simple is to use <b>Spring Boot</b> and <b>Spring Data Redis</b>.<br /><br /><script src="https://gist.github.com/lordofthejars/4ae6a603210ce54df213d24ce17a221a.js"></script> <br /><div style="text-align: justify;">It is important to notice that by default <b>Spring Data Redis</b> is configured to connect to <i>localhost</i> and port <i>6379</i>, but you can override those values by setting system properties (<i>spring.redis.host</i> and <i>spring.redis.port</i>) or environment variables (<i>SPRING_REDIS_HOST</i> and <i>SPRING_REDIS_PORT</i>).</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">But now it is time to write a test for this piece of code. The main problem you might get is that you need a Redis server installed in all machines that need to execute these tests such as developers machine or Jenkins slaves.&nbsp;</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">This is not a problem per se but when you start working on more and more projects you'll need more and more databases installed on the system, and what even can be worst not exactly the same version as required on production.&nbsp;</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">To avoid this problem, one possible solution is using <b>Docker</b> and containers. So instead of relaying on having each database installed on the system, you only depends on <b>Docker</b>. Then the test just starts the repository container, in our case Redis, executes the test(s) and finally stops the container.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">And this is where <b>Arquillian</b> (and <b>Arquillian Cube</b>) helps you on automating everything.</div><div style="text-align: justify;"><b>Arquillian Cube</b> is an <b>Arquillian</b> extension that can be used to manager <b>Docker</b> containers from <b>Arquillian.</b></div><div style="text-align: justify;"><b><br /></b></div><div style="text-align: justify;">To use <b>Arquillian Cube</b> you need a <b>Docker</b> daemon running on a computer (it can be local or not), but probably it will be at local.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"></div><div style="text-align: justify;">By default the <b>Docker</b> server uses <i>UNIX</i> sockets for communicating with the <b>Docker</b> client. <b>Arquillian Cube</b> will attempt to detect the operating system it is running on and either set <i>docker-java</i> to use <i>UNIX</i> socket on <i>Linux</i> or to <i>Boot2Docker/Docker-Machine</i> on <i>Windows/Mac</i> as the default <i>URI, </i>so your test is portable across several <b>Docker</b> installations and you don't need to worry about configuring it, <b>Arquillian Cube</b> adapts to what you have installed.</div><div style="text-align: justify;"><b><br /></b></div><div style="text-align: justify;"><b>Arquillian Cube</b> offers three different ways to define container(s).</div><div style="text-align: justify;"></div><ul><li>Defining a <i>docker-compose</i> file.</li><li>Defining a<i> Container Object</i>.</li><li>Using <i>Container Object DSL</i>.</li></ul><br /><div style="text-align: justify;">For this post, Container Object DSL approach is the one used. To define a container to be started before executing tests and stopped after you only need to write next piece of code.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><script src="https://gist.github.com/lordofthejars/5d2cc19349beafcd552a6eccb14b1468.js"></script></div><div style="text-align: justify;">In this case a JUnit Rule is used to define which image should be used in the test (<i>redis:3.2.6</i>) and add as binding port the Redis port (<i>6379</i>).<br /><br />The full test looks like:<br /><br /><script src="https://gist.github.com/lordofthejars/cf23e513d13fae30acff1cb8195ea6a4.js"></script> Notice that it is a simple <b>Spring Boot</b> test using their bits and bobs, but <b>Arquillian Cube</b> JUnit Rule is used in the test to start and stop the Redis image.<br /><br />Last important thing to notice is that test contains an implementation of <i>ApplicationContextInitializer</i> so we can configure environment with Docker data (host and binding port of Redis container) so <b>Spring Data Redis</b> can connect to correct instance.<br /><br />Last but not least build.gradle file defines required dependencies, which looks like:<br /><br /><script src="https://gist.github.com/lordofthejars/084bcb9fa7a40260f8ae250c3b8d8cff.js"></script></div><div style="text-align: justify;"><br />You can read more about <b>Arquillian Cube</b> at&nbsp;<a href="http://arquillian.org/arquillian-cube/">http://arquillian.org/arquillian-cube/</a></div><div style="text-align: justify;"><br />We keep learning,<br />Alex<br /><br /><blockquote class="tr_bq">Hercules and his gifts, Spiderman's control, And Batman with his fists, And clearly I don't see myself upon that list (Something just like this - The Chainsmokers &amp; Coldplay)</blockquote><br />Music:&nbsp;<a href="https://www.youtube.com/watch?v=FM7MFYoylVs">https://www.youtube.com/watch?v=FM7MFYoylVs</a><br /><br /></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com0tag:blogger.com,1999:blog-19517292.post-79032803431635879922017-04-10T15:30:00.000+02:002017-04-10T15:40:51.489+02:00Arquillian Persistence with MongoDB and Docker<div class="separator" style="clear: both; text-align: center;"><a href="https://avatars1.githubusercontent.com/u/330981?v=3&amp;s=200" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: right;"><img border="0" height="200" src="https://avatars1.githubusercontent.com/u/330981?v=3&amp;s=200" width="200" /></a></div><div class="separator" style="clear: both; text-align: center;"><a href="https://edumaven.com/mongodb/img/logo-mongodb-tagline.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="86" src="https://edumaven.com/mongodb/img/logo-mongodb-tagline.png" width="200" /></a></div><br /><div style="text-align: justify;">In this screencast you are going to see how you can use <b>Arquillian Persistence Extension</b> (<a href="https://github.com/arquillian/arquillian-extension-persistence/tree/2.0.0">https://github.com/arquillian/arquillian-extension-persistence/tree/2.0.0</a>) and <b>Docker</b> to write persistence tests for <b>MongoDB</b>.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">To manage <b>Docker</b> lifecycle, I have used <b>Arquillian Cube</b> (<a href="http://arquillian.org/arquillian-cube/">http://arquillian.org/arquillian-cube/</a>) and for populating data into <b>MongoDB</b>, the fairly new integration between <b>Arquillian Persistence Extension</b> (aka <i>APE</i>) and <b>NoSQLUnit</b> (<a href="https://github.com/lordofthejars/nosql-unit">https://github.com/lordofthejars/nosql-unit</a>).</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Source code can be found at&nbsp;<a href="https://github.com/arquillian-testing-microservices/beer-workshop/tree/solution/mongodb-beer-service-docker">https://github.com/arquillian-testing-microservices/beer-workshop/tree/solution/mongodb-beer-service-docker</a></div><div style="text-align: justify;"><br /></div><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="YOUTUBE-iframe-video" data-thumbnail-src="https://i.ytimg.com/vi/EEIGJhA6GdQ/0.jpg" frameborder="0" height="266" src="https://www.youtube.com/embed/EEIGJhA6GdQ?feature=player_embedded" width="320"></iframe></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: left;">We keep learning,</div><div class="separator" style="clear: both; text-align: left;">Alex.</div><div class="separator" style="clear: both; text-align: left;"><br /></div><blockquote class="tr_bq" style="clear: both;">Ridi, Pagliaccio, Sul tuo amore infranto! Ridi del duol, che t'avvelena il cor! (Vesti la giubba (Pagliacci) - Leoncavallo)</blockquote>Music:&nbsp;<a href="https://www.youtube.com/watch?v=Z0PMq4XGtZ4">https://www.youtube.com/watch?v=Z0PMq4XGtZ4</a><br /><br /><div style="text-align: justify;"><br /></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com0tag:blogger.com,1999:blog-19517292.post-1881782400861991882017-03-24T15:53:00.000+01:002017-03-24T15:53:00.228+01:003 ways of using Docker Containers for Testing in Arquillian<div class="separator" style="clear: both; text-align: center;"><a href="http://core0.staticworld.net/images/article/2015/04/container-100579502-primary.idge.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://core0.staticworld.net/images/article/2015/04/container-100579502-primary.idge.jpg" height="212" width="320" /></a></div><br /><div style="text-align: justify;"><b>Arquillian Cube</b> is an Arquillian extension that can be used to manager <b>Docker</b> containers from Arquillian.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">With this extension you can start a <b>Docker</b> container(s), execute Arquillian tests and after that shutdown the container(s).</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">The first thing you need to do is add Arquillian Cube dependency. This can be done by using Arquillian Universe approach:</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><script src="https://gist.github.com/lordofthejars/4b92d949eff62c3d9f2366048ea8a072.js"></script></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Then you have three ways of defining the containers you want to start.<br /><br /><b>The first approach</b> is using <i>docker-compose</i> format. You only need to define the docker-compose file required for your tests, and <b>Arquillian Cube</b> automatically reads it, start all containers, execute the tests and finally after that they stop and remove them.<br /><br /><script src="https://gist.github.com/lordofthejars/ffd58545043042fe8485a8d23d2e2f19.js"></script> In previous example a docker compose file version 2 is defined (it can be stored in the <i>root</i> of the project, or in <i>src/{main, test}/docker</i> or in <i>src/{main, test}/resources</i> and <b>Arquillian Cube</b> will pick it up automatically), creates the defined network and start the service defined container, executes the given test. and finally stops and removes network and container. The key point here is that this happens automatically, you don't need to do anything manual.<br /><br /><b>The second approach</b> is using <b>Container Object</b> pattern.&nbsp;&nbsp;You can think of a <b>Container Object</b> as a mechanism to encapsulate areas (data and actions) related to a container that your test might interact with. In this case no <i>docker-compose </i>is required.<br /><br /><script src="https://gist.github.com/lordofthejars/aa717e447033d0dfe8f20ceff2f6a325.js"></script> In this case you are using annotations to define how the container should looks like. Also since you are using java objects, you can add methods that encapsulates operations with the container itself, like in this object where the operation of checking if a file has been uploaded has been added in the container object.<br /><br />Finally in your test you only need to annotate it with&nbsp;<i>@Cube</i> annotation.<br /><br />Notice that you can even create the definition of the container programmatically:<br /><br /><script src="https://gist.github.com/lordofthejars/0b73896db3c13ace529c3cf3268f179b.js"></script> In this case a <i>Dockerfile</i> file is created programmatically within the <b>Container Object </b>and used for building and starting the container.<br /><br /><div style="text-align: justify;"><b>The third way</b> is using Container Object DSL. This approach avoids you from creating a <b>Container Object </b>class and use annotations to define it. It can be created using a DSL provided for this purpose:</div><br /><script src="https://gist.github.com/lordofthejars/a911600c487898accf4ca65c30457337.js"></script> In this case the approach is very similar to the previous one, but you are using a DSL to define the container.<br /><br />You've got three ways, the first one is the standard one following docker-compose conventions, the other ones can be used for defining reusable pieces for your tests.<br /><br />You can read more about <b>Arquillian Cube</b> at&nbsp;<a href="http://arquillian.org/arquillian-cube/">http://arquillian.org/arquillian-cube/</a><br /><br />We keep learning,<br />Alex<br /><blockquote class="tr_bq">And did you think this fool could never win, Well look at me, i'm coming back again, I got a taste of love in a simple way, And if you need to know while i'm still standing you just fade away (I'm still Standing - Elton John)</blockquote>Music:&nbsp;<a href="https://www.youtube.com/watch?v=ZHwVBirqD2s">https://www.youtube.com/watch?v=ZHwVBirqD2s</a><br /><br /><br /></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com0tag:blogger.com,1999:blog-19517292.post-23235729447037219972017-01-09T17:50:00.001+01:002017-01-09T17:50:51.527+01:00Develop A Microservice with Forge, WildFly Swarm and Arquillian. Keep It Simple.<div class="separator" style="clear: both; text-align: center;"><a href="http://forge.jboss.org/1.x/images/forge.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://forge.jboss.org/1.x/images/forge.png" height="87" width="200" /></a><a href="http://wildfly-swarm.io/images/swarm_logo_final.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://wildfly-swarm.io/images/swarm_logo_final.png" height="55" width="200" /></a><a href="https://static.dzone.com/dz1/dz-files/arquillian_logo_450px.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="73" src="https://static.dzone.com/dz1/dz-files/arquillian_logo_450px.png" width="200" /></a>&nbsp;<a href="http://testdetective.com/wp-content/uploads/2015/09/berlin_en.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://testdetective.com/wp-content/uploads/2015/09/berlin_en.png" height="33" width="200" /></a></div><div class="p1" style="text-align: justify;"><div class="separator" style="clear: both; text-align: center;"><br /></div><br />In this post we are going to see how to develop a microservice using <a href="http://wildfly-swarm.io/">WildFly Swarm</a> and <a href="http://forge.jboss.org/">Forge</a> and testing it with <a href="http://arquillian.org/">Arquillian</a> and <a href="http://rest-assured.io/">Rest Assured</a>.</div><div class="p1" style="text-align: justify;"><br /></div><div class="p1" style="text-align: justify;"><b>WildFly Swarm</b> offers an innovative approach to packaging and running Java EE applications by packaging them with just enough of the server runtime to "java -jar" your application.</div><div class="p1" style="text-align: justify;"><br /></div><div class="p1" style="text-align: justify;"><b>JBoss Forge</b> is a software development tool that extends your Java IDE, providing wizards and extensions (add-ons) for different technologies and solutions.</div><div class="p1" style="text-align: justify;"><br /></div><div class="p1"></div><div class="p1" style="text-align: justify;"><b>Arquillian</b> is a platform that simplifies integration testing for Java middleware. It deals with all the plumbing of container management, deployment, and framework initialization so you can focus on the task of writing your tests—real tests.</div><div class="p1" style="text-align: justify;"><br /></div><div class="p1" style="text-align: justify;"><b>REST Assured</b> brings the simplicity of testing and validating REST services in dynamic languages such as Ruby and Groovy into the Java domain.</div><div class="p1" style="text-align: justify;"><br /></div><div class="p1" style="text-align: justify;">So the first thing you need to do is installing Forge, to do it you can just download the CLI console from&nbsp;<a href="http://downloads.jboss.org/forge/releases/3.4.0.Final/forge-distribution-3.4.0.Final-offline.zip">http://downloads.jboss.org/forge/releases/3.4.0.Final/forge-distribution-3.4.0.Final-offline.zip</a> or navigate to <a href="http://forge.jboss.org/download">http://forge.jboss.org/download</a>&nbsp;and download the plugin for Eclipse, Netbeans or IntelliJ. For this example, I am going to use the CLI one.</div><div class="p1" style="text-align: justify;"><br /></div><div class="p1" style="text-align: justify;">After you've installed Forge and it is available in PATH environment variable you can start working on it.</div><div class="p1" style="text-align: justify;"><br />First of all go to the directory where you want to store the project and run <span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">forge</span>.<br />After a few seconds, you'll see that <b>Forge</b> is started and you are ready to type commands:<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-cGYbUlJt9PI/WHNoye968jI/AAAAAAAABMQ/fWSItaDq_4oiCkTC5EaewX1JQHu0YgWFQCLcB/s1600/Screen%2BShot%2B2017-01-09%2Bat%2B11.32.33.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="176" src="https://4.bp.blogspot.com/-cGYbUlJt9PI/WHNoye968jI/AAAAAAAABMQ/fWSItaDq_4oiCkTC5EaewX1JQHu0YgWFQCLcB/s400/Screen%2BShot%2B2017-01-09%2Bat%2B11.32.33.png" width="400" /></a></div><br /><br /></div><div class="p1"><div style="text-align: justify;">After that you need to install the <i>wildfly-swarm addon</i>. To do it just type next command on <b>Forge</b> shell:</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">&gt; addon-install-from-git --url https://github.com/forge/wildfly-swarm-addon</span></div></div><div class="p2"><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Then the latest addon will be downloaded and installed. After this setup step, you can start creating your microservice by calling:</div><div style="text-align: justify;"><br /></div></div><div class="p1"><div style="text-align: justify;"><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">&gt; project-new --top-level-package org.superbiz --named foo --type wildfly-swarm</span></div><div style="text-align: justify;"><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;"><br /></span></div><div style="text-align: justify;"><span style="font-family: &quot;times&quot; , &quot;times new roman&quot; , serif;">This command creates a new project called <i>foo</i>, with <i>pom.xml</i> prepared with all wildfly swarm requirements. Next step is adding a wildfly swarm fragment. A fragment is a way to define which modules you want to be able at runtime.</span></div><div style="text-align: justify;"><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;"><br /></span></div></div><div class="p1"><div style="text-align: justify;"><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">&gt; wildfly-swarm-add-fraction --fractions microprofile</span></div></div><div class="p1"><div style="text-align: justify;"><br /></div><div style="text-align: justify;">In this case microprofile fraction is added. This means that at runtime CDI&nbsp;+ JSON-P&nbsp;+ JAXRS will be available.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Addon also creates a JAX-RS endpoint as an example, you can check it by running next two commands:</div><div style="text-align: justify;"><br /></div><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">&gt; cd src/main/java/org/superbiz/rest/HelloWorldEndpoint.java</span></div><div class="p1"><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">&gt; ls</span></div><div class="p2"><br /><div style="text-align: justify;">Then return to root of the project and let's call the command that creates an Arquilian test for the microservice.</div><div style="text-align: justify;"><br /></div></div><div class="p1"><div style="text-align: justify;"><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">&gt; wildfly-swarm-new-test --target-package org.superbiz --named HelloWorldEndpointTest --as-client</span></div></div><div class="p1"><div style="text-align: justify;"><br /></div><div style="text-align: justify;">In this case the test is called <i>HelloWorldEndpointTest</i> and test is going to run in Arquillian <i>as-client</i> mode (which means that the test is not deployed inside the container and will be run at local runtime). You can check the generated code with next two commands:</div><div style="text-align: justify;"><br /></div><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">&gt; cd src/test/java/org/superbiz</span></div><div class="p1"><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">&gt; cat HelloWorldEndpointTest.java</span></div><div class="p2"><div style="text-align: justify;"><br /></div></div><div class="p1"><div style="text-align: justify;">Notice that test does not validate nothing yet, but since we are using as-client mode, the test injects the URL where the application is started. Let's add some checks using <b>REST-assured</b>.</div><div style="text-align: justify;">Return to the root of the project and add REST-assured dependency by calling next command:</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">&gt; project-add-dependencies io.rest-assured:rest-assured:3.0.1:test</span></div></div><div class="p1"><div style="text-align: justify;"><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">&gt; cat pom.xml</span></div></div><div class="p2"><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Finally you can use <b>REST-assured</b> in empty test to validate that your microservice endpoint effectively returns <i>"Hello from WildFly Swam!"</i>.</div><div style="text-align: justify;"><br /></div><script src="https://gist.github.com/lordofthejars/97584d42d320ad59d2dc3df753ab6f48.js"></script> <br /><div style="text-align: justify;">When you run this test, what it is happening behind the scene is that the microservice is packaged and deployed locally. When service is ready to receive incoming requests, then the test will send a GET request to <i>/hello</i> and asserts that the response body is&nbsp;<i>"Hello from WildFly Swam!"</i></div></div><div class="p1"><div style="text-align: justify;"><br /></div></div><div class="p1">You can see this running at <a href="https://youtu.be/9xb6GIZ1gjs">https://youtu.be/9xb6GIZ1gjs</a><br /><br /><div style="text-align: justify;">This is a really simple example, and this was the intention of this post. Just show you how using <b>Forge</b> and just running some commands you get an started project with its integration test running.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">We keep learning,</div><div style="text-align: justify;">Alex.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><blockquote class="tr_bq">I'm not giving up today, There's nothing getting in my way, And if you knock knock me over, I will get back up again (Get Back Up Again - Trolls)</blockquote><br />Music:&nbsp;<a href="https://www.youtube.com/watch?v=IFuFm0m2wj0">https://www.youtube.com/watch?v=IFuFm0m2wj0</a></div></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com0tag:blogger.com,1999:blog-19517292.post-68851829681360838812016-10-13T15:42:00.001+02:002016-10-13T15:42:42.416+02:00Build Docker Images with Maven and Gradle<div class="separator" style="clear: both; text-align: center;"><a href="http://www.greenhomebuilding.com/images/articles/container2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://www.greenhomebuilding.com/images/articles/container2.jpg" height="229" width="320" /></a></div><br /><br /><div style="text-align: justify;">One of the things that you might want to do if you are using <b>Docker</b> and <b>Java</b> is building the image from a <i>Dockerfile</i> in your build tool (<b>Maven</b> or <b>Gradle</b>). &nbsp;In this post I am going to show you how to do it in both cases.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">I am going to assume that you have the de-facto project layout, having the <i>Dockerfile</i> file at the root of the project.</div><br /><b>Maven</b><br /><div style="text-align: justify;"><br /></div><div style="text-align: justify;">There are several <b>Maven</b> plugins that can be used for building a <b>Docker</b> image in <b>Maven</b>, but one of the most used is <i><a href="https://github.com/fabric8io/fabric8-maven-plugin">fabric8-maven-plugin</a></i>.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">To start you need to register and configure the plugin in <i>pom.xml</i>:</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><script src="https://gist.github.com/lordofthejars/6d4bbf26b13032f7d348863d67c98baa.js"></script> In <i>configuration</i> section you set the image name and the directory where <i>Dockerfile</i> is located.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Any additional files located in the <i>dockerFileDir</i> directory will also be added to the build context. Since <i>Dockerfile</i> is on the root of the project, the target directory is added too. The problem arises because this plugin uses <i>target/docker</i> to generate the build and if you try to build it you'll get next exception:&nbsp;&nbsp;<i>tar file cannot include itself</i>. To avoid this problem you need to create&nbsp;<i>.maven-dockerignore</i> file specifying which directory must be ignored at the same level as <i>Dockerfile</i>:</div><div style="text-align: justify;"><br /></div><script src="https://gist.github.com/lordofthejars/34df564023a2585862bde8902bffa54c.js"></script> And that's all, after that you can do:<br /><br /><i>mvn package docker:build</i><br /><br /><div style="text-align: justify;">Notice that this plugin honor <b>Docker</b> environment variables like <i>DOCKER_HOST, DOCKER_CERT_PATH</i>, ... so if your environment is correctly configured you don't need to do anything else.</div><div style="text-align: justify;"><br /></div><b>Gradle</b><br /><div style="text-align: justify;"><span style="font-weight: bold;"><br /></span></div><div style="text-align: justify;">There are several <b>Gradle</b> plugins that can be used for building a <b>Docker</b> image in <b>Gradle</b>, but one of the most used is <a href="https://github.com/bmuschko/gradle-docker-plugin">gradle-docker-plugin</a>.</div><br /><div style="text-align: justify;">To start you need to register and configure the plugin in <i>build.gradle</i>:</div><div style="text-align: justify;"><br /></div><script src="https://gist.github.com/lordofthejars/1d11340ce768bea4d822712b48c993de.js"></script> <br /><div style="text-align: justify;">In case of <b>Gradle</b>, you need to configure Docker host properties since plugin does not honor <b>Docker</b> environment variables. You need to configure them in <i>docker {}</i> block.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Finally you create a task of type <i>DockerBuildImage</i>, where you set the <i>Dockerfile</i> root directory using <i>inputDir</i> attribute and image name using <i>tag</i> attribute.</div><br /><b>Conclusions</b><br /><div style="text-align: justify;"><b><br /></b></div><div style="text-align: justify;">So in this post you've seen different ways of doing the same in two different build tools, which is building a <b>Docker</b> image from a <i>Dockerfile</i>. Notice that these plugins also allows you to define the <i>Dockerfile</i> content as a configuration field, so you are not creating a Dockerfile file, but specifying its content inside the build tool. You can read more about this feature at <a href="https://dmp.fabric8.io/">https://dmp.fabric8.io/</a>&nbsp;in case of <b>Maven</b> plugin and&nbsp;&nbsp;<a href="https://github.com/bmuschko/gradle-docker-plugin#creating-a-dockerfile-and-building-an-image">https://github.com/bmuschko/gradle-docker-plugin#creating-a-dockerfile-and-building-an-image</a>&nbsp;in case of Gradle.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">We keep learning,</div><div style="text-align: justify;">Alex.</div><div style="text-align: justify;"><br /></div><blockquote class="tr_bq" style="text-align: justify;">Bees'll buzz, kids'll blow dandelion fuzz, And I'll be doing whatever snow does in summer., A drink in my hand, my snow up against the burning sand, Prob'ly getting gorgeously tanned in summer. (In Summer - Frozen)</blockquote><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Music:&nbsp;<a href="https://www.youtube.com/watch?v=UFatVn1hP3o">https://www.youtube.com/watch?v=UFatVn1hP3o</a></div><b><br /></b>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com0tag:blogger.com,1999:blog-19517292.post-28110903411711080912016-09-22T15:20:00.002+02:002016-09-22T15:20:51.558+02:00Authenticating with JGit<div class="separator" style="clear: both; text-align: center;"><a href="https://git-for-windows.github.io/img/git_logo.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="200" src="https://git-for-windows.github.io/img/git_logo.png" width="200" /></a></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><b>JGit</b> is a lightweight, pure Java library implementing the Git version control system. You can do a lot of operations using Java language such as create or clone Git repos, create branches, make commits, rebase or tag, you can see <a href="https://github.com/centic9/jgit-cookbook/tree/master/src/main/java/org/dstadler/jgit">this</a> repo to learn how to use <b>JGit</b> and how to code the different commands.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">But one thing that does not cover extensively is the authentication process. In this post I am going to show you how how to authenticate to a Git repository with <b>JGit</b>.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">First thing to do is add <b>JGit</b> as dependency:</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><script src="https://gist.github.com/lordofthejars/85eb4792041755432c36f4b228b93a20.js"></script></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Then let's see a simple clone without authentication:<br /><br /><script src="https://gist.github.com/lordofthejars/d278d1164f1edd6040d0d0a31ae56216.js"></script> In this case no authentication method is set. Now let's see how to add a username and password in case of for example private repos:<br /><br /><script src="https://gist.github.com/lordofthejars/a59b8eb51677b58cf2c26562405d8d0d.js"></script><br /><br />In this case you only need to set as credential provider the <i>UsernameAndPasswordCredentialsProvider</i> and pass the required username and password.<br /><br />The final scenario I am going to show here is how to authenticate against a git repository using your ssh keys, that is using (<i>~/.ssh/id_rsa</i>) and setting the passphrase to access it.<br /><br /><script src="https://gist.github.com/lordofthejars/c43edb08ecb841e5afcfa8d80ab9d89e.js"></script> <br />In this case you need to extend <i>JSchConfigSessionFactory</i> to be able to set passphrase to access to private key. To do it you set a custom <i>UserInfo</i> implementation where the <i>getPassphrase</i> method returns the passphrase to use and <i>promptPassphrase</i> method should return true.<br /><br />After that you only need to set the transport configuration to the one created.<br /><br />We keep learning,<br />Alex.<br /><blockquote class="tr_bq">Chan eil inneal-ciùil a ghleusar, 'Dhùisgeas smuain mo chléibh gu aoibh, Mar nì duan o bheul nan caileag, Oidhche mhath leibh, beannachd leibh (Oidche Mhath Leibh - Ossian)</blockquote>Music:&nbsp;<a href="https://www.youtube.com/watch?v=mi4SCOYAdEk">https://www.youtube.com/watch?v=mi4SCOYAdEk</a></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com0tag:blogger.com,1999:blog-19517292.post-1402881733749138932016-09-19T16:41:00.001+02:002016-09-19T16:41:06.905+02:00Arquillian Chameleon for the sake of simplicity<div class="separator" style="clear: both; text-align: center;"><a href="http://kids.nationalgeographic.com/content/dam/kids/photos/animals/Reptiles/A-G/chameleon-sleepy.jpg.adapt.945.1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://kids.nationalgeographic.com/content/dam/kids/photos/animals/Reptiles/A-G/chameleon-sleepy.jpg.adapt.945.1.jpg" height="179" width="320" /></a></div><br /><div style="text-align: justify;">When using <b>Arquillian</b>, one of the things you need to do is defining under which container you want to execute all your tests.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">And this is done by adding a dependency in the classpath for the adapter and depending on the mode used (embedded, managed or remote) having to download the application server manually. For example this happens when Wildfly is used in embedded or managed mode.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">An example of a <i>pom.xml</i> using Wildfly could be:</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><script src="https://gist.github.com/lordofthejars/0dc26a07bb1810e0f334a32a323ca1b2.js"></script></div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Notice that in previous script, you need to define the <b>Arquillian</b> adapter, in this case the managed one, and use <i>maven-dependency-plugin</i> to download Wildfly distribution file used by <b>Arquillian</b>.<br /><br />This approach is good and it works, but it has three drawbacks:<br /><br /><ol><li>You need to repeat all these lines in every build script you want to use <b>Arquillian</b> and Wildfly.</li><li>In case you need to use another application server in another project, you need to know which adapter artifact is required and if it is necessary to download &nbsp;the artifacts or not. For example in case of Jetty embedded it is not necessary to download any distribution, you only need set the embedded dependency.</li><li>If you want to test your code against several application servers you have the problem number 2 plus start dealing with profiles.</li></ol><div>But all these problems can be fixed using <b><a href="https://github.com/arquillian/arquillian-container-chameleon">Arquillian Chameleon</a></b>. <b>Arquillian Chameleon</b> is a generic container which reads from <i>arquillian.xml</i> which container, which version and which mode you want to use in your tests, and he will take care of adding required adapter into classpath, download any required distribution and configure the protocol (this is something that as a user you should not touch).</div><div><br /></div><div>How to use <b>Arquillian Chameleon</b> is pretty easy.&nbsp;Do whatever you would do normally such as adding <b>Arquillian</b> <i>bom</i> and add Chameleon Container instead of any application-server specific artifact:</div><div><br /></div><div><script src="https://gist.github.com/lordofthejars/490de85913d0aa8363e0071ab79659f7.js"></script></div><div><br /></div><div>Then create in <i>src/test/resources</i> the <b>Arquillian</b> configuration file called <i>arquillian.xml </i>with next configuration:</div><div><br /><script src="https://gist.github.com/lordofthejars/ac965484faa060f31bb493930d0d1413.js"></script><br />Notice that now you only need to use a friendly property called <i>chameleonTarget</i> to define which container, version and mode you want to use. In previous example<i> Wildfly 9.0.0.Final</i> with managed adapter.<br /><br />When running any test with this configuration, <b>Chameleon</b> will check if Wildfly 9.0.0.Final distribution is downloaded, and if not download it, then will add to classpath the managed adapter for Wildfly 9.0.0 and finally execute the test as any other <b>Arquillian</b> test.<br /><br />What's happening if you want to use Payara instead of Wildfly? You only need to change <i>chameleonTarget</i> property to&nbsp;<span style="font-family: Courier New, Courier, monospace;">payara:4.1.1.163:managed</span>, to for example run tests against Payara 4.1.1 in managed mode.</div><div><br />TIP: You can set this property using a Java system property (<span style="font-family: Courier New, Courier, monospace;">-Darq.container.chameleon.chameleonTarget =&nbsp;payara:4.1.1.163:managed</span>)<br /><br />Currently next containers are supported by <b>Chameleon</b>:<br /><br /><ul><li>JBoss EAP 6.x, 7.x</li><li>WildFly 10.x, 9.x, 8.x</li><li>JBoss AS 7.x</li><li>GlassFish 3.1.2, 4.x</li><li>Payara 4.x</li></ul><br />We keep learning,<br />Alex.<br /><blockquote class="tr_bq">I can see you, Your brown skin shining in the sun, I see you walking real slow(The boys of summer - The Ataris)</blockquote>Music:&nbsp;<a href="https://www.youtube.com/watch?v=Qt6Lkgs0kiU">https://www.youtube.com/watch?v=Qt6Lkgs0kiU</a><br /><br /></div></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com0tag:blogger.com,1999:blog-19517292.post-66913431327989969382016-08-29T19:09:00.002+02:002016-08-29T19:17:36.713+02:00Configuring Maven Release Plugin to Skip Tests<div class="separator" style="clear: both; text-align: center;"><a href="http://www.thamesskiphire.co.uk/images/skip-hire.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://www.thamesskiphire.co.uk/images/skip-hire.jpg" height="150" width="200" /></a></div><br /><div style="text-align: justify;">If you are using Maven and using Maven Release Plugin, you would like to skip the execution of tests during the release plugin execution. The reason might be very different but might depend on the nature of the project or how CI pipeline is implemented.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Notice that this might be a really improvement in releasing time since performing the release with Maven Release Plugin implies executing the same tests twice, one in <a href="http://maven.apache.org/maven-release/maven-release-plugin/examples/prepare-release.html">prepare</a> step and the other one in <a href="http://maven.apache.org/maven-release/maven-release-plugin/examples/perform-release.html">perform</a> step.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">To avoid executing tests in prepare phase you need to run as:</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">mvn -DpreparationGoals=clean release:prepare</span></div><div style="text-align: justify;"><i><br /></i></div><div style="text-align: justify;">If you want to avoid executing tests during perform phase you need to run as:</div><div style="text-align: justify;"><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;"><br /></span></div><div style="text-align: justify;"><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace;">mvn -Darguments="-Dmaven.test.skip=true" release:perform</span></div><div style="text-align: justify;"><i><br /></i></div><div style="text-align: justify;">Please it is important to note that I am not saying you don't need to execute tests during release process, what I am saying is that something your release process doesn't fit the standard release process of the plugin and for example you are already running tests before executing the plugin.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">We keep learning,</div><div style="text-align: justify;">Alex.</div><blockquote class="tr_bq" style="text-align: justify;">Say it ain't so, I will not go, Turn the lights off, carry me home, Keep your head still, I'll be your thrill, The night will go on, my little windmill (All The Small Things - Blink-182)</blockquote><div style="text-align: justify;">Music:&nbsp;<a href="https://www.youtube.com/watch?v=9Ht5RZpzPqw">https://www.youtube.com/watch?v=9Ht5RZpzPqw</a></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com0tag:blogger.com,1999:blog-19517292.post-66267536471849052232016-08-18T13:28:00.002+02:002016-08-18T13:28:57.655+02:00Making Web UI testing great again with Arquillian, Docker and Selenium (part 1)<div class="separator" style="clear: both; text-align: center;"><a href="http://arquillian.org/images/arquillian_crown_icon_glossy_256.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://arquillian.org/images/arquillian_crown_icon_glossy_256.png" height="200" width="200" /></a><a href="https://pbs.twimg.com/profile_images/378800000124779041/fbbb494a7eef5f9278c6967b6072ca3e_400x400.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="200" src="https://pbs.twimg.com/profile_images/378800000124779041/fbbb494a7eef5f9278c6967b6072ca3e_400x400.png" width="200" /></a></div><div style="text-align: justify;"><br /><b>Introduction to the Problem</b></div><div style="text-align: justify;"><br />Most of the time when you need to write functional tests/end-to-end tests for web UI, you end up by using <a href="http://www.seleniumhq.org/">Selenium</a>, which it can consider the de-facto tool in Java world for web UI testing. I am sure you've already used it for these kind of tests.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">But probably at the same time you've been faced on some the most common problems in functional testing, some related with Web UI testing and others not.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">For example <b>one of the major</b> problems usually people find in functional tests are the preparation of the environment, to run the tests you need to boot up a server and deploy your application, then install/start the database, also maybe the cache system and so on with all the servers, leaving the user to install locally each of the service. Some errors could happen like installing incorrect version of the server used in production, reusing another local installation of the database which might not be the same version or for example running them in a different JDK version of the one used in production.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">But also there are some other problems that are more specific to Web UI testing such as browser installation or configuration of WebDriver properties.<br /><br /><b>Fixing First Problem</b><br /><br />To fix the first problem, the most easier solution you can think is using Docker containers and of course <a href="https://docs.docker.com/compose/">Docker compose</a>&nbsp;since you can define and run multi-container Docker applications. So basically you define in <i>docker-compose</i> file, all the servers that you might need to run the tests so when you run tests you have all of them running and more important with a fixed version, so you can be sure that the tests are always run against a known/desired specific version of the servers, same JDK, ... and not depending on what is installed in developers/CI machine.<br /><br />But this approach has one problem. You need to specifically run <i>docker-compose up</i>, <i>docker-compose down. </i>Of course you can automate this in your build script, which will solve the problem on CI environment, but if a developer wants to execute a test from IDE, let's say for debugging, then he needs to be aware of that fact.<br /><br />And this is what <a href="https://github.com/arquillian/arquillian-cube">Arquillian Cube</a>&nbsp;solves. Arquillian Cube is an Arquillian extensions that &nbsp;uses <i>docker-compose</i> file to start and configure all the containers defined there, execute the tests and finally shutting down all of them. The good news is that since Arquillian works with JUnit (and TestNG and Spock), you can run the tests from the IDE without worrying about starting and stopping containers since Docker lifecycle is managed by Arquillian Cube.<br /><br />So first part of the problem that is defining the test environment is fixed with Arquillian Cube. Let's see how to fix the second one.<br /><br /><b>Fixing Second Problem</b><br /><br />Selenium project provides a <a href="https://github.com/SeleniumHQ/docker-selenium">Docker images</a> with Selenium standalone or Selenium node with browser (Firefox or Chrome) and a VNC server installed.<br /><br />So it seems a perfect fit to fix the problem of having to install browsers with a concrete version or concrete configurations locally since you can use a docker image with a browser configured for the tests.<br /><br /><b>New Problems When Using Docker for Testing</b><br /><br />And that's cool, but it has some problems. The first one is that you need to create a docker-compose file specific for testing purposes, although this is not a bad thing per se, but it requires more management from dev part to maintain this file as well and of course repeat again and again in all the projects you want to use it, defining the browser to use and the VNC client image to get the recording for future inspection.<br /><br />The second problem is the configuration of WebDriver. When running WebDriver against a remote browser, you need to set the location (IP) of the browser and configure the RemoteWebDriver accordantly with desired capabilities.<br /><br />So again you have to write in all the tests the WebDriver configuration again and again. You can create a factory class that can be reused in all the projects, and it is good, but you still have one problem, some developers might use Docker machine so IP would not be static and might change every time, other might be using native Docker, and for example some phases of CI pipeline might run the tests against a remote fully environment like preproduction environment, so before executing tests you would need to specify manually the IP of container of Docker host.<br /><br />And the third problem you'll get is that you need to instruct WebDriver to open a page:<br /><br /><i>webdriver.get("http://www.google.com");</i><br /><br />The problem is that in this case the browser is inside the Docker infrastructre so you need to set the internal IP of the server container, so you don't only need to know the Docker host IP for connecting the remote web driver but also the internal IP of the server container to open the page in remote browser using the <i>get</i> method. And again this might be quite difficult to acquire in an automatic way.<br /><br />But all these problems are solved when using the new integration between <a href="https://docs.jboss.org/author/display/ARQ/Drone">Arquillian Drone</a> and Arquillian Cube.<br /><br /><b>Fixing New Problems</b><br /><b><br /></b>Arquillian Drone is an Arquillian extension that integrates Selenium WebDriver to Arquillian. This extension manages the configuration of the WebDriver so you don't need to repeat it in all your tests, and also the lifecycle of the browser.<br /><br />So as you can see this pair of extensions seems a perfect fit for solving these problems. Drone takes care of configuration meanwhile Cube takes care of configuring correctly the Selenium/VNC containers and starting and stopping them.<br /><br />As you might see, you don't need to worry about creating&nbsp;<i>docker-compose</i>&nbsp;file for testing purposes. You only need to create the one used for deploying, and Arquillian will take care of the rest.<br /><br /><b>Example</b><br /><b><br /></b>The first thing to do is create a project with required dependencies. For this example we are using Maven, but you can achieve the same using other build tools.<br /><br /><script src="https://gist.github.com/lordofthejars/7178852d3fd59a812ebe9a18bcff78f0.js"></script> Things important to notice is that you are using BOM definitions for setting versions of the components. Then we set Arquillian Standalone dependency because our test is not going to have <i>@Deployment</i> method since the deployment file is already created inside the Docker image used in the application.&nbsp;Finally Arquillian Cube and Arquillian Drone dependencies are added.<br /><br />Next step is creating at <i>src/test/resources</i> a file called <i>arquillian.xml</i> which is used for configuring extensions.<br /><br /><script src="https://gist.github.com/lordofthejars/2d082344cf81d42f17cc4afaf88f0d0c.js"></script> You can see that:<br /><br /><ul><li>You need to specify the docker machine name where to start containers in case of using docker machine. If you are using native Docker then you don't need to set this attribute.</li><li>You need to set a location relative to root folder of the project where <i>docker-compose</i> file is located. Note that you could use any other name.</li></ul><div>You can customize WebDriver as well configuring Arquillian Drone (<a href="https://docs.jboss.org/author/display/ARQ/Drone">https://docs.jboss.org/author/display/ARQ/Drone</a>), &nbsp;but for this test the defaults are enough. Note that now the default browser is <b>firefox</b>.</div><div><br /></div><div><b>IMPORTANT</b>: if you are using native Linux Docker installation, comment the configuring line of <i>machineName</i>. If you are using docker machine and it is called different to <i>dev</i>, then adapt <i>machineName</i> in <i>arquillian.xml</i> too.</div><div><br /></div><div>Next step is creating the <i>docker-compose</i> file at root directory.</div><div><br /></div><div><script src="https://gist.github.com/lordofthejars/2f3e3529627da8cc81648ed3e8aeb17c.js"></script></div><div>Simple compose file which defines only one container. This containers exposes the 80 port but then it is bound to port 8080. This container start a <i>Go</i> program listening to root context and returning <i>Hello World</i> in HTML format.<br /><br />And finally the test:<br /><br /><script src="https://gist.github.com/lordofthejars/8af1d7dfadee77bd4d94fe45f454964c.js"></script> There are some interesting parts in this test.<br /><br /><ul><li>It is a standard Arquillian test in the sense it uses Arquillian runner.</li><li>Uses&nbsp;<i>@Drone</i> injection mechanism provided by Arquillian Drone to enrich test with a WebDriver configured to connect to remote browser.</li><li>Uses&nbsp;<i>@CubeIp</i> annotation to enrich test with the internal IP of the container <i>helloworld</i>. Since browser is running inside Docker host, we can use the internal IP for this purpose. Also it is important that you need to use the exposed port and not the bind port.</li><li>Everything else is managed by Arquillian Cube like the start and stop of the Docker containers(<i>helloworld</i> in this case) but also the ones containing the browser and the VNC client. If you put a debug point inside test method, and then execute a <i>docker ps</i> on a terminal, you'll see that three containers are started, not just <i>helloworld</i>.&nbsp;</li><li>If after running the test you inspect <i>target/reports/videos</i> directory you will find the video recording of the test.</li></ul><div>You can also see an screencast of this in action:</div><div><br /></div><div><iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/tEypn-uw2Yo" width="560"></iframe></div><div><br /></div><div>So as you can see using Arquillian Cube with Arquillian Drone makes your test and <i>docker-compose</i> file looks really neat. &nbsp;Test only contains things related of the test and not about WebDriver configuration. Also your <i>docker-compose</i> looks clear, it only contains things related to business, not about testing.</div><div><br /></div><div>In this post you've seen how to use Arquillian Cube&nbsp;+ Arquillian Drone. In next one you'll see the integration with Arquillian Graphene, which will simplify even more the test to just focusing testing and not on WebDriver calls.</div><div><br /></div><div>We keep learning,</div><div>Alex.</div><br /><blockquote class="tr_bq">When I look 'round, I only see outta one eye<br />As the smoke surrounds my head, the sauna (Stickin' In My Eye - NOFX)</blockquote><br /><div>Source Code: <a href="https://github.com/lordofthejars/drone-cube-example">https://github.com/lordofthejars/drone-cube-example</a></div><div>Music:&nbsp;<a href="https://www.youtube.com/watch?v=A2vn18DVSbU">https://www.youtube.com/watch?v=A2vn18DVSbU</a></div></div></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com0tag:blogger.com,1999:blog-19517292.post-65707420643639718102016-03-31T17:43:00.002+02:002016-04-07T09:32:22.225+02:00Continuous Stress Testing for your JAX-RS (and JavaEE) applications with Gatling + Gradle + Jenkins Pipeline<div class="separator" style="clear: both; text-align: center;"></div><div class="separator" style="clear: both; text-align: center;"></div><div class="separator" style="clear: both; text-align: center;"><a href="http://gatling.io/images/gatling-logo.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://gatling.io/images/gatling-logo.png" height="161" width="200" /></a></div><div style="text-align: justify;">In this post I am going to explain how to use <b><a href="http://gatling.io/">Gatling</a></b> project to write stress tests for your <b>JAX-RS Java EE</b>&nbsp;endpoints, and how to integrate them with <a href="https://gradle.org/"><b>Gradle</b></a> and <a href="http://bit.ly/1V6CxtX"><b>Jenkins Pipeline</b></a>, so instead of having a simple stress tests, what you have is a <b>continuous stress</b> testing, where each commit might fire these kind of tests automatically, providing automatic assertions and more important graphical feedback of each execution so you can monitorize how the performance is evolving in your application.</div><br />First thing to develop is the <b>JAX-RS</b>&nbsp;JavaEE service:<br /><br /><script src="https://gist.github.com/lordofthejars/f6beaee8e44d58f5694c510b3af0a46f.js"></script> <br /><div style="text-align: justify;">There is nothing special, this is an asynchronous<b> JAX-RS </b>endpoint that connects to <a href="http://swapi.co/">swapi.co</a> site, retrieves all the information of Star Wars planets, calculates the average of orbital period and finally it returns it in form of text. For sake of simplicity, I am not going to show you all the other classes but they are quite simple and at the end of the post I will provide you the github repository.&nbsp;</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">The application is packaged inside a war file and deployed into an application server. In this case into an <a href="http://tomee.apache.org/">Apache TomEE 7</a>&nbsp;deployed inside the official Apache TomEE <a href="https://hub.docker.com/_/tomee/">Docker</a> image.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;">Next step is configuring <b>Gradle</b> build script with <b>Gatling</b> dependencies. Since <b>Gatling</b> is written in <i>Scala</i> you need to use <i>Scala</i> plugin.</div><div style="text-align: justify;"><br /></div><div style="text-align: justify;"><script src="https://gist.github.com/lordofthejars/731fe53e698eb7606e4f88b8f0516b87.js"></script></div><div style="text-align: justify;">After that, it is time to write our first stress test. It is important to notice that writing stress tests for <b>Gatling</b> is writing a <i>Scala</i> class using the provided&nbsp;DSL. Even for people who has never seen <i>Scala</i> is pretty intuitive how to use it.<br /><br />So create a directory called <span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;">src/test/scala</span><i> </i>and create a new class called&nbsp;<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small; font-style: italic;">AverageOrbitalPeriodSimulation.scala </span>with next content: <br /><script src="https://gist.github.com/lordofthejars/ff7d98c1a663c92a671348b4ddee0143.js"></script></div><div style="text-align: justify;"><br />Every simulation must extends <span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;">Simulation</span> object. This simulation takes base URL of the service from <span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;">starwars_planets_url</span> environment or system property, it creates the scenario pointing to the endpoint defined in <b>JAX-RS</b>, &nbsp;and finally during 3 seconds it will gradually add users until 10 users are running at the same time. The test will pass only if all the requests succeed in less than 3 seconds.<br /><br />Now we need to run this test. You will notice that this is not a JUnit test, so you cannot do a <i>Run As JUnit</i> test. What you need to do is use a runnable class provided by <b>Gatling </b>which requires you pass as argument the simulation class. This is really easy to do with <b>Gradle</b>.<br /><br /><script src="https://gist.github.com/lordofthejars/ee41857f271174efd719ed46f61dbc2c.js"></script> We are defining a <b>Gradle</b> task of type <i>JavaExec</i>, since what we want is to run a runnable class. Then we make the life a bit easier for developer by automatically detect that if <span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;">starwars_planets_url</span> is not set, we are running this test into a machine that has Docker installed so probably this is the host to be used.<br />Finally we override the environment variable if it is required, we set the runnable class with required properties and we configure <b>Gradle</b> to execute this task every time the <i>test</i> task is executed (<span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;">./gradlew test</span>).<br /><br />If you run it, you might see some output messages from <b>Gatling</b>, and after all a message like: please open the following file: <i>/Users/..../stress-test/build/reports/gatling-results/averageorbitalperiodsimulation-1459413095563/index.html </i>and this is where you can get the report. Notice that a random number is appended at the end of the directory and this is important as we are going to see later. The report might looks like:<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-KCqnLjucLgk/VvzhoE0DP1I/AAAAAAAAA1Q/B7l2yb7CE4o2fsPWpXPVA81q6iXRFFG5g/s1600/Screen%2BShot%2B2016-03-31%2Bat%2B10.36.15.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="168" src="https://3.bp.blogspot.com/-KCqnLjucLgk/VvzhoE0DP1I/AAAAAAAAA1Q/B7l2yb7CE4o2fsPWpXPVA81q6iXRFFG5g/s320/Screen%2BShot%2B2016-03-31%2Bat%2B10.36.15.png" width="320" /></a></div><br /><br />At this time we have <b>Gatling</b> integrated with <b>Gradle</b>, but there is a missing piece here, and it is adding the <i>continuous</i> part on the equation. For adding continuous stress testing we are going to use <b>Jenkins</b> and <b>Jenkins Pipeline as CI server&nbsp;</b>so for each commit<b>&nbsp;</b>stress tests are executed<b>&nbsp;</b>among other tasks such as compile, run unit, integration tests, or code quality gate.<br /><br />Historically <b>Jenkins</b> jobs were configured using web UI, requiring users to manually create jobs, fill the details of the job and create the pipeline through web browser. Also this makes keeping configuration of the job separated from the actual code being built.<br /><br />With the introduction of <b>Jenkins Pipeline</b> plugin. This plugin is a Groovy DSL that let's implement you the entire build process in a file and store that alongside its code. <b>Jenkins 2.0</b> comes by default with this plugin, but if you are using Jenkins 1.X you can install it as any other plugin (<a href="https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Plugin">https://wiki.jenkins-ci.org/display/JENKINS/Pipeline+Plugin</a>)<br /><br />So now we can start coding our release plugin but for the purpose of this post only stress part is going to be covered. You need to create a file called <span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;">Jenkinsfile</span> (the name is not mandatory but it is the de-facto name) on the root of your project, and in this case with next content:<br /><br /><script src="https://gist.github.com/lordofthejars/1a009e7532c55b4aa45fb01d9282240a.js"></script> In this case we are defining a new stage which is called Stress Test. Stage step is only used as informative and it will be used for logging purposes. Next a node is defined. A node is a <b>Jenkins</b> executor where to execute the code. Inside this node, the source code is checked out from the same location where <span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;">Jenkinsfile</span> is placed, sets a new environment variable pointing out to the location where the application is deployed, and finally a shell step which executes the <b>Gradle</b> test task.<br /><br />Last step in Jenkins is to create a new job of type Pipeline and set the location of the Jenkinsfile. So go to <i>Jenkins &gt; New Item &gt; Pipeline</i> and give a name to the job.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://2.bp.blogspot.com/-S2C59XC-3Ys/Vvz0IucCJYI/AAAAAAAAA1g/R9Ygo845EL0p7xBCDRGJoQslQSRK_N1ZA/s1600/Screen%2BShot%2B2016-03-31%2Bat%2B11.55.28.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="120" src="https://2.bp.blogspot.com/-S2C59XC-3Ys/Vvz0IucCJYI/AAAAAAAAA1g/R9Ygo845EL0p7xBCDRGJoQslQSRK_N1ZA/s400/Screen%2BShot%2B2016-03-31%2Bat%2B11.55.28.png" width="400" /></a></div><br />Then you only need to go to <i>Pipeline</i> section and configure the SCM repository where the project is stored.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-6oKa07yhsls/Vvz03DZkyrI/AAAAAAAAA1o/Cp6CYkl1nUY1duGZYXnmYtoagllBS3C2Q/s1600/Screen%2BShot%2B2016-03-31%2Bat%2B11.56.30.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="242" src="https://4.bp.blogspot.com/-6oKa07yhsls/Vvz03DZkyrI/AAAAAAAAA1o/Cp6CYkl1nUY1duGZYXnmYtoagllBS3C2Q/s400/Screen%2BShot%2B2016-03-31%2Bat%2B11.56.30.png" width="400" /></a></div><br />And then if you have correctly configured the hooks from <i>Jenkins</i> and your SCM server, this job is going to be executed for every commit, so your stress tests are going to run continuously.<br /><br />Of course probably you have noticed that stress tests are executed but no reports are published in <b>Jenkins</b>, so you have no way to see or compare results from different executions. For this reason you can use <i>publishHtml</i> plugin to store the generated reports in <b>Jenkins</b>. If you don't have the plugin installed yet, you need to install it as any other <b>Jenkins</b> plugin.<br /><br /><i>PublishHtml</i> plugin allows us to publish some html files generated by our build tool to <b>Jenkins</b> so they are available to users and also categorised by build number. You need to configure the location of the directory of files to publish, and here we find the first problem, do you remember that <b>Gatling</b> generates a directory with a random number? So we need to fix this first. You can follow different strategies, but the easiest one is simply rename the directory to a known static name after the tests.<br /><br />Open <b>Gradle</b> build file and add next content.<br /><br /><script src="https://gist.github.com/lordofthejars/3c1dd3690e23d990003977b94aa170c8.js"></script> We are creating a new task executed at the end of <i>test</i> task that renames the last created directory to <i>averageorbitalperiodsimulation</i>.<br /><br />Final step is add after <i>shell</i> call in <span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;">Jenkinsfile</span> next call:<br /><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;"><br /></span><span style="font-family: &quot;courier new&quot; , &quot;courier&quot; , monospace; font-size: x-small;">publishHTML(target: [reportDir:'stress-test/build/reports/gatling-results/averageorbitalperiodsimulation', reportFiles: 'index.html', reportName: 'Gatling report', keepAll: true])</span></div><div style="text-align: justify;"><br />After that you might see a link in the job page that points to the report.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-PN_kiJ_aU5U/Vv0lOMML_hI/AAAAAAAAA14/lsiZ2kEOywAO-xR9kGGNyX8KrouGTZhCw/s1600/Screen%2BShot%2B2016-03-31%2Bat%2B15.22.30.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="101" src="https://1.bp.blogspot.com/-PN_kiJ_aU5U/Vv0lOMML_hI/AAAAAAAAA14/lsiZ2kEOywAO-xR9kGGNyX8KrouGTZhCw/s400/Screen%2BShot%2B2016-03-31%2Bat%2B15.22.30.png" width="400" /></a></div><br />And that's all, thanks of <b>Gradle</b> and <b>Jenkins</b> you can implement a continuous stress testing strategy in an easy way and just using code the language all developers speak.<br /><br />We keep learning,<br />Alex.<br /><blockquote class="tr_bq">I can live whatever way I please, I move around among the seven seas, No one will miss me when the sun goes down, and in the morning I'be out of town (Movin' Cruisin' - The Fantastic Oceans)</blockquote><br />Music:&nbsp;<a href="https://www.youtube.com/watch?v=Byg5Xq_pb74">https://www.youtube.com/watch?v=Byg5Xq_pb74</a><br />Source Code:&nbsp;<a href="https://github.com/lordofthejars/starwars">https://github.com/lordofthejars/starwars</a><br /><br /><br /></div>Alex Sotohttps://plus.google.com/105815787800921676476noreply@blogger.com1