diff --git a/RestEasy Example/pom.xml b/resteasy/pom.xml similarity index 100% rename from RestEasy Example/pom.xml rename to resteasy/pom.xml diff --git a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java b/resteasy/src/main/java/com/baeldung/client/ServicesInterface.java similarity index 92% rename from RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java rename to resteasy/src/main/java/com/baeldung/client/ServicesInterface.java index 3d03c16faf..19ad0c1ec3 100644 --- a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java +++ b/resteasy/src/main/java/com/baeldung/client/ServicesInterface.java @@ -31,6 +31,6 @@ public interface ServicesInterface { @DELETE @Path("/deletemovie") - Response deleteMovie(@QueryParam("imdbId") String imdbID); + Response deleteMovie(@QueryParam("imdbId") String imdbId); } diff --git a/RestEasy Example/src/main/java/com/baeldung/model/Movie.java b/resteasy/src/main/java/com/baeldung/model/Movie.java similarity index 100% rename from RestEasy Example/src/main/java/com/baeldung/model/Movie.java rename to resteasy/src/main/java/com/baeldung/model/Movie.java diff --git a/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java b/resteasy/src/main/java/com/baeldung/server/MovieCrudService.java similarity index 100% rename from RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java rename to resteasy/src/main/java/com/baeldung/server/MovieCrudService.java diff --git a/RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java b/resteasy/src/main/java/com/baeldung/server/RestEasyServices.java similarity index 100% rename from RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java rename to resteasy/src/main/java/com/baeldung/server/RestEasyServices.java diff --git a/RestEasy Example/src/main/webapp/WEB-INF/classes/logback.xml b/resteasy/src/main/webapp/WEB-INF/classes/logback.xml similarity index 100% rename from RestEasy Example/src/main/webapp/WEB-INF/classes/logback.xml rename to resteasy/src/main/webapp/WEB-INF/classes/logback.xml diff --git a/RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml b/resteasy/src/main/webapp/WEB-INF/jboss-deployment-structure.xml similarity index 100% rename from RestEasy Example/src/main/webapp/WEB-INF/jboss-deployment-structure.xml rename to resteasy/src/main/webapp/WEB-INF/jboss-deployment-structure.xml diff --git a/RestEasy Example/src/main/webapp/WEB-INF/jboss-web.xml b/resteasy/src/main/webapp/WEB-INF/jboss-web.xml similarity index 100% rename from RestEasy Example/src/main/webapp/WEB-INF/jboss-web.xml rename to resteasy/src/main/webapp/WEB-INF/jboss-web.xml diff --git a/RestEasy Example/src/main/webapp/WEB-INF/web.xml b/resteasy/src/main/webapp/WEB-INF/web.xml similarity index 100% rename from RestEasy Example/src/main/webapp/WEB-INF/web.xml rename to resteasy/src/main/webapp/WEB-INF/web.xml diff --git a/resteasy/src/test/java/com/baeldung/server/RestEasyClientTest.java b/resteasy/src/test/java/com/baeldung/server/RestEasyClientTest.java new file mode 100644 index 0000000000..680affe042 --- /dev/null +++ b/resteasy/src/test/java/com/baeldung/server/RestEasyClientTest.java @@ -0,0 +1,144 @@ +package com.baeldung.server; + +import com.baeldung.client.ServicesInterface; +import com.baeldung.model.Movie; +import org.apache.commons.io.IOUtils; +import org.codehaus.jackson.map.DeserializationConfig; +import org.codehaus.jackson.map.ObjectMapper; +import org.jboss.resteasy.client.jaxrs.ResteasyClient; +import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; +import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; +import org.junit.Before; +import org.junit.Test; +import javax.naming.NamingException; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.text.SimpleDateFormat; +import java.util.List; +import java.util.Locale; + +public class RestEasyClientTest { + + Movie transformerMovie = null; + Movie batmanMovie = null; + ObjectMapper jsonMapper = null; + + @Before + public void setup() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NamingException { + + jsonMapper = new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); + jsonMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); + SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); + jsonMapper.setDateFormat(sdf); + + try (InputStream inputStream = new RestEasyClientTest().getClass().getResourceAsStream("./movies/transformer.json")) { + String transformerMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); + transformerMovie = jsonMapper.readValue(transformerMovieAsString, Movie.class); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Test is going to die ...", e); + } + + try (InputStream inputStream = new RestEasyClientTest().getClass().getResourceAsStream("./movies/batman.json")) { + String batmanMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); + batmanMovie = jsonMapper.readValue(batmanMovieAsString, Movie.class); + + } catch (Exception e) { + throw new RuntimeException("Test is going to die ...", e); + } + } + + @Test + public void testListAllMovies() { + + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(transformerMovie); + moviesResponse.close(); + moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + + List movies = simple.listMovies(); + System.out.println(movies); + } + + @Test + public void testMovieByImdbId() { + + String transformerImdbId = "tt0418279"; + + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(transformerMovie); + moviesResponse.close(); + + Movie movies = simple.movieByImdbId(transformerImdbId); + System.out.println(movies); + } + + @Test + public void testAddMovie() { + + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + moviesResponse = simple.addMovie(transformerMovie); + + if (moviesResponse.getStatus() != Response.Status.CREATED.getStatusCode()) { + System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); + } + + moviesResponse.close(); + System.out.println("Response Code: " + Response.Status.OK.getStatusCode()); + } + + @Test + public void testDeleteMovie() { + + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + moviesResponse = simple.deleteMovie(batmanMovie.getImdbId()); + + if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { + System.out.println(moviesResponse.readEntity(String.class)); + throw new RuntimeException("Failed : HTTP error code : " + moviesResponse.getStatus()); + } + + moviesResponse.close(); + System.out.println("Response Code: " + Response.Status.OK.getStatusCode()); + } + + @Test + public void testUpdateMovie() { + + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://127.0.0.1:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + Response moviesResponse = simple.addMovie(batmanMovie); + moviesResponse.close(); + batmanMovie.setTitle("Batman Begins"); + moviesResponse = simple.updateMovie(batmanMovie); + + if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) { + System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus()); + } + + moviesResponse.close(); + System.out.println("Response Code: " + Response.Status.OK.getStatusCode()); + } + +} \ No newline at end of file diff --git a/resteasy/src/test/resources/com/baeldung/server/movies/batman.json b/resteasy/src/test/resources/com/baeldung/server/movies/batman.json new file mode 100644 index 0000000000..82aaaa8f40 --- /dev/null +++ b/resteasy/src/test/resources/com/baeldung/server/movies/batman.json @@ -0,0 +1,4 @@ +{ + "title": "Batman", + "imdbId": "tt0096895" +} \ No newline at end of file diff --git a/resteasy/src/test/resources/com/baeldung/server/movies/transformer.json b/resteasy/src/test/resources/com/baeldung/server/movies/transformer.json new file mode 100644 index 0000000000..634cefc73c --- /dev/null +++ b/resteasy/src/test/resources/com/baeldung/server/movies/transformer.json @@ -0,0 +1,4 @@ +{ + "title": "Transformers", + "imdbId": "tt0418279" +} \ No newline at end of file