Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
kasundie30 authored Sep 17, 2024
2 parents 5860e8d + 340f5eb commit d9e3842
Show file tree
Hide file tree
Showing 50 changed files with 1,374 additions and 413 deletions.
4 changes: 0 additions & 4 deletions back-end/src/main/java/com/example/demo/appuser/AppUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.HashSet;
import java.util.Set;


/**
* Represents an application user.
* This class is a JPA entity and implements Spring Security's UserDetails interface
Expand All @@ -26,8 +25,6 @@
@Entity
public class AppUser implements UserDetails {



/**
* The sequence generator for the primary key of the AppUser entity.
* This generator is used to generate unique IDs for each AppUser.
Expand All @@ -39,7 +36,6 @@ public class AppUser implements UserDetails {
)
@Id
@GeneratedValue(

strategy = GenerationType.SEQUENCE,
generator = "member_sequence"
)
Expand Down
17 changes: 9 additions & 8 deletions back-end/src/main/java/com/example/demo/task/TaskService.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public void addUserToTask(int task_ID, long userID){
taskRepository.save(task);
}

// public void addTaskWithUsers(Task task, Set<AppUser> taskMembers){
//
// task.setAssignedMembers(taskMembers);
// taskRepository.save(task);
// }
public void addTaskWithUsers(Task task, Set<AppUser> taskMembers){

task.setAssignedMembers(taskMembers);
taskRepository.save(task);
}


public void deleteTask(int task_ID){
Expand All @@ -61,8 +61,9 @@ public List<Task> findUserTasks(Long userID) {
return taskRepository.findTasksByUser(user);
}

public void deleteTaskUsers(int taskID,Long userID){
taskRepository.deleteUserFromTasks(taskID,userID);
}
// public void deleteTaskUsers(int taskID,Long userID){
// taskRepository.deleteUserFromTasks(taskID,userID);
// }
}


2 changes: 1 addition & 1 deletion back-end/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ server:
spring:
datasource:
url: jdbc:mysql://localhost:3306/cycle
password: "&*531cW9/?"
password:
username: root
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
Expand Down
133 changes: 133 additions & 0 deletions back-end/src/test/java/com/example/demo/DeliverableServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// JUnit Testing For DeliverableService

package com.example.demo;

import com.example.demo.Deliverables.Deliverable;
import com.example.demo.Deliverables.DeliverableNotFoundException;
import com.example.demo.Deliverables.DeliverableRepository;
import com.example.demo.Deliverables.DeliverableService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

@SpringBootTest
public class DeliverableServiceTest {

@Mock
private DeliverableRepository deliverableRepo;

@InjectMocks
private DeliverableService deliverableService;

private Deliverable deliverable;

@BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);

deliverable = new Deliverable();
deliverable.setDeliverableRelatedNo("D001");
deliverable.setWorkPackageNo("WP001");
deliverable.setDeliverableNo("DEL001");
deliverable.setDeliverableName("Initial Report");
deliverable.setDescription("This is a test description.");
deliverable.setLeadBeneficiary("Lead1");
deliverable.setType("Report");
deliverable.setDisseminationLevel("Public");
deliverable.setDueDate(LocalDate.of(2024, 12, 31));
}

/* Testing that the service successfully adds a new deliverable by saving it
to the repository and returns the added deliverable */
@Test
public void testAddDeliverable() {
when(deliverableRepo.save(any(Deliverable.class))).thenReturn(deliverable);
Deliverable newDeliverable = deliverableService.addDeliverable(deliverable);
assertNotNull(newDeliverable);
assertEquals("D001", newDeliverable.getDeliverableRelatedNo());
}

/* Testing that the service retrieves all deliverables from the repository and returns a
list containing the deliverables, checking that the list is not empty and contains the correct deliverable */
@Test
public void testGetAllDeliverables() {
List<Deliverable> deliverables = new ArrayList<>();
deliverables.add(deliverable);
when(deliverableRepo.findAll()).thenReturn(deliverables);

List<Deliverable> result = deliverableService.getAllDeliverables();
assertFalse(result.isEmpty());
assertEquals(1, result.size());
assertEquals("D001", result.get(0).getDeliverableRelatedNo());
}

/* Testing that the service retrieves a specific deliverable by its ID from the repository
and returns the correct deliverable when found */
@Test
public void testGetDeliverableById() {
when(deliverableRepo.findById("D001")).thenReturn(Optional.of(deliverable));
Deliverable result = deliverableService.getDeliverableById("D001");
assertNotNull(result);
assertEquals("D001", result.getDeliverableRelatedNo());
}

/* Testing that the service throws a DeliverableNotFoundException when attempting to retrieve
a deliverable with an ID that does not exist in the repository */
@Test
public void testGetDeliverableById_NotFound() {
when(deliverableRepo.findById("D002")).thenReturn(Optional.empty());
Exception exception = assertThrows(DeliverableNotFoundException.class, () -> {
deliverableService.getDeliverableById("D002");
});
assertEquals("Could not found the Deliverable with ID D002", exception.getMessage());
}

/* Testing that the service updates an existing deliverable in the repository and returns
the updated deliverable when the deliverable is found and updated successfully */
@Test
public void testUpdateDeliverable() {
Deliverable updatedDeliverable = new Deliverable();
updatedDeliverable.setDeliverableRelatedNo("D001");
updatedDeliverable.setDeliverableName("Updated Report");

when(deliverableRepo.findById("D001")).thenReturn(Optional.of(deliverable));
when(deliverableRepo.save(any(Deliverable.class))).thenReturn(updatedDeliverable);

Deliverable result = deliverableService.updateDeliverable("D001", updatedDeliverable);
assertNotNull(result);
assertEquals("Updated Report", result.getDeliverableName());
}

/* Testing that the service successfully deletes a deliverable by its ID when it exists
in the repository, and returns a confirmation message */
@Test
public void testDeleteDeliverable() {
when(deliverableRepo.existsById("D001")).thenReturn(true);
String result = deliverableService.deleteDeliverable("D001");
verify(deliverableRepo, times(1)).deleteById("D001");
assertEquals("Deliverable with id D001 has been deleted!", result);
}

/* Testing that the service throws a DeliverableNotFoundException when attempting to delete a deliverable
with an ID that does not exist in the repository */
@Test
public void testDeleteDeliverable_NotFound() {
when(deliverableRepo.existsById("D002")).thenReturn(false);
Exception exception = assertThrows(DeliverableNotFoundException.class, () -> {
deliverableService.deleteDeliverable("D002");
});
assertEquals("Could not found the Deliverable with ID D002", exception.getMessage());
}
}

37 changes: 23 additions & 14 deletions back-end/src/test/java/com/example/demo/FileControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// JUnit Testing For FileController

package com.example.demo;

import com.example.demo.download.FileController;
Expand All @@ -8,13 +10,11 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;
Expand All @@ -33,30 +33,37 @@ public class FileControllerTest {

private FileEntity fileEntity;

// @BeforeEach
// void setUp() {
// MockitoAnnotations.openMocks(this);
// fileEntity = new FileEntity("test.txt", "text/plain", "Hello World".getBytes());
// fileEntity.setFileId(1L);
// }
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
fileEntity = new FileEntity("test.txt", "text/plain", "Hello World".getBytes(), "Test File", true);
fileEntity.setFileId(1L);
}

// Testing that the controller successfully retrieves a list of all files through the service
@Test
void testGetAllFiles() {
when(fileService.getAllFiles()).thenReturn(Arrays.asList(fileEntity));


List<FileEntity> files = fileController.getAllFiles();

assertEquals(1, files.size());
verify(fileService, times(1)).getAllFiles();
}

// Testing that the controller correctly handles the file download, including content type and disposition headers
@Test
void testDownloadFile() {
when(fileService.getFileById(1L)).thenReturn(Optional.of(fileEntity));
ResponseEntity<Resource> response = fileController.downloadFile(1L);

assertEquals(MediaType.parseMediaType("text/plain"), response.getHeaders().getContentType());
assertEquals("attachment; filename=\"test.txt\"", response.getHeaders().getContentDisposition().toString());
verify(fileService, times(1)).getFileById(1L);
}

// Testing that the controller processes a file upload correctly and returns a successful response message
@Test
void testUploadFile() {
MockMultipartFile mockMultipartFile = new MockMultipartFile(
Expand All @@ -65,11 +72,13 @@ void testUploadFile() {
"text/plain",
"Hello World".getBytes()
);
String displayName = "Test File";
boolean visibleToAll = true;

ResponseEntity<String> responseEntity = fileController.uploadFile(mockMultipartFile, null, displayName, visibleToAll);

// ResponseEntity<String> responseEntity = fileController.uploadFile(mockMultipartFile);
//
// assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
// assertEquals("File uploaded successfully", responseEntity.getBody());
// verify(fileService, times(1)).saveFile(any(FileEntity.class));
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
assertEquals("Content uploaded successfully", responseEntity.getBody());
verify(fileService, times(1)).saveFile(any(FileEntity.class));
}
}
}
105 changes: 105 additions & 0 deletions back-end/src/test/java/com/example/demo/FileServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// JUnit Testing For FileService

package com.example.demo;

import com.example.demo.download.FileEntity;
import com.example.demo.download.FileRepository;
import com.example.demo.download.FileService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

public class FileServiceTest {

@Mock
private FileRepository fileRepository;

@InjectMocks
private FileService fileService;

private FileEntity fileEntity;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
fileEntity = new FileEntity("test.txt", "text/plain", "Hello World".getBytes(), "Test File", true);
fileEntity.setFileId(1L);
}

// Testing that the service method correctly fetches all files from the repository
@Test
void testGetAllFiles() {
when(fileRepository.findAll()).thenReturn(Arrays.asList(fileEntity));

List<FileEntity> files = fileService.getAllFiles();

assertNotNull(files);
assertEquals(1, files.size());
assertEquals("test.txt", files.get(0).getFileName());
verify(fileRepository, times(1)).findAll();
}

// Testing that the service method retrieves a file by its ID from the repository
@Test
void testGetFileById() {
when(fileRepository.findById(1L)).thenReturn(Optional.of(fileEntity));


Optional<FileEntity> foundFile = fileService.getFileById(1L);

assertTrue(foundFile.isPresent());
assertEquals("test.txt", foundFile.get().getFileName());
verify(fileRepository, times(1)).findById(1L);
}

// Testing that the service method successfully saves a file in the repository
@Test
void testSaveFile() {
when(fileRepository.save(fileEntity)).thenReturn(fileEntity);

FileEntity savedFile = fileService.saveFile(fileEntity);

assertNotNull(savedFile);
assertEquals("test.txt", savedFile.getFileName());
verify(fileRepository, times(1)).save(fileEntity);
}

// Testing that the service method correctly deletes a file by its ID from the repository
@Test
void testDeleteFile() {
fileService.deleteFile(1L);

verify(fileRepository, times(1)).deleteById(1L);
}

// Test with a save failure and checks that if the service properly handles the exception
@Test
void testSaveFileException() {
when(fileRepository.save(any(FileEntity.class))).thenThrow(new RuntimeException("Save failed"));

RuntimeException thrown = assertThrows(RuntimeException.class, () -> {
fileService.saveFile(fileEntity);
});
assertEquals("Failed to save file", thrown.getMessage());
}

// Test with a delete failure and ensures that if the service handles the exception correctly
@Test
void testDeleteFileException() {
doThrow(new RuntimeException("Delete failed")).when(fileRepository).deleteById(1L);

RuntimeException thrown = assertThrows(RuntimeException.class, () -> {
fileService.deleteFile(1L);
});
assertEquals("Failed to delete file", thrown.getMessage());
}
}
Loading

0 comments on commit d9e3842

Please sign in to comment.