Skip to content

Commit

Permalink
update some UT to use interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
zakariamaaraki committed May 12, 2024
1 parent be741eb commit 8274a94
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Repository
public class ExecutionRepositoryDefault implements ExecutionRepository {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,88 @@
package com.cp.compiler.repositories;public class ExecutionRepositoryDefaultTests {
package com.cp.compiler.repositories;

import com.cp.compiler.executions.Execution;
import com.cp.compiler.repositories.executions.ExecutionRepository;
import com.cp.compiler.repositories.executions.ExecutionRepositoryDefault;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.List;
import java.util.UUID;

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

class ExecutionRepositoryDefaultTests {

private ExecutionRepository executionRepository;

@BeforeEach
void setUp() {
executionRepository = new ExecutionRepositoryDefault();
}

@Test
void addExecution_shouldAddExecutionToRepository() {
// Given
Execution execution = Mockito.mock(Execution.class);
when(execution.getId()).thenReturn(UUID.randomUUID().toString());

// When
Execution addedExecution = executionRepository.addExecution(execution);

// Then
assertNotNull(addedExecution);
assertEquals(execution, addedExecution);
assertTrue(executionRepository.getExecutions().contains(execution));
}

@Test
void removeExecution_shouldRemoveExecutionFromRepository() {
// Given
Execution execution = Mockito.mock(Execution.class);
String executionId = UUID.randomUUID().toString();
when(execution.getId()).thenReturn(executionId);
executionRepository.addExecution(execution);

// When
Execution removedExecution = executionRepository.removeExecution(executionId);

// Then
assertNotNull(removedExecution);
assertEquals(execution, removedExecution);
assertFalse(executionRepository.getExecutions().contains(execution));
}

@Test
void getExecutions_shouldReturnAllExecutions() {
// Given
Execution execution1 = Mockito.mock(Execution.class);
when(execution1.getId()).thenReturn(UUID.randomUUID().toString());
Execution execution2 = Mockito.mock(Execution.class);
when(execution2.getId()).thenReturn(UUID.randomUUID().toString());
executionRepository.addExecution(execution1);
executionRepository.addExecution(execution2);

// When
List<Execution> executions = executionRepository.getExecutions();

// Then
assertNotNull(executions);
assertEquals(2, executions.size());
assertTrue(executions.contains(execution1));
assertTrue(executions.contains(execution2));
}

@Test
void removeExecution_shouldReturnNullIfExecutionDoesNotExist() {
// Given
String nonExistentExecutionId = UUID.randomUUID().toString();

// When
Execution removedExecution = executionRepository.removeExecution(nonExistentExecutionId);

// Then
assertNull(removedExecution);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.cp.compiler.contract.problems.Problem;
import com.cp.compiler.exceptions.problems.InvalidProblemException;
import com.cp.compiler.exceptions.problems.ProblemNotFoundException;
import com.cp.compiler.repositories.problems.ProblemsRepository;
import com.cp.compiler.repositories.problems.ProblemsRepositoryDefault;
import com.fasterxml.jackson.core.type.TypeReference;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -25,7 +26,7 @@ class ProblemsRepositoryTests {
private TypeReference<ArrayList<Problem>> typeReference;

@InjectMocks
private ProblemsRepositoryDefault problemsRepository;
private ProblemsRepository problemsRepository;

@BeforeEach
void setUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.cp.compiler.models.testcases.TransformedTestCase;
import com.cp.compiler.repositories.executions.ExecutionRepository;
import com.cp.compiler.services.platform.containers.ContainerService;
import com.cp.compiler.services.platform.garbagecollector.ExecutionGarbageCollector;
import com.cp.compiler.services.platform.garbagecollector.ExecutionGarbageCollectorDefault;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -22,7 +23,7 @@ class ExecutionGarbageCollectorDefaultTests {

private ExecutionRepository executionRepository;
private ContainerService containerService;
private ExecutionGarbageCollectorDefault garbageCollector;
private ExecutionGarbageCollector garbageCollector;

@BeforeEach
void setUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void shouldReturnMaxCpus() {
@Test
void allowNewExecutionShouldReturnTrue() {
// Given
var resources = new ResourcesDefault(MAX_CPUS, MAX_REQUESTS);
Resources resources = new ResourcesDefault(MAX_CPUS, MAX_REQUESTS);

// When
boolean allowNewExecution = resources.allowNewExecution();
Expand All @@ -37,7 +37,7 @@ void allowNewExecutionShouldReturnTrue() {
@Test
void reserveResourcesShouldIncrementTheCounter() {
// Given
var resources = new ResourcesDefault(MAX_CPUS, MAX_REQUESTS);
Resources resources = new ResourcesDefault(MAX_CPUS, MAX_REQUESTS);

// When
int counter = resources.reserveResources();
Expand All @@ -49,7 +49,7 @@ void reserveResourcesShouldIncrementTheCounter() {
@Test
void cleanupShouldDecrementTheCounter() {
// Given
var resources = new ResourcesDefault(MAX_CPUS, MAX_REQUESTS);
Resources resources = new ResourcesDefault(MAX_CPUS, MAX_REQUESTS);
resources.reserveResources();

// When
Expand All @@ -62,7 +62,7 @@ void cleanupShouldDecrementTheCounter() {
@Test
void cleanupShouldReturnZero() {
// Given
var resources = new ResourcesDefault(MAX_CPUS, MAX_REQUESTS);
Resources resources = new ResourcesDefault(MAX_CPUS, MAX_REQUESTS);

// When
int counter = resources.cleanup();
Expand All @@ -74,7 +74,7 @@ void cleanupShouldReturnZero() {
@Test
void shouldReturnNumberOfExecutions() {
// Given
var resources = new ResourcesDefault(MAX_CPUS, MAX_REQUESTS);
Resources resources = new ResourcesDefault(MAX_CPUS, MAX_REQUESTS);

// When
boolean allow = resources.allowNewExecution();
Expand All @@ -88,7 +88,7 @@ void shouldReturnNumberOfExecutions() {
@Test
void shouldReturnMaxNumberOfRequests() {
// Given
var resources = new ResourcesDefault(MAX_CPUS, MAX_REQUESTS);
Resources resources = new ResourcesDefault(MAX_CPUS, MAX_REQUESTS);

// When
int maxRequests = resources.getMaxRequests();
Expand All @@ -100,7 +100,7 @@ void shouldReturnMaxNumberOfRequests() {
@Test
void getAvailableResourcesShouldReturnTheRightValue() {
// Given
var resources = new ResourcesDefault(MAX_CPUS, MAX_REQUESTS);
Resources resources = new ResourcesDefault(MAX_CPUS, MAX_REQUESTS);

// When
AvailableResources availableResources = resources.getAvailableResources();
Expand Down

0 comments on commit 8274a94

Please sign in to comment.