Skip to content
Snippets Groups Projects
Commit 80716008 authored by Birk Øvstetun Narvhus's avatar Birk Øvstetun Narvhus
Browse files

added connection and test

parent db814f5c
No related branches found
No related tags found
No related merge requests found
package ntnu.idatt2016.v233.SmartMat.entity;
package ntnu.idatt2016.v233.SmartMat.entity.product;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToMany;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* Allergy is an entity class representing an allergy
*
* @author Stian Lyng and Anders
* @version 1.1.001
* @version 1.2
* @since 19.04.2023
*
*/
......@@ -29,4 +33,8 @@ public class Allergy{
String name;
@Column(name = "allergy_description")
String description;
@ManyToMany(mappedBy = "allergies")
@JsonIgnoreProperties({"allergies"})
private List<Product> products;
}
\ No newline at end of file
package ntnu.idatt2016.v233.SmartMat.entity.product;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
......@@ -21,5 +22,6 @@ public class Category {
String description;
@OneToMany(mappedBy = "category")
@JsonIgnoreProperties({"category"})
private List<Product> products;
}
\ No newline at end of file
package ntnu.idatt2016.v233.SmartMat.entity.product;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
......@@ -7,6 +8,8 @@ import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* Product is an entity class representing a product in the system.
* All other info about the product is fetched from api call on fronted.
......@@ -42,4 +45,13 @@ public class Product{
@Column(name = "expiration_date")
int expirationDate;
@ManyToMany
@JsonIgnoreProperties({"products"})
@JoinTable(
name = "product_allergy",
joinColumns = @JoinColumn(name = "ean"),
inverseJoinColumns = @JoinColumn(name = "allergy_name"))
List<Allergy> allergies;
}
package ntnu.idatt2016.v233.SmartMat.repository;
import ntnu.idatt2016.v233.SmartMat.entity.Allergy;
import ntnu.idatt2016.v233.SmartMat.entity.product.Allergy;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
/**
* Repository for allergies
*
* @author Stian Lyng
* @author Stian Lyng and birk
* @version 1.2
* @since 19.04.2023
* @since 21.04.2023
*/
public interface AllergyRepository extends JpaRepository<Allergy, String> {
/**
* Finds all allergies by product id
* @param id the id of the product
* @return list of allergies
*/
List<Allergy> findAllByProductsEan(long id);
}
package ntnu.idatt2016.v233.SmartMat.repository;
import ntnu.idatt2016.v233.SmartMat.entity.Allergy;
import ntnu.idatt2016.v233.SmartMat.entity.product.Allergy;
import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
......@@ -17,6 +18,9 @@ class AllergyRepositoryTest {
@Autowired
private AllergyRepository allergyRepository;
@Autowired
private ProductRepository productRepository;
@Test
@DisplayName("Test findByName")
void testFindByName() {
......@@ -85,4 +89,33 @@ class AllergyRepositoryTest {
Assertions.assertTrue(allergies.contains(allergy1));
Assertions.assertTrue(allergies.contains(allergy2));
}
@Test
void findByProductId() {
Allergy allergy = Allergy.builder()
.name("testName")
.description("testDescription")
.build();
Product product = Product.builder()
.ean(1234567890123L)
.name("Test Product")
.description("This is a test product")
.allergies(List.of(allergy))
.build();
allergyRepository.save(allergy);
productRepository.save(product);
Assertions.assertNotNull(allergyRepository.findAllByProductsEan(product.getEan()));
allergyRepository.findAllByProductsEan(product.getEan()).forEach(allergy1 -> {
Assertions.assertNull(allergy1.getProducts());
});
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment