-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from AttAditya/testing
Added Tests for Appeals
- Loading branch information
Showing
4 changed files
with
290 additions
and
13 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
src/test/java/com/zolobooky/booky/AppealAPITestAssets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters