Merge pull request 'issue-86' (#102) from issue-86 into master
All checks were successful
Java CI with Maven / build-and-push-frontend (push) Successful in 2m45s
Java CI with Maven / build-and-push-backend (push) Successful in 1m31s

Reviewed-on: #102
This commit is contained in:
sebastian 2024-03-14 14:57:43 +01:00
commit 574c86af7e
40 changed files with 838 additions and 1256 deletions

View File

@ -3,25 +3,6 @@ run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Set up Maven
uses: stCarolas/setup-maven@v4.5
with:
maven-version: 3.8.2
- name: Build with Maven
run: mvn -B package --file backend/pom.xml
build-and-push-frontend:
runs-on: ubuntu-latest
steps:

View File

@ -21,6 +21,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>

View File

@ -2,15 +2,25 @@ package core;
import core.entities.RoleEntity;
import core.entities.UserRole;
import core.entities.timemanager.AdvancedTaskSchedule;
import core.repositories.RoleRepository;
import core.repositories.UserRepository;
import core.services.PropertyService;
import core.services.TaskScheduleService;
import core.services.ntfy.TaskSchedulingService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
@SpringBootApplication
@EnableScheduling
public class DemoApplication{
public static void main(String[] args) {
@ -18,19 +28,12 @@ public class DemoApplication{
}
/*@Bean
public CommandLineRunner init(RoleRepository roleRepository, UserRepository userRepository, PropertyService propertyService) {
@Bean
public CommandLineRunner init(TaskSchedulingService taskSchedulingService) {
return args -> {
for (UserRole userRole : UserRole.values()) {
if(!roleRepository.existsByName(userRole)) {
roleRepository.save(new RoleEntity(userRole));
}
}
propertyService.init();
FirstUserObserver observer = new FirstUserObserver(userRepository);
observer.start();
taskSchedulingService.scheduleStartReminders();
};
}*/
}
}

View File

@ -0,0 +1,55 @@
package core.api.controller;
import core.api.models.auth.SimpleStatusResponse;
import core.api.models.users.NtfyInformation;
import core.entities.User;
import core.repositories.UserRepository;
import core.services.UserService;
import core.services.ntfy.TaskSchedulingService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.Optional;
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api")
public class NtfyController {
@Autowired private UserRepository userRepository;
@Autowired private UserService userService;
@Autowired private TaskSchedulingService taskSchedulingService;
@GetMapping("/ntfy")
public ResponseEntity<?> getNtfyInformation() {
String username = SecurityContextHolder.getContext().getAuthentication().getName();
Optional<User> userOptional = userRepository.findByUsername(username);
if(userOptional.isPresent()) {
return ResponseEntity.ok(new NtfyInformation(userOptional.get()));
} else {
return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
}
}
@PostMapping("/ntfy")
public ResponseEntity<?> editNtfyInformation(@Valid @RequestBody NtfyInformation ntfyInformation) {
String username = SecurityContextHolder.getContext().getAuthentication().getName();
Optional<User> user = userRepository.findByUsername(username);
if(user.isPresent()) {
userService.editNtfyInformation(user.get(), ntfyInformation);
try {
taskSchedulingService.scheduleStartReminders();
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
return ResponseEntity.ok(new SimpleStatusResponse("success"));
} else {
return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
}
}
}

View File

@ -9,6 +9,8 @@ import core.entities.timemanager.AdvancedTaskSchedule;
import core.entities.timemanager.BasicTaskSchedule;
import core.entities.timemanager.Task;
import core.services.*;
import core.services.ntfy.TaskSchedulingService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
@ -27,6 +29,7 @@ public class ScheduleController {
@Autowired private TaskScheduleService taskScheduleService;
@Autowired private TaskService taskService;
@Autowired private TaskSchedulingService taskSchedulingService;
@GetMapping("/schedules")
public ResponseEntity<?> loadAllSchedulesOfUser() {
String username = SecurityContextHolder.getContext().getAuthentication().getName();
@ -172,6 +175,7 @@ public class ScheduleController {
if(scheduleResult.getExitCode() == ServiceExitCode.ENTITY_ALREADY_EXIST) {
return ResponseEntity.status(409).body(new SimpleStatusResponse("failed"));
} else {
taskSchedulingService.sendRunningTaskNotification(scheduleResult.getResult());
return ResponseEntity.ok(scheduleResult.getResult().toScheduleInfo());
}
}
@ -198,6 +202,15 @@ public class ScheduleController {
}
ServiceResult<AbstractSchedule> serviceResult = taskScheduleService.activateSchedule(permissionResult.getResult());
if(serviceResult.getResult() instanceof AdvancedTaskSchedule) {
try {
taskSchedulingService.stopStartReminderNotification((AdvancedTaskSchedule) serviceResult.getResult());
} catch (SchedulerException e) {
throw new RuntimeException(e);
}
}
taskSchedulingService.sendRunningTaskNotification(serviceResult.getResult());
return ResponseEntity.ok(new ScheduleActivateResponse(serviceResult.getResult().getStartTime()));
}

View File

@ -0,0 +1,53 @@
package core.api.models.users;
import core.entities.User;
public class NtfyInformation {
private String ntfy_host;
private String ntfy_topic;
private String ntfy_user;
private String ntfy_token;
public NtfyInformation(User user) {
this.ntfy_host = user.getNtfy_host();
this.ntfy_topic = user.getNtfy_topic();
this.ntfy_user = "****";
this.ntfy_token = "****";
}
public NtfyInformation() {
}
public String getNtfy_host() {
return ntfy_host;
}
public void setNtfy_host(String ntfy_host) {
this.ntfy_host = ntfy_host;
}
public String getNtfy_topic() {
return ntfy_topic;
}
public void setNtfy_topic(String ntfy_topic) {
this.ntfy_topic = ntfy_topic;
}
public String getNtfy_user() {
return ntfy_user;
}
public void setNtfy_user(String ntfy_user) {
this.ntfy_user = ntfy_user;
}
public String getNtfy_token() {
return ntfy_token;
}
public void setNtfy_token(String ntfy_token) {
this.ntfy_token = ntfy_token;
}
}

View File

@ -42,6 +42,11 @@ public class User {
inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<RoleEntity> roles = new HashSet<>();
private String ntfy_host;
private String ntfy_topic;
private String ntfy_username;
private String ntfy_token;
public User() {
}
@ -98,6 +103,38 @@ public class User {
this.roles = roles;
}
public String getNtfy_host() {
return ntfy_host;
}
public void setNtfy_host(String ntfy_host) {
this.ntfy_host = ntfy_host;
}
public String getNtfy_topic() {
return ntfy_topic;
}
public void setNtfy_topic(String ntfy_topic) {
this.ntfy_topic = ntfy_topic;
}
public String getNtfy_username() {
return ntfy_username;
}
public void setNtfy_username(String ntfy_username) {
this.ntfy_username = ntfy_username;
}
public String getNtfy_token() {
return ntfy_token;
}
public void setNtfy_token(String ntfy_token) {
this.ntfy_token = ntfy_token;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View File

@ -0,0 +1,15 @@
package core.repositories.timemanager;
import core.entities.timemanager.AdvancedTaskSchedule;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.time.LocalDate;
import java.util.List;
@Repository
public interface AdvancedScheduleRepository extends CrudRepository<AdvancedTaskSchedule, Long> {
}

View File

@ -19,4 +19,5 @@ public interface ScheduleRepository extends CrudRepository<AbstractSchedule, Lon
@Query(value = "SELECT s FROM AbstractSchedule s WHERE s.task.taskgroup.user.username = ?1 AND s.startTime is NOT NULL and s.stopTime is NULL")
Optional<AbstractSchedule> getActiveScheduleOfUser(String username);
}

View File

@ -12,6 +12,7 @@ import core.entities.timemanager.AdvancedTaskSchedule;
import core.entities.timemanager.BasicTaskSchedule;
import core.entities.timemanager.Task;
import core.repositories.UserRepository;
import core.repositories.timemanager.AdvancedScheduleRepository;
import core.repositories.timemanager.ScheduleRepository;
import core.repositories.timemanager.TaskRepository;
import org.springframework.beans.factory.annotation.Autowired;
@ -32,6 +33,7 @@ public class TaskScheduleService {
@Autowired private UserRepository userRepository;
@Autowired
private TaskRepository taskRepository;
@Autowired private AdvancedScheduleRepository advancedScheduleRepository;
public List<AbstractSchedule> getAllSchedulesOfUser(String username) {
return scheduleRepository.findAllByUsername(username);
@ -225,6 +227,19 @@ public class TaskScheduleService {
return findScheduleByDate(allSchedules, date);
}
public List<AdvancedTaskSchedule> findSchedulesByDate(LocalDate date) {
Iterable<AdvancedTaskSchedule> allSchedules = advancedScheduleRepository.findAll();
List<AdvancedTaskSchedule> schedulesToday = new ArrayList<>();
for(AdvancedTaskSchedule advancedTaskSchedule : allSchedules) {
if(advancedTaskSchedule.getScheduleStart().toLocalDate().equals(date) && advancedTaskSchedule.getStartTime() == null &&
advancedTaskSchedule.getScheduleStart().isBefore(LocalDateTime.now())) {
schedulesToday.add(advancedTaskSchedule);
}
}
return schedulesToday;
}
private List<AbstractSchedule> findScheduleByDate(List<AbstractSchedule> schedules, LocalDate date) {
List<AbstractSchedule> filteredSchedules = new ArrayList<>();
for(AbstractSchedule schedule : schedules) {

View File

@ -3,6 +3,7 @@ package core.services;
import core.api.models.account.AccountDeleteRequest;
import core.api.models.account.EmailChangeRequest;
import core.api.models.account.PasswordChangeRequest;
import core.api.models.users.NtfyInformation;
import core.api.models.users.UserAddInfo;
import core.api.models.users.UserInfo;
import core.api.models.users.UserUpdateInfo;
@ -152,4 +153,13 @@ public class UserService {
userRepository.deleteByUsername(username);
return 0;
}
public void editNtfyInformation(User user, NtfyInformation ntfyInformation) {
user.setNtfy_host(ntfyInformation.getNtfy_host());
user.setNtfy_topic(ntfyInformation.getNtfy_topic());
user.setNtfy_username(ntfyInformation.getNtfy_user());
user.setNtfy_token(ntfyInformation.getNtfy_token());
userRepository.save(user);
}
}

View File

@ -0,0 +1,29 @@
package core.services.ntfy;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.io.IOException;
import java.net.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class NtfyTask implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
HttpClient httpClient = HttpClient.newHttpClient();
try {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("http://localhost:4280/Test"))
.POST(HttpRequest.BodyPublishers.ofString(jobExecutionContext.getMergedJobDataMap().getString("data")))
.build();
httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,39 @@
package core.services.ntfy;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class NtfyTaskStartNotification implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
HttpClient httpClient = HttpClient.newHttpClient();
String msg = "Task " + jobExecutionContext.getMergedJobDataMap().getString("task") + " should have been started by now!";
try {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(jobExecutionContext.getMergedJobDataMap().getString("ntfy_host") + "/Test"))
.POST(HttpRequest.BodyPublishers.ofString(msg))
.header("Tags", "warning")
.header("Title", "Task Starting Reminder")
.header("Actions", "view, Open TimeScheduler, "+jobExecutionContext.getMergedJobDataMap().getString("frontend_domain")+", clear=true")
.build();
httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,90 @@
package core.services.ntfy;
import core.entities.User;
import core.entities.timemanager.AbstractSchedule;
import core.entities.timemanager.AdvancedTaskSchedule;
import core.repositories.timemanager.ScheduleRepository;
import core.repositories.timemanager.TaskRepository;
import core.services.TaskScheduleService;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
@Service
public class TaskSchedulingService {
@Value("${frontend.domain}")
private String frontend_domain;
@Autowired private TaskScheduleService taskScheduleService;
private Scheduler scheduler;
public void scheduleStartingTask(AdvancedTaskSchedule advancedTaskSchedule) throws SchedulerException {
scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
JobDetail job = JobBuilder.newJob(NtfyTaskStartNotification.class)
.withIdentity(String.valueOf(advancedTaskSchedule.getScheduleID()))
.build();
job.getJobDataMap().put("task", advancedTaskSchedule.getTask().getTaskName());
job.getJobDataMap().put("ntfy_host", advancedTaskSchedule.getTask().getTaskgroup().getUser().getNtfy_host());
job.getJobDataMap().put("ntfy_topic", advancedTaskSchedule.getTask().getTaskgroup().getUser().getNtfy_topic());
job.getJobDataMap().put("frontend_domain", frontend_domain);
Trigger trigger = TriggerBuilder.newTrigger().startAt(calculateDelayInMillis(advancedTaskSchedule.getScheduleStart())).build();
scheduler.scheduleJob(job, trigger);
}
public void sendRunningTaskNotification(AbstractSchedule abstractSchedule) {
HttpClient httpClient = HttpClient.newHttpClient();
User user = abstractSchedule.getTask().getTaskgroup().getUser();
try {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(user.getNtfy_host()+ "/" + user.getNtfy_topic()))
.POST(HttpRequest.BodyPublishers.ofString("Running Task " + abstractSchedule.getTask().getTaskName()))
.header("Tags", "heavy_check_mark")
.header("Title", "Task Running")
.header("Actions", "view, Open TimeScheduler, "+frontend_domain+", clear=true")
.build();
httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
private static Date calculateDelayInMillis(LocalDateTime executionTime) {
return Date.from(executionTime.atZone(ZoneId.systemDefault()).toInstant());
}
@Scheduled(cron = "0 0 0 * * *")
public void scheduleStartReminders() throws SchedulerException {
List<AdvancedTaskSchedule> advancedTaskSchedules = taskScheduleService.findSchedulesByDate(LocalDate.now());
for(AdvancedTaskSchedule advancedTaskSchedule : advancedTaskSchedules) {
if(advancedTaskSchedule.getTask().getTaskgroup().getUser().getNtfy_host() != null) {
scheduleStartingTask(advancedTaskSchedule);
}
}
}
public void stopStartReminderNotification(AdvancedTaskSchedule schedule) throws SchedulerException {
scheduler.deleteJob(JobKey.jobKey(String.valueOf(schedule.getScheduleID())));
}
}

View File

@ -27,6 +27,7 @@ spring.jpa.open-in-view=false
# Spring Data Rest Setup
spring.data.rest.base-path=/api
@ -42,4 +43,8 @@ spring.servlet.multipart.max-request-size=4196KB
demo.webapp.jwtSecret=demoWebappSecretKey
demo.webapp.jwtExpirationMS=86400000
spring.jackson.time-zone=UTC
server.servlet.session.cookie.samesite=None
server.servlet.session.cookie.samesite=None
ntfy.host=http://localhost:4280
ntfy.topic=Test
frontend.domain=http://localhost:4200

View File

@ -1,13 +0,0 @@
package core;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
/*@SpringBootTest*/
class DemoApplicationTests {
/*@Test
void contextLoads() {
}*/
}

View File

@ -1,57 +0,0 @@
package core.schedules;
import core.entities.User;
import core.entities.timemanager.AbstractSchedule;
import core.repositories.timemanager.ScheduleRepository;
import core.services.ServiceResult;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlGroup;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ExtendWith(SpringExtension.class)
@DataJpaTest
public class ScheduleRepositoryTest {
@Autowired private ScheduleRepository scheduleRepository;
@Autowired private TestEntityManager entityManager;
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void getAllSchedulesOfUser() {
User referenceUser_1 = entityManager.find(User.class, 1L);
User referenceUser_2 = entityManager.find(User.class, 2L);
assertEquals(0, scheduleRepository.findAllByUsername(referenceUser_2.getUsername()).size());
assertEquals(5, scheduleRepository.findAllByUsername(referenceUser_1.getUsername()).size());
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void getActiveScheduleOfUser() {
User referenceUser_1 = entityManager.find(User.class, 1L);
User referenceUser_2 = entityManager.find(User.class, 2L);
Optional<AbstractSchedule> result_1 = scheduleRepository.getActiveScheduleOfUser(referenceUser_2.getUsername());
assertTrue(result_1.isEmpty());
Optional<AbstractSchedule> result_2 = scheduleRepository.getActiveScheduleOfUser(referenceUser_1.getUsername());
assertTrue(result_2.isPresent());
}
}

View File

@ -1,321 +0,0 @@
package core.schedules;
import core.api.models.timemanager.taskSchedule.scheduleInfos.AdvancedScheduleFieldInfo;
import core.api.models.timemanager.taskSchedule.scheduleInfos.AdvancedScheduleInfo;
import core.api.models.timemanager.taskSchedule.scheduleInfos.BasicScheduleFieldInfo;
import core.api.models.timemanager.taskSchedule.ForgottenScheduleInfo;
import core.entities.timemanager.AbstractSchedule;
import core.entities.timemanager.AdvancedTaskSchedule;
import core.entities.timemanager.BasicTaskSchedule;
import core.entities.timemanager.Task;
import core.services.ServiceExitCode;
import core.services.ServiceResult;
import core.services.TaskScheduleService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlGroup;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
@Transactional
public class ScheduleServiceTest {
@Autowired
private EntityManager entityManager;
@Autowired private TaskScheduleService taskScheduleService;
private static final String username = "Testuser1";
private static final String username2 = "Testuser2";
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void getAllSchedulesOfUser() {
assertEquals(0, taskScheduleService.getAllSchedulesOfUser(username2).size());
List<AbstractSchedule> result_1 = taskScheduleService.getAllSchedulesOfUser(username);
assertEquals(5, result_1.size());
assertTrue(result_1.contains(entityManager.find(AbstractSchedule.class, 1L)));
assertTrue(result_1.contains(entityManager.find(AbstractSchedule.class, 2L)));
assertTrue(result_1.contains(entityManager.find(AbstractSchedule.class, 3L)));
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void scheduleBasic() {
//Situation 1: Schedule finished Task
ServiceResult<AbstractSchedule> result_1 = taskScheduleService.scheduleBasic(entityManager.find(Task.class, 2L), new BasicScheduleFieldInfo(LocalDate.now()));
assertEquals(ServiceExitCode.INVALID_OPERATION, result_1.getExitCode());
//Situation 2: Schedule before today
ServiceResult<AbstractSchedule> result_2 = taskScheduleService.scheduleBasic(entityManager.find(Task.class, 1L), new BasicScheduleFieldInfo(LocalDate.of(2010, 3, 14)));
assertEquals(ServiceExitCode.INVALID_OPERATION, result_2.getExitCode());
//Situation 3: Valid schedule
ServiceResult<AbstractSchedule> result_3 = taskScheduleService.scheduleBasic(entityManager.find(Task.class, 1L), new BasicScheduleFieldInfo(LocalDate.now()));
assertEquals(ServiceExitCode.OK, result_3.getExitCode());
assertThat(entityManager.find(BasicTaskSchedule.class, result_3.getResult().getScheduleID())).isNotNull();
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void editBasicSchedule() {
//Situation 1: Reschedule finished task
ServiceResult<AbstractSchedule> result_1 = taskScheduleService.editBasicSchedule(entityManager.find(BasicTaskSchedule.class, 2L), new BasicScheduleFieldInfo(LocalDate.now()));
assertEquals(ServiceExitCode.INVALID_OPERATION, result_1.getExitCode());
//Situation 2: Reschedule unfinished task with invalid reschedule date
ServiceResult<AbstractSchedule> result_2 = taskScheduleService.editBasicSchedule(entityManager.find(BasicTaskSchedule.class, 1L), new BasicScheduleFieldInfo(LocalDate.of(2011, 3, 4)));
assertEquals(ServiceExitCode.INVALID_OPERATION, result_2.getExitCode());
//Situation 3: Reschedule unfinished task with valid reschedule date
LocalDate oldDate = entityManager.find(BasicTaskSchedule.class, 1L).getScheduleDate();
ServiceResult<AbstractSchedule> result_3 = taskScheduleService.editBasicSchedule(entityManager.find(BasicTaskSchedule.class, 1L), new BasicScheduleFieldInfo(LocalDate.now()));
assertEquals(ServiceExitCode.OK, result_3.getExitCode());
assertNotEquals(((BasicTaskSchedule) result_3.getResult()).getScheduleDate(), oldDate);
//Situation 4: Reschedule already running schedule
ServiceResult<AbstractSchedule> result_4 = taskScheduleService.editBasicSchedule(entityManager.find(BasicTaskSchedule.class, 4L),
new BasicScheduleFieldInfo(LocalDate.now()));
assertEquals(ServiceExitCode.INVALID_OPERATION, result_4.getExitCode());
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void deleteSchedule() {
for(long i=1; i<=2; i++) {
taskScheduleService.deleteSchedule(entityManager.find(BasicTaskSchedule.class, i));
assertThat(entityManager.find(BasicTaskSchedule.class, i)).isNull();
}
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void getFilteredScheduledOfUser() {
//Invalid user
assertEquals(0, taskScheduleService.getFilteredScheduledOfUser(LocalDate.now(), false, "Quatsch").size());
assertEquals(0, taskScheduleService.getFilteredScheduledOfUser(LocalDate.now(), true, "Quatsch").size());
//User with no tasks/schedules
assertEquals(0, taskScheduleService.getFilteredScheduledOfUser(LocalDate.now(), false, username2).size());
assertEquals(0, taskScheduleService.getFilteredScheduledOfUser(LocalDate.now(), true, username2).size());
//user with tasks and schedules
List<AbstractSchedule> result_1 = taskScheduleService.getFilteredScheduledOfUser(LocalDate.of(2024,11,11), false, username);
assertEquals(3, result_1.size());
assertTrue(result_1.contains(entityManager.find(BasicTaskSchedule.class, 3L)));
assertTrue(result_1.contains(entityManager.find(BasicTaskSchedule.class, 4L)));
assertTrue(result_1.contains(entityManager.find(BasicTaskSchedule.class, 5L)));
List<AbstractSchedule> result_2 = taskScheduleService.getFilteredScheduledOfUser(LocalDate.of(2024,11,11), true, username);
assertEquals(1, result_2.size());
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void scheduleNow() {
//Situation 1: Task has already an active schedule
ServiceResult<AbstractSchedule> result_1 = taskScheduleService.scheduleNow(entityManager.find(Task.class, 3L));
assertEquals(ServiceExitCode.ENTITY_ALREADY_EXIST, result_1.getExitCode());
//Situation 2: Task is already finished
ServiceResult<AbstractSchedule> result_2 = taskScheduleService.scheduleNow(entityManager.find(Task.class, 2L));
assertEquals(ServiceExitCode.INVALID_OPERATION, result_2.getExitCode());
//Situation 3: Task can be scheduled and started
ServiceResult<AbstractSchedule> result_3 = taskScheduleService.scheduleNow(entityManager.find(Task.class, 5L));
assertEquals(ServiceExitCode.OK, result_3.getExitCode());
assertThat(entityManager.find(BasicTaskSchedule.class, result_3.getResult().getScheduleID())).isNotNull();
//Situation 4: Running Advanced Schedule
ServiceResult<AbstractSchedule> result_4 = taskScheduleService.scheduleNow(entityManager.find(Task.class, 17L));
assertEquals(ServiceExitCode.ENTITY_ALREADY_EXIST, result_4.getExitCode());
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void activateSchedule() {
//Activate already running schedule
ServiceResult<AbstractSchedule> result_1 = taskScheduleService.activateSchedule(entityManager.find(BasicTaskSchedule.class, 4L));
assertNotNull(result_1.getResult().getStartTime());
assertNull(result_1.getResult().getStopTime());
entityManager.remove(entityManager.find(BasicTaskSchedule.class, 4L));
assertThat(entityManager.find(BasicTaskSchedule.class, 4L)).isNull();
//Activate not running schedule
ServiceResult<AbstractSchedule> result_2 = taskScheduleService.activateSchedule(entityManager.find(BasicTaskSchedule.class, 5L));
assertNotNull(result_2.getResult().getStartTime());
assertNull(result_2.getResult().getStopTime());
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void stopSchedule() {
//Stop schedule that is not running 4
ServiceResult<AbstractSchedule> result_1 = taskScheduleService.stopSchedule(entityManager.find(BasicTaskSchedule.class, 5L), false);
assertNull(result_1.getResult().getStartTime());
assertNull(result_1.getResult().getStopTime());
//Stop schedule (without finishing)
ServiceResult<AbstractSchedule> result_2 = taskScheduleService.stopSchedule(entityManager.find(BasicTaskSchedule.class, 4L), false);
assertNotNull(result_2.getResult().getStartTime());
assertNotNull(result_2.getResult().getStopTime());
//Stop schedule with finishing
ServiceResult<AbstractSchedule> result_3 = taskScheduleService.stopSchedule(entityManager.find(BasicTaskSchedule.class, 7L), true);
assertNotNull(result_3.getResult().getStartTime());
assertNotNull(result_3.getResult().getStopTime());
assertTrue(result_3.getResult().getTask().isFinished());
assertFalse(result_3.getResult().isStartable());
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void registerForgottenSchedule() {
//Register task schedule for task that is already finished
LocalDateTime startTime = LocalDateTime.now().minusMinutes(10L);
LocalDateTime finishTime = LocalDateTime.now();
ServiceResult<AbstractSchedule> result_1 = taskScheduleService.registerForgottenSchedule(entityManager.find(Task.class, 2L), new ForgottenScheduleInfo(startTime, finishTime));
assertEquals(ServiceExitCode.INVALID_OPERATION, result_1.getExitCode());
ServiceResult<AbstractSchedule> result_2 = taskScheduleService.registerForgottenSchedule(entityManager.find(Task.class, 5L), new ForgottenScheduleInfo(startTime, finishTime));
assertEquals(ServiceExitCode.OK, result_2.getExitCode());
assertEquals(startTime, result_2.getResult().getStartTime());
assertEquals(finishTime, result_2.getResult().getStopTime());
assertThat(entityManager.find(BasicTaskSchedule.class, result_2.getResult().getScheduleID())).isNotNull();
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void getAllMissedSchedulesOfUser() {
assertEquals(1, taskScheduleService.getAllMissedSchedulesOfUser(username).size());
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void scheduleAdvanced() {
//Situation 1: Schedule finished Task
ServiceResult<AbstractSchedule> result_1 = taskScheduleService.scheduleAdvanced(entityManager.find(Task.class, 18L),
new AdvancedScheduleFieldInfo(LocalDateTime.now().plusHours(1L), LocalDateTime.now().plusHours(2L)));
assertEquals(ServiceExitCode.INVALID_OPERATION, result_1.getExitCode());
//Situation 2: Schedule Start is before today
ServiceResult<AbstractSchedule> result_2 = taskScheduleService.scheduleAdvanced(entityManager.find(Task.class, 17L),
new AdvancedScheduleFieldInfo(LocalDateTime.now().minusDays(1L), LocalDateTime.now().plusHours(2L)));
assertEquals(ServiceExitCode.INVALID_OPERATION, result_2.getExitCode());
//Situation 3: Schedule End is before today
ServiceResult<AbstractSchedule> result_3 = taskScheduleService.scheduleAdvanced(entityManager.find(Task.class, 17L),
new AdvancedScheduleFieldInfo(LocalDateTime.now().minusDays(2L), LocalDateTime.now().minusDays(1L)));
assertEquals(ServiceExitCode.INVALID_OPERATION, result_3.getExitCode());
//Situation 4: Start after stop
ServiceResult<AbstractSchedule> result_4 = taskScheduleService.scheduleAdvanced(entityManager.find(Task.class, 17L),
new AdvancedScheduleFieldInfo(LocalDateTime.now().plusHours(2L), LocalDateTime.now().plusHours(1L)));
assertEquals(ServiceExitCode.INVALID_PARAMETER, result_4.getExitCode());
//Situation 5: Valid schedule
ServiceResult<AbstractSchedule> result_5 = taskScheduleService.scheduleAdvanced(entityManager.find(Task.class, 17L),
new AdvancedScheduleFieldInfo(LocalDateTime.now(), LocalDateTime.now().plusHours(1L)));
assertEquals(ServiceExitCode.OK, result_5.getExitCode());
assertThat(entityManager.find(AdvancedTaskSchedule.class, result_5.getResult().getScheduleID())).isNotNull();
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void editscheduleAdvanced() {
//Situation 1: Schedule finished Task
ServiceResult<AbstractSchedule> result_1 = taskScheduleService.editAdvancedSchedule(entityManager.find(AdvancedTaskSchedule.class, 12L),
new AdvancedScheduleFieldInfo(LocalDateTime.now().plusHours(1L), LocalDateTime.now().plusHours(2L)));
assertEquals(ServiceExitCode.INVALID_OPERATION, result_1.getExitCode());
//Situation 2: Schedule Start is before today
ServiceResult<AbstractSchedule> result_2 = taskScheduleService.editAdvancedSchedule(entityManager.find(AdvancedTaskSchedule.class, 11L),
new AdvancedScheduleFieldInfo(LocalDateTime.now().minusDays(1L), LocalDateTime.now().plusHours(2L)));
assertEquals(ServiceExitCode.INVALID_OPERATION, result_2.getExitCode());
//Situation 3: Schedule End is before today
ServiceResult<AbstractSchedule> result_3 = taskScheduleService.editAdvancedSchedule(entityManager.find(AdvancedTaskSchedule.class, 11L),
new AdvancedScheduleFieldInfo(LocalDateTime.now().minusDays(2L), LocalDateTime.now().minusDays(1L)));
assertEquals(ServiceExitCode.INVALID_OPERATION, result_3.getExitCode());
//Situation 4: Start after stop
ServiceResult<AbstractSchedule> result_4 = taskScheduleService.editAdvancedSchedule(entityManager.find(AdvancedTaskSchedule.class, 11L),
new AdvancedScheduleFieldInfo(LocalDateTime.now().plusHours(2L), LocalDateTime.now().plusHours(1L)));
assertEquals(ServiceExitCode.INVALID_PARAMETER, result_4.getExitCode());
//Situation 5: Valid schedule
ServiceResult<AbstractSchedule> result_5 = taskScheduleService.editAdvancedSchedule(entityManager.find(AdvancedTaskSchedule.class, 11L),
new AdvancedScheduleFieldInfo(LocalDateTime.now(), LocalDateTime.now().plusHours(1L)));
assertEquals(ServiceExitCode.OK, result_5.getExitCode());
assertThat(entityManager.find(AdvancedTaskSchedule.class, result_5.getResult().getScheduleID())).isNotNull();
//Situation 6: reschedule already running schedule
ServiceResult<AbstractSchedule> result_6 = taskScheduleService.editAdvancedSchedule(entityManager.find(AdvancedTaskSchedule.class, 9L),
new AdvancedScheduleFieldInfo(LocalDateTime.now(), LocalDateTime.now().plusHours(1L)));
assertEquals(ServiceExitCode.INVALID_OPERATION, result_6.getExitCode());
}
}

View File

@ -1,93 +0,0 @@
package core.taskgroups;
import core.entities.User;
import core.entities.timemanager.Task;
import core.entities.timemanager.Taskgroup;
import core.repositories.timemanager.TaskgroupRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.List;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(SpringExtension.class)
@DataJpaTest
public class TaskgroupRepsitoryTest {
@Autowired private TaskgroupRepository taskgroupRepository;
@Autowired private TestEntityManager testEntityManager;
@Test
@Sql("classpath:taskgroupRepositoryTestEntries.sql")
void findAllByUser() {
User testUser1 = testEntityManager.find(User.class, 1L);
User testUser2 = testEntityManager.find(User.class, 2L);
List<Taskgroup> result_user2 = taskgroupRepository.findAllByUser(testUser2.getUsername());
assertEquals(0, result_user2.size());
List<Taskgroup> result_user1 = taskgroupRepository.findAllByUser(testUser1.getUsername());
assertEquals(8, result_user1.size());
}
@Test
@Sql("classpath:taskgroupRepositoryTestEntries.sql")
void findAllTopTaskgroupsByUser() {
User testUser1 = testEntityManager.find(User.class, 1L);
User testUser2 = testEntityManager.find(User.class, 2L);
//Situation 1: Empty user, no taskgroups
assertEquals(0, taskgroupRepository.findAllTopTaskgroupsByUser(testUser2.getUsername()).size());
//Situation 2: Only top taskgroups are returned
List<Taskgroup> topgroups_user1 = taskgroupRepository.findAllTopTaskgroupsByUser(testUser1.getUsername());
assertEquals(3, topgroups_user1.size());
assertTrue(topgroups_user1.contains(testEntityManager.find(Taskgroup.class, 1L)));
assertTrue(topgroups_user1.contains(testEntityManager.find(Taskgroup.class, 2L)));
assertTrue(topgroups_user1.contains(testEntityManager.find(Taskgroup.class, 5L)));
//Situation 3: User with username does not exist
assertEquals(0, taskgroupRepository.findAllTopTaskgroupsByUser("Rotzbakke").size());
}
@Test
@Sql("classpath:taskgroupRepositoryTestEntries.sql")
void deleteAllByUser() {
User testUser1 = testEntityManager.find(User.class, 1L);
User testUser2 = testEntityManager.find(User.class, 2L);
taskgroupRepository.deleteAllByUser(testUser2);
for(long i=1; i<=8; i++) {
assertThat(testEntityManager.find(Taskgroup.class, i)).isNotNull();
}
taskgroupRepository.deleteAllByUser(testUser1);
for(long i=1; i<=8; i++) {
assertThat(testEntityManager.find(Taskgroup.class, i)).isNull();
}
}
@Test
@Sql("classpath:taskgroupRepositoryTestEntries.sql")
void delete() {
Taskgroup no_children = testEntityManager.find(Taskgroup.class, 1L);
Taskgroup taskgroup_with_children = testEntityManager.find(Taskgroup.class, 5L);
taskgroupRepository.delete(no_children);
assertThat(testEntityManager.find(Taskgroup.class, 1L)).isNull();
for(long i=2; i<=8; i++) {
assertThat(testEntityManager.find(Taskgroup.class, i)).isNotNull();
}
taskgroupRepository.delete(taskgroup_with_children);
for(long i=5; i<=8; i++) {
assertThat(testEntityManager.find(Taskgroup.class, i)).isNull();
}
}
}

View File

@ -1,202 +0,0 @@
package core.taskgroups;
import core.api.models.timemanager.taskgroup.TaskgroupFieldInfo;
import core.entities.User;
import core.entities.timemanager.Task;
import core.entities.timemanager.Taskgroup;
import core.repositories.UserRepository;
import core.repositories.timemanager.TaskgroupRepository;
import core.services.PermissionResult;
import core.services.ServiceExitCode;
import core.services.ServiceResult;
import core.services.TaskgroupService;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlGroup;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit4.SpringRunner;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
@Transactional
public class TaskgroupServiceTest {
@Autowired
private TaskgroupRepository taskgroupRepository;
@Autowired
private TaskgroupService taskgroupService;
@Autowired
private EntityManager entityManager;
private static final String username = "Testuser1";
private static final String username2 = "Testuser2";
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql")
})
void getTaskgroupByIDAndUsername() {
//Situation 1: correct taskgroup and username
assertFalse(taskgroupRepository.findById(1L).isEmpty());
PermissionResult<Taskgroup> permissionResult = taskgroupService.getTaskgroupByIDAndUsername(1L, username);
assertThat(permissionResult).isNotNull();
assertTrue(permissionResult.isHasPermissions());
assertEquals(ServiceExitCode.OK, permissionResult.getExitCode());
assertEquals(taskgroupRepository.findById(1L).get(), permissionResult.getResult());
//Situation 2: invalid taskgroup
PermissionResult<Taskgroup> invalid_group = taskgroupService.getTaskgroupByIDAndUsername(-1L, username);
assertThat(invalid_group).isNotNull();
assertEquals(ServiceExitCode.MISSING_ENTITY, invalid_group.getExitCode());
assertFalse(invalid_group.isHasPermissions());
//Situation 3: invalid user
PermissionResult<Taskgroup> invalid_user = taskgroupService.getTaskgroupByIDAndUsername(1L, "Rotzbakke");
assertThat(invalid_user).isNotNull();
assertEquals(ServiceExitCode.OK, invalid_user.getExitCode());
assertFalse(invalid_user.isHasPermissions());
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql")
})
void addTaskgroup() {
TaskgroupFieldInfo taskgroupFieldInfo = new TaskgroupFieldInfo("Taskgroup", -1);
//Situation 1: No such user
assertThrows(NoSuchElementException.class, () -> taskgroupService.addTaskgroup(taskgroupFieldInfo, "Rotzbakke"));
//Situation 2: Taskgroup already exists
Taskgroup taskgroup = taskgroupRepository.findById(1L).get();
ServiceResult<Taskgroup> creationResult = taskgroupService.addTaskgroup(new TaskgroupFieldInfo(taskgroup.getTaskgroupName(), -1), username);
assertEquals(ServiceExitCode.ENTITY_ALREADY_EXIST, creationResult.getExitCode());
assertThat(creationResult.getResult()).isNull();
//Situation 3: Taskgroup does not exist, no parent
ServiceResult<Taskgroup> creationResult_3 = taskgroupService.addTaskgroup(taskgroupFieldInfo, username);
assertEquals(ServiceExitCode.OK, creationResult_3.getExitCode());
assertTrue(taskgroupRepository.existsById(creationResult_3.getResult().getTaskgroupID()));
//Situation 4: Taskgroup does not exist, with parent; invalid parent (no existent)
ServiceResult<Taskgroup> creationResult_4 = taskgroupService.addTaskgroup(new TaskgroupFieldInfo("Situation 4", 100L), username);
assertEquals(ServiceExitCode.MISSING_ENTITY, creationResult_4.getExitCode());
//Situation 5: Taskgroup does not exist, parent exist
ServiceResult<Taskgroup> creationResult_5 = taskgroupService.addTaskgroup(new TaskgroupFieldInfo("Situation 5", 2L), username);
assertEquals(ServiceExitCode.OK, creationResult_5.getExitCode());
assertTrue(taskgroupRepository.existsById(creationResult_5.getResult().getTaskgroupID()));
//Situation 6: taskgroup exist on another user
ServiceResult<Taskgroup> creationResult_6 = taskgroupService.addTaskgroup(new TaskgroupFieldInfo(taskgroup.getTaskgroupName(), -1), username2);
assertEquals(ServiceExitCode.OK, creationResult_6.getExitCode());
assertTrue(taskgroupRepository.existsById(creationResult_6.getResult().getTaskgroupID()));
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql")
})
void editTaskgroup() {
Taskgroup taskgroup = taskgroupRepository.findById(1L).get();
//Situation 1: Nothing changed
ServiceExitCode exitCode_1 = taskgroupService.editTaskgroup(taskgroup, new TaskgroupFieldInfo(taskgroup.getTaskgroupName(),-1));
assertEquals(ServiceExitCode.OK, exitCode_1);
//Situation 2: Name is already taken
ServiceExitCode exitCode_2 = taskgroupService.editTaskgroup(taskgroup, new TaskgroupFieldInfo("Taskgroup 1", -1));
assertEquals(ServiceExitCode.ENTITY_ALREADY_EXIST, exitCode_2);
//Situation 3: All fine
ServiceExitCode exitCode_3 = taskgroupService.editTaskgroup(taskgroup, new TaskgroupFieldInfo("Situation 3", -1));
assertEquals(ServiceExitCode.OK, exitCode_3);
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql")
})
void deleteTaskgroup() {
taskgroupService.deleteTaskgroup(entityManager.find(Taskgroup.class, 1L));
assertThat(entityManager.find(Taskgroup.class, 1L)).isNull();
taskgroupService.deleteTaskgroup(entityManager.find(Taskgroup.class, 2L));
assertThat(entityManager.find(Taskgroup.class, 2L)).isNull();
assertThat(entityManager.find(Taskgroup.class, 3L)).isNull();
for(long i=1; i<=14; i++) {
assertThat(entityManager.find(Task.class, i)).isNull();
}
taskgroupService.deleteTaskgroup(entityManager.find(Taskgroup.class, 5L));
assertThat(entityManager.find(Taskgroup.class, 5L)).isNull();
assertThat(entityManager.find(Taskgroup.class, 6L)).isNull();
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql")
})
void getTopTaskgroupsByUser() {
//Situation 1: User without taskgroups
List<Taskgroup> taskgroups_user2 = taskgroupService.getTopTaskgroupsByUser(username2);
assertEquals(0, taskgroups_user2.size());
//Situation 2: user with taskgroups
List<Taskgroup> taskgroups_user1 = taskgroupService.getTopTaskgroupsByUser(username);
assertEquals(3, taskgroups_user1.size());
assertTrue(taskgroups_user1.contains(entityManager.find(Taskgroup.class, 1L)));
assertTrue(taskgroups_user1.contains(entityManager.find(Taskgroup.class, 2L)));
assertTrue(taskgroups_user1.contains(entityManager.find(Taskgroup.class, 5L)));
//Situation 3: No existent username
assertEquals(0, taskgroupService.getTopTaskgroupsByUser("Rotzbakke").size());
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql")
})
void deleteTaskgroupByUser() {
User referenceUser1 = entityManager.find(User.class, 1L);
User referenceUser2 = entityManager.find(User.class, 2L);
taskgroupService.deleteTaskgroupByUser(referenceUser2);
for(long i=1; i<=8; i++) {
assertThat(entityManager.find(Taskgroup.class, i)).isNotNull();
}
taskgroupService.deleteTaskgroupByUser(referenceUser1);
for(long i=1; i<=8; i++) {
assertThat(entityManager.find(Taskgroup.class, i)).isNull();
}
for(long i=1; i<=14; i++) {
assertThat(entityManager.find(Task.class, i)).isNull();
}
}
}

View File

@ -1,160 +0,0 @@
package core.tasks;
import core.entities.User;
import core.entities.timemanager.Task;
import core.entities.timemanager.Taskgroup;
import core.repositories.timemanager.TaskRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlGroup;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.time.LocalDate;
import java.util.List;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ExtendWith(SpringExtension.class)
@DataJpaTest
public class TaskRepositoryTest {
@Autowired private TaskRepository taskRepository;
@Autowired private TestEntityManager testEntityManager;
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql")
})
void findAllByUser() {
User testUser1 = testEntityManager.find(User.class, 1L);
User testUser2 = testEntityManager.find(User.class, 2L);
//Situation 1: Non existent user
assertEquals(0, taskRepository.findAllByUser("Rotzbakke", true).size());
assertEquals(0, taskRepository.findAllByUser("Rotzbakke", false).size());
//Situation 2: User with no tasks at all
assertEquals(0, taskRepository.findAllByUser(testUser2.getUsername(), false).size());
assertEquals(0, taskRepository.findAllByUser(testUser2.getUsername(), true).size());
//Situation 3: User with finished and unfinished tasks
List<Task> tasks_user1_false = taskRepository.findAllByUser(testUser1.getUsername(), false);
List<Task> tasks_user1_true = taskRepository.findAllByUser(testUser1.getUsername(), true);
assertEquals(7, tasks_user1_true.size());
assertEquals(8, tasks_user1_false.size());
assertTrue(tasks_user1_false.contains(testEntityManager.find(Task.class, 1L)));
assertTrue(tasks_user1_false.contains(testEntityManager.find(Task.class, 3L)));
assertTrue(tasks_user1_false.contains(testEntityManager.find(Task.class, 5L)));
assertTrue(tasks_user1_false.contains(testEntityManager.find(Task.class, 7L)));
assertTrue(tasks_user1_false.contains(testEntityManager.find(Task.class, 9L)));
assertTrue(tasks_user1_false.contains(testEntityManager.find(Task.class, 11L)));
assertTrue(tasks_user1_false.contains(testEntityManager.find(Task.class, 12L)));
assertTrue(tasks_user1_true.contains(testEntityManager.find(Task.class, 2L)));
assertTrue(tasks_user1_true.contains(testEntityManager.find(Task.class, 4L)));
assertTrue(tasks_user1_true.contains(testEntityManager.find(Task.class, 6L)));
assertTrue(tasks_user1_true.contains(testEntityManager.find(Task.class, 8L)));
assertTrue(tasks_user1_true.contains(testEntityManager.find(Task.class, 10L)));
assertTrue(tasks_user1_true.contains(testEntityManager.find(Task.class, 13L)));
assertTrue(tasks_user1_true.contains(testEntityManager.find(Task.class, 14L)));
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql")
})
void deleteAllByTaskgroup() {
Taskgroup taskgroup_no_tasks = testEntityManager.find(Taskgroup.class, 1L);
taskRepository.deleteAllByTaskgroup(taskgroup_no_tasks);
for(long i=1; i<=14; i++) {
assertThat(testEntityManager.find(Task.class, i)).isNotNull();
}
Taskgroup taskgroup_with_task = testEntityManager.find(Taskgroup.class, 2L);
taskRepository.deleteAllByTaskgroup(taskgroup_with_task);
for(long i=1; i<=14; i++) {
Task task = testEntityManager.find(Task.class, i);
assertThat(task).isNull();
}
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql")
})
void deleteByTaskID() {
taskRepository.deleteByTaskID(-1);
for(long i=1; i<=14; i++) {
assertThat(testEntityManager.find(Task.class, i)).isNotNull();
}
taskRepository.deleteByTaskID(1);
assertThat(testEntityManager.find(Task.class, 1L)).isNull();
for(long i=2; i<=14; i++) {
assertThat(testEntityManager.find(Task.class, i)).isNotNull();
}
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql")
})
void findAllOverdue() {
User testuser1 = testEntityManager.find(User.class, 1L);
LocalDate referenceDate = LocalDate.of(2023, 10, 11);
List<Task> overdue = taskRepository.findAllOverdue(testuser1.getUsername(), referenceDate);
assertEquals(2, overdue.size());
assertTrue(overdue.contains(testEntityManager.find(Task.class, 3L)));
assertTrue(overdue.contains(testEntityManager.find(Task.class, 5L)));
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql")
})
void findAllUpcoming() {
User testuser1 = testEntityManager.find(User.class, 1L);
LocalDate referenceDate = LocalDate.of(2023, 10, 11);
List<Task> upcoming = taskRepository.findAllUpcoming(testuser1.getUsername(), referenceDate);
assertEquals(2, upcoming.size());
assertTrue(upcoming.contains(testEntityManager.find(Task.class, 7L)));
assertTrue(upcoming.contains(testEntityManager.find(Task.class, 9L)));
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql")
})
void findAllActive() {
User testuser1 = testEntityManager.find(User.class, 1L);
LocalDate referenceDate = LocalDate.of(2023, 10, 11);
List<Task> active = taskRepository.findAllActive(testuser1.getUsername(), referenceDate);
//1,3,5,11,12
assertEquals(6, active.size());
assertTrue(active.contains(testEntityManager.find(Task.class, 1L)));
assertTrue(active.contains(testEntityManager.find(Task.class, 3L)));
assertTrue(active.contains(testEntityManager.find(Task.class, 5L)));
assertTrue(active.contains(testEntityManager.find(Task.class, 11L)));
assertTrue(active.contains(testEntityManager.find(Task.class, 12L)));
}
}

View File

@ -1,287 +0,0 @@
package core.tasks;
import core.api.models.timemanager.tasks.TaskFieldInfo;
import core.api.models.timemanager.tasks.TaskScope;
import core.entities.timemanager.BasicTaskSchedule;
import core.entities.timemanager.Task;
import core.entities.timemanager.Taskgroup;
import core.repositories.timemanager.TaskRepository;
import core.repositories.timemanager.TaskgroupRepository;
import core.services.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlGroup;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.time.LocalDate;
import java.util.List;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
@Transactional
public class TaskServiceTest {
@Autowired
private TaskService taskService;
@Autowired
private EntityManager entityManager;
private static final String username = "Testuser1";
private static final String username2 = "Testuser2";
@Autowired
private TaskRepository taskRepository;
@Autowired
private TaskgroupRepository taskgroupRepository;
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void createTask() {
//Situation 1: Task with that name already exists in a taskgroup
TaskFieldInfo taskFieldInfo_1 = new TaskFieldInfo(entityManager.find(Task.class, 1L).getTaskName(), 0, null, null);
ServiceResult<Task> situation_1 = taskService.createTask(entityManager.find(Taskgroup.class, 2L), taskFieldInfo_1);
assertEquals(ServiceExitCode.ENTITY_ALREADY_EXIST, situation_1.getExitCode());
//Situation 2: Task with that name exists in another taskgroup
TaskFieldInfo taskFieldInfo_2 = new TaskFieldInfo(entityManager.find(Task.class, 1L).getTaskName(), 0, null, null);
ServiceResult<Task> result_2 = taskService.createTask(entityManager.find(Taskgroup.class, 1L), taskFieldInfo_2);
assertEquals(ServiceExitCode.OK, result_2.getExitCode());
assertThat(entityManager.find(Task.class, result_2.getResult().getTaskID())).isNotNull();
//Situation 3: Normal, everything fine
TaskFieldInfo taskFieldInfo_3 = new TaskFieldInfo("Situation 3", 0, null, null);
ServiceResult<Task> result_3 = taskService.createTask(entityManager.find(Taskgroup.class, 1L), taskFieldInfo_3);
assertEquals(ServiceExitCode.OK, result_3.getExitCode());
assertThat(entityManager.find(Task.class, result_3.getResult().getTaskID())).isNotNull();
//Robustness - Test for invalid dates
//Situation 4: Deadline, no start
TaskFieldInfo taskFieldInfo_4 = new TaskFieldInfo("Situation 4", 0, null, LocalDate.now());
ServiceResult<Task> result_4 = taskService.createTask(entityManager.find(Taskgroup.class, 1L), taskFieldInfo_4);
assertEquals(ServiceExitCode.OK, result_4.getExitCode());
assertThat(entityManager.find(Task.class, result_4.getResult().getTaskID())).isNotNull();
//Situation 5: Start but no deadline
TaskFieldInfo taskFieldInfo_5 = new TaskFieldInfo("Situation 5", 0, LocalDate.now(),null);
ServiceResult<Task> result_5 = taskService.createTask(entityManager.find(Taskgroup.class, 1L), taskFieldInfo_5);
assertEquals(ServiceExitCode.OK, result_5.getExitCode());
assertThat(entityManager.find(Task.class, result_5.getResult().getTaskID())).isNotNull();
//Situation 6: Deadline before start (invalid)
TaskFieldInfo taskFieldInfo_6 = new TaskFieldInfo("Situation 6", 0, LocalDate.now(),LocalDate.of(2010, 3, 20));
ServiceResult<Task> result_6= taskService.createTask(entityManager.find(Taskgroup.class, 1L), taskFieldInfo_6);
assertEquals(ServiceExitCode.INVALID_PARAMETER, result_6.getExitCode());
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void getTaskPermissions() {
//Situation 1: correct task and username
PermissionResult<Task> permissionResult_1 = taskService.getTaskPermissions(1L, username);
assertEquals(ServiceExitCode.OK, permissionResult_1.getExitCode());
assertTrue(permissionResult_1.isHasPermissions());
assertEquals(entityManager.find(Task.class, 1L), permissionResult_1.getResult());
//Situation 2: invalid taskgroup
PermissionResult<Task> permissionResult_2 = taskService.getTaskPermissions(200L, username);
assertEquals(ServiceExitCode.MISSING_ENTITY, permissionResult_2.getExitCode());
assertFalse(permissionResult_2.isHasPermissions());
assertThat(permissionResult_2.getResult()).isNull();
//Situation 3: invalid user
PermissionResult<Task> permissionResult_3 = taskService.getTaskPermissions(1L, username2);
assertEquals(ServiceExitCode.OK, permissionResult_3.getExitCode());
assertFalse(permissionResult_2.isHasPermissions());
assertEquals(entityManager.find(Task.class, 1L), permissionResult_3.getResult());
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void editTask() {
//Situation 1: Nothing is updated
Task task_1 = entityManager.find(Task.class, 1L);
TaskFieldInfo taskFieldInfo_1 = new TaskFieldInfo(entityManager.find(Task.class, 1L).getTaskName(), 0, null, null);
ServiceResult<Task> result_1 = taskService.editTask(entityManager.find(Task.class, 1L), taskFieldInfo_1);
assertEquals(ServiceExitCode.OK, result_1.getExitCode());
assertEquals(task_1, result_1.getResult());
//Situation 2: Name is updated to name that already exists within the taskgroup
Task task_2 = entityManager.find(Task.class, 1L);
TaskFieldInfo taskFieldInfo_2 = new TaskFieldInfo(entityManager.find(Task.class, 2L).getTaskName(), 0, null, null);
ServiceResult<Task> result_2 = taskService.editTask(entityManager.find(Task.class, 1L), taskFieldInfo_2);
task_2.setTaskName(entityManager.find(Task.class, 2L).getTaskName());
assertEquals(ServiceExitCode.ENTITY_ALREADY_EXIST, result_2.getExitCode());
assertNotEquals(task_2, result_2.getResult());
//Situation 3: Name is updated to nonexistend Name
Task task_3 = entityManager.find(Task.class, 1L);
TaskFieldInfo taskFieldInfo_3 = new TaskFieldInfo("Situation 3", 0, null, null);
ServiceResult<Task> result_3 = taskService.editTask(entityManager.find(Task.class, 1L), taskFieldInfo_3);
task_3.setTaskName("Situation 3");
assertEquals(ServiceExitCode.OK, result_3.getExitCode());
assertEquals(task_3, result_3.getResult());
//Situation 4: eta is updated
Task task_4 = entityManager.find(Task.class, 1L);
TaskFieldInfo taskFieldInfo_4 = new TaskFieldInfo(task_4.getTaskName(), 90, null, null);
ServiceResult<Task> result_4 = taskService.editTask(entityManager.find(Task.class, 1L), taskFieldInfo_4);
task_4.setEta(90);
assertEquals(ServiceExitCode.OK, result_4.getExitCode());
assertEquals(task_4, result_4.getResult());
//Situation 5: deadline and start is updated in a valid way
Task task_5 = entityManager.find(Task.class, 1L);
TaskFieldInfo taskFieldInfo_5 = new TaskFieldInfo(task_5.getTaskName(), task_5.getEta(), LocalDate.of(2023, 4, 9), LocalDate.of(2023,5,9));
ServiceResult<Task> result_5 = taskService.editTask(entityManager.find(Task.class, 1L), taskFieldInfo_5);
task_5.setStartDate(LocalDate.of(2023,4,9));
task_5.setDeadline(LocalDate.of(2023,5,9));
assertEquals(ServiceExitCode.OK, result_5.getExitCode());
assertEquals(task_5, result_5.getResult());
//Situation 6: Deadline and start are updated in a not valid way
Task task_6 = entityManager.find(Task.class, 1L);
TaskFieldInfo taskFieldInfo_6 = new TaskFieldInfo(task_5.getTaskName(), task_5.getEta(), LocalDate.of(2023, 5, 9), LocalDate.of(2023,4,9));
ServiceResult<Task> result_6 = taskService.editTask(entityManager.find(Task.class, 1L), taskFieldInfo_6);
task_6.setStartDate(LocalDate.of(2023,5,9));
task_6.setDeadline(LocalDate.of(2023,4,9));
assertEquals(ServiceExitCode.INVALID_PARAMETER, result_6.getExitCode());
assertNotEquals(task_6, result_6.getResult());
//Situation 10 Startdate = null;
Task task_10 = entityManager.find(Task.class, 5L);
TaskFieldInfo taskFieldInfo_10 = new TaskFieldInfo(task_10.getTaskName(), task_10.getEta(),null, task_10.getDeadline());
ServiceResult<Task> result_10 = taskService.editTask(entityManager.find(Task.class, 5L), taskFieldInfo_10);
task_10.setStartDate(null);
assertEquals(ServiceExitCode.OK, result_10.getExitCode());
assertEquals(task_10, result_10.getResult());
//Situation 11 Deadline = null
Task task_11 = entityManager.find(Task.class, 5L);
TaskFieldInfo taskFieldInfo_11 = new TaskFieldInfo(task_11.getTaskName(), task_11.getEta(),task_11.getStartDate(), null);
ServiceResult<Task> result_11 = taskService.editTask(entityManager.find(Task.class, 5L), taskFieldInfo_11);
task_11.setDeadline(null);
assertEquals(ServiceExitCode.OK, result_11.getExitCode());
assertEquals(task_11, result_11.getResult());
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void deleteTask() {
for(long i=1; i<=15; i++) {
taskService.deleteTask(entityManager.find(Task.class, i));
assertThat(entityManager.find(Task.class, i)).isNull();
}
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void clearTasks() {
//Situation 1: Delete from taskgroup with no tasks
taskService.clearTasks(entityManager.find(Taskgroup.class, 1L));
for(long i=1; i<=15; i++) {
assertThat(entityManager.find(Task.class, i)).isNotNull();
}
//Situation 2: Delete from taskgroup with tasks
taskService.clearTasks(entityManager.find(Taskgroup.class, 2L));
for(long i=1; i<=15; i++) {
assertThat(entityManager.find(Task.class, i)).isNull();
}
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void finishTask() {
taskService.finishTask(entityManager.find(Task.class, 1L));
assertTrue(entityManager.find(Task.class, 1L).isFinished());
taskService.finishTask(entityManager.find(Task.class, 2L));
assertTrue(entityManager.find(Task.class, 1L).isFinished());
assertThat(entityManager.find(BasicTaskSchedule.class, 2L)).isNull();
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void loadAllTasks() {
//Situation 1: No tasks existing
List<Task> result_1 = taskService.loadAllTasks(username2, TaskScope.UNFINISHED);
assertEquals(0, result_1.size());
//Situation 2: Tasks existing, Unfinished#
List<Task> result_2 = taskService.loadAllTasks(username, TaskScope.UNFINISHED);
assertEquals(8, result_2.size());
assertTrue(result_2.contains(entityManager.find(Task.class, 1L)));
assertTrue(result_2.contains(entityManager.find(Task.class, 3L)));
assertTrue(result_2.contains(entityManager.find(Task.class, 5L)));
assertTrue(result_2.contains(entityManager.find(Task.class, 7L)));
assertTrue(result_2.contains(entityManager.find(Task.class, 9L)));
assertTrue(result_2.contains(entityManager.find(Task.class, 11L)));
assertTrue(result_2.contains(entityManager.find(Task.class, 12L)));
//Situation 3: Finished tasks
List<Task> result_3 = taskService.loadAllTasks(username, TaskScope.FINISHED);
assertEquals(7, result_3.size());
assertTrue(result_3.contains(entityManager.find(Task.class, 2L)));
assertTrue(result_3.contains(entityManager.find(Task.class, 4L)));
assertTrue(result_3.contains(entityManager.find(Task.class, 6L)));
assertTrue(result_3.contains(entityManager.find(Task.class, 8L)));
assertTrue(result_3.contains(entityManager.find(Task.class, 10L)));
assertTrue(result_3.contains(entityManager.find(Task.class, 13L)));
assertTrue(result_3.contains(entityManager.find(Task.class, 14L)));
//overdue
List<Task> result_4 = taskService.loadAllTasks(username, TaskScope.OVERDUE);
assertEquals(2, result_4.size());
assertTrue(result_4.contains(entityManager.find(Task.class, 3L)));
assertTrue(result_4.contains(entityManager.find(Task.class, 5L)));
//upcoming
List<Task> result_5 = taskService.loadAllTasks(username, TaskScope.UPCOMING);
assertEquals(2, result_5.size());
assertTrue(result_5.contains(entityManager.find(Task.class, 7L)));
assertTrue(result_5.contains(entityManager.find(Task.class, 9L)));
//Active
List<Task> result_6 = taskService.loadAllTasks(username, TaskScope.ACTIVE);
assertEquals(6, result_6.size());
assertTrue(result_6.contains(entityManager.find(Task.class, 1L)));
assertTrue(result_6.contains(entityManager.find(Task.class, 3L)));
assertTrue(result_6.contains(entityManager.find(Task.class, 5L)));
assertTrue(result_6.contains(entityManager.find(Task.class, 11L)));
assertTrue(result_6.contains(entityManager.find(Task.class, 12L)));
}
}

View File

@ -1,87 +0,0 @@
package core.users;
import core.entities.User;
import core.repositories.UserRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.Optional;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.*;
@DataJpaTest
@ExtendWith(SpringExtension.class)
public class UserRepositoryTests {
@Autowired
private UserRepository userRepository;
@Autowired
private TestEntityManager testEntityManager;
@Test
@Sql("classpath:userRepisotoryTestEntries.sql")
void test_findByUsername() {
//Situation 1: User with name "FawKes100" is present
User referenceUser = testEntityManager.find(User.class, 1L);
Optional<User> findResult = userRepository.findByUsername(referenceUser.getUsername());
assertTrue(findResult.isPresent());
assertEquals(referenceUser,findResult.get());
//Situation 2: No user with the required name is present
findResult = userRepository.findByUsername("fawkes1001");
assertFalse(findResult.isPresent());
}
@Test
@Sql("classpath:userRepisotoryTestEntries.sql")
void test_existsByMail() {
User referenceUser = testEntityManager.find(User.class, 1L);
//Situation 1: invalid email format should not matter
assertFalse(userRepository.existsByEmail("FawKes100"));
//Situation 2: No user exists with such a mail
assertFalse(userRepository.existsByEmail("mail@fawkes100.de"));
//Situation 3: User with this mail exists
assertTrue(userRepository.existsByEmail(referenceUser.getEmail()));
}
@Test
@Sql("classpath:userRepisotoryTestEntries.sql")
void test_existsByUsername() {
User referenceUser = testEntityManager.find(User.class, 1L);
//Situation 1: No such user
assertFalse(userRepository.existsByUsername("FawKes100"));
//Situation 3: User with this name exists
assertTrue(userRepository.existsByUsername(referenceUser.getUsername()));
}
@Test
@Sql("classpath:userRepisotoryTestEntries.sql")
void test_deleteUserByName() {
User referenceUser1 = testEntityManager.find(User.class, 1L);
User referenceUser2 = testEntityManager.find(User.class, 2L);
userRepository.deleteByUsername(referenceUser2.getUsername());
assertThat(testEntityManager.find(User.class, referenceUser2.getId())).isNull();
assertThat(testEntityManager.find(User.class, referenceUser1.getId())).isNotNull();
}
@Test
@Sql("classpath:userRepisotoryTestEntries.sql")
void test_countUsers() {
assertEquals(2, userRepository.countUsers());
}
}

View File

@ -5,6 +5,7 @@ api/account.service.ts
api/api.ts
api/history.service.ts
api/login.service.ts
api/ntfy.service.ts
api/properties.service.ts
api/schedule.service.ts
api/task.service.ts
@ -32,6 +33,7 @@ model/loginRequest.ts
model/loginResponse.ts
model/manualScheduleStopInfo.ts
model/models.ts
model/ntfyInformation.ts
model/passwordChangeRequest.ts
model/propertiesInfo.ts
model/propertyInfo.ts

View File

@ -5,6 +5,7 @@ import { HttpClient } from '@angular/common/http';
import { AccountService } from './api/account.service';
import { HistoryService } from './api/history.service';
import { LoginService } from './api/login.service';
import { NtfyService } from './api/ntfy.service';
import { PropertiesService } from './api/properties.service';
import { ScheduleService } from './api/schedule.service';
import { TaskService } from './api/task.service';

View File

@ -4,6 +4,8 @@ export * from './history.service';
import { HistoryService } from './history.service';
export * from './login.service';
import { LoginService } from './login.service';
export * from './ntfy.service';
import { NtfyService } from './ntfy.service';
export * from './properties.service';
import { PropertiesService } from './properties.service';
export * from './schedule.service';
@ -14,4 +16,4 @@ export * from './taskgroup.service';
import { TaskgroupService } from './taskgroup.service';
export * from './users.service';
import { UsersService } from './users.service';
export const APIS = [AccountService, HistoryService, LoginService, PropertiesService, ScheduleService, TaskService, TaskgroupService, UsersService];
export const APIS = [AccountService, HistoryService, LoginService, NtfyService, PropertiesService, ScheduleService, TaskService, TaskgroupService, UsersService];

View File

@ -0,0 +1,210 @@
/**
* API Title
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
/* tslint:disable:no-unused-variable member-ordering */
import { Inject, Injectable, Optional } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams,
HttpResponse, HttpEvent, HttpParameterCodec, HttpContext
} from '@angular/common/http';
import { CustomHttpParameterCodec } from '../encoder';
import { Observable } from 'rxjs';
import { NtfyInformation } from '../model/models';
import { SimpleStatusResponse } from '../model/models';
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
import { Configuration } from '../configuration';
@Injectable({
providedIn: 'root'
})
export class NtfyService {
protected basePath = 'http://127.0.0.1:8080/api';
public defaultHeaders = new HttpHeaders();
public configuration = new Configuration();
public encoder: HttpParameterCodec;
constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
if (configuration) {
this.configuration = configuration;
}
if (typeof this.configuration.basePath !== 'string') {
if (typeof basePath !== 'string') {
basePath = this.basePath;
}
this.configuration.basePath = basePath;
}
this.encoder = this.configuration.encoder || new CustomHttpParameterCodec();
}
private addToHttpParams(httpParams: HttpParams, value: any, key?: string): HttpParams {
if (typeof value === "object" && value instanceof Date === false) {
httpParams = this.addToHttpParamsRecursive(httpParams, value);
} else {
httpParams = this.addToHttpParamsRecursive(httpParams, value, key);
}
return httpParams;
}
private addToHttpParamsRecursive(httpParams: HttpParams, value?: any, key?: string): HttpParams {
if (value == null) {
return httpParams;
}
if (typeof value === "object") {
if (Array.isArray(value)) {
(value as any[]).forEach( elem => httpParams = this.addToHttpParamsRecursive(httpParams, elem, key));
} else if (value instanceof Date) {
if (key != null) {
httpParams = httpParams.append(key,
(value as Date).toISOString().substr(0, 10));
} else {
throw Error("key may not be null if value is Date");
}
} else {
Object.keys(value).forEach( k => httpParams = this.addToHttpParamsRecursive(
httpParams, value[k], key != null ? `${key}.${k}` : k));
}
} else if (key != null) {
httpParams = httpParams.append(key, value);
} else {
throw Error("key may not be null if value is not object or array");
}
return httpParams;
}
/**
* Get ntfy information
* Get ntfy information
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
* @param reportProgress flag to report request and response progress.
*/
public ntfyGet(observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext}): Observable<NtfyInformation>;
public ntfyGet(observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext}): Observable<HttpResponse<NtfyInformation>>;
public ntfyGet(observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext}): Observable<HttpEvent<NtfyInformation>>;
public ntfyGet(observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext}): Observable<any> {
let localVarHeaders = this.defaultHeaders;
let localVarCredential: string | undefined;
// authentication (API_TOKEN) required
localVarCredential = this.configuration.lookupCredential('API_TOKEN');
if (localVarCredential) {
localVarHeaders = localVarHeaders.set('Authorization', 'Bearer ' + localVarCredential);
}
let localVarHttpHeaderAcceptSelected: string | undefined = options && options.httpHeaderAccept;
if (localVarHttpHeaderAcceptSelected === undefined) {
// to determine the Accept header
const httpHeaderAccepts: string[] = [
'application/json'
];
localVarHttpHeaderAcceptSelected = this.configuration.selectHeaderAccept(httpHeaderAccepts);
}
if (localVarHttpHeaderAcceptSelected !== undefined) {
localVarHeaders = localVarHeaders.set('Accept', localVarHttpHeaderAcceptSelected);
}
let localVarHttpContext: HttpContext | undefined = options && options.context;
if (localVarHttpContext === undefined) {
localVarHttpContext = new HttpContext();
}
let responseType_: 'text' | 'json' = 'json';
if(localVarHttpHeaderAcceptSelected && localVarHttpHeaderAcceptSelected.startsWith('text')) {
responseType_ = 'text';
}
return this.httpClient.get<NtfyInformation>(`${this.configuration.basePath}/ntfy`,
{
context: localVarHttpContext,
responseType: <any>responseType_,
withCredentials: this.configuration.withCredentials,
headers: localVarHeaders,
observe: observe,
reportProgress: reportProgress
}
);
}
/**
* Edit ntfy information
* Edit ntfy information
* @param ntfyInformation
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
* @param reportProgress flag to report request and response progress.
*/
public ntfyPost(ntfyInformation?: NtfyInformation, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext}): Observable<SimpleStatusResponse>;
public ntfyPost(ntfyInformation?: NtfyInformation, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext}): Observable<HttpResponse<SimpleStatusResponse>>;
public ntfyPost(ntfyInformation?: NtfyInformation, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext}): Observable<HttpEvent<SimpleStatusResponse>>;
public ntfyPost(ntfyInformation?: NtfyInformation, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext}): Observable<any> {
let localVarHeaders = this.defaultHeaders;
let localVarCredential: string | undefined;
// authentication (API_TOKEN) required
localVarCredential = this.configuration.lookupCredential('API_TOKEN');
if (localVarCredential) {
localVarHeaders = localVarHeaders.set('Authorization', 'Bearer ' + localVarCredential);
}
let localVarHttpHeaderAcceptSelected: string | undefined = options && options.httpHeaderAccept;
if (localVarHttpHeaderAcceptSelected === undefined) {
// to determine the Accept header
const httpHeaderAccepts: string[] = [
'application/json'
];
localVarHttpHeaderAcceptSelected = this.configuration.selectHeaderAccept(httpHeaderAccepts);
}
if (localVarHttpHeaderAcceptSelected !== undefined) {
localVarHeaders = localVarHeaders.set('Accept', localVarHttpHeaderAcceptSelected);
}
let localVarHttpContext: HttpContext | undefined = options && options.context;
if (localVarHttpContext === undefined) {
localVarHttpContext = new HttpContext();
}
// to determine the Content-Type header
const consumes: string[] = [
'application/json'
];
const httpContentTypeSelected: string | undefined = this.configuration.selectHeaderContentType(consumes);
if (httpContentTypeSelected !== undefined) {
localVarHeaders = localVarHeaders.set('Content-Type', httpContentTypeSelected);
}
let responseType_: 'text' | 'json' = 'json';
if(localVarHttpHeaderAcceptSelected && localVarHttpHeaderAcceptSelected.startsWith('text')) {
responseType_ = 'text';
}
return this.httpClient.post<SimpleStatusResponse>(`${this.configuration.basePath}/ntfy`,
ntfyInformation,
{
context: localVarHttpContext,
responseType: <any>responseType_,
withCredentials: this.configuration.withCredentials,
headers: localVarHeaders,
observe: observe,
reportProgress: reportProgress
}
);
}
}

View File

@ -15,6 +15,7 @@ export * from './inlineResponse409';
export * from './loginRequest';
export * from './loginResponse';
export * from './manualScheduleStopInfo';
export * from './ntfyInformation';
export * from './passwordChangeRequest';
export * from './propertiesInfo';
export * from './propertyInfo';

View File

@ -0,0 +1,32 @@
/**
* API Title
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 1.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
export interface NtfyInformation {
/**
* host of ntfy service
*/
ntfy_host: string;
/**
* topic of ntfy service
*/
ntfy_topic: string;
/**
* username of ntfy account for publishing news
*/
ntfy_user: string;
/**
* token to ntfy useraccount
*/
ntfy_token?: string;
}

View File

@ -89,6 +89,8 @@ import { ScheduleHistoryComponent } from './statistics/schedule-history/schedule
import {MatButtonToggleModule} from "@angular/material/button-toggle";
import {MatGridListModule} from "@angular/material/grid-list";
import { StopScheduleManuallyComponent } from './dashboard/active-schedule/stop-schedule-manually/stop-schedule-manually.component';
import { ConnectionSettingsComponent } from './user-settings/connection-settings/connection-settings.component';
import { NtfySettingsComponent } from './user-settings/connection-settings/ntfy-settings/ntfy-settings.component';
@NgModule({
declarations: [
AppComponent,
@ -134,6 +136,8 @@ import { StopScheduleManuallyComponent } from './dashboard/active-schedule/stop-
HeatmapActivityComponent,
ScheduleHistoryComponent,
StopScheduleManuallyComponent,
ConnectionSettingsComponent,
NtfySettingsComponent,
],
imports: [
BrowserModule,

View File

@ -0,0 +1 @@
<app-ntfy-settings></app-ntfy-settings>

View File

@ -0,0 +1,21 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ConnectionSettingsComponent } from './connection-settings.component';
describe('ConnectionSettingsComponent', () => {
let component: ConnectionSettingsComponent;
let fixture: ComponentFixture<ConnectionSettingsComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ConnectionSettingsComponent]
});
fixture = TestBed.createComponent(ConnectionSettingsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@ -0,0 +1,10 @@
import { Component } from '@angular/core';
@Component({
selector: 'app-connection-settings',
templateUrl: './connection-settings.component.html',
styleUrls: ['./connection-settings.component.css']
})
export class ConnectionSettingsComponent {
}

View File

@ -0,0 +1,24 @@
<mat-form-field appearance="outline" class="long-form">
<mat-label>Hostname</mat-label>
<input matInput [formControl]="host_formCtrl" placeholder="https://ntfy.sh">
</mat-form-field>
<mat-form-field appearance="outline" class="long-form">
<mat-label>Topic</mat-label>
<input matInput [formControl]="topic_formCtrl" placeholder="Topic">
</mat-form-field>
<mat-form-field appearance="outline" class="long-form">
<mat-label>Username</mat-label>
<input matInput [formControl]="user_formCtrl" placeholder="username">
</mat-form-field>
<mat-form-field appearance="outline" class="long-form">
<mat-label>Token</mat-label>
<input matInput [formControl]="token_formCtrl" placeholder="Token" type="password">
</mat-form-field>
<div style="float: right">
<button mat-raised-button color="primary" (click)="save()">Save</button>
</div>

View File

@ -0,0 +1,21 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { NtfySettingsComponent } from './ntfy-settings.component';
describe('NtfySettingsComponent', () => {
let component: NtfySettingsComponent;
let fixture: ComponentFixture<NtfySettingsComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [NtfySettingsComponent]
});
fixture = TestBed.createComponent(NtfySettingsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@ -0,0 +1,49 @@
import { Component } from '@angular/core';
import {NtfyService} from "../../../../api";
import {Form, FormBuilder, FormControl, FormGroup, Validators} from "@angular/forms";
import {MatSnackBar} from "@angular/material/snack-bar";
@Component({
selector: 'app-ntfy-settings',
templateUrl: './ntfy-settings.component.html',
styleUrls: ['./ntfy-settings.component.css']
})
export class NtfySettingsComponent {
host_formCtrl = new FormControl('', [Validators.required])
topic_formCtrl = new FormControl('', [Validators.required])
user_formCtrl = new FormControl('', [Validators.required])
token_formCtrl = new FormControl('', [Validators.required])
constructor(private ntfyService: NtfyService,
private snackbar: MatSnackBar) {
}
ngOnInit() {
this.ntfyService.ntfyGet().subscribe({
next: resp => {
this.host_formCtrl.setValue(resp.ntfy_host);
this.topic_formCtrl.setValue(resp.ntfy_topic);
this.user_formCtrl.setValue(resp.ntfy_user);
this.token_formCtrl.setValue(resp.ntfy_token!);
}
})
}
save() {
const token = this.token_formCtrl.value === "****" ? "" : this.token_formCtrl.value!
const user = this.user_formCtrl.value === "****" ? "" : this.user_formCtrl.value!
this.ntfyService.ntfyPost({
ntfy_host: this.host_formCtrl.value!,
ntfy_topic: this.topic_formCtrl.value!,
ntfy_token: token,
ntfy_user: user
}).subscribe({
next: resp => {
this.snackbar.open("Ntfy-Configuration updated", "", {duration: 2000})
this.user_formCtrl.setValue("****");
this.token_formCtrl.setValue("****");
}
})
}
}

View File

@ -4,4 +4,10 @@
<app-account-settings></app-account-settings>
</div>
</mat-tab>
<mat-tab label="Connections">
<div class="container">
<app-connection-settings></app-connection-settings>
</div>
</mat-tab>
</mat-tab-group>

View File

@ -2027,7 +2027,53 @@ paths:
type: array
items:
$ref: '#/components/schemas/ScheduleInfo'
/ntfy:
get:
security:
- API_TOKEN: []
tags:
- ntfy
description: Get ntfy information
summary: Get ntfy information
responses:
200:
description: Operation successfull
content:
application/json:
schema:
$ref: '#/components/schemas/NtfyInformation'
404:
description: Not found
content:
application/json:
schema:
$ref: '#/components/schemas/SimpleStatusResponse'
post:
security:
- API_TOKEN: []
tags:
- ntfy
description: Edit ntfy information
summary: Edit ntfy information
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/NtfyInformation'
responses:
200:
description: Operation successfull
content:
application/json:
schema:
$ref: '#/components/schemas/SimpleStatusResponse'
404:
description: Operation successfull
content:
application/json:
schema:
$ref: '#/components/schemas/SimpleStatusResponse'
components:
securitySchemes:
@ -2712,4 +2758,25 @@ components:
duration:
type: number
description: duration in minutes
example: 10
example: 10
NtfyInformation:
required:
- ntfy_host
- ntfy_topic
additionalProperties: false
properties:
ntfy_host:
type: string
description: host of ntfy service
example: https://ntfy.fawkes100.de
ntfy_topic:
type: string
description: topic of ntfy service
example: Topicname
ntfy_user:
type: string
description: username of ntfy account for publishing news
example: password
ntfy_token:
type: string
description: token to ntfy useraccount