Added testCase for List All Movies and Add a Movie

This commit is contained in:
Giuseppe Bueti 2016-01-30 20:39:28 +01:00
parent 4b5ad0629a
commit 663def8e2c
8 changed files with 184 additions and 31 deletions

View File

@ -85,6 +85,35 @@
<scope>${resteasy.scope}</scope>
</dependency>
<!-- Junit Library -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<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>

View File

@ -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 {

View File

@ -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;
}
}

View File

@ -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;

View File

@ -1,4 +1,4 @@
package com.baeldung.server.service;
package com.baeldung.server;
import javax.ws.rs.ApplicationPath;

View File

@ -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();
}
}

View File

@ -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"
}

View File

@ -17,6 +17,6 @@
"imdbRating": "7.1",
"imdbVotes": "492,225",
"imdbID": "tt0418279",
"Type": "movie",
"type": "movie",
"response": "True"
}