TimestampTokenConverter { public static TimestampToken toTimestampToken(TimestampDTO timestampDTO) throws DSSException { Objects.requireNonNull(timestampDTO, "TimestampDTO cannot be null!"); Objects.requireNonNull(timestampDTO.getBinaries(), "TimestampDTO binaries cannot be null!"); Objects.requireNonNull(timestampDTO.getType(), "TimestampDTO type cannot be null!"); try { TimestampToken timestampToken = new TimestampToken(timestampDTO.getBinaries(), timestampDTO.getType()); timestampToken.setCanonicalizationMethod(timestampDTO.getCanonicalizationMethod()); if (timestampDTO.getIncludes() != null) { timestampToken.setTimestampIncludes(toTimestampIncludes(timestampDTO.getIncludes())); } return timestampToken; } catch (TSPException | IOException | CMSException e) { throw new DSSException(String.format("Cannot convert a TimestampDTO to TimestampToken class, reason : '%s'", e.getMessage()), e); } }  static List<TimestampToken> toTimestampTokens(List<TimestampDTO> timestampDTOs); static TimestampToken toTimestampToken(TimestampDTO timestampDTO); static TimestampDTO toTimestampDTO(TimestampToken timestampToken);  }
@Test public void toTimestampTokenTest() throws Exception { TimestampDTO timestampDTO = new TimestampDTO(Utils.fromBase64(timestampBinaries), TimestampType.INDIVIDUAL_DATA_OBJECTS_TIMESTAMP); timestampDTO.setCanonicalizationMethod(CanonicalizationMethod.INCLUSIVE); timestampDTO.setIncludes(Arrays.asList(new TimestampIncludeDTO("reference-id-1", true))); TimestampToken timestampToken = TimestampTokenConverter.toTimestampToken(timestampDTO); assertNotNull(timestampToken); assertEquals(TimestampType.INDIVIDUAL_DATA_OBJECTS_TIMESTAMP, timestampToken.getTimeStampType()); assertEquals(CanonicalizationMethod.INCLUSIVE, timestampToken.getCanonicalizationMethod()); assertEquals(1, timestampToken.getTimestampIncludes().size()); assertEquals("reference-id-1", timestampToken.getTimestampIncludes().get(0).getURI()); assertTrue(timestampToken.getTimestampIncludes().get(0).isReferencedData()); assertTrue(Arrays.equals(Utils.fromBase64(timestampBinaries), timestampToken.getEncoded())); }
@Test public void emptyTimestampTokenDTOTest() { Exception e = assertThrows(NullPointerException.class, () -> TimestampTokenConverter.toTimestampToken(null)); assertEquals("TimestampDTO cannot be null!", e.getMessage()); TimestampDTO emptyDTO = new TimestampDTO(); e = assertThrows(NullPointerException.class, () -> TimestampTokenConverter.toTimestampToken(emptyDTO)); assertEquals("TimestampDTO binaries cannot be null!", e.getMessage()); TimestampDTO nullBinary = new TimestampDTO(null, TimestampType.CONTENT_TIMESTAMP); e = assertThrows(NullPointerException.class, () -> TimestampTokenConverter.toTimestampToken(nullBinary)); assertEquals("TimestampDTO binaries cannot be null!", e.getMessage()); TimestampDTO nullType = new TimestampDTO(Utils.fromBase64(timestampBinaries), null); e = assertThrows(NullPointerException.class, () -> TimestampTokenConverter.toTimestampToken(nullType)); assertEquals("TimestampDTO type cannot be null!", e.getMessage()); }
@Test public void wrongBinaryTest() { TimestampDTO wrongBinary = new TimestampDTO(new byte[] { 1, 2, 3 }, TimestampType.CONTENT_TIMESTAMP); Exception e = assertThrows(DSSException.class, () -> TimestampTokenConverter.toTimestampToken(wrongBinary)); assertTrue(e.getMessage().contains("Cannot convert a TimestampDTO to TimestampToken class")); }