-
Notifications
You must be signed in to change notification settings - Fork 9
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
sets up infra to load heavy data #1618
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.example.learning.repository; | ||
|
||
import com.example.learning.entities.Post; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface PostRepository extends JpaRepository<Post, Long> {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.example.learning.repository; | ||
|
||
import com.example.learning.entities.Tag; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface TagRepository extends JpaRepository<Tag, Long> {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.example.learning.utils; | ||
|
||
public interface AppConstants { | ||
|
||
String PROFILE_TEST = "test"; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
spring.datasource.url=jdbc:postgresql://localhost:5432/appdb | ||
spring.datasource.username=appuser | ||
spring.datasource.password=secret | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid committing credentials to source control. Storing credentials in code is flagged by SonarCloud and is a significant security risk, even in local property files. Consider using environment variables, secrets manager, or other secure methods to supply sensitive credentials. Additionally, ensure this password is changed and revoked. Do you want me to outline a secure approach using environment variables or a credentials vault? 🧰 Tools🪛 GitHub Check: SonarCloud[failure] 3-3: Credentials should not be hard-coded Revoke and change this password, as it is compromised.See more on SonarQube Cloud |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.example.learning; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.instancio.Select.all; | ||
import static org.instancio.Select.field; | ||
|
||
import com.example.learning.common.AbstractIntegrationTest; | ||
import com.example.learning.entities.Post; | ||
import com.example.learning.entities.PostComment; | ||
import com.example.learning.entities.PostDetails; | ||
import com.example.learning.entities.PostTag; | ||
import com.example.learning.entities.Tag; | ||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import org.instancio.Instancio; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ApplicationIntegrationTest extends AbstractIntegrationTest { | ||
|
||
@Test | ||
void contextLoads() { | ||
// Create a finite dataset of tags with unique tag names | ||
List<Tag> finiteTags = Instancio.ofList(Tag.class) | ||
.size(100) // Limit to 100 unique tags | ||
.ignore(field(Tag::getId)) | ||
.create(); | ||
|
||
// Generate Post objects with specific fields ignored | ||
List<Post> postList = Instancio.ofList(Post.class) | ||
.size(100) | ||
.ignore(field(Post::getId)) | ||
.ignore(field(Post::getCreatedAt)) | ||
.ignore(field(Post::getModifiedAt)) | ||
.supply(all(LocalDateTime.class), () -> LocalDateTime.now()) | ||
.supply(field(Post::getDetails), () -> Instancio.of(PostDetails.class) | ||
.ignore(field(PostDetails::getId)) | ||
.ignore(field(PostDetails::getCreatedAt)) | ||
.ignore(field(PostDetails::getModifiedAt)) | ||
.ignore(field(PostDetails::getPost)) | ||
.create()) | ||
.supply(field(Post::getComments), () -> new ArrayList<PostComment>()) | ||
.supply(field(Post::getTags), () -> new ArrayList<PostTag>()) | ||
.create(); | ||
|
||
// Assign tags and comments to posts, reusing finite tags | ||
postList.forEach(post -> { | ||
post.setDetails(post.getDetails()); | ||
|
||
List<PostComment> postCommentList = Instancio.ofList(PostComment.class) | ||
.size(50) // Assuming 50 comments per post | ||
.ignore(field(PostComment::getId)) | ||
.ignore(field(PostComment::getCreatedAt)) | ||
.ignore(field(PostComment::getModifiedAt)) | ||
.ignore(field(PostComment::getPost)) | ||
.create(); | ||
postCommentList.forEach(post::addComment); | ||
|
||
// Randomly select tags from the finiteTags dataset | ||
Collections.shuffle(finiteTags); // Shuffle to randomize selection | ||
List<Tag> tags = finiteTags.stream() | ||
.limit(30) // Assign 30 tags per post | ||
.toList(); | ||
tags.forEach(post::addTag); | ||
}); | ||
|
||
// Save the modified posts | ||
List<Post> savedPostList = postRepository.saveAll(postList); | ||
|
||
// Assertions to validate the results | ||
assertThat(savedPostList).isNotEmpty().hasSize(postList.size()); | ||
assertThat(tagRepository.count()).isEqualTo(100); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.example.learning.common; | ||
|
||
import static com.example.learning.utils.AppConstants.PROFILE_TEST; | ||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; | ||
|
||
import com.example.learning.repository.PostRepository; | ||
import com.example.learning.repository.TagRepository; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.instancio.junit.InstancioExtension; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.web.servlet.assertj.MockMvcTester; | ||
|
||
@ActiveProfiles({PROFILE_TEST}) | ||
@SpringBootTest( | ||
webEnvironment = RANDOM_PORT, | ||
classes = {SQLContainerConfig.class}, | ||
properties = {"jdbc.datasource-proxy.enabled=false"}) | ||
@AutoConfigureMockMvc | ||
@ExtendWith(InstancioExtension.class) | ||
public abstract class AbstractIntegrationTest { | ||
|
||
@Autowired | ||
protected MockMvcTester mockMvcTester; | ||
|
||
@Autowired | ||
protected ObjectMapper objectMapper; | ||
|
||
@Autowired | ||
protected PostRepository postRepository; | ||
|
||
@Autowired | ||
protected TagRepository tagRepository; | ||
} |
Check failure
Code scanning / SonarCloud
Credentials should not be hard-coded High