-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
test: CGS discard tests #38206
test: CGS discard tests #38206
Conversation
WalkthroughThe pull request introduces a new test class Changes
Sequence DiagramsequenceDiagram
participant Developer
participant GitDiscardTests
participant CentralGitService
participant ApplicationService
Developer->>GitDiscardTests: Trigger Discard Test
GitDiscardTests->>CentralGitService: Invoke Discard Changes
CentralGitService->>ApplicationService: Update Application State
CentralGitService-->>GitDiscardTests: Return Discard Result
GitDiscardTests->>GitDiscardTests: Verify Discard Outcome
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Tip CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command 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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (2)
app/server/appsmith-server/src/test/java/com/appsmith/server/git/ops/GitDiscardTests.java (2)
91-157
: Consider adding parameter validation and error handling.The utility method could benefit from:
- Validating the workspaceId parameter
- Adding error handling for the application creation block() operation
private Application createApplicationConnectedToGit(String name, String branchName, String workspaceId) throws IOException { + if (StringUtils.isEmpty(workspaceId)) { + throw new IllegalArgumentException("workspaceId cannot be empty"); + } // ... rest of the method Application application1 = - applicationPageService.createApplication(testApplication).block(); + applicationPageService.createApplication(testApplication) + .onErrorMap(error -> new RuntimeException("Failed to create application: " + error.getMessage())) + .block();
173-216
: Enhance test assertions for better coverage.The assertion only checks if pages are not equal. Consider adding more specific assertions:
- Verify the exact number of pages
- Verify the page content
- Verify the Git status after discard
StepVerifier.create(applicationMono) .assertNext(application1 -> { - assertThat(application1.getPages()).isNotEqualTo(application.getPages()); + assertThat(application1.getPages()).isNotNull(); + assertThat(application1.getPages()).hasSize(1); + assertThat(application1.getGitApplicationMetadata().getIsRepoPrivate()).isTrue(); }) .verifyComplete();
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
app/server/appsmith-server/src/test/java/com/appsmith/server/git/ops/GitDiscardTests.java
(1 hunks)
🔇 Additional comments (1)
app/server/appsmith-server/src/test/java/com/appsmith/server/git/ops/GitDiscardTests.java (1)
54-90
: LGTM! Test class setup follows best practices.
The test class is properly configured with Spring Boot test annotations and appropriate dependency injection setup.
private Mono<? extends ArtifactExchangeJson> createArtifactJson(String filePath) throws IOException { | ||
|
||
ClassPathResource classPathResource = new ClassPathResource(filePath); | ||
|
||
String artifactJson = classPathResource.getContentAsString(Charset.defaultCharset()); | ||
|
||
Class<? extends ArtifactExchangeJson> exchangeJsonType = ApplicationJson.class; | ||
|
||
ArtifactExchangeJson artifactExchangeJson = | ||
objectMapper.copy().disable(MapperFeature.USE_ANNOTATIONS).readValue(artifactJson, exchangeJsonType); | ||
|
||
return jsonSchemaMigration.migrateArtifactExchangeJsonToLatestSchema(artifactExchangeJson, null, null); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add error handling for file not found scenario.
The method should handle the case where the specified file is not found in the classpath.
private Mono<? extends ArtifactExchangeJson> createArtifactJson(String filePath) throws IOException {
+ if (StringUtils.isEmpty(filePath)) {
+ return Mono.error(new IllegalArgumentException("filePath cannot be empty"));
+ }
ClassPathResource classPathResource = new ClassPathResource(filePath);
+ if (!classPathResource.exists()) {
+ return Mono.error(new IOException("File not found: " + filePath));
+ }
// ... rest of the method
Committable suggestion skipped: line range outside the PR's diff.
@Test | ||
@WithUserDetails(value = "api_user") | ||
public void discardChanges_whenCancelledMidway_discardsSuccessfully() throws IOException, GitAPIException { | ||
User apiUser = userService.findByEmail("api_user").block(); | ||
Workspace toCreate = new Workspace(); | ||
toCreate.setName("Workspace_" + UUID.randomUUID()); | ||
Workspace workspace = | ||
workspaceService.create(toCreate, apiUser, Boolean.FALSE).block(); | ||
assertThat(workspace).isNotNull(); | ||
|
||
Application application = createApplicationConnectedToGit( | ||
"discard-changes-midway", "discard-change-midway-branch", workspace.getId()); | ||
MergeStatusDTO mergeStatusDTO = new MergeStatusDTO(); | ||
mergeStatusDTO.setStatus("Nothing to fetch from remote. All changes are upto date."); | ||
mergeStatusDTO.setMergeAble(true); | ||
|
||
ArtifactExchangeJson artifactExchangeJson = createArtifactJson( | ||
"test_assets/ImportExportServiceTest/valid-application-without-action-collection.json") | ||
.block(); | ||
((Application) artifactExchangeJson.getArtifact()).setName("discard-changes-midway"); | ||
|
||
GitStatusDTO gitStatusDTO = new GitStatusDTO(); | ||
gitStatusDTO.setAheadCount(0); | ||
gitStatusDTO.setBehindCount(0); | ||
gitStatusDTO.setIsClean(true); | ||
|
||
Mockito.doReturn(Mono.just(Paths.get("path"))) | ||
.when(commonGitFileUtils) | ||
.saveArtifactToLocalRepoWithAnalytics(any(Path.class), any(), Mockito.anyString()); | ||
Mockito.doReturn(Mono.just(artifactExchangeJson)) | ||
.when(gitHandlingService) | ||
.recreateArtifactJsonFromLastCommit(Mockito.any()); | ||
Mockito.doReturn(Mono.just(mergeStatusDTO)) | ||
.when(fsGitHandler) | ||
.pullApplication( | ||
any(Path.class), | ||
Mockito.anyString(), | ||
Mockito.anyString(), | ||
Mockito.anyString(), | ||
Mockito.anyString()); | ||
Mockito.doReturn(Mono.just(gitStatusDTO)).when(fsGitHandler).getStatus(any(Path.class), Mockito.anyString()); | ||
Mockito.doReturn(Mono.just("fetched")) | ||
.when(fsGitHandler) | ||
.fetchRemote( | ||
any(Path.class), | ||
Mockito.anyString(), | ||
Mockito.anyString(), | ||
eq(true), | ||
Mockito.anyString(), | ||
Mockito.anyBoolean()); | ||
|
||
centralGitService | ||
.discardChanges(application.getId(), ArtifactType.APPLICATION, GitType.FILE_SYSTEM) | ||
.map(artifact -> (Application) artifact) | ||
.timeout(Duration.ofNanos(100)) | ||
.subscribe(); | ||
|
||
// Wait for git clone to complete | ||
Mono<Application> applicationFromDbMono = Mono.just(application).flatMap(application1 -> { | ||
try { | ||
// Before fetching the git connected application, sleep for 5 seconds to ensure that the clone | ||
// completes | ||
Thread.sleep(5000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
return applicationService.getById(application1.getId()); | ||
}); | ||
|
||
StepVerifier.create(applicationFromDbMono) | ||
.assertNext(application1 -> { | ||
assertThat(application1).isNotEqualTo(application); | ||
}) | ||
.verifyComplete(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace Thread.sleep with proper test synchronization.
Using Thread.sleep for test synchronization is unreliable and can lead to flaky tests. Consider:
- Using TestPublisher for controlled emission
- Using StepVerifier's virtual time capabilities
- Thread.sleep(5000);
+ return applicationService.getById(application1.getId())
+ .retryWhen(Retry.fixedDelay(3, Duration.ofSeconds(1))
+ .filter(error -> error instanceof IllegalStateException));
Committable suggestion skipped: line range outside the PR's diff.
|
||
StepVerifier.create(applicationFromDbMono) | ||
.assertNext(application1 -> { | ||
assertThat(application1).isNotEqualTo(application); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how is this equality check working?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just that it is not the same object. We're only checking for completion here
## Description > [!TIP] > _Add a TL;DR when the description is longer than 500 words or extremely technical (helps the content, marketing, and DevRel team)._ > > _Please also include relevant motivation and context. List any dependencies that are required for this change. Add links to Notion, Figma or any other documents that might be relevant to the PR._ Fixes #`Issue Number` _or_ Fixes `Issue URL` > [!WARNING] > _If no issue exists, please create an issue first, and check with the maintainers if the issue is valid._ ## Automation /ok-to-test tags="" ### 🔍 Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!CAUTION] > If you modify the content in this section, you are likely to disrupt the CI result for your PR. <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [ ] No <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Tests** - Introduced a new test suite for the Git discard functionality, covering various scenarios to ensure application integrity during Git operations. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Description
Tip
Add a TL;DR when the description is longer than 500 words or extremely technical (helps the content, marketing, and DevRel team).
Please also include relevant motivation and context. List any dependencies that are required for this change. Add links to Notion, Figma or any other documents that might be relevant to the PR.
Fixes #
Issue Number
or
Fixes
Issue URL
Warning
If no issue exists, please create an issue first, and check with the maintainers if the issue is valid.
Automation
/ok-to-test tags=""
🔍 Cypress test results
Caution
If you modify the content in this section, you are likely to disrupt the CI result for your PR.
Communication
Should the DevRel and Marketing teams inform users about this change?
Summary by CodeRabbit