Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ public Course getCourse(@PathParam("courseOrder") int courseOrder) {

@PUT
@Path("courses/{courseOrder}")
public Response putCourse(@PathParam("courseOrder") int courseOrder, Course course) {
Course existingCourse = courses.get(courseOrder);

if (existingCourse == null || existingCourse.getId() != course.getId() || !(existingCourse.getName().equals(course.getName()))) {
courses.put(courseOrder, course);
return Response.ok().build();
public Response updateCourse(@PathParam("courseOrder") int courseOrder, Course course) {
Course existingCourse = courses.get(courseOrder);
if (existingCourse == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}

return Response.notModified().build();
if (existingCourse.equals(course)) {
return Response.notModified().build();
}
courses.put(courseOrder, course);
return Response.ok().build();
}

@Path("courses/{courseOrder}/students")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import javax.xml.bind.annotation.XmlRootElement;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -35,31 +36,42 @@ public List<Student> getStudents() {
public void setStudents(List<Student> students) {
this.students = students;
}

@GET
@Path("{studentOrder}")
public Student getStudent(@PathParam("studentOrder")int studentOrder) {
public Student getStudent(@PathParam("studentOrder") int studentOrder) {
return students.get(studentOrder);
}

@POST
public Response postStudent(Student student) {
public Response createStudent(Student student) {
if (students == null) {
students = new ArrayList<>();
}
if (students.contains(student)) {
return Response.status(Response.Status.CONFLICT).build();
}
students.add(student);
return Response.ok(student).build();
}

@DELETE
@Path("{studentOrder}")
public Response deleteStudent(@PathParam("studentOrder") int studentOrder) {
Student student = students.get(studentOrder);
if (student != null) {
students.remove(studentOrder);
return Response.ok().build();
} else {
return Response.notModified().build();
if (students == null || studentOrder >= students.size()) {
return Response.status(Response.Status.NOT_FOUND).build();
}
students.remove(studentOrder);
return Response.ok().build();
}

@Override
public int hashCode() {
return id + name.hashCode();
}

@Override
public boolean equals(Object obj) {
return (obj instanceof Course) && (id == ((Course) obj).getId()) && (name.equals(((Course) obj).getName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,14 @@ public String getName() {
public void setName(String name) {
this.name = name;
}

@Override
public int hashCode() {
return id + name.hashCode();
}

@Override
public boolean equals(Object obj) {
return (obj instanceof Student) && (id == ((Student) obj).getId()) && (name.equals(((Student) obj).getName()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<Course>
<id>2</id>
<name>Apache CXF Support for RESTful</name>
</Course>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<Student>
<id>1</id>
<name>Student A</name>
</Student>
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

<Student>
<id>3</id>
<name>Student C</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<Course>
<id>1</id>
<name>REST with Spring</name>
</Course>
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,57 @@ public static void closeClient() throws IOException {
}

@Test
public void whenPutCourse_thenCorrect() throws IOException {
public void whenUpdateNonExistentCourse_thenReceiveNotFoundResponse() throws IOException {
HttpPut httpPut = new HttpPut(BASE_URL + "3");
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("course.xml");
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("non_existent_course.xml");
httpPut.setEntity(new InputStreamEntity(resourceStream));
httpPut.setHeader("Content-Type", "text/xml");

HttpResponse response = client.execute(httpPut);
assertEquals(404, response.getStatusLine().getStatusCode());
}

@Test
public void whenUpdateUnchangedCourse_thenReceiveNotModifiedResponse() throws IOException {
HttpPut httpPut = new HttpPut(BASE_URL + "1");
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("unchanged_course.xml");
httpPut.setEntity(new InputStreamEntity(resourceStream));
httpPut.setHeader("Content-Type", "text/xml");

HttpResponse response = client.execute(httpPut);
assertEquals(304, response.getStatusLine().getStatusCode());
}

@Test
public void whenUpdateValidCourse_thenReceiveOKResponse() throws IOException {
HttpPut httpPut = new HttpPut(BASE_URL + "2");
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("changed_course.xml");
httpPut.setEntity(new InputStreamEntity(resourceStream));
httpPut.setHeader("Content-Type", "text/xml");

HttpResponse response = client.execute(httpPut);
assertEquals(200, response.getStatusLine().getStatusCode());

Course course = getCourse(3);
assertEquals(3, course.getId());
Course course = getCourse(2);
assertEquals(2, course.getId());
assertEquals("Apache CXF Support for RESTful", course.getName());
}

@Test
public void whenCreateConflictStudent_thenReceiveConflictResponse() throws IOException {
HttpPost httpPost = new HttpPost(BASE_URL + "1/students");
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("conflict_student.xml");
httpPost.setEntity(new InputStreamEntity(resourceStream));
httpPost.setHeader("Content-Type", "text/xml");

HttpResponse response = client.execute(httpPost);
assertEquals(409, response.getStatusLine().getStatusCode());
}

@Test
public void whenPostStudent_thenCorrect() throws IOException {
public void whenCreateValidStudent_thenReceiveOKResponse() throws IOException {
HttpPost httpPost = new HttpPost(BASE_URL + "2/students");
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("student.xml");
InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream("created_student.xml");
httpPost.setEntity(new InputStreamEntity(resourceStream));
httpPost.setHeader("Content-Type", "text/xml");

Expand All @@ -64,7 +97,14 @@ public void whenPostStudent_thenCorrect() throws IOException {
}

@Test
public void whenDeleteStudent_thenCorrect() throws IOException {
public void whenDeleteInvalidStudent_thenReceiveNotFoundResponse() throws IOException {
HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/2");
HttpResponse response = client.execute(httpDelete);
assertEquals(404, response.getStatusLine().getStatusCode());
}

@Test
public void whenDeleteValidStudent_thenReceiveOKResponse() throws IOException {
HttpDelete httpDelete = new HttpDelete(BASE_URL + "1/students/0");
HttpResponse response = client.execute(httpDelete);
assertEquals(200, response.getStatusLine().getStatusCode());
Expand Down