Last active
March 8, 2016 13:40
-
-
Save tqvarnst/f16cb4acc203d1837a16 to your computer and use it in GitHub Desktop.
Arquillian test case for AttributeConverter
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 AttributeConverterTest { | |
@EJB | |
PersonService personService; | |
@Deployment | |
public static Archive<?> createDeployment() { | |
JavaArchive archive = ShrinkWrap.create(JavaArchive.class) | |
.addAsResource("META-INF/persistence.xml","META-INF/persistence.xml") | |
.addPackages(true,"com.redhat.demos") | |
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); | |
return archive; | |
} | |
@Test | |
@UsingDataSet("person.yml") | |
public void testPasswordConverter() { | |
Person person = personService.getPerson(1001L); | |
assertEquals("John", person.getFirstName()); | |
assertEquals("1234567890", person.getPassword()); //This value is converted from MTIzNDU2Nzg5MA== which is stored in the DB | |
} | |
} |
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 PasswordConverter implements AttributeConverter<String, String> { | |
@Override | |
public String convertToDatabaseColumn(String arg0) { | |
if(arg0!=null) { | |
return Base64.getEncoder().encodeToString(arg0.getBytes()); | |
} else { | |
return null; | |
} | |
} | |
@Override | |
public String convertToEntityAttribute(String arg0) { | |
if(arg0!=null) { | |
return new String(Base64.getDecoder().decode(arg0)); | |
} else { | |
return null; | |
} | |
} | |
} |
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
@Entity(name="person") | |
@Table(name="PERSON") | |
public class Person { | |
//Additional Columns removed for clarity | |
@Column | |
@Convert(converter=PasswordConverter.class) | |
String password; | |
public String getPassword() { | |
return password; | |
} | |
public void setPassword(String password) { | |
this.password = password; | |
} | |
//Methods removed for clarity | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment