Merge pull request 'turs' (#31) from turs into master
Reviewed-on: http://git.plannaplan.pl/filipizydorczyk/backend/pulls/31
This commit is contained in:
commit
ca440a3fd5
@ -46,10 +46,7 @@ W paczce dla proda w protpertiesach poufne dane odczytywane są ze zmiennych śr
|
||||
Zeby spakowac apke do `jara` wystarcza dwie komendy zaczynajac z glownego katalogu projektu
|
||||
|
||||
```
|
||||
mvn clean
|
||||
mvn install
|
||||
cd restservice
|
||||
mvn clean package spring-boot:repackage
|
||||
mvn clean; mvn install; cd restservice; mvn clean package spring-boot:repackage
|
||||
```
|
||||
|
||||
Utworzony zostanie jar w `restservice/target/restservice-1.0-SNAPSHOT.jar`. Oczywiscie zeby jar zadzialal kontenery dockerowe musza byc odpalone (lub baza danych na serwerze jesli zmienialismy propertisy z localhost)
|
||||
|
118
buisnesslogic/src/main/java/com/plannaplan/entities/AppConfig.java
Executable file
118
buisnesslogic/src/main/java/com/plannaplan/entities/AppConfig.java
Executable file
@ -0,0 +1,118 @@
|
||||
package com.plannaplan.entities;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import com.plannaplan.models.TourData;
|
||||
import com.plannaplan.types.AppState;
|
||||
|
||||
/**
|
||||
* entity that keeps app configurations
|
||||
*/
|
||||
@Entity
|
||||
public class AppConfig {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
private Date firstTourStart;
|
||||
private Date firstTourEnd;
|
||||
private Date secondTourStart;
|
||||
private Date secondTourEnd;
|
||||
private Timestamp configDate;
|
||||
|
||||
/**
|
||||
* no parameter construcotor
|
||||
*/
|
||||
public AppConfig() {
|
||||
}
|
||||
|
||||
/**
|
||||
* constructor
|
||||
*
|
||||
* @param firstTour first TourData instacne
|
||||
* @param scondTour second TourData instacne
|
||||
*/
|
||||
public AppConfig(TourData firstTour, TourData scondTour) {
|
||||
this.firstTourStart = firstTour.getStart();
|
||||
this.firstTourEnd = firstTour.getEnd();
|
||||
this.secondTourStart = scondTour.getStart();
|
||||
this.secondTourEnd = scondTour.getEnd();
|
||||
this.configDate = new Timestamp(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* second tour end getter
|
||||
*
|
||||
* @return Date inforamtion when second tour ends
|
||||
*/
|
||||
public Date getSecondTourEnd() {
|
||||
return secondTourEnd;
|
||||
}
|
||||
|
||||
/**
|
||||
* first second start getter
|
||||
*
|
||||
* @return Date inforamtion when second tour start
|
||||
*/
|
||||
public Date getSecondTourStart() {
|
||||
return secondTourStart;
|
||||
}
|
||||
|
||||
/**
|
||||
* first tour end getter
|
||||
*
|
||||
* @return Date inforamtion when first tour end
|
||||
*/
|
||||
public Date getFirstTourEnd() {
|
||||
return firstTourEnd;
|
||||
}
|
||||
|
||||
/**
|
||||
* first tour start getter
|
||||
*
|
||||
* @return Date inforamtion when first tour start
|
||||
*/
|
||||
public Date getFirstTourStart() {
|
||||
return firstTourStart;
|
||||
}
|
||||
|
||||
/**
|
||||
* config date getter
|
||||
*
|
||||
* @return Timestamp when configuration took place
|
||||
*/
|
||||
public Timestamp getConfigDate() {
|
||||
return configDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* current state getter
|
||||
*
|
||||
* @return AppState of app at the moment of calling method
|
||||
*/
|
||||
public AppState getCurrentState() {
|
||||
final Date now = new Date(System.currentTimeMillis());
|
||||
if (this.secondTourEnd.before(now)) {
|
||||
return AppState.NO_TOUR;
|
||||
}
|
||||
if (this.secondTourStart.before(now)) {
|
||||
return AppState.SECOND_TOUR;
|
||||
}
|
||||
if (this.firstTourEnd.before(now)) {
|
||||
return AppState.NO_TOUR;
|
||||
}
|
||||
if (this.firstTourStart.before(now)) {
|
||||
return AppState.FIRST_TOUR;
|
||||
}
|
||||
|
||||
return AppState.NO_TOUR;
|
||||
}
|
||||
|
||||
}
|
@ -11,7 +11,8 @@ import javax.persistence.Id;
|
||||
import com.plannaplan.types.UserRoles;
|
||||
|
||||
/**
|
||||
* Entity of User grouping of state ssociated about id,name,surname,email,role,token,tokenCreatedDate
|
||||
* Entity of User grouping of state ssociated about
|
||||
* id,name,surname,email,role,token,tokenCreatedDate
|
||||
*/
|
||||
|
||||
@Entity
|
||||
@ -28,18 +29,16 @@ public class User {
|
||||
private UserRoles role;
|
||||
private String token;
|
||||
private Timestamp tokenUsageDate;
|
||||
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
/*
|
||||
* User
|
||||
*
|
||||
* @param name name given to the user
|
||||
/**
|
||||
*
|
||||
* @param name name given to the user
|
||||
* @param surname surname given to the user
|
||||
* @param email mail given to the user
|
||||
* @param role role given to the user
|
||||
* @param mail mail given to the user
|
||||
* @param role
|
||||
*/
|
||||
public User(String name, String surname, String mail, UserRoles role) {
|
||||
this.name = name;
|
||||
@ -48,136 +47,144 @@ public class User {
|
||||
this.role = role;
|
||||
}
|
||||
|
||||
/*
|
||||
* User
|
||||
*
|
||||
* @param name name given to the user
|
||||
/**
|
||||
*
|
||||
* @param name name given to the user
|
||||
* @param surname surname given to the user
|
||||
* @param email mail given to the user
|
||||
* @param usosId id in the USOS system
|
||||
* @param role role given to the user
|
||||
* @param mail mail given to the user
|
||||
* @param usosId id in the USOS system
|
||||
* @param role
|
||||
*/
|
||||
public User(String name, String surname, String mail, String usosId, UserRoles role){
|
||||
this(name,surname,mail,role);
|
||||
this.usosId = usosId;
|
||||
public User(String name, String surname, String mail, String usosId, UserRoles role) {
|
||||
this(name, surname, mail, role);
|
||||
this.usosId = usosId;
|
||||
}
|
||||
|
||||
/*
|
||||
* getusosId
|
||||
*
|
||||
* @return usosId
|
||||
/**
|
||||
* usos id getter
|
||||
*
|
||||
* @return usosid
|
||||
*/
|
||||
public String getUsosId() {
|
||||
return usosId;
|
||||
}
|
||||
|
||||
/*
|
||||
* getEmail
|
||||
*
|
||||
* @return email
|
||||
/**
|
||||
* email getter
|
||||
*
|
||||
* @return mailof user
|
||||
*/
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
/*
|
||||
* setEmail
|
||||
*
|
||||
* @param email set email to the user
|
||||
/**
|
||||
* email setter
|
||||
*
|
||||
* @param email user email
|
||||
*/
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
/*
|
||||
* getTokenUsageDate
|
||||
*
|
||||
* @return tokenUsageDate
|
||||
/**
|
||||
* token usage getter
|
||||
*
|
||||
* @return Timestamp when token was used
|
||||
*/
|
||||
public Timestamp getTokenUsageDate() {
|
||||
return tokenUsageDate;
|
||||
}
|
||||
|
||||
/*
|
||||
* getToken
|
||||
*
|
||||
* @return token
|
||||
/**
|
||||
* token getter
|
||||
*
|
||||
* @return user token
|
||||
*/
|
||||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
/*
|
||||
* setToken
|
||||
*
|
||||
* @param token set token to the entity
|
||||
/**
|
||||
* token seter. Sets token and automaticly set time when was set
|
||||
*
|
||||
* @param token token to set
|
||||
*/
|
||||
public void setToken(String token) {
|
||||
this.tokenUsageDate = new Timestamp(System.currentTimeMillis());
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
/* getName
|
||||
*
|
||||
* @return name
|
||||
/**
|
||||
* name setter
|
||||
*
|
||||
* @return String user name
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/* getRole
|
||||
*
|
||||
* @return role
|
||||
/**
|
||||
* user rolse getter
|
||||
*
|
||||
* @return UserRoles of user
|
||||
*/
|
||||
public UserRoles getRole() {
|
||||
return role;
|
||||
}
|
||||
|
||||
/* setRole
|
||||
*
|
||||
* @param role set role to the entity
|
||||
/**
|
||||
* user role setter
|
||||
*
|
||||
* @param role to be set
|
||||
*/
|
||||
public void setRole(UserRoles role) {
|
||||
this.role = role;
|
||||
}
|
||||
|
||||
/*
|
||||
* getSurname
|
||||
* @return surname
|
||||
/**
|
||||
* surname getter
|
||||
*
|
||||
* @return string surname
|
||||
*/
|
||||
public String getSurname() {
|
||||
return surname;
|
||||
}
|
||||
|
||||
/*
|
||||
* setSurname
|
||||
* @param surname set surname into entity user
|
||||
/**
|
||||
* surname setter
|
||||
*
|
||||
* @param surname string to be set as surnames
|
||||
*/
|
||||
public void setSurname(String surname) {
|
||||
this.surname = surname;
|
||||
}
|
||||
|
||||
/*
|
||||
* setName
|
||||
* @param name set name into entity user
|
||||
/**
|
||||
* name stter
|
||||
*
|
||||
* @param name stirng to be set as name
|
||||
*/
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/*
|
||||
* getId
|
||||
* @return id
|
||||
/**
|
||||
* id getter
|
||||
*
|
||||
* @return id in database
|
||||
*/
|
||||
public Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
/*
|
||||
* isCredentialsNonExpired
|
||||
* Returns TRUE if is Credentials Non Expired in the otherwise it returns false
|
||||
/**
|
||||
* it checks if given ammount of time passed since last token usage. If not
|
||||
* retunr true and reset time otherwise return false and token won work anymore
|
||||
*
|
||||
* @return boolena if credentials (token) is expired or not
|
||||
*/
|
||||
public boolean isCredentialsNonExpired() {
|
||||
public boolean isCredentialsNonExpired() {
|
||||
final long diffInMilliseconds = Math
|
||||
.abs(this.tokenUsageDate.getTime() - new Timestamp(System.currentTimeMillis()).getTime());
|
||||
final long minutes = TimeUnit.MILLISECONDS.toMinutes(diffInMilliseconds);
|
||||
|
@ -1,51 +1,50 @@
|
||||
package com.plannaplan.models;
|
||||
|
||||
import java.util.Date;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class ConfigData {
|
||||
private Date start;
|
||||
private Date end;
|
||||
private TourData firstTour;
|
||||
private TourData secondTour;
|
||||
private InputStream filestream;
|
||||
|
||||
/*
|
||||
* ConfigData
|
||||
*
|
||||
* @param start when the configdata begins
|
||||
* @param end when the configdata ends
|
||||
* @param filestream where the filestream is
|
||||
/**
|
||||
* constructor
|
||||
*
|
||||
* @param firstTour date of first tours
|
||||
* @param secondTour date of second tours
|
||||
* @param filestream filestram with data to import
|
||||
*/
|
||||
public ConfigData(Date start, Date end, InputStream filestream) {
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
public ConfigData(TourData firstTour, TourData secondTour, InputStream filestream) {
|
||||
this.firstTour = firstTour;
|
||||
this.secondTour = secondTour;
|
||||
this.filestream = filestream;
|
||||
}
|
||||
|
||||
/*
|
||||
* getFilestream
|
||||
*
|
||||
* @return filestream
|
||||
/**
|
||||
* second tour getter
|
||||
*
|
||||
* @return TourData second tour
|
||||
*/
|
||||
public TourData getSecondTour() {
|
||||
return secondTour;
|
||||
}
|
||||
|
||||
/**
|
||||
* first tour getter
|
||||
*
|
||||
* @return TourData first tour
|
||||
*/
|
||||
public TourData getFirstTour() {
|
||||
return firstTour;
|
||||
}
|
||||
|
||||
/**
|
||||
* filestream getter
|
||||
*
|
||||
* @return InputStream with data to import
|
||||
*/
|
||||
public InputStream getFilestream() {
|
||||
return filestream;
|
||||
}
|
||||
|
||||
/*
|
||||
* getEnd
|
||||
*
|
||||
* @return end
|
||||
*/
|
||||
public Date getEnd() {
|
||||
return end;
|
||||
}
|
||||
|
||||
/*
|
||||
* getStart
|
||||
*
|
||||
* @return start
|
||||
*/
|
||||
public Date getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
}
|
||||
|
49
buisnesslogic/src/main/java/com/plannaplan/models/TourData.java
Executable file
49
buisnesslogic/src/main/java/com/plannaplan/models/TourData.java
Executable file
@ -0,0 +1,49 @@
|
||||
package com.plannaplan.models;
|
||||
|
||||
import java.sql.Date;
|
||||
|
||||
public class TourData {
|
||||
|
||||
private Date start;
|
||||
private Date end;
|
||||
|
||||
/**
|
||||
* construcotr for java.sql.Date
|
||||
*
|
||||
* @param start Date when tour start
|
||||
* @param end Date when tour end
|
||||
*/
|
||||
public TourData(Date start, Date end) {
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
/**
|
||||
* constructor construcotr for java.util.Date
|
||||
*
|
||||
* @param start Date when tour start
|
||||
* @param end Date when tour end
|
||||
*/
|
||||
public TourData(java.util.Date start, java.util.Date end) {
|
||||
this.start = new Date(start.getTime());
|
||||
this.end = new Date(end.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* tour end getter
|
||||
*
|
||||
* @return Date end of tour
|
||||
*/
|
||||
public Date getEnd() {
|
||||
return end;
|
||||
}
|
||||
|
||||
/**
|
||||
* tour start getter
|
||||
*
|
||||
* @return Date beginning of tour
|
||||
*/
|
||||
public Date getStart() {
|
||||
return start;
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.plannaplan.repositories;
|
||||
|
||||
import com.plannaplan.entities.AppConfig;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface AppConfigRepository extends JpaRepository<AppConfig, Long> {
|
||||
}
|
@ -2,28 +2,64 @@ 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.stereotype.Component;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.plannaplan.configutils.*;
|
||||
import com.plannaplan.entities.AppConfig;
|
||||
|
||||
/**
|
||||
* FileReader is used for reading xls file from input stream.
|
||||
* FileReader is used for reading xls file from input stream.
|
||||
*/
|
||||
@Component
|
||||
public class ConfiguratorService {
|
||||
|
||||
@Autowired
|
||||
private FileToDatabaseMigrator migrator;
|
||||
@Autowired
|
||||
private AppConfigRepository configRepo;
|
||||
|
||||
public ConfiguratorService() {
|
||||
}
|
||||
|
||||
/**
|
||||
* methoid to config system
|
||||
*
|
||||
* @param data ConfigData containng system configs
|
||||
*/
|
||||
public void config(ConfigData data) {
|
||||
FileReader reader = new FileReader(data.getFilestream());
|
||||
FileData coursesData = reader.read();
|
||||
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);
|
||||
}
|
||||
}
|
5
buisnesslogic/src/main/java/com/plannaplan/types/AppState.java
Executable file
5
buisnesslogic/src/main/java/com/plannaplan/types/AppState.java
Executable file
@ -0,0 +1,5 @@
|
||||
package com.plannaplan.types;
|
||||
|
||||
public enum AppState {
|
||||
FIRST_TOUR, SECOND_TOUR, NO_TOUR
|
||||
}
|
141
buisnesslogic/src/test/java/com/plannaplan/entities/AppConfigTest.java
Executable file
141
buisnesslogic/src/test/java/com/plannaplan/entities/AppConfigTest.java
Executable file
@ -0,0 +1,141 @@
|
||||
package com.plannaplan.entities;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.sql.Date;
|
||||
|
||||
import com.plannaplan.models.TourData;
|
||||
import com.plannaplan.types.AppState;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AppConfigTest {
|
||||
|
||||
private static long ONE_DAY = 86400000;
|
||||
|
||||
@Test
|
||||
public void shouldReturnNoTourDueToTooEarly() {
|
||||
final Date firtstTourStart = new Date(System.currentTimeMillis() + ONE_DAY);
|
||||
final Date firtstTourEnd = new Date(System.currentTimeMillis() + 2 * ONE_DAY);
|
||||
final Date secondTourStart = new Date(System.currentTimeMillis() + 3 * ONE_DAY);
|
||||
final Date secondTourEnd = new Date(System.currentTimeMillis() + 4 * ONE_DAY);
|
||||
|
||||
final AppConfig config = new AppConfig(new TourData(firtstTourStart, firtstTourEnd),
|
||||
new TourData(secondTourStart, secondTourEnd));
|
||||
assertTrue(config.getCurrentState() == AppState.NO_TOUR);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnFirstTourDueToStart() throws InterruptedException {
|
||||
final Date firtstTourStart = new Date(System.currentTimeMillis());
|
||||
final Date firtstTourEnd = new Date(System.currentTimeMillis() + 2 * ONE_DAY);
|
||||
final Date secondTourStart = new Date(System.currentTimeMillis() + 3 * ONE_DAY);
|
||||
final Date secondTourEnd = new Date(System.currentTimeMillis() + 4 * ONE_DAY);
|
||||
|
||||
final AppConfig config = new AppConfig(new TourData(firtstTourStart, firtstTourEnd),
|
||||
new TourData(secondTourStart, secondTourEnd));
|
||||
Thread.sleep(1000);
|
||||
assertTrue(config.getCurrentState() == AppState.FIRST_TOUR);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnFirstTourDueToInBetween() {
|
||||
final Date firtstTourStart = new Date(System.currentTimeMillis() - ONE_DAY);
|
||||
final Date firtstTourEnd = new Date(System.currentTimeMillis() + 2 * ONE_DAY);
|
||||
final Date secondTourStart = new Date(System.currentTimeMillis() + 3 * ONE_DAY);
|
||||
final Date secondTourEnd = new Date(System.currentTimeMillis() + 4 * ONE_DAY);
|
||||
|
||||
final AppConfig config = new AppConfig(new TourData(firtstTourStart, firtstTourEnd),
|
||||
new TourData(secondTourStart, secondTourEnd));
|
||||
assertTrue(config.getCurrentState() == AppState.FIRST_TOUR);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNoTourInLastDay() throws InterruptedException {
|
||||
final Date firtstTourStart = new Date(System.currentTimeMillis() - ONE_DAY);
|
||||
final Date firtstTourEnd = new Date(System.currentTimeMillis());
|
||||
final Date secondTourStart = new Date(System.currentTimeMillis() + 3 * ONE_DAY);
|
||||
final Date secondTourEnd = new Date(System.currentTimeMillis() + 4 * ONE_DAY);
|
||||
|
||||
final AppConfig config = new AppConfig(new TourData(firtstTourStart, firtstTourEnd),
|
||||
new TourData(secondTourStart, secondTourEnd));
|
||||
Thread.sleep(1000);
|
||||
assertTrue(config.getCurrentState() == AppState.NO_TOUR);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNoTourInBetween() {
|
||||
final Date firtstTourStart = new Date(System.currentTimeMillis() - 2 * ONE_DAY);
|
||||
final Date firtstTourEnd = new Date(System.currentTimeMillis() - ONE_DAY);
|
||||
final Date secondTourStart = new Date(System.currentTimeMillis() + 3 * ONE_DAY);
|
||||
final Date secondTourEnd = new Date(System.currentTimeMillis() + 4 * ONE_DAY);
|
||||
|
||||
final AppConfig config = new AppConfig(new TourData(firtstTourStart, firtstTourEnd),
|
||||
new TourData(secondTourStart, secondTourEnd));
|
||||
assertTrue(config.getCurrentState() == AppState.NO_TOUR);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnSecondTourInStart() throws InterruptedException {
|
||||
final Date firtstTourStart = new Date(System.currentTimeMillis() - 2 * ONE_DAY);
|
||||
final Date firtstTourEnd = new Date(System.currentTimeMillis() - ONE_DAY);
|
||||
final Date secondTourStart = new Date(System.currentTimeMillis());
|
||||
final Date secondTourEnd = new Date(System.currentTimeMillis() + 4 * ONE_DAY);
|
||||
|
||||
final AppConfig config = new AppConfig(new TourData(firtstTourStart, firtstTourEnd),
|
||||
new TourData(secondTourStart, secondTourEnd));
|
||||
Thread.sleep(1000);
|
||||
assertTrue(config.getCurrentState() == AppState.SECOND_TOUR);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnSecondTourInBetween() {
|
||||
final Date firtstTourStart = new Date(System.currentTimeMillis() - 3 * ONE_DAY);
|
||||
final Date firtstTourEnd = new Date(System.currentTimeMillis() - 2 * ONE_DAY);
|
||||
final Date secondTourStart = new Date(System.currentTimeMillis() - ONE_DAY);
|
||||
final Date secondTourEnd = new Date(System.currentTimeMillis() + 4 * ONE_DAY);
|
||||
|
||||
final AppConfig config = new AppConfig(new TourData(firtstTourStart, firtstTourEnd),
|
||||
new TourData(secondTourStart, secondTourEnd));
|
||||
assertTrue(config.getCurrentState() == AppState.SECOND_TOUR);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNoTourDueToEndSecond() throws InterruptedException {
|
||||
final Date firtstTourStart = new Date(System.currentTimeMillis() - 3 * ONE_DAY);
|
||||
final Date firtstTourEnd = new Date(System.currentTimeMillis() - 2 * ONE_DAY);
|
||||
final Date secondTourStart = new Date(System.currentTimeMillis() - ONE_DAY);
|
||||
final Date secondTourEnd = new Date(System.currentTimeMillis());
|
||||
|
||||
final AppConfig config = new AppConfig(new TourData(firtstTourStart, firtstTourEnd),
|
||||
new TourData(secondTourStart, secondTourEnd));
|
||||
Thread.sleep(1000);
|
||||
assertTrue(config.getCurrentState() == AppState.NO_TOUR);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNoTourAfterSecondEnd() {
|
||||
final Date firtstTourStart = new Date(System.currentTimeMillis() - 4 * ONE_DAY);
|
||||
final Date firtstTourEnd = new Date(System.currentTimeMillis() - 3 * ONE_DAY);
|
||||
final Date secondTourStart = new Date(System.currentTimeMillis() - 2 * ONE_DAY);
|
||||
final Date secondTourEnd = new Date(System.currentTimeMillis() - ONE_DAY);
|
||||
|
||||
final AppConfig config = new AppConfig(new TourData(firtstTourStart, firtstTourEnd),
|
||||
new TourData(secondTourStart, secondTourEnd));
|
||||
assertTrue(config.getCurrentState() == AppState.NO_TOUR);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnSecondTourWhereThereIsABrake() throws InterruptedException {
|
||||
final Date firtstTourStart = new Date(System.currentTimeMillis() - 4 * ONE_DAY);
|
||||
final Date firtstTourEnd = new Date(System.currentTimeMillis());
|
||||
final Date secondTourStart = new Date(System.currentTimeMillis());
|
||||
final Date secondTourEnd = new Date(System.currentTimeMillis() + ONE_DAY);
|
||||
|
||||
final AppConfig config = new AppConfig(new TourData(firtstTourStart, firtstTourEnd),
|
||||
new TourData(secondTourStart, secondTourEnd));
|
||||
Thread.sleep(1000);
|
||||
assertTrue(config.getCurrentState() == AppState.SECOND_TOUR);
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
3
init.sql
3
init.sql
@ -1 +1,2 @@
|
||||
CREATE DATABASE IF NOT EXISTS test;
|
||||
CREATE DATABASE IF NOT EXISTS test;
|
||||
SET GLOBAL time_zone = '+1:00';
|
@ -1,8 +1,10 @@
|
||||
package com.plannaplan;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.sql.Date;
|
||||
|
||||
import com.plannaplan.models.ConfigData;
|
||||
import com.plannaplan.models.TourData;
|
||||
import com.plannaplan.entities.User;
|
||||
import com.plannaplan.services.UserService;
|
||||
import com.plannaplan.types.UserRoles;
|
||||
@ -38,13 +40,22 @@ public class App {
|
||||
SpringApplication.run(App.class, args);
|
||||
}
|
||||
|
||||
/**
|
||||
* method to import mocked data to testing app after startz. It is called only
|
||||
* in dev profile
|
||||
*/
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void importData() {
|
||||
System.out.println(Logo.getInitInfo(isDev));
|
||||
|
||||
if (this.isDev) {
|
||||
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("Zajecia.xlsx");
|
||||
ConfigData data = new ConfigData(null, null, inputStream);
|
||||
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.contrl.config(data);
|
||||
|
||||
User newuser = new User();
|
||||
|
@ -4,12 +4,15 @@ import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
import com.plannaplan.App;
|
||||
import com.plannaplan.models.ConfigData;
|
||||
import com.plannaplan.models.TourData;
|
||||
import com.plannaplan.services.ConfiguratorService;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
@ -33,13 +36,26 @@ public class ConfigController {
|
||||
@Autowired
|
||||
private ConfiguratorService contrl;
|
||||
|
||||
@PostMapping("/config")
|
||||
@PostMapping(path = "/config", consumes = { "multipart/form-data" })
|
||||
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
||||
@ApiOperation("Imports data to system. To call you need to provide ADMIN token")
|
||||
public ResponseEntity<String> configApp(
|
||||
@RequestParam("file") @ApiParam(value = "file .xlsx that contains courses and groups with apoinnted rules") MultipartFile file) {
|
||||
@RequestParam("file") @ApiParam(value = "file .xlsx that contains courses and groups with apoinnted rules") MultipartFile file,
|
||||
@RequestParam("firstTourBegin") @DateTimeFormat(pattern = "dd.MM.yyyy") @ApiParam(value = "Date when first tour begin in format dd.MM.yyyy") Date firstTourBegin,
|
||||
@RequestParam("firstTourEnd") @DateTimeFormat(pattern = "dd.MM.yyyy") @ApiParam(value = "Date when first tour ends in format dd.MM.yyyy") Date firstTourEnd,
|
||||
@RequestParam("secondTourBegin") @DateTimeFormat(pattern = "dd.MM.yyyy") @ApiParam(value = "Date when second tour begin in format dd.MM.yyyy") Date secondTourBegin,
|
||||
@RequestParam("secondTourEnd") @DateTimeFormat(pattern = "dd.MM.yyyy") @ApiParam(value = "Date when second tour ends in format dd.MM.yyyy") Date secondTourEnd) {
|
||||
try {
|
||||
final ConfigData data = new ConfigData(null, null, file.getInputStream());
|
||||
if (!(firstTourBegin.before(firstTourEnd)
|
||||
&& (firstTourEnd.before(secondTourBegin) || firstTourEnd.equals(secondTourBegin))
|
||||
&& secondTourBegin.before(secondTourEnd))) {
|
||||
return new ResponseEntity<>("Bad dates", HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
final TourData firstTour = new TourData(firstTourBegin, firstTourEnd);
|
||||
final TourData secondTour = new TourData(secondTourBegin, secondTourEnd);
|
||||
|
||||
final ConfigData data = new ConfigData(firstTour, secondTour, file.getInputStream());
|
||||
this.contrl.config(data);
|
||||
return new ResponseEntity<>("Sucess", HttpStatus.OK);
|
||||
} catch (IOException e) {
|
||||
|
@ -2,11 +2,15 @@ package com.plannaplan.controllers;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.sql.Date;
|
||||
|
||||
import com.plannaplan.entities.AppConfig;
|
||||
import com.plannaplan.entities.User;
|
||||
import com.plannaplan.services.ConfiguratorService;
|
||||
import com.plannaplan.services.UserService;
|
||||
import com.plannaplan.types.UserRoles;
|
||||
|
||||
@ -28,9 +32,15 @@ public class ConfigControllerTest extends AbstractControllerTest {
|
||||
|
||||
private static final String FILE_NAME = "Zajecia.xlsx";
|
||||
private static final String CONFIG_ENDPOINT = "/api/v1/configurator/config";
|
||||
private static final String FIRST_TOUR_START = "firstTourBegin";
|
||||
private static final String FIRST_TOUR_END = "firstTourEnd";
|
||||
private static final String SECOND_TOUR_START = "secondTourBegin";
|
||||
private static final String SECOND_TOUR_END = "secondTourEnd";
|
||||
|
||||
@Autowired
|
||||
private UserService service;
|
||||
@Autowired
|
||||
private ConfiguratorService configService;
|
||||
|
||||
@Test
|
||||
public void shouldReturnNoAuthorized() throws Exception {
|
||||
@ -52,11 +62,40 @@ public class ConfigControllerTest extends AbstractControllerTest {
|
||||
final String token = this.service.login(usr).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).header("Authorization", "Bearer " + token))
|
||||
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).param(FIRST_TOUR_START, "12.12.2020")
|
||||
.param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "16.12.2020")
|
||||
.param(SECOND_TOUR_END, "20.12.2020").header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldInsertedDataBeSameInDatabase() throws Exception {
|
||||
final String mail = "shouldReturnOKAuthorized@ConfigController.test";
|
||||
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
|
||||
|
||||
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
|
||||
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
|
||||
final String token = this.service.login(usr).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).param(FIRST_TOUR_START, "12.12.2020")
|
||||
.param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "16.12.2020")
|
||||
.param(SECOND_TOUR_END, "20.12.2020").header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
final AppConfig config = this.configService.getCurrentConfig();
|
||||
assertTrue(config.getFirstTourStart().compareTo(Date.valueOf("2020-12-12")) == 0,
|
||||
"Date in database is wrong. Perhabs database has wrong timezone set.");
|
||||
assertTrue(config.getFirstTourEnd().compareTo(Date.valueOf("2020-12-14")) == 0,
|
||||
"Date in database is wrong. Perhabs database has wrong timezone set.");
|
||||
assertTrue(config.getSecondTourStart().compareTo(Date.valueOf("2020-12-16")) == 0,
|
||||
"Date in database is wrong. Perhabs database has wrong timezone set.");
|
||||
assertTrue(config.getSecondTourEnd().compareTo(Date.valueOf("2020-12-20")) == 0,
|
||||
"Date in database is wrong. Perhabs database has wrong timezone set.");
|
||||
// yyyy-mm-dd
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnDenyNoAdminAuthorized() throws Exception {
|
||||
final String mail = "shouldReturnDenyNoAdminAuthorized@ConfigController.test";
|
||||
@ -72,4 +111,112 @@ public class ConfigControllerTest extends AbstractControllerTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFailDueToWrongFirstTour() throws Exception {
|
||||
final String mail = "shouldFailDueToWrongFirstTour@ConfigController.test";
|
||||
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
|
||||
|
||||
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
|
||||
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
|
||||
final String token = this.service.login(usr).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).param(FIRST_TOUR_START, "12.12.2020")
|
||||
.param(FIRST_TOUR_END, "12.12.2020").param(SECOND_TOUR_START, "16.12.2020")
|
||||
.param(SECOND_TOUR_END, "20.12.2020").header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().is4xxClientError());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFailDueToWrongSecondTour() throws Exception {
|
||||
final String mail = "shouldFailDueToWrongSecondTour@ConfigController.test";
|
||||
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
|
||||
|
||||
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
|
||||
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
|
||||
final String token = this.service.login(usr).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).param(FIRST_TOUR_START, "12.12.2020")
|
||||
.param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "16.12.2020")
|
||||
.param(SECOND_TOUR_END, "16.12.2020").header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().is4xxClientError());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFailDueToWrongBothTour() throws Exception {
|
||||
final String mail = "shouldFailDueToWrongBothTour@ConfigController.test";
|
||||
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
|
||||
|
||||
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
|
||||
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
|
||||
final String token = this.service.login(usr).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).param(FIRST_TOUR_START, "12.12.2020")
|
||||
.param(FIRST_TOUR_END, "12.12.2020").param(SECOND_TOUR_START, "16.12.2020")
|
||||
.param(SECOND_TOUR_END, "16.12.2020").header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().is4xxClientError());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFailDueToWrongTourTransition() throws Exception {
|
||||
final String mail = "shouldFailDueToWrongTourTransition@ConfigController.test";
|
||||
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
|
||||
|
||||
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
|
||||
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
|
||||
final String token = this.service.login(usr).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).param(FIRST_TOUR_START, "12.12.2020")
|
||||
.param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "13.12.2020")
|
||||
.param(SECOND_TOUR_END, "16.12.2020").header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().is4xxClientError());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFailDueToWrongDateFormat() throws Exception {
|
||||
final String mail = "shouldFailDueToWrongDateFormat@ConfigController.test";
|
||||
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
|
||||
|
||||
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
|
||||
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
|
||||
final String token = this.service.login(usr).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).param(FIRST_TOUR_START, "12.12.2020")
|
||||
.param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "16.12.2020")
|
||||
.param(SECOND_TOUR_END, "20/12/2020").header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().is4xxClientError());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFailDueToNoDate() throws Exception {
|
||||
final String mail = "shouldFailDueToNoDate@ConfigController.test";
|
||||
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
|
||||
|
||||
final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME);
|
||||
final MockMultipartFile file = new MockMultipartFile("file", inputStream);
|
||||
final String token = this.service.login(usr).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(multipart(CONFIG_ENDPOINT).file(file).header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().is4xxClientError());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFailDueToNoFile() throws Exception {
|
||||
final String mail = "shouldFailDueToNoFile@ConfigController.test";
|
||||
final User usr = this.service.save(new User(null, null, mail, UserRoles.ADMIN));
|
||||
|
||||
final String token = this.service.login(usr).getToken();
|
||||
|
||||
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
|
||||
mockMvc.perform(multipart(CONFIG_ENDPOINT).param(FIRST_TOUR_START, "12.12.2020")
|
||||
.param(FIRST_TOUR_END, "14.12.2020").param(SECOND_TOUR_START, "16.12.2020")
|
||||
.param(SECOND_TOUR_END, "20.12.2020").header("Authorization", "Bearer " + token))
|
||||
.andExpect(status().is4xxClientError());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.plannaplan.security.cas;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
@ -9,12 +10,15 @@ public class CustomUAMCasValidatorTest {
|
||||
private String serviceUrl;
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void shouldValidateWithDomain() {
|
||||
|
||||
CustomUAMCasValidator validator = new CustomUAMCasValidator(serviceUrl, "ST-54649-5x4h09vzUpEIyAGmf1sz-cas.amu.edu.pl");
|
||||
|
||||
validator.validate();
|
||||
// you need to privide fresh ticket to make this test pass that's why it is
|
||||
// marked as ignored
|
||||
|
||||
CustomUAMCasValidator validator = new CustomUAMCasValidator(serviceUrl,
|
||||
"ST-54649-5x4h09vzUpEIyAGmf1sz-cas.amu.edu.pl");
|
||||
|
||||
validator.validate();
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user