-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Method migrated to AdminService from AdminController and unit test wr…
…ote for saveAdminMessage() method.
- Loading branch information
Badri Paudel
committed
Mar 17, 2024
1 parent
ca1b946
commit b65a06c
Showing
3 changed files
with
87 additions
and
13 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package info.keeper.service; | ||
|
||
import info.keeper.models.AdminMessage; | ||
import info.keeper.repositories.AdminRepository; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.security.Principal; | ||
import java.util.Date; | ||
|
||
@Service | ||
public class AdminService { | ||
|
||
private AdminRepository adminRepository; | ||
|
||
public AdminService(AdminRepository adminRepository) { | ||
this.adminRepository = adminRepository; | ||
} | ||
|
||
public void saveAdminMessage(AdminMessage adminMessage, Principal principal) { | ||
adminMessage.setDate(new Date()); | ||
adminMessage.setPostedBy(principal.getName().split("@")[0]); // show only before @ | ||
adminRepository.save(adminMessage); | ||
} | ||
} |
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,57 @@ | ||
package info.keeper.service; | ||
|
||
import info.keeper.models.AdminMessage; | ||
import info.keeper.repositories.AdminRepository; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.security.Principal; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
@SpringBootTest | ||
class AdminServiceTest { | ||
@InjectMocks | ||
AdminService adminService; | ||
|
||
@Mock | ||
AdminRepository adminRepository; | ||
@Mock | ||
Principal principal; | ||
|
||
private String username; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
this.username = "smith@hello.io"; | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
} | ||
|
||
@Test | ||
void saveAdminMessage() { | ||
// Principal principal = mock(Principal.class); // can be mocked this way as well. | ||
when(principal.getName()).thenReturn(this.username); | ||
|
||
AdminMessage adminMessage = new AdminMessage(); | ||
adminMessage.setMessage("Admin message: From Testing Service."); | ||
adminMessage.setPostedBy(this.username.split("@")[0]); | ||
adminMessage.setId(100); | ||
|
||
// when this is called | ||
this.adminService.saveAdminMessage(adminMessage, principal); | ||
// verify the result. | ||
verify(adminRepository, times(1)).save(any(AdminMessage.class)); // saved entity was the instance of AdminMessage | ||
verify(adminRepository, times(1)).save(adminMessage); | ||
assertNotNull(adminMessage.getDate()); // make sure date was set | ||
assertEquals("smith", adminMessage.getPostedBy()); | ||
} | ||
} |