generated from cepdnaclk/eYY-XXX-project-template
-
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.
- Loading branch information
Showing
50 changed files
with
1,374 additions
and
413 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
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
133 changes: 133 additions & 0 deletions
133
back-end/src/test/java/com/example/demo/DeliverableServiceTest.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,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()); | ||
} | ||
} | ||
|
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
105 changes: 105 additions & 0 deletions
105
back-end/src/test/java/com/example/demo/FileServiceTest.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,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()); | ||
} | ||
} |
Oops, something went wrong.