### Question:
EncryptedFieldConverter extends TypeConverter implements SimpleValueConverter { @Override public Object decode(Class<?> targetClass, Object fromDBObject, MappedField optionalExtraInfo) { if (fromDBObject == null || !(fromDBObject instanceof byte[]) || (! optionalExtraInfo.getField().isAnnotationPresent(Encrypted.class))) { return fromDBObject; } byte[] decrypted = getEncryptionService().decryptData((byte[]) fromDBObject); if (optionalExtraInfo.getType() == String.class) { return new String(decrypted, ENCODING); } else if (optionalExtraInfo.getType() == Integer.class) { String string = new String(decrypted, ENCODING); return Integer.parseInt(string); } else if (optionalExtraInfo.getType() == EncryptedZonedDateTime.class) { String string = new String(decrypted, ENCODING); return new EncryptedZonedDateTime(ZonedDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(string)), ZoneId.of("UTC"))); } else { return decrypted; } } EncryptedFieldConverter(); @Override Object encode(Object value, MappedField optionalExtraInfo); @Override Object decode(Class<?> targetClass, Object fromDBObject, MappedField optionalExtraInfo); EncryptionService getEncryptionService();  }### Answer:
@Test public void decodeZonedDateTime() throws Exception { EncryptedFieldConverter cut = createCut(); final Field mockedField = mock(Field.class); when(mockedField.isAnnotationPresent(Encrypted.class)).thenReturn(true); final MappedField mockedMappedField = mock(MappedField.class); when(mockedMappedField.getField()).thenReturn(mockedField); when(mockedMappedField.getType()).thenReturn(EncryptedZonedDateTime.class); final Object decode = cut.decode(ZonedDateTime.class, Base64.decodeBase64(ENCODED_ZONEDDATETIME), mockedMappedField); assertThat(decode, instanceOf(EncryptedZonedDateTime.class)); assertThat(((EncryptedZonedDateTime) decode).getZonedDateTime(), is(TEST_ZONEDDATETIME)); }
@Test public void decodeString() throws Exception { EncryptedFieldConverter cut = createCut(); final Field mockedField = mock(Field.class); when(mockedField.isAnnotationPresent(Encrypted.class)).thenReturn(true); final MappedField mockedMappedField = mock(MappedField.class); when(mockedMappedField.getField()).thenReturn(mockedField); when(mockedMappedField.getType()).thenReturn(String.class); final Object decode = cut.decode(String.class, Base64.decodeBase64(ENCODED_STRING), mockedMappedField); assertThat(decode, instanceOf(String.class)); assertThat(decode, is(TEST_STRING)); }
@Test public void decodeInteger() throws Exception { EncryptedFieldConverter cut = createCut(); final Field mockedField = mock(Field.class); when(mockedField.isAnnotationPresent(Encrypted.class)).thenReturn(true); final MappedField mockedMappedField = mock(MappedField.class); when(mockedMappedField.getField()).thenReturn(mockedField); when(mockedMappedField.getType()).thenReturn(Integer.class); final Object decode = cut.decode(Integer.class, Base64.decodeBase64(ENCODED_INTEGER), mockedMappedField); assertThat(decode, instanceOf(Integer.class)); assertThat(decode, is(TEST_INTEGER)); }
@Test public void decodeByteArray() throws Exception { EncryptedFieldConverter cut = createCut(); final Field mockedField = mock(Field.class); when(mockedField.isAnnotationPresent(Encrypted.class)).thenReturn(true); final MappedField mockedMappedField = mock(MappedField.class); when(mockedMappedField.getField()).thenReturn(mockedField); when(mockedMappedField.getType()).thenReturn(byte[].class); final Object decode = cut.decode(byte[].class, Base64.decodeBase64(ENCODED_BYTE_ARRAY), mockedMappedField); assertThat(decode, instanceOf(byte[].class)); assertThat(decode, is(TEST_BYTE_ARRAY)); }