Read the application log when testing your Spring application

From time to time I need to check so that a function correctly logs error messages to the application log. Here is one solution using Logback in a Spring application

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.Appender;

import org.mockito.ArgumentMatcher;

import org.slf4j.LoggerFactory;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

@Test
public void shouldWriteErrorMessageToLog() {
  Logger l = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
  Appender mockAppender = mock(Appender.class);
  l.addAppender(mockAppender);

  /*
   * Call function that is expected to write to the application log
   */

  verify(mockAppender).doAppend(argThat(new ArgumentMatcher() {
    @Override
    public boolean matches(final Object argument) {
      return ((LoggingEvent)argument).getMessage().contains("FooBar");
    }
  }));
}

Tested with Spring Boot v2.2.4 and TestNG v6.11

Software Mangement 101

After the new and flashy system has been introduced into an organisation it starts to be the time for some software management, mainly to keep the system up to date with current techniques but also to keep it easy to run and upgrade. Here are some of my observations from my years as a software engineer:

1. At least once a year:
  • Check language version
  • Check OS version
  • Check any used framework versions

If anything above is overdue for an update or out of support:
Update as the first priority!

2. After this we do (in order):
  1. Bugfixes
  2. Operational improvements
  3. New Features
3. All bug fixes and new features should have tests that prove they work:
  • Unit tests for backend changes
  • Selenium, or eqvivalent, for frontend
  • Aim for a test coverage of at least 80%
  • Always have at leas one more developer that checks any change before a merge/deploy
  • ALL TEST MUST PASS BEFORE A DEPLOY TO PRODUCTION

It does not have to be more complicated than this 🙂 Remember to KEEP IT SIMPLE!

Yubikey: Manual pair a PIV Yubikey with OSX

Every now and then things does not work as intended. For example: when putting a new Yubikey that has certificates, PIN and PUK installed into a slot on your Mac, a pairing dialog should appear. This does not always happen and in this case we need to do a “manual pairing”.
I will here explain how:

1. Insert the Yubikey into the Mac
2. Open a terminal
3. Run the sc_auth command below

sc_auth identities

This will output something like this:

SmartCard: com.apple.pivtoken:2B20E9654D142033695ADEC481CFD11EBA45EC00
Unpaired identities:
C28BE4EC86FAAC4B5EFE825947240B2CE03BA4F2       Certificate For PIV Authentication (<username>)

4. Now run the following comman to start the pairing process

sudo sc_auth pair -f -u<username> -hC28BE4EC86FAAC4B5EFE825947240B2CE03BA4F2

NOTE:
<username> – is the username of the identity that you want to pair with your Yubikey
“C28BE4EC86FAAC4B5EFE825947240B2CE03BA4F2” – is the Yubikey hash for the identity above (the CN of the authentication certificate)

The process will now begin with asking for the Yubikey PIN

5. If all goes well you should now see a message “Pairing Successful”

You are now ready to use your Yubikey on your Mac

Tested on OSX Catalina 10.15.4 and Yubikey firmware 4.4.5