From 9627e748850010f2ebb172f520dd32e5ba16c184 Mon Sep 17 00:00:00 2001 From: Jessica Date: Mon, 27 May 2024 23:32:08 +0200 Subject: [PATCH] Add dataaccess layer --- web_backend/pom.xml | 51 ++++++-- .../device/dataaccess/model/DeviceEntity.java | 53 ++++++++ .../device/dataaccess/model/DeviceType.java | 11 ++ .../repository/DeviceRepository.java | 9 ++ .../backend/device/logic/DeviceService.java | 30 +++++ .../device/logic/DeviceServiceImpl.java | 33 +++++ .../pet/dataaccess/model/PetEntity.java | 91 +++++++++++++ .../pet/dataaccess/model/PetTypeEntity.java | 29 +++++ .../dataaccess/repository/PetRepository.java | 10 ++ .../repository/PetTypeRepository.java | 11 ++ .../backend/pet/logic/PetService.java | 15 +++ .../backend/pet/logic/PetServiceImpl.java | 27 ++++ .../backend/pet/logic/PetTypeService.java | 14 ++ .../backend/pet/logic/PetTypeServiceImpl.java | 19 +++ .../user/dataaccess/model/UserEntity.java | 66 ++++++++++ .../dataaccess/repository/UserRepository.java | 10 ++ .../backend/user/logic/UserService.java | 16 +++ .../backend/user/logic/UserServiceImpl.java | 20 +++ .../src/main/resources/application.properties | 17 +++ .../device/logic/DeviceServiceTest.java | 37 ++++++ .../pet/repository/PetRepositoryTest.java | 82 ++++++++++++ .../backend/user/UserRepositoryTest.java | 122 ++++++++++++++++++ .../src/test/resources/application.properties | 11 ++ 23 files changed, 773 insertions(+), 11 deletions(-) create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/device/dataaccess/model/DeviceEntity.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/device/dataaccess/model/DeviceType.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/device/dataaccess/repository/DeviceRepository.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/device/logic/DeviceService.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/device/logic/DeviceServiceImpl.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/model/PetEntity.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/model/PetTypeEntity.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/repository/PetRepository.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/repository/PetTypeRepository.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/pet/logic/PetService.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/pet/logic/PetServiceImpl.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/pet/logic/PetTypeService.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/pet/logic/PetTypeServiceImpl.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/user/dataaccess/model/UserEntity.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/user/dataaccess/repository/UserRepository.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/user/logic/UserService.java create mode 100644 web_backend/src/main/java/haw/teamagochi/backend/user/logic/UserServiceImpl.java create mode 100644 web_backend/src/test/java/haw/teamagochi/backend/device/logic/DeviceServiceTest.java create mode 100644 web_backend/src/test/java/haw/teamagochi/backend/pet/repository/PetRepositoryTest.java create mode 100644 web_backend/src/test/java/haw/teamagochi/backend/user/UserRepositoryTest.java create mode 100644 web_backend/src/test/resources/application.properties diff --git a/web_backend/pom.xml b/web_backend/pom.xml index 6302cb7b..85474830 100644 --- a/web_backend/pom.xml +++ b/web_backend/pom.xml @@ -9,6 +9,9 @@ 3.3.1 10.13.0 3.12.1 + 3.0.2 + 3.1.0 + 1.18.32 21 UTF-8 UTF-8 @@ -17,7 +20,6 @@ 3.9.4 true 3.2.5 - 3.1.0 @@ -39,20 +41,47 @@ io.quarkus quarkus-rest-client-jackson + + io.quarkus + quarkus-hibernate-orm-panache + + + io.quarkus + quarkus-jdbc-postgresql + + + io.quarkus + quarkus-jdbc-h2 + io.quarkus quarkus-junit5 test + + io.quarkus + quarkus-test-h2 + test + io.rest-assured rest-assured test - jakarta.ws.rs - jakarta.ws.rs-api - ${jakarta.ws.rs.version} + jakarta.ws.rs + jakarta.ws.rs-api + ${jakarta.ws.rs.version} + + + jakarta.validation + jakarta.validation-api + ${jakarta.validation.version} + + + org.projectlombok + lombok + ${lombok.version} @@ -134,6 +163,13 @@ org.jsonschema2pojo jsonschema2pojo-maven-plugin 1.2.1 + + + + generate + + + src/main/resources/jsonschema/events haw.teamagochi.backend.leshanclient.sse @@ -145,13 +181,6 @@ false false - - - - generate - - - diff --git a/web_backend/src/main/java/haw/teamagochi/backend/device/dataaccess/model/DeviceEntity.java b/web_backend/src/main/java/haw/teamagochi/backend/device/dataaccess/model/DeviceEntity.java new file mode 100644 index 00000000..344ce6d6 --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/device/dataaccess/model/DeviceEntity.java @@ -0,0 +1,53 @@ +package haw.teamagochi.backend.device.dataaccess.model; +import io.quarkus.hibernate.orm.panache.PanacheEntity; +import jakarta.persistence.Embedded; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.validation.constraints.Size; +import java.util.Objects; +import lombok.Getter; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@RequiredArgsConstructor +@Entity +public class DeviceEntity { + + // oh nooo, I changed smt. Plz recompile + + public DeviceEntity() {} + + @Id + @GeneratedValue + private long id; + + @NonNull + @Size(max = 255) + private String name; + + @Embedded + @NonNull + private DeviceType deviceType; + + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + DeviceEntity device = (DeviceEntity) o; + return id == device.id && name.equals(device.name) && deviceType == device.deviceType; + } + + @Override + public int hashCode() { + return Objects.hash(id, name, deviceType); + } +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/device/dataaccess/model/DeviceType.java b/web_backend/src/main/java/haw/teamagochi/backend/device/dataaccess/model/DeviceType.java new file mode 100644 index 00000000..603aa91a --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/device/dataaccess/model/DeviceType.java @@ -0,0 +1,11 @@ +package haw.teamagochi.backend.device.dataaccess.model; + +import jakarta.persistence.Embeddable; + +@Embeddable +public enum DeviceType { + FROG; + + DeviceType() { + } +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/device/dataaccess/repository/DeviceRepository.java b/web_backend/src/main/java/haw/teamagochi/backend/device/dataaccess/repository/DeviceRepository.java new file mode 100644 index 00000000..d61706c5 --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/device/dataaccess/repository/DeviceRepository.java @@ -0,0 +1,9 @@ +package haw.teamagochi.backend.device.dataaccess.repository; + +import haw.teamagochi.backend.device.dataaccess.model.DeviceEntity; +import io.quarkus.hibernate.orm.panache.PanacheRepository; +import jakarta.enterprise.context.ApplicationScoped; + +@ApplicationScoped +public class DeviceRepository implements PanacheRepository { +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/device/logic/DeviceService.java b/web_backend/src/main/java/haw/teamagochi/backend/device/logic/DeviceService.java new file mode 100644 index 00000000..6c0ce9d8 --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/device/logic/DeviceService.java @@ -0,0 +1,30 @@ +package haw.teamagochi.backend.device.logic; + + +import haw.teamagochi.backend.device.dataaccess.model.DeviceEntity; +import haw.teamagochi.backend.device.dataaccess.model.DeviceType; + +public interface DeviceService { + + /** + * Creates a device and saves it persistently in the database. + * Any future changes to the device object's attributes will be updated automatically in the database. + * @param name name of the device + * @param deviceType device type + * @return persisted device object + */ + DeviceEntity createDevice(String name, DeviceType deviceType); + + /** + * Returns whether device exists in the database. + * @param id id of the device + * @return whether device was found + */ + boolean deviceExists(long id); + + /** + * Deletes all devices from the database. + */ + void deleteAll(); + +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/device/logic/DeviceServiceImpl.java b/web_backend/src/main/java/haw/teamagochi/backend/device/logic/DeviceServiceImpl.java new file mode 100644 index 00000000..4cb8b352 --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/device/logic/DeviceServiceImpl.java @@ -0,0 +1,33 @@ +package haw.teamagochi.backend.device.logic; + +import haw.teamagochi.backend.device.dataaccess.model.DeviceEntity; +import haw.teamagochi.backend.device.dataaccess.model.DeviceType; +import haw.teamagochi.backend.device.dataaccess.repository.DeviceRepository; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; + +@ApplicationScoped +public class DeviceServiceImpl implements DeviceService { + + @Inject + DeviceRepository deviceRepository; + + @Override + public DeviceEntity createDevice(String name, DeviceType deviceType) { + DeviceEntity device = new DeviceEntity(name, deviceType); + deviceRepository.persist(device); + return device; + } + + @Override + public boolean deviceExists(long id) { + DeviceEntity device = deviceRepository.findById(id); + return device != null; + } + + @Override + public void deleteAll() { + deviceRepository.deleteAll(); + } + +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/model/PetEntity.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/model/PetEntity.java new file mode 100644 index 00000000..a65faa1c --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/model/PetEntity.java @@ -0,0 +1,91 @@ +package haw.teamagochi.backend.pet.dataaccess.model; + +import haw.teamagochi.backend.device.dataaccess.model.DeviceEntity; +import io.quarkus.hibernate.orm.panache.PanacheEntity; +import jakarta.annotation.Nullable; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToOne; +import jakarta.validation.constraints.Past; +import jakarta.validation.constraints.Pattern; +import jakarta.validation.constraints.PositiveOrZero; +import jakarta.validation.constraints.Size; +import java.sql.Date; +import java.util.Objects; +import lombok.Getter; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@RequiredArgsConstructor +@Entity +public class PetEntity { + + @Id + @GeneratedValue + private long id; + + public PetEntity() {} + + + @NonNull + @Size(max = 255) + private String name; + + + /* + @NonNull + @Pattern(regexp = "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$") // source: https://www.geeksforgeeks.org/how-to-validate-hexadecimal-color-code-using-regular-expression/ + private String color; + */ + + private int happiness = 0; + private int wellbeing = 0; + private int health = 0; + private int hunger = 0; + private int cleanliness = 0; + private int fun = 0; + + @PositiveOrZero + private int xp = 0; + + @Nullable + @Past + private Date lastTimeOnDevice; + + + @ManyToOne + @NonNull + private PetTypeEntity petType; + + + @OneToOne + @Nullable + private DeviceEntity device; + + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + PetEntity petEntity = (PetEntity) o; + return id == petEntity.id; + } + + @Override + public int hashCode() { + return Objects.hash(id); + } + + + +} + diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/model/PetTypeEntity.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/model/PetTypeEntity.java new file mode 100644 index 00000000..c24700b3 --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/model/PetTypeEntity.java @@ -0,0 +1,29 @@ +package haw.teamagochi.backend.pet.dataaccess.model; +import io.quarkus.hibernate.orm.panache.PanacheEntity; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.validation.constraints.Size; +import lombok.Getter; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@RequiredArgsConstructor +@Entity +public class PetTypeEntity { + + @Id + @GeneratedValue + private long id; + + public PetTypeEntity() {} + + @NonNull + @Size(max = 255) + private String name; + + +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/repository/PetRepository.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/repository/PetRepository.java new file mode 100644 index 00000000..e50403b5 --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/repository/PetRepository.java @@ -0,0 +1,10 @@ +package haw.teamagochi.backend.pet.dataaccess.repository; + +import haw.teamagochi.backend.pet.dataaccess.model.PetEntity; +import io.quarkus.hibernate.orm.panache.PanacheRepository; +import jakarta.enterprise.context.ApplicationScoped; + +@ApplicationScoped +public class PetRepository implements PanacheRepository { + +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/repository/PetTypeRepository.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/repository/PetTypeRepository.java new file mode 100644 index 00000000..2d884fbd --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/dataaccess/repository/PetTypeRepository.java @@ -0,0 +1,11 @@ +package haw.teamagochi.backend.pet.dataaccess.repository; + + +import haw.teamagochi.backend.pet.dataaccess.model.PetTypeEntity; +import io.quarkus.hibernate.orm.panache.PanacheRepository; +import jakarta.enterprise.context.ApplicationScoped; + +@ApplicationScoped +public class PetTypeRepository implements PanacheRepository { + +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/PetService.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/PetService.java new file mode 100644 index 00000000..cd94dc60 --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/PetService.java @@ -0,0 +1,15 @@ +package haw.teamagochi.backend.pet.logic; + +import haw.teamagochi.backend.pet.dataaccess.model.PetEntity; +import haw.teamagochi.backend.pet.dataaccess.model.PetTypeEntity; + +public interface PetService { + /** + * Creates a pet and saves it persistently in the database. + * Any future changes to the pet object's attributes will be updated automatically in the database. + * @param name name for the pet + * @param petType pet type of the pet + * @return persisted pet object + */ + PetEntity createPet(String name, PetTypeEntity petType); +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/PetServiceImpl.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/PetServiceImpl.java new file mode 100644 index 00000000..6d5b5c88 --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/PetServiceImpl.java @@ -0,0 +1,27 @@ +package haw.teamagochi.backend.pet.logic; + +import haw.teamagochi.backend.pet.dataaccess.model.PetEntity; +import haw.teamagochi.backend.pet.dataaccess.model.PetTypeEntity; +import haw.teamagochi.backend.pet.dataaccess.repository.PetRepository; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; + +@ApplicationScoped +public class PetServiceImpl implements PetService { + + @Inject + PetRepository petRepository; + + /** + * Creates a pet and saves it persistently. + * @param name Name of the pet + * @param petType The pet type of the pet. Needs to have been persisted before calling this method. + * @return A pet object with the given attributes. + */ + public PetEntity createPet(String name, PetTypeEntity petType) { + PetEntity pet = new PetEntity(name, petType); + petRepository.persist(pet); + return pet; + } + +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/PetTypeService.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/PetTypeService.java new file mode 100644 index 00000000..ae96667a --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/PetTypeService.java @@ -0,0 +1,14 @@ +package haw.teamagochi.backend.pet.logic; + +import haw.teamagochi.backend.pet.dataaccess.model.PetTypeEntity; + +public interface PetTypeService { + + /** + * Creates a pet type and saves it persistently in the database. + * Any future changes to the pet type object's attributes will be updated automatically in the database. + * @param name name for the pet type + * @return persisted pet type object + */ + PetTypeEntity createPetType(String name); +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/PetTypeServiceImpl.java b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/PetTypeServiceImpl.java new file mode 100644 index 00000000..ad789f53 --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/pet/logic/PetTypeServiceImpl.java @@ -0,0 +1,19 @@ +package haw.teamagochi.backend.pet.logic; + +import haw.teamagochi.backend.pet.dataaccess.model.PetTypeEntity; +import haw.teamagochi.backend.pet.dataaccess.repository.PetTypeRepository; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; + +@ApplicationScoped +public class PetTypeServiceImpl implements PetTypeService { + + @Inject + PetTypeRepository petTypeRepository; + + public PetTypeEntity createPetType(String name) { + PetTypeEntity petType = new PetTypeEntity(name); + petTypeRepository.persist(petType); + return petType; + } +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/user/dataaccess/model/UserEntity.java b/web_backend/src/main/java/haw/teamagochi/backend/user/dataaccess/model/UserEntity.java new file mode 100644 index 00000000..fd3403bc --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/user/dataaccess/model/UserEntity.java @@ -0,0 +1,66 @@ +package haw.teamagochi.backend.user.dataaccess.model; + +import haw.teamagochi.backend.device.dataaccess.model.DeviceEntity; +import haw.teamagochi.backend.pet.dataaccess.model.PetEntity; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; +import jakarta.validation.Valid; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import lombok.Getter; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@RequiredArgsConstructor +@Entity +public class UserEntity { + + @Id + @GeneratedValue + private long id; + + public UserEntity() {} + + @NonNull + @Column(unique = true) + private UUID externalID; + + @OneToMany + private List pets = new ArrayList<>(); + + @OneToMany + private List devices = new ArrayList<>(); + + /** + * Adds a pet to the user entity. + * This method does not guarantee that the user pets are up-to-date in the database. + * @param pet must not be null and must not violate any database constraints + * @throws IllegalArgumentException + */ + public void addPet(@Valid PetEntity pet) { + if (pet == null) throw new IllegalArgumentException("Pet must not be null."); + this.pets.add(pet); + } + + + + /** + * Adds a device to the user entity. + * This method does not guarantee that the user devices are up-to-date in the database. + * @param device must not be null and must not violate any database constraints + * @throws IllegalArgumentException + */ + public void addDevice(@Valid DeviceEntity device) { + if (device == null) throw new IllegalArgumentException("Device must not be null."); + this.devices.add(device); + } + + +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/user/dataaccess/repository/UserRepository.java b/web_backend/src/main/java/haw/teamagochi/backend/user/dataaccess/repository/UserRepository.java new file mode 100644 index 00000000..65176812 --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/user/dataaccess/repository/UserRepository.java @@ -0,0 +1,10 @@ +package haw.teamagochi.backend.user.dataaccess.repository; + +import haw.teamagochi.backend.user.dataaccess.model.UserEntity; +import io.quarkus.hibernate.orm.panache.PanacheRepository; +import jakarta.enterprise.context.ApplicationScoped; + +@ApplicationScoped +public class UserRepository implements PanacheRepository { + +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/user/logic/UserService.java b/web_backend/src/main/java/haw/teamagochi/backend/user/logic/UserService.java new file mode 100644 index 00000000..c598c48b --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/user/logic/UserService.java @@ -0,0 +1,16 @@ +package haw.teamagochi.backend.user.logic; + +import haw.teamagochi.backend.user.dataaccess.model.UserEntity; +import java.util.UUID; + +public interface UserService { + + /** + * Creates a user and saves it persistently in the database. + * Any future changes to the user object's attributes will be updated automatically in the database. + * @param uuid the UUID for the user + * @return persisted user object + */ + UserEntity createUser(UUID uuid); + +} diff --git a/web_backend/src/main/java/haw/teamagochi/backend/user/logic/UserServiceImpl.java b/web_backend/src/main/java/haw/teamagochi/backend/user/logic/UserServiceImpl.java new file mode 100644 index 00000000..41a30438 --- /dev/null +++ b/web_backend/src/main/java/haw/teamagochi/backend/user/logic/UserServiceImpl.java @@ -0,0 +1,20 @@ +package haw.teamagochi.backend.user.logic; + +import haw.teamagochi.backend.user.dataaccess.model.UserEntity; +import haw.teamagochi.backend.user.dataaccess.repository.UserRepository; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import java.util.UUID; + +@ApplicationScoped +public class UserServiceImpl implements UserService{ + + @Inject + UserRepository userRepository; + + public UserEntity createUser(UUID uuid) { + UserEntity user = new UserEntity(); + userRepository.persist(user); + return user; + } +} diff --git a/web_backend/src/main/resources/application.properties b/web_backend/src/main/resources/application.properties index e2a9be2e..ead8f65f 100644 --- a/web_backend/src/main/resources/application.properties +++ b/web_backend/src/main/resources/application.properties @@ -2,3 +2,20 @@ quarkus.rest-client.leshan-event-api.url=https://leshan.eclipseprojects.io/api quarkus.rest-client.leshan-event-api.verify-host=false quarkus.rest-client.leshan-event-api.http2=true + + +# drop and create the database at startup (use `update` to only update the schema) +quarkus.hibernate-orm.database.generation= drop-and-create + +# datasource configuration +quarkus.datasource.devservices.enabled=false + +quarkus.datasource.db-kind = h2 +quarkus.datasource.username = "sa" +quarkus.datasource.password = "" +quarkus.datasource.jdbc.url = jdbc:h2:~/test + +# quarkus.datasource.db-kind = postgresql +# quarkus.datasource.username = sa +# quarkus.datasource.password = sa +# quarkus.datasource.jdbc.url = jdbc:postgresql://localhost:5432/ \ No newline at end of file diff --git a/web_backend/src/test/java/haw/teamagochi/backend/device/logic/DeviceServiceTest.java b/web_backend/src/test/java/haw/teamagochi/backend/device/logic/DeviceServiceTest.java new file mode 100644 index 00000000..b8d524fe --- /dev/null +++ b/web_backend/src/test/java/haw/teamagochi/backend/device/logic/DeviceServiceTest.java @@ -0,0 +1,37 @@ +package haw.teamagochi.backend.device.logic; + +import haw.teamagochi.backend.device.dataaccess.model.DeviceEntity; +import haw.teamagochi.backend.device.dataaccess.model.DeviceType; +import io.quarkus.test.common.QuarkusTestResource; +import io.quarkus.test.h2.H2DatabaseTestResource; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import jakarta.transaction.Transactional; +import jakarta.validation.ConstraintViolationException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +@QuarkusTest +@QuarkusTestResource(H2DatabaseTestResource.class) +public class DeviceServiceTest { + + @Inject + DeviceService deviceService; + + @BeforeEach + @Transactional + public void beforeEach() { + deviceService.deleteAll(); + } + + @Test + @Transactional + public void testConstraint() { + DeviceEntity device = deviceService.createDevice("name", DeviceType.FROG); + Assertions.assertThrows(ConstraintViolationException.class, ()-> { + device.setName(null); + }); + + } +} diff --git a/web_backend/src/test/java/haw/teamagochi/backend/pet/repository/PetRepositoryTest.java b/web_backend/src/test/java/haw/teamagochi/backend/pet/repository/PetRepositoryTest.java new file mode 100644 index 00000000..8b55467a --- /dev/null +++ b/web_backend/src/test/java/haw/teamagochi/backend/pet/repository/PetRepositoryTest.java @@ -0,0 +1,82 @@ +package haw.teamagochi.backend.pet.repository; + +import haw.teamagochi.backend.pet.dataaccess.model.PetEntity; +import haw.teamagochi.backend.pet.dataaccess.model.PetTypeEntity; +import haw.teamagochi.backend.pet.dataaccess.repository.PetRepository; +import haw.teamagochi.backend.pet.dataaccess.repository.PetTypeRepository; +import io.quarkus.test.common.QuarkusTestResource; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import jakarta.transaction.Transactional; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; +import io.quarkus.test.h2.H2DatabaseTestResource; + +@QuarkusTest +@QuarkusTestResource(H2DatabaseTestResource.class) + +public class PetRepositoryTest { + + @Inject + PetRepository petRepository; + + @Inject + PetTypeRepository petTypeRepository; + + private static PetEntity defaultPet; + private static PetTypeEntity defaultPetType; + + @BeforeAll + public static void beforeAll() { + defaultPetType = new PetTypeEntity(); + defaultPet = new PetEntity( + "petname", + //"fakecolor", + defaultPetType + ); + } + + + @BeforeEach + @Transactional + public void beforeEach() { + + // Deletion order important? + petRepository.deleteAll(); + petTypeRepository.deleteAll(); + + + } + + + /* + @Test + public void testColorRegex() { + Assertions.assertThrows(Exception.class, ()-> defaultPet.setColor("wrong_color_syntax")); + } + */ + + + @Test + @Transactional + public void testHibernatePersistence() { + + petTypeRepository.persist(defaultPetType); + petRepository.persist(defaultPet); + + String oldName = defaultPet.getName(); + String newName = "new pet name"; + + defaultPet.setName(newName); + PetEntity persistedPet = petRepository.findById(defaultPet.getId()); + + Assertions.assertEquals(newName, persistedPet.getName()); + } + + + + + +} diff --git a/web_backend/src/test/java/haw/teamagochi/backend/user/UserRepositoryTest.java b/web_backend/src/test/java/haw/teamagochi/backend/user/UserRepositoryTest.java new file mode 100644 index 00000000..46e419e7 --- /dev/null +++ b/web_backend/src/test/java/haw/teamagochi/backend/user/UserRepositoryTest.java @@ -0,0 +1,122 @@ +package haw.teamagochi.backend.user; + +import haw.teamagochi.backend.device.dataaccess.model.DeviceEntity; +import haw.teamagochi.backend.device.dataaccess.model.DeviceType; +import haw.teamagochi.backend.device.logic.DeviceService; +import haw.teamagochi.backend.pet.dataaccess.model.PetEntity; +import haw.teamagochi.backend.pet.dataaccess.model.PetTypeEntity; +import haw.teamagochi.backend.user.dataaccess.model.UserEntity; +import haw.teamagochi.backend.device.dataaccess.repository.DeviceRepository; +import haw.teamagochi.backend.pet.dataaccess.repository.PetRepository; +import haw.teamagochi.backend.pet.dataaccess.repository.PetTypeRepository; +import haw.teamagochi.backend.user.dataaccess.repository.UserRepository; +import haw.teamagochi.backend.device.logic.DeviceServiceImpl; +import haw.teamagochi.backend.user.logic.UserService; +import haw.teamagochi.backend.user.logic.UserServiceImpl; +import io.quarkus.test.common.QuarkusTestResource; +import io.quarkus.test.h2.H2DatabaseTestResource; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import jakarta.transaction.Transactional; +import java.util.UUID; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +@QuarkusTest +@QuarkusTestResource(H2DatabaseTestResource.class) +public class UserRepositoryTest { + + @Inject + UserService userService; + + @Inject + DeviceService deviceService; + + @Inject + UserRepository userRepository; + + @Inject + PetTypeRepository petTypeRepository; + + @Inject + PetRepository petRepository; + + @Inject + DeviceRepository deviceRepository; + + private static UserEntity defaultUser; + + @BeforeAll + public static void beforeAll() { + UUID uuid = new UUID(1,1); + defaultUser = new UserEntity( + uuid + ); + } + + @BeforeEach + @Transactional + public void beforeEach() { + userRepository.deleteAll(); + deviceRepository.deleteAll(); + userRepository.deleteAll(); + } + + @Test + @Transactional + public void testRepositoryAccess() { + Assertions.assertDoesNotThrow(() -> { + + // create entities + PetTypeEntity petType = new PetTypeEntity(); + PetEntity pet = new PetEntity( + "petname", + //"wrong_color_syntax", + petType); + DeviceEntity device = new DeviceEntity(); + + // persist entities + petTypeRepository.persist(petType); + petRepository.persist(pet); + deviceRepository.persist(device); + + defaultUser.addPet(pet); + defaultUser.addDevice(device); + + userRepository.persist(defaultUser); + }); + + } + + @Test + @Transactional + public void testHibernatePersistenceAcrossMethodCalls() { + UUID uuid1 = new UUID(1,1); + UUID uuid2 = new UUID(2,2); + + UserEntity user = userService.createUser(uuid1); + user.setExternalID(uuid2); + + UserEntity loadedUser = userRepository.findById(user.getId()); + + Assertions.assertEquals(user, loadedUser); + } + + @Test + @Transactional + public void testListAttributePersistence() { + UUID uuid1 = new UUID(1,1); + + UserEntity user = userService.createUser(uuid1); + DeviceEntity device = deviceService.createDevice("name", DeviceType.FROG); + user.addDevice(device); + + UserEntity loadedUser = userRepository.findById(user.getId()); + + + Assertions.assertEquals(user.getDevices(), loadedUser.getDevices()); + } + +} diff --git a/web_backend/src/test/resources/application.properties b/web_backend/src/test/resources/application.properties new file mode 100644 index 00000000..f4ef6cb3 --- /dev/null +++ b/web_backend/src/test/resources/application.properties @@ -0,0 +1,11 @@ +# drop and create the database at startup (use `update` to only update the schema) +quarkus.hibernate-orm.database.generation= drop-and-create + +# datasource configuration +quarkus.datasource.devservices.enabled=false +quarkus.datasource.db-kind = h2 +quarkus.datasource.username = sa +quarkus.datasource.password = +quarkus.datasource.jdbc.url = jdbc:h2:~/test + +