Skip to content

Commit

Permalink
Create some avatars for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentTreguier committed Sep 16, 2024
1 parent c59d44a commit 8c78c45
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
20 changes: 17 additions & 3 deletions src/main/java/app/fyreplace/api/data/dev/DataSeeder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.fyreplace.api.data.dev;

import static java.util.Objects.requireNonNull;
import static java.util.stream.IntStream.range;

import app.fyreplace.api.data.Block;
Expand All @@ -19,6 +20,7 @@
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.transaction.Transactional;
import java.io.IOException;
import org.eclipse.microprofile.config.inject.ConfigProperty;

@SuppressWarnings({"UnusedReturnValue", "unused"})
Expand All @@ -44,8 +46,8 @@ public void onShutdown(@Observes final ShutdownEvent event) {

@Transactional
public void insertData() {
range(0, 20).forEach(i -> createUser("user_" + i, true));
range(0, 10).forEach(i -> createUser("user_inactive_" + i, false));
range(0, 20).forEach(i -> createUser("user_" + i, true, i % 2 == 0));
range(0, 10).forEach(i -> createUser("user_inactive_" + i, false, false));
final var user = User.findByUsername("user_0");
range(0, 20).forEach(i -> createPost(user, "Post " + i, true, false));
range(0, 20).forEach(i -> createPost(user, "Draft " + i, false, false));
Expand Down Expand Up @@ -74,12 +76,24 @@ public void deleteData() {
}

@Transactional(Transactional.TxType.REQUIRES_NEW)
public User createUser(final String username, final boolean active) {
public User createUser(final String username, final boolean active, final boolean hasAvatar) {
final var user = new User();
user.username = username;
user.active = active;
user.persist();

if (hasAvatar) {
try (final var stream = getClass().getResourceAsStream("/META-INF/resources/images/logo-maskable.png")) {
final var storedFile = new StoredFile(
"avatars", user.username, requireNonNull(stream).readAllBytes());
storedFile.persist();
user.avatar = storedFile;
user.persist();
} catch (final IOException e) {
throw new RuntimeException(e);
}
}

final var email = new Email();
email.user = user;
email.email = username + "@example.org";
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/app/fyreplace/api/testing/UserTestsBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class UserTestsBase extends TransactionalTestsBase {
@Override
public void beforeEach() {
super.beforeEach();
range(0, getActiveUserCount()).forEach(i -> dataSeeder.createUser("user_" + i, true));
range(0, getInactiveUserCount()).forEach(i -> dataSeeder.createUser("user_inactive_" + i, false));
range(0, getActiveUserCount()).forEach(i -> dataSeeder.createUser("user_" + i, true, false));
range(0, getInactiveUserCount()).forEach(i -> dataSeeder.createUser("user_inactive_" + i, false, false));
}

public int getActiveUserCount() {
Expand Down

0 comments on commit 8c78c45

Please sign in to comment.