From b1091cd3bc1df2c366b559e48f04c97d9fd06f23 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Tue, 8 Dec 2020 18:16:17 +0100 Subject: [PATCH 01/11] Added tour data model --- README.md | 5 +- .../com/plannaplan/models/ConfigData.java | 67 +++++++++---------- .../java/com/plannaplan/models/TourData.java | 38 +++++++++++ 3 files changed, 72 insertions(+), 38 deletions(-) create mode 100755 buisnesslogic/src/main/java/com/plannaplan/models/TourData.java diff --git a/README.md b/README.md index 281203b..bc96534 100755 --- a/README.md +++ b/README.md @@ -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) diff --git a/buisnesslogic/src/main/java/com/plannaplan/models/ConfigData.java b/buisnesslogic/src/main/java/com/plannaplan/models/ConfigData.java index 6cd1fc3..f5c966d 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/models/ConfigData.java +++ b/buisnesslogic/src/main/java/com/plannaplan/models/ConfigData.java @@ -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; - } - } diff --git a/buisnesslogic/src/main/java/com/plannaplan/models/TourData.java b/buisnesslogic/src/main/java/com/plannaplan/models/TourData.java new file mode 100755 index 0000000..2c661c7 --- /dev/null +++ b/buisnesslogic/src/main/java/com/plannaplan/models/TourData.java @@ -0,0 +1,38 @@ +package com.plannaplan.models; + +import java.util.Date; + +public class TourData { + + private Date start; + private Date end; + + /** + * construcotr + * + * @param start Date when tour start + * @param end Date when tour end + */ + public TourData(Date start, Date end) { + this.start = start; + this.end = end; + } + + /** + * 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; + } +} From 7c0c22e7905589a90fb34aea018b4fa725dc4e9f Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Wed, 9 Dec 2020 15:02:06 +0100 Subject: [PATCH 02/11] Added required parameters to config --- .../java/com/plannaplan/models/TourData.java | 15 ++- .../controllers/ConfigController.java | 23 +++- .../controllers/ConfigControllerTest.java | 116 +++++++++++++++++- 3 files changed, 148 insertions(+), 6 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/models/TourData.java b/buisnesslogic/src/main/java/com/plannaplan/models/TourData.java index 2c661c7..7858f08 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/models/TourData.java +++ b/buisnesslogic/src/main/java/com/plannaplan/models/TourData.java @@ -1,6 +1,6 @@ package com.plannaplan.models; -import java.util.Date; +import java.sql.Date; public class TourData { @@ -8,7 +8,7 @@ public class TourData { private Date end; /** - * construcotr + * construcotr for java.sql.Date * * @param start Date when tour start * @param end Date when tour end @@ -18,6 +18,17 @@ public class TourData { 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 * diff --git a/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java b/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java index 58b94e1..33866b5 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java @@ -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,27 @@ 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 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") Date firstTourBegin, + @RequestParam("firstTourEnd") @DateTimeFormat(pattern = "dd.MM.yyyy") Date firstTourEnd, + @RequestParam("secondTourBegin") @DateTimeFormat(pattern = "dd.MM.yyyy") Date secondTourBegin, + @RequestParam("secondTourEnd") @DateTimeFormat(pattern = "dd.MM.yyyy") Date secondTourEnd) { try { - final ConfigData data = new ConfigData(null, null, file.getInputStream()); + System.out.println(firstTourBegin); + 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) { diff --git a/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java index ec73e79..72cafe8 100755 --- a/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java @@ -28,6 +28,10 @@ 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; @@ -52,7 +56,9 @@ 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()); } @@ -72,4 +78,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()); + } + } From f08e7cf1e5be6013e2084eae13833e32e545f70c Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Wed, 9 Dec 2020 15:10:19 +0100 Subject: [PATCH 03/11] Swagger doscs --- .../com/plannaplan/controllers/ConfigController.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java b/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java index 33866b5..f6fc8df 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ConfigController.java @@ -41,12 +41,11 @@ public class ConfigController { @ApiOperation("Imports data to system. To call you need to provide ADMIN token") public ResponseEntity configApp( @RequestParam("file") @ApiParam(value = "file .xlsx that contains courses and groups with apoinnted rules") MultipartFile file, - @RequestParam("firstTourBegin") @DateTimeFormat(pattern = "dd.MM.yyyy") Date firstTourBegin, - @RequestParam("firstTourEnd") @DateTimeFormat(pattern = "dd.MM.yyyy") Date firstTourEnd, - @RequestParam("secondTourBegin") @DateTimeFormat(pattern = "dd.MM.yyyy") Date secondTourBegin, - @RequestParam("secondTourEnd") @DateTimeFormat(pattern = "dd.MM.yyyy") Date secondTourEnd) { + @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 { - System.out.println(firstTourBegin); if (!(firstTourBegin.before(firstTourEnd) && (firstTourEnd.before(secondTourBegin) || firstTourEnd.equals(secondTourBegin)) && secondTourBegin.before(secondTourEnd))) { From 0cee4543561f7527f3f6161e6726b844f3850a7c Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Wed, 9 Dec 2020 16:43:29 +0100 Subject: [PATCH 04/11] Added saving conifg to databse --- .../com/plannaplan/entities/AppConfig.java | 94 +++++++++++++++++++ .../repositories/AppConfigRepository.java | 9 ++ .../plannaplan/services/AppConfigService.java | 26 +++++ .../services/ConfiguratorService.java | 11 ++- .../src/main/java/com/plannaplan/App.java | 9 +- 5 files changed, 147 insertions(+), 2 deletions(-) create mode 100755 buisnesslogic/src/main/java/com/plannaplan/entities/AppConfig.java create mode 100755 buisnesslogic/src/main/java/com/plannaplan/repositories/AppConfigRepository.java create mode 100755 buisnesslogic/src/main/java/com/plannaplan/services/AppConfigService.java diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/AppConfig.java b/buisnesslogic/src/main/java/com/plannaplan/entities/AppConfig.java new file mode 100755 index 0000000..49a035f --- /dev/null +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/AppConfig.java @@ -0,0 +1,94 @@ +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; + +/** + * 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; + } + +} diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/AppConfigRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/AppConfigRepository.java new file mode 100755 index 0000000..c512311 --- /dev/null +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/AppConfigRepository.java @@ -0,0 +1,9 @@ +package com.plannaplan.repositories; + +import com.plannaplan.entities.AppConfig; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface AppConfigRepository extends JpaRepository { + +} diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/AppConfigService.java b/buisnesslogic/src/main/java/com/plannaplan/services/AppConfigService.java new file mode 100755 index 0000000..6f66bd5 --- /dev/null +++ b/buisnesslogic/src/main/java/com/plannaplan/services/AppConfigService.java @@ -0,0 +1,26 @@ +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 e0fa31c..f053f5d 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/ConfiguratorService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ConfiguratorService.java @@ -7,22 +7,31 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; 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 AppConfigService configService; public ConfiguratorService() { } + /** + * methoid to config system + * + * @param data ConfigData containng system congis + */ public void config(ConfigData data) { FileReader reader = new FileReader(data.getFilestream()); FileData coursesData = reader.read(); + this.configService.save(new AppConfig(data.getFirstTour(), data.getSecondTour())); migrator.migrate(coursesData); } diff --git a/restservice/src/main/java/com/plannaplan/App.java b/restservice/src/main/java/com/plannaplan/App.java index 1e114ba..2c5d4c9 100755 --- a/restservice/src/main/java/com/plannaplan/App.java +++ b/restservice/src/main/java/com/plannaplan/App.java @@ -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; @@ -44,7 +46,12 @@ public class App { 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())), + new TourData(new Date(System.currentTimeMillis()), + new Date(System.currentTimeMillis())), + inputStream); this.contrl.config(data); User newuser = new User(); From 062b02da6c3b3ff3591be2641763133f0a6abf5a Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Wed, 9 Dec 2020 16:57:49 +0100 Subject: [PATCH 05/11] user docs corrected --- .../java/com/plannaplan/entities/User.java | 141 +++++++++--------- 1 file changed, 74 insertions(+), 67 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/User.java b/buisnesslogic/src/main/java/com/plannaplan/entities/User.java index 9bf955d..0633dcc 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/User.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/User.java @@ -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); From 0b807e355c0c4b39d8affeda9667ecd9696ce7b4 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Wed, 9 Dec 2020 17:43:48 +0100 Subject: [PATCH 06/11] chckpoint: test to buisnesslogic/src/test/java/com/plannaplan/repositories/AppConfigRepositoryTest.java --- .../repositories/AppConfigRepository.java | 3 ++ .../plannaplan/services/AppConfigService.java | 1 + .../repositories/AppConfigRepositoryTest.java | 33 +++++++++++++++++++ .../controllers/ConfigControllerTest.java | 23 +++++++++++++ 4 files changed, 60 insertions(+) create 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 c512311..798f94f 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/repositories/AppConfigRepository.java +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/AppConfigRepository.java @@ -3,7 +3,10 @@ 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 index 6f66bd5..6950b53 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/AppConfigService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/AppConfigService.java @@ -23,4 +23,5 @@ public class AppConfigService { public AppConfig save(AppConfig appConfig) { return this.repo.save(appConfig); } + } diff --git a/buisnesslogic/src/test/java/com/plannaplan/repositories/AppConfigRepositoryTest.java b/buisnesslogic/src/test/java/com/plannaplan/repositories/AppConfigRepositoryTest.java new file mode 100755 index 0000000..0da417f --- /dev/null +++ b/buisnesslogic/src/test/java/com/plannaplan/repositories/AppConfigRepositoryTest.java @@ -0,0 +1,33 @@ +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/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java index 72cafe8..d07445f 100755 --- a/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java @@ -2,11 +2,14 @@ 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.assertFalse; import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*; import java.io.InputStream; +import com.plannaplan.entities.AppConfig; import com.plannaplan.entities.User; +import com.plannaplan.services.AppConfigService; import com.plannaplan.services.UserService; import com.plannaplan.types.UserRoles; @@ -36,6 +39,9 @@ public class ConfigControllerTest extends AbstractControllerTest { @Autowired private UserService service; + @Autowired + private AppConfigService configService; + @Test public void shouldReturnNoAuthorized() throws Exception { final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME); @@ -63,6 +69,23 @@ public class ConfigControllerTest extends AbstractControllerTest { } + @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()); + assertFalse(true); + } + @Test public void shouldReturnDenyNoAdminAuthorized() throws Exception { final String mail = "shouldReturnDenyNoAdminAuthorized@ConfigController.test"; From d2a679fb221ab6cf928bbd9579d1c19c331f8734 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Fri, 11 Dec 2020 15:27:38 +0100 Subject: [PATCH 07/11] added dev conig --- restservice/src/main/java/com/plannaplan/App.java | 6 +++--- .../com/plannaplan/controllers/ConfigControllerTest.java | 5 ----- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/restservice/src/main/java/com/plannaplan/App.java b/restservice/src/main/java/com/plannaplan/App.java index 2c5d4c9..478504e 100755 --- a/restservice/src/main/java/com/plannaplan/App.java +++ b/restservice/src/main/java/com/plannaplan/App.java @@ -48,9 +48,9 @@ public class App { InputStream inputStream = getClass().getClassLoader().getResourceAsStream("Zajecia.xlsx"); ConfigData data = new ConfigData( new TourData(new Date(System.currentTimeMillis()), - new Date(System.currentTimeMillis())), - new TourData(new Date(System.currentTimeMillis()), - 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); diff --git a/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java index d07445f..517f674 100755 --- a/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java @@ -7,9 +7,7 @@ import static org.springframework.security.test.web.servlet.setup.SecurityMockMv import java.io.InputStream; -import com.plannaplan.entities.AppConfig; import com.plannaplan.entities.User; -import com.plannaplan.services.AppConfigService; import com.plannaplan.services.UserService; import com.plannaplan.types.UserRoles; @@ -39,9 +37,6 @@ public class ConfigControllerTest extends AbstractControllerTest { @Autowired private UserService service; - @Autowired - private AppConfigService configService; - @Test public void shouldReturnNoAuthorized() throws Exception { final InputStream inputStream = getClass().getClassLoader().getResourceAsStream(FILE_NAME); From 15533525af4b536e8a28d919daefe67019b2a30e Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Fri, 11 Dec 2020 16:18:43 +0100 Subject: [PATCH 08/11] Database timezone fixed --- init.sql | 3 ++- restservice/src/main/java/com/plannaplan/App.java | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/init.sql b/init.sql index beb60b6..487d135 100755 --- a/init.sql +++ b/init.sql @@ -1 +1,2 @@ -CREATE DATABASE IF NOT EXISTS test; \ No newline at end of file +CREATE DATABASE IF NOT EXISTS test; +SET GLOBAL time_zone = '+1:00'; \ No newline at end of file diff --git a/restservice/src/main/java/com/plannaplan/App.java b/restservice/src/main/java/com/plannaplan/App.java index 478504e..5e54f59 100755 --- a/restservice/src/main/java/com/plannaplan/App.java +++ b/restservice/src/main/java/com/plannaplan/App.java @@ -40,6 +40,10 @@ 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)); From 5a1108e1bf4f2a81b01dacf1990b7bc5204a9b7b Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Fri, 11 Dec 2020 18:08:23 +0100 Subject: [PATCH 09/11] 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); + + } + } From e02523b4f41654aadc37eefda054dff006e1173e Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Sat, 12 Dec 2020 13:33:11 +0100 Subject: [PATCH 10/11] Added current config getter --- .../services/ConfiguratorService.java | 5 +---- .../controllers/ConfigControllerTest.java | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/ConfiguratorService.java b/buisnesslogic/src/main/java/com/plannaplan/services/ConfiguratorService.java index d98a6c5..8f550f4 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/ConfiguratorService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ConfiguratorService.java @@ -5,13 +5,10 @@ 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.*; @@ -34,7 +31,7 @@ public class ConfiguratorService { /** * methoid to config system * - * @param data ConfigData containng system congis + * @param data ConfigData containng system configs */ public void config(ConfigData data) { FileReader reader = new FileReader(data.getFilestream()); diff --git a/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java index 517f674..b6a2e95 100755 --- a/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/ConfigControllerTest.java @@ -2,12 +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.assertFalse; +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; @@ -36,6 +39,8 @@ public class ConfigControllerTest extends AbstractControllerTest { @Autowired private UserService service; + @Autowired + private ConfiguratorService configService; @Test public void shouldReturnNoAuthorized() throws Exception { @@ -78,7 +83,17 @@ public class ConfigControllerTest extends AbstractControllerTest { .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()); - assertFalse(true); + + 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 From 84d7ed8e6556526292183524e5e8eb62ac850345 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Sat, 12 Dec 2020 15:33:00 +0100 Subject: [PATCH 11/11] Added method to getting app states --- .../com/plannaplan/entities/AppConfig.java | 24 +++ .../java/com/plannaplan/types/AppState.java | 5 + .../plannaplan/entities/AppConfigTest.java | 141 ++++++++++++++++++ .../cas/CustomUAMCasValidatorTest.java | 12 +- 4 files changed, 178 insertions(+), 4 deletions(-) create mode 100755 buisnesslogic/src/main/java/com/plannaplan/types/AppState.java create mode 100755 buisnesslogic/src/test/java/com/plannaplan/entities/AppConfigTest.java diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/AppConfig.java b/buisnesslogic/src/main/java/com/plannaplan/entities/AppConfig.java index 49a035f..6487691 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/AppConfig.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/AppConfig.java @@ -9,6 +9,7 @@ import javax.persistence.GenerationType; import javax.persistence.Id; import com.plannaplan.models.TourData; +import com.plannaplan.types.AppState; /** * entity that keeps app configurations @@ -91,4 +92,27 @@ public class AppConfig { 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; + } + } diff --git a/buisnesslogic/src/main/java/com/plannaplan/types/AppState.java b/buisnesslogic/src/main/java/com/plannaplan/types/AppState.java new file mode 100755 index 0000000..3feacef --- /dev/null +++ b/buisnesslogic/src/main/java/com/plannaplan/types/AppState.java @@ -0,0 +1,5 @@ +package com.plannaplan.types; + +public enum AppState { + FIRST_TOUR, SECOND_TOUR, NO_TOUR +} diff --git a/buisnesslogic/src/test/java/com/plannaplan/entities/AppConfigTest.java b/buisnesslogic/src/test/java/com/plannaplan/entities/AppConfigTest.java new file mode 100755 index 0000000..a280c22 --- /dev/null +++ b/buisnesslogic/src/test/java/com/plannaplan/entities/AppConfigTest.java @@ -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); + } + +} diff --git a/restservice/src/test/java/com/plannaplan/security/cas/CustomUAMCasValidatorTest.java b/restservice/src/test/java/com/plannaplan/security/cas/CustomUAMCasValidatorTest.java index cab037e..a4de2b7 100755 --- a/restservice/src/test/java/com/plannaplan/security/cas/CustomUAMCasValidatorTest.java +++ b/restservice/src/test/java/com/plannaplan/security/cas/CustomUAMCasValidatorTest.java @@ -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(); } }