Posts

How to create a Cassandra container for testing with Keyspace and the latest schema with a single script call

Image
The problem: We have an application that uses Cassandra as the preferred persistence technology and we would like to have a one-command container running for local development and testing.

In other words, we need the Cassandra container to have the Keyspace and the up-to-date schema following our migration scripts by executing something like  ./run_containerised_cassandra_locally.sh
Breaking this problem into two sub-problems we identify that in order to do that we need a DB migration tool. In RDBMS we have tools like Flyway or Liquibase. For Cassandra, with a little bit of digging, com.hhandoko:cassandra-migration can be found. Which we can be incorporated into our codebase as a dependency or can exist as a standalone artefact (jar file) to be run on demand by a script. The latest is what we are going to use here.
When we go to releases of that repo, we can see the version of the jar with dependencies. (Current versioncassandra-migration-0.17-jar-with-dependencies.jar).

We want to …

DTOs conversion with reflection example - applied on Thymeleaf (or how to get read of Pojo's conversion boilerplate)

I was playing around with Thymeleaf the other day and I wanted to pass all values of my ModelAttribute POJO (Person.class in the example) to Spring's UI Model so I can use the values to the template. (Example to follow).


A form is filled, and the values are passed to a controller that will render them to a new view (html) page. Nothing complex or useful, but just an alternative to hello world if you will.

The ModelAttribute Person.class. Ignore the @ModelElement annotations for now.

The main page that will render a simple form with 1 textfield (name),  one numeric field (age), one drop down menu to restrict the input values of a java enum (sex) and of course the submit button.

When we click the submit button the following controller will be called, passing the values via the Person object. We then are passing them to Spring's model attribute so we can use them to our template. Lines 4-6 should belong in a different class but for simplicity I am putting them in the controlle…

[Video] OOD Guided by Memes, the fun on being SOLID

Image
This is a presentation I made for my colleagues on SOLID principles and clean code.
After some positive feedback I decided to record it. Constructive feedback is more than welcome :)


How to test file operations with Junit5 TempDir

In Junit4 we had TemporaryFolder. In Junit5 the way to test objects that are interacting with the file system it is pretty straightforward with the use of @TempDir.

To make things easier to understand lets assume that we have a class that reads all the files, directories, sub directories and files in sub directories and gathers information about them in a form of a simple dto that contains the extension, if it is directory, the absolute path etc. and adds them in a list.

So given a Path you get a list of objects that contain the file information.

In order to test this functionality we are going to use the TempDir helper from Junit5.
This will create a temporary directory in our filesystem that will be cleaned automatically when our testing is finished.

Check out the example bellow



Check on line 20 how we pass the temporary directory as a test parameter annotated accordingly.

@TempDir will create a directory (depending on your OS) on a tmp folder like /tmp/junit6110933399026626368, but th…

Java Primitive's little secrets

Here is a bunch of java primitives' interesting behaviour that we ofter ignore or forget! Recap time in 3 - 2 - 1...
Data type promotions When you try to perform a numerical operation on a short type for example, the result will be promoted to int, therefore it wont compile if you try to store the result in a short variable unless you explicitly cast it (check line x).


The same applies to byte. Adding two numbers of type byte they will automatically be promoted to short.

What about int? Adding two number of type int together wont result to a long promotion (of course)...

Same as int if you try to to add together two float numbers, the result wont be promoted to double.

All the above are great... but... There is an interesting behaviour when you do the following


the above is perfectly valid, but the bellow results in a compilation error:


Yes indeed!

an easier one now:
Please welcome the infamous Numeric Overflow!



Make your bash script runnable from every directory on the terminal

Have you ever wanted to create a utility like ls for example and make it available from everywhere in the linux's/mac's terminal?

There two ways to do that.
Way 1 (lets assume that my script is in a file with the name hello.)
a.move your script to /bin

sudo mv hello /bin/hello

b.make your script executable

sudo chmod +x /bin/hello

you are ready to ramble!


Way 2
create a file with different functions and aliases like the example bellow. Lets assume that the file name is .myfunc

the file might look like:


Now on your home directory find a file called .bashrc. If you are using a different shell (like zsh) the file might be different. For example if you are using zsh you should look for .zshrc.

Mind that the file names starting with dot (.) in POSIX systems (like linux and macOs) are hidden files.

when you find the file .bashrc, edit it with VI or the editor of your choice and add somewhere (it does not matter where) the line
source {path to your .myfunc file}
If .myfunc is stored …