124 lines
5.0 KiB
Java
124 lines
5.0 KiB
Java
package pl.edu.amu.wmi.bookapi.Integration.api;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.TestInstance;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
import org.springframework.test.web.servlet.MockMvc;
|
|
import pl.edu.amu.wmi.bookapi.fixtures.IntegrationTestUtil;
|
|
import pl.edu.amu.wmi.bookapi.fixtures.api.BookControllerRequest;
|
|
import pl.edu.amu.wmi.bookapi.models.BookDocument;
|
|
|
|
import java.util.List;
|
|
|
|
import static org.hamcrest.Matchers.*;
|
|
import static org.junit.Assert.*;
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
|
|
|
@SpringBootTest
|
|
@AutoConfigureMockMvc
|
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
|
@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class })
|
|
public class BookControllerInt {
|
|
@Autowired
|
|
MongoTemplate mongoTemplate;
|
|
|
|
@Autowired
|
|
MockMvc mvc;
|
|
|
|
@Autowired
|
|
IntegrationTestUtil testUtil;
|
|
|
|
private BookControllerRequest bookRequests;
|
|
|
|
@BeforeEach
|
|
void cleanCollections() {
|
|
this.bookRequests = new BookControllerRequest(mvc, new ObjectMapper());
|
|
testUtil.cleanCollections();
|
|
}
|
|
|
|
@Test
|
|
void should_add_a_book() throws Exception {
|
|
this.bookRequests.addBook("admin", "12345", "auth", "title")
|
|
.andExpect(status().isOk());
|
|
assertEquals(mongoTemplate.findAll(BookDocument.class).get(0).getEan(), "12345");
|
|
}
|
|
|
|
@Test
|
|
void should_list_books_for_user() throws Exception {
|
|
this.bookRequests.addBook("admin", "12345", "auth", "title")
|
|
.andExpect(status().isOk());
|
|
this.bookRequests.addBook("admin", "12345", "auth", "title")
|
|
.andExpect(status().isOk());
|
|
bookRequests.getBooksForUser("admin")
|
|
.andExpect(status().isOk())
|
|
.andExpect(jsonPath("$.*.ownerUsername", equalTo(List.of("admin", "admin"))));
|
|
}
|
|
|
|
@Test
|
|
void should_delete_a_book() throws Exception {
|
|
this.bookRequests.addBook("admin", "12345", "auth", "title")
|
|
.andExpect(status().isOk());
|
|
BookDocument foundBook = mongoTemplate.findAll(BookDocument.class).get(0);
|
|
assertEquals(foundBook.getEan(), "12345");
|
|
this.bookRequests.deleteBook("admin", foundBook.getId());
|
|
assertEquals(0, mongoTemplate.findAll(BookDocument.class).size());
|
|
}
|
|
|
|
@Test
|
|
void should_list_all_books() throws Exception {
|
|
this.bookRequests.addBook("admin1", "123451", "auth1", "title1")
|
|
.andExpect(status().isOk());
|
|
this.bookRequests.addBook("admin2", "123452", "auth2", "title2")
|
|
.andExpect(status().isOk());
|
|
this.bookRequests.addBook("admin3", "123453", "auth3", "title3")
|
|
.andExpect(status().isOk());
|
|
|
|
this.bookRequests.getAllBooks()
|
|
.andExpect(status().isOk())
|
|
.andExpect(jsonPath("$").isArray());
|
|
}
|
|
|
|
@Test
|
|
void should_update_a_book() throws Exception {
|
|
this.bookRequests.addBook("admin", "12345", "1", "2")
|
|
.andDo(print());
|
|
BookDocument foundBook = mongoTemplate.findAll(BookDocument.class).get(0);
|
|
String bookId = foundBook.getId();
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
this.bookRequests.updateBook(bookId, "admin",
|
|
"{\n" +
|
|
" \"ean\": " + objectMapper.writeValueAsString("ean") + ",\n" +
|
|
" \"author\": " + objectMapper.writeValueAsString("author") + ",\n" +
|
|
" \"title\": " + objectMapper.writeValueAsString("title") + "\n" +
|
|
"}").andExpect(status().isOk());
|
|
|
|
BookDocument foundBookAfterUpdate = mongoTemplate.findAll(BookDocument.class).get(0);
|
|
|
|
assertEquals("ean", foundBookAfterUpdate.getEan());
|
|
assertEquals("author", foundBookAfterUpdate.getAuthor());
|
|
assertEquals("title", foundBookAfterUpdate.getTitle());
|
|
|
|
// And should allow for partial update
|
|
this.bookRequests.updateBook(bookId, "admin",
|
|
"{\n" +
|
|
" \"ean\": " + objectMapper.writeValueAsString("ean-1-modified") + "\n" +
|
|
"}").andExpect(status().isOk());
|
|
|
|
BookDocument foundBookAfterPartialUpdate = mongoTemplate.findAll(BookDocument.class).get(0);
|
|
|
|
assertEquals("ean-1-modified", foundBookAfterPartialUpdate.getEan());
|
|
assertEquals("author", foundBookAfterPartialUpdate.getAuthor());
|
|
assertEquals("title", foundBookAfterPartialUpdate.getTitle());
|
|
}
|
|
}
|