Added testCase for all Services
This commit is contained in:
parent
a85c9cbda5
commit
a2a026a503
|
@ -99,21 +99,6 @@
|
|||
<version>2.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>2.7.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-annotations</artifactId>
|
||||
<version>2.7.0</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
|
|
@ -17,6 +17,12 @@ public interface ServicesInterface {
|
|||
Movie movieByImdbID(@QueryParam("imdbID") String imdbID);
|
||||
|
||||
|
||||
@GET
|
||||
@Path("/listmovies")
|
||||
@Produces({"application/json"})
|
||||
List<Movie> listMovies();
|
||||
|
||||
|
||||
@POST
|
||||
@Path("/addmovie")
|
||||
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
|
||||
|
@ -34,9 +40,5 @@ public interface ServicesInterface {
|
|||
Response deleteMovie(@QueryParam("imdbID") String imdbID);
|
||||
|
||||
|
||||
@GET
|
||||
@Path("/listmovies")
|
||||
@Produces({"application/json"})
|
||||
List<Movie> listMovies();
|
||||
|
||||
}
|
||||
|
|
|
@ -18,18 +18,21 @@ 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 ***");
|
||||
System.out.println("*** Calling getinfo for a given ImdbID***");
|
||||
|
||||
if(inventory.containsKey(imdbID)){
|
||||
return inventory.get(imdbID);
|
||||
}else return null;
|
||||
|
||||
Movie movie=new Movie();
|
||||
movie.setImdbID(imdbID);
|
||||
return movie;
|
||||
}
|
||||
|
||||
|
||||
@POST
|
||||
@Path("/addmovie")
|
||||
@Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
|
||||
|
@ -41,6 +44,7 @@ public class MovieCrudService {
|
|||
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();
|
||||
|
@ -54,7 +58,7 @@ 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();
|
||||
}
|
||||
|
@ -79,6 +83,7 @@ public class MovieCrudService {
|
|||
return Response.status(Response.Status.OK).build();
|
||||
}
|
||||
|
||||
|
||||
@GET
|
||||
@Path("/listmovies")
|
||||
@Produces({"application/json"})
|
||||
|
|
|
@ -33,7 +33,7 @@
|
|||
</servlet-class>
|
||||
<init-param>
|
||||
<param-name>javax.ws.rs.Application</param-name>
|
||||
<param-value>com.baeldung.server.service.RestEasyServices</param-value>
|
||||
<param-value>com.baeldung.server.RestEasyServices</param-value>
|
||||
</init-param>
|
||||
</servlet>
|
||||
|
||||
|
|
|
@ -1,112 +0,0 @@
|
|||
package com.baeldung.server;
|
||||
|
||||
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 {
|
||||
|
||||
|
||||
Movie transformerMovie=null;
|
||||
Movie batmanMovie=null;
|
||||
ObjectMapper jsonMapper=null;
|
||||
|
||||
@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(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 Response moviesResponse = simple.addMovie(batmanMovie);
|
||||
|
||||
if (moviesResponse.getStatus() != 201) {
|
||||
System.out.println(moviesResponse.readEntity(String.class));
|
||||
throw new RuntimeException("Failed : HTTP error code : "
|
||||
+ moviesResponse.getStatus());
|
||||
}
|
||||
|
||||
moviesResponse.close();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,189 @@
|
|||
package com.baeldung.server;
|
||||
|
||||
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.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;
|
||||
|
||||
@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 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) {
|
||||
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(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest"));
|
||||
ServicesInterface simple = target.proxy(ServicesInterface.class);
|
||||
|
||||
Response moviesResponse = simple.addMovie(transformerMovie);
|
||||
moviesResponse.close();
|
||||
moviesResponse = simple.addMovie(batmanMovie);
|
||||
moviesResponse.close();
|
||||
|
||||
final List<Movie> movies = simple.listMovies();
|
||||
System.out.println(movies);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testMovieByImdbID() {
|
||||
|
||||
String transformerImdbId="tt0418279";
|
||||
|
||||
try {
|
||||
ResteasyClient client = new ResteasyClientBuilder().build();
|
||||
ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest"));
|
||||
ServicesInterface simple = target.proxy(ServicesInterface.class);
|
||||
|
||||
Response moviesResponse = simple.addMovie(transformerMovie);
|
||||
moviesResponse.close();
|
||||
|
||||
final Movie movies = simple.movieByImdbID(transformerImdbId);
|
||||
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);
|
||||
|
||||
Response moviesResponse = simple.addMovie(batmanMovie);
|
||||
moviesResponse.close();
|
||||
moviesResponse = simple.addMovie(transformerMovie);
|
||||
|
||||
if (moviesResponse.getStatus() != Response.Status.CREATED.getStatusCode()) {
|
||||
//System.out.println(moviesResponse.readEntity(String.class));
|
||||
System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus());
|
||||
}
|
||||
moviesResponse.close();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDeleteMovie() {
|
||||
|
||||
String transformerImdbId="tt0418279";
|
||||
|
||||
try {
|
||||
ResteasyClient client = new ResteasyClientBuilder().build();
|
||||
ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest"));
|
||||
ServicesInterface simple = target.proxy(ServicesInterface.class);
|
||||
|
||||
Response moviesResponse = simple.addMovie(batmanMovie);
|
||||
moviesResponse.close();
|
||||
moviesResponse = simple.deleteMovie(transformerImdbId);
|
||||
moviesResponse.close();
|
||||
|
||||
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();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testUpdateMovie() {
|
||||
|
||||
try {
|
||||
|
||||
ResteasyClient client = new ResteasyClientBuilder().build();
|
||||
ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://localhost:8080/RestEasyTutorial/rest"));
|
||||
ServicesInterface simple = target.proxy(ServicesInterface.class);
|
||||
|
||||
Response moviesResponse = simple.addMovie(batmanMovie);
|
||||
moviesResponse.close();
|
||||
batmanMovie.setImdbVotes("300,000");
|
||||
moviesResponse = simple.updateMovie(batmanMovie);
|
||||
|
||||
if (moviesResponse.getStatus() != Response.Status.OK.getStatusCode()) {
|
||||
//System.out.println(moviesResponse.readEntity(String.class));
|
||||
System.out.println("Failed : HTTP error code : " + moviesResponse.getStatus());
|
||||
}
|
||||
|
||||
moviesResponse.close();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue