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

Use JsonUtil.getJsonX more often #10071

Merged
merged 10 commits into from
Jul 31, 2024

Conversation

bencomp
Copy link
Contributor

@bencomp bencomp commented Oct 30, 2023

What this PR does / why we need it:
When I found the static methods stringToJsonObject and stringToJsonArray in MetricsUtil while working on JsonUtil.getJsonObject and getJsonArray (in #10062), I saw an opportunity for deduplicating efforts. It looked like the MetricsUtil methods were not tested either (in Coveralls) and would not close the JsonReader properly if something goes wrong (no try-with-resources or try-finally). This PR addresses these issues and fixes some minor other things in the same files.

  • It turned out that there were tests in MetricsUtilTest, but because they were in a static inner class, they did not run. I removed the static modifier and now they work again.
  • To address Use JsonUtil.getArray and JsonUtil.getObject instead of MetricsUtil.stringToJsonArray/stringToJsonObject #10061: Use JsonUtil from within the MetricsUtil methods so that the method signature doesn't change while resources close on exceptions
  • Check the input str == null path
  • Add a private constructor for MetricsUtil and remove constructing it from the test class, because it is a utility class that should not be instantiated
  • The public static field YEAR_AND_MONTH_PATTERN was not final
  • Removed type specifiers to the right of =
  • Removed public modifiers from tests and test classes

Which issue(s) this PR closes:

Closes #10061

Special notes for your reviewer:
Please check the new test. This work is related to #10062, although this is mostly syntactic cleanup.
Sonarlint still recommends to have stringToJsonObject and stringToJsonArray return an empty collection instead of null, but that is a bigger refactor.

Suggestions on how to test this:
See that the new and re-enabled tests pass.

Does this PR introduce a user interface change? If mockups are available, please link/include them here:
No

Is there a release notes update needed for this change?:
No

Additional documentation:
n/a

@bencomp bencomp marked this pull request as ready for review October 30, 2023 20:50
@pdurbin pdurbin added Size: 3 A percentage of a sprint. 2.1 hours. Component: Code Infrastructure formerly "Feature: Code Infrastructure" labels Feb 28, 2024
@coveralls
Copy link

Coverage Status

coverage: 20.704% (+0.08%) from 20.627%
when pulling c8bec05 on bencomp:10061-metricsutil-jsonutil
into a466c97 on IQSS:develop.

@sekmiller sekmiller self-assigned this Jul 23, 2024
@sekmiller sekmiller removed their assignment Jul 23, 2024
@stevenwinship stevenwinship self-assigned this Jul 31, 2024
@stevenwinship stevenwinship merged commit 0d27957 into IQSS:develop Jul 31, 2024
11 checks passed
@stevenwinship stevenwinship removed their assignment Jul 31, 2024
@pdurbin pdurbin added this to the 6.4 milestone Jul 31, 2024
@bencomp bencomp deleted the 10061-metricsutil-jsonutil branch July 31, 2024 20:59
stevenwinship added a commit that referenced this pull request Aug 2, 2024
commit 0d27957
Author: Ben Companjen <ben@companjen.name>
Date:   Wed Jul 31 20:55:11 2024 +0200

    Use JsonUtil.getJsonX more often (#10071)

    * Update MetricsUtilTest classifiers

    * Add private MetricsUtil constructor

    * Make public static field also final

    This looks like it should have been final

    * Use Java's standard order of field classifiers

    * Use empty diamond with generic constructors

    * Return value directly

    * Put long notes at top of javadoc

    * Call JsonUtil.getJsonX to prevent resource leak

    And add javadoc to the methods

    * Test StringToJsonX returns null on null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Code Infrastructure formerly "Feature: Code Infrastructure" Size: 3 A percentage of a sprint. 2.1 hours.
Projects
Status: Done 🧹
Development

Successfully merging this pull request may close these issues.

Use JsonUtil.getArray and JsonUtil.getObject instead of MetricsUtil.stringToJsonArray/stringToJsonObject
5 participants