diff --git a/src/main/java/entities/RenameMe.java b/src/main/java/entities/RenameMe.java deleted file mode 100644 index d0db66f..0000000 --- a/src/main/java/entities/RenameMe.java +++ /dev/null @@ -1,61 +0,0 @@ -package entities; - -import java.io.Serializable; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.NamedQuery; - - -@Entity -@NamedQuery(name = "RenameMe.deleteAllRows", query = "DELETE from RenameMe") -public class RenameMe implements Serializable { - - private static final long serialVersionUID = 1L; - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - public RenameMe() { - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - // TODO, delete this class, or rename to an Entity class that makes sense for what you are about to do - // Delete EVERYTHING below if you decide to use this class, it's dummy data used for the initial demo - private String dummyStr1; - private String dummyStr2; - - public RenameMe(String dummyStr1, String dummyStr2) { - this.dummyStr1 = dummyStr1; - this.dummyStr2 = dummyStr2; - } - - public String getDummyStr1() { - return dummyStr1; - } - - public void setDummyStr1(String dummyStr1) { - this.dummyStr1 = dummyStr1; - } - - public String getDummyStr2() { - return dummyStr2; - } - - public void setDummyStr2(String dummyStr2) { - this.dummyStr2 = dummyStr2; - } - - - - - -} diff --git a/src/main/java/facades/CarFacade.java b/src/main/java/facades/CarFacade.java index f4568d1..0b31440 100644 --- a/src/main/java/facades/CarFacade.java +++ b/src/main/java/facades/CarFacade.java @@ -2,7 +2,6 @@ import dto.CarDTO; import entities.Car; -import entities.RenameMe; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; diff --git a/src/main/java/facades/FacadeExample.java b/src/main/java/facades/FacadeExample.java deleted file mode 100644 index 494d841..0000000 --- a/src/main/java/facades/FacadeExample.java +++ /dev/null @@ -1,51 +0,0 @@ -package facades; - -import entities.RenameMe; -import java.util.List; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; - -/** - * - * Rename Class to a relevant name Add add relevant facade methods - */ -public class FacadeExample { - - private static FacadeExample instance; - private static EntityManagerFactory emf; - - //Private Constructor to ensure Singleton - private FacadeExample() {} - - - /** - * - * @param _emf - * @return an instance of this facade class. - */ - public static FacadeExample getFacadeExample(EntityManagerFactory _emf) { - if (instance == null) { - emf = _emf; - instance = new FacadeExample(); - } - return instance; - } - - private EntityManager getEntityManager() { - return emf.createEntityManager(); - } - - //TODO Remove/Change this before use - public long getRenameMeCount(){ - EntityManager em = emf.createEntityManager(); - try{ - long renameMeCount = (long)em.createQuery("SELECT COUNT(r) FROM RenameMe r").getSingleResult(); - return renameMeCount; - }finally{ - em.close(); - } - - } - -} diff --git a/src/main/java/rest/ApplicationConfig.java b/src/main/java/rest/ApplicationConfig.java index 1503a37..262ab96 100644 --- a/src/main/java/rest/ApplicationConfig.java +++ b/src/main/java/rest/ApplicationConfig.java @@ -23,7 +23,6 @@ private void addRestResourceClasses(Set> resources) { resources.add(org.glassfish.jersey.server.wadl.internal.WadlResource.class); resources.add(rest.CarResource.class); resources.add(rest.JokeResource.class); - resources.add(rest.RenameMeResource.class); resources.add(rest.StudentRessource.class); resources.add(rest.WhoDidWhatResource.class); } diff --git a/src/main/java/rest/RenameMeResource.java b/src/main/java/rest/RenameMeResource.java deleted file mode 100644 index 93a14e0..0000000 --- a/src/main/java/rest/RenameMeResource.java +++ /dev/null @@ -1,46 +0,0 @@ -package rest; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import entities.RenameMe; -import utils.EMF_Creator; -import facades.FacadeExample; -import javax.persistence.EntityManagerFactory; -import javax.ws.rs.Consumes; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; - -//Todo Remove or change relevant parts before ACTUAL use -@Path("xxx") -public class RenameMeResource { - - private static final EntityManagerFactory EMF = EMF_Creator.createEntityManagerFactory( - "pu", - "jdbc:mysql://localhost:3307/startcode", - "dev", - "ax2", - EMF_Creator.Strategy.CREATE); - private static final FacadeExample FACADE = FacadeExample.getFacadeExample(EMF); - private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); - - @GET - @Produces({MediaType.APPLICATION_JSON}) - public String demo() { - return "{\"msg\":\"Hello World\"}"; - } - @Path("count") - @GET - @Produces({MediaType.APPLICATION_JSON}) - public String getRenameMeCount() { - long count = FACADE.getRenameMeCount(); - //System.out.println("--------------->"+count); - return "{\"count\":"+count+"}"; //Done manually so no need for a DTO - } - - -} diff --git a/src/test/java/facades/FacadeExampleTest.java b/src/test/java/facades/FacadeExampleTest.java deleted file mode 100644 index e4d9087..0000000 --- a/src/test/java/facades/FacadeExampleTest.java +++ /dev/null @@ -1,84 +0,0 @@ -package facades; - -import utils.EMF_Creator; -import entities.RenameMe; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.AfterEach; -import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import utils.Settings; -import utils.EMF_Creator.DbSelector; -import utils.EMF_Creator.Strategy; - -//Uncomment the line below, to temporarily disable this test -@Disabled -public class FacadeExampleTest { - - private static EntityManagerFactory emf; - private static FacadeExample facade; - - public FacadeExampleTest() { - } - - //@BeforeAll - public static void setUpClass() { - emf = EMF_Creator.createEntityManagerFactory( - "pu", - "jdbc:mysql://localhost:3307/startcode_test", - "dev", - "ax2", - EMF_Creator.Strategy.CREATE); - facade = FacadeExample.getFacadeExample(emf); - } - - /* **** HINT **** - A better way to handle configuration values, compared to the UNUSED example above, is to store those values - ONE COMMON place accessible from anywhere. - The file config.properties and the corresponding helper class utils.Settings is added just to do that. - See below for how to use these files. This is our RECOMENDED strategy - */ - @BeforeAll - public static void setUpClassV2() { - emf = EMF_Creator.createEntityManagerFactory(DbSelector.TEST,Strategy.DROP_AND_CREATE); - facade = FacadeExample.getFacadeExample(emf); - } - - @AfterAll - public static void tearDownClass() { -// Clean up database after test is done or use a persistence unit with drop-and-create to start up clean on every test - } - - // Setup the DataBase in a known state BEFORE EACH TEST - //TODO -- Make sure to change the script below to use YOUR OWN entity class - @BeforeEach - public void setUp() { - EntityManager em = emf.createEntityManager(); - try { - em.getTransaction().begin(); - em.createNamedQuery("RenameMe.deleteAllRows").executeUpdate(); - em.persist(new RenameMe("Some txt", "More text")); - em.persist(new RenameMe("aaa", "bbb")); - - em.getTransaction().commit(); - } finally { - em.close(); - } - } - - @AfterEach - public void tearDown() { -// Remove any data after each test was run - } - - // TODO: Delete or change this method - @Test - public void testAFacadeMethod() { - assertEquals(2, facade.getRenameMeCount(), "Expects two rows in the database"); - } - -} \ No newline at end of file diff --git a/src/test/java/rest/RenameMeResourceTest.java b/src/test/java/rest/RenameMeResourceTest.java deleted file mode 100644 index eafece2..0000000 --- a/src/test/java/rest/RenameMeResourceTest.java +++ /dev/null @@ -1,109 +0,0 @@ -package rest; - -import entities.RenameMe; -import utils.EMF_Creator; -import io.restassured.RestAssured; -import static io.restassured.RestAssured.given; -import io.restassured.parsing.Parser; -import java.net.URI; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.ws.rs.core.UriBuilder; -import org.glassfish.grizzly.http.server.HttpServer; -import org.glassfish.grizzly.http.util.HttpStatus; -import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; -import org.glassfish.jersey.server.ResourceConfig; -import static org.hamcrest.Matchers.equalTo; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import utils.EMF_Creator.DbSelector; -import utils.EMF_Creator.Strategy; - -//Uncomment the line below, to temporarily disable this test -@Disabled -public class RenameMeResourceTest { - - private static final int SERVER_PORT = 7777; - private static final String SERVER_URL = "http://localhost/api"; - //Read this line from a settings-file since used several places - private static final String TEST_DB = "jdbc:mysql://localhost:3307/startcode_test"; - - static final URI BASE_URI = UriBuilder.fromUri(SERVER_URL).port(SERVER_PORT).build(); - private static HttpServer httpServer; - private static EntityManagerFactory emf; - - static HttpServer startServer() { - ResourceConfig rc = ResourceConfig.forApplication(new ApplicationConfig()); - return GrizzlyHttpServerFactory.createHttpServer(BASE_URI, rc); - } - - @BeforeAll - public static void setUpClass() { - emf = EMF_Creator.createEntityManagerFactory(DbSelector.TEST, Strategy.CREATE); - - //NOT Required if you use the version of EMF_Creator.createEntityManagerFactory used above - //System.setProperty("IS_TEST", TEST_DB); - //We are using the database on the virtual Vagrant image, so username password are the same for all dev-databases - - httpServer = startServer(); - - //Setup RestAssured - RestAssured.baseURI = SERVER_URL; - RestAssured.port = SERVER_PORT; - - RestAssured.defaultParser = Parser.JSON; - } - - @AfterAll - public static void closeTestServer(){ - //System.in.read(); - httpServer.shutdownNow(); - } - - // Setup the DataBase (used by the test-server and this test) in a known state BEFORE EACH TEST - //TODO -- Make sure to change the script below to use YOUR OWN entity class - @BeforeEach - public void setUp() { - EntityManager em = emf.createEntityManager(); - try { - em.getTransaction().begin(); - em.createNamedQuery("RenameMe.deleteAllRows").executeUpdate(); - em.persist(new RenameMe("Some txt","More text")); - em.persist(new RenameMe("aaa","bbb")); - - em.getTransaction().commit(); - } finally { - em.close(); - } - } - - @Test - public void testServerIsUp() { - System.out.println("Testing is server UP"); - given().when().get("/xxx").then().statusCode(200); - } - - //This test assumes the database contains two rows - @Test - public void testDummyMsg() throws Exception { - given() - .contentType("application/json") - .get("/xxx/").then() - .assertThat() - .statusCode(HttpStatus.OK_200.getStatusCode()) - .body("msg", equalTo("Hello World")); - } - - @Test - public void testCount() throws Exception { - given() - .contentType("application/json") - .get("/xxx/count").then() - .assertThat() - .statusCode(HttpStatus.OK_200.getStatusCode()) - .body("count", equalTo(2)); - } -}