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

Merge branch 'main' into 'bugfix/104-fix-allergy-item-connection-in-jpa'

Main

See merge request idatt2106-v23-03/backend!66
parents 50346448 24ab6b96
No related branches found
No related tags found
No related merge requests found
......@@ -38,7 +38,6 @@ public class CorsConfig {
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins(domainProperty.domain())
.allowedOrigins("https://smartmat.app")
.allowedMethods(Arrays.asList(
HttpMethod.GET.name(),
HttpMethod.POST.name(),
......
package ntnu.idatt2016.v233.SmartMat.entity.product;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@NoArgsConstructor
@AllArgsConstructor
@Builder
......@@ -19,4 +19,7 @@ public class Category {
String categoryName;
@Column(name = "category_description")
String description;
@OneToMany(mappedBy = "category")
private List<Product> products;
}
\ No newline at end of file
package ntnu.idatt2016.v233.SmartMat.entity.product;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
......@@ -30,8 +29,10 @@ public class Product{
@Column(name = "description")
String description;
@Column(name ="category_name")
String categoryName;
@ManyToOne
@JoinColumn(name = "category_name")
@JsonIgnoreProperties({"products"})
Category category;
@Column(name = "image_url")
String url;
......
package ntnu.idatt2016.v233.SmartMat.repository;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.List;
import java.util.Optional;
import ntnu.idatt2016.v233.SmartMat.entity.product.Category;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
......@@ -17,6 +13,8 @@ import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import ntnu.idatt2016.v233.SmartMat.entity.product.Product;
import static org.junit.jupiter.api.Assertions.*;
@DataJpaTest
public class ProductRepositoryTest {
......@@ -34,7 +32,6 @@ public class ProductRepositoryTest {
.ean(1234567890123L)
.name("Test Product")
.description("This is a test product")
.categoryName("TestCategory")
.build();
entityManager.persist(product);
}
......@@ -69,7 +66,6 @@ public class ProductRepositoryTest {
.ean(1234567890124L)
.name("New Product")
.description("This is a new product")
.categoryName("TestCategory")
.build();
productRepository.save(newProduct);
List<Product> products = productRepository.findAll();
......@@ -86,4 +82,25 @@ public class ProductRepositoryTest {
assertEquals(0, products.size());
}
@Test
void CategoryShouldBeSetCorrecltly() {
Product product = new Product();
Category category = Category.builder()
.categoryName("Test5")
.description("Test Description")
.build();
product.setCategory(category);
productRepository.save(product);
assertNotNull(product.getCategory());
Optional<Product> tempProduct = productRepository.findById(product.getEan());
assertTrue(tempProduct.isPresent());
assertNotNull(tempProduct.get().getCategory());
assertNull(tempProduct.get().getCategory().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