Added testCase for List All Movies and Add a Movie
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -13,7 +13,7 @@ import javax.xml.bind.annotation.XmlType;
|
||||
"country",
|
||||
"director",
|
||||
"genre",
|
||||
"imdbId",
|
||||
"imdbID",
|
||||
"imdbRating",
|
||||
"imdbVotes",
|
||||
"language",
|
||||
@@ -36,7 +36,7 @@ public class Movie {
|
||||
protected String country;
|
||||
protected String director;
|
||||
protected String genre;
|
||||
protected String imdbId;
|
||||
protected String imdbID;
|
||||
protected String imdbRating;
|
||||
protected String imdbVotes;
|
||||
protected String language;
|
||||
@@ -173,27 +173,27 @@ public class Movie {
|
||||
}
|
||||
|
||||
/**
|
||||
* Recupera il valore della propriet� imdbId.
|
||||
* Recupera il valore della propriet� imdbID.
|
||||
*
|
||||
* @return
|
||||
* possible object is
|
||||
* {@link String }
|
||||
*
|
||||
*/
|
||||
public String getImdbId() {
|
||||
return imdbId;
|
||||
public String getImdbID() {
|
||||
return imdbID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Imposta il valore della propriet� imdbId.
|
||||
* Imposta il valore della propriet� imdbID.
|
||||
*
|
||||
* @param value
|
||||
* allowed object is
|
||||
* {@link String }
|
||||
*
|
||||
*/
|
||||
public void setImdbId(String value) {
|
||||
this.imdbId = value;
|
||||
public void setImdbID(String value) {
|
||||
this.imdbID = value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -540,7 +540,7 @@ public class Movie {
|
||||
", country='" + country + '\'' +
|
||||
", director='" + director + '\'' +
|
||||
", genre='" + genre + '\'' +
|
||||
", imdbId='" + imdbId + '\'' +
|
||||
", imdbID='" + imdbID + '\'' +
|
||||
", imdbRating='" + imdbRating + '\'' +
|
||||
", imdbVotes='" + imdbVotes + '\'' +
|
||||
", language='" + language + '\'' +
|
||||
@@ -564,14 +564,14 @@ public class Movie {
|
||||
|
||||
Movie movie = (Movie) o;
|
||||
|
||||
if (imdbId != null ? !imdbId.equals(movie.imdbId) : movie.imdbId != null) return false;
|
||||
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;
|
||||
int result = imdbID != null ? imdbID.hashCode() : 0;
|
||||
result = 31 * result + (title != null ? title.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -18,21 +18,18 @@ public class MovieCrudService {
|
||||
|
||||
private Map<String,Movie> inventory = new HashMap<String, Movie>();
|
||||
|
||||
|
||||
@GET
|
||||
@Path("/getinfo")
|
||||
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
|
||||
public Movie movieByImdbID(@QueryParam("imdbId") String imdbID){
|
||||
public Movie movieByImdbID(@QueryParam("imdbID") String imdbID){
|
||||
|
||||
System.out.println("*** Calling getinfo for a given ImdbID***");
|
||||
|
||||
if(inventory.containsKey(imdbID)){
|
||||
return inventory.get(imdbID);
|
||||
}else return null;
|
||||
System.out.println("*** Calling getinfo ***");
|
||||
|
||||
Movie movie=new Movie();
|
||||
movie.setImdbID(imdbID);
|
||||
return movie;
|
||||
}
|
||||
|
||||
|
||||
@POST
|
||||
@Path("/addmovie")
|
||||
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
|
||||
@@ -40,12 +37,11 @@ public class MovieCrudService {
|
||||
|
||||
System.out.println("*** Calling addMovie ***");
|
||||
|
||||
if (null!=inventory.get(movie.getImdbId())){
|
||||
if (null!=inventory.get(movie.getImdbID())){
|
||||
return Response.status(Response.Status.NOT_MODIFIED)
|
||||
.entity("Movie is Already in the database.").build();
|
||||
}
|
||||
|
||||
inventory.put(movie.getImdbId(),movie);
|
||||
inventory.put(movie.getImdbID(),movie);
|
||||
|
||||
return Response.status(Response.Status.CREATED).build();
|
||||
}
|
||||
@@ -58,11 +54,11 @@ public class MovieCrudService {
|
||||
|
||||
System.out.println("*** Calling updateMovie ***");
|
||||
|
||||
if (null==inventory.get(movie.getImdbId())){
|
||||
if (null!=inventory.get(movie.getImdbID())){
|
||||
return Response.status(Response.Status.NOT_MODIFIED)
|
||||
.entity("Movie is not in the database.\nUnable to Update").build();
|
||||
}
|
||||
inventory.put(movie.getImdbId(),movie);
|
||||
inventory.put(movie.getImdbID(),movie);
|
||||
return Response.status(Response.Status.OK).build();
|
||||
|
||||
}
|
||||
@@ -70,7 +66,7 @@ public class MovieCrudService {
|
||||
|
||||
@DELETE
|
||||
@Path("/deletemovie")
|
||||
public Response deleteMovie(@QueryParam("imdbId") String imdbID){
|
||||
public Response deleteMovie(@QueryParam("imdbID") String imdbID){
|
||||
|
||||
System.out.println("*** Calling deleteMovie ***");
|
||||
|
||||
@@ -83,7 +79,6 @@ public class MovieCrudService {
|
||||
return Response.status(Response.Status.OK).build();
|
||||
}
|
||||
|
||||
|
||||
@GET
|
||||
@Path("/listmovies")
|
||||
@Produces({"application/json"})
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
package com.baeldung.server.service;
|
||||
|
||||
import com.baeldung.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;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
@Path("/movies")
|
||||
public class MovieCrudService {
|
||||
|
||||
|
||||
private Map<String,Movie> inventory = new HashMap<String, Movie>();
|
||||
|
||||
@GET
|
||||
@Path("/getinfo")
|
||||
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
|
||||
public Movie movieByImdbID(@QueryParam("imdbID") String imdbID){
|
||||
|
||||
System.out.println("*** Calling getinfo ***");
|
||||
|
||||
Movie movie=new Movie();
|
||||
movie.setImdbID(imdbID);
|
||||
return movie;
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/addmovie")
|
||||
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
|
||||
public Response addMovie(Movie movie){
|
||||
|
||||
System.out.println("*** Calling addMovie ***");
|
||||
|
||||
if (null!=inventory.get(movie.getImdbID())){
|
||||
return Response.status(Response.Status.NOT_MODIFIED)
|
||||
.entity("Movie is Already in the database.").build();
|
||||
}
|
||||
inventory.put(movie.getImdbID(),movie);
|
||||
|
||||
return Response.status(Response.Status.CREATED).build();
|
||||
}
|
||||
|
||||
|
||||
@PUT
|
||||
@Path("/updatemovie")
|
||||
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
|
||||
public Response updateMovie(Movie movie){
|
||||
|
||||
System.out.println("*** Calling updateMovie ***");
|
||||
|
||||
if (null!=inventory.get(movie.getImdbID())){
|
||||
return Response.status(Response.Status.NOT_MODIFIED)
|
||||
.entity("Movie is not in the database.\nUnable to Update").build();
|
||||
}
|
||||
inventory.put(movie.getImdbID(),movie);
|
||||
return Response.status(Response.Status.OK).build();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@DELETE
|
||||
@Path("/deletemovie")
|
||||
public Response deleteMovie(@QueryParam("imdbID") String imdbID){
|
||||
|
||||
System.out.println("*** Calling deleteMovie ***");
|
||||
|
||||
if (null==inventory.get(imdbID)){
|
||||
return Response.status(Response.Status.NOT_FOUND)
|
||||
.entity("Movie is not in the database.\nUnable to Delete").build();
|
||||
}
|
||||
|
||||
inventory.remove(imdbID);
|
||||
return Response.status(Response.Status.OK).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/listmovies")
|
||||
@Produces({"application/json"})
|
||||
public List<Movie> listMovies(){
|
||||
|
||||
return inventory.values().stream().collect(Collectors.toCollection(ArrayList::new));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.baeldung.server.service;
|
||||
|
||||
|
||||
import javax.ws.rs.ApplicationPath;
|
||||
import javax.ws.rs.core.Application;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Created by Admin on 29/01/2016.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
@ApplicationPath("/rest")
|
||||
public class RestEasyServices extends Application {
|
||||
|
||||
private Set<Object> singletons = new HashSet<Object>();
|
||||
|
||||
public RestEasyServices() {
|
||||
singletons.add(new MovieCrudService());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Object> getSingletons() {
|
||||
return singletons;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Class<?>> getClasses() {
|
||||
return super.getClasses();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getProperties() {
|
||||
return super.getProperties();
|
||||
}
|
||||
}
|
||||
@@ -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<Movie> 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<Movie> 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();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
{
|
||||
"title": "Transformers",
|
||||
"year": "2007",
|
||||
"rated": "PG-13",
|
||||
"released": "03 Jul 2007",
|
||||
"runtime": "144 min",
|
||||
"genre": "Action, Adventure, Sci-Fi",
|
||||
"director": "Michael Bay",
|
||||
"writer": "Roberto Orci (screenplay), Alex Kurtzman (screenplay), John Rogers (story), Roberto Orci (story), Alex Kurtzman (story)",
|
||||
"actors": "Shia LaBeouf, Megan Fox, Josh Duhamel, Tyrese Gibson",
|
||||
"plot": "A long time ago, far away on the planet of Cybertron, a war is being waged between the noble Autobots (led by the wise Optimus Prime) and the devious Decepticons (commanded by the dreaded Megatron) for control over the Allspark, a mystical talisman that would grant unlimited power to whoever possesses it. The Autobots managed to smuggle the Allspark off the planet, but Megatron blasts off in search of it. He eventually tracks it to the planet of Earth (circa 1850), but his reckless desire for power sends him right into the Arctic Ocean, and the sheer cold forces him into a paralyzed state. His body is later found by Captain Archibald Witwicky, but before going into a comatose state Megatron uses the last of his energy to engrave into the Captain's glasses a map showing the location of the Allspark, and to send a transmission to Cybertron. Megatron is then carried away aboard the Captain's ship. A century later, Captain Witwicky's grandson Sam Witwicky (nicknamed Spike by his friends) buys his first car. To his shock, he discovers it to be Bumblebee, an Autobot in disguise who is to protect Spike, who possesses the Captain's glasses and the map engraved on them. But Bumblebee is not the only Transformer to have arrived on Earth - in the desert of Qatar, the Decepticons Blackout and Scorponok attack a U.S. military base, causing the Pentagon to send their special Sector Seven agents to capture all \"specimens of this alien race.\" Spike and his girlfriend Mikaela find themselves in the midst of a grand battle between the Autobots and the Decepticons, stretching from Hoover Dam all the way to Los Angeles. Meanwhile, deep inside Hoover Dam, the cryogenically stored body of Megatron awakens.",
|
||||
"language": "English, Spanish",
|
||||
"country": "USA",
|
||||
"awards": "Nominated for 3 Oscars. Another 18 wins & 40 nominations.",
|
||||
"poster": "http://ia.media-imdb.com/images/M/MV5BMTQwNjU5MzUzNl5BMl5BanBnXkFtZTYwMzc1MTI3._V1_SX300.jpg",
|
||||
"metascore": "61",
|
||||
"imdbRating": "7.1",
|
||||
"imdbVotes": "492,225",
|
||||
"imdbID": "tt0418279",
|
||||
"Type": "movie",
|
||||
"response": "True"
|
||||
}
|
||||
Reference in New Issue
Block a user