Skip to content

Instantly share code, notes, and snippets.

@thjanssen
Created April 19, 2015 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thjanssen/9a1628c80531d4100796 to your computer and use it in GitHub Desktop.
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)
CreditCard:
- id: 1
name: My Name
ccNumber: egFfkhd8cRh82tvsh3VVUg==
@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);
}
}
}
<?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>
@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");
}
@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);
}
@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