Skip to content

Commit

Permalink
refactor option and dataItem to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
enricocolasante committed Dec 6, 2023
1 parent 177bba6 commit 903aa74
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.hisp.dhis.rules;
package org.hisp.dhis.rules

/*
* Copyright (c) 2004-2020, University of Oslo
Expand Down Expand Up @@ -32,33 +32,4 @@
* Class is place holder for program rule variable, Constant and program environment variable display name and type.
* @author Zubair Asghar
*/
public record DataItem(String displayName, ItemValueType valueType) {

public static Builder builder()
{
return new Builder();
}

public static class Builder
{
private String displayName;
private ItemValueType itemValueType;

public Builder value( String value )
{
this.displayName = value;
return this;
}

public Builder valueType( ItemValueType valueType )
{
this.itemValueType = valueType;
return this;
}

public DataItem build()
{
return new DataItem( displayName, itemValueType );
}
}
}
data class DataItem(val displayName: String, val valueType: ItemValueType)
9 changes: 0 additions & 9 deletions src/main/java/org/hisp/dhis/rules/Option.java

This file was deleted.

6 changes: 6 additions & 0 deletions src/main/java/org/hisp/dhis/rules/Option.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.hisp.dhis.rules

/**
* @author rajazubair
*/
data class Option(val name: String, val code: String)
4 changes: 2 additions & 2 deletions src/main/java/org/hisp/dhis/rules/models/RuleVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ default String getOptionName( String value )
return value;
}
return options().stream()
.filter( op -> Objects.equals( value, op.code() ) )
.map( Option::name)
.filter( op -> Objects.equals( value, op.getCode() ) )
.map( Option::getName)
.findAny()
.orElse( value );
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/hisp/dhis/rules/RuleEngineFunctionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ public void optionSetNameShouldBeUsed()

RuleEvent ruleEvent = RuleEvent.create( "test_event", "test_program_stage",
RuleEvent.Status.ACTIVE, new Date(), new Date(), "", null, List.of(RuleDataValue.create(
new Date(), "test_program_stage", "test_data_element", option1.code())), "", null);
new Date(), "test_program_stage", "test_data_element", option1.getCode())), "", null);
List<RuleEffect> ruleEffects = ruleEngine.evaluate( ruleEvent ).call();

assertEquals( 1, ruleEffects.size() );
assertEquals( option1.name() , ruleEffects.get( 0 ).data() );
assertEquals( option1.getName() , ruleEffects.get( 0 ).data() );
assertEquals( ruleAction , ruleEffects.get( 0 ).ruleAction() );
}

Expand All @@ -185,11 +185,11 @@ public void optionSetCodeShouldBeUsed()

RuleEvent ruleEvent = RuleEvent.create( "test_event", "test_program_stage",
RuleEvent.Status.ACTIVE, new Date(), new Date(), "", null, List.of(RuleDataValue.create(
new Date(), "test_program_stage", "test_data_element", option2.code())), "", null);
new Date(), "test_program_stage", "test_data_element", option2.getCode())), "", null);
List<RuleEffect> ruleEffects = ruleEngine.evaluate( ruleEvent ).call();

assertEquals(1, ruleEffects.size() );
assertEquals( option2.code() , ruleEffects.get( 0 ).data() );
assertEquals( option2.getCode() , ruleEffects.get( 0 ).data() );
assertEquals( ruleAction , ruleEffects.get( 0 ).ruleAction() );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ public void setUp()
{
itemStore = new HashMap<>();

DataItem var_1 = DataItem.builder().value( test_var_one ).valueType( ItemValueType.TEXT ).build();
DataItem var_2 = DataItem.builder().value( test_var_two ).valueType( ItemValueType.TEXT ).build();
DataItem var_8 = DataItem.builder().value( test_var_three ).valueType( ItemValueType.TEXT ).build();
DataItem var_3 = DataItem.builder().value( test_var_date_one ).valueType( ItemValueType.DATE ).build();
DataItem var_4 = DataItem.builder().value( test_var_date_two ).valueType( ItemValueType.DATE ).build();
DataItem var_5 = DataItem.builder().value( completionDate ).valueType( ItemValueType.DATE ).build();
DataItem var_6 = DataItem.builder().value( constant ).valueType( ItemValueType.TEXT ).build();
DataItem var_7 = DataItem.builder().value( currentDate ).valueType( ItemValueType.DATE ).build();
DataItem var_9 = DataItem.builder().value( test_var_number ).valueType( ItemValueType.NUMBER ).build();
DataItem var_1 = new DataItem( test_var_one, ItemValueType.TEXT);
DataItem var_2 = new DataItem( test_var_two, ItemValueType.TEXT );
DataItem var_8 = new DataItem( test_var_three, ItemValueType.TEXT );
DataItem var_3 = new DataItem( test_var_date_one, ItemValueType.DATE );
DataItem var_4 = new DataItem( test_var_date_two, ItemValueType.DATE );
DataItem var_5 = new DataItem( completionDate, ItemValueType.DATE );
DataItem var_6 = new DataItem( constant, ItemValueType.TEXT );
DataItem var_7 = new DataItem( currentDate, ItemValueType.DATE );
DataItem var_9 = new DataItem( test_var_number, ItemValueType.NUMBER );

itemStore.put( "test_var_one", var_1 );
itemStore.put( "test_var_two", var_2 );
Expand Down

0 comments on commit 903aa74

Please sign in to comment.