spring-webflow-samples icon indicating copy to clipboard operation
spring-webflow-samples copied to clipboard

Unit test - Mobel validator bean not called

Open mchavaillaz opened this issue 7 years ago • 0 comments

Hi

I'm writing flow execution tests

Why is my model validator spring bean not called when I trigger a transition?

First here is my flow configuration:

<on-start>
	<!-- Build the form used by the view states -->
	<evaluate expression="grantPHRAccessFormFactory.getForm(externalContext.sessionMap.get('currentPerson'))" result="flowScope.grantPHRAccessForm"/>
</on-start>

<view-state id="enterEmail" view="grant-phr-access/enter-email" model="grantPHRAccessForm">
	<transition on="next" to="checkIfIsEmailForNewUserKnown" validate="true"/>
	<transition on="cancel" to="cancel"/>
</view-state>

<decision-state id="checkIfIsEmailForNewUserKnown">
	<if test="grantPHRAccessForm.isEmailForNewUserKnown()" then="checkIfUserAlreadyExistForEmail" else="createNewUser"/>
</decision-state>

<decision-state id="checkIfUserAlreadyExistForEmail">
	<if test="userServiceImpl.findByEmailNotDeleted(grantPHRAccessForm.emailForNewUser).size() > 0" then="grantAccessToExistingPersonAccount" else="createNewUser"/>
</decision-state>

<view-state id="grantAccessToExistingPersonAccount" view="grant-phr-access/existing-person-account">
	<transition on="back" to="enterEmail"/>
	<transition on="next" to="doYouWantToGenerateNewPassword"/>
	<transition on="cancel" to="cancel"/>
</view-state>

<view-state id="createNewUser" view="grant-phr-access/create-new-user" model="grantPHRAccessForm">
	<transition on="back" to="enterEmail"/>
	<transition on="next" to="communicateCredentials"/>
	<transition on="cancel" to="cancel" validate="false"/>
</view-state>

Here is my flow execution test with its configuration:

@Autowired
private GrantPHRAccessFormValidator grantPHRAccessFormValidator;

@Override
protected FlowDefinitionResource getResource(FlowDefinitionResourceFactory flowDefinitionResourceFactory) {
     return flowDefinitionResourceFactory.createFileResource("src/main/webapp/WEB-INF/flows/grant-phr-access.xml");
}

@Override
protected void configureFlowBuilderContext(MockFlowBuilderContext builderContext) {
     builderContext.registerBean("grantPHRAccessFormValidator", grantPHRAccessFormValidator);
     builderContext.registerBean("GrantPHRAccessFormValidator", grantPHRAccessFormValidator);
}

@Test
public void transitionFromEmailToCreateNewPersonAccountTest() {
     // Tool
     final GrantPHRAccessForm grantPHRAccessForm = GrantPHRAccessForm.builder() //
          .isEmailForNewUserKnown(false) //
	  .build();
     // Configure data for this test
     context.getSessionMap().put("currentPerson", new Person());

    when(grantPHRAccessFormFactory.getForm(any(Person.class))).thenReturn(grantPHRAccessForm);

     // Start the flow, trigger event and check if we went through the correct states in the order expected
     startFlow(context);
     assertCurrentStateEquals("enterEmail");
     context.setEventId("next");
     resumeFlow(context);
     assertCurrentStateEquals("createNewUser");
     verify(grantPHRAccessFormValidator, times(1)).validateEnterEmail(grantPHRAccessForm, any(ValidationContext.class));
}

And here is my model validator bean:

@Component
public class GrantPHRAccessFormValidator {

	@Autowired
	private MessageSource messageSource;

	public void validateEnterEmail(GrantPHRAccessForm grantPHRAccessForm, ValidationContext validationContext) {
		// Tools
		final MessageContext messageContext = validationContext.getMessageContext();
		final Locale locale = LocaleContextHolder.getLocale();
		final String code = "registration.error.email.invalid.format";
		final Object[] params = null;

		if (grantPHRAccessForm.isEmailForNewUserKnown()) {
			if (!Utilities.isValidEmail(grantPHRAccessForm.getEmailForNewUser(), false)) {
				messageContext.addMessage( //
					new MessageBuilder() //
						.error() //
						.source("emailForNewUser") //
                                                .defaultText(messageSource.getMessage(code, params, locale)) //
						.build());
			}
		}
	}
}

My code goes through the states I expected but the "grantPHRAccessFormValidator.validateEnterEmail()" method is not triggered.

Do I have to simulate somehow a "submit form" instead of the "context.setEventId("next");" Or do I have to register the "grantPHRAccessFormValidator" bean in another way?

Thank you for you support.

Best regards

mchavaillaz avatar Jun 20 '18 15:06 mchavaillaz