Skip to content

Commit

Permalink
Merge pull request #45 from AttAditya/testing
Browse files Browse the repository at this point in the history
Added Tests for Appeals
  • Loading branch information
2k4sm authored Apr 11, 2024
2 parents 73a77df + 26c9cd8 commit 0f408f9
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 13 deletions.
117 changes: 117 additions & 0 deletions src/test/java/com/zolobooky/booky/AppealAPITestAssets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.zolobooky.booky;

import com.zolobooky.booky.appeals.AppealEntity;
import com.zolobooky.booky.appeals.dto.CreateAppealDTO;
import com.zolobooky.booky.appeals.dto.UpdateAppealDTO;
import com.zolobooky.booky.books.BookEntity;
import com.zolobooky.booky.commons.CustomStatus;
import com.zolobooky.booky.users.UserEntity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class AppealAPITestAssets {

public UserEntity demoUser;

public BookEntity demoBook;

public final List<AppealEntity> appealEntityList;

public AppealEntity appealEntity1;

public AppealEntity appealEntity2;

public AppealEntity appealEntity3;

public AppealEntity appealEntity4;

public AppealAPITestAssets() {
demoUser = new UserEntity();
demoUser.setId(42);
demoUser.setName("Demo");
demoUser.setFcmToken("Demo-FCM-TOKEN");

demoBook = new BookEntity();
demoBook.setId(900);
demoBook.setName("Test Book 2");
demoBook.setStatus(CustomStatus.BookStatus.AVAILABLE);
demoBook.setThumbnail("TEST IMAGE URL");
demoBook.setOwner(demoUser);

appealEntity1 = new AppealEntity();
appealEntity1.setTrans_id(1);
appealEntity1.setTrans_status(CustomStatus.TransactionStatus.PENDING);
appealEntity1.setBookId(demoBook);
appealEntity1.setBorrowerId(demoUser);
appealEntity1.setInitiation_date(new Date(1712174174));
appealEntity1.setCompletion_date(new Date(1712174174));
appealEntity1.setStatus_change_date(new Date(1712174174));
appealEntity1.setExpected_completion_date(new Date(1712174174));

appealEntity2 = new AppealEntity();
appealEntity2.setTrans_id(2);
appealEntity2.setTrans_status(CustomStatus.TransactionStatus.PENDING);
appealEntity2.setBookId(demoBook);
appealEntity2.setBorrowerId(demoUser);
appealEntity2.setInitiation_date(new Date(1712174174));
appealEntity2.setCompletion_date(new Date(1712174174));
appealEntity2.setStatus_change_date(new Date(1712174174));
appealEntity2.setExpected_completion_date(new Date(1712174174));

appealEntity3 = new AppealEntity();
appealEntity3.setTrans_id(3);
appealEntity3.setTrans_status(CustomStatus.TransactionStatus.PENDING);
appealEntity3.setBookId(demoBook);
appealEntity3.setBorrowerId(demoUser);
appealEntity3.setInitiation_date(new Date(1712174174));
appealEntity3.setCompletion_date(new Date(1712174174));
appealEntity3.setStatus_change_date(new Date(1712174174));
appealEntity3.setExpected_completion_date(new Date(1712174174));

appealEntity4 = new AppealEntity();
appealEntity4.setTrans_id(4);
appealEntity4.setTrans_status(CustomStatus.TransactionStatus.PENDING);
appealEntity4.setBookId(demoBook);
appealEntity4.setBorrowerId(demoUser);
appealEntity4.setInitiation_date(new Date(1712174174));
appealEntity4.setCompletion_date(new Date(1712174174));
appealEntity4.setStatus_change_date(new Date(1712174174));
appealEntity4.setExpected_completion_date(new Date(1712174174));

appealEntityList = List.of(appealEntity1, appealEntity2, appealEntity3, appealEntity4);
}

public List<AppealEntity> getAllAppeals() {
return this.appealEntityList;
}

public AppealEntity getAppeal() {
return this.appealEntity3;
}

public String toJSONString(CreateAppealDTO dto) {
Integer borrowerId = dto.getBorrowerId();
Integer bookId = dto.getBookId();
Date date = dto.getExpected_completion_date();

String formattedDate = new SimpleDateFormat("yyyy-MM-dd").format(date);

return "{\"book_id\":" + bookId + ", \"borrower_id\":" + borrowerId + ", \"expected_completion_date\":\""
+ formattedDate + "\"}";
}

public String toJSONString(UpdateAppealDTO dto) {
return "{\"trans_status\":\"" + dto.getTrans_status().toString() + "\"}";
}

public AppealEntity postAppeal() {
return this.appealEntity4;
}

public AppealEntity updateAppealStatus() {
return this.appealEntity2;
}

}
76 changes: 76 additions & 0 deletions src/test/java/com/zolobooky/booky/AppealServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.zolobooky.booky;

import com.zolobooky.booky.appeals.AppealController;
import com.zolobooky.booky.appeals.AppealEntity;
import com.zolobooky.booky.appeals.AppealService;
import com.zolobooky.booky.appeals.dto.CreateAppealDTO;
import com.zolobooky.booky.appeals.dto.UpdateAppealDTO;
import com.zolobooky.booky.commons.CustomStatus;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import java.text.SimpleDateFormat;
import java.util.Date;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(AppealController.class)
public class AppealServiceTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private AppealService appealService;

private final AppealAPITestAssets appealAPITestAssets = new AppealAPITestAssets();

@Test
void getAllAppealsTest() throws Exception {
when(appealService.getAllAppeals(-1, -1)).thenReturn(appealAPITestAssets.getAllAppeals());
mockMvc.perform(get("/v0/appeals")).andExpect(status().isOk());
}

@Test
void getAppealByIdTest() throws Exception {
when(appealService.getAppeal(3)).thenReturn(appealAPITestAssets.getAppeal());
mockMvc.perform(get("/v0/appeals/3")).andExpect(status().isOk());
}

@Test
void postAppealTest() throws Exception {
Date demoDate = new SimpleDateFormat("yyyy-MM-dd").parse("2024-04-04");
CreateAppealDTO createAppealDTO = new CreateAppealDTO(900, 42, demoDate);

AppealEntity appealEntity = appealAPITestAssets.postAppeal();
when(appealService.createAppeal(any(createAppealDTO.getClass()))).thenReturn(appealEntity);

String payload = appealAPITestAssets.toJSONString(createAppealDTO);

mockMvc.perform(post("/v0/appeals").contentType(MediaType.APPLICATION_JSON).content(payload))
.andExpect(status().isOk());
}

// @Test
void updateAppealStatusTest() throws Exception {
UpdateAppealDTO updateAppealDTO = new UpdateAppealDTO();
updateAppealDTO.setTrans_status(CustomStatus.TransactionStatus.PENDING);

AppealEntity appealEntity = appealAPITestAssets.updateAppealStatus();
when(appealService.updateAppealStatus(3, updateAppealDTO)).thenReturn(appealEntity);

String payload = appealAPITestAssets.toJSONString(updateAppealDTO);
System.out.println(payload);

mockMvc.perform(patch("/v0/appeals/3").contentType(MediaType.APPLICATION_JSON).content(payload))
.andExpect(status().isOk());
}

}
50 changes: 43 additions & 7 deletions src/test/java/com/zolobooky/booky/BookAPITestAssets.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

import com.zolobooky.booky.books.BookEntity;
import com.zolobooky.booky.books.dto.CreateBookDTO;
import com.zolobooky.booky.books.dto.UpdateBookDTO;
import com.zolobooky.booky.commons.CustomStatus;
import com.zolobooky.booky.users.UserEntity;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import java.sql.Date;
import java.util.List;

@SpringBootTest
public class BookAPITestAssets {

private List<BookEntity> books;
private final List<BookEntity> books;

public BookEntity book1;

Expand Down Expand Up @@ -65,11 +65,47 @@ public BookEntity getDelistedBook() {
return book1;
}

public CreateBookDTO getCreateBookDTO() {
CreateBookDTO bookDTO = new CreateBookDTO();
bookDTO.setName(book2.getName());
bookDTO.setOwner(book2.getOwner().getId());
return bookDTO;
/*
* public CreateBookDTO getCreateBookDTO() { CreateBookDTO bookDTO = new
* CreateBookDTO(); bookDTO.setName(book2.getName());
* bookDTO.setOwner(book2.getOwner().getId()); return bookDTO; }
*/

public String toJSONString(CreateBookDTO dto) {
String name = dto.getName();
String author = dto.getAuthor();
String description = dto.getDescription();
Integer maxBorrow = dto.getMaxBorrow();
String thumbnail = dto.getThumbnail();
Integer owner = dto.getOwner();

return "{" + "\"name\": \"" + name + "\", " + "\"author\": \"" + author + "\", " + "\"description\": \""
+ description + "\", " + "\"maxBorrow\":" + maxBorrow + ", " + "\"thumbnail\": \"" + thumbnail + "\", "
+ "\"owner\": \"" + owner + "\"" + "}";
}

public String toJSONString(UpdateBookDTO dto) {
String name = dto.getName();
String author = dto.getAuthor();
String description = dto.getDescription();
Integer maxBorrow = dto.getMaxBorrow();
String thumbnail = dto.getThumbnail();

return "{" + "\"name\": \"" + name + "\", " + "\"author\": \"" + author + "\", " + "\"description\": \""
+ description + "\", " + "\"maxBorrow\": " + maxBorrow + ", " + "\"thumbnail\": \"" + thumbnail + "\""
+ "}";
}

public BookEntity createBook() {
return book1;
}

public BookEntity updateBook() {
return book1;
}

public BookEntity updateBookStatus() {
return book1;
}

}
60 changes: 54 additions & 6 deletions src/test/java/com/zolobooky/booky/BookServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package com.zolobooky.booky;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zolobooky.booky.books.BookController;
import com.zolobooky.booky.books.BookEntity;
import com.zolobooky.booky.books.BookService;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import com.zolobooky.booky.books.dto.CreateBookDTO;
import jdk.jfr.ContentType;
import com.zolobooky.booky.books.dto.UpdateBookDTO;
import com.zolobooky.booky.commons.CustomStatus;
import org.junit.jupiter.api.Test;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

Expand All @@ -26,9 +29,6 @@ public class BookServiceTest {
@MockBean
private BookService bookService;

@Autowired
private ObjectMapper objectMapper;

private final BookAPITestAssets bookAPITestAssets = new BookAPITestAssets();

@Test
Expand All @@ -49,4 +49,52 @@ void delistBookSerivceTest() throws Exception {
this.mockMvc.perform(delete("/v0/books/0")).andExpect(status().isOk());
}

@Test
void createBookTest() throws Exception {
CreateBookDTO createBookDTO = new CreateBookDTO();
createBookDTO.setName("Demo Book DTO");
createBookDTO.setAuthor("Demo User");
createBookDTO.setDescription("This is a Demo Book");
createBookDTO.setMaxBorrow(1);
createBookDTO.setThumbnail("https://demo.book.url/");
createBookDTO.setOwner(1);

BookEntity bookEntity = bookAPITestAssets.createBook();
when(bookService.createBook(any(createBookDTO.getClass()))).thenReturn(bookEntity);

String payload = bookAPITestAssets.toJSONString(createBookDTO);

mockMvc.perform(post("/v0/books").contentType(MediaType.APPLICATION_JSON).content(payload))
.andExpect(status().isOk());
}

@Test
void updateBookTest() throws Exception {
UpdateBookDTO updateBookDTO = new UpdateBookDTO();
updateBookDTO.setName("Demo Book DTO");
updateBookDTO.setAuthor("Demo User");
updateBookDTO.setDescription("This is a Demo Book");
updateBookDTO.setMaxBorrow(1);
updateBookDTO.setThumbnail("https://demo.book.url/");

BookEntity bookEntity = bookAPITestAssets.updateBook();
when(bookService.updateBook(any(updateBookDTO.getClass()), any(Integer.class))).thenReturn(bookEntity);

String payload = bookAPITestAssets.toJSONString(updateBookDTO);

mockMvc.perform(patch("/v0/books/1").contentType(MediaType.APPLICATION_JSON).content(payload))
.andExpect(status().isOk());
}

// @Test
void changeBookStatusTest() throws Exception {
BookEntity bookEntity = bookAPITestAssets.updateBookStatus();

when(bookService.updateStatus(any(Integer.class), any(CustomStatus.BookStatus.class))).thenReturn(bookEntity);
mockMvc
.perform(put("/v0/books/1").contentType(MediaType.APPLICATION_JSON)
.content("" + CustomStatus.BookStatus.DELISTED))
.andExpect(status().isOk());
}

}

0 comments on commit 0f408f9

Please sign in to comment.