From e30809b37e60293a6cb6517db92189c33818fa34 Mon Sep 17 00:00:00 2001 From: Floris Thijssen Date: Thu, 8 Aug 2024 15:34:07 +0200 Subject: [PATCH] improve error message when form is not found (#1449) * improve error message when form is not found * rename method --------- Co-authored-by: Floris Thijssen Co-authored-by: valtimo-platform[bot] <80107705+valtimo-platform[bot]@users.noreply.github.com> --- .../validation/OnStartUpViewModelValidator.kt | 73 ++++++++++--------- .../OnStartUpViewModelValidatorTest.kt | 15 ++++ 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/form-view-model/src/main/kotlin/com/ritense/formviewmodel/validation/OnStartUpViewModelValidator.kt b/form-view-model/src/main/kotlin/com/ritense/formviewmodel/validation/OnStartUpViewModelValidator.kt index caa6c32ada..821acc5155 100644 --- a/form-view-model/src/main/kotlin/com/ritense/formviewmodel/validation/OnStartUpViewModelValidator.kt +++ b/form-view-model/src/main/kotlin/com/ritense/formviewmodel/validation/OnStartUpViewModelValidator.kt @@ -40,45 +40,52 @@ class OnStartUpViewModelValidator( @EventListener(ApplicationReadyEvent::class) fun validate() { for (viewModelLoader in viewModelLoaders) { - val formDefinition = - formIoFormDefinitionService.getFormDefinitionByName(viewModelLoader.getFormName()).get() + validateViewModelLoader(viewModelLoader) + } + } - // Note: Forms added via console get a warning notice. - if (!formDefinition.isReadOnly) { - logger.warn { - "This form (${viewModelLoader.getFormName()}) is not read-only. This means that the form definition is not added via configuration." + - "Be cautious when changing the form definition because this is only validated on ApplicationReadyEvent" - } + fun validateViewModelLoader( + viewModelLoader: ViewModelLoader<*> + ) { + val formDefinition = + formIoFormDefinitionService.getFormDefinitionByName(viewModelLoader.getFormName()) + .orElseThrow{ NoSuchElementException("Could not find form [${viewModelLoader.getFormName()}] declared in ${viewModelLoader.javaClass}") } + + // Note: Forms added via console get a warning notice. + if (!formDefinition.isReadOnly) { + logger.warn { + "This form (${viewModelLoader.getFormName()}) is not read-only. This means that the form definition is not added via configuration." + + "Be cautious when changing the form definition because this is only validated on ApplicationReadyEvent" } - validateViewModel(viewModelLoader, formDefinition).let { missingProperties -> - if (missingProperties.isNotEmpty()) { - logger.error { - "The following properties are missing in the view model for form " + - "(${viewModelLoader.getFormName()}): $missingProperties" - } - // Validate Start form submission for the view model - formViewModelStartFormSubmissionHandlerFactory.getHandler( - viewModelLoader.getFormName() - )?.let { - validateStartFormSubmission(it, formDefinition).let { missingSubmissionProperties -> - if (missingSubmissionProperties.isNotEmpty()) { - logger.error { - "The following properties are missing in the start form submission for form " + - "(${viewModelLoader.getFormName()}): $missingSubmissionProperties" - } + } + validateViewModel(viewModelLoader, formDefinition).let { missingProperties -> + if (missingProperties.isNotEmpty()) { + logger.error { + "The following properties are missing in the view model for form " + + "(${viewModelLoader.getFormName()}): $missingProperties" + } + // Validate Start form submission for the view model + formViewModelStartFormSubmissionHandlerFactory.getHandler( + viewModelLoader.getFormName() + )?.let { + validateStartFormSubmission(it, formDefinition).let { missingSubmissionProperties -> + if (missingSubmissionProperties.isNotEmpty()) { + logger.error { + "The following properties are missing in the start form submission for form " + + "(${viewModelLoader.getFormName()}): $missingSubmissionProperties" } } } + } - formViewModelUserTaskSubmissionHandlerFactory.getHandler( - viewModelLoader.getFormName() - )?.let { - validateUserTaskSubmission(it, formDefinition).let { missingSubmissionProperties -> - if (missingSubmissionProperties.isNotEmpty()) { - logger.error { - "The following properties are missing in the user task submission for form " + - "(${viewModelLoader.getFormName()}): $missingSubmissionProperties" - } + formViewModelUserTaskSubmissionHandlerFactory.getHandler( + viewModelLoader.getFormName() + )?.let { + validateUserTaskSubmission(it, formDefinition).let { missingSubmissionProperties -> + if (missingSubmissionProperties.isNotEmpty()) { + logger.error { + "The following properties are missing in the user task submission for form " + + "(${viewModelLoader.getFormName()}): $missingSubmissionProperties" } } } diff --git a/form-view-model/src/test/kotlin/com/ritense/formviewmodel/validation/OnStartUpViewModelValidatorTest.kt b/form-view-model/src/test/kotlin/com/ritense/formviewmodel/validation/OnStartUpViewModelValidatorTest.kt index 40a7dc6a4d..fa0474c92b 100644 --- a/form-view-model/src/test/kotlin/com/ritense/formviewmodel/validation/OnStartUpViewModelValidatorTest.kt +++ b/form-view-model/src/test/kotlin/com/ritense/formviewmodel/validation/OnStartUpViewModelValidatorTest.kt @@ -13,6 +13,8 @@ import com.ritense.formviewmodel.viewmodel.ViewModelLoader import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import org.mockito.Mockito import org.mockito.Mockito.mock import org.mockito.kotlin.doReturn import org.mockito.kotlin.mock @@ -88,6 +90,19 @@ class OnStartUpViewModelValidatorTest : BaseTest() { assertThat(missingFields).contains("age") } + @Test + fun `should throw exception when form could not be found`() { + val viewModelLoader: ViewModelLoader<*> = Mockito.mock() + whenever(viewModelLoader.getFormName()).thenReturn("I do not exist") + + val exception = assertThrows { + onStartUpViewModelValidator.validateViewModelLoader( + viewModelLoader + ) + } + assertThat(exception.message).contains("Could not find form [I do not exist] declared in class com.ritense.formviewmodel.viewmodel.ViewModelLoader\$MockitoMock") + } + @Test fun `should log validation errors to stdout`() { // Redirect System.err to capture what is printed