Thursday, November 20, 2014

Spring Boot and JUnit

New gig is ramping up the use of Spring Boot. Absolutely love it. New gig is also committed to the idea of micro-services. There's pros and cons with that one. Moving on... Start with an example like this: 

https://spring.io/guides/gs/accessing-data-rest/ 

And we can temporarily store some rest/json data in a quick spring boot app with a quick maven command: 

mvn spring-boot:run

The trick is that if you want to run a j-unit test against this new web service, how do you identify it?  Should it spawn a new thread?  Should the test have a static dependency somewhere else in the test harness?

With a soup of annotations, you get the right way to do this unit test:

http://www.jayway.com/2014/07/04/integration-testing-a-spring-boot-application

But here's the thing about the above blog post.  It won't work.

It's missing a critical static pair of methods:


private static ConfigurableApplicationContext appContext;
    
@BeforeClass
public static void startBootApp(){
    appContext = SpringApplication.run(MockDataApplication.class, "");
}
    
@AfterClass
public static void shutdownBootApp(){
    appContext.close();

}


You have to do execute these statically.  You only want one spring boot application running before the unit test runs.  If you use @Before annotation, you'll get a new app launched before every @Test block.  

So that's it.  It's pretty sweet.  You load up the JPA repository with data, then you run tests against that web service, checking for the data.  Plus the RestAsssured library has a very slick json validation format. 

Work in progress of a test project can be found here:

https://github.com/bgardella/spring-boot-example







Wednesday, September 12, 2012

Spring Async and Future

I had a pesky reporting job that was going to be slow no matter what sql-fu I was going to come up with.  I knew that Spring had asynchronous tasks which I had used before but they kinda sucked.  Lots of manual bean building and xml config nasties.  But behold, I came across a nice annotation I was not aware of:

@Async


And I was off to the races.

I'll update with my example later.  For now, I give you the best blog entry to be found as of yet:

http://blog.inflinx.com/2012/09/09/spring-async-and-future-report-generation-example/

Thanks, Random Thoughts.


Tuesday, April 10, 2012

AVPlayer audio problems?

I'm finally working on an iphone app.  Yes, welcome to 2008!  And this one goes out to anyone who can't get their audio to play on their device, but it will work on simulators!  Fun!

Grrrrrr.

So here's the magic line of objective c that will get your video to play sound on a device.  Are you ready?  Here goes:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL];

I'm staring at this line of code.  It appears to say that any errors with audio, ignore them.  So now your audio will work.  Yay, Apple.  This is the future of app developement?

Sunday, January 29, 2012

ReCaptcha without plugins

So I'm not that fantastic at reading documentation.  But let's face it.  Coders are not that great at spelling things out.  Exhibit A:

http://code.google.com/apis/recaptcha/docs/verify.html

There is nothing on this API explanation that explains that you can't make this call from javascript.  Although I should have known that putting both public and private keys in JS seems a little insecure.

Ulitimately, you gotta make the reCaptcha verification call from the server-side.  Since I'm a java/spring guy I made a controller that does that.  This is the step that would have been obvious if I just looked more carefully at the example plugins.  But still.

@Controller
public class RecaptchaController {

    public static final String PUBLIC_KEY = "XXX";
    private static final String PRIVATE_KEY = "YYY";
    private static final String RECAPTCHA_URL = "http://www.google.com/recaptcha/api/verify";
   
    @RequestMapping(value="/recaptcha", method=RequestMethod.POST)
    public void checkServer(@RequestParam("remoteip") String remoteip, @RequestParam("challenge") String challenge,
                            @RequestParam("response") String response, HttpServletResponse httpResp){
           
        HttpClient httpClient = new HttpClient();
        PostMethod post = new PostMethod( RECAPTCHA_URL );

        post.addParameter("privatekey", PRIVATE_KEY);
        post.addParameter("remoteip", remoteip);
        post.addParameter("challenge", challenge);
        post.addParameter("response", response);
       
        String resp = "false";
        try {
            httpClient.executeMethod(post);
            resp = getResponseAsString(post);
        } catch (Exception e) {
            resp = "false";
        }
       
        if(resp.startsWith("false")){
            resp = "ERROR";
        }else{
            resp = "SUCCESS";
        }
       
        try {
            OutputStreamWriter writer = new OutputStreamWriter( httpResp.getOutputStream() );
            writer.write(resp);
            writer.close();
        } catch (IOException e) {
            // do nothing
        }
       
    }
   
    private String getResponseAsString(HttpMethod hm) throws IOException {
        InputStream inputStream = hm.getResponseBodyAsStream();

        StringWriter writer = new StringWriter();
        IOUtils.copy(inputStream, writer, "UTF-8");

        return writer.toString();
    }  
}

Thursday, November 10, 2011

Mobile Javascript Links

Before I forget, I wanted to pass on some helpful bookmarks
for mobile javascript:



Here's how you do that mini-scroll on page load to hide the address bar:

   setTimeout(function(){
       // Hide the address bar!
       window.scrollTo(0, 1);
   }, 0);


Here's a very fancy network detector:

/**
 * Original idea:
 * http://stackoverflow.com/questions/4697470/detecting-wifi-connectivity-in-mobile-safari
 *
 * but more importantly here:
 * http://www.thefutureoftheweb.com/blog/image-onload-isnt-being-called
 *
 */
metai.checkNetwork = function( contentId ){

       var imageEl=document.
       createElement('img');
       var imageurl=hostURI+contextRoot+'/image/mobile/login/caution.png';
       imageEl.style.visibility='hidden';
       //imageEl.onload=metai.networkLoad;
       //var imagetook=0;
       var startDate=new Date();
       imageEl.onload=function(){
           var endDate=new Date();
           var latency=endDate.getTime()-startDate.getTime();
           //alert(latency);

           if( latency < 200 ){
               playFast( contentId );
           }else{
               playSlow( contentId );
           }
       }

       imageEl.src=imageurl+'?r='+Math.round(Math.random()*1000);
       document.body.appendChild(imageEl);

}

Tuesday, November 1, 2011

List of Helpful Mac Apps

I'm starting this post to collect the most useful OSX utilities as I run across them.
  1. WMA converter:  http://www.nch.com.au/switch/index.html

Friday, October 28, 2011

Smell My Awesomeness

This week I launched my first mobile product.  It's a simple html/javascript product optimized for mobile devices.  I'm pretty proud of it, but the content is NSFW so I'm not forwarding a direct link yet.  But here's some customer feedback that makes me proud:

"Only dissapointment is that it works infinitely better and more fluidly than the actual site itself – and my 3G connection is poor in comparison too!"

Yes, I am one proud papa this week.