SignedTokenFactory implements SimpleTokenFactory { @Override public AttachmentAccessToken generateAttachmentAccessToken(String username, String blobId) { Preconditions.checkArgument(! Strings.isNullOrEmpty(blobId)); ZonedDateTime expirationTime = zonedDateTimeProvider.get().plusMinutes(5); return AttachmentAccessToken.builder() .username(username) .blobId(blobId) .expirationDate(expirationTime) .signature(signatureHandler.sign(Joiner.on(AttachmentAccessToken.SEPARATOR) .join(blobId, username, DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(expirationTime)))) .build(); } @Inject  SignedTokenFactory(SignatureHandler signatureHandler, ZonedDateTimeProvider zonedDateTimeProvider); @Override ContinuationToken generateContinuationToken(String username); @Override AttachmentAccessToken generateAttachmentAccessToken(String username, String blobId);  }
@Test public void generateAttachmentAccessTokenShouldThrowWhenUsernameIsNull() throws Exception { assertThatThrownBy(() -> toKenFactory.generateAttachmentAccessToken(null, "blobId")) .isInstanceOf(NullPointerException.class); }
@Test public void generateAttachmentAccessTokenShouldThrowWhenBlobIdIsNull() throws Exception { assertThatThrownBy(() -> toKenFactory.generateAttachmentAccessToken("username", null)) .isInstanceOf(IllegalArgumentException.class); }
@Test public void generateAttachmentAccessTokenShouldHaveTheRightOutPut() throws Exception { zonedDateTimeProvider.setFixedDateTime(DATE); assertThat(toKenFactory.generateAttachmentAccessToken("user", "blobId").serialize()) .isEqualTo("user_2011-12-03T10:20:30+01:00_eSg1mVxMpqw5/u6wsTAatP7hoHDoI7blEW0hxGPrRMMj3hECT+YhbUCdhz9Lb4U+jsYPgNLDuAHwxin79xXfLoq0nVsogEE32svRYVvbaDpro+EOtkAHhYnYxWnAGxB/70u7Zyw0oYGmWOwkCkLDFsWKglMp9IUpOJQP50zbzbdW+4dKlAi/8VmN8jFyZx40envRbgEn4Q2QQbnUH/7F9+vdLIl+bAfcj6QlevqFRsUkmTZelkv1rtGUAvnPSBQL4TeBx5Qk/eEiw8IbB2lbCIAoIFZC6Vl8QOO5Y6LFzmqHL9i0BjvuoiZ8FKQS0pGd5CU6pwc7sv0xD82Vx1eFiw=="); }