EncryptMergedResult implements MergedResult { @Override public Object getValue(final int columnIndex, final Class<?> type) throws SQLException { if (!queryWithCipherColumn) { return mergedResult.getValue(columnIndex, type); } Optional<EncryptAlgorithm> encryptAlgorithm = metaData.findEncryptor(columnIndex); if (!encryptAlgorithm.isPresent()) { return mergedResult.getValue(columnIndex, type); } String ciphertext = (String) mergedResult.getValue(columnIndex, String.class); return null == ciphertext ? null : encryptAlgorithm.get().decrypt(ciphertext); }  @Override boolean next(); @Override Object getValue(final int columnIndex, final Class<?> type); @Override Object getCalendarValue(final int columnIndex, final Class<?> type, final Calendar calendar); @Override InputStream getInputStream(final int columnIndex, final String type); @Override boolean wasNull();  }
@Test public void assertGetValueWithQueryWithPlainColumn() throws SQLException { when(mergedResult.getValue(1, String.class)).thenReturn("VALUE"); assertThat(new EncryptMergedResult(metaData, mergedResult, false).getValue(1, String.class), is("VALUE")); }
@Test public void assertGetValueWithQueryWithCipherColumnAndMismatchedEncryptor() throws SQLException { when(mergedResult.getValue(1, String.class)).thenReturn("VALUE"); when(metaData.findEncryptor(1)).thenReturn(Optional.empty()); assertThat(new EncryptMergedResult(metaData, mergedResult, true).getValue(1, String.class), is("VALUE")); }
@Test public void assertGetValueWithQueryWithCipherColumnAndMatchedEncryptorWithNotNullCiphertext() throws SQLException { when(mergedResult.getValue(1, String.class)).thenReturn("VALUE"); EncryptAlgorithm encryptAlgorithm = mock(EncryptAlgorithm.class); when(encryptAlgorithm.decrypt("VALUE")).thenReturn("ORIGINAL_VALUE"); when(metaData.findEncryptor(1)).thenReturn(Optional.of(encryptAlgorithm)); assertThat(new EncryptMergedResult(metaData, mergedResult, true).getValue(1, String.class), is("ORIGINAL_VALUE")); }
@Test public void assertGetValueWithQueryWithCipherColumnAndMatchedEncryptorWithNullCiphertext() throws SQLException { EncryptAlgorithm encryptAlgorithm = mock(EncryptAlgorithm.class); when(metaData.findEncryptor(1)).thenReturn(Optional.of(encryptAlgorithm)); assertNull(new EncryptMergedResult(metaData, mergedResult, true).getValue(1, String.class)); }