ClientDAO { public void sawClient(Client client, @Nullable Principal principal) { Instant now = Instant.now(); Instant lastSeen = Optional.ofNullable(client.getLastSeen()) .map(ls -> Instant.ofEpochSecond(ls.toEpochSecond())) .orElse(EPOCH); final Instant expiration; if (principal instanceof CertificatePrincipal) { expiration = ((CertificatePrincipal) principal).getCertificateExpiration(); } else { expiration = EPOCH; } if (now.isAfter(lastSeen.plus(LAST_SEEN_THRESHOLD))) { dslContext.transaction(configuration -> { Param<Long> lastSeenValue = DSL.val(now.getEpochSecond(), CLIENTS.LASTSEEN); Param<Long> expirationValue = DSL.val(expiration.getEpochSecond(), CLIENTS.EXPIRATION); DSL.using(configuration) .update(CLIENTS) .set(CLIENTS.LASTSEEN, when(CLIENTS.LASTSEEN.isNull(), lastSeenValue) .otherwise(greatest(CLIENTS.LASTSEEN, lastSeenValue))) .set(CLIENTS.EXPIRATION, expirationValue) .where(CLIENTS.ID.eq(client.getId())) .execute(); }); } } private  ClientDAO(DSLContext dslContext, ClientMapper clientMapper,
      RowHmacGenerator rowHmacGenerator); long createClient(String name, String user, String description,
      @Nullable URI spiffeId); void deleteClient(Client client); void sawClient(Client client, @Nullable Principal principal); Optional<Client> getClientByName(String name); Optional<Client> getClientBySpiffeId(URI spiffeId); Optional<Client> getClientById(long id); ImmutableSet<Client> getClients();  }
@Test public void sawClientTest() { assertThat(client1.getLastSeen()).isNull(); assertThat(client2.getLastSeen()).isNull(); Instant expiration = Instant.now(); expiration = expiration.minusNanos(expiration.get(NANO_OF_SECOND)); CertificatePrincipal principal = mock(CertificatePrincipal.class); when(principal.getCertificateExpiration()).thenReturn(expiration); ApiDate now = ApiDate.now(); clientDAO.sawClient(client1, principal); Client client1v2 = clientDAO.getClientByName(client1.getName()).get(); Client client2v2 = clientDAO.getClientByName(client2.getName()).get(); assertThat(client1v2.getLastSeen()).isNotNull(); assertTrue(client1v2.getLastSeen().toEpochSecond() >= now.toEpochSecond()); assertThat(client2v2.getLastSeen()).isNull(); assertThat(client1v2.getExpiration()).isNotNull(); assertThat(client1v2.getExpiration().toInstant()).isEqualTo(expiration); assertThat(client2v2.getExpiration()).isNull(); }