From 7c0c22e7905589a90fb34aea018b4fa725dc4e9f Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Wed, 9 Dec 2020 15:02:06 +0100 Subject: [PATCH] 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()); + } + }