Skip to content

Commit

Permalink
avniproject/avni-security#16 | Add basic tests for EnhancedValidation…
Browse files Browse the repository at this point in the history
…Service
  • Loading branch information
1t5j0y committed Sep 18, 2023
1 parent 23ac585 commit 60ba48f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 60ba48f

Please sign in to comment.