Skip to content

Refactor API product category test case#13976

Open
sgayangi wants to merge 1 commit intowso2:masterfrom
sgayangi:devportal-categories
Open

Refactor API product category test case#13976
sgayangi wants to merge 1 commit intowso2:masterfrom
sgayangi:devportal-categories

Conversation

@sgayangi
Copy link
Copy Markdown
Contributor

@sgayangi sgayangi commented Feb 26, 2026

Added refactoring for #13943

Summary by CodeRabbit

  • Tests
    • Improved API product creation integration tests by replacing manual category setup with factory-generated categories and performing category creation/deletion via the API.
    • Added HTTP response assertions for category create/delete, verified category visibility in publisher/portal flows, and ensured automated cleanup for more reliable, deterministic tests.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 26, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3b9a271 and 0c88bdf.

📒 Files selected for processing (1)
  • all-in-one-apim/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • all-in-one-apim/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java

Walkthrough

Refactored test testCreateAndInvokeApiProductWithAPICategoryAdded to create and delete API categories via the admin REST API using DtoFactory.createApiCategoryDTO; captures the created categoryId/categoryName, asserts HTTP responses for add/delete, and verifies category propagation in API product and published API.

Changes

Cohort / File(s) Summary
API Category Test Refactoring
all-in-one-apim/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java
Added DtoFactory import; replaced manual APICategoryDTO construction with DtoFactory.createApiCategoryDTO; create category via restAPIAdmin.addApiCategory and assert HTTP 201; capture and reuse generated apiCategoryId and categoryName; verify category presence in API product/publisher/portal; delete category via restAPIAdmin.deleteApiCategory and assert HTTP 200; removed previous in-test existence handling.

Sequence Diagram(s)

(omitted — changes are test refactor and do not introduce new multi-component control flow needing a sequence diagram)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • chamilaadhi
  • tharindu1st
  • Arshardh
  • AnuGayan

Poem

🐰 A DTO hop, a RESTful cheer,
I craft a name and fetch an id dear,
I add, I check, I tidy the trace,
Delete the trail without a chase,
Tests hop forward — steady, clear.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: refactoring of API product category test case, which aligns with the changeset's focus on restructuring category handling in APIProductCreationTestCase.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
all-in-one-apim/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java (1)

450-452: Resource cleanup is fragile: category deletion may be skipped on test failure.

The category deletion at lines 451-452 is placed inline after the assertions. If any assertion fails between lines 404-449, the test will throw an exception and the category will not be deleted, leaving orphaned test data in the system.

Consider wrapping the test logic in a try-finally block to ensure cleanup always occurs, or use an instance variable with @AfterMethod cleanup.

🧹 Suggested pattern for reliable cleanup
+    private String apiCategoryIdToCleanup;
+
     `@Test`(groups = {"wso2.am"}, description = "Test creation and deployment of API Product with an API " +
             "category")
     public void testCreateAndInvokeApiProductWithAPICategoryAdded() throws Exception {
         // Create the Marketing category first
         String categoryName = "Marketing-" + UUID.randomUUID().toString().substring(0, 8);
         String description = "Marketing category for testing";

         APICategoryDTO categoryDTO = DtoFactory.createApiCategoryDTO(categoryName, description);
         //Add the api category
         ApiResponse<APICategoryDTO> addedApiCategory = restAPIAdmin.addApiCategory(categoryDTO);
         //Assert the status code and api category ID
         Assert.assertEquals(addedApiCategory.getStatusCode(), HttpStatus.SC_CREATED);

         APICategoryDTO addedApiCategoryDTO = addedApiCategory.getData();
         String apiCategoryId = addedApiCategoryDTO.getId();
-        categoryDTO.setId(apiCategoryId);
+        apiCategoryIdToCleanup = apiCategoryId;
         
+        try {
             // ... rest of the test logic ...
-        
-        ApiResponse<Void> apiResponse = restAPIAdmin.deleteApiCategory(apiCategoryId);
-        Assert.assertEquals(apiResponse.getStatusCode(), HttpStatus.SC_OK);
+        } finally {
+            if (apiCategoryIdToCleanup != null) {
+                restAPIAdmin.deleteApiCategory(apiCategoryIdToCleanup);
+            }
+        }
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@all-in-one-apim/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java`
around lines 450 - 452, The test currently deletes the category inline after
assertions which can be skipped if an assertion fails; ensure cleanup always
runs by moving deletion into a finally block or into an `@AfterMethod` that uses
an instance variable for apiCategoryId so
restAPIAdmin.deleteApiCategory(apiCategoryId) is always invoked; update the test
method (where apiCategoryId is set) to assign the id to a field and implement an
`@AfterMethod` that calls restAPIAdmin.deleteApiCategory(apiCategoryId) and
asserts the ApiResponse status (ApiResponse<Void>, deleteApiCategory) so
orphaned categories are removed even on test failures.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@all-in-one-apim/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java`:
- Around line 397-409: The test uses DtoFactory.createApiCategoryDTO but the
import for DtoFactory is missing and causes compilation failure; add the import
org.wso2.am.integration.test.impl.DtoFactory to the file. Also remove the dead
assignment categoryDTO.setId(apiCategoryId) (the categoryDTO object is not used
afterwards) to clean up unused code; locate the APICategoryDTO categoryDTO
declaration and the subsequent categoryDTO.setId(...) call and delete that
single assignment line.

---

Nitpick comments:
In
`@all-in-one-apim/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java`:
- Around line 450-452: The test currently deletes the category inline after
assertions which can be skipped if an assertion fails; ensure cleanup always
runs by moving deletion into a finally block or into an `@AfterMethod` that uses
an instance variable for apiCategoryId so
restAPIAdmin.deleteApiCategory(apiCategoryId) is always invoked; update the test
method (where apiCategoryId is set) to assign the id to a field and implement an
`@AfterMethod` that calls restAPIAdmin.deleteApiCategory(apiCategoryId) and
asserts the ApiResponse status (ApiResponse<Void>, deleteApiCategory) so
orphaned categories are removed even on test failures.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c218662 and 01337ae.

📒 Files selected for processing (1)
  • all-in-one-apim/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java

@sgayangi sgayangi force-pushed the devportal-categories branch from 01337ae to 3b9a271 Compare February 26, 2026 06:02
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
all-in-one-apim/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java (1)

449-451: Consider using try-finally to ensure category cleanup on test failure.

The category deletion is placed at the end of the test method. If any assertion fails earlier (e.g., lines 404, 426, 435-436, 441-442, 447-448), the category won't be deleted, leaving orphaned test data.

♻️ Optional: Wrap test body in try-finally for reliable cleanup
     public void testCreateAndInvokeApiProductWithAPICategoryAdded() throws Exception {
         // Create the Marketing category first
         String categoryName = "Marketing-" + UUID.randomUUID().toString().substring(0, 8);
         String description = "Marketing category for testing";
 
         APICategoryDTO categoryDTO = DtoFactory.createApiCategoryDTO(categoryName, description);
         //Add the api category
         ApiResponse<APICategoryDTO> addedApiCategory = restAPIAdmin.addApiCategory(categoryDTO);
         //Assert the status code and api category ID
         Assert.assertEquals(addedApiCategory.getStatusCode(), HttpStatus.SC_CREATED);
 
         APICategoryDTO addedApiCategoryDTO = addedApiCategory.getData();
         String apiCategoryId = addedApiCategoryDTO.getId();
 
+        try {
             String provider = user.getUserName();
             // ... rest of test logic ...
             Assert.assertTrue(categoriesInReceivedAPI.contains(categoryName));
+        } finally {
+            ApiResponse<Void> apiResponse = restAPIAdmin.deleteApiCategory(apiCategoryId);
+            Assert.assertEquals(apiResponse.getStatusCode(), HttpStatus.SC_OK);
+        }
-        
-        ApiResponse<Void> apiResponse = restAPIAdmin.deleteApiCategory(apiCategoryId);
-        Assert.assertEquals(apiResponse.getStatusCode(), HttpStatus.SC_OK);
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@all-in-one-apim/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java`
around lines 449 - 451, Test currently deletes the API category only at the end,
risking orphaned data if earlier assertions fail; wrap the test logic in a
try-finally so cleanup always runs. Move the body that makes assertions into a
try block and perform the call to restAPIAdmin.deleteApiCategory(apiCategoryId)
and the Assert on apiResponse.getStatusCode() inside a finally block (or call
deleteApiCategory inside finally and assert its status there), referencing the
existing apiCategoryId variable and deleteApiCategory / ApiResponse<Void>
apiResponse to ensure the category is removed even when the test fails.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In
`@all-in-one-apim/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java`:
- Around line 449-451: Test currently deletes the API category only at the end,
risking orphaned data if earlier assertions fail; wrap the test logic in a
try-finally so cleanup always runs. Move the body that makes assertions into a
try block and perform the call to restAPIAdmin.deleteApiCategory(apiCategoryId)
and the Assert on apiResponse.getStatusCode() inside a finally block (or call
deleteApiCategory inside finally and assert its status there), referencing the
existing apiCategoryId variable and deleteApiCategory / ApiResponse<Void>
apiResponse to ensure the category is removed even when the test fails.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 01337ae and 3b9a271.

📒 Files selected for processing (1)
  • all-in-one-apim/modules/integration/tests-integration/tests-backend/src/test/java/org/wso2/am/integration/tests/apiproduct/APIProductCreationTestCase.java

@sgayangi sgayangi changed the title Refactor API product creation test case Refactor API product category test case Feb 26, 2026
@sgayangi sgayangi force-pushed the devportal-categories branch from 3b9a271 to 0c88bdf Compare February 26, 2026 06:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant