Skip to content

Commit

Permalink
improve error message when form is not found (#1449)
Browse files Browse the repository at this point in the history
* improve error message when form is not found

* rename method

---------

Co-authored-by: Floris Thijssen <[email protected]>
Co-authored-by: valtimo-platform[bot] <80107705+valtimo-platform[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 8, 2024
1 parent 41f4b00 commit e30809b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<NoSuchElementException> {
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
Expand Down

0 comments on commit e30809b

Please sign in to comment.