From 60ba48fb905b5a93fde35f69c84827d926ce0aa5 Mon Sep 17 00:00:00 2001 From: Joy A Date: Mon, 18 Sep 2023 10:27:12 +0530 Subject: [PATCH] avniproject/avni-security#16 | Add basic tests for EnhancedValidationService --- .../service/EnhancedValidationService.java | 2 +- .../EnhancedValidationServiceTest.java | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 avni-server-api/src/test/java/org/avni/server/service/EnhancedValidationServiceTest.java diff --git a/avni-server-api/src/main/java/org/avni/server/service/EnhancedValidationService.java b/avni-server-api/src/main/java/org/avni/server/service/EnhancedValidationService.java index c29cbc398..389edf281 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/EnhancedValidationService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/EnhancedValidationService.java @@ -84,7 +84,7 @@ public ValidationResult validateObservationsAndDecisionsAgainstFormMapping(List< return ValidationResult.Success; } - private ValidationResult handleValidationFailure(String errorMessage) { + public ValidationResult handleValidationFailure(String errorMessage) { logger.error(String.format("ValidationError: %s", errorMessage)); ValidationException validationException = new ValidationException(errorMessage); bugsnag.notify(validationException); diff --git a/avni-server-api/src/test/java/org/avni/server/service/EnhancedValidationServiceTest.java b/avni-server-api/src/test/java/org/avni/server/service/EnhancedValidationServiceTest.java new file mode 100644 index 000000000..ae6bf2974 --- /dev/null +++ b/avni-server-api/src/test/java/org/avni/server/service/EnhancedValidationServiceTest.java @@ -0,0 +1,56 @@ +package org.avni.server.service; + +import com.bugsnag.Bugsnag; +import org.avni.server.common.ValidationResult; +import org.avni.server.dao.AddressLevelTypeRepository; +import org.avni.server.dao.ConceptRepository; +import org.avni.server.dao.IndividualRepository; +import org.avni.server.dao.SubjectTypeRepository; +import org.avni.server.web.validation.ValidationException; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; + +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +public class EnhancedValidationServiceTest { + @Mock + private FormMappingService formMappingService; + @Mock + private OrganisationConfigService organisationConfigService; + @Mock + private Bugsnag bugsnag; + @Mock + private ConceptRepository conceptRepository; + @Mock + private SubjectTypeRepository subjectTypeRepository; + @Mock + private IndividualRepository individualRepository; + @Mock + private AddressLevelTypeRepository addressLevelTypeRepository; + + private EnhancedValidationService enhancedValidationService; + + @Before + public void setup() { + initMocks(this); + enhancedValidationService = new EnhancedValidationService(formMappingService, organisationConfigService, bugsnag, conceptRepository, subjectTypeRepository, individualRepository, addressLevelTypeRepository); + } + + @Test(expected = ValidationException.class) + public void shouldThrowValidationExceptionForInvalidDataIfFailOnValidationIsEnabled() { + when(organisationConfigService.isFailOnValidationErrorEnabled()).thenReturn(true); + String errorMessage = "Dummy Error Message"; + enhancedValidationService.handleValidationFailure(errorMessage); + } + + @Test + public void shouldReturnValidationFailureForInvalidDataIfFailOnValidationIsDisabled() { + when(organisationConfigService.isFailOnValidationErrorEnabled()).thenReturn(false); + String errorMessage = "Dummy Error Message"; + ValidationResult validationResult = enhancedValidationService.handleValidationFailure(errorMessage); + assertTrue(validationResult.isFailure()); + } +}