Created
April 19, 2015 03:43
-
-
Save thjanssen/9a1628c80531d4100796 to your computer and use it in GitHub Desktop.
Testing with Aliens: How to test a JPA type converter with Arquillian (http://www.thoughts-on-java.org/2014/06/testing-with-aliens-how-to-test-jpa.html)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CreditCard: | |
- id: 1 | |
name: My Name | |
ccNumber: egFfkhd8cRh82tvsh3VVUg== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Converter | |
public class CryptoConverter implements AttributeConverter<String, String> { | |
private static final String ALGORITHM = "AES/ECB/PKCS5Padding"; | |
private static final byte[] KEY = "MySuperSecretKey".getBytes(); | |
@Override | |
public String convertToDatabaseColumn(String ccNumber) { | |
// do some encryption | |
Key key = new SecretKeySpec(KEY, "AES"); | |
try { | |
Cipher c = Cipher.getInstance(ALGORITHM); | |
c.init(Cipher.ENCRYPT_MODE, key); | |
return Base64.encodeBytes(c.doFinal(ccNumber.getBytes())); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
@Override | |
public String convertToEntityAttribute(String dbData) { | |
// do some decryption | |
Key key = new SecretKeySpec(KEY, "AES"); | |
try { | |
Cipher c = Cipher.getInstance(ALGORITHM); | |
c.init(Cipher.DECRYPT_MODE, key); | |
return new String(c.doFinal(Base64.decode(dbData))); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | |
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |
... | |
<properties> | |
<version.junit>4.11</version.junit> | |
<version.arquillian>1.1.3.Final</version.arquillian> | |
<version.arquillian_persistence>1.0.0.Alpha7</version.arquillian_persistence> | |
<version.wildfly>8.1.0.Final</version.wildfly> | |
</properties> | |
<dependencyManagement> | |
<dependencies> | |
... | |
<dependency> | |
<groupId>org.jboss.arquillian</groupId> | |
<artifactId>arquillian-bom</artifactId> | |
<version>${version.arquillian}</version> | |
<type>pom</type> | |
<scope>import</scope> | |
</dependency> | |
</dependencies> | |
</dependencyManagement> | |
<dependencies> | |
... | |
<dependency> | |
<groupId>javax.enterprise</groupId> | |
<artifactId>cdi-api</artifactId> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>${version.junit}</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.jboss.arquillian.junit</groupId> | |
<artifactId>arquillian-junit-container</artifactId> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.jboss.arquillian.extension</groupId> | |
<artifactId>arquillian-persistence-dbunit</artifactId> | |
<version>${version.arquillian_persistence}</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(Arquillian.class) | |
public class TestCryptoConverter { | |
@Deployment | |
public static JavaArchive createDeployment() { | |
return ShrinkWrap | |
.create(JavaArchive.class) | |
.addClasses(CreditCard.class, CryptoConverter.class, | |
TestCryptoConverter.class) | |
.addAsManifestResource("META-INF/persistence.xml", | |
"persistence.xml") | |
.addAsManifestResource("META-INF/orm.xml", "orm.xml") | |
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@PersistenceContext | |
private EntityManager em; | |
@Test | |
@ShouldMatchDataSet(value = "data/cc.yml", excludeColumns = "id") | |
public void testEncryption() { | |
CreditCard cc = new CreditCard(); | |
cc.setName("My Name"); | |
cc.setCcNumber("123456789"); | |
this.em.persist(cc); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
@UsingDataSet("data/cc.yml") | |
public void testRead() { | |
CreditCard cc = this.em | |
.createNamedQuery(CreditCard.BY_NUMBER, CreditCard.class) | |
.setParameter("number", "123456789").getSingleResult(); | |
Assert.assertEquals("My Name", cc.getName()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment