From d70aca0acac1053546f40562ef35ca7f46117c45 Mon Sep 17 00:00:00 2001 From: meliurwen Date: Mon, 13 Jan 2020 00:39:20 +0100 Subject: [PATCH] Aggiunto Item ItemRepo e relativi test --- mivan/src/main/java/mivan/model/Item.java | 61 ++++++++++++++ .../java/mivan/repository/ItemRepository.java | 13 +++ .../mivan/repository/ItemRepositoryImpl.java | 81 +++++++++++++++++++ mivan/src/test/java/mivan/ItemTest.java | 58 +++++++++++++ 4 files changed, 213 insertions(+) create mode 100644 mivan/src/main/java/mivan/model/Item.java create mode 100644 mivan/src/main/java/mivan/repository/ItemRepository.java create mode 100644 mivan/src/main/java/mivan/repository/ItemRepositoryImpl.java create mode 100644 mivan/src/test/java/mivan/ItemTest.java diff --git a/mivan/src/main/java/mivan/model/Item.java b/mivan/src/main/java/mivan/model/Item.java new file mode 100644 index 0000000..856d073 --- /dev/null +++ b/mivan/src/main/java/mivan/model/Item.java @@ -0,0 +1,61 @@ +package mivan.model; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +@Entity +@Table(name="item") +public class Item implements Serializable{ + private static final long serialVersionUID = 1993L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", unique = true) + private long id; + + @ManyToOne(fetch=FetchType.LAZY) + @JoinColumn(name="isbn") + private Book book; + + @ManyToOne(fetch=FetchType.LAZY) + @JoinColumn(name="location") + private Location location; + + public Item () {} + public Item(Book book, Location location){ + super(); + this.book = book; + this.location = location; + } + + public long getId() { + return this.id; + } + + public Book getBook(){ + return this.book; + } + + public void setBook(Book book){ + this.book = book; + } + + public Location getLocation() { + return this.location; + } + + public void setLocation(Location location) { + this.location = location; + } + + +} \ No newline at end of file diff --git a/mivan/src/main/java/mivan/repository/ItemRepository.java b/mivan/src/main/java/mivan/repository/ItemRepository.java new file mode 100644 index 0000000..e011ab2 --- /dev/null +++ b/mivan/src/main/java/mivan/repository/ItemRepository.java @@ -0,0 +1,13 @@ +package mivan.repository; + +import mivan.model.Item; + +public interface ItemRepository extends Repository { + + public void deleteItemById(Long id); + + public void addItem(Item item); + + public int getSize(); + +} \ No newline at end of file diff --git a/mivan/src/main/java/mivan/repository/ItemRepositoryImpl.java b/mivan/src/main/java/mivan/repository/ItemRepositoryImpl.java new file mode 100644 index 0000000..6c89526 --- /dev/null +++ b/mivan/src/main/java/mivan/repository/ItemRepositoryImpl.java @@ -0,0 +1,81 @@ +package mivan.repository; +import java.util.List; +import java.util.Optional; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +import mivan.model.Item; + +public class ItemRepositoryImpl implements ItemRepository { + private EntityManagerFactory entityManagerFactory; + + public ItemRepositoryImpl() { + this.entityManagerFactory = Persistence.createEntityManagerFactory("mivan"); + } + @Override + public Optional findById(Long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + Item item = entityManager.find(Item.class, id); + entityManager.close(); + return Optional.ofNullable(item); + } + + @Override + public Iterable findAll() { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + List items = entityManager.createQuery("FROM Item", Item.class).getResultList(); + entityManager.close(); + return items; + } + + @Override + public void deleteItemById(Long id) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + Item item = entityManager.find(Item.class, id); + entityManager.remove(item); + entityManager.getTransaction().commit(); + entityManager.close(); + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + } + + @Override + public void addItem(Item item) { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + try { + if (!entityManager.getTransaction().isActive()) { + entityManager.getTransaction().begin(); + } + entityManager.persist(item); + entityManager.getTransaction().commit(); + entityManager.close(); + + } catch (Exception ex) { + entityManager.getTransaction().rollback(); + } + + } + + @Override + public int getSize() { + final EntityManager entityManager = this.entityManagerFactory.createEntityManager(); + int size = 0; + try { + size = entityManager.createQuery("FROM Item").getResultList().size(); + + entityManager.close(); + } catch (Exception ex) { + size = 0; + } + return size; + } + + +} \ No newline at end of file diff --git a/mivan/src/test/java/mivan/ItemTest.java b/mivan/src/test/java/mivan/ItemTest.java new file mode 100644 index 0000000..a3e5b8b --- /dev/null +++ b/mivan/src/test/java/mivan/ItemTest.java @@ -0,0 +1,58 @@ +package mivan; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Optional; + +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; + +import mivan.model.Book; +import mivan.model.Item; +import mivan.model.Location; +import mivan.repository.BookRepository; +import mivan.repository.ItemRepository; +import mivan.repository.ItemRepositoryImpl; + +@TestMethodOrder(OrderAnnotation.class) +public class ItemTest { + private ItemRepository itemRepository = new ItemRepositoryImpl(); + + + @Test + @Order(1) + void testAddItem() { + Book book = null; + Location location = null; + Item item = new Item(book, location); + itemRepository.addItem(item); + int size = itemRepository.getSize(); + assertEquals(1, size); + } + + + @Test + @Order(2) + void testRemoveItem() { + int startSize = itemRepository.getSize(); + Book book = null; + Location location = null; + Item item = new Item(book, location); + itemRepository.addItem(item); + int size = itemRepository.getSize(); + assertEquals(startSize + 1, size); + + Optional optItem2 = itemRepository.findById(item.getId()); + + Item item2 = optItem2.get(); + long ID = item2.getId(); + itemRepository.deleteItemById(ID); + + int finalSize = itemRepository.getSize(); + assertEquals(startSize, finalSize); + + + } +} \ No newline at end of file