Added method to getting app states

This commit is contained in:
Filip Izydorczyk 2020-12-12 15:33:00 +01:00
parent e02523b4f4
commit 84d7ed8e65
4 changed files with 178 additions and 4 deletions

View File

@ -9,6 +9,7 @@ import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import com.plannaplan.models.TourData; import com.plannaplan.models.TourData;
import com.plannaplan.types.AppState;
/** /**
* entity that keeps app configurations * entity that keeps app configurations
@ -91,4 +92,27 @@ public class AppConfig {
return configDate; 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;
}
} }

View File

@ -0,0 +1,5 @@
package com.plannaplan.types;
public enum AppState {
FIRST_TOUR, SECOND_TOUR, NO_TOUR
}

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

View File

@ -1,5 +1,6 @@
package com.plannaplan.security.cas; package com.plannaplan.security.cas;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -9,12 +10,15 @@ public class CustomUAMCasValidatorTest {
private String serviceUrl; private String serviceUrl;
@Test @Test
@Ignore
public void shouldValidateWithDomain() { public void shouldValidateWithDomain() {
// 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"); CustomUAMCasValidator validator = new CustomUAMCasValidator(serviceUrl,
"ST-54649-5x4h09vzUpEIyAGmf1sz-cas.amu.edu.pl");
validator.validate(); validator.validate();
} }
} }