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

Added new endpoint to retrieve address for order by order id #1608

Merged
merged 3 commits into from
Feb 11, 2025

Conversation

KizerovDmitriy
Copy link
Contributor

@KizerovDmitriy KizerovDmitriy commented Feb 11, 2025

Summary by CodeRabbit

  • New Features

    • Added endpoints to update and retrieve order addresses, enhancing address management capabilities for authorized users.
    • Integrated new security permissions to allow controlled access to the address retrieval endpoint.
  • Tests

    • Extended test coverage to verify the functionality of the new address update and retrieval endpoints.
    • Added tests for the address service to ensure proper handling of address retrieval scenarios.
    • Introduced a new test class for verifying the address conversion logic.

@KizerovDmitriy KizerovDmitriy self-assigned this Feb 11, 2025
Copy link

coderabbitai bot commented Feb 11, 2025

Walkthrough

This pull request introduces new functionality for managing address-related operations. It adds a GET endpoint to the security configuration and the AddressController, allowing users with specific roles to retrieve order addresses. Additionally, a PATCH endpoint for updating addresses is introduced. The changes include the creation of a new AddressService interface and its implementation, a mapper for converting OrderAddress entities to DTOs, and corresponding test cases to ensure the reliability of these additions.

Changes

File(s) Change Summary
core/.../SecurityConfig.java Added a new GET request matcher for the endpoint /get-address-for-order/{orderId} with role restrictions (ADMIN, UBS_EMPLOYEE).
core/.../AddressController.java
core/.../AddressControllerTest.java
Added AddressService as a dependency; introduced two endpoints: GET for fetching an order address and PATCH for updating an address; updated tests to verify the GET endpoint.
service-api/.../AddressService.java Introduced a new AddressService interface with the getAddressForOrder(Long orderId) method.
service/.../OrderAddressToUpdateAddressDto.java
service/.../OrderAddressToUpdateAddressDtoTest.java
Added a converter class to map OrderAddress to UpdateAddressDto; included corresponding tests to validate the conversion logic.
service/.../AddressServiceImpl.java
service/.../AddressServiceTest.java
Implemented the AddressService via AddressServiceImpl using repository lookup and mapping; added tests covering both successful retrieval and not-found scenarios.
service/.../ModelUtils.java Added a utility method getOrderAddress1() to generate a sample OrderAddress instance for testing purposes.

Sequence Diagram(s)

sequenceDiagram
    participant C as Client
    participant AC as AddressController
    participant AS as AddressServiceImpl
    participant OR as OrderAddressRepository
    participant MM as ModelMapper

    C->>AC: GET /get-address-for-order/{orderId}
    AC->>AS: getAddressForOrder(orderId)
    AS->>OR: findByOrderId(orderId)
    OR-->>AS: OrderAddress (or none)
    alt Address found
        AS->>MM: map(OrderAddress to UpdateAddressDto)
        MM-->>AS: UpdateAddressDto
        AS-->>AC: UpdateAddressDto (with orderId set)
        AC-->>C: 200 OK with UpdateAddressDto
    else Address not found
        AS-->>AC: throw NotFoundException
        AC-->>C: Exception Response
    end
Loading

Possibly related PRs

Suggested reviewers

  • ChernenkoVitaliy
  • softservedata

Poem

In the realm of code, a new path is unfurled,
With endpoints that serve a digital world.
Address flows smoothly from service to UI,
Mapping and testing, reaching up to the sky.
A little cheer for each commit today,
Code blossoms like art in a refined display!
🎉 Happy coding and seamless integrations!

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@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 (5)
service/src/main/java/greencity/service/ubs/AddressServiceImpl.java (1)

21-28: Consider adding transaction management.

The implementation looks good with proper error handling. Consider adding @Transactional(readOnly = true) annotation since this is a read-only operation.

    @Override
+   @Transactional(readOnly = true)
    public UpdateAddressDto getAddressForOrder(Long orderId) {
service/src/main/java/greencity/mapping/location/OrderAddressToUpdateAddressDto.java (1)

12-31: Consider adding null safety checks.

The mapping logic is comprehensive, but consider adding null safety checks for optional fields to prevent potential NullPointerExceptions.

    protected UpdateAddressDto convert(OrderAddress source) {
+       if (source == null) {
+           return null;
+       }
        return UpdateAddressDto.builder()
service/src/test/java/greencity/mapping/location/OrderAddressToUpdateAddressDtoTest.java (1)

16-35: Add tests for edge cases.

While the happy path is well tested, consider adding tests for:

  1. Null input handling
  2. Empty/blank string fields
  3. Special characters in address fields

Example test case for null handling:

@Test
void convertNullTest() {
    var result = mapper.convert(null);
    assertNull(result);
}
service/src/test/java/greencity/service/ubs/AddressServiceTest.java (1)

46-55: Consider adding more edge cases.

While the negative test case is good, consider adding tests for:

  • Null orderId
  • Zero orderId
  • Very large orderId values
core/src/main/java/greencity/controller/AddressController.java (1)

229-245: Enhance API documentation for better clarity.

The documentation could be improved by:

  • Adding possible error scenarios in the description
  • Documenting the response structure
  • Including example values

Example documentation:

/**
 * Retrieves the address for an order with the given id.
 *
 * @param orderId The id of the order
 * @return The address details for the order
 * @throws NotFoundException if the order or address is not found
 * @response.example {
 *   "id": 1,
 *   "street": "Main St",
 *   "city": "Example City"
 * }
 */
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between adc52eb and 8b71a91.

📒 Files selected for processing (9)
  • core/src/main/java/greencity/configuration/SecurityConfig.java (1 hunks)
  • core/src/main/java/greencity/controller/AddressController.java (4 hunks)
  • core/src/test/java/greencity/controller/AddressControllerTest.java (3 hunks)
  • service-api/src/main/java/greencity/service/ubs/AddressService.java (1 hunks)
  • service/src/main/java/greencity/mapping/location/OrderAddressToUpdateAddressDto.java (1 hunks)
  • service/src/main/java/greencity/service/ubs/AddressServiceImpl.java (1 hunks)
  • service/src/test/java/greencity/ModelUtils.java (1 hunks)
  • service/src/test/java/greencity/mapping/location/OrderAddressToUpdateAddressDtoTest.java (1 hunks)
  • service/src/test/java/greencity/service/ubs/AddressServiceTest.java (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (4)
service-api/src/main/java/greencity/service/ubs/AddressService.java (1)

5-14: Well-structured interface with clear documentation!

The interface follows single responsibility principle and includes comprehensive JavaDoc documentation.

service/src/test/java/greencity/service/ubs/AddressServiceTest.java (1)

30-44: Good test coverage for successful address retrieval.

The test effectively verifies:

  • Correct repository method invocation
  • Proper mapping of the result
  • Response validation
core/src/main/java/greencity/configuration/SecurityConfig.java (1)

150-152: Security configuration looks good.

The endpoint is properly secured with appropriate role-based access control (ADMIN and UBS_EMPLOYEE roles).

service/src/test/java/greencity/ModelUtils.java (1)

5856-5872: Well-structured test data method!

The new getOrderAddress1() method is a good addition to the test utilities. It provides a complete test OrderAddress object with both Ukrainian and English translations, which will be valuable for testing address-related functionality.

Comment on lines +178 to +185
@Test
void getAddressForOrderTest() throws Exception {
mockMvc.perform(get(ubsLink + "/get-address-for-order/{id}", 1L)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());

verify(addressService).getAddressForOrder(1L);
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance test coverage for the getAddressForOrder endpoint.

While the basic success case is covered, consider adding tests for:

  • Invalid order ID
  • Not found scenario
  • Response body validation

Example test for error scenario:

@Test
void getAddressForOrderNotFoundTest() throws Exception {
    when(addressService.getAddressForOrder(-1L))
        .thenThrow(new NotFoundException("Address not found"));

    mockMvc.perform(get(ubsLink + "/get-address-for-order/{id}", -1L)
        .contentType(MediaType.APPLICATION_JSON))
        .andExpect(status().isNotFound());
}

Copy link

@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: 0

🧹 Nitpick comments (1)
core/src/main/java/greencity/controller/AddressController.java (1)

243-243: Consider using a more RESTful endpoint path.

The current path /get-address-for-order/{orderId} uses a verb in the URL. Consider a more RESTful approach:

-@GetMapping("/get-address-for-order/{orderId}")
+@GetMapping("/orders/{orderId}/address")

This follows REST conventions by using nouns and hierarchical structure to represent resources.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between be2c9d3 and f139f0b.

📒 Files selected for processing (1)
  • core/src/main/java/greencity/controller/AddressController.java (4 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (3)
core/src/main/java/greencity/controller/AddressController.java (3)

14-14: LGTM! Dependency injection is properly implemented.

The AddressService is correctly injected using constructor injection via @requiredargsconstructor, following best practices.

Also applies to: 50-50


205-227: LGTM! Well-documented PATCH endpoint with proper validation.

The endpoint follows REST best practices with:

  • Appropriate use of PATCH for updates
  • Input validation
  • Comprehensive error documentation
  • Clear API documentation

229-246: LGTM! Successfully implemented the requested functionality.

The endpoint correctly implements the PR objective to retrieve address for order by order ID, with proper error handling and documentation.

@KizerovDmitriy KizerovDmitriy merged commit 091c5f2 into dev Feb 11, 2025
5 checks passed
@KizerovDmitriy KizerovDmitriy deleted the get-address-for-order branch February 11, 2025 10:27
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.

4 participants