Skip to content

Commit

Permalink
Fix naming consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentTreguier committed Aug 20, 2024
1 parent 536ef61 commit 19c323b
Show file tree
Hide file tree
Showing 21 changed files with 41 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/main/java/app/fyreplace/api/data/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class Post extends AuthoredEntityBase implements Reportable {
@Schema(required = true, minimum = "0")
public long voteCount;

public static final Duration shelfLife = Duration.ofDays(7);
public static final Duration SHELF_LIFE = Duration.ofDays(7);

@Override
public void softDelete() {
Expand All @@ -56,7 +56,7 @@ public List<Chapter> getChapters() {

@JsonIgnore
public boolean isOld() {
return published && Instant.now().isAfter(dateCreated.plus(shelfLife));
return published && Instant.now().isAfter(dateCreated.plus(SHELF_LIFE));
}

public void publish(final int life, final boolean anonymous) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/app/fyreplace/api/data/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
@Entity
@Table(name = "users")
public class User extends UserDependentEntityBase implements Reportable {
public static final Set<String> forbiddenUsernames = new HashSet<>(Arrays.asList(
public static final Set<String> FORBIDDEN_USERNAMES = new HashSet<>(Arrays.asList(
"admin",
"admins",
"administrator",
Expand Down Expand Up @@ -148,6 +148,7 @@ public Profile getProfile() {
return new Profile(id, username, avatar != null ? avatar.toString() : null, getTint());
}

@SuppressWarnings("unused")
@Schema(required = true)
public boolean getBlocked() {
return Block.count("source = ?1 and target = ?2", currentUser, this) > 0;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/app/fyreplace/api/data/dev/DataSeeder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@
@ApplicationScoped
public final class DataSeeder {
@ConfigProperty(name = "app.local-dev")
boolean useExampleData;
boolean localDev;

@ConfigProperty(name = "app.posts.starting-life")
int postsStartingLife;

public void onStartup(@Observes final StartupEvent event) {
if (useExampleData) {
if (localDev) {
insertData();
}
}

public void onShutdown(@Observes final ShutdownEvent event) {
if (useExampleData) {
if (localDev) {
deleteData();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public Iterable<Post> listPostsFeed() {
final var user = User.getFromSecurityContext(context, null, false);
final var conditions = new ArrayList<>(List.of("dateCreated > ?1", "published = true", "life > 0"));
final var sorting = Sort.by("life", "dateCreated", "id");
final var deadline = Instant.now().minus(Post.shelfLife);
final var deadline = Instant.now().minus(Post.SHELF_LIFE);

if (user != null) {
conditions.addAll(List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public final class UsersEndpoint {
@APIResponse(responseCode = "409", description = "Conflict")
@CacheResult(cacheName = "requests", keyGenerator = DuplicateRequestKeyGenerator.class)
public Response createUser(@NotNull @Valid final UserCreation input) {
if (User.forbiddenUsernames.contains(input.username())) {
if (User.FORBIDDEN_USERNAMES.contains(input.username())) {
throw new ForbiddenException("username_forbidden");
} else if (User.count("username", input.username()) > 0) {
throw new ConflictException("username_taken");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.List;

public record Policy(String Version, List<Statement> Statement) {
public static final String currentVersion = "2012-10-17";
public static final String CURRENT_VERSION = "2012-10-17";

public record Statement(String Effect, String Principal, String Action, String Resource) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public void onStartup(@Observes final StartupEvent event) {

try {
b.bucket(config.bucket())
.policy(objectMapper.writeValueAsString(new Policy(Policy.currentVersion, List.of(statement))));
.policy(objectMapper.writeValueAsString(
new Policy(Policy.CURRENT_VERSION, List.of(statement))));
} catch (final JsonProcessingException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public abstract class TransactionalTestsBase {
@TestHTTPResource("image.txt")
URL text;

protected static final String fakeId = "00000000-0000-0000-0000-000000000000";
protected static final String FAKE_ID = "00000000-0000-0000-0000-000000000000";

@BeforeEach
public void beforeEach() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void createChapterInDraftWhileUnauthenticated() {
@TestSecurity(user = "user_0")
public void createChapterInNonExistentPost() {
final var chapterCount = Chapter.count();
given().pathParam("id", fakeId).post().then().statusCode(404);
given().pathParam("id", FAKE_ID).post().then().statusCode(404);
assertEquals(chapterCount, Chapter.count());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void deleteChapterInDraftWhileUnauthenticated(final int position) {
@TestSecurity(user = "user_0")
public void deleteChapterInNonExistentPost(final int position) {
final var chapterCount = Chapter.count();
given().pathParam("id", fakeId).delete(String.valueOf(position)).then().statusCode(404);
given().pathParam("id", FAKE_ID).delete(String.valueOf(position)).then().statusCode(404);
assertEquals(chapterCount, Chapter.count());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void setChapterTextInNonExistentPost() throws IOException {
try (final var stream = openStream("jpeg")) {
given().contentType(ContentType.BINARY)
.body(stream.readAllBytes())
.pathParam("id", fakeId)
.pathParam("id", FAKE_ID)
.put(position + "/image")
.then()
.statusCode(404);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void setChapterPositionInNonExistentPost(final int to) {
final var oldPosition = chapter.position;
given().contentType(ContentType.JSON)
.body(new ChapterPositionUpdate(to))
.pathParam("id", fakeId)
.pathParam("id", FAKE_ID)
.put(from + "/position")
.then()
.statusCode(404);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void setChapterTextInNonExistentPost() {
final var chapter = draft.getChapters().getFirst();
final var oldText = chapter.text;
given().body("Hello")
.pathParam("id", fakeId)
.pathParam("id", FAKE_ID)
.put(position + "/text")
.then()
.statusCode(404);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public final class CountCommentsTests extends CommentTestsBase {
@Inject
DataSeeder dataSeeder;

private static final int readCommentCount = 10;
private static final int READ_COMMENT_COUNT = 10;

private static final int notReadCommentCount = 6;
private static final int NOT_READ_COMMENT_COUNT = 6;

@Test
@TestSecurity(user = "user_1")
Expand All @@ -38,7 +38,7 @@ public void countComments() {
.get("count")
.then()
.statusCode(200)
.body(equalTo(String.valueOf(readCommentCount + notReadCommentCount)));
.body(equalTo(String.valueOf(READ_COMMENT_COUNT + NOT_READ_COMMENT_COUNT)));
}

@Test
Expand All @@ -49,7 +49,7 @@ public void countReadComments() {
.get("count")
.then()
.statusCode(200)
.body(equalTo(String.valueOf(readCommentCount)));
.body(equalTo(String.valueOf(READ_COMMENT_COUNT)));
}

@Test
Expand All @@ -60,7 +60,7 @@ public void countNotReadComments() {
.get("count")
.then()
.statusCode(200)
.body(equalTo(String.valueOf(notReadCommentCount)));
.body(equalTo(String.valueOf(NOT_READ_COMMENT_COUNT)));
}

@Test
Expand All @@ -83,15 +83,15 @@ public void beforeEach() {
Comment.deleteAll();
final var user1 = User.findByUsername("user_1");
final var user2 = User.findByUsername("user_2");
range(0, readCommentCount).forEach(i -> dataSeeder.createComment(user2, post, "Comment " + i, false));
range(0, READ_COMMENT_COUNT).forEach(i -> dataSeeder.createComment(user2, post, "Comment " + i, false));
requireNonNull(user1).subscribeTo(post);
final var subscription = Subscription.<Subscription>find("user = ?1 and post = ?2", user1, post)
.firstResult();
subscription.lastCommentSeen = Comment.<Comment>find(
"post", Comment.sorting().descending(), post)
.firstResult();
subscription.persist();
range(0, notReadCommentCount).forEach(i -> dataSeeder.createComment(user2, post, "Comment " + i, false));
range(0, NOT_READ_COMMENT_COUNT).forEach(i -> dataSeeder.createComment(user2, post, "Comment " + i, false));
range(0, 10)
.forEach(i -> dataSeeder.createComment(
user2, Post.find("id != ?1", post.id).firstResult(), "Comment " + i, false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void createCommentOnNonExistentPost() {
final var input = new CommentCreation("Text", false);
given().contentType(ContentType.JSON)
.body(input)
.pathParam("id", fakeId)
.pathParam("id", FAKE_ID)
.post()
.then()
.statusCode(404);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void deleteCommentTooFar() {
@TestSecurity(user = "user_0")
public void deleteCommentOnNonExistentPost() {
final var commentCount = Comment.count();
given().pathParam("id", fakeId).delete(String.valueOf(0)).then().statusCode(404);
given().pathParam("id", FAKE_ID).delete(String.valueOf(0)).then().statusCode(404);
assertEquals(commentCount, Comment.count());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void deleteOtherEmail() {
@TestSecurity(user = "user_0")
public void deleteNonExistentEmail() {
final var emailCount = Email.count();
given().delete(fakeId).then().statusCode(404);
given().delete(FAKE_ID).then().statusCode(404);
assertEquals(emailCount, Email.count());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void setMainWithOtherEmail() {
@Test
@TestSecurity(user = "user_0")
public void setMainWithNonExistentEmail() {
given().put(fakeId + "/main").then().statusCode(404);
given().put(FAKE_ID + "/main").then().statusCode(404);
secondaryEmail = Email.findById(secondaryEmail.id);
assertFalse(secondaryEmail.isMain());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public final class CountPostsTests extends PostTestsBase {
@Inject
DataSeeder dataSeeder;

private static final int publishedPostCount = 10;
private static final int PUBLISHED_POST_COUNT = 10;

private static final int draftPostCount = 6;
private static final int DRAFT_POST_COUNT = 6;

@Test
@TestSecurity(user = "user_0")
Expand All @@ -34,7 +34,7 @@ public void countSubscribedPosts() {
.get("count")
.then()
.statusCode(200)
.body(equalTo(String.valueOf(publishedPostCount)));
.body(equalTo(String.valueOf(PUBLISHED_POST_COUNT)));
}

@Test
Expand All @@ -44,7 +44,7 @@ public void countPublishedPosts() {
.get("count")
.then()
.statusCode(200)
.body(equalTo(String.valueOf(publishedPostCount)));
.body(equalTo(String.valueOf(PUBLISHED_POST_COUNT)));
}

@Test
Expand All @@ -54,7 +54,7 @@ public void countDrafts() {
.get("count")
.then()
.statusCode(200)
.body(equalTo(String.valueOf(draftPostCount)));
.body(equalTo(String.valueOf(DRAFT_POST_COUNT)));
}

@BeforeEach
Expand All @@ -64,8 +64,8 @@ public void beforeEach() {
super.beforeEach();
Post.deleteAll();
final var user = User.findByUsername("user_0");
range(0, publishedPostCount).forEach(i -> dataSeeder.createPost(user, "Post " + i, true, false));
range(0, draftPostCount).forEach(i -> dataSeeder.createPost(user, "Post " + i, false, false));
range(0, PUBLISHED_POST_COUNT).forEach(i -> dataSeeder.createPost(user, "Post " + i, true, false));
range(0, DRAFT_POST_COUNT).forEach(i -> dataSeeder.createPost(user, "Post " + i, false, false));
range(0, 10).forEach(i -> dataSeeder.createPost(User.findByUsername("user_1"), "Post " + i, false, false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import io.quarkus.test.security.TestSecurity;
import io.restassured.http.ContentType;
import jakarta.transaction.Transactional;
import java.time.Duration;
import java.time.Instant;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -65,9 +64,7 @@ public void votePostWithoutSpread() {
public void voteOldPost() {
QuarkusTransaction.requiringNew()
.run(() -> Post.update(
"dateCreated = ?1 where id = ?2",
Instant.now().minus(Post.shelfLife.plus(Duration.ofDays(1))),
post.id));
"dateCreated = ?1 where id = ?2", Instant.now().minus(Post.SHELF_LIFE.plusDays(1)), post.id));
final var voteCount = Vote.count();
final var postLife = post.life;
given().contentType(ContentType.JSON)
Expand All @@ -84,9 +81,7 @@ public void voteOldPost() {
public void voteOldDraft() {
QuarkusTransaction.requiringNew()
.run(() -> Post.update(
"dateCreated = ?1 where id = ?2",
Instant.now().minus(Post.shelfLife.plus(Duration.ofDays(1))),
draft.id));
"dateCreated = ?1 where id = ?2", Instant.now().minus(Post.SHELF_LIFE.plusDays(1)), draft.id));
final var voteCount = Vote.count();
final var postLife = draft.life;
given().contentType(ContentType.JSON)
Expand Down Expand Up @@ -162,7 +157,7 @@ public void voteNonExistentPost() {
final var voteCount = Vote.count();
given().contentType(ContentType.JSON)
.body(new VoteCreation(false))
.post(fakeId + "/vote")
.post(FAKE_ID + "/vote")
.then()
.statusCode(404);
assertEquals(voteCount, Vote.count());
Expand All @@ -183,7 +178,7 @@ public void votePostWhileUnauthenticated() {
final var voteCount = Vote.count();
given().contentType(ContentType.JSON)
.body(new VoteCreation(false))
.post(fakeId + "/vote")
.post(FAKE_ID + "/vote")
.then()
.statusCode(401);
assertEquals(voteCount, Vote.count());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void deleteOtherSubscription() {
@TestSecurity(user = "user_0")
public void deleteNonExistentSubscription() {
final var subscriptionCount = Subscription.count("user.username = 'user_0' and unreadCommentCount > 0");
given().delete(fakeId).then().statusCode(404);
given().delete(FAKE_ID).then().statusCode(404);
assertEquals(subscriptionCount, Subscription.count("user.username = 'user_0' and unreadCommentCount > 0"));
}
}

0 comments on commit 19c323b

Please sign in to comment.