Merge remote-tracking branch 'origin/master' into ZPI-163

This commit is contained in:
Marcin Woźniak 2020-11-18 09:05:43 +01:00
commit 9ad8db527d
Signed by: y0rune
GPG Key ID: F204C385F57EB348
39 changed files with 400 additions and 243 deletions

View File

@ -40,6 +40,10 @@ Na koniec w przęglądarce dostaniesz w odpowiedzi token. W samym pliku można z
Żeby zobaczyć dokumentację api trzeba wejść w przeglądarce na `http://localhost:1285/swagger-ui.html` po odpaleniu aplikacji.
### Nazewnictwo odpowiedzi
Każdą odp zaczynamy od modelu, który opisuje np. `Courses` a kończymy na `Response`. Między tymi dwoma członami możemy dodawać modyfikatory opisujące dokładniej odpowiedź np. `Default`. W ten sposób możemy otrzymać nazwę `CoursesDefaultResponse.java`
## Packaging
Zeby spakowac apke do `jara` wystarcza dwie komendy zaczynajac z glownego katalogu projektu

View File

@ -1,6 +1,7 @@
package com.plannaplan.entities;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.concurrent.TimeUnit;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@ -15,6 +16,8 @@ import com.plannaplan.types.UserRoles;
@Entity
public class User {
private static final float TOKEN_EXPIRE_MINUTES = 15;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ -23,7 +26,7 @@ public class User {
private String email;
private UserRoles role;
private String token;
private Date tokenCreatedDate;
private Timestamp tokenUsageDate;
public User() {
}
@ -43,8 +46,8 @@ public class User {
this.email = email;
}
public Date getTokenCreatedDate() {
return tokenCreatedDate;
public Timestamp getTokenUsageDate() {
return tokenUsageDate;
}
public String getToken() {
@ -52,7 +55,7 @@ public class User {
}
public void setToken(String token) {
this.tokenCreatedDate = new Date(System.currentTimeMillis());
this.tokenUsageDate = new Timestamp(System.currentTimeMillis());
this.token = token;
}
@ -83,4 +86,16 @@ public class User {
public Long getId() {
return this.id;
}
public boolean isCredentialsNonExpired() {
final long diffInMilliseconds = Math
.abs(this.tokenUsageDate.getTime() - new Timestamp(System.currentTimeMillis()).getTime());
final long minutes = TimeUnit.MILLISECONDS.toMinutes(diffInMilliseconds);
if (minutes > TOKEN_EXPIRE_MINUTES) {
return false;
}
this.tokenUsageDate = new Timestamp(System.currentTimeMillis());
return true;
}
}

View File

@ -0,0 +1,13 @@
package com.plannaplan.exceptions;
public class TokenExpiredException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
public TokenExpiredException(String message) {
super(message);
}
}

View File

@ -0,0 +1,71 @@
package com.plannaplan.entities;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
public class UserTest {
@Test
public void shouldResturnTokenIsNotExpired() {
final User user = new User();
user.setToken("testowy-bezpieczny-token");
assertTrue(user.isCredentialsNonExpired(), "Credential shouldnt expire yet");
long minutes = this.getMinutesDiff(user.getTokenUsageDate());
assertTrue(minutes == 0, "Should update last token usage");
}
@Test
public void shouldResturnTokenIsNotExpiredWithLessThan15minutes()
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
final User user = new User();
user.setToken("testowy-bezpieczny-token");
this.setLastTokenUsage(user, new Timestamp(System.currentTimeMillis() - (6 * 60 * 1000)));
assertTrue(user.isCredentialsNonExpired(), "Credential shouldnt expire yet");
final long minutes = this.getMinutesDiff(user.getTokenUsageDate());
assertTrue(minutes == 0, "Should update last token usage");
}
@Test
public void shouldResturnTokenIsExpired()
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
final User user = new User();
user.setToken("testowy-bezpieczny-token");
this.setLastTokenUsage(user, new Timestamp(System.currentTimeMillis() - (25 * 60 * 1000)));
assertFalse(user.isCredentialsNonExpired(), "Credential should expire");
final long diffInMilliseconds = Math
.abs(user.getTokenUsageDate().getTime() - new Timestamp(System.currentTimeMillis()).getTime());
final long minutes = TimeUnit.MILLISECONDS.toMinutes(diffInMilliseconds);
assertTrue(minutes > 0, "Shouldn't update last token usage");
}
private long getMinutesDiff(Timestamp timestamp) {
final long diffInMilliseconds = Math
.abs(timestamp.getTime() - new Timestamp(System.currentTimeMillis()).getTime());
final long minutes = TimeUnit.MILLISECONDS.toMinutes(diffInMilliseconds);
return minutes;
}
private void setLastTokenUsage(User user, Timestamp timestampToSet)
throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
Field reader = User.class.getDeclaredField("tokenUsageDate");
reader.setAccessible(true);
reader.set(user, timestampToSet);
}
}

View File

@ -1,12 +1,18 @@
package com.plannaplan;
import java.util.Collections;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.service.Parameter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@ -18,9 +24,15 @@ public class Swagger2Config extends WebMvcConfigurationSupport {
@Bean
public Docket createRestApi() {
Parameter authHeader = new ParameterBuilder()
.parameterType("header")
.name("Authorization")
.modelRef(new ModelRef("string"))
.build();
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.plannaplan.controllers")).paths(PathSelectors.any())
.build();
.apis(RequestHandlerSelectors.basePackage("com.plannaplan")).paths(PathSelectors.any())
.build()
.globalOperationParameters(Collections.singletonList(authHeader));
}
@Override

View File

@ -16,7 +16,7 @@ import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision;
import com.plannaplan.entities.User;
import com.plannaplan.responses.mappers.AssignmentResponseMappers;
import com.plannaplan.responses.models.GetCurrentAssignmentsResponse;
import com.plannaplan.responses.models.AssignmentResponse;
import com.plannaplan.services.AssignmentService;
import com.plannaplan.services.CommisionService;
@ -40,7 +40,7 @@ public class AssignmentsController extends TokenBasedController {
@GetMapping("/user")
@ApiOperation(value = "Return user current assignemts (from newest commision). STUDENT Token needs to be provided.")
public ResponseEntity<List<GetCurrentAssignmentsResponse>> getCurrentAssignments() throws Exception {
public ResponseEntity<List<AssignmentResponse>> getCurrentAssignments() throws Exception {
User user = this.getCurrentUser().orElseThrow(() -> new NullPointerException("User not found"));
Optional<Commision> com = this.commisionService.getNewestCommision(user);

View File

@ -37,7 +37,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
@RestController
@CrossOrigin
@RequestMapping("/api/" + App.API_VERSION + "/commisions")
@Api(tags = { "Commisions" }, value = "Commisions", description = "Commision is representation of student selected assignments at time. All assignments are attached to some commision so we can see current assignments and also browse histroy of changes for given user")
@Api(tags = { "Commisions" }, value = "Commisions", description = "Commision is representation of student selected assignments at time. All assignments are attached to some commision so we can see current assignments and also browse history of changes for given user")
public class CommisionController extends TokenBasedController {
@Autowired

View File

@ -5,8 +5,8 @@ import java.util.List;
import com.plannaplan.App;
import com.plannaplan.entities.Course;
import com.plannaplan.responses.mappers.CoursesResponseMappers;
import com.plannaplan.responses.models.GetCoursesResponse;
import com.plannaplan.responses.models.GetCoursesWithGroupsResponse;
import com.plannaplan.responses.models.CoursesDefaultResponse;
import com.plannaplan.responses.models.CoursesWithGroupsResponse;
import com.plannaplan.services.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
@ -39,11 +39,11 @@ public class CoursesController {
@RequestParam(name = "groups", defaultValue = "false") @ApiParam(value = "Boolean if you want to have resopnse with associated groups or without") Boolean groups) {
List<Course> courses = this.courseService.getAllCourses();
if (groups) {
final List<GetCoursesWithGroupsResponse> response = CoursesResponseMappers
final List<CoursesWithGroupsResponse> response = CoursesResponseMappers
.mapToGetCoursesWithGroupsResponse(courses);
return new ResponseEntity<>(response, HttpStatus.OK);
}
final List<GetCoursesResponse> response = CoursesResponseMappers.mapToGetCoursesResponse(courses);
final List<CoursesDefaultResponse> response = CoursesResponseMappers.mapToGetCoursesResponse(courses);
return new ResponseEntity<>(response, HttpStatus.OK);
}
}

View File

@ -5,8 +5,8 @@ import java.util.List;
import com.plannaplan.App;
import com.plannaplan.entities.Groups;
import com.plannaplan.responses.mappers.GroupsMappers;
import com.plannaplan.responses.models.DefaultGroupResponse;
import com.plannaplan.responses.models.GetCourseGroupsResponse;
import com.plannaplan.responses.models.GroupDefaultResponse;
import com.plannaplan.responses.models.CourseWithGroupsResponse;
import com.plannaplan.services.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
@ -35,7 +35,7 @@ public class GroupController {
@GetMapping("/course/{id}")
@ApiOperation(value = "Return list of lectures and classes (if present) given course")
public ResponseEntity<GetCourseGroupsResponse<? extends DefaultGroupResponse>> getCourses(
public ResponseEntity<CourseWithGroupsResponse<? extends GroupDefaultResponse>> getCourses(
@PathVariable(name = "id") Long id,
@RequestParam(name = "capacity", defaultValue = "true") @ApiParam(value = "Boolean if we want to have capacity field in response") Boolean capacity) {
List<Groups> groups = this.groupService.getGroupsByCourse(id);

View File

@ -5,7 +5,7 @@ import java.util.List;
import com.plannaplan.App;
import com.plannaplan.entities.User;
import com.plannaplan.responses.mappers.UserResponseMappers;
import com.plannaplan.responses.models.SearchForStudentsResponse;
import com.plannaplan.responses.models.UserResponse;
import com.plannaplan.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
@ -36,10 +36,10 @@ public class UsersController {
@GetMapping("/student/search")
@PreAuthorize("hasRole('ROLE_DEANERY')")
@ApiOperation(value = "Serch for user by providing query. If query is empty it will return all students. You need token with DEANERY role to call this")
public ResponseEntity<List<SearchForStudentsResponse>> configApp(
public ResponseEntity<List<UserResponse>> configApp(
@RequestParam("query") @ApiParam(value = "Query to filter all students. If empty will match everyone") String query) {
final List<User> searches = this.userService.searchForStudents(query);
final List<SearchForStudentsResponse> response = UserResponseMappers.mapToDefaultResponse(searches);
final List<UserResponse> response = UserResponseMappers.mapToDefaultResponse(searches);
return new ResponseEntity<>(response, HttpStatus.OK);
}
}

View File

@ -8,13 +8,13 @@ import java.util.Map;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Course;
import com.plannaplan.entities.Groups;
import com.plannaplan.responses.models.GetCurrentAssignmentsResponse;
import com.plannaplan.responses.models.AssignmentResponse;
import com.plannaplan.types.GroupType;
public class AssignmentResponseMappers {
public static final List<GetCurrentAssignmentsResponse> mapToResponse(List<Assignment> assignments) {
List<GetCurrentAssignmentsResponse> response = new ArrayList<>();
public static final List<AssignmentResponse> mapToResponse(List<Assignment> assignments) {
List<AssignmentResponse> response = new ArrayList<>();
HashMap<Course, List<Groups>> courses = new HashMap<>();
assignments.stream().forEach((Assignment assignment) -> {
final Groups group = assignment.getGroup();
@ -29,14 +29,14 @@ public class AssignmentResponseMappers {
final Course course = entry.getKey();
final List<Groups> courseGroups = entry.getValue();
if (courseGroups.size() == 1) {
response.add(new GetCurrentAssignmentsResponse(course, courseGroups.get(0)));
response.add(new AssignmentResponse(course, courseGroups.get(0)));
}
if (courseGroups.size() == 2) {
final Groups lecture = courseGroups.stream().filter(o -> o.getType() == GroupType.LECTURE).findFirst()
.get();
final Groups classes = courseGroups.stream().filter(o -> o.getType() == GroupType.CLASS).findFirst()
.get();
response.add(new GetCurrentAssignmentsResponse(course, lecture, classes));
response.add(new AssignmentResponse(course, lecture, classes));
}
}

View File

@ -5,16 +5,16 @@ import java.util.Objects;
import java.util.stream.Collectors;
import com.plannaplan.entities.Course;
import com.plannaplan.responses.models.GetCoursesResponse;
import com.plannaplan.responses.models.GetCoursesWithGroupsResponse;
import com.plannaplan.responses.models.CoursesDefaultResponse;
import com.plannaplan.responses.models.CoursesWithGroupsResponse;
public class CoursesResponseMappers {
public static final List<GetCoursesResponse> mapToGetCoursesResponse(List<Course> courses) {
return courses.stream().filter(Objects::nonNull).map(GetCoursesResponse::new).collect(Collectors.toList());
public static final List<CoursesDefaultResponse> mapToGetCoursesResponse(List<Course> courses) {
return courses.stream().filter(Objects::nonNull).map(CoursesDefaultResponse::new).collect(Collectors.toList());
}
public static final List<GetCoursesWithGroupsResponse> mapToGetCoursesWithGroupsResponse(List<Course> courses) {
return courses.stream().filter(Objects::nonNull).map(GetCoursesWithGroupsResponse::new)
public static final List<CoursesWithGroupsResponse> mapToGetCoursesWithGroupsResponse(List<Course> courses) {
return courses.stream().filter(Objects::nonNull).map(CoursesWithGroupsResponse::new)
.collect(Collectors.toList());
}
}

View File

@ -6,53 +6,53 @@ import java.util.Objects;
import java.util.stream.Collectors;
import com.plannaplan.entities.Groups;
import com.plannaplan.responses.models.DefaultGroupResponse;
import com.plannaplan.responses.models.GetCourseGroupsResponse;
import com.plannaplan.responses.models.WithCapacityGroupResponse;
import com.plannaplan.responses.models.GroupDefaultResponse;
import com.plannaplan.responses.models.CourseWithGroupsResponse;
import com.plannaplan.responses.models.GroupWithCapacityResponse;
import com.plannaplan.types.GroupType;
public class GroupsMappers {
public static List<DefaultGroupResponse> mapToDefaultResponse(List<Groups> groups) {
return groups.stream().filter(Objects::nonNull).map(DefaultGroupResponse::new).collect(Collectors.toList());
public static List<GroupDefaultResponse> mapToDefaultResponse(List<Groups> groups) {
return groups.stream().filter(Objects::nonNull).map(GroupDefaultResponse::new).collect(Collectors.toList());
}
public static List<WithCapacityGroupResponse> mapToCapacityResponse(List<Groups> groups) {
return groups.stream().filter(Objects::nonNull).map(WithCapacityGroupResponse::new)
public static List<GroupWithCapacityResponse> mapToCapacityResponse(List<Groups> groups) {
return groups.stream().filter(Objects::nonNull).map(GroupWithCapacityResponse::new)
.collect(Collectors.toList());
}
public static GetCourseGroupsResponse<DefaultGroupResponse> mapToGetCourseGroupsDefaultResponse (List<Groups> groups){
public static CourseWithGroupsResponse<GroupDefaultResponse> mapToGetCourseGroupsDefaultResponse (List<Groups> groups){
List<DefaultGroupResponse> lectures = new ArrayList<>();
List<DefaultGroupResponse> classes = new ArrayList<>();
List<GroupDefaultResponse> lectures = new ArrayList<>();
List<GroupDefaultResponse> classes = new ArrayList<>();
groups.stream().forEach(group -> {
if (group.getType() == GroupType.CLASS) {
classes.add(new DefaultGroupResponse(group));
classes.add(new GroupDefaultResponse(group));
} else {
lectures.add(new DefaultGroupResponse(group));
lectures.add(new GroupDefaultResponse(group));
}
});
return new GetCourseGroupsResponse<>(classes, lectures);
return new CourseWithGroupsResponse<>(classes, lectures);
}
public static GetCourseGroupsResponse<WithCapacityGroupResponse> mapToGetCourseGroupsWithCapacityResponse (List<Groups> groups){
public static CourseWithGroupsResponse<GroupWithCapacityResponse> mapToGetCourseGroupsWithCapacityResponse (List<Groups> groups){
List<WithCapacityGroupResponse> lectures = new ArrayList<>();
List<WithCapacityGroupResponse> classes = new ArrayList<>();
List<GroupWithCapacityResponse> lectures = new ArrayList<>();
List<GroupWithCapacityResponse> classes = new ArrayList<>();
groups.stream().forEach(group -> {
if (group.getType() == GroupType.CLASS) {
classes.add(new WithCapacityGroupResponse(group));
classes.add(new GroupWithCapacityResponse(group));
} else {
lectures.add(new WithCapacityGroupResponse(group));
lectures.add(new GroupWithCapacityResponse(group));
}
});
return new GetCourseGroupsResponse<>(classes, lectures);
return new CourseWithGroupsResponse<>(classes, lectures);
}
}

View File

@ -5,11 +5,11 @@ import java.util.Objects;
import java.util.stream.Collectors;
import com.plannaplan.entities.User;
import com.plannaplan.responses.models.SearchForStudentsResponse;
import com.plannaplan.responses.models.UserResponse;
public class UserResponseMappers {
public static List<SearchForStudentsResponse> mapToDefaultResponse(List<User> groups) {
return groups.stream().filter(Objects::nonNull).map(SearchForStudentsResponse::new)
public static List<UserResponse> mapToDefaultResponse(List<User> groups) {
return groups.stream().filter(Objects::nonNull).map(UserResponse::new)
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,44 @@
package com.plannaplan.responses.models;
import com.plannaplan.entities.Course;
import com.plannaplan.entities.Groups;
import com.plannaplan.types.GroupType;
import io.swagger.annotations.ApiModel;
@ApiModel(description = "Response shows information about given assigment to course.", value = "AssignmentResponse")
public class AssignmentResponse {
private Long id;
private String name;
private GroupWithCapacityResponse classes;
private GroupWithCapacityResponse lecture;
public AssignmentResponse(Course course, Groups lecture, Groups classes) {
this.id = course.getId();
this.name = course.getName();
this.lecture = lecture == null ? null : new GroupWithCapacityResponse(lecture);
this.classes = classes == null ? null : new GroupWithCapacityResponse(classes);
}
public AssignmentResponse(Course course, Groups group) {
this(course, group.getType() == GroupType.LECTURE ? group : null,
group.getType() == GroupType.CLASS ? group : null);
}
public GroupWithCapacityResponse getLecture() {
return this.lecture;
}
public GroupWithCapacityResponse getClasses() {
return this.classes;
}
public String getName() {
return this.name;
}
public Long getId() {
return this.id;
}
}

View File

@ -4,8 +4,15 @@ import java.sql.Timestamp;
import com.plannaplan.entities.Commision;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(description = "Response shows information about commision.", value = "CommisionResponse")
public class CommisionResponse {
@ApiModelProperty(value = "ID created by database")
private Long id;
@ApiModelProperty(value = "Timestamp where the user commit the commision")
private Timestamp commisionDate;
public CommisionResponse(Commision commision) {

View File

@ -3,12 +3,15 @@ package com.plannaplan.responses.models;
import java.util.ArrayList;
import java.util.List;
public class GetCourseGroupsResponse <T> {
import io.swagger.annotations.ApiModel;
@ApiModel(description = "Response shows information about groups to given course.", value = "CourseWithGroupsResponse")
public class CourseWithGroupsResponse <T> {
private List<T> lectures = new ArrayList<>();
private List<T> classes = new ArrayList<>();
public GetCourseGroupsResponse(List<T> classes, List<T> lectures ){
public CourseWithGroupsResponse(List<T> classes, List<T> lectures ){
this.lectures = lectures;
this.classes = classes;
}

View File

@ -0,0 +1,15 @@
package com.plannaplan.responses.models;
import com.plannaplan.entities.Course;
import com.plannaplan.responses.models.abstracts.CoursesResponse;
import io.swagger.annotations.ApiModel;
@ApiModel(description = "Response shows information about course.", value = "CoursesDefaultResponse")
public class CoursesDefaultResponse extends CoursesResponse {
public CoursesDefaultResponse(Course course) {
super(course);
}
}

View File

@ -0,0 +1,37 @@
package com.plannaplan.responses.models;
import java.util.ArrayList;
import java.util.List;
import com.plannaplan.entities.Course;
import com.plannaplan.responses.models.abstracts.CoursesResponse;
import com.plannaplan.types.GroupType;
import io.swagger.annotations.ApiModel;
@ApiModel(description = "Response shows information about groups to given course.", value = "CoursesWithGroupsResponse")
public class CoursesWithGroupsResponse extends CoursesResponse {
private List<GroupDefaultResponse> lectures = new ArrayList<>();
private List<GroupDefaultResponse> classes = new ArrayList<>();
public CoursesWithGroupsResponse(Course course) {
super(course);
course.getGroups().stream().forEach(group -> {
if (group.getType() == GroupType.CLASS) {
this.classes.add(new GroupDefaultResponse(group));
} else {
this.lectures.add(new GroupDefaultResponse(group));
}
});
}
public List<GroupDefaultResponse> getClasses() {
return this.classes;
}
public List<GroupDefaultResponse> getLectures() {
return this.lectures;
}
}

View File

@ -1,12 +0,0 @@
package com.plannaplan.responses.models;
import com.plannaplan.entities.Course;
import com.plannaplan.responses.models.abstracts.CoursesResponse;
public class GetCoursesResponse extends CoursesResponse {
public GetCoursesResponse(Course course) {
super(course);
}
}

View File

@ -1,34 +0,0 @@
package com.plannaplan.responses.models;
import java.util.ArrayList;
import java.util.List;
import com.plannaplan.entities.Course;
import com.plannaplan.responses.models.abstracts.CoursesResponse;
import com.plannaplan.types.GroupType;
public class GetCoursesWithGroupsResponse extends CoursesResponse {
private List<DefaultGroupResponse> lectures = new ArrayList<>();
private List<DefaultGroupResponse> classes = new ArrayList<>();
public GetCoursesWithGroupsResponse(Course course) {
super(course);
course.getGroups().stream().forEach(group -> {
if (group.getType() == GroupType.CLASS) {
this.classes.add(new DefaultGroupResponse(group));
} else {
this.lectures.add(new DefaultGroupResponse(group));
}
});
}
public List<DefaultGroupResponse> getClasses() {
return this.classes;
}
public List<DefaultGroupResponse> getLectures() {
return this.lectures;
}
}

View File

@ -1,41 +0,0 @@
package com.plannaplan.responses.models;
import com.plannaplan.entities.Course;
import com.plannaplan.entities.Groups;
import com.plannaplan.types.GroupType;
public class GetCurrentAssignmentsResponse {
private Long id;
private String name;
private WithCapacityGroupResponse classes;
private WithCapacityGroupResponse lecture;
public GetCurrentAssignmentsResponse(Course course, Groups lecture, Groups classes) {
this.id = course.getId();
this.name = course.getName();
this.lecture = lecture == null ? null : new WithCapacityGroupResponse(lecture);
this.classes = classes == null ? null : new WithCapacityGroupResponse(classes);
}
public GetCurrentAssignmentsResponse(Course course, Groups group) {
this(course, group.getType() == GroupType.LECTURE ? group : null,
group.getType() == GroupType.CLASS ? group : null);
}
public WithCapacityGroupResponse getLecture() {
return this.lecture;
}
public WithCapacityGroupResponse getClasses() {
return this.classes;
}
public String getName() {
return this.name;
}
public Long getId() {
return this.id;
}
}

View File

@ -4,16 +4,31 @@ import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Groups;
import com.plannaplan.types.GroupType;
public class DefaultGroupResponse {
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(description = "Response shows information about given group.", value = "GroupDefaultResponse")
public class GroupDefaultResponse {
@ApiModelProperty(value = "ID created by database.")
private Long id;
@ApiModelProperty(value = "Value shows what day when the course is. Example 0 is Monday.")
private int day;
@ApiModelProperty(value = "Value shows time when the course takes.")
private String time;
@ApiModelProperty(value = "Value shows degree, name and surname.")
private String lecturer;
@ApiModelProperty(value = "Value shows room where the class takes.")
private String room;
@ApiModelProperty(value = "Value shows kind of group. The types are LECTURE or CLASS.")
private GroupType type;
public DefaultGroupResponse(Groups group) {
public GroupDefaultResponse(Groups group) {
this.id = group.getId() != null ? group.getId() : null;
this.day = group.getDay() != null ? group.getDay().label : -1;
this.time = group.getTimeString() != null ? group.getTimeString() : "";
@ -22,7 +37,7 @@ public class DefaultGroupResponse {
this.type = group.getType() != null ? group.getType() : null;
}
public DefaultGroupResponse(Assignment assignment) {
public GroupDefaultResponse(Assignment assignment) {
this(assignment.getGroup());
}

View File

@ -0,0 +1,25 @@
package com.plannaplan.responses.models;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Groups;
import io.swagger.annotations.ApiModel;
@ApiModel(description = "Response shows information about group with included capacity.", value = "GroupWithCapacityResponse")
public class GroupWithCapacityResponse extends GroupDefaultResponse {
private int capacity;
public GroupWithCapacityResponse(Groups group) {
super(group);
this.capacity = group.getCapacity();
}
public GroupWithCapacityResponse(Assignment assignment) {
this(assignment.getGroup());
}
public int getCapacity() {
return capacity;
}
}

View File

@ -2,14 +2,17 @@ package com.plannaplan.responses.models;
import com.plannaplan.entities.User;
public class SearchForStudentsResponse {
import io.swagger.annotations.ApiModel;
@ApiModel(description = "Response shows information about user.", value = "UserResponse")
public class UserResponse {
private Long id;
private String name;
private String surname;
private String email;
public SearchForStudentsResponse(User user) {
public UserResponse(User user) {
this.id = user.getId();
this.name = user.getName();
this.surname = user.getSurname();

View File

@ -1,23 +0,0 @@
package com.plannaplan.responses.models;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Groups;
public class WithCapacityGroupResponse extends DefaultGroupResponse {
private int capacity;
public WithCapacityGroupResponse(Groups group) {
super(group);
this.capacity = group.getCapacity();
}
public WithCapacityGroupResponse(Assignment assignment) {
this(assignment.getGroup());
}
public int getCapacity() {
return capacity;
}
}

View File

@ -70,8 +70,11 @@ public class AuthenticationProvider extends AbstractUserDetailsAuthenticationPro
@Override
public boolean isCredentialsNonExpired() {
// is being done in other task
return true;
if (user.isCredentialsNonExpired()) {
userService.save(user);
return true;
}
return false;
}
@Override

View File

@ -9,7 +9,7 @@ import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Course;
import com.plannaplan.entities.Groups;
import com.plannaplan.responses.models.GetCurrentAssignmentsResponse;
import com.plannaplan.responses.models.AssignmentResponse;
import com.plannaplan.entities.Lecturer;
import com.plannaplan.types.WeekDay;
@ -25,9 +25,9 @@ public class AssignmentResponseMappersTest {
new Lecturer("krul.", "Wladyslaw", "Potocki")), com),
new Assignment(new Groups(32, "A4-1", course, 420, WeekDay.MONDAY,
new Lecturer("krul.", "Wladyslaw", "Potocki")), com));
final List<GetCurrentAssignmentsResponse> response = AssignmentResponseMappers.mapToResponse(groups);
final List<AssignmentResponse> response = AssignmentResponseMappers.mapToResponse(groups);
assertTrue(response.size() == 1);
assertTrue(response.get(0) instanceof GetCurrentAssignmentsResponse);
assertTrue(response.get(0) instanceof AssignmentResponse);
}
}

View File

@ -6,8 +6,8 @@ import java.util.Arrays;
import java.util.List;
import com.plannaplan.entities.Course;
import com.plannaplan.responses.models.GetCoursesResponse;
import com.plannaplan.responses.models.GetCoursesWithGroupsResponse;
import com.plannaplan.responses.models.CoursesDefaultResponse;
import com.plannaplan.responses.models.CoursesWithGroupsResponse;
import org.junit.Test;
@ -15,21 +15,21 @@ public class CoursesResponseMappersTest {
@Test
public void shouldMapListCoursesToResponseList() {
final List<Course> courses = Arrays.asList(new Course(), new Course());
final List<GetCoursesResponse> response = CoursesResponseMappers.mapToGetCoursesResponse(courses);
final List<CoursesDefaultResponse> response = CoursesResponseMappers.mapToGetCoursesResponse(courses);
assertTrue(response.size() == 2);
assertTrue(response.get(0) instanceof GetCoursesResponse);
assertTrue(response.get(0) instanceof CoursesDefaultResponse);
}
@Test
public void shouldMapListCoursesToResponseWithGroupsList() {
final List<Course> courses = Arrays.asList(new Course(), new Course());
final List<GetCoursesWithGroupsResponse> response = CoursesResponseMappers
final List<CoursesWithGroupsResponse> response = CoursesResponseMappers
.mapToGetCoursesWithGroupsResponse(courses);
assertTrue(response.size() == 2);
assertTrue(response.get(0) instanceof GetCoursesWithGroupsResponse);
assertTrue(response.get(0) instanceof CoursesWithGroupsResponse);
}
}

View File

@ -7,9 +7,9 @@ import java.util.List;
import com.plannaplan.entities.Groups;
import com.plannaplan.entities.Lecturer;
import com.plannaplan.responses.models.DefaultGroupResponse;
import com.plannaplan.responses.models.GetCourseGroupsResponse;
import com.plannaplan.responses.models.WithCapacityGroupResponse;
import com.plannaplan.responses.models.GroupDefaultResponse;
import com.plannaplan.responses.models.CourseWithGroupsResponse;
import com.plannaplan.responses.models.GroupWithCapacityResponse;
import com.plannaplan.types.WeekDay;
import org.junit.Test;
@ -19,10 +19,10 @@ public class GroupsMappersTest {
public void shouldMapToResponseIncludingCapacity() {
final List<Groups> gropus = Arrays.asList(
new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki")));
final List<WithCapacityGroupResponse> response = GroupsMappers.mapToCapacityResponse(gropus);
final List<GroupWithCapacityResponse> response = GroupsMappers.mapToCapacityResponse(gropus);
assert (response.get(0).getCapacity() == 42);
assert (response.get(0) instanceof WithCapacityGroupResponse);
assert (response.get(0) instanceof GroupWithCapacityResponse);
assert (response.size() == 1);
}
@ -30,9 +30,9 @@ public class GroupsMappersTest {
public void shouldMapToResponseWiothoutCapacity() {
final List<Groups> gropus = Arrays.asList(
new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki")));
final List<DefaultGroupResponse> response = GroupsMappers.mapToDefaultResponse(gropus);
final List<GroupDefaultResponse> response = GroupsMappers.mapToDefaultResponse(gropus);
assert (response.get(0) instanceof DefaultGroupResponse);
assert (response.get(0) instanceof GroupDefaultResponse);
assert (response.size() == 1);
}
@ -41,7 +41,7 @@ public class GroupsMappersTest {
final List<Groups> groups = Arrays.asList(
new Groups(150, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki")), new Groups(24, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki")));
final GetCourseGroupsResponse<DefaultGroupResponse> response = GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups);
final CourseWithGroupsResponse<GroupDefaultResponse> response = GroupsMappers.mapToGetCourseGroupsDefaultResponse(groups);
assertTrue(response.getClasses().size() == 1);
assertTrue(response.getLectures().size() == 1);

View File

@ -7,7 +7,7 @@ import java.util.List;
import com.plannaplan.entities.User;
import com.plannaplan.types.UserRoles;
import com.plannaplan.responses.models.SearchForStudentsResponse;
import com.plannaplan.responses.models.UserResponse;
import org.junit.Test;
@ -25,8 +25,8 @@ public class UserResponseMappersTest {
final List<User> users = Arrays.asList(new User(F_NAME, F_SURNAMENAME, F_EMAIL, UserRoles.TEST_USER),
new User(S_NAME, S_SURNAMENAME, S_EMAIL, UserRoles.TEST_USER));
final List<SearchForStudentsResponse> resposne = UserResponseMappers.mapToDefaultResponse(users);
final List<UserResponse> resposne = UserResponseMappers.mapToDefaultResponse(users);
assertTrue(resposne.size() == 2);
assertTrue(resposne.get(0) instanceof SearchForStudentsResponse);
assertTrue(resposne.get(0) instanceof UserResponse);
}
}

View File

@ -9,7 +9,7 @@ import com.plannaplan.types.WeekDay;
import org.junit.Test;
public class GetCurrentAssignmentsResponseTest {
public class AssignmentResponseTest {
@Test
public void shouldMapGroupsToResponse() {
@ -19,10 +19,10 @@ public class GetCurrentAssignmentsResponseTest {
final Groups classes = new Groups(22, "A4-2", null, 580, WeekDay.MONDAY,
new Lecturer("krul.", "Wladyslaw", "Potocki"));
final GetCurrentAssignmentsResponse response = new GetCurrentAssignmentsResponse(course, lecture, classes);
final AssignmentResponse response = new AssignmentResponse(course, lecture, classes);
assertTrue(response.getName() == "Kurs");
assertTrue(response.getLecture() instanceof WithCapacityGroupResponse);
assertTrue(response.getClasses() instanceof WithCapacityGroupResponse);
assertTrue(response.getLecture() instanceof GroupWithCapacityResponse);
assertTrue(response.getClasses() instanceof GroupWithCapacityResponse);
}
@Test
@ -31,9 +31,9 @@ public class GetCurrentAssignmentsResponseTest {
final Groups lecture = new Groups(142, "A4-1", null, 520, WeekDay.MONDAY,
new Lecturer("krul.", "Wladyslaw", "Potocki"));
final GetCurrentAssignmentsResponse response = new GetCurrentAssignmentsResponse(course, lecture);
final AssignmentResponse response = new AssignmentResponse(course, lecture);
assertTrue(response.getName() == "Kurs");
assertTrue(response.getLecture() instanceof WithCapacityGroupResponse);
assertTrue(response.getLecture() instanceof GroupWithCapacityResponse);
assertTrue(response.getClasses() == null);
}
@ -43,10 +43,10 @@ public class GetCurrentAssignmentsResponseTest {
final Groups classes = new Groups(22, "A4-2", null, 580, WeekDay.MONDAY,
new Lecturer("krul.", "Wladyslaw", "Potocki"));
final GetCurrentAssignmentsResponse response = new GetCurrentAssignmentsResponse(course, classes);
final AssignmentResponse response = new AssignmentResponse(course, classes);
assertTrue(response.getName() == "Kurs");
assertTrue(response.getLecture() == null);
assertTrue(response.getClasses() instanceof WithCapacityGroupResponse);
assertTrue(response.getClasses() instanceof GroupWithCapacityResponse);
}
}

View File

@ -0,0 +1,28 @@
package com.plannaplan.responses.models;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.List;
import com.plannaplan.entities.Groups;
import org.junit.Test;
public class CourseWithGroupsResponseTest {
@Test public void shouldCreateInstance(){
final List<GroupDefaultResponse> classes = Arrays.asList(new GroupDefaultResponse(new Groups()));
final List<GroupDefaultResponse> lectures = Arrays.asList(new GroupDefaultResponse(new Groups()));
final CourseWithGroupsResponse<GroupDefaultResponse> instance = new CourseWithGroupsResponse<>(classes,lectures);
assertTrue(instance.getClasses().get(0) instanceof GroupDefaultResponse);
assertTrue(instance.getLectures().get(0) instanceof GroupDefaultResponse);
}
}

View File

@ -6,11 +6,11 @@ import com.plannaplan.entities.Course;
import org.junit.Test;
public class GetCoursesResponseTest {
public class CoursesDefaultResponseTest {
@Test
public void shouldMapFilledCourse() {
final Course course = new Course("Test", "XD");
final GetCoursesResponse response = new GetCoursesResponse(course);
final CoursesDefaultResponse response = new CoursesDefaultResponse(course);
assertTrue(response.getId() == null);
assertTrue(response.getName().equals("Test"));
@ -19,7 +19,7 @@ public class GetCoursesResponseTest {
@Test
public void shouldMapEmptyCourse() {
final Course course = new Course();
final GetCoursesResponse response = new GetCoursesResponse(course);
final CoursesDefaultResponse response = new CoursesDefaultResponse(course);
assertTrue(response.getId() == null);
assertTrue(response.getName().equals(""));

View File

@ -6,11 +6,11 @@ import com.plannaplan.entities.Course;
import org.junit.Test;
public class GetCoursesWithGroupsResponseTest {
public class CoursesWithGroupsResponseTest {
@Test
public void shouldMapCourseToResponseWithGroupsIncluded() {
final GetCoursesWithGroupsResponse response = new GetCoursesWithGroupsResponse(
final CoursesWithGroupsResponse response = new CoursesWithGroupsResponse(
new Course("Programowanie funkcyjne", "xd"));
assertTrue(response.getName() == "Programowanie funkcyjne");

View File

@ -1,28 +0,0 @@
package com.plannaplan.responses.models;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.List;
import com.plannaplan.entities.Groups;
import org.junit.Test;
public class GetCourseGroupsResponseTest {
@Test public void shouldCreateInstance(){
final List<DefaultGroupResponse> classes = Arrays.asList(new DefaultGroupResponse(new Groups()));
final List<DefaultGroupResponse> lectures = Arrays.asList(new DefaultGroupResponse(new Groups()));
final GetCourseGroupsResponse<DefaultGroupResponse> instance = new GetCourseGroupsResponse<>(classes,lectures);
assertTrue(instance.getClasses().get(0) instanceof DefaultGroupResponse);
assertTrue(instance.getLectures().get(0) instanceof DefaultGroupResponse);
}
}

View File

@ -10,13 +10,13 @@ import com.plannaplan.types.WeekDay;
import org.junit.Test;
public class DefaultGroupResponseTest {
public class GroupDefaultResponseTest {
@Test
public void shouldMapAssignmentClassToResponse() {
final Assignment assignment = new Assignment(
new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki")), null);
final DefaultGroupResponse response = new DefaultGroupResponse(assignment);
final GroupDefaultResponse response = new GroupDefaultResponse(assignment);
assertTrue(response.getDay() == 0);
assertTrue(response.getLecturer().equals("krul. Wladyslaw Potocki"));
assertTrue(response.getRoom().equals("A4-1"));
@ -29,7 +29,7 @@ public class DefaultGroupResponseTest {
final Groups group = new Groups(42, "A4-1", null, 520, WeekDay.MONDAY,
new Lecturer("krul.", "Wladyslaw", "Potocki"));
final DefaultGroupResponse response = new DefaultGroupResponse(group);
final GroupDefaultResponse response = new GroupDefaultResponse(group);
assertTrue(response.getDay() == 0);
assertTrue(response.getLecturer().equals("krul. Wladyslaw Potocki"));
assertTrue(response.getRoom().equals("A4-1"));
@ -41,7 +41,7 @@ public class DefaultGroupResponseTest {
public void shouldMapEmptyGroupClassToResponse() {
final Groups group = new Groups();
final DefaultGroupResponse response = new DefaultGroupResponse(group);
final GroupDefaultResponse response = new GroupDefaultResponse(group);
assertTrue(response.getDay() == -1);
assertTrue(response.getLecturer().equals(""));
assertTrue(response.getRoom().equals(""));

View File

@ -10,13 +10,13 @@ import com.plannaplan.types.WeekDay;
import org.junit.Test;
public class WithCapacityGroupResponseTest {
public class GroupWithCapacityResponseTest {
@Test
public void shouldMapAssignmentClassToResponse() {
final Assignment assignment = new Assignment(
new Groups(42, "A4-1", null, 520, WeekDay.MONDAY, new Lecturer("krul.", "Wladyslaw", "Potocki")), null);
final WithCapacityGroupResponse response = new WithCapacityGroupResponse(assignment);
final GroupWithCapacityResponse response = new GroupWithCapacityResponse(assignment);
assertTrue(response.getCapacity() == 42);
assertTrue(response.getDay() == 0);
assertTrue(response.getLecturer().equals("krul. Wladyslaw Potocki"));
@ -30,7 +30,7 @@ public class WithCapacityGroupResponseTest {
final Groups group = new Groups(42, "A4-1", null, 520, WeekDay.MONDAY,
new Lecturer("krul.", "Wladyslaw", "Potocki"));
final WithCapacityGroupResponse response = new WithCapacityGroupResponse(group);
final GroupWithCapacityResponse response = new GroupWithCapacityResponse(group);
assertTrue(response.getCapacity() == 42);
assertTrue(response.getDay() == 0);
assertTrue(response.getLecturer().equals("krul. Wladyslaw Potocki"));
@ -43,7 +43,7 @@ public class WithCapacityGroupResponseTest {
public void shouldMapEmptyGroupClassToResponse() {
final Groups group = new Groups();
final WithCapacityGroupResponse response = new WithCapacityGroupResponse(group);
final GroupWithCapacityResponse response = new GroupWithCapacityResponse(group);
assertTrue(response.getCapacity() == 0);
assertTrue(response.getDay() == -1);
assertTrue(response.getLecturer().equals(""));

View File

@ -7,14 +7,14 @@ import com.plannaplan.types.UserRoles;
import org.junit.Test;
public class SearchForStudentsResponseTest {
public class UserResponseTest {
private static final String NAME = "Tytus";
private static final String SURNAMENAME = "Romek";
private static final String EMAIL = "i@Atomek.pl";
@Test
public void shouldMapUserToResponseElement() {
final SearchForStudentsResponse response = new SearchForStudentsResponse(
final UserResponse response = new UserResponse(
new User(NAME, SURNAMENAME, EMAIL, UserRoles.TEST_USER));
assertTrue(response.getName().equals(NAME));
assertTrue(response.getSurname().equals(SURNAMENAME));