-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Unit test ScriptAssert validator
PostPosted: Mon Aug 15, 2011 7:12 am 
Newbie

Joined: Mon Jul 25, 2011 11:55 pm
Posts: 7
hi,

we have been using HV for a couple of weeks now and everything works nicely. We are using Spring/JSF/Eclipselink.

For class level constraints we have a mixture of ScriptAssert and custom validators. Some of the custom validators have DAO beans injected to perform lookups. We can unit test the custom validators in isolation and mock out the dao bean. We have hit a roadblock though when going to test the ScriptAssert annotations as we cannot seem to isolate them like the custom validator. As a result the whole class needs to be validated which then causes problems as the custom validators end up with the DAO beans being set to null. As a result a NPE is thrown. I have looked at the HV API and while I can get descriptors for the ScriptAssert I cannot get the ScriptAssert Validator. Is this possible?

Do not want to throw away the ScriptAssert annotations as they are easier and more convenient then custom validators.

cheers,

Ralph


Top
 Profile  
 
 Post subject: Re: Unit test ScriptAssert validator
PostPosted: Mon Aug 15, 2011 7:57 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

can you describe your problem in more detail? Some actual code examples would be great.
Quote:
For class level constraints we have a mixture of ScriptAssert and custom validators. Some of the custom validators have DAO beans injected to perform lookups. We can unit test the custom validators in isolation and mock out the dao bean. We have hit a roadblock though when going to test the ScriptAssert annotations as we cannot seem to isolate them like the custom validator.

What is the problem? How and what do you try to test in the first place?

Quote:
I have looked at the HV API and while I can get descriptors for the ScriptAssert I cannot get the ScriptAssert Validator. Is this possible?

I am not sure I understand what you mean and you seem to mix up descriptors with ContstraintValidator instances. org.hibernate.validator.constraints.impl.ScriptAssertValidator is a public class. Not sure what you mean that you cannot get it.

--Hardy


Top
 Profile  
 
 Post subject: Re: Unit test ScriptAssert validator
PostPosted: Tue Aug 16, 2011 1:33 am 
Newbie

Joined: Mon Jul 25, 2011 11:55 pm
Posts: 7
hi Hardy,

I can easily test the custom validators. Code example.

Code:
      ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
      ActProvisionBeforeOutcomeValidator actProvisionBeforeOutcomeValidator =
            factory.getConstraintValidatorFactory().getInstance(ActProvisionBeforeOutcomeValidator.class);
      actProvisionBeforeOutcomeValidator.setReviewEventTypeDao(reviewEventTypeDao); // dao classes mocked
      actProvisionBeforeOutcomeValidator.setReviewOutcomeDao(reviewOutcomeDao);
      boolean isValid = actProvisionBeforeOutcomeValidator.isValid(event, null);


Now I need want to test the @ScriptAssert on my entity class in isolation from the custom validators.

Code:
   
@ScriptAssert.List({
           @ScriptAssert(
            lang = "javascript",
            script = "_this.eventCompleted == 'Y' ? _this.dateEventCompleted != null : true",
            message = "The date must be entered for a completed event.")
           ... lots more @ScriptAssert follow ...


What is the best way to get a handle on this ScriptAssert so I can unit test it? The below does not work and I would not expect to work as we have a list of script asserts.

Code:
      ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
      ScriptAssertValidator scriptAssertValidator =
            factory.getConstraintValidatorFactory().getInstance(ScriptAssertValidator.class);

      boolean isValid = scriptAssertValidator.isValid(event, null);


thanks,

Ralph


Top
 Profile  
 
 Post subject: Re: Unit test ScriptAssert validator
PostPosted: Tue Aug 16, 2011 3:24 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi Ralph,

I think I see now what your problem is. You need to initialize your ConstraintValidator and this is only possible with a @ScriptAssert annnotation. The way we handle this in our unit test is to proxy annotations see ScriptAssertValidatorTest and AnnotationFactory.

Using the annotation factory you can mock/proxy the @ScriptAssertAnnotation and test the ScriptAssertValidator directly by passing the proxy to initialize and then call isValid.

Is this what you are after?


Top
 Profile  
 
 Post subject: Re: Unit test ScriptAssert validator
PostPosted: Tue Aug 16, 2011 3:26 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Btw, your code does not work, because you are skipping the initialization, unless you are skipping some code.


Top
 Profile  
 
 Post subject: Re: Unit test ScriptAssert validator
PostPosted: Fri Aug 19, 2011 2:12 am 
Newbie

Joined: Mon Jul 25, 2011 11:55 pm
Posts: 7
thanks Hardy,

that got me across the line. I have added a utility method which calls out to the methods you provided in the link. Have pasted the utility method here in case it provides helpful for anyone else.

Code:
   /**
    * Run all @ScriptAssert validations against the given class.
    *
    * @param pClass
    *            Class of pObject
    * @param pObject
    *            Instance of pClass
    * @return A list of messages where the script assert validation returned
    *         false
    */
   public static List<String> validateScriptAssertionsForClass(final Class<?> pClass, final Object pObject) {

      Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
      BeanDescriptor beanDescriptor = validator.getConstraintsForClass(pClass);
   
      Set<ConstraintDescriptor<?>> constraintDescriptors = beanDescriptor.findConstraints().lookingAt(Scope.LOCAL_ELEMENT).getConstraintDescriptors();

      List<String> msgs = new ArrayList<String>();

      for (ConstraintDescriptor<?> descriptor : constraintDescriptors) {
         if (descriptor.getAnnotation().annotationType() == ScriptAssert.class) {
            Map<String, Object> attributeMap = descriptor.getAttributes();
               
            String script = String.valueOf(attributeMap.get("script"));
            String lang = String.valueOf(attributeMap.get("lang"));
            String msg = String.valueOf(attributeMap.get("message"));
   
            ConstraintValidator<ScriptAssert, Object> scriptAssertValidator = getInitializedValidator(lang, script);
            
            if (!scriptAssertValidator.isValid(pObject, null)) {
               msgs.add(msg);
            }
   
         }
      }
      return msgs;
   }


thanks again,

Ralph


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.