From 5a1108e1bf4f2a81b01dacf1990b7bc5204a9b7b Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Fri, 11 Dec 2020 18:08:23 +0100 Subject: [PATCH] Added getting newest config --- .../repositories/AppConfigRepository.java | 4 --- .../plannaplan/services/AppConfigService.java | 27 --------------- .../services/ConfiguratorService.java | 34 +++++++++++++++++-- .../repositories/AppConfigRepositoryTest.java | 33 ------------------ .../services/ConfiguratorServiceTest.java | 25 +++++++++++++- 5 files changed, 56 insertions(+), 67 deletions(-) delete mode 100755 buisnesslogic/src/main/java/com/plannaplan/services/AppConfigService.java delete mode 100755 buisnesslogic/src/test/java/com/plannaplan/repositories/AppConfigRepositoryTest.java diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/AppConfigRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/AppConfigRepository.java index 798f94f..08e3fbb 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/repositories/AppConfigRepository.java +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/AppConfigRepository.java @@ -3,10 +3,6 @@ package com.plannaplan.repositories; import com.plannaplan.entities.AppConfig; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; public interface AppConfigRepository extends JpaRepository { - - @Query("FROM AppConfig ORDER BY configDate DESC") - AppConfig getCurrentConfig(); } diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/AppConfigService.java b/buisnesslogic/src/main/java/com/plannaplan/services/AppConfigService.java deleted file mode 100755 index 6950b53..0000000 --- a/buisnesslogic/src/main/java/com/plannaplan/services/AppConfigService.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.plannaplan.services; - -import com.plannaplan.entities.AppConfig; -import com.plannaplan.repositories.AppConfigRepository; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -@Service -public class AppConfigService { - @Autowired - private AppConfigRepository repo; - - public AppConfigService() { - } - - /** - * saves config instance to dadabase - * - * @param appConfig AppConfig instance with correct dates - * @return AppConfig with id from databse after save - */ - public AppConfig save(AppConfig appConfig) { - return this.repo.save(appConfig); - } - -} diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/ConfiguratorService.java b/buisnesslogic/src/main/java/com/plannaplan/services/ConfiguratorService.java index f053f5d..d98a6c5 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/ConfiguratorService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ConfiguratorService.java @@ -2,10 +2,18 @@ package com.plannaplan.services; import com.plannaplan.models.ConfigData; import com.plannaplan.models.FileData; +import com.plannaplan.repositories.AppConfigRepository; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Component; +import java.security.DrbgParameters.Reseed; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collector; +import java.util.stream.Collectors; + import com.plannaplan.configutils.*; import com.plannaplan.entities.AppConfig; @@ -18,7 +26,7 @@ public class ConfiguratorService { @Autowired private FileToDatabaseMigrator migrator; @Autowired - private AppConfigService configService; + private AppConfigRepository configRepo; public ConfiguratorService() { } @@ -31,8 +39,30 @@ public class ConfiguratorService { public void config(ConfigData data) { FileReader reader = new FileReader(data.getFilestream()); FileData coursesData = reader.read(); - this.configService.save(new AppConfig(data.getFirstTour(), data.getSecondTour())); + this.configRepo.save(new AppConfig(data.getFirstTour(), data.getSecondTour())); migrator.migrate(coursesData); } + /** + * current config getter + * + * @return AppConfig with newest config_date + */ + public AppConfig getCurrentConfig() { + final List repsonse = this.configRepo.findAll().stream().sorted(new Comparator() { + @Override + public int compare(AppConfig i1, AppConfig i2) { + if (i1.getConfigDate().after(i2.getConfigDate())) { + return -1; + } + if (i1.getConfigDate().before(i2.getConfigDate())) { + return 1; + } + return 0; + + } + }).collect(Collectors.toList()); + + return repsonse.get(0); + } } \ No newline at end of file diff --git a/buisnesslogic/src/test/java/com/plannaplan/repositories/AppConfigRepositoryTest.java b/buisnesslogic/src/test/java/com/plannaplan/repositories/AppConfigRepositoryTest.java deleted file mode 100755 index 0da417f..0000000 --- a/buisnesslogic/src/test/java/com/plannaplan/repositories/AppConfigRepositoryTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.plannaplan.repositories; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.sql.Date; - -import com.plannaplan.entities.AppConfig; -import com.plannaplan.models.TourData; - -import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringRunner; -import org.junit.runner.RunWith; - -@RunWith(SpringRunner.class) -@SpringBootTest -@ContextConfiguration -public class AppConfigRepositoryTest { - - @Autowired - private AppConfigRepository repo; - - @Test - public void shouldReturnNewestConfigInstance() { - final Date dateToCheck = new Date(System.currentTimeMillis()); - this.repo.save(new AppConfig(new TourData(null, null), new TourData(null, null))); - this.repo.save(new AppConfig(new TourData(dateToCheck, null), new TourData(null, null))); - - assertTrue(this.repo.getCurrentConfig().getFirstTourStart().equals(dateToCheck)); - } -} diff --git a/buisnesslogic/src/test/java/com/plannaplan/services/ConfiguratorServiceTest.java b/buisnesslogic/src/test/java/com/plannaplan/services/ConfiguratorServiceTest.java index 4b925f9..0190b2c 100755 --- a/buisnesslogic/src/test/java/com/plannaplan/services/ConfiguratorServiceTest.java +++ b/buisnesslogic/src/test/java/com/plannaplan/services/ConfiguratorServiceTest.java @@ -8,9 +8,13 @@ import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.assertTrue; import java.io.InputStream; +import java.sql.Date; import com.plannaplan.TestApplication; +import com.plannaplan.entities.AppConfig; import com.plannaplan.models.ConfigData; +import com.plannaplan.models.TourData; +import com.plannaplan.repositories.AppConfigRepository; import org.junit.Test; import org.junit.runner.RunWith; @@ -32,11 +36,18 @@ public class ConfiguratorServiceTest { @Autowired private LecturerService lecturerService; + @Autowired + private AppConfigRepository appConfigRepo; + @Test public void shouldImportDataToDataBase() { final InputStream inputStream = getClass().getClassLoader() .getResourceAsStream(TestApplication.TEST_CONFIG_FILE); - final ConfigData data = new ConfigData(null, null, inputStream); + final ConfigData data = new ConfigData( + new TourData(new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis() + 86400000)), + new TourData(new Date(System.currentTimeMillis() + 86400000), + new Date(System.currentTimeMillis() + 2 * 86400000)), + inputStream); this.configuratorService.config(data); int courses_ammount = this.courseService.getCoursesAmmount(); @@ -46,4 +57,16 @@ public class ConfiguratorServiceTest { assertTrue(courses_ammount > 0 && groups_ammount > 0 && lecturers_ammount > 0); } + @Test + public void shouldRetrunNewestConfig() throws InterruptedException { + final Date dateToCheck = new Date(System.currentTimeMillis()); + this.appConfigRepo.save(new AppConfig(new TourData(null, null), new TourData(null, null))); + Thread.sleep(2000); + this.appConfigRepo.save(new AppConfig(new TourData(dateToCheck, null), new TourData(null, null))); + + AppConfig response = this.configuratorService.getCurrentConfig(); + assertTrue(response.getFirstTourStart() != null); + + } + }