Skip to content

Conversation

@hichamboushaba
Copy link
Member

@hichamboushaba hichamboushaba commented Dec 3, 2024

Description

As part of my Hack Week, I'm bringing back the ApiFaker project paqN3M-SI-p2, and I'm splitting the feature into multiple PRs.

This PR adds the apifaker gradle module, and with it the following changes:

  1. A Room database to hold the list of faked endpoints.
  2. The EndpointProcessor class is responsible for matching and finding the faked endpoints.
  3. The OkHttp interceptor that replaces the response when needed.

Steps to reproduce

Code review and green CI should be enough.

But if you prefer to do some tests, you can use the following PR #13070

  • I have considered if this change warrants release notes and have added them to RELEASE-NOTES.txt if necessary. Use the "[Internal]" label for non-user-facing changes.

Reviewer (or Author, in the case of optional code reviews):

Please make sure these conditions are met before approving the PR, or request changes if the PR needs improvement:

  • The PR is small and has a clear, single focus, or a valid explanation is provided in the description. If needed, please request to split it into smaller PRs.
  • Ensure Adequate Unit Test Coverage: The changes are reasonably covered by unit tests or an explanation is provided in the PR description.
  • Manual Testing: The author listed all the tests they ran, including smoke tests when needed (e.g., for refactorings). The reviewer confirmed that the PR works as expected on big (tablet) and small (phone) in case of UI changes, and no regressions are added.

@hichamboushaba hichamboushaba added type: task An internally driven task. milestone-not-required Prevents the Milestone check to fail the build labels Dec 3, 2024
@dangermattic
Copy link
Collaborator

dangermattic commented Dec 3, 2024

11 Warnings
⚠️ This PR is larger than 300 lines of changes. Please consider splitting it into smaller PRs for easier and faster reviews.
⚠️ Class ApiFakerConfig is missing tests, but unit-tests-exemption label was set to ignore this.
⚠️ Class ApiFakerInterceptor is missing tests, but unit-tests-exemption label was set to ignore this.
⚠️ Class ApiFakerUiHelper is missing tests, but unit-tests-exemption label was set to ignore this.
⚠️ Class ApiFakerDatabase is missing tests, but unit-tests-exemption label was set to ignore this.
⚠️ Class EndpointTypeConverter is missing tests, but unit-tests-exemption label was set to ignore this.
⚠️ Class QueryParameterConverter is missing tests, but unit-tests-exemption label was set to ignore this.
⚠️ Class Screen is missing tests, but unit-tests-exemption label was set to ignore this.
⚠️ Class EndpointDetailsViewModel is missing tests, but unit-tests-exemption label was set to ignore this.
⚠️ Class HomeViewModel is missing tests, but unit-tests-exemption label was set to ignore this.
⚠️ This PR is assigned to the milestone 21.3. This milestone is due in less than 2 days.
Please make sure to get it merged by then or assign it to a milestone with a later deadline.

Generated by 🚫 Danger

@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Dec 3, 2024

📲 You can test the changes from this Pull Request in WooCommerce-Wear Android by scanning the QR code below to install the corresponding build.
App Name WooCommerce-Wear Android
Platform⌚️ Wear OS
FlavorJalapeno
Build TypeDebug
Commit1f7edf5
Direct Downloadwoocommerce-wear-prototype-build-pr13052-1f7edf5.apk

@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Dec 3, 2024

📲 You can test the changes from this Pull Request in WooCommerce Android by scanning the QR code below to install the corresponding build.

App Name WooCommerce Android
Platform📱 Mobile
FlavorJalapeno
Build TypeDebug
Commit1f7edf5
Direct Downloadwoocommerce-prototype-build-pr13052-1f7edf5.apk

@codecov-commenter
Copy link

codecov-commenter commented Dec 3, 2024

Codecov Report

Attention: Patch coverage is 1.93705% with 405 lines in your changes missing coverage. Please review.

Project coverage is 40.22%. Comparing base (71e0fae) to head (1f7edf5).
Report is 227 commits behind head on trunk.

Files with missing lines Patch % Lines
.../woocommerce/android/apifaker/EndpointProcessor.kt 0.00% 82 Missing ⚠️
...id/apifaker/ui/details/EndpointDetailsViewModel.kt 0.00% 76 Missing ⚠️
...m/woocommerce/android/apifaker/ApiFakerUiHelper.kt 0.00% 68 Missing ⚠️
...om/woocommerce/android/apifaker/ui/DropDownMenu.kt 0.00% 36 Missing ⚠️
...oocommerce/android/apifaker/ApiFakerInterceptor.kt 0.00% 26 Missing ⚠️
...com/woocommerce/android/apifaker/ApiFakerConfig.kt 0.00% 21 Missing ⚠️
...woocommerce/android/apifaker/ui/ApiFakerNavHost.kt 0.00% 21 Missing ⚠️
...commerce/android/apifaker/ui/home/HomeViewModel.kt 0.00% 16 Missing ⚠️
...merce/android/apifaker/db/EndpointTypeConverter.kt 0.00% 10 Missing ⚠️
...oocommerce/android/apifaker/db/ApiFakerDatabase.kt 0.00% 8 Missing ⚠️
... and 11 more
Additional details and impacted files
@@             Coverage Diff              @@
##              trunk   #13052      +/-   ##
============================================
- Coverage     40.43%   40.22%   -0.22%     
  Complexity     6230     6230              
============================================
  Files          1302     1321      +19     
  Lines         75458    75870     +412     
  Branches      10358    10404      +46     
============================================
+ Hits          30513    30520       +7     
- Misses        42285    42690     +405     
  Partials       2660     2660              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hichamboushaba hichamboushaba force-pushed the task/api-faker-1 branch 2 times, most recently from 185c0cb to c5117e3 Compare December 4, 2024 13:43
@JorgeMucientes JorgeMucientes self-assigned this Dec 6, 2024
Base automatically changed from task/api-faker-prep to trunk December 6, 2024 16:47
Copy link
Contributor

@JorgeMucientes JorgeMucientes left a comment

Choose a reason for hiding this comment

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

Great job @hichamboushaba, looking forward to see this in action in subsequent PRs. Code looks clean and well organised. I just left a couple minor questions, but nothing blocking.

Comment on lines +13 to +19
includeGroup "org.wordpress"
includeGroup "org.wordpress.fluxc"
includeGroup "org.wordpress.fluxc.plugins"
includeGroup "org.wordpress.wellsql"
includeGroup "org.wordpress.mediapicker"
includeGroup "com.automattic"
includeGroup "com.automattic.tracks"
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 all these? I see you only add FluxC as dependency (which also makes sense since it's where all the requests happen). But why include tracks, mediapicker, wellsql, wordpress ?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't like this either, but I tried to just keep the same setup as what we have in the other modules, personally I think this code should be in the root build.gradle file, or better in a convention plugin, but since this is not the setup we have yet, I prefer to keep the same setup as the other modules here, as the includeGroup statements won't have any impact here, they are just used for defining the repo itself.

.protocol(Protocol.HTTP_1_1)
.message("Fake Response")
.code(fakeResponse.statusCode)
// TODO check if it's safe to always use JSON as the content type
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the body of the faked request is anything the user inputs from the app, wouldn't it make more sense to leave MediaType null? For example sometimes we get and html body for API errors. Haven't tested yet, but what happens if we keep this application/json content type and enter non json body?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a good point; but for now I'll keep it like this, my rationale for hardcording application/json here is that all of the API requests we have in the app use JSON responses, the HTML errors are an exception that can be caused by a specific site setup that returns an error before the site handles the error.

what happens if we keep this application/json content type and enter non json body?

It should work as you expect from the app, the upper layer will try to deserialize the content, and would fail, then return a JSON error, this is what happens when we receive HTML instead of JSON (for example this case: p1723565504566789/1720009283.216679-slack-C6H8C3G23 API org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject)

url.encodedPath.trimEnd('/').matches(Regex(JETPACK_TUNNEL_REGEX)) &&
!startsWith("{\"data\":")
) {
"{\"data\": $this}"
Copy link
Contributor

Choose a reason for hiding this comment

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

This brings up the same question from above. What if we don't want to have JSON formatted body with data { } as the root of if? Like the example mentioned above when simulating an error that returns html?

This makes sure the log is not printed when the matched endpoints use different query parameters.
[Hack Week] ApiFaker: match endpoints using query parameters
[Hack Week] ApiFaker: display a hint on the main app UI when the ApiFaker is enabled
[Hack Week] ApiFaker: Adding, deleting and deleting endpoints
[Hack Week] ApiFaker: Home screen and integration with the app
@hichamboushaba hichamboushaba removed the status: do not merge Dependent on another PR, ready for review but not ready for merge. label Dec 17, 2024
@hichamboushaba hichamboushaba merged commit 2f219c5 into trunk Dec 19, 2024
15 checks passed
@hichamboushaba hichamboushaba deleted the task/api-faker-1 branch December 19, 2024 08:50
@wzieba wzieba mentioned this pull request Jan 14, 2025
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: task An internally driven task. unit-tests-exemption

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants