Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trunk 6124 Eliminated unnecessary methods (toDatetime(), getDatetime(), and getDate() should all do the same thing) #4533

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

subhamkumarr
Copy link

Description of what I changed

Eliminated unnecessary methods (toDatetime(), getDatetime(), and getDate() should all do the same thing)

Issue I worked on

https://openmrs.atlassian.net/browse/TRUNK-6124

Checklist: I completed these to help reviewers :)

  • My IDE is configured to follow the code style of this project.

  • I ran mvn clean package right before creating this pull request and
    added all formatting changes to my commit.

  • All new and existing tests passed.

  • My pull request is based on the latest changes of the master branch.

Eliminate unnecessary methods (toDatetime(), getDatetime(), and getDate() should all do the same thing)
Eliminate unnecessary methods (toDatetime(), getDatetime(), and getDate() should all do the same thing)
@tendomart
Copy link
Contributor

@subhamkumarr change the title to reflect what's being done in the ticket

@subhamkumarr subhamkumarr changed the title Trunk 6124 Trunk 6124 Eliminated unnecessary methods (toDatetime(), getDatetime(), and getDate() should all do the same thing) Jan 17, 2024
@subhamkumarr
Copy link
Author

@tendomart added

@subhamkumarr
Copy link
Author

subhamkumarr commented Jan 17, 2024

@tendomart should i also add test to the code?

import org.junit.Test;
import static org.junit.Assert.*;

public class ResultTest {

@Test
public void testToDatetimeWithNonNullValueDatetime() {
    // Create a Result with a non-null valueDatetime
    Result result = new Result(new Date(), Result.Datatype.DATETIME, null, null, new Date(), null, null, null);

    // Call toDatetime and assert the expected result
    assertEquals(result.getValueDatetime(), result.toDatetime());
}

@Test
public void testToDatetimeWithTextValue() {
    // Create a Result with datatype TEXT and a non-null valueText that can be parsed into a date
    String dateString = "2022-01-01";
    Result result = new Result(new Date(), Result.Datatype.TEXT, null, null, null, null, dateString, null);

    // Call toDatetime and assert the expected result
    assertEquals(DateUtils.parseDate(dateString), result.toDatetime());
}

@Test
public void testToDatetimeWithTextValueParsingError() {
    // Create a Result with datatype TEXT and a valueText that cannot be parsed into a date
    Result result = new Result(new Date(), Result.Datatype.TEXT, null, null, null, null, "invalidDate", null);

    // Call toDatetime and assert that it returns null
    assertNull(result.toDatetime());
}

@Test
public void testToDatetimeWithMultipleResults() {
    // Create a Result with multiple sub-results, each having a different valueDatetime
    Result subResult1 = new Result(new Date(), Result.Datatype.DATETIME, null, null, new Date(), null, null, null);
    Result subResult2 = new Result(new Date(), Result.Datatype.DATETIME, null, null, DateUtils.addDays(new Date(), 1), null, null, null);
    Result result = new Result(Arrays.asList(subResult1, subResult2));

    // Call toDatetime and assert that it returns the earliest valueDatetime among sub-results
    assertEquals(subResult1.toDatetime(), result.toDatetime());
}

}

@tendomart
Copy link
Contributor

Yes please, just to make sure nothing is breaking.

@subhamkumarr
Copy link
Author

subhamkumarr commented Jan 17, 2024

@tendomart Sir everything is good from my end. Please merge it.

The allergy field in the Allergy UI allows the user to enter a numeric value, considering allergies cannot have numeric name, we must validate the input string before allowing the user to procced further
@@ -21,6 +21,8 @@
import org.openmrs.Obs;
import org.openmrs.api.context.Context;
import org.openmrs.logic.LogicException;
import org.junit.Test;
import static org.junit.Assert.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the wild card

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tendomart Wild card? i did not get you

@tendomart
Copy link
Contributor

tendomart commented Jan 18, 2024

yes @subhamkumarr line 25

import static org.junit.Assert.*;

avoid imports like which end with .*, always import a specific class that you're using, avoid importing an entire package

look at this https://wiki.openmrs.org/display/docs/Java+Conventions

in short , use onlly absolute paths

@subhamkumarr
Copy link
Author

@tendomart done the changes. Could you please review and merge.

@@ -191,4 +191,12 @@ public void validate_shouldRejectNumericReactionValue() {
validator.validate(allergy, errors);
assertTrue(errors.hasErrors());
}
@Test
public void validate_shouldRejectNumericAllergenValue() {
Allergen allergen = new Allergen(AllergenType.DRUG, null , "45" );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the space after null and "45"

@@ -64,6 +64,11 @@ public void validate(Object target, Errors errors) {

Allergy allergy = (Allergy) target;

// Additional validation: Ensure allergen does not contain numeric values
if (allergy.getAllergen() != null && StringUtils.isNumeric(allergy.getAllergen().getNonCodedAllergen())) {
Copy link
Contributor

@tendomart tendomart Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid using null instead do something llike
if (StringUtils.isNotEmpty(allergy.getAllergen()) && StringUtils.isNumeric(allergy.getAllergen().getNonCodedAllergen()))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (StringUtils.isNumeric(Objects.requireNonNull(allergy.getAllergen(), "Allergen must not be null").getNonCodedAllergen()))
Is this is ok?

if (allergy.getAllergen() != null && StringUtils.isNumeric(allergy.getAllergen().getNonCodedAllergen())) {
errors.rejectValue("allergen", "error.allergyapi.allergy.Allergen.cannotContainNumeric");
}

if (allergy.getReactionNonCoded() != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the same here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (Objects.nonNull(allergy.getReactionNonCoded())) is this is OK?

Copy link
Contributor

@tendomart tendomart Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to do as described above or follow the the OpenMRS Java Conventions

public void validate_shouldRejectNumericAllergenValue() {
Allergen allergen = new Allergen(AllergenType.DRUG, null , "45" );
allergy.setAllergen(allergen);
Errors errors = new BindException(allergy,"allergy");
Copy link
Contributor

@tendomart tendomart Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add space between allergy and "allergy like
Errors errors = new BindException(allergy, "allergy");

@@ -543,9 +543,13 @@ public Date toDatetime() {
}
catch (Exception e) {}
}
return valueDatetime;
else {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need this extra space ?

@@ -94,6 +94,7 @@ error.email.alreadyInUse=Email address is already assigned to another user accou
error.activationkey.invalid =Invalid user activation Key
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can choose to remove the spaces before and after the Equal signs(=)

@tendomart
Copy link
Contributor

@subhamkumarr have you responded to all the changes which I requested above ?

@subhamkumarr
Copy link
Author

@tendomart I made all the changes you asked me to do. Please review.

@@ -91,7 +91,7 @@ Context.DAO.only=This method can only be called from the ContextDAO class, not {
error.rank.notPositiveInteger=Rank can only be a positive integer
error.email.invalid =Invalid Email address
error.email.alreadyInUse=Email address is already assigned to another user account
error.activationkey.invalid =Invalid user activation Key
error.activationkey.invalid=Invalid user activation Key
error.usernameOrEmail.notNullOrBlank =Username Or email cannot be null or blank
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove extra space here

public void validate_shouldRejectNumericAllergenValue() {
Allergen allergen = new Allergen(AllergenType.DRUG, null, "45");
allergy.setAllergen(allergen);
Errors errors = new BindException (allergy,"allergy");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too...extra spaces

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tendomart Done Please check

@subhamkumarr
Copy link
Author

@tendomart is this good for merge?

@@ -191,4 +191,12 @@ public void validate_shouldRejectNumericReactionValue() {
validator.validate(allergy, errors);
assertTrue(errors.hasErrors());
}
@Test
public void validate_shouldRejectNumericAllergenValue() {
Allergen allergen=new Allergen(AllergenType.DRUG, null, "45");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put spaces around the equal signs

public void validate_shouldRejectNumericAllergenValue() {
Allergen allergen=new Allergen(AllergenType.DRUG, null, "45");
allergy.setAllergen(allergen);
Errors errors=new BindException (allergy, "allergy");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be like this
Errors errors = new BindException(allergy, "allergy");

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tendomart Done

@subhamkumarr
Copy link
Author

subhamkumarr commented Jan 19, 2024

@dkayiwa could you please review my PR.

@tendomart
Copy link
Contributor

You have some failing tests..

do you need help in that ?


 Error:  Errors: 
Error:    AllergyValidatorTest.validate_shouldFailIfAllergenIsNull:82 » NullPointer Allergen must not be null
Error:    AllergyValidatorTest.validate_shouldFailIfPatientIsNull:73 » NullPointer Allergen must not be null
Error:    AllergyValidatorTest.validate_shouldRejectNumericReactionValue:191 » NullPointer Allergen must not be null

@subhamkumarr
Copy link
Author

@tendomart yes i need help

@subhamkumarr
Copy link
Author

@tendomart what changes i need to make?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants