-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Introduce `UserSettings` Signed-off-by: jaivalis <[email protected]> * test: added some UTs Signed-off-by: jaivalis <[email protected]> * test: added UT Signed-off-by: jaivalis <[email protected]> * fix: smells Signed-off-by: jaivalis <[email protected]> * test: added moderately useful UT Signed-off-by: jaivalis <[email protected]> --------- Signed-off-by: jaivalis <[email protected]>
- Loading branch information
Showing
23 changed files
with
778 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
raccoon-entities/src/main/java/com/raccoon/entity/UserSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.raccoon.entity; | ||
|
||
import java.time.LocalDate; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.OneToOne; | ||
import jakarta.persistence.Table; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import static java.util.Objects.isNull; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@Entity | ||
@Table(name = "UserSettings") | ||
public class UserSettings { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long userSettingId; | ||
|
||
@OneToOne | ||
@JoinColumn(name = "user_id", nullable = false) | ||
private RaccoonUser user; | ||
|
||
@Column(nullable = false) | ||
private Integer notifyIntervalDays = 1; | ||
|
||
private Boolean unsubscribed = Boolean.FALSE; | ||
|
||
public void updateFrom(UserSettings other) { | ||
this.notifyIntervalDays = other.getNotifyIntervalDays(); | ||
this.unsubscribed = other.getUnsubscribed(); | ||
} | ||
|
||
public boolean shouldNotify(LocalDate lastNotified) { | ||
if (unsubscribed) { | ||
return false; | ||
} | ||
if (isNull(lastNotified)) { | ||
return true; | ||
} | ||
return LocalDate.now().isAfter(lastNotified.plusDays(notifyIntervalDays)); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
raccoon-entities/src/main/java/com/raccoon/entity/repository/UserSettingsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.raccoon.entity.repository; | ||
|
||
import com.raccoon.entity.UserSettings; | ||
|
||
import java.util.Optional; | ||
|
||
import io.quarkus.hibernate.orm.panache.PanacheRepository; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
@ApplicationScoped | ||
public class UserSettingsRepository implements PanacheRepository<UserSettings> { | ||
|
||
public Optional<UserSettings> findByUserId(Long userId) { | ||
return find("user.id", userId).stream().findFirst(); | ||
} | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
raccoon-entities/src/test/java/com/raccoon/entity/UserSettingsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.raccoon.entity; | ||
|
||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.LocalDate; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
class UserSettingsTest { | ||
|
||
@Test | ||
void updateFrom_should_updateNotifyIntervalDays() { | ||
UserSettings userSettings = new UserSettings(); | ||
userSettings.setNotifyIntervalDays(5); | ||
UserSettings other = new UserSettings(); | ||
other.setNotifyIntervalDays(10); | ||
other.setUnsubscribed(true); | ||
|
||
userSettings.updateFrom(other); | ||
|
||
assertThat(userSettings.getNotifyIntervalDays()).isEqualTo(10); | ||
assertThat(userSettings.getUnsubscribed()).isTrue(); | ||
} | ||
|
||
@Test | ||
void shouldNotify_should_returnTrue_when_lastNotifiedNull() { | ||
UserSettings userSettings = new UserSettings(); | ||
userSettings.setNotifyIntervalDays(1); | ||
userSettings.setUnsubscribed(false); | ||
|
||
assertThat(userSettings.shouldNotify(null)).isTrue(); | ||
} | ||
|
||
@Test | ||
void shouldNotify_should_returnTrue_when_withinInterval() { | ||
UserSettings userSettings = new UserSettings(); | ||
userSettings.setNotifyIntervalDays(1); | ||
userSettings.setUnsubscribed(false); | ||
LocalDate lastNotified = LocalDate.now().minusDays(2); | ||
|
||
assertThat(userSettings.shouldNotify(lastNotified)).isTrue(); | ||
} | ||
|
||
@Test | ||
void shouldNotify_should_returnTrue_when_outsideInterval() { | ||
UserSettings userSettings = new UserSettings(); | ||
userSettings.setNotifyIntervalDays(1); | ||
userSettings.setUnsubscribed(false); | ||
LocalDate lastNotified = LocalDate.now(); | ||
|
||
assertThat(userSettings.shouldNotify(lastNotified)) | ||
.isFalse(); | ||
} | ||
|
||
@Test | ||
void shouldNotify_should_returnTrue_when_outsideInterval2() { | ||
UserSettings userSettings = new UserSettings(); | ||
userSettings.setNotifyIntervalDays(3); | ||
userSettings.setUnsubscribed(false); | ||
LocalDate lastNotified = LocalDate.now().minusDays(1); | ||
|
||
boolean result = userSettings.shouldNotify(lastNotified); | ||
|
||
assertThat(result).isFalse(); | ||
} | ||
|
||
@Test | ||
void shouldNotify_should_returnFalse_when_unsubscribed() { | ||
UserSettings userSettings = new UserSettings(); | ||
userSettings.setNotifyIntervalDays(1); | ||
userSettings.setUnsubscribed(true); | ||
LocalDate lastNotified = LocalDate.now().minusDays(1); | ||
|
||
boolean result = userSettings.shouldNotify(lastNotified); | ||
|
||
assertThat(result).isFalse(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
raccoon-entities/src/test/java/com/raccoon/entity/repository/UserSettingsRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.raccoon.entity.repository; | ||
|
||
import com.raccoon.entity.RaccoonUser; | ||
import com.raccoon.entity.UserSettings; | ||
import com.raccoon.entity.factory.UserFactory; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import io.quarkus.test.TestTransaction; | ||
import io.quarkus.test.common.WithTestResource; | ||
import io.quarkus.test.h2.H2DatabaseTestResource; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import jakarta.inject.Inject; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@QuarkusTest | ||
@WithTestResource(H2DatabaseTestResource.class) | ||
@TestTransaction | ||
class UserSettingsRepositoryTest { | ||
|
||
@Inject | ||
UserRepository userRepository; | ||
@Inject | ||
UserSettingsRepository userSettingsRepository; | ||
@Inject | ||
UserFactory factory; | ||
|
||
@Test | ||
void findByUserId_should_returnUserSettings_when_userExists() { | ||
RaccoonUser user = factory.createUser("[email protected]"); | ||
UserSettings settings = new UserSettings(); | ||
settings.setUser(user); | ||
userSettingsRepository.persist(settings); | ||
|
||
Optional<UserSettings> result = userSettingsRepository.findByUserId(user.id); | ||
|
||
assertThat(result).isPresent(); | ||
assertThat(result.get().getUser()) | ||
.isEqualTo(user); | ||
} | ||
|
||
@Test | ||
void findByUserId_should_returnEmpty_when_userDoesNotExist() { | ||
RaccoonUser user = factory.createUser("[email protected]"); | ||
userRepository.persist(List.of(user)); | ||
UserSettings settings = new UserSettings(); | ||
settings.setUser(user); | ||
userSettingsRepository.persist(settings); | ||
|
||
Optional<UserSettings> result = userSettingsRepository.findByUserId(user.id + 1); | ||
|
||
assertThat(result).isNotPresent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.