diff --git a/RestEasy Example/pom.xml b/RestEasy Example/pom.xml index b16c5e8267..8dabfc863b 100644 --- a/RestEasy Example/pom.xml +++ b/RestEasy Example/pom.xml @@ -85,6 +85,35 @@ ${resteasy.scope} + + + + junit + junit + 4.4 + + + + commons-io + commons-io + 2.4 + + + + com.fasterxml.jackson.core + jackson-core + 2.7.0 + + + + com.fasterxml.jackson.core + jackson-annotations + 2.7.0 + + + + + diff --git a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java index 53e88961be..2585c32438 100644 --- a/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java +++ b/RestEasy Example/src/main/java/com/baeldung/client/ServicesInterface.java @@ -1,15 +1,13 @@ package com.baeldung.client; -import com.baeldung.Movie; +import com.baeldung.model.Movie; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; - +@Path("/movies") public interface ServicesInterface { diff --git a/RestEasy Example/src/main/java/com/baeldung/Movie.java b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java similarity index 86% rename from RestEasy Example/src/main/java/com/baeldung/Movie.java rename to RestEasy Example/src/main/java/com/baeldung/model/Movie.java index c0041d2e95..052ba081c1 100644 --- a/RestEasy Example/src/main/java/com/baeldung/Movie.java +++ b/RestEasy Example/src/main/java/com/baeldung/model/Movie.java @@ -1,5 +1,5 @@ -package com.baeldung; +package com.baeldung.model; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; @@ -532,4 +532,47 @@ public class Movie { this.year = value; } + @Override + public String toString() { + return "Movie{" + + "actors='" + actors + '\'' + + ", awards='" + awards + '\'' + + ", country='" + country + '\'' + + ", director='" + director + '\'' + + ", genre='" + genre + '\'' + + ", imdbID='" + imdbID + '\'' + + ", imdbRating='" + imdbRating + '\'' + + ", imdbVotes='" + imdbVotes + '\'' + + ", language='" + language + '\'' + + ", metascore='" + metascore + '\'' + + ", poster='" + poster + '\'' + + ", rated='" + rated + '\'' + + ", released='" + released + '\'' + + ", response='" + response + '\'' + + ", runtime='" + runtime + '\'' + + ", title='" + title + '\'' + + ", type='" + type + '\'' + + ", writer='" + writer + '\'' + + ", year='" + year + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Movie movie = (Movie) o; + + if (imdbID != null ? !imdbID.equals(movie.imdbID) : movie.imdbID != null) return false; + return title != null ? title.equals(movie.title) : movie.title == null; + + } + + @Override + public int hashCode() { + int result = imdbID != null ? imdbID.hashCode() : 0; + result = 31 * result + (title != null ? title.hashCode() : 0); + return result; + } } diff --git a/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java similarity index 96% rename from RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java rename to RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java index d1973e7037..60e0121966 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/service/MovieCrudService.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/MovieCrudService.java @@ -1,11 +1,10 @@ -package com.baeldung.server.service; +package com.baeldung.server; -import com.baeldung.Movie; +import com.baeldung.model.Movie; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import javax.ws.rs.ext.Provider; import java.util.ArrayList; import java.util.HashMap; import java.util.List; diff --git a/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java b/RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java similarity index 95% rename from RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java rename to RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java index 16b6200ad1..8c57d2c9b4 100644 --- a/RestEasy Example/src/main/java/com/baeldung/server/service/RestEasyServices.java +++ b/RestEasy Example/src/main/java/com/baeldung/server/RestEasyServices.java @@ -1,4 +1,4 @@ -package com.baeldung.server.service; +package com.baeldung.server; import javax.ws.rs.ApplicationPath; diff --git a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java index e711233979..c77f494862 100644 --- a/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java +++ b/RestEasy Example/src/test/java/com/baeldung/server/RestEasyClient.java @@ -1,49 +1,111 @@ package com.baeldung.server; -import com.baeldung.Movie; +import com.baeldung.model.Movie; import com.baeldung.client.ServicesInterface; +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.BeforeClass; +import org.junit.Test; +import javax.naming.NamingException; +import javax.ws.rs.core.Link; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriBuilder; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.List; +import java.util.Locale; public class RestEasyClient { - public static void main(String[] args) { - Movie st = new Movie(); - st.setImdbID("12345"); + Movie transformerMovie=null; + Movie batmanMovie=null; + ObjectMapper jsonMapper=null; - /* - * Alternatively you can use this simple String to send - * instead of using a Student instance - * - * String jsonString = "{\"id\":12,\"firstName\":\"Catain\",\"lastName\":\"Hook\",\"age\":10}"; - */ + @BeforeClass + public static void loadMovieInventory(){ + + + + } + + @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 RestEasyClient().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 RestEasyClient().getClass().getResourceAsStream("./movies/batman.json")) { + String batmanMovieAsString = String.format(IOUtils.toString(inputStream, StandardCharsets.UTF_8)); + batmanMovie = jsonMapper.readValue(batmanMovieAsString, Movie.class); + + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException("Test is going to die ...", e); + } + + } + + @Test + public void testListAllMovies() { try { ResteasyClient client = new ResteasyClientBuilder().build(); - ResteasyWebTarget target = client.target("http://localhost:8080/RestEasyTutorial/rest/movies/listmovies"); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); + ServicesInterface simple = target.proxy(ServicesInterface.class); + + final List movies = simple.listMovies(); + System.out.println(movies); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + + @Test + public void testAddMovie() { + + try { + ResteasyClient client = new ResteasyClientBuilder().build(); + ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest")); ServicesInterface simple = target.proxy(ServicesInterface.class); - final List movies = simple.listMovies(); + final Response moviesResponse = simple.addMovie(batmanMovie); - /* - if (response.getStatus() != 200) { + if (moviesResponse.getStatus() != 201) { + System.out.println(moviesResponse.readEntity(String.class)); throw new RuntimeException("Failed : HTTP error code : " - + response.getStatus()); + + moviesResponse.getStatus()); } - System.out.println("Server response : \n"); - System.out.println(response.readEntity(String.class)); + moviesResponse.close(); - response.close(); -*/ } catch (Exception e) { - e.printStackTrace(); - } } diff --git a/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json b/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json new file mode 100644 index 0000000000..28061d5bf9 --- /dev/null +++ b/RestEasy Example/src/test/resources/com/baeldung/server/movies/batman.json @@ -0,0 +1,22 @@ +{ + "title": "Batman", + "year": "1989", + "rated": "PG-13", + "released": "23 Jun 1989", + "runtime": "126 min", + "genre": "Action, Adventure", + "director": "Tim Burton", + "writer": "Bob Kane (Batman characters), Sam Hamm (story), Sam Hamm (screenplay), Warren Skaaren (screenplay)", + "actors": "Michael Keaton, Jack Nicholson, Kim Basinger, Robert Wuhl", + "plot": "The Dark Knight of Gotham City begins his war on crime with his first major enemy being the clownishly homicidal Joker.", + "language": "English, French", + "country": "USA, UK", + "awards": "Won 1 Oscar. Another 9 wins & 22 nominations.", + "poster": "http://ia.media-imdb.com/images/M/MV5BMTYwNjAyODIyMF5BMl5BanBnXkFtZTYwNDMwMDk2._V1_SX300.jpg", + "metascore": "66", + "imdbRating": "7.6", + "imdbVotes": "256,000", + "imdbID": "tt0096895", + "type": "movie", + "response": "True" +} \ No newline at end of file diff --git a/RestEasy Example/src/test/resources/server/movies/transformer.json b/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json similarity index 99% rename from RestEasy Example/src/test/resources/server/movies/transformer.json rename to RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json index 2154868265..a3b033a8ba 100644 --- a/RestEasy Example/src/test/resources/server/movies/transformer.json +++ b/RestEasy Example/src/test/resources/com/baeldung/server/movies/transformer.json @@ -17,6 +17,6 @@ "imdbRating": "7.1", "imdbVotes": "492,225", "imdbID": "tt0418279", - "Type": "movie", + "type": "movie", "response": "True" } \ No newline at end of file