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

sets up infra to load heavy data #1618

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions jpa/boot-jpa-jooq-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc-openapi.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -85,6 +85,11 @@
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.instancio</groupId>
<artifactId>instancio-junit</artifactId>
<version>5.2.1</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -266,4 +271,4 @@
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@
return details;
}

public void setDetails(PostDetails details) {
if (details == null) {
if (this.details != null) {
this.details.setPost(null);
}
} else {
details.setPost(this);
}
this.details = details;
}

public List<PostTag> getTags() {
return tags;
}
Expand Down Expand Up @@ -148,17 +159,6 @@
comment.setPost(null);
}

public void setDetails(PostDetails details) {
if (details == null) {
if (this.details != null) {
this.details.setPost(null);
}
} else {
details.setPost(this);
}
this.details = details;
}

public void addTag(Tag tag) {
PostTag postTag = new PostTag(this, tag);
this.tags.add(postTag);
Expand Down Expand Up @@ -188,7 +188,7 @@
}

@Override
public boolean equals(Object o) {

Check warning on line 191 in jpa/boot-jpa-jooq-sample/src/main/java/com/example/learning/entities/Post.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

'equals()' method which does not check class of parameter

`equals()` should check the class of its parameter
if (this == o) return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
Post post = (Post) o;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;
import java.util.StringJoiner;

@Entity
@Table(
Expand Down Expand Up @@ -58,6 +59,15 @@ public Tag setTagDescription(String tagDescription) {
return this;
}

@Override
public String toString() {
return new StringJoiner(", ", Tag.class.getSimpleName() + "[", "]")
.add("id=" + id)
.add("tagName='" + tagName + "'")
.add("tagDescription='" + tagDescription + "'")
.toString();
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
Expand Down
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

Check failure

Code scanning / SonarCloud

Credentials should not be hard-coded High

Revoke and change this password, as it is compromised. See more on SonarQube Cloud
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

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

15 changes: 12 additions & 3 deletions jpa/boot-jpa-jooq-sample/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ server.port=8080
server.shutdown=graceful
spring.jmx.enabled=false
spring.mvc.problemdetails.enabled=true
spring.threads.virtual.enabled=true

################ Actuator #####################
management.endpoints.web.exposure.include=configprops,env,health,info,logfile,loggers,metrics,prometheus
management.endpoint.health.access=read_only
management.endpoint.health.access=READ_ONLY

################ Database #####################
spring.jpa.show-sql=false
Expand All @@ -16,7 +17,6 @@ spring.datasource.hikari.auto-commit=false
spring.datasource.hikari.pool-name=HikariPool-${spring.application.name}
spring.datasource.hikari.data-source-properties.ApplicationName=${spring.application.name}
spring.jpa.hibernate.ddl-auto=none
#spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.jdbc.time_zone=UTC
spring.jpa.properties.hibernate.generate_statistics=false
spring.jpa.properties.hibernate.jdbc.batch_size=25
Expand All @@ -27,4 +27,13 @@ spring.jpa.properties.hibernate.query.in_clause_parameter_padding=true
spring.jpa.properties.hibernate.query.plan_cache_max_size=4096
spring.jpa.properties.hibernate.connection.provider_disables_autocommit=true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.threads.virtual.enabled=true

jdbc.datasource-proxy.logging=slf4j
jdbc.datasource-proxy.include-parameter-values=true
jdbc.datasource-proxy.query.enable-logging=true
jdbc.datasource-proxy.query.logger-name=query-logger
jdbc.datasource-proxy.slow-query.enable-logging=true
jdbc.datasource-proxy.slow-query.logger-name=slow-query-logger

logging.level.query-logger=DEBUG
logging.level.slow-query-logger=DEBUG
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
@ActiveProfiles({PROFILE_TEST})
@SpringBootTest(
webEnvironment = RANDOM_PORT,
classes = {ContainersConfig.class})
classes = {ContainersConfig.class},
properties = {"jdbc.datasource-proxy.enabled=false"})
@AutoConfigureMockMvc
public abstract class AbstractIntegrationTest {

Expand Down
Loading