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

improve error message when form is not found #1449

Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
2a23c6d
improve error message when form is not found
floris-thijssen-ritense Jun 28, 2024
5f61264
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 1, 2024
30a164c
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 3, 2024
ffbe84c
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 4, 2024
e36ac02
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 10, 2024
8e680a3
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 11, 2024
a13630a
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 15, 2024
16ede6e
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 15, 2024
51b8154
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 16, 2024
0440632
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 16, 2024
fd999b2
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 17, 2024
5b5c70d
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 17, 2024
3e25ca2
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 18, 2024
6d449dc
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 22, 2024
22fcb3d
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 22, 2024
dd5fbf1
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 22, 2024
2a05248
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 24, 2024
4d7eea4
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 25, 2024
0bd565c
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 29, 2024
341e06c
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 29, 2024
f8866be
rename method
floris-thijssen-ritense Jul 30, 2024
699bc03
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Jul 31, 2024
4b98986
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Aug 1, 2024
4cae0f4
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Aug 1, 2024
3119a09
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Aug 2, 2024
401b73c
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Aug 5, 2024
e068740
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Aug 5, 2024
743c44d
Merge branch 'next-minor' into story/improve-error-message-when-no-vi…
valtimo-platform[bot] Aug 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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