Added getting newest config

This commit is contained in:
Filip Izydorczyk 2020-12-11 18:08:23 +01:00
parent 15533525af
commit 5a1108e1bf
5 changed files with 56 additions and 67 deletions

View File

@ -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<AppConfig, Long> {
@Query("FROM AppConfig ORDER BY configDate DESC")
AppConfig getCurrentConfig();
}

View File

@ -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);
}
}

View File

@ -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<AppConfig> repsonse = this.configRepo.findAll().stream().sorted(new Comparator<AppConfig>() {
@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);
}
}

View File

@ -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));
}
}

View File

@ -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);
}
}