Skip to content

Commit

Permalink
Method migrated to AdminService from AdminController and unit test wr…
Browse files Browse the repository at this point in the history
…ote for saveAdminMessage() method.
  • Loading branch information
Badri Paudel committed Mar 17, 2024
1 parent ca1b946 commit b65a06c
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/main/java/info/keeper/controllers/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import info.keeper.models.AdminMessage;
import info.keeper.models.User;
import info.keeper.repositories.AdminRepository;
import info.keeper.repositories.UserRepository;
import info.keeper.service.AdminService;
import info.keeper.utils.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
Expand All @@ -17,11 +17,12 @@
@Controller
@RequestMapping(value = "/admin", method = RequestMethod.GET)
public class AdminController {
private AdminRepository adminRepository;
private AdminService adminService;
private UserRepository userRepository;

public AdminController(AdminRepository adminRepository, UserRepository userRepository) {
this.adminRepository = adminRepository;
@Autowired
public AdminController(AdminService adminService, UserRepository userRepository) {
this.adminService = adminService;
this.userRepository = userRepository;
}

Expand All @@ -38,16 +39,8 @@ public String showAdminPage(Principal principal, Model model) {
@PostMapping("/saveMessage")
public String saveMessage(@ModelAttribute("adminMessage")AdminMessage adminMessage,
Principal principal, HttpSession session) {
saveAdminMessage(adminMessage, principal);
this.adminService.saveAdminMessage(adminMessage, principal);
session.setAttribute("message", new Message("You have sent notice to everyone", "alert-success"));
return "redirect:/admin/index/";
}

// TODO: Move to a separate admin service file.
private void saveAdminMessage(AdminMessage adminMessage, Principal principal) {
adminMessage.setDate(new Date());
adminMessage.setPostedBy(principal.getName().split("@")[0]); // show only before @
adminRepository.save(adminMessage);

}
}
24 changes: 24 additions & 0 deletions src/main/java/info/keeper/service/AdminService.java
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);
}
}
57 changes: 57 additions & 0 deletions src/test/java/info/keeper/service/AdminServiceTest.java
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());
}
}

0 comments on commit b65a06c

Please sign in to comment.