From 903aa7407c0792cf9cb3414f20e9da2c521cb742 Mon Sep 17 00:00:00 2001 From: Enrico Date: Wed, 6 Dec 2023 12:02:23 +0100 Subject: [PATCH] refactor option and dataItem to kotlin --- .../dhis/rules/{DataItem.java => DataItem.kt} | 33 ++----------------- src/main/java/org/hisp/dhis/rules/Option.java | 9 ----- src/main/java/org/hisp/dhis/rules/Option.kt | 6 ++++ .../hisp/dhis/rules/models/RuleVariable.java | 4 +-- .../dhis/rules/RuleEngineFunctionTest.java | 8 ++--- .../rules/RuleEngineGetDescriptionTest.java | 18 +++++----- 6 files changed, 23 insertions(+), 55 deletions(-) rename src/main/java/org/hisp/dhis/rules/{DataItem.java => DataItem.kt} (70%) delete mode 100644 src/main/java/org/hisp/dhis/rules/Option.java create mode 100644 src/main/java/org/hisp/dhis/rules/Option.kt diff --git a/src/main/java/org/hisp/dhis/rules/DataItem.java b/src/main/java/org/hisp/dhis/rules/DataItem.kt similarity index 70% rename from src/main/java/org/hisp/dhis/rules/DataItem.java rename to src/main/java/org/hisp/dhis/rules/DataItem.kt index 25a15c2c..b92c948d 100644 --- a/src/main/java/org/hisp/dhis/rules/DataItem.java +++ b/src/main/java/org/hisp/dhis/rules/DataItem.kt @@ -1,4 +1,4 @@ -package org.hisp.dhis.rules; +package org.hisp.dhis.rules /* * Copyright (c) 2004-2020, University of Oslo @@ -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) diff --git a/src/main/java/org/hisp/dhis/rules/Option.java b/src/main/java/org/hisp/dhis/rules/Option.java deleted file mode 100644 index 9a5f68c2..00000000 --- a/src/main/java/org/hisp/dhis/rules/Option.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.hisp.dhis.rules; - - -/** - * @author rajazubair - */ - -public record Option(String name, String code) { -} diff --git a/src/main/java/org/hisp/dhis/rules/Option.kt b/src/main/java/org/hisp/dhis/rules/Option.kt new file mode 100644 index 00000000..7e5a9576 --- /dev/null +++ b/src/main/java/org/hisp/dhis/rules/Option.kt @@ -0,0 +1,6 @@ +package org.hisp.dhis.rules + +/** + * @author rajazubair + */ +data class Option(val name: String, val code: String) diff --git a/src/main/java/org/hisp/dhis/rules/models/RuleVariable.java b/src/main/java/org/hisp/dhis/rules/models/RuleVariable.java index 808d4962..a37290c5 100644 --- a/src/main/java/org/hisp/dhis/rules/models/RuleVariable.java +++ b/src/main/java/org/hisp/dhis/rules/models/RuleVariable.java @@ -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 ); } diff --git a/src/test/java/org/hisp/dhis/rules/RuleEngineFunctionTest.java b/src/test/java/org/hisp/dhis/rules/RuleEngineFunctionTest.java index d6e767fd..581c2232 100644 --- a/src/test/java/org/hisp/dhis/rules/RuleEngineFunctionTest.java +++ b/src/test/java/org/hisp/dhis/rules/RuleEngineFunctionTest.java @@ -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 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() ); } @@ -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 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() ); } diff --git a/src/test/java/org/hisp/dhis/rules/RuleEngineGetDescriptionTest.java b/src/test/java/org/hisp/dhis/rules/RuleEngineGetDescriptionTest.java index e86a2f9b..752be7e0 100644 --- a/src/test/java/org/hisp/dhis/rules/RuleEngineGetDescriptionTest.java +++ b/src/test/java/org/hisp/dhis/rules/RuleEngineGetDescriptionTest.java @@ -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 );