Added testCase for List All Movies and Add a Movie
This commit is contained in:
parent
a0c22cc225
commit
72a74b8096
|
@ -85,6 +85,35 @@
|
||||||
<scope>${resteasy.scope}</scope>
|
<scope>${resteasy.scope}</scope>
|
||||||
</dependency>
|
</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>
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,13 @@
|
||||||
package com.baeldung.client;
|
package com.baeldung.client;
|
||||||
|
|
||||||
import com.baeldung.Movie;
|
import com.baeldung.model.Movie;
|
||||||
|
|
||||||
import javax.ws.rs.*;
|
import javax.ws.rs.*;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
|
|
||||||
|
@Path("/movies")
|
||||||
public interface ServicesInterface {
|
public interface ServicesInterface {
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
package com.baeldung;
|
package com.baeldung.model;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlAccessType;
|
import javax.xml.bind.annotation.XmlAccessType;
|
||||||
import javax.xml.bind.annotation.XmlAccessorType;
|
import javax.xml.bind.annotation.XmlAccessorType;
|
||||||
|
@ -532,4 +532,47 @@ public class Movie {
|
||||||
this.year = value;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -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.*;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import javax.ws.rs.ext.Provider;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.server.service;
|
package com.baeldung.server;
|
||||||
|
|
||||||
|
|
||||||
import javax.ws.rs.ApplicationPath;
|
import javax.ws.rs.ApplicationPath;
|
|
@ -1,49 +1,111 @@
|
||||||
package com.baeldung.server;
|
package com.baeldung.server;
|
||||||
|
|
||||||
import com.baeldung.Movie;
|
import com.baeldung.model.Movie;
|
||||||
import com.baeldung.client.ServicesInterface;
|
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.ResteasyClient;
|
||||||
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
|
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
|
||||||
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
|
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.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
public class RestEasyClient {
|
public class RestEasyClient {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
|
|
||||||
Movie st = new Movie();
|
Movie transformerMovie=null;
|
||||||
st.setImdbID("12345");
|
Movie batmanMovie=null;
|
||||||
|
ObjectMapper jsonMapper=null;
|
||||||
|
|
||||||
/*
|
@BeforeClass
|
||||||
* Alternatively you can use this simple String to send
|
public static void loadMovieInventory(){
|
||||||
* instead of using a Student instance
|
|
||||||
*
|
|
||||||
* String jsonString = "{\"id\":12,\"firstName\":\"Catain\",\"lastName\":\"Hook\",\"age\":10}";
|
|
||||||
*/
|
}
|
||||||
|
|
||||||
|
@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 {
|
try {
|
||||||
ResteasyClient client = new ResteasyClientBuilder().build();
|
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);
|
ServicesInterface simple = target.proxy(ServicesInterface.class);
|
||||||
final List<Movie> movies = simple.listMovies();
|
|
||||||
|
|
||||||
/*
|
final List<Movie> movies = simple.listMovies();
|
||||||
if (response.getStatus() != 200) {
|
System.out.println(movies);
|
||||||
throw new RuntimeException("Failed : HTTP error code : "
|
|
||||||
+ response.getStatus());
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("Server response : \n");
|
|
||||||
System.out.println(response.readEntity(String.class));
|
|
||||||
|
|
||||||
response.close();
|
|
||||||
*/
|
@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) {
|
} catch (Exception e) {
|
||||||
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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"
|
||||||
|
}
|
|
@ -17,6 +17,6 @@
|
||||||
"imdbRating": "7.1",
|
"imdbRating": "7.1",
|
||||||
"imdbVotes": "492,225",
|
"imdbVotes": "492,225",
|
||||||
"imdbID": "tt0418279",
|
"imdbID": "tt0418279",
|
||||||
"Type": "movie",
|
"type": "movie",
|
||||||
"response": "True"
|
"response": "True"
|
||||||
}
|
}
|
Loading…
Reference in New Issue