From 7a67d8b1fc950bfad134575d461aa424d8c03350 Mon Sep 17 00:00:00 2001 From: George Tziavas <g.tziavas@ac.upatras.gr> Date: Mon, 9 Sep 2024 12:39:17 +0300 Subject: [PATCH] modified: pom.xml modified: src/main/java/org/etsi/osl/metrico/model/Job.java modified: src/main/java/org/etsi/osl/metrico/services/JobService.java new file: src/test/java/org/etsi/osl/metrico/model/JobTest.java new file: src/test/java/org/etsi/osl/metrico/services/JobServiceTest.java new file: src/test/resources/application.yml --- pom.xml | 68 +++-------- .../java/org/etsi/osl/metrico/model/Job.java | 24 ++-- .../etsi/osl/metrico/services/JobService.java | 2 +- .../org/etsi/osl/metrico/model/JobTest.java | 72 ++++++++++++ .../osl/metrico/services/JobServiceTest.java | 106 ++++++++++++++++++ src/test/resources/application.yml | 8 ++ 6 files changed, 218 insertions(+), 62 deletions(-) create mode 100644 src/test/java/org/etsi/osl/metrico/model/JobTest.java create mode 100644 src/test/java/org/etsi/osl/metrico/services/JobServiceTest.java create mode 100644 src/test/resources/application.yml diff --git a/pom.xml b/pom.xml index 3220e40..b0054cc 100644 --- a/pom.xml +++ b/pom.xml @@ -176,78 +176,38 @@ <version>${mysql-connector.version}</version> </dependency> - <!-- Testing --> - <!-- <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-test</artifactId> - <scope>test</scope> - </dependency> + <!-- Testing --> <dependency> <groupId>org.junit.jupiter</groupId> - <artifactId>junit-jupiter-engine</artifactId> + <artifactId>junit-jupiter-api</artifactId> + <version>5.10.1</version> <scope>test</scope> </dependency> - <dependency> - <groupId>org.junit.platform</groupId> - <artifactId>junit-platform-commons</artifactId> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <version>5.7.0</version> <scope>test</scope> </dependency> - <dependency> - <groupId>org.junit.platform</groupId> - <artifactId>junit-platform-runner</artifactId> + <groupId>com.h2database</groupId> + <artifactId>h2</artifactId> <scope>test</scope> </dependency> <dependency> - <groupId>org.apache.activemq</groupId> - <artifactId>activemq-broker</artifactId> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-test-autoconfigure</artifactId> + <version>3.2.2</version> <scope>test</scope> </dependency> - - <dependency> - <groupId>javax.annotation</groupId> - <artifactId>javax.annotation-api</artifactId> - <version>1.3.2</version> - </dependency> - <dependency> - <groupId>io.swagger.core.v3</groupId> - <artifactId>swagger-annotations</artifactId> - <version>2.1.11</version> - </dependency> - - <dependency> - <groupId>jakarta.validation</groupId> - <artifactId>jakarta.validation-api</artifactId> - <version>3.0.0</version> - </dependency> - - <dependency> - <groupId>org.openapitools</groupId> - <artifactId>jackson-databind-nullable</artifactId> - <version>0.2.1</version> - </dependency> - - <dependency> - <groupId>javax.jms</groupId> - <artifactId>javax.jms-api</artifactId> - <version>2.0.1</version> + <groupId>org.springframework</groupId> + <artifactId>spring-test</artifactId> + <version>6.1.3</version> <scope>test</scope> </dependency> - <dependency> - <groupId>commons-io</groupId> - <artifactId>commons-io</artifactId> - <version>2.15.1</version> - <scope>test</scope> - </dependency> --> - <!-- <dependency>--> - <!-- <groupId>org.springframework</groupId>--> - <!-- <artifactId>spring-web</artifactId>--> - <!-- <version>6.1.5</version>--> - <!-- </dependency>--> </dependencies> <build> diff --git a/src/main/java/org/etsi/osl/metrico/model/Job.java b/src/main/java/org/etsi/osl/metrico/model/Job.java index fba21c0..8724ee8 100644 --- a/src/main/java/org/etsi/osl/metrico/model/Job.java +++ b/src/main/java/org/etsi/osl/metrico/model/Job.java @@ -4,19 +4,13 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; import lombok.Getter; import lombok.Setter; -import org.etsi.osl.metrico.reposervices.JobRepoService; import org.etsi.osl.tmf.pm628.model.ExecutionStateType; import org.hibernate.annotations.GenericGenerator; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.format.annotation.DateTimeFormat; -import java.time.Duration; import java.time.OffsetDateTime; -import java.util.Map; import java.util.UUID; -import java.util.concurrent.*; +import java.util.concurrent.ScheduledFuture; @Getter @Setter @@ -84,4 +78,20 @@ public class Job{ this(startDateTime, endDateTime); this.executionInterval = executionInterval; } + + @Override + public String toString() { + return "Job{" + + "uuid=" + uuid + + ", state=" + state + + ", startDateTime=" + startDateTime + + ", endDateTime=" + endDateTime + + ", executionInterval=" + executionInterval + + ", dataAccessEndPointRef=" + dataAccessEndPointRef + + ", scheduleDefinitionRef=" + scheduleDefinitionRef + + ", measurementCollectionJobRef=" + measurementCollectionJobRef + + ", deleted=" + deleted + + '}'; + } + } diff --git a/src/main/java/org/etsi/osl/metrico/services/JobService.java b/src/main/java/org/etsi/osl/metrico/services/JobService.java index 6280a9e..4c5e3ca 100644 --- a/src/main/java/org/etsi/osl/metrico/services/JobService.java +++ b/src/main/java/org/etsi/osl/metrico/services/JobService.java @@ -19,6 +19,7 @@ import java.util.concurrent.*; public class JobService { private static final Logger logger = LoggerFactory.getLogger(JobService.class); + @Getter private static final Map<UUID, Job> jobs = new ConcurrentHashMap<>(); @Getter @@ -52,7 +53,6 @@ public class JobService { public Job startJob(Runnable task, OffsetDateTime startDateTime, OffsetDateTime endDateTime, Integer executionInterval) { Job job = new Job(startDateTime, endDateTime, executionInterval); long initialDelay = Duration.between(OffsetDateTime.now(), startDateTime).getSeconds(); - job.setState(ExecutionStateType.PENDING); try{ job = jobRepoService.createAndSaveJob(); } catch (NullPointerException e){ diff --git a/src/test/java/org/etsi/osl/metrico/model/JobTest.java b/src/test/java/org/etsi/osl/metrico/model/JobTest.java new file mode 100644 index 0000000..31a6c29 --- /dev/null +++ b/src/test/java/org/etsi/osl/metrico/model/JobTest.java @@ -0,0 +1,72 @@ +package org.etsi.osl.metrico.model; + +import org.etsi.osl.tmf.pm628.model.ExecutionStateType; +import org.junit.jupiter.api.Test; + +import java.time.OffsetDateTime; +import java.time.temporal.ChronoUnit; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +class JobTest { + + @Test + void testDefaultConstructor() { + Job job = new Job(); + assertEquals(ExecutionStateType.PENDING, job.getState()); + assertEquals(10, job.getExecutionInterval()); + assertNotNull(job.getStartDateTime()); + assertTrue(ChronoUnit.SECONDS.between(OffsetDateTime.now(), job.getStartDateTime()) <= 1); + } + + @Test + void testConstructorWithStopDateTime() { + OffsetDateTime stopDateTime = OffsetDateTime.now().plusDays(1); + Job job = new Job(stopDateTime); + assertEquals(stopDateTime, job.getEndDateTime()); + assertEquals(ExecutionStateType.PENDING, job.getState()); + assertEquals(10, job.getExecutionInterval()); + } + + @Test + void testConstructorWithStartAndStopDateTime() { + OffsetDateTime startDateTime = OffsetDateTime.now(); + OffsetDateTime stopDateTime = startDateTime.plusDays(1); + Job job = new Job(startDateTime, stopDateTime); + assertEquals(startDateTime, job.getStartDateTime()); + assertEquals(stopDateTime, job.getEndDateTime()); + assertEquals(ExecutionStateType.PENDING, job.getState()); + assertEquals(10, job.getExecutionInterval()); + } + + @Test + void testConstructorWithAllParams() { + OffsetDateTime startDateTime = OffsetDateTime.now(); + OffsetDateTime stopDateTime = startDateTime.plusDays(1); + Integer executionInterval = 15; + Job job = new Job(startDateTime, stopDateTime, executionInterval); + assertEquals(startDateTime, job.getStartDateTime()); + assertEquals(stopDateTime, job.getEndDateTime()); + assertEquals(executionInterval, job.getExecutionInterval()); + assertEquals(ExecutionStateType.PENDING, job.getState()); + } + + @Test + void testToString() { + Job job = new Job(); + job.setUuid(UUID.fromString("123e4567-e89b-12d3-a456-426614174000")); + job.setState(ExecutionStateType.PENDING); + job.setStartDateTime(OffsetDateTime.parse("2023-01-01T10:15:30+01:00")); + job.setEndDateTime(OffsetDateTime.parse("2023-01-02T10:15:30+01:00")); + job.setExecutionInterval(10); + job.setDataAccessEndPointRef(UUID.fromString("123e4567-e89b-12d3-a456-426614174001")); + job.setScheduleDefinitionRef(UUID.fromString("123e4567-e89b-12d3-a456-426614174002")); + job.setMeasurementCollectionJobRef(UUID.fromString("123e4567-e89b-12d3-a456-426614174003")); + job.setDeleted(false); + + String expected = "Job{uuid=123e4567-e89b-12d3-a456-426614174000, state=pending, startDateTime=2023-01-01T10:15:30+01:00, endDateTime=2023-01-02T10:15:30+01:00, executionInterval=10, dataAccessEndPointRef=123e4567-e89b-12d3-a456-426614174001, scheduleDefinitionRef=123e4567-e89b-12d3-a456-426614174002, measurementCollectionJobRef=123e4567-e89b-12d3-a456-426614174003, deleted=false}"; + assertEquals(expected, job.toString()); + } + +} \ No newline at end of file diff --git a/src/test/java/org/etsi/osl/metrico/services/JobServiceTest.java b/src/test/java/org/etsi/osl/metrico/services/JobServiceTest.java new file mode 100644 index 0000000..9d6dc0b --- /dev/null +++ b/src/test/java/org/etsi/osl/metrico/services/JobServiceTest.java @@ -0,0 +1,106 @@ +package org.etsi.osl.metrico.services; + +import org.etsi.osl.metrico.model.Job; +import org.etsi.osl.metrico.reposervices.JobRepoService; +import org.etsi.osl.tmf.pm628.model.ExecutionStateType; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.slf4j.Logger; + +import java.time.OffsetDateTime; +import java.util.UUID; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +class JobServiceTest { + + @Mock + private JobRepoService jobRepoService; + + @Mock + private ScheduledExecutorService scheduler; + + @Mock + private Logger logger; + + private JobService jobService; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + jobService = new JobService(jobRepoService); + } + + @Test + void testStartJob_Success() { + // Initialize the job with specific parameters + OffsetDateTime startDateTime = OffsetDateTime.now().plusSeconds(5); + OffsetDateTime endDateTime = startDateTime.plusMinutes(10); + Integer executionInterval = 5; + Runnable task = () -> {}; + Job expectedJob = new Job(startDateTime, endDateTime, executionInterval); + // Assert the initial state is PENDING + assertEquals(expectedJob.getState(), ExecutionStateType.PENDING); + + // Set the UUID and state of the expected job after it has been saved to the db + + expectedJob.setState(ExecutionStateType.INPROGRESS); + + // Mock jobRepoService.createAndSaveJob() to return the expected Job + when(jobRepoService.createAndSaveJob()).thenReturn(expectedJob); + +// // Mock scheduler.scheduleAtFixedRate() to not throw an exception +// ScheduledFuture<?> mockFuture = mock(ScheduledFuture.class); +// when(scheduler.scheduleAtFixedRate(any(Runnable.class), eq(5L), eq(5L), eq(TimeUnit.SECONDS))) +// .thenReturn(mockFuture); + +// // Call jobService.startJob() with valid parameters +// Job resultJob = jobService.startJob(task, startDateTime, endDateTime, executionInterval); +// +// // Verify job state is INPROGRESS, and job is added to the jobs map +// assertEquals(ExecutionStateType.INPROGRESS, resultJob.getState()); +// assertNotNull(JobService.getJobs().get(resultJob.getUuid())); +// assertEquals(expectedJob, JobService.getJobs().get(resultJob.getUuid())); + } + @Test + void testStartJob_NullPointerException() { + // Mock jobRepoService.createAndSaveJob() to throw NullPointerException + // Call jobService.startJob() with valid parameters + // Verify job state is FAILED + } + + @Test + void testStartJob_RejectedExecutionException() { + // Mock scheduler.scheduleAtFixedRate() to throw RejectedExecutionException + // Call jobService.startJob() with valid parameters + // Verify job state is FAILED + } + + @Test + void testStopJob_InProgress() { + // Add a job to the jobs map with state INPROGRESS + // Call JobService.stopJob() with the job's UUID + // Verify job state is CANCELLED + } + + @Test + void testStopJob_AlreadyCompleted() { + // Add a job to the jobs map with state COMPLETED + // Call JobService.stopJob() with the job's UUID + // Verify job state remains COMPLETED + } + + @Test + void testStopJob_DoesNotExist() { + // Call JobService.stopJob() with a non-existent job UUID + // Verify appropriate warning is logged + } +} \ No newline at end of file diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml new file mode 100644 index 0000000..4e3970a --- /dev/null +++ b/src/test/resources/application.yml @@ -0,0 +1,8 @@ +spring: + datasource: + url: jdbc:h2:mem:testdb + driverClassName: org.h2.Driver + username: sa + password: password + jpa: + database-platform: org.hibernate.dialect.H2Dialect \ No newline at end of file -- GitLab