Added required parameters to config

This commit is contained in:
Filip Izydorczyk 2020-12-09 15:02:06 +01:00
parent b1091cd3bc
commit 7c0c22e790
3 changed files with 148 additions and 6 deletions

View File

@ -1,6 +1,6 @@
package com.plannaplan.models; package com.plannaplan.models;
import java.util.Date; import java.sql.Date;
public class TourData { public class TourData {
@ -8,7 +8,7 @@ public class TourData {
private Date end; private Date end;
/** /**
* construcotr * construcotr for java.sql.Date
* *
* @param start Date when tour start * @param start Date when tour start
* @param end Date when tour end * @param end Date when tour end
@ -18,6 +18,17 @@ public class TourData {
this.end = end; 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 * tour end getter
* *

View File

@ -4,12 +4,15 @@ import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.io.IOException; import java.io.IOException;
import java.util.Date;
import com.plannaplan.App; import com.plannaplan.App;
import com.plannaplan.models.ConfigData; import com.plannaplan.models.ConfigData;
import com.plannaplan.models.TourData;
import com.plannaplan.services.ConfiguratorService; import com.plannaplan.services.ConfiguratorService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
@ -33,13 +36,27 @@ public class ConfigController {
@Autowired @Autowired
private ConfiguratorService contrl; private ConfiguratorService contrl;
@PostMapping("/config") @PostMapping(path = "/config", consumes = { "multipart/form-data" })
@PreAuthorize("hasRole('ROLE_ADMIN')") @PreAuthorize("hasRole('ROLE_ADMIN')")
@ApiOperation("Imports data to system. To call you need to provide ADMIN token") @ApiOperation("Imports data to system. To call you need to provide ADMIN token")
public ResponseEntity<String> configApp( 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") 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 { 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); this.contrl.config(data);
return new ResponseEntity<>("Sucess", HttpStatus.OK); return new ResponseEntity<>("Sucess", HttpStatus.OK);
} catch (IOException e) { } catch (IOException e) {

View File

@ -28,6 +28,10 @@ public class ConfigControllerTest extends AbstractControllerTest {
private static final String FILE_NAME = "Zajecia.xlsx"; private static final String FILE_NAME = "Zajecia.xlsx";
private static final String CONFIG_ENDPOINT = "/api/v1/configurator/config"; 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 @Autowired
private UserService service; private UserService service;
@ -52,7 +56,9 @@ public class ConfigControllerTest extends AbstractControllerTest {
final String token = this.service.login(usr).getToken(); final String token = this.service.login(usr).getToken();
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); 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()); .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());
}
} }