Permalink
Browse files

[9] Add Graphene WebFragments and CDI Test Producer to bypass the Dat…

…abase layer in web component tests
  • Loading branch information...
1 parent acda558 commit db849028d7e5622c4666816272956e9f577fdfe2 @aslakknutsen aslakknutsen committed with ALRubinger Oct 3, 2012
@@ -19,68 +19,71 @@
@Entity
public class Conference implements Identifiable, Serializable {
- private static final long serialVersionUID = 1L;
-
- @Id
- private String id;
-
- @NotNull
- private String name;
-
- private String tagLine;
-
- @Embedded @Valid @NotNull
- private Duration duration;
-
- @OneToMany(fetch = FetchType.EAGER, orphanRemoval = true) @Valid
- private Set<Session> sessions;
-
- public Conference() {
- this.id = UUID.randomUUID().toString();
- }
-
- public String getId() {
- return id;
- }
-
- public String getName() {
- return name;
- }
-
- public Conference setName(String name) {
- this.name = name;
- return this;
- }
-
- public String getTagLine() {
- return tagLine;
- }
-
- public Conference setTagLine(String tagLine) {
- this.tagLine = tagLine;
- return this;
- }
-
- public Conference setDuration(Duration duration) {
- this.duration = duration;
- return this;
- }
-
- public Duration getDuration() {
- return duration;
- }
-
- public Set<Session> getSessions() {
- return Collections.unmodifiableSet(sessions);
- }
-
- public Conference addSession(Session session) {
- if(sessions == null) {
- this.sessions = new HashSet<Session>();
+ private static final long serialVersionUID = 1L;
+
+ @Id
+ private String id;
+
+ @NotNull
+ private String name;
+
+ private String tagLine;
+
+ @Embedded
+ @Valid
+ @NotNull
+ private Duration duration;
+
+ @OneToMany(fetch = FetchType.EAGER, orphanRemoval = true)
+ @Valid
+ private Set<Session> sessions;
+
+ public Conference() {
+ this.id = UUID.randomUUID().toString();
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public Conference setName(String name) {
+ this.name = name;
+ return this;
}
- if(!sessions.contains(session)) {
- sessions.add(session);
+
+ public String getTagLine() {
+ return tagLine;
+ }
+
+ public Conference setTagLine(String tagLine) {
+ this.tagLine = tagLine;
+ return this;
+ }
+
+ public Conference setDuration(Duration duration) {
+ this.duration = duration;
+ return this;
+ }
+
+ public Duration getDuration() {
+ return duration;
+ }
+
+ public Set<Session> getSessions() {
+ return Collections.unmodifiableSet(sessions);
+ }
+
+ public Conference addSession(Session session) {
+ if (sessions == null) {
+ this.sessions = new HashSet<Session>();
+ }
+ if (!sessions.contains(session)) {
+ sessions.add(session);
+ }
+ return this;
}
- return this;
- }
}
@@ -16,51 +16,52 @@
public class DomainValidationTestCase {
- private Validator validator;
+ private Validator validator;
- @Before
- public void setupValidationFactory() {
- validator = Validation.buildDefaultValidatorFactory().getValidator();
- }
+ @Before
+ public void setupValidationFactory() {
+ validator = Validation.buildDefaultValidatorFactory().getValidator();
+ }
- @Test
- public void shouldNotAllowNullName() {
- Conference conference = new Conference();
- assertValidationConsraint(conference, "NotNull", "name");
- }
+ @Test
+ public void shouldNotAllowNullName() {
+ Conference conference = new Conference();
+ assertValidationConsraint(conference, "NotNull", "name");
+ }
- @Test
- public void shouldNotAllowNullDuration() {
- Conference conference = new Conference();
- assertValidationConsraint(conference, "NotNull", "duration");
- }
+ @Test
+ public void shouldNotAllowNullDuration() {
+ Conference conference = new Conference();
+ assertValidationConsraint(conference, "NotNull", "duration");
+ }
- @Test(expected = IllegalArgumentException.class)
- public void shouldNotAllowEndBeforeStart() {
- new Duration(new Date(), new Date(System.currentTimeMillis()-1000));
- }
-
- private <T> void assertValidationConsraint(T object, String type, String... properties) {
- Set<ConstraintViolation<T>> errors = validator.validate(object);
+ @Test(expected = IllegalArgumentException.class)
+ public void shouldNotAllowEndBeforeStart() {
+ new Duration(new Date(), new Date(System.currentTimeMillis() - 1000));
+ }
- Assert.assertFalse("Expecting vaidation errors", errors.isEmpty());
+ private <T> void assertValidationConsraint(T object, String type,
+ String... properties) {
+ Set<ConstraintViolation<T>> errors = validator.validate(object);
- for (String property : properties) {
- ConstraintViolation<T> validationFound = null;
- for (ConstraintViolation<T> cv : errors) {
- if(property.equals(cv.getPropertyPath().toString())) {
- validationFound = cv;
- break;
+ Assert.assertFalse("Expecting vaidation errors", errors.isEmpty());
+
+ for (String property : properties) {
+ ConstraintViolation<T> validationFound = null;
+ for (ConstraintViolation<T> cv : errors) {
+ if (property.equals(cv.getPropertyPath().toString())) {
+ validationFound = cv;
+ break;
+ }
+ }
+ if (validationFound == null) {
+ String msg = "Expected validation error on property [%s] but non found. Found errors [%s]";
+ Assert.fail(String.format(msg, property, errors));
+ } else if (!validationFound.getMessageTemplate().contains(type)) {
+ String msg = "Expected validation error on property [%s] of type [%s]. Found error [%s]";
+ Assert.fail(String.format(msg, property, type, validationFound));
+ }
}
- }
- if(validationFound == null) {
- String msg = "Expected validation error on property [%s] but non found. Found errors [%s]";
- Assert.fail(String.format(msg, property, errors));
- } else if(!validationFound.getMessageTemplate().contains(type)) {
- String msg = "Expected validation error on property [%s] of type [%s]. Found error [%s]";
- Assert.fail(String.format(msg, property, type, validationFound));
- }
- }
- }
+ }
}
@@ -12,28 +12,28 @@
@Stateless
public class RelationRepository {
- @PersistenceContext
- private EntityManager manager;
-
- public Relation add(Identifiable source, Relation.Type type, Identifiable target) {
- Relation rel = new Relation(source.getId(), target.getId(), type);
- manager.persist(rel);
- return rel;
- }
-
- // TODO: implement as criteria query ?
- public <T extends Identifiable> List<T> findTargets(Identifiable source, Relation.Type type, Class<T> targetType) {
-
- StringBuilder q = new StringBuilder();
- q.append("select t from " + targetType.getSimpleName() + " t , Relation r ");
- q.append("where t.id = r.key.targetId ");
- q.append("and r.key.sourceId = :source ");
- q.append("and r.key.type = :type");
-
- TypedQuery<T> query = manager.createQuery(q.toString(), targetType);
- query.setParameter("source", source.getId());
- query.setParameter("type", type);
-
- return query.getResultList();
- }
+ @PersistenceContext
+ private EntityManager manager;
+
+ public Relation add(Identifiable source, Relation.Type type, Identifiable target) {
+ Relation rel = new Relation(source.getId(), target.getId(), type);
+ manager.persist(rel);
+ return rel;
+ }
+
+ // TODO: implement as criteria query ?
+ public <T extends Identifiable> List<T> findTargets(Identifiable source, Relation.Type type, Class<T> targetType) {
+
+ StringBuilder q = new StringBuilder();
+ q.append("select t from ").append(targetType.getSimpleName()).append(" t , Relation r ");
+ q.append("where t.id = r.key.targetId ");
+ q.append("and r.key.sourceId = :source ");
+ q.append("and r.key.type = :type");
+
+ TypedQuery<T> query = manager.createQuery(q.toString(), targetType);
+ query.setParameter("source", source.getId());
+ query.setParameter("type", type);
+
+ return query.getResultList();
+ }
}
Oops, something went wrong.

0 comments on commit db84902

Please sign in to comment.