Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test - Mobel validator bean not called #46

Open
mchavaillaz opened this issue Jun 20, 2018 · 0 comments
Open

Unit test - Mobel validator bean not called #46

mchavaillaz opened this issue Jun 20, 2018 · 0 comments

Comments

@mchavaillaz
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant