55 lines
1.9 KiB
Java
55 lines
1.9 KiB
Java
package pl.edu.amu.wmi.bookapi.fixtures.api;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.test.web.servlet.MockMvc;
|
|
import org.springframework.test.web.servlet.ResultActions;
|
|
import pl.edu.amu.wmi.bookapi.models.BookDocument;
|
|
|
|
import java.net.URI;
|
|
|
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
|
|
|
public class BookControllerRequest {
|
|
private final MockMvc mvc;
|
|
private final ObjectMapper objectMapper;
|
|
|
|
public BookControllerRequest(MockMvc mvc, ObjectMapper objectMapper) {
|
|
this.mvc = mvc;
|
|
this.objectMapper = objectMapper;
|
|
}
|
|
|
|
public ResultActions getBooksForUser(String userName) throws Exception {
|
|
return mvc.perform(get("/api/books"));
|
|
}
|
|
|
|
public ResultActions getAllBooks() throws Exception {
|
|
return mvc.perform(get("/api/books/public"));
|
|
}
|
|
|
|
public ResultActions updateBook(String bookId, String userId, String jsonBody) throws Exception {
|
|
return mvc.perform(patch("/api/books/" + bookId)
|
|
.contentType(MediaType.APPLICATION_JSON)
|
|
.content(jsonBody));
|
|
}
|
|
|
|
public ResultActions deleteBook(String userName, String bookId) throws Exception {
|
|
return mvc.perform(delete("/api/books/" + bookId));
|
|
}
|
|
|
|
public ResultActions addBook(
|
|
String userName,
|
|
String ean,
|
|
String author,
|
|
String title
|
|
) throws Exception {
|
|
return mvc.perform(post(URI.create("/api/books"))
|
|
.contentType(MediaType.APPLICATION_JSON)
|
|
.content("{\n" +
|
|
" \"ean\": " + objectMapper.writeValueAsString(ean) + ",\n" +
|
|
" \"author\": " + objectMapper.writeValueAsString(author) + ",\n" +
|
|
" \"title\": " + objectMapper.writeValueAsString(title) + "\n" +
|
|
"}"));
|
|
}
|
|
}
|