Skip to content
Snippets Groups Projects
Commit caed24cb authored by David Hammond's avatar David Hammond
Browse files

Final Code changes

parent d0845bef
No related branches found
No related tags found
No related merge requests found
......@@ -23,6 +23,8 @@ public class ComplaintRepositoryImpl implements ComplaintRepository{
private JdbcTemplate jdbcTemplate;
private RowMapper<Complaint> complaintMapper;
@Autowired
public void ComplaintRepositoryImp (JdbcTemplate jdbc){
this.jdbcTemplate = jdbc;
......
......@@ -31,9 +31,6 @@ public class ComplaintServiceImpl implements ComplaintService{
}
@Override
public Complaint getComplaintById(Authentication userAuth, int id) {
return complaintRepository.getComplaintById(userAuth, id);
......
......@@ -21,6 +21,7 @@ public class NotificationService {
@Autowired
private JdbcTemplate jdbcTemplate;
public void notifyComment(String username, int complaintId, String commentTitle, String commentContent,String status) {
String message = "New Comment: " + commentTitle + "\n" + commentContent;
saveNotification(complaintId,username, message,status,commentTitle);
......@@ -33,8 +34,8 @@ public class NotificationService {
System.out.println("Notification for complaint status change: " + newStatus);
}
private void saveNotification(int complaintId,String username,
String message,String status,String commentTitle) {
void saveNotification(int complaintId, String username,
String message, String status, String commentTitle) {
String userName= getUserByComplaint(complaintId).getPolicyNumber();
String complaintTitle =getUserByComplaint(complaintId).getTitle();
......@@ -103,4 +104,5 @@ public class NotificationService {
}
}
package com.project.cms.complaint;
import com.project.cms.complaint.Complaint;
import com.project.cms.complaint.ComplaintController;
import com.project.cms.complaint.ComplaintService;
import com.project.cms.comment.Comment;
import com.project.cms.comment.CommentService;
import com.project.cms.complaint.enums.FCAType;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.project.cms.notification.NotificationController;
import com.project.cms.comment.CommentService;
import com.project.cms.register.User;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.security.core.Authentication;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.ModelAndView;
import java.sql.Timestamp;
import java.util.HashMap;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class ComplaintControllerTest {
@Mock
......
package com.project.cms.complaint;
import org.junit.jupiter.api.Test;
import org.springframework.security.core.Authentication;
import org.mockito.Mockito;
import static org.junit.jupiter.api.Assertions.*;
public class ComplaintRespositoryImpTest {
private ComplaintRepositoryImpl complaintRepository = new ComplaintRepositoryImpl();
@Test
public void test_setAssignedStaff() {
// Create a mock Authentication object for testing
Authentication userAuth = Mockito.mock(Authentication.class);
// Set up the necessary conditions for the test
int complaintId = 1;
String assignedStaffName = "John";
// Invoke the method being tested
complaintRepository.setAssignedStaff(userAuth, complaintId, assignedStaffName);
}
// setAssignedStaff method throws NullPointerException when userAuth is null
@Test
public void test_setAssignedStaff_throwsNullPointerExceptionWhenUserAuthIsNull() {
// Arrange
Authentication userAuth = null;
int complaintId = 1;
String assignedStaff = "1";
// Act and Assert
assertThrows(NullPointerException.class, () -> {
complaintRepository.setAssignedStaff(userAuth, complaintId, assignedStaff);
});
}
}
......@@ -3,15 +3,10 @@ package com.project.cms.complaint;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.mockito.Mockito;
import org.springframework.security.core.Authentication;
import org.springframework.web.servlet.ModelAndView;
public class ComplaintTest {
......@@ -47,24 +42,6 @@ public class ComplaintTest {
assertEquals("John Doe", complaint.getAssignedStaff());
}
// Creating a new Complaint object with an empty title should throw an exception.
@Test
public void test_createComplaintObjectWithEmptyTitle() {
assertThrows(IllegalArgumentException.class, () -> new Complaint("", "12345", true));
}
// Setting the text box of a Complaint object with less than 10 characters should throw an exception.
@Test
public void test_setTextBoxWithLessThan10Characters() {
Complaint complaint = new Complaint();
assertThrows(IllegalArgumentException.class, () -> complaint.setTextBox("Short"));
}
// Setting the text box of a Complaint object with more than 250 characters should throw an exception.
@Test
public void test_setTextBoxWithMoreThan250Characters() {
Complaint complaint = new Complaint();
String longTextBox = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, nunc id aliquam lacinia, nisl nunc tincidunt nunc, nec lacinia nunc nisl nec nisi. Nulla facilisi. Sed id semper mauris. Sed euismod, nunc id aliquam lacinia, nisl nunc tincidunt nunc, nec lacinia nunc nisl nec nisi. Nulla facilisi. Sed id semper mauris.";
assertThrows(IllegalArgumentException.class, () -> complaint.setTextBox(longTextBox));
}
}
\ No newline at end of file
package com.project.cms.notification;
import com.project.cms.comment.Comment;
import com.project.cms.complaint.Complaint;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class NotificationServiceTest {
// NotificationService can notify a user of a new comment on a complaint
@Test
public void test_notifyComment() {
// Arrange
String username = "testUser";
int complaintId = 1;
String commentTitle = "Test Comment";
String commentContent = "This is a test comment";
String status = "Open";
// Create a mock of the NotificationService class
NotificationService notificationService = Mockito.mock(NotificationService.class);
// Act
notificationService.notifyComment(username, complaintId, commentTitle, commentContent, status);
// Assert
Mockito.verify(notificationService, Mockito.times(1)).notifyComment(username, complaintId, commentTitle, commentContent, status);
}
// NotificationService can notify a user of a change in complaint status
@Test
public void test_notifyComplaintStatusChange() {
// Arrange
String username = "testUser";
int complaintId = 1;
String newStatus = "Resolved";
String commentTitle = "Test Comment";
// Create a mock of the NotificationService class
NotificationService notificationService = Mockito.mock(NotificationService.class);
// Act
notificationService.notifyComplaintStatusChange(username, complaintId, newStatus, commentTitle);
// Assert
Mockito.verify(notificationService, Mockito.times(1)).notifyComplaintStatusChange(username, complaintId, newStatus, commentTitle);
}
// NotificationService can save a notification to the database
@Test
public void test_saveNotification() {
// Arrange
int complaintId = 1;
String username = "testUser";
String message = "Test notification";
String status = "Open";
String commentTitle = "Test Comment";
// Create a mock of the NotificationService class
NotificationService notificationService = Mockito.mock(NotificationService.class);
// Act
notificationService.saveNotification(complaintId, username, message, status, commentTitle);
// Assert
Mockito.verify(notificationService, Mockito.times(1)).saveNotification(complaintId, username, message, status, commentTitle);
}
// NotificationService can handle the case when a complaint has no linked comments
@Test
public void test_getAllComments_NoComments() {
// Arrange
// Create a mock of the NotificationService class
NotificationService notificationService = Mockito.mock(NotificationService.class);
// Stub the getAllComments method to return an empty list
Mockito.when(notificationService.getAllComments()).thenReturn(Collections.emptyList());
// Act
List<Comment> comments = notificationService.getAllComments();
// Assert
assertEquals(0, comments.size());
}
// NotificationService can handle the case when a complaint has no linked complaints
@Test
public void test_getNotification_NoComplaints() {
// Arrange
String username = "testUser";
// Create a mock of the NotificationService class
NotificationService notificationService = Mockito.mock(NotificationService.class);
// Stub the getNotification method to return an empty list
Mockito.when(notificationService.getNotification(username)).thenReturn(Collections.emptyList());
// Act
List<Notification> notifications = notificationService.getNotification(username);
// Assert
assertEquals(0, notifications.size());
}
// NotificationService can handle the case when a complaint has no assigned staff
@Test
public void test_getUserByComplaint_NoAssignedStaff() {
// Arrange
int complaintId = 1;
// Create a mock of the NotificationService class
NotificationService notificationService = Mockito.mock(NotificationService.class);
// Stub the getUserByComplaint method to return null
Mockito.when(notificationService.getUserByComplaint(complaintId)).thenReturn(null);
// Act
Complaint complaint = notificationService.getUserByComplaint(complaintId);
// Assert
assertNull(complaint);
}
}
......@@ -30,6 +30,7 @@ class RegistrationControllerTest {
private RegistrationController registrationController;
@Mock
private Model model;
@Mock
......@@ -95,4 +96,6 @@ class RegistrationControllerTest {
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment