TouK Hackathon – March 2019

Hackathons are prone to fail. It’s because we have very little time to create something useful, actually working and making a wow!-effect.

So this time we wanted to run “THE HACKATHON”, a well-prepared event with a very high success rate. We called it “Ship IT!” to ensure that we will focus on delivering a working product, not only on having fun while making it. We formed an organising team responsible for internal communication, collecting the needs, removing obstacles and buying some snacks and pizza for the event itself.

We thought that if we want to succeed, we’ll have to prepare well. So each team gathered before the event and talked about their plans, the technologies they intend to use and what hardware they need to order. The organizers met with all teams, exchanged details and gave some good advice.

And finally, the day has come! Seven teams started the two-day hacking session at 9AM, continued until 5PM, and showed the demos of all seven projects after 4.30PM on the second day. We gathered 25 participants total.

Here is a quick presentation of the projects we delivered.

Lidar/ROS.org based robot

robot image We always wanted to build a fully autonomous vehicle to transport sandwiches and monitor the Wi-Fi quality over the whole office space. The first step we took was creating a remote controlled platform with Lidar, which allowed to build the office map and made a foundation for future experiments. We reached this goal with ROS.org and mapped a few corridors and rooms in our office at the end of the hackathon. Now we’re almost ready to conquer Mars :D

Quak Liero-like Game

quak team Quak is a PvP platform game inspired by Liero or Soldat. We used a mix of Kotlin, libGDX and box2d (physics) to build a good-looking 2D game in less than 2 days. Destructible level as a mesh of boxes are generated from provided png layers. Additionally, we implemented some extras to diversify the player experience, such as multiple weapons, double jump, blood effects and explosions.

Quiz Game

quiz game DuckQuiz is a game we wanted to use at public event, e.g. conferences. It’s a little bit similar to the legendary Flappy Bird. Your goal is to make the title hero – a duck – go as far as possible! Correct answers give the duck a bump, so the more questions you answer correctly, the further the duck will go. Be careful, though! Three wrong answers will make a brick fall on the duck’s head! To spice things up a little, we decided to introduce a score threshold, which – once achieved – guaranteed a prize. Plus, of course, if you beat the highest score, you will make history as the reigning champion Duck Flyer. The quiz application is written in Kotlin for Android. We used RxJava and Retrofit for communication with the server. For the back-end side, we chose SpringBoot along with Kotlin. There is also a web-based “Hall of Fame” panel and a simple CMS which allows the management of the quiz questions, written in ReactJS.

Own Card Authorization system

demo4 We’re waiting for our private brand new vending machine which we’re going to use for distribution of snacks in Touk. We’ve noticed that we can communicate with it using MDB protocol. We’d like to authenticate in the machine with RFID cards which are commonly used in TouK to open doors. As a first step, we wanted to create authorization system based on RFID cards, but with additional PIN verification part added. Our system should be integrated with internal LDAP. We’ve separated our project into 4 modules: backend (spring boot), web frontend (scalajs-react), mobile app (android) and hardware device (arduino yun). Backend part connects all modules and allows matching RFID cards UID with LDAP logins. Web frontend is designed for users to set and manage their PINs. The mobile app at the moment is dedicated for our administrator to make pairing existing cards with LDAP logins easier. Finally, hardware device was created to read data from card, get PIN from user and check if PIN is correct (via backend). In the next step, we’d like to integrate our system with mentioned vending machine and make payments using it. There is also a plan to enhance mobile app and use it as mobile payment terminal and integrate web UI with Touk SelfCare system.

Notification lights

lights hackers Three suites (3 lights in red, yellow and blue) controlled by Raspberry Pi Zero W communicating with the managing service via MQTT protocol. The service handles messages from Rocket’s (our chat) outgoing web hooks and notifications from GitLab (e.g. opened merge requests).

Team gathering app (e.g. for football matches)

Team gathering app We regularly play football and volleyball after work, but we sometimes struggle with completing the teams. So far, we used a sign-up system based on Confluence pages, which is not mobile-friendly. We also wanted to allow registration for “reserve” players from outside the company. That’s why we built a simple mobile app with a sign-up form, game history and basic player ranking. The app was built with Dart using the Flutter framework and Firebase for storage and authorization.

Internal time reporting API

hr This HR project’s goal was to integrate and simplify the way we log time in our company. We’ve achieved that with a Google Calendar-like GUI and multiple microservices written in TypeScript, Python, Java and Scala!

The Grand Finale

demo1 At the end of “Ship IT!”, the teams had demonstrated the effects of their work. They shared some successes and other stories :) about the used software and hardware and the lessons learned during those two intensive days. The audience were amused when the robot started to explore the room. The Quak team had a bloody battle in their game. The lights were blinking when one of the team members wrote “kanapki” on our internal TouK chat.

demo2 demo3 demo5 demo6

It was very impressive that each team managed to deliver a viable result and show their projects in action. We can’t wait to run the next edition of The TouK Hackahton and hope there will be even more participants and surprising ideas to see.

You May Also Like

Spock basics

Spock (homepage) is like its authors say 'testing and specification framework'. Spock combines very elegant and natural syntax with the powerful capabilities. And what is most important it is easy to use.

One note at the very beginning: I assume that you are already familiar with principles of Test Driven Development and you know how to use testing framework like for example JUnit.

So how can I start?


Writing spock specifications is very easy. We need basic configuration of Spock and Groovy dependencies (if you are using mavenized project with Eclipse look to my previous post: Spock, Java and Maven). Once we have everything set up and running smooth we can write our first specs (spec or specification is equivalent for test class in other frameworks like JUnit of TestNG).

What is great with Spock is fact that we can use it to test both Groovy projects and pure Java projects or even mixed projects.


Let's go!


Every spec class must inherit from spock.lang.Specification class. Only then test runner will recognize it as test class and start tests. We will write few specs for this simple class: User class and few tests not connected with this particular class.

We start with defining our class:
import spock.lang.*

class UserSpec extends Specification {

}
Now we can proceed to defining test fixtures and test methods.

All activites we want to perform before each test method, are to be put in def setup() {...} method and everything we want to be run after each test should be put in def cleanup() {...} method (they are equivalents for JUnit methods with @Before and @After annotations).

It can look like this:
class UserSpec extends Specification {
User user
Document document

def setup() {
user = new User()
document = DocumentTestFactory.createDocumentWithTitle("doc1")
}

def cleanup() {

}
}
Of course we can use field initialization for instantiating test objects:
class UserSpec extends Specification {
User user = new User()
Document document = DocumentTestFactory.createDocumentWithTitle("doc1")

def setup() {

}

def cleanup() {

}
}

What is more readable or preferred? It is just a matter of taste because according to Spock docs behaviour is the same in these two cases.

It is worth mentioning that JUnit @BeforeClass/@AfterClass are also present in Spock as def setupSpec() {...} and def cleanupSpec() {...}. They will be runned before first test and after last test method.


First tests


In Spock every method in specification class, expect setup/cleanup, is treated by runner as a test method (unless you annotate it with @Ignore).

Very interesting feature of Spock and Groovy is ability to name methods with full sentences just like regular strings:
class UserSpec extends Specification {
// ...

def "should assign coment to user"() {
// ...
}
}
With such naming convention we can write real specification and include details about specified behaviour in method name, what is very convenient when reading test reports and analyzing errors.

Test method (also called feature method) is logically divided into few blocks, each with its own purpose. Blocks are defined like labels in Java (but they are transformed with Groovy AST transform features) and some of them must be put in code in specific order.

Most basic and common schema for Spock test is:
class UserSpec extends Specification {
// ...

def "should assign coment to user"() {
given:
// do initialization of test objects
when:
// perform actions to be tested
then:
// collect and analyze results
}
}

But there are more blocks like:
  • setup
  • expect
  • where
  • cleanup
In next section I am going to describe each block shortly with little examples.

given block

This block is used to setup test objects and their state. It has to be first block in test and cannot be repeated. Below is little example how can it be used:
class UserSpec extends Specification {
// ...

def "should add project to user and mark user as project's owner"() {
given:
User user = new User()
Project project = ProjectTestFactory.createProjectWithName("simple project")
// ...
}
}

In this code given block contains initialization of test objects and nothing more. We create simple user without any specified attributes and project with given name. In case when some of these objects could be reused in more feature methods, it could be worth putting initialization in setup method.

when and then blocks

When block contains action we want to test (Spock documentation calls it 'stimulus'). This block always occurs in pair with then block, where we are verifying response for satisfying certain conditions. Assume we have this simple test case:
class UserSpec extends Specification {
// ...

def "should assign user to comment when adding comment to user"() {
given:
User user = new User()
Comment comment = new Comment()
when:
user.addComment(comment)
then:
comment.getUserWhoCreatedComment().equals(user)
}

// ...
}

In when block there is a call of tested method and nothing more. After we are sure our action was performed, we can check for desired conditions in then block.

Then block is very well structured and its every line is treated by Spock as boolean statement. That means, Spock expects that we write instructions containing comparisons and expressions returning true or false, so we can create then block with such statements:
user.getName() == "John"
user.getAge() == 40
!user.isEnabled()
Each of lines will be treated as single assertion and will be evaluated by Spock.

Sometimes we expect that our method throws an exception under given circumstances. We can write test for it with use of thrown method:
class CommentSpec extends Specification {
def "should throw exception when adding null document to comment"() {
given:
Comment comment = new Comment()
when:
comment.setCommentedDocument(null)
then:
thrown(RuntimeException)
}
}

In this test we want to make sure that passing incorrect parameters is correctly handled by tested method and that method throws an exception in response. In case you want to be certain that method does not throw particular exception, simply use notThrown method.


expect block

Expect block is primarily used when we do not want to separate when and then blocks because it is unnatural. It is especially useful for simple test (and according to TDD rules all test should be simple and short) with only one condition to check, like in this example (it is simple but should show the idea):
def "should create user with given name"() {
given:
User user = UserTestFactory.createUser("john doe")
expect:
user.getName() == "john doe"
}



More blocks!


That were very simple tests with standard Spock test layout and canonical divide into given/when/then parts. But Spock offers more possibilities in writing tests and provides more blocks.


setup/cleanup blocks

These two blocks have the very same functionality as the def setup and def cleanup methods in specification. They allow to perform some actions before test and after test. But unlike these methods (which are shared between all tests) blocks work only in methods they are defined in. 


where - easy way to create readable parameterized tests

Very often when we create unit tests there is a need to "feed" them with sample data to test various cases and border values. With Spock this task is very easy and straighforward. To provide test data to feature method, we need to use where block. Let's take a look at little the piece of code:

def "should successfully validate emails with valid syntax"() {
expect:
emailValidator.validate(email) == true
where:
email }

In this example, Spock creates variable called email which is used when calling method being tested. Internally feature method is called once, but framework iterates over given values and calls expect/when block as many times as there are values (however, if we use @Unroll annotation Spock can create separate run for each of given values, more about it in one of next examples).

Now, lets assume that we want our feature method to test both successful and failure validations. To achieve that goal we can create few 
parameterized variables for both input parameter and expected result. Here is a little example:

def "should perform validation of email addresses"() {
expect:
emailValidator.validate(email) == result
where:
email result }
Well, it looks nice, but Spock can do much better. It offers tabular format of defining parameters for test what is much more readable and natural. Lets take a look:
def "should perform validation of email addresses"() {
expect:
emailValidator.validate(email) == result
where:
email | result
"WTF" | false
"@domain" | false
"foo@bar.com" | true
"a@test" | false
}
In this code, each column of our "table" is treated as a separate variable and rows are values for subsequent test iterations.

Another useful feature of Spock during parameterizing test is its ability to "unroll" each parameterized test. Feature method from previous example could be defined as (the body stays the same, so I do not repeat it):
@Unroll("should validate email #email")
def "should perform validation of email addresses"() {
// ...
}
With that annotation, Spock generate few methods each with its own name and run them separately. We can use symbols from where blocks in @Unroll argument by preceding it with '#' sign what is a signal to Spock to use it in generated method name.


What next?


Well, that was just quick and short journey  through Spock and its capabilities. However, with that basic tutorial you are ready to write many unit tests. In one of my future posts I am going to describe more features of Spock focusing especially on its mocking abilities.