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

Lectures: Fix an issue when saving lecture units in guided mode without linked competency #9758

Merged
merged 3 commits into from
Nov 12, 2024

Conversation

florian-glombik
Copy link
Contributor

@florian-glombik florian-glombik commented Nov 12, 2024

Checklist

General

Server

Motivation and Context

Found the bug when working on Lectures: Add status bar to lecture creation and edit mode #9655.
If Lecture Units are created within the guided mode and no competency is linked, the server throws an internal server error.

Description

This PR allows the creation of a lecture unit without linking it to a competency.

Steps for Testing

Prerequisites:

  • 1 Instructor
  • 2 Students
  • 1 Exam with a Programming Exercise
  1. Create a lecture unit in guided mode
  2. Try to add a lecture unit (e.g. text lecture unit) without defining a linked competency
  3. Verify no error is thrown

Testserver States

Note

These badges show the state of the test servers.
Green = Currently available, Red = Currently locked
Click on the badges to get to the test servers.







Review Progress

Performance Review

Code Review

  • Code Review 1
  • Code Review 2

Manual Tests

  • Test 1
  • Test 2

Screenshots

Bug

bugLectureUnitWithoutCompetency

With the Bugfix

lectureUnitWithoutCompetencyBug-fixed

@florian-glombik florian-glombik added this to the 7.7.1 milestone Nov 12, 2024
@florian-glombik florian-glombik self-assigned this Nov 12, 2024
@florian-glombik florian-glombik requested a review from a team as a code owner November 12, 2024 12:34
@github-actions github-actions bot added tests server Pull requests that update Java code. (Added Automatically!) lecture Pull requests that affect the corresponding module labels Nov 12, 2024
Copy link

coderabbitai bot commented Nov 12, 2024

Walkthrough

The changes in this pull request involve modifications to the LectureUnitService class, specifically the saveWithCompetencyLinks method, to include a null check for the links variable. Additionally, a new test method has been added to the LectureUnitServiceTest class to verify the behavior of the updated method when links is null. This ensures that the method handles null references correctly and does not result in a NullPointerException.

Changes

File Change Summary
src/main/java/de/tum/cit/aet/artemis/lecture/service/LectureUnitService.java Updated saveWithCompetencyLinks method to include a null check for the links variable.
src/test/java/de/tum/cit/aet/artemis/lecture/LectureUnitServiceTest.java Added testSaveWithCompetencyLinksWithNullLinks method to test behavior with null competency links.

Possibly related PRs

Suggested labels

bugfix, ready for review

Suggested reviewers

  • JohannesStoehr
  • MaximilianAnzinger
  • egekurt123
  • N0W0RK

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 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 or @coderabbitai title 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: 0

🧹 Outside diff range and nitpick comments (3)
src/test/java/de/tum/cit/aet/artemis/lecture/LectureUnitServiceTest.java (1)

92-104: Test looks good with some suggestions for improvement.

The test effectively verifies the null handling of competency links. Consider these improvements:

  1. Extract the magic number 1L to a constant for better maintainability
  2. Make the saving lambda more descriptive of what it's mocking
  3. Consider adding more edge cases (e.g., empty list vs null)

Here's a suggested improvement:

 class LectureUnitServiceTest extends AbstractSpringIntegrationIndependentTest {
     private static final String TEST_PREFIX = "lecuniservtst";
+    private static final Long TEST_UNIT_ID = 1L;
 
     @Test
     void testSaveWithCompetencyLinksWithNullLinks() {
         AttachmentUnit lectureUnit = new AttachmentUnit();
         lectureUnit.setCompetencyLinks(null);
 
-        LectureUnit savedLectureUnit = lectureUnitService.saveWithCompetencyLinks(lectureUnit, unit -> {
-            unit.setId(1L);
-            return unit;
-        });
+        LectureUnit savedLectureUnit = lectureUnitService.saveWithCompetencyLinks(lectureUnit, 
+            // Mock persistence by setting ID
+            unit -> {
+                unit.setId(TEST_UNIT_ID);
+                return unit;
+            });
 
         assertThat(savedLectureUnit).isNotNull();
         assertThat(savedLectureUnit.getCompetencyLinks()).isEmpty();
     }
src/main/java/de/tum/cit/aet/artemis/lecture/service/LectureUnitService.java (2)

256-256: Reorder null check for better efficiency

While adding the null check is correct, it should be placed before Hibernate.isInitialized(links) for better efficiency, as there's no need to check initialization status of a null object.

-        if (Hibernate.isInitialized(links) && links != null && !links.isEmpty()) {
+        if (links != null && Hibernate.isInitialized(links) && !links.isEmpty()) {

Line range hint 251-262: Add JavaDoc for method parameters

The method lacks documentation for its parameters. Consider adding parameter descriptions to improve code maintainability.

     /**
      * Saves the exercise and links it to the competencies.
      *
      * @param lectureUnit  the lecture unit to save
-     * @param saveFunction function to save the exercise
+     * @param saveFunction function to save the lecture unit, typically a repository save operation
      * @param <T>          type of the lecture unit
-     * @return saved exercise
+     * @return saved lecture unit with updated competency links
      */
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between de36813 and b86b3c1.

📒 Files selected for processing (2)
  • src/main/java/de/tum/cit/aet/artemis/lecture/service/LectureUnitService.java (1 hunks)
  • src/test/java/de/tum/cit/aet/artemis/lecture/LectureUnitServiceTest.java (2 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
src/main/java/de/tum/cit/aet/artemis/lecture/service/LectureUnitService.java (1)

Pattern src/main/java/**/*.java: naming:CamelCase; principles:{single_responsibility,small_methods,no_duplication}; db:{perf_queries,datetime_not_timestamp}; rest:{stateless,singleton,delegate_logic,http_only,minimal_dtos}; dtos:{java_records,no_entities,min_data,single_resp}; di:constructor_injection; kiss:simple_code; file_handling:os_indep_paths; practices:{least_access,avoid_transactions,code_reuse,static_member_ref,prefer_primitives}; sql:{param_annotation,uppercase,avoid_subqueries};java:avoid_star_imports

src/test/java/de/tum/cit/aet/artemis/lecture/LectureUnitServiceTest.java (1)

Pattern src/test/java/**/*.java: test_naming: descriptive; test_size: small_specific; fixed_data: true; junit5_features: true; assert_use: assertThat; assert_specificity: true; archunit_use: enforce_package_rules; db_query_count_tests: track_performance; util_service_factory_pattern: true; avoid_db_access: true; mock_strategy: static_mocks; context_restart_minimize: true

🔇 Additional comments (2)
src/test/java/de/tum/cit/aet/artemis/lecture/LectureUnitServiceTest.java (1)

14-14: LGTM!

The import statement is correctly placed and properly used in the new test method.

src/main/java/de/tum/cit/aet/artemis/lecture/service/LectureUnitService.java (1)

Line range hint 251-262: Verify test coverage and usage patterns

Let's ensure this change is properly tested and identify all callers of this method.

✅ Verification successful

Test coverage and usage patterns are adequate.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check test coverage and usage patterns for saveWithCompetencyLinks

# Find test files that might contain tests for this method
echo "Looking for relevant test files..."
fd -e java -e kt --full-path "test.*LectureUnit.*Test"

# Find all usages of saveWithCompetencyLinks
echo -e "\nChecking usage patterns..."
rg "saveWithCompetencyLinks" --type java

Length of output: 5443

Copy link
Contributor

@JohannesStoehr JohannesStoehr left a comment

Choose a reason for hiding this comment

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

Code

Copy link
Contributor

@SimonEntholzer SimonEntholzer left a comment

Choose a reason for hiding this comment

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

Code 👍

Copy link
Contributor

@az108 az108 left a comment

Choose a reason for hiding this comment

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

Tested on Ts1, no issues were found

image

Copy link

@sachmii sachmii left a comment

Choose a reason for hiding this comment

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

Tested on TS1, no error was thrown.

Copy link

@HawKhiem HawKhiem left a comment

Choose a reason for hiding this comment

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

Tested on TS3. No error was thrown when creating a new unit

Copy link

@Feras797 Feras797 left a comment

Choose a reason for hiding this comment

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

Tested on TS1, when creating a lecture unit without defining a linked competency no error is thrown.

@florian-glombik florian-glombik changed the title Lectures: Fix saving lecture units Lectures: Fix saving lecture units without linked competency Nov 12, 2024
@krusche krusche changed the title Lectures: Fix saving lecture units without linked competency Lectures: Fix an issue when saving lecture units in guided mode without linked competency Nov 12, 2024
@krusche krusche merged commit f485527 into develop Nov 12, 2024
43 of 44 checks passed
@krusche krusche deleted the bugfix/lectures/fix-saving-competencies branch November 12, 2024 22:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lecture Pull requests that affect the corresponding module ready for review ready to merge server Pull requests that update Java code. (Added Automatically!) small tests
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

8 participants