Skip to content

Commit

Permalink
fix: added more importModes to program rule variable names validator …
Browse files Browse the repository at this point in the history
…[DHIS2-11273] (dhis2#8155)

* fix: added more importModes to program rule variable names validator

* tests: added a specific test case

* style: fix sonarcloud warning

* style: changed List for Set for better performances
  • Loading branch information
gnespolino authored Jun 9, 2021
1 parent 8303051 commit a752e32
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import static org.hisp.dhis.dxf2.Constants.PROGRAM_RULE_VARIABLE_NAME_INVALID_KEYWORDS;

import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.IntStream;

Expand All @@ -44,6 +45,7 @@
import org.springframework.stereotype.Component;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

/**
* @author Zubair Asghar.
Expand All @@ -60,6 +62,12 @@ public class ProgramRuleVariableObjectBundleHook extends AbstractObjectBundleHoo
.put( ProgramRuleVariableSourceType.TEI_ATTRIBUTE, this::processTEA )
.build();

private static final Set<ImportStrategy> UPDATE_STRATEGIES = ImmutableSet.of(
ImportStrategy.UPDATE,
ImportStrategy.CREATE_AND_UPDATE,
ImportStrategy.NEW_AND_UPDATES,
ImportStrategy.UPDATES );

private static final String FROM_PROGRAM_RULE_VARIABLE = " from ProgramRuleVariable prv where prv.name = :name and prv.program.uid = :programUid";

private final String PROGRAM_RULE_VARIABLE_NAME_INVALID_KEYWORDS_REGEX;
Expand Down Expand Up @@ -88,7 +96,7 @@ private void validateUniqueProgramRuleName( ObjectBundle bundle,
{
Query<ProgramRuleVariable> query = getProgramRuleVariableQuery( programRuleVariable );

int allowedCount = bundle.getImportMode() == ImportStrategy.UPDATE ? 1 : 0;
int allowedCount = UPDATE_STRATEGIES.contains( bundle.getImportMode() ) ? 1 : 0;

if ( query.getResultList().size() > allowedCount )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ public void shouldFailValidationInvalidCount()
assertTrue( errorReports.stream().anyMatch( e -> e.getErrorCode().equals( E4051 ) ) );
}

@Test
public void shouldNotFailValidationInvalidCount()
{
when( programRuleVariable.getProgram() ).thenReturn( program );
when( objectBundle.getImportMode() ).thenReturn( ImportStrategy.CREATE_AND_UPDATE );
when( query.getResultList() ).thenReturn( Collections.singletonList( new ProgramRuleVariable() ) );

when( programRuleVariable.getName() ).thenReturn( "word" );
List<ErrorReport> errorReports = programRuleVariableObjectBundleHook.validate( programRuleVariable,
objectBundle );
assertEquals( 0, errorReports.size() );
}

@Test
public void shouldFailValidationInvalidCountAndInvalidName()
{
Expand Down

0 comments on commit a752e32

Please sign in to comment.