Mocking session in a Spock test for a java class

Spock is an excellent test framework that is built on top of Groovy and can be used to replace JUnit for testing Java classes. The majority of spock syntax is simple, but sometimes we run into issues where it is difficult to mock a class. Session is one of those examples. Given we want to do something simple, say get the name from Spring Security in session we’d have a method call like this

private String getUserId() {
  Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  return auth.getName();
}

To test this we need to mock session for this method and also for any method that calls this private method. To do so you need to mock the Authentication class and the SecurityContext class. Since this mocking is needed in multiple methods I’ve created a separate method in my spock class to mock the security context. The full test and method look like below

void "test getUserId"() {
  given:
  setSecurityContext()
  when:
  def response = service.getUserId()
  then:
  response == "hello"
}

def setSecurityContext() {
  def authentication = Mock(Authentication)
  authentication.getName() >> "hello"
  def securityContext = Mock(SecurityContext)
  securityContext.getAuthentication() >> authentication
  SecurityContextHolder.setContext(securityContext)
}

Has anyone found an easier way to do this? Or is this the way it needs to be?

read more

Moving a file from a local computer to a server through a jump server

This is a problem I’ve been routinely facing at work because we have new firewall rules and can only access new servers through a jump box. Accessing the server through ssh isn’t a problem after I’ve ssh’d into the jump server, but moving a file from my local computer to the other server is a pain. What you need to do is move the file from your local computer to an accesible directory on the jump server. For me this involves using WinSCP to drag and drop the file to my /home/mbusche directory.

Once the file has been moved to the jump server you need to use the scp command to move the file from the jump server to the other server. In my case the command looks something like this.

scp /home/mubusche/sonar.jar mbusche@cvms1255:/home/mbusche/

The syntax is

scp fileLocationOnCurrentServer username@servername:folderLocationToMoveFile/

Now generally you won’t have write access to all folders under your username and may need to sudo in as another user to move the file to the directory you need. To do that you need to login to the destination server via ssh, sign in as a user with permissions and then move the folder

ssh serverName
sudo su - userWithAccess
sudo mv /home/mbusche/sonar.jar /webdata/plugins/

read more

I'm speaking at dev.Objective() 2016

I’m happy to announce I’ve been selected to speak at dev.Objective() 2016! I will speaking on “Delivering Responsibly”.

Session Description

I’ll cover how to deliver fast, flexible and accessible websites. How you can deliver a consistent user experience to all users whether they’re on a small screen device or a 4k monitor. Whether they have a 2G connection or Google Fiber.

I’ll discuss some of the challenges we as developers face when trying to deliver responsibly and how to overcome some of those challenges. New specifications will make some of these challenges easier and I’ll cover how you can implement some of these features in non greenfield browsers.

I will discuss how http/2 mitigates some of the challenges we face, but how it doesn’t change as much as we might think when it comes to delivering responsibly.

Lastly I’ll cover the new Service Worker Specification and what it brings to the table.

read more

Multiple insert statement without counter

I often have a need to write an INSERT statement that adds multiple rows to a database. Obviously you can run any number of inserts separately, but that’s innefficient, so I loop over a list with a counter to know when to end. Recently I learned that the counter is unnecessary and you can create a fake SELECT statement to avoid needing a counter

<cfquery>
INSERT INTO Attendees (Schedule_ID, Attendee_ID, Meeting_ID, User_ID)
<cfloop list="#variables[eventlocation]#" index="i">
  SELECT #Schedule_ID#, #i#, #Meeting_ID#, 1
  UNION ALL
</cfloop>
<!--- returns no rows --->
SELECT 0, 0, 0, 0
WHERE 1 = 0
</cfquery>

For the sake of brevity I’ve avoided omitted the cfqueryparam’s that should always be used in queries.

read more

I'm speaking at Iowa Code Camp Fall 2015

I’m excited to announce I will be speaking at Iowa Code Camp on December 5th in Ankeny, Iowa on Responsible Responsive Web Design.

I’ll be discussing how you can design a website responsibly and delivery a consistent user experience to users on small and large screen devices. I’ll also talk about how to decrease your website’s page size quickly and effectively to make your website load fast even on the slowest of connections. We’ll also cover using the newest HTML specifications even on browsers that do not have native support.

read more

Opening/Editing Sharepoint documents in Chrome

If you’re like most people you don’t use Internet Explorer for any of your web browsing needs unless you’re required to. Thankfully most websites are compatible with most browsers and those instances are few and far between. If you use Sharepoint though, you know most activities function much better when you use Internet Explorer and some functionality is not available.

For example if you want to edit a document clicking the EDIT button gives you an error saying it cannot find the appropriate program to edit with. Thankfully I stumbled across an “edit in browser” option. When viewing a document click on the “…” which brings up a pop up. From there click on “…” again and you will see an Edit in Browser option. Not all functionality is available when you do this, but for most quick edits this will work excellent.

EditSharepointDocumentInChrome

read more

Increasing heap size for a Grails application

I had been running into a lot of issues with Grails running out of heap space on my computer and finally found a solution. In the Grails command line documentation is mentions you can set a maximum heap space when you run grails through the command line. I would keep forgetting to set this and have to restart the app.

export GRAILS_OPTS="-XX:PermSize=128m -XX:MaxPermSize=512m -Xms256m -Xmx512m"
grails run-app

I knew that had to be an easier way, so I created an Environment Variable called GRAILS_OPTS and set the value to

-XX:PermSize=128m -XX:MaxPermSize=512m -Xms256m -Xmx512m -server

Now these settings are used anytime I run grails.

read more

Fixing ESPN FantasyCast being broken in Chrome

This is the 2nd year in a row that ESPN has had a bug in their Fantasy Cast website so it does not render properly in Chrome. If your page looks like this, it’s broken. The good news is it’s easy to fix with a simple bookmarklet.

fantasy

Simply create a bookmark with this as the url

javascript:(function(){$('#real .progame-list').css("width", "101%");})()

and click on it to fix the page. Once clicked the page will look like this, which is much more user friendly.

ESPNFantasyCastChromeFixed

read more

Creating a bookmarklet to log a user in

As someone who is continually logging into an internal website while writing code I knew there had to be a better way than entering my username and password 100 times a day. To complicate this I’m also alternating between users with different admin rights, access, etc and remembering multiple usernames and passwords can be tedious. The solution? Create a bookmarklet that fills out the elements on the screen and clicks the submit button. I wouldn’t recommend you use this for anything other than an internal only application, but if you want to store your password in plain text in your bookmarks that are probably synced between computers go right ahead :).

You’ll need to inspect the DOM and get the ID values of the username, password and form to make this work. What you do is set the dcoument into a variable get your element by the ID and set the value to your username. The same goes for the password. If you’re having trouble getting this to work make sure your input fields have ID and you have the case typed in correctly.

javascript:(function(){
  var d=document;
  e=d.getElementById("userId");
  e.value="matt.busche";
  u=d.getElementById("password");
  u.value="hunter2";
  f=d.getElementById("loginForm");
  f.submit();
})();

Simple copy and paste the above code into the url field of a bookmark and you’ll be saving multiple seconds daily.

read more