Added testCase for List All Movies and Add a Movie
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
package com.baeldung.server;
|
||||
|
||||
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.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));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user