diff --git a/core/database.mv.db b/core/database.mv.db index ca641dab7b3779b5718bac20e7526a27f31ae4fa..6a79fd6fa0459afb5b4d59b08e63d215d31e2170 100644 Binary files a/core/database.mv.db and b/core/database.mv.db differ diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Model/Map/Overworld.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Model/Map/Overworld.java index ebde8497d8ba6bc99209fb211cd632bb2e843257..1462415c4b55098d314777cc5fe1c89e49cdebec 100644 --- a/core/src/com/galaxytrucker/galaxytruckerreloaded/Model/Map/Overworld.java +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Model/Map/Overworld.java @@ -1,10 +1,7 @@ package com.galaxytrucker.galaxytruckerreloaded.Model.Map; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; +import lombok.*; import javax.persistence.*; import java.io.Serializable; @@ -17,6 +14,8 @@ import java.util.Map; @NamedQueries({ @NamedQuery(name = "Overworld.getByUsername", query = "select o from Overworld o where o.associatedUser =: name") }) +@Getter +@Setter public class Overworld implements Serializable { /** ID */ @@ -24,21 +23,19 @@ public class Overworld implements Serializable { @Id private int id; + /** Seed */ + @NonNull + private int seed; + /** Username used as ID */ @NonNull private String associatedUser; /** Stores planet and their location on the map */ - @OneToMany + @OneToMany(cascade = CascadeType.ALL) private Map<String,Planet> planetMap; /** The start planet */ - @NonNull - @OneToOne + @OneToOne(cascade = CascadeType.ALL) private Planet startPlanet; - - /** Constructor */ - public Overworld(int seed) { - planetMap = new HashMap<>(); - } } diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Test/Server/Persistence/CrewDAOTest.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Test/Server/Persistence/CrewDAOTest.java index 4d91f5c23833197c81865562a46bddb020b053b6..943f53ae70714319c137694d68f216dcba233268 100644 --- a/core/src/com/galaxytrucker/galaxytruckerreloaded/Test/Server/Persistence/CrewDAOTest.java +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Test/Server/Persistence/CrewDAOTest.java @@ -12,6 +12,7 @@ import javax.persistence.EntityManager; import javax.xml.crypto.Data; import java.util.UUID; +/** Test the crew database access object */ public class CrewDAOTest { diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Test/Server/Persistence/OverworldDAOTest.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Test/Server/Persistence/OverworldDAOTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f0688aa00bc5f830b3ee2a5907a4027af4bf59d6 --- /dev/null +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Test/Server/Persistence/OverworldDAOTest.java @@ -0,0 +1,54 @@ +package com.galaxytrucker.galaxytruckerreloaded.Test.Server.Persistence; + +import com.galaxytrucker.galaxytruckerreloaded.Model.Map.Overworld; +import com.galaxytrucker.galaxytruckerreloaded.Server.Database.Database; +import com.galaxytrucker.galaxytruckerreloaded.Server.Persistence.OverworldDAO; +import org.junit.Assert; +import org.junit.Test; + +import javax.persistence.EntityManager; +import java.util.UUID; + +/** Test the overworld database access object */ +public class OverworldDAOTest { + + /** EntityManager */ + private EntityManager entityManager = Database.getEntityManager(); + + /** OverworldDAO */ + private OverworldDAO overworldDAO = new OverworldDAO(); + + /** Test adding a new overworld to the database */ + @Test + public void testPersist() { + try { + Overworld overworld = new Overworld(UUID.randomUUID().hashCode(), UUID.randomUUID().hashCode(), "test"); + overworldDAO.persist(overworld); + entityManager.getTransaction().begin(); + Overworld o2 = entityManager.find(Overworld.class,overworld.getId()); + entityManager.getTransaction().commit(); + Assert.assertEquals(overworld.getSeed(),o2.getSeed()); + } + catch (Exception e){ + e.printStackTrace(); + throw new IllegalArgumentException(e.getMessage()); + } + } + + /** Test removing an existing overworld from the database */ + @Test + public void testRemove(){ + try { + Overworld overworld = new Overworld(UUID.randomUUID().hashCode(),UUID.randomUUID().hashCode(),"test"); + overworldDAO.persist(overworld); + overworldDAO.remove(overworld); + entityManager.getTransaction().begin(); + Assert.assertNull(entityManager.find(Overworld.class,overworld.getId())); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new IllegalArgumentException(e.getMessage()); + } + } +} diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Test/Server/Persistence/PlanetDAOTest.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Test/Server/Persistence/PlanetDAOTest.java new file mode 100644 index 0000000000000000000000000000000000000000..bd51e4fbc536e1900de873429a2a8b8f91633209 --- /dev/null +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Test/Server/Persistence/PlanetDAOTest.java @@ -0,0 +1,73 @@ +package com.galaxytrucker.galaxytruckerreloaded.Test.Server.Persistence; + +import com.galaxytrucker.galaxytruckerreloaded.Model.Map.Planet; +import com.galaxytrucker.galaxytruckerreloaded.Model.Map.PlanetEvent; +import com.galaxytrucker.galaxytruckerreloaded.Model.Ship; +import com.galaxytrucker.galaxytruckerreloaded.Server.Database.Database; +import com.galaxytrucker.galaxytruckerreloaded.Server.Persistence.PlanetDAO; +import org.junit.Assert; +import org.junit.Test; + +import javax.persistence.EntityManager; +import java.util.ArrayList; + +/** Test the planet database access object */ +public class PlanetDAOTest { + + /** Entity manager */ + private EntityManager entityManager = Database.getEntityManager(); + + /** Planet DAO */ + private PlanetDAO planetDAO = new PlanetDAO(); + + /** Test adding a new planet to the database */ + @Test + public void testPersist(){ + Planet planet = new Planet(TraderDAOTest.planetNameGenerator(),0,0, PlanetEvent.VOID,new ArrayList<Ship>()); + try { + planetDAO.persist(planet); + entityManager.getTransaction().begin(); + Assert.assertEquals(planet.getName(),entityManager.find(Planet.class,planet.getName()).getName()); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new IllegalArgumentException(e.getMessage()); + } + } + + /** Test editing a planet in the database */ + @Test + public void testEdit(){ + Planet planet = new Planet(TraderDAOTest.planetNameGenerator(),0,0, PlanetEvent.VOID,new ArrayList<Ship>()); + try { + planetDAO.persist(planet); + planet.setEvent(PlanetEvent.SHOP); + planetDAO.update(planet); + entityManager.getTransaction().begin(); + Assert.assertEquals(PlanetEvent.SHOP,entityManager.find(Planet.class,planet.getName()).getEvent()); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new IllegalArgumentException(e.getMessage()); + } + } + + /** Test removing a planet from the database */ + @Test + public void testRemove(){ + Planet planet = new Planet(TraderDAOTest.planetNameGenerator(),0,0, PlanetEvent.VOID,new ArrayList<Ship>()); + try { + planetDAO.persist(planet); + planetDAO.remove(planet); + entityManager.getTransaction().begin(); + Assert.assertNull(entityManager.find(Planet.class,planet.getName())); + entityManager.getTransaction().commit(); + } + catch (Exception e){ + e.printStackTrace(); + throw new IllegalArgumentException(e.getMessage()); + } + } +} diff --git a/core/src/com/galaxytrucker/galaxytruckerreloaded/Test/Server/Persistence/TraderDAOTest.java b/core/src/com/galaxytrucker/galaxytruckerreloaded/Test/Server/Persistence/TraderDAOTest.java index de577e1bb42837e355adaa028d443dafeaa2d797..72f50bfc18a41933eea6bf8138668b58fa13d03f 100644 --- a/core/src/com/galaxytrucker/galaxytruckerreloaded/Test/Server/Persistence/TraderDAOTest.java +++ b/core/src/com/galaxytrucker/galaxytruckerreloaded/Test/Server/Persistence/TraderDAOTest.java @@ -97,7 +97,7 @@ public class TraderDAOTest { /** Random planet name generator * @return a random planet name */ - private String planetNameGenerator(){ + public static String planetNameGenerator(){ Random random = new Random(); String alphabet = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"; alphabet = alphabet.replace(',',' ');