From 6e65c26e3cfb171b774ea12fe51b91e116b6d4b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Wed, 6 Jan 2021 13:40:04 +0100 Subject: [PATCH 01/30] Checkpoint: szkielet MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../com/plannaplan/entities/Exchange.java | 57 +++++++++++++++++++ .../repositories/ExchangeRepository.java | 11 ++++ .../plannaplan/services/ExchangeService.java | 18 ++++++ .../controllers/ExchangeController.java | 27 +++++++++ 4 files changed, 113 insertions(+) create mode 100644 buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java create mode 100644 buisnesslogic/src/main/java/com/plannaplan/repositories/ExchangeRepository.java create mode 100644 buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java create mode 100644 restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java new file mode 100644 index 0000000..9f8293d --- /dev/null +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java @@ -0,0 +1,57 @@ +package com.plannaplan.entities; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Exchange { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + private User trader; + private Assignment ownedAssignment; + private Groups desiredAssignment; + + public Exchange() { + + } + + public Exchange(User trader, Assignment ownedAssignment, Groups desiredAssignment){ + this.trader = trader; + this.ownedAssignment = ownedAssignment; + this.desiredAssignment = desiredAssignment; + } + + public Long getId(){ + return this.id; + } + + public Groups getDesiredAssignment() { + return desiredAssignment; + } + + public void setDesiredAssignment(Groups desiredAssignment) { + this.desiredAssignment = desiredAssignment; + } + + public Assignment getOwnedAssignment() { + return ownedAssignment; + } + + public void setOwnedAssignment(Assignment ownedAssignment) { + this.ownedAssignment = ownedAssignment; + } + + public User getTrader() { + return trader; + } + + public void setTrader(User trader) { + this.trader = trader; + } + +} diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/ExchangeRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/ExchangeRepository.java new file mode 100644 index 0000000..5c6be33 --- /dev/null +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/ExchangeRepository.java @@ -0,0 +1,11 @@ +package com.plannaplan.repositories; + +import com.plannaplan.entities.Exchange; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ExchangeRepository extends JpaRepository{ + +} diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java new file mode 100644 index 0000000..59d8002 --- /dev/null +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java @@ -0,0 +1,18 @@ +package com.plannaplan.services; + +import com.plannaplan.entities.Exchange; +import com.plannaplan.repositories.ExchangeRepository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class ExchangeService { + + @Autowired + private ExchangeRepository repo; + + public Exchange save(Exchange exchange){ + return this.repo.save(exchange); + } +} diff --git a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java new file mode 100644 index 0000000..79d67e8 --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java @@ -0,0 +1,27 @@ +package com.plannaplan.controllers; + +import java.util.Map; + +import com.plannaplan.App; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; + +@RestController +@CrossOrigin +@RequestMapping("/api/" + App.API_VERSION + "/exchanges") +@Api(tags = { "Exchange" }, value = "Exchange", description = "Endpoint to exchange with accepted assignments.") +public class ExchangeController { + + @PostMapping("/exchange") + @ApiOperation(value = "Return all courses") + public ResponseEntity createExchange(@RequestBody Map exchangeRequest){ + return null; + } +} From 3bcc35e75b543bc20a07387d53d6100184b3ae94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Wed, 6 Jan 2021 14:34:25 +0100 Subject: [PATCH 02/30] Checkpoint: dziala sprawdzanie values in the exchanges/exchange MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../com/plannaplan/entities/Exchange.java | 10 +++++++++ .../controllers/ExchangeController.java | 21 +++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java index 9f8293d..37b7111 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java @@ -4,6 +4,8 @@ import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToOne; @Entity public class Exchange { @@ -12,8 +14,16 @@ public class Exchange { @GeneratedValue(strategy = GenerationType.AUTO) private Long id; + @OneToOne + @JoinColumn(name = "trader_id") private User trader; + + @OneToOne + @JoinColumn(name = "owned_id") private Assignment ownedAssignment; + + @OneToOne + @JoinColumn(name = "desired_id") private Groups desiredAssignment; public Exchange() { diff --git a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java index 79d67e8..5341dcb 100644 --- a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java @@ -1,8 +1,15 @@ package com.plannaplan.controllers; import java.util.Map; +import java.util.Optional; import com.plannaplan.App; +import com.plannaplan.entities.Assignment; +import com.plannaplan.entities.Groups; +import com.plannaplan.entities.User; +import com.plannaplan.repositories.ExchangeRepository; + +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PostMapping; @@ -18,10 +25,20 @@ import io.swagger.annotations.ApiOperation; @RequestMapping("/api/" + App.API_VERSION + "/exchanges") @Api(tags = { "Exchange" }, value = "Exchange", description = "Endpoint to exchange with accepted assignments.") public class ExchangeController { - + @PostMapping("/exchange") @ApiOperation(value = "Return all courses") - public ResponseEntity createExchange(@RequestBody Map exchangeRequest){ + public ResponseEntity createExchange(@RequestBody Map exchangeRequest){ + Long user = exchangeRequest.get("user"); + Long assignment = exchangeRequest.get("assignment"); + Long group = exchangeRequest.get("group"); + + if(user == null || assignment == null|| group == null ){ + return new ResponseEntity<>("Some of provided value missing", HttpStatus.BAD_REQUEST); + } + + //System.out.println("KUPA"); + return null; } } From f24e62a3358b2f37898b79203e1dba6834fcdab4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Wed, 6 Jan 2021 14:50:34 +0100 Subject: [PATCH 03/30] Checkpoint: works first try with endpoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../controllers/ExchangeController.java | 39 ++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java index 5341dcb..5c775cc 100644 --- a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java @@ -5,10 +5,16 @@ import java.util.Optional; import com.plannaplan.App; import com.plannaplan.entities.Assignment; +import com.plannaplan.entities.Exchange; import com.plannaplan.entities.Groups; import com.plannaplan.entities.User; import com.plannaplan.repositories.ExchangeRepository; +import com.plannaplan.services.AssignmentService; +import com.plannaplan.services.ExchangeService; +import com.plannaplan.services.GroupService; +import com.plannaplan.services.UserService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; @@ -25,18 +31,41 @@ import io.swagger.annotations.ApiOperation; @RequestMapping("/api/" + App.API_VERSION + "/exchanges") @Api(tags = { "Exchange" }, value = "Exchange", description = "Endpoint to exchange with accepted assignments.") public class ExchangeController { + + @Autowired + private UserService userService; + + @Autowired + private GroupService groupService; + + @Autowired + private AssignmentService assignmentService; + @Autowired + private ExchangeService exchangeService; + @PostMapping("/exchange") @ApiOperation(value = "Return all courses") public ResponseEntity createExchange(@RequestBody Map exchangeRequest){ - Long user = exchangeRequest.get("user"); - Long assignment = exchangeRequest.get("assignment"); - Long group = exchangeRequest.get("group"); - if(user == null || assignment == null|| group == null ){ - return new ResponseEntity<>("Some of provided value missing", HttpStatus.BAD_REQUEST); + final Long userId = exchangeRequest.get("user"); + final Long assignmentId = exchangeRequest.get("assignment"); + final Long groupId = exchangeRequest.get("group"); + + if(userId == null || assignmentId == null|| groupId == null ){ + return new ResponseEntity<>("Some of values are missing", HttpStatus.BAD_REQUEST); } + final Optional user = this.userService.getById(userId); + final Optional assignment = this.assignmentService.getById(assignmentId); + final Optional group = this.groupService.getGroupById(groupId); + + if(user.isEmpty() || assignment.isEmpty() || group.isEmpty()){ + return new ResponseEntity<>("Some of provided value does not exist.", HttpStatus.BAD_REQUEST); + } + + this.exchangeService.save(new Exchange(user.get(), assignment.get(), group.get())); + //System.out.println("KUPA"); return null; From df7701ebc80a68c957fb29d8d31cb4c7e9b217d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Wed, 6 Jan 2021 16:23:18 +0100 Subject: [PATCH 04/30] Checkpoints: Part 1 of Added docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../com/plannaplan/entities/Assignment.java | 8 ++++ .../com/plannaplan/entities/Exchange.java | 41 +++++++++++-------- .../plannaplan/services/ExchangeService.java | 4 ++ .../controllers/ExchangeController.java | 33 +++++++++------ 4 files changed, 57 insertions(+), 29 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java index 2f0722b..429d482 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java @@ -47,6 +47,14 @@ public class Assignment { return this.group.getRegisteredStudents().contains(this.commision.getCommisionOwner()); } + /** + * Getter of commision + * @return Commision Commision of given assignments + */ + public Commision getCommision(){ + return this.commision; + } + /** * Assignment * diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java index 37b7111..785c681 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java @@ -7,6 +7,9 @@ import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; +/** + * Entity that keeps user exchange offer. + */ @Entity public class Exchange { @@ -14,10 +17,6 @@ public class Exchange { @GeneratedValue(strategy = GenerationType.AUTO) private Long id; - @OneToOne - @JoinColumn(name = "trader_id") - private User trader; - @OneToOne @JoinColumn(name = "owned_id") private Assignment ownedAssignment; @@ -30,38 +29,48 @@ public class Exchange { } - public Exchange(User trader, Assignment ownedAssignment, Groups desiredAssignment){ - this.trader = trader; + public Exchange(Assignment ownedAssignment, Groups desiredAssignment){ this.ownedAssignment = ownedAssignment; this.desiredAssignment = desiredAssignment; } + + /** + * @return Long ID in database + */ public Long getId(){ return this.id; } + + /** + * @return Groups Target group + */ public Groups getDesiredAssignment() { return desiredAssignment; } + + /** + * @param desiredAssignment Target group + */ public void setDesiredAssignment(Groups desiredAssignment) { this.desiredAssignment = desiredAssignment; } + + /** + * @return Assignment Owned assignment + */ public Assignment getOwnedAssignment() { return ownedAssignment; } + + /** + * @param ownedAssignment Owned assignment + */ public void setOwnedAssignment(Assignment ownedAssignment) { this.ownedAssignment = ownedAssignment; } - - public User getTrader() { - return trader; - } - - public void setTrader(User trader) { - this.trader = trader; - } - -} +} \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java index 59d8002..8cd7bb0 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java @@ -12,6 +12,10 @@ public class ExchangeService { @Autowired private ExchangeRepository repo; + /** + * @param exchange Instance to save in database + * @return Exchange Instance contains database id + */ public Exchange save(Exchange exchange){ return this.repo.save(exchange); } diff --git a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java index 5c775cc..719f33c 100644 --- a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java @@ -8,11 +8,10 @@ import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Exchange; import com.plannaplan.entities.Groups; import com.plannaplan.entities.User; -import com.plannaplan.repositories.ExchangeRepository; +import com.plannaplan.exceptions.UserNotFoundException; import com.plannaplan.services.AssignmentService; import com.plannaplan.services.ExchangeService; import com.plannaplan.services.GroupService; -import com.plannaplan.services.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -30,10 +29,13 @@ import io.swagger.annotations.ApiOperation; @CrossOrigin @RequestMapping("/api/" + App.API_VERSION + "/exchanges") @Api(tags = { "Exchange" }, value = "Exchange", description = "Endpoint to exchange with accepted assignments.") -public class ExchangeController { +public class ExchangeController extends TokenBasedController{ - @Autowired - private UserService userService; + + /* + * Endpoint do wylistowania exchangow użytkownika + * Endpoint do usuwania exchanga + */ @Autowired private GroupService groupService; @@ -46,28 +48,33 @@ public class ExchangeController { @PostMapping("/exchange") @ApiOperation(value = "Return all courses") - public ResponseEntity createExchange(@RequestBody Map exchangeRequest){ + public ResponseEntity createExchange(@RequestBody Map exchangeRequest) + throws UserNotFoundException { - final Long userId = exchangeRequest.get("user"); + final User asker = this.getCurrentUser() + .orElseThrow(() -> new UserNotFoundException("Invalid token")); final Long assignmentId = exchangeRequest.get("assignment"); final Long groupId = exchangeRequest.get("group"); - if(userId == null || assignmentId == null|| groupId == null ){ + if(assignmentId == null || groupId == null ){ return new ResponseEntity<>("Some of values are missing", HttpStatus.BAD_REQUEST); } - final Optional user = this.userService.getById(userId); final Optional assignment = this.assignmentService.getById(assignmentId); final Optional group = this.groupService.getGroupById(groupId); - if(user.isEmpty() || assignment.isEmpty() || group.isEmpty()){ + if(assignment.isEmpty() || group.isEmpty()){ return new ResponseEntity<>("Some of provided value does not exist.", HttpStatus.BAD_REQUEST); } - this.exchangeService.save(new Exchange(user.get(), assignment.get(), group.get())); + final Assignment assignmentInstance = assignment.get(); - //System.out.println("KUPA"); + if(!(assignmentInstance.getCommision().getCommisionOwner().getId() == asker.getId() && assignmentInstance.isAccepted())){ + return new ResponseEntity<>("Some of problems appeared. Check if you have access to given assignment and if it is accepted", HttpStatus.BAD_REQUEST); + } - return null; + this.exchangeService.save(new Exchange(assignment.get(), group.get())); + + return new ResponseEntity<>("Success", HttpStatus.OK); } } From b633d2c2dfe740c666aa0961979dfe460b92baf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Wed, 6 Jan 2021 16:50:08 +0100 Subject: [PATCH 05/30] Checkpoints: WORKS but not for all MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../plannaplan/repositories/ExchangeRepository.java | 9 +++++++++ .../java/com/plannaplan/services/ExchangeService.java | 8 ++++++++ .../com/plannaplan/controllers/ExchangeController.java | 10 +++++++--- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/ExchangeRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/ExchangeRepository.java index 5c6be33..796625a 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/repositories/ExchangeRepository.java +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/ExchangeRepository.java @@ -1,11 +1,20 @@ package com.plannaplan.repositories; +import java.util.Optional; + +import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Exchange; +import com.plannaplan.entities.Groups; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @Repository public interface ExchangeRepository extends JpaRepository{ + + @Query("FROM Exchange WHERE owned_id = ?1 AND desired_id = ?2") + Optional checkForExchange(@Param("owned_id") Assignment assignment, @Param("desired_id") Groups group); } diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java index 8cd7bb0..a2f6556 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java @@ -1,6 +1,10 @@ package com.plannaplan.services; +import java.util.Optional; + +import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Exchange; +import com.plannaplan.entities.Groups; import com.plannaplan.repositories.ExchangeRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -19,4 +23,8 @@ public class ExchangeService { public Exchange save(Exchange exchange){ return this.repo.save(exchange); } + + public Optional checkForExchange(Assignment assignment, Groups group){ + return this.repo.checkForExchange(assignment, group); + } } diff --git a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java index 719f33c..e9a032c 100644 --- a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java @@ -68,13 +68,17 @@ public class ExchangeController extends TokenBasedController{ } final Assignment assignmentInstance = assignment.get(); + final Groups groupInstance = group.get(); + final Optional exchangeInstance = this.exchangeService.checkForExchange(assignmentInstance, groupInstance); - if(!(assignmentInstance.getCommision().getCommisionOwner().getId() == asker.getId() && assignmentInstance.isAccepted())){ - return new ResponseEntity<>("Some of problems appeared. Check if you have access to given assignment and if it is accepted", HttpStatus.BAD_REQUEST); + if(!(assignmentInstance.getCommision().getCommisionOwner().getId() == asker.getId() && assignmentInstance.isAccepted() && exchangeInstance.isEmpty())){ + return new ResponseEntity<>("Some of problems appeared. Check if you have access to given assignment and if it is accepted or the exchange has not been already added.", HttpStatus.BAD_REQUEST); } - this.exchangeService.save(new Exchange(assignment.get(), group.get())); + this.exchangeService.save(new Exchange(assignmentInstance, groupInstance)); return new ResponseEntity<>("Success", HttpStatus.OK); } + + } From 90f154f23bbf80dafc85ff4702cd2fa68d9adcb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Wed, 6 Jan 2021 17:12:20 +0100 Subject: [PATCH 06/30] Checkpoint: Added docs, improved ExchangeEntity --- .../src/main/java/com/plannaplan/entities/Exchange.java | 2 +- .../main/java/com/plannaplan/services/ExchangeService.java | 6 ++++++ .../java/com/plannaplan/controllers/ExchangeController.java | 3 +-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java index 785c681..c0830c4 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java @@ -18,7 +18,7 @@ public class Exchange { private Long id; @OneToOne - @JoinColumn(name = "owned_id") + @JoinColumn(name = "owned_id", unique = true) private Assignment ownedAssignment; @OneToOne diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java index a2f6556..cd31023 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java @@ -24,6 +24,12 @@ public class ExchangeService { return this.repo.save(exchange); } + + /** + * @param assignment Assignment to trade for + * @param group Desired group + * @return Optional with Exchange if exist + */ public Optional checkForExchange(Assignment assignment, Groups group){ return this.repo.checkForExchange(assignment, group); } diff --git a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java index e9a032c..b9c4869 100644 --- a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java @@ -69,9 +69,8 @@ public class ExchangeController extends TokenBasedController{ final Assignment assignmentInstance = assignment.get(); final Groups groupInstance = group.get(); - final Optional exchangeInstance = this.exchangeService.checkForExchange(assignmentInstance, groupInstance); - if(!(assignmentInstance.getCommision().getCommisionOwner().getId() == asker.getId() && assignmentInstance.isAccepted() && exchangeInstance.isEmpty())){ + if(!(assignmentInstance.getCommision().getCommisionOwner().getId().equals(asker.getId()) && assignmentInstance.isAccepted())){ return new ResponseEntity<>("Some of problems appeared. Check if you have access to given assignment and if it is accepted or the exchange has not been already added.", HttpStatus.BAD_REQUEST); } From ebacbe892dde352914404bd1709689104d9e11f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Wed, 6 Jan 2021 17:20:49 +0100 Subject: [PATCH 07/30] gettoken.py WORKING IN MY GENTOO MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- gettoken.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/gettoken.py b/gettoken.py index 462e131..6bb1b14 100755 --- a/gettoken.py +++ b/gettoken.py @@ -6,8 +6,12 @@ from urllib.parse import parse_qs import sys import requests as r +no_browser = False + if len(sys.argv) > 1 : API_ADDRESS = "http://" + sys.argv[1] + ":1285" + if (sys.argv[1] == "no-browser" or sys.argv[2] == "no-browser"): + no_browser = True else: API_ADDRESS = "http://localhost:1285" @@ -39,5 +43,6 @@ def wait_for_request(server_class=HTTPServer, url = 'https://cas.amu.edu.pl/cas/login?service=http://localhost:' + \ str(PORT) + '&locale=pl' -webbrowser.open_new_tab(url) +if no_browser == False: + webbrowser.open_new_tab(url) wait_for_request() From c8c8b62263934ca1666606544b54805c5fa77377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Wed, 6 Jan 2021 17:29:18 +0100 Subject: [PATCH 08/30] gettoken.py, README.md - update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- README.md | 14 +++++++++++++- gettoken.py | 8 ++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 2eae90f..17fd8e2 100755 --- a/README.md +++ b/README.md @@ -17,10 +17,22 @@ mvn spring-boot:run Żeby tesotwać API wpełni potrzebny nam jest token który otrzymujemy na podstawie ticketa z systemu autoryzacyjnego **CAS**. Z tego powodu system autoryzacji działa inaczej niż w "książkowych" restowych aplikacjach i np Postman za nas jej nie dokona. Musimy mu podać już uzyskany token. Aby łatwo go uzyskać odpal skrypt -``` +```bash python gettoken.py ``` +Jeżeli chcesz używać skryptu zmiejaniąc gdzie znajduje się backend wpisz: + +```bash +python gettoken.py 192.168.0.212 +``` + +Jeżeli chcesz używać skryptu bez uruchamiania przeglądarki wpisz: + +```bash +python gettoken.py no-browser +``` + Na koniec w przęglądarce dostaniesz w odpowiedzi token. W samym pliku można zmienić porty aplikacji jeśli to potrzebne. # Profiles diff --git a/gettoken.py b/gettoken.py index 6bb1b14..5971656 100755 --- a/gettoken.py +++ b/gettoken.py @@ -7,13 +7,13 @@ import sys import requests as r no_browser = False +API_ADDRESS = "http://localhost:1285" -if len(sys.argv) > 1 : - API_ADDRESS = "http://" + sys.argv[1] + ":1285" +if len(sys.argv) > 1 : if (sys.argv[1] == "no-browser" or sys.argv[2] == "no-browser"): no_browser = True -else: - API_ADDRESS = "http://localhost:1285" + else: + API_ADDRESS = "http://" + sys.argv[1] + ":1285" PORT = 3000 From 56120c4724a97135e3a16f6049240627697ea468 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Thu, 7 Jan 2021 16:01:25 +0100 Subject: [PATCH 09/30] Checkpoint: deleting is working MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../plannaplan/services/ExchangeService.java | 16 ++++++++++++- .../controllers/ExchangeController.java | 24 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java index cd31023..7a78e5a 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java @@ -23,8 +23,22 @@ public class ExchangeService { public Exchange save(Exchange exchange){ return this.repo.save(exchange); } - + + /** + * @param id Id of exchange in database + * @return Optional Exchange if found + */ + public Optional getById(Long id){ + return this.repo.findById(id); + } + /** + * @param entity Exchange entity which we would like to delete + */ + public void deleteExchange(Exchange entity){ + this.repo.delete(entity); + } + /** * @param assignment Assignment to trade for * @param group Desired group diff --git a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java index b9c4869..e255fe0 100644 --- a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java @@ -18,6 +18,8 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -79,5 +81,27 @@ public class ExchangeController extends TokenBasedController{ return new ResponseEntity<>("Success", HttpStatus.OK); } + @DeleteMapping("/exchange/{id}") + @ApiOperation(value = "Delete exchange offer") + public ResponseEntity deleteExchange(@PathVariable(name = "id", required = false) Long offerId) + throws UserNotFoundException { + final User asker = this.getCurrentUser() + .orElseThrow(() -> new UserNotFoundException("Invalid token")); + + final Optional exchange = this.exchangeService.getById(offerId); + + if(exchange.isEmpty()){ + return new ResponseEntity<>("Given offer does not exist.", HttpStatus.BAD_REQUEST); + } + + final Exchange exchangeToDelete = exchange.get(); + + if(!(exchangeToDelete.getOwnedAssignment().getCommision().getCommisionOwner().getId().equals(asker.getId()))){ + return new ResponseEntity<>("You have not permission for that action.", HttpStatus.BAD_REQUEST); + } + + this.exchangeService.deleteExchange(exchangeToDelete); + return new ResponseEntity<>("Success", HttpStatus.OK); + } } From 842e38898af1e0a11e74d161a0e261761a4cbc17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Thu, 7 Jan 2021 16:51:35 +0100 Subject: [PATCH 10/30] Checkpoint: return all exchanges works MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../com/plannaplan/entities/Exchange.java | 39 +++++++++++++------ .../repositories/ExchangeRepository.java | 4 ++ .../plannaplan/services/ExchangeService.java | 9 +++++ .../controllers/ExchangeController.java | 18 +++++++++ .../mappers/ExchangeResponseMappers.java | 14 +++++++ .../responses/models/ExchangeResponse.java | 36 +++++++++++++++++ 6 files changed, 109 insertions(+), 11 deletions(-) create mode 100644 restservice/src/main/java/com/plannaplan/responses/mappers/ExchangeResponseMappers.java create mode 100644 restservice/src/main/java/com/plannaplan/responses/models/ExchangeResponse.java diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java index c0830c4..9ee37b8 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java @@ -8,7 +8,7 @@ import javax.persistence.JoinColumn; import javax.persistence.OneToOne; /** - * Entity that keeps user exchange offer. + * Entity that keeps user exchange offer. */ @Entity public class Exchange { @@ -20,29 +20,49 @@ public class Exchange { @OneToOne @JoinColumn(name = "owned_id", unique = true) private Assignment ownedAssignment; - + @OneToOne @JoinColumn(name = "desired_id") private Groups desiredAssignment; - - public Exchange() { + private Long ownerId; + + public Exchange(){ + } - public Exchange(Assignment ownedAssignment, Groups desiredAssignment){ + /** + * @return Long ID of exchange trader + */ + public Long getOwnerId() { + return ownerId; + } + + /** + * @param ownerId ID of exchange trader + */ + public void setOwnerId(Long ownerId) { + this.ownerId = ownerId; + } + + /** + * + * @param ownedAssignment Assignment which owner would like to trade + * @param desiredAssignment Groups instance that trader wants + */ + public Exchange(Assignment ownedAssignment, Groups desiredAssignment) { this.ownedAssignment = ownedAssignment; this.desiredAssignment = desiredAssignment; + this.ownerId = this.ownedAssignment.getCommision().getCommisionOwner().getId(); } - /** * @return Long ID in database */ public Long getId(){ return this.id; } - - + /** * @return Groups Target group */ @@ -50,7 +70,6 @@ public class Exchange { return desiredAssignment; } - /** * @param desiredAssignment Target group */ @@ -58,7 +77,6 @@ public class Exchange { this.desiredAssignment = desiredAssignment; } - /** * @return Assignment Owned assignment */ @@ -66,7 +84,6 @@ public class Exchange { return ownedAssignment; } - /** * @param ownedAssignment Owned assignment */ diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/ExchangeRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/ExchangeRepository.java index 796625a..0a1a210 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/repositories/ExchangeRepository.java +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/ExchangeRepository.java @@ -1,5 +1,6 @@ package com.plannaplan.repositories; +import java.util.List; import java.util.Optional; import com.plannaplan.entities.Assignment; @@ -16,5 +17,8 @@ public interface ExchangeRepository extends JpaRepository{ @Query("FROM Exchange WHERE owned_id = ?1 AND desired_id = ?2") Optional checkForExchange(@Param("owned_id") Assignment assignment, @Param("desired_id") Groups group); + + @Query("FROM Exchange WHERE ownerId = ?1") + List getByUserId(@Param("id") Long id); } diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java index 7a78e5a..143f459 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java @@ -1,5 +1,6 @@ package com.plannaplan.services; +import java.util.List; import java.util.Optional; import com.plannaplan.entities.Assignment; @@ -32,6 +33,14 @@ public class ExchangeService { return this.repo.findById(id); } + /** + * @param id Id of user + * @return List of exchanges that belong to user + */ + public List getByUserId(Long id){ + return this.repo.getByUserId(id); + } + /** * @param entity Exchange entity which we would like to delete */ diff --git a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java index e255fe0..dccf17e 100644 --- a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java @@ -1,5 +1,6 @@ package com.plannaplan.controllers; +import java.util.List; import java.util.Map; import java.util.Optional; @@ -9,6 +10,8 @@ import com.plannaplan.entities.Exchange; import com.plannaplan.entities.Groups; import com.plannaplan.entities.User; import com.plannaplan.exceptions.UserNotFoundException; +import com.plannaplan.responses.mappers.ExchangeResponseMappers; +import com.plannaplan.responses.models.ExchangeResponse; import com.plannaplan.services.AssignmentService; import com.plannaplan.services.ExchangeService; import com.plannaplan.services.GroupService; @@ -19,6 +22,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -104,4 +108,18 @@ public class ExchangeController extends TokenBasedController{ this.exchangeService.deleteExchange(exchangeToDelete); return new ResponseEntity<>("Success", HttpStatus.OK); } + + @GetMapping("/exchange/all") + @ApiOperation(value = "Get exchange offers") + public ResponseEntity> getExchange() + throws UserNotFoundException { + + final User asker = this.getCurrentUser() + .orElseThrow(() -> new UserNotFoundException("Invalid token")); + + final List response = exchangeService.getByUserId(asker.getId()); + final List listOfResponses = ExchangeResponseMappers.mapToDefaultResponse(response); + + return new ResponseEntity<>(listOfResponses, HttpStatus.OK); + } } diff --git a/restservice/src/main/java/com/plannaplan/responses/mappers/ExchangeResponseMappers.java b/restservice/src/main/java/com/plannaplan/responses/mappers/ExchangeResponseMappers.java new file mode 100644 index 0000000..5adc9d6 --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/responses/mappers/ExchangeResponseMappers.java @@ -0,0 +1,14 @@ +package com.plannaplan.responses.mappers; + +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +import com.plannaplan.entities.Exchange; +import com.plannaplan.responses.models.ExchangeResponse; + +public class ExchangeResponseMappers { + public static final List mapToDefaultResponse(List exchanges) { + return exchanges.stream().filter(Objects::nonNull).map(ExchangeResponse::new).collect(Collectors.toList()); + } +} diff --git a/restservice/src/main/java/com/plannaplan/responses/models/ExchangeResponse.java b/restservice/src/main/java/com/plannaplan/responses/models/ExchangeResponse.java new file mode 100644 index 0000000..7e6057c --- /dev/null +++ b/restservice/src/main/java/com/plannaplan/responses/models/ExchangeResponse.java @@ -0,0 +1,36 @@ +package com.plannaplan.responses.models; + +import com.plannaplan.entities.Exchange; + +public class ExchangeResponse { + private Long id; + private GroupDefaultResponse ownedAssignment; + private GroupDefaultResponse desiredGroup; + + public ExchangeResponse(Exchange exchange){ + this.id = exchange.getId(); + this.ownedAssignment = new GroupDefaultResponse(exchange.getOwnedAssignment().getGroup()); + this.desiredGroup = new GroupDefaultResponse(exchange.getDesiredAssignment()); + } + + public GroupDefaultResponse getDesiredGroup() { + return desiredGroup; + } + public void setDesiredGroup(GroupDefaultResponse desiredGroup) { + this.desiredGroup = desiredGroup; + } + public GroupDefaultResponse getOwnedAssignment() { + return ownedAssignment; + } + public void setOwnedAssignment(GroupDefaultResponse ownedAssignment) { + this.ownedAssignment = ownedAssignment; + } + public Long getId() { + return id; + } + public void setId(Long id) { + this.id = id; + } + + +} From e24938dfce9d2f76dc0bedff0a28a0c7d019eb56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Thu, 7 Jan 2021 17:02:12 +0100 Subject: [PATCH 11/30] Checkpoint: return per ID works MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../controllers/ExchangeController.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java index dccf17e..6c9634e 100644 --- a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java @@ -122,4 +122,26 @@ public class ExchangeController extends TokenBasedController{ return new ResponseEntity<>(listOfResponses, HttpStatus.OK); } + + @GetMapping("/exchange/{id}") + @ApiOperation(value = "Get exchange offers") + public ResponseEntity getExchangeById(@PathVariable(name = "id", required = false) Long offerId) + throws UserNotFoundException { + + final User asker = this.getCurrentUser() + .orElseThrow(() -> new UserNotFoundException("Invalid token")); + + final Optional exchange = this.exchangeService.getById(offerId); + + if(exchange.isEmpty()){ + return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST); + } + + final Exchange exchangeInstance = exchange.get(); + if(!exchangeInstance.getOwnerId().equals(asker.getId())){ + return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST); + } + + return new ResponseEntity<>(new ExchangeResponse(exchangeInstance), HttpStatus.OK); + } } From c7feb10997dc6d228a6faa9e93a1a55a57e8a3db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Sun, 10 Jan 2021 10:57:33 +0100 Subject: [PATCH 12/30] Checkpoint: ownedGroups.contains(groupId) and tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../controllers/ExchangeController.java | 13 ++-- .../controllers/ExchangeControllerTest.java | 65 +++++++++++++++++++ 2 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java diff --git a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java index 6c9634e..7b90d64 100644 --- a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java @@ -3,6 +3,8 @@ package com.plannaplan.controllers; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.stream.Collector; +import java.util.stream.Collectors; import com.plannaplan.App; import com.plannaplan.entities.Assignment; @@ -36,12 +38,6 @@ import io.swagger.annotations.ApiOperation; @RequestMapping("/api/" + App.API_VERSION + "/exchanges") @Api(tags = { "Exchange" }, value = "Exchange", description = "Endpoint to exchange with accepted assignments.") public class ExchangeController extends TokenBasedController{ - - - /* - * Endpoint do wylistowania exchangow użytkownika - * Endpoint do usuwania exchanga - */ @Autowired private GroupService groupService; @@ -61,6 +57,11 @@ public class ExchangeController extends TokenBasedController{ .orElseThrow(() -> new UserNotFoundException("Invalid token")); final Long assignmentId = exchangeRequest.get("assignment"); final Long groupId = exchangeRequest.get("group"); + final List ownedGroups = asker.getStudentRegisteredGrups().stream().map(Groups::getId).collect(Collectors.toList()); + + if(ownedGroups.contains(groupId)){ + return new ResponseEntity<>("User has already got this group.", HttpStatus.BAD_REQUEST); + } if(assignmentId == null || groupId == null ){ return new ResponseEntity<>("Some of values are missing", HttpStatus.BAD_REQUEST); diff --git a/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java new file mode 100644 index 0000000..613dd8a --- /dev/null +++ b/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java @@ -0,0 +1,65 @@ +package com.plannaplan.controllers; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import static org.junit.Assert.assertTrue; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import javax.validation.constraints.AssertTrue; + +@RunWith(SpringRunner.class) +@SpringBootTest +@ContextConfiguration +public class ExchangeControllerTest extends AbstractControllerTest{ + + @Test + public void shouldGetAllUsersExchanges(){ + assertTrue(false); + } + + @Test + public void shouldFailGettingExchange(){ + assertTrue(false); + } + + @Test + public void shouldFailPostDueToExchangeNoFound(){ + assertTrue(false); + } + + @Test + public void shouldInsertExchange(){ + assertTrue(false); + } + + @Test + public void shouldDenyExchangeDueToAssigmentOverlapping(){ + assertTrue(false); + } + + @Test + public void shouldDenyPostDueToAssignmentNotAccepted(){ + assertTrue(false); + } + + @Test + public void shouldFailDeleteDueToWrongPermissions(){ + assertTrue(false); + } + + @Test + public void shouldFailDeleteDueToExchangeNoFound(){ + assertTrue(false); + } + @Test + public void shouldFailPostDueToGroupAlreadyAccepted(){ + assertTrue(false); + } +} From 2e6f373997d5821d501a4d99025b20c9e4570059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Sun, 10 Jan 2021 12:45:51 +0100 Subject: [PATCH 13/30] Checkpoint: 5/11 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../controllers/ExchangeControllerTest.java | 164 ++++++++++++++++-- 1 file changed, 148 insertions(+), 16 deletions(-) diff --git a/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java index 613dd8a..89f35bb 100644 --- a/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java @@ -2,64 +2,196 @@ package com.plannaplan.controllers; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.MethodMode; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*; import static org.junit.Assert.assertTrue; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import java.nio.charset.Charset; + import javax.validation.constraints.AssertTrue; +import com.plannaplan.entities.Groups; +import com.plannaplan.entities.User; +import com.plannaplan.exceptions.UserNotFoundException; +import com.plannaplan.services.GroupService; +import com.plannaplan.services.UserService; +import com.plannaplan.types.UserRoles; +import com.plannaplan.types.WeekDay; + @RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration -public class ExchangeControllerTest extends AbstractControllerTest{ - +public class ExchangeControllerTest extends AbstractControllerTest { + + private final static String EXCHANGE_ENDPOINT = "/api/v1/exchanges/exchange"; + private static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(), + MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); + + @Autowired + private UserService userService; + + @Autowired + private GroupService groupService; + @Test - public void shouldGetAllUsersExchanges(){ - assertTrue(false); + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) + public void shouldGetAllUsersExchanges() throws Exception { + final User user = this.userService.save(new User(null, null, + "shouldGetAllUsersExchanges@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); } @Test - public void shouldFailGettingExchange(){ - assertTrue(false); + public void shouldFailGettingNotExistingExchange() throws Exception { + final User user = this.userService.save(new User(null, null, + "shouldFailGettingNotExistingExchange@ExchangeController.test", UserRoles.STUDENT)); + + final String token = this.userService.login(user).getToken(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(get(EXCHANGE_ENDPOINT + "/" + user.getId()).header("Authorization", "Bearer " + token)).andExpect(status().isBadRequest()); } @Test - public void shouldFailPostDueToExchangeNoFound(){ - assertTrue(false); + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) + public void shouldGetSingleExchange(){ + final User user = this.userService.save(new User(null, null, + "shouldGetSingleExchange@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); } @Test + public void shouldFailGettingExchangeDueToPermission(){ + final User user = this.userService.save(new User(null, null, + "shouldFailGettingExchangeDueToPermission@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); + } + + @Test + public void shouldFailPostDueToAssignmentNotFound() throws Exception{ + final User user = this.userService.save(new User(null, null, + "shouldFailPostDueToAssignmentNotFound@ExchangeController.test", UserRoles.STUDENT)); + final String token = this.userService.login(user).getToken(); + final Groups group = this.groupService.save(new Groups(212, "A2-1", null, 420, WeekDay.WEDNESDAY, null)); + + MockMvc mockMvc = + MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + + token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ user.getId() +", \"group\": "+ group.getId() +" }")).andExpect(status().isBadRequest()); + } + + @Test + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) public void shouldInsertExchange(){ - assertTrue(false); + final User user = this.userService.save(new User(null, null, + "shouldFailGettingNotExistingExchange@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); } @Test + public void shouldFailInsertExchangeDueToMissingGroup() throws Exception{ + final User user = this.userService.save(new User(null, null, + "shouldFailInsertExchangeDueToMissingGroup@ExchangeController.test", UserRoles.STUDENT)); + final String token = this.userService.login(user).getToken(); + + MockMvc mockMvc = + MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + + token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ user.getId() +" }")).andExpect(status().isBadRequest()); + } + + @Test + public void shouldFailInsertExchangeDueToMissingAssignment() throws Exception{ + final User user = this.userService.save(new User(null, null, + "shouldFailInsertExchangeDueToMissingAssignment@ExchangeController.test", UserRoles.STUDENT)); + final String token = this.userService.login(user).getToken(); + final Groups group = this.groupService.save(new Groups(212, "A2-1", null, 420, WeekDay.WEDNESDAY, null)); + + MockMvc mockMvc = + MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + + token).contentType(APPLICATION_JSON_UTF8).content("{\"group\": "+ group.getId() +" }")).andExpect(status().isBadRequest()); + } + + @Test + public void shouldFailInsertExchangeDueToMissingParam() throws Exception{ + final User user = this.userService.save(new User(null, null, + "shouldFailInsertExchangeDueToMissingParam@ExchangeController.test", UserRoles.STUDENT)); + final String token = this.userService.login(user).getToken(); + + MockMvc mockMvc = + MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + + token).contentType(APPLICATION_JSON_UTF8)).andExpect(status().isBadRequest()); + } + + @Test + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) public void shouldDenyExchangeDueToAssigmentOverlapping(){ - assertTrue(false); + final User user = this.userService.save(new User(null, null, + "shouldDenyExchangeDueToAssigmentOverlapping@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); + } + @Test + public void shouldDenyPostDueToAssignmentNotAccepted() throws Exception{ + final User user = this.userService.save(new User(null, null, + "shouldDenyPostDueToAssignmentNotAccepted@ExchangeController.test", UserRoles.STUDENT)); + final String token = this.userService.login(user).getToken(); + final Groups group = this.groupService.save(new Groups(212, "A2-1", null, 420, WeekDay.WEDNESDAY, null)); + + MockMvc mockMvc = + MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + + token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ user.getId() +", \"group\": "+ group.getId() +" }")).andExpect(status().isBadRequest()); } @Test - public void shouldDenyPostDueToAssignmentNotAccepted(){ - assertTrue(false); + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) + public void shouldDeleteExchange(){ + final User user = this.userService.save(new User(null, null, + "shouldDeleteExchange@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); } @Test public void shouldFailDeleteDueToWrongPermissions(){ - assertTrue(false); + final User user = this.userService.save(new User(null, null, + "shouldFailDeleteDueToWrongPermissions@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); } @Test - public void shouldFailDeleteDueToExchangeNoFound(){ - assertTrue(false); + public void shouldFailDeleteDueToMissingParam(){ + final User user = this.userService.save(new User(null, null, + "shouldFailDeleteDueToMissingParam@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); } + @Test + public void shouldFailDeleteDueToExchangeNotFound(){ + final User user = this.userService.save(new User(null, null, + "shouldFailDeleteDueToExchangeNotFound@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); + } + + @Test + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) public void shouldFailPostDueToGroupAlreadyAccepted(){ - assertTrue(false); + final User user = this.userService.save(new User(null, null, + "shouldFailPostDueToGroupAlreadyAccepted@ExchangeController.test", UserRoles.STUDENT)); + assertTrue(false); } } From 81f870eaa1eefccc615ef2c1c6ef5fcf9cf09ba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Sun, 10 Jan 2021 14:15:58 +0100 Subject: [PATCH 14/30] Checkpoint: 15/1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../controllers/ExchangeControllerTest.java | 162 ++++++++++++++---- 1 file changed, 125 insertions(+), 37 deletions(-) diff --git a/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java index 89f35bb..02b7f7d 100644 --- a/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java @@ -21,11 +21,14 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import java.nio.charset.Charset; -import javax.validation.constraints.AssertTrue; - +import com.plannaplan.entities.Assignment; +import com.plannaplan.entities.Commision; +import com.plannaplan.entities.Exchange; import com.plannaplan.entities.Groups; import com.plannaplan.entities.User; -import com.plannaplan.exceptions.UserNotFoundException; +import com.plannaplan.services.AssignmentService; +import com.plannaplan.services.CommisionService; +import com.plannaplan.services.ExchangeService; import com.plannaplan.services.GroupService; import com.plannaplan.services.UserService; import com.plannaplan.types.UserRoles; @@ -46,12 +49,30 @@ public class ExchangeControllerTest extends AbstractControllerTest { @Autowired private GroupService groupService; + @Autowired + private AssignmentService assignmentService; + + @Autowired + private CommisionService commisionService; + + @Autowired + private ExchangeService exchangeService; + @Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) public void shouldGetAllUsersExchanges() throws Exception { final User user = this.userService.save(new User(null, null, - "shouldGetAllUsersExchanges@ExchangeController.test", UserRoles.STUDENT)); - assertTrue(false); + "shouldGetAllUsersExchanges@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + final String token = this.userService.login(user).getToken(); + final Groups group = this.groupService.save(new Groups(215, "A2-2", null, 520, WeekDay.TUESDAY, null)); + final Groups groupDesired = this.groupService.save(new Groups(216, "A2-3", null, 530, WeekDay.MONDAY, null)); + final Commision commision = this.commisionService.save(new Commision(user)); + final Assignment assignment = this.assignmentService.save(new Assignment(group, commision)); + this.exchangeService.save(new Exchange(assignment, groupDesired)); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(get(EXCHANGE_ENDPOINT + "/all").header("Authorization", "Bearer " + + token)).andExpect(status().isOk()); } @Test @@ -67,17 +88,38 @@ public class ExchangeControllerTest extends AbstractControllerTest { @Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - public void shouldGetSingleExchange(){ + public void shouldGetSingleExchange() throws Exception{ final User user = this.userService.save(new User(null, null, - "shouldGetSingleExchange@ExchangeController.test", UserRoles.STUDENT)); - assertTrue(false); + "shouldGetSingleExchange@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + final String token = this.userService.login(user).getToken(); + final Groups group = this.groupService.save(new Groups(215, "A2-2", null, 520, WeekDay.TUESDAY, null)); + final Groups groupDesired = this.groupService.save(new Groups(216, "A2-3", null, 530, WeekDay.MONDAY, null)); + final Commision commision = this.commisionService.save(new Commision(user)); + final Assignment assignment = this.assignmentService.save(new Assignment(group, commision)); + final Exchange exchange = this.exchangeService.save(new Exchange(assignment, groupDesired)); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(get(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + + token)).andExpect(status().isOk()); } @Test - public void shouldFailGettingExchangeDueToPermission(){ + public void shouldFailGettingExchangeDueToPermission() throws Exception{ final User user = this.userService.save(new User(null, null, - "shouldFailGettingExchangeDueToPermission@ExchangeController.test", UserRoles.STUDENT)); - assertTrue(false); + "shouldFailGettingExchangeDueToPermission@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + final Groups group = this.groupService.save(new Groups(215, "A2-2", null, 520, WeekDay.TUESDAY, null)); + final Groups groupDesired = this.groupService.save(new Groups(216, "A2-3", null, 530, WeekDay.MONDAY, null)); + final Commision commision = this.commisionService.save(new Commision(user)); + final Assignment assignment = this.assignmentService.save(new Assignment(group, commision)); + final Exchange exchange = this.exchangeService.save(new Exchange(assignment, groupDesired)); + + final User user2 = this.userService.save(new User(null, null, + "shouldFailGettingExchangeDueToPermission2@ExchangeController.test", "11112", UserRoles.STUDENT, 321)); + final String token2 = this.userService.login(user2).getToken(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(post(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + + token2)).andExpect(status().is4xxClientError()); } @Test @@ -95,10 +137,20 @@ public class ExchangeControllerTest extends AbstractControllerTest { @Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - public void shouldInsertExchange(){ + public void shouldInsertExchange() throws Exception{ final User user = this.userService.save(new User(null, null, - "shouldFailGettingNotExistingExchange@ExchangeController.test", UserRoles.STUDENT)); - assertTrue(false); + "shouldInsertExchange@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + final String token = this.userService.login(user).getToken(); + final Groups group = this.groupService.save(new Groups(215, "A2-2", null, 520, WeekDay.TUESDAY, null)); + final Groups groupDesired = this.groupService.save(new Groups(216, "A2-3", null, 530, WeekDay.MONDAY, null)); + final Commision commision = this.commisionService.save(new Commision(user)); + final Assignment assignment = this.assignmentService.save(new Assignment(group, commision)); + + this.assignmentService.callAcceptAlgorythm(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + + token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ assignment.getId() +", \"group\": "+ groupDesired.getId() +" }")).andExpect(status().isOk()); } @Test @@ -107,8 +159,7 @@ public class ExchangeControllerTest extends AbstractControllerTest { "shouldFailInsertExchangeDueToMissingGroup@ExchangeController.test", UserRoles.STUDENT)); final String token = this.userService.login(user).getToken(); - MockMvc mockMvc = - MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ user.getId() +" }")).andExpect(status().isBadRequest()); } @@ -120,8 +171,7 @@ public class ExchangeControllerTest extends AbstractControllerTest { final String token = this.userService.login(user).getToken(); final Groups group = this.groupService.save(new Groups(212, "A2-1", null, 420, WeekDay.WEDNESDAY, null)); - MockMvc mockMvc = - MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8).content("{\"group\": "+ group.getId() +" }")).andExpect(status().isBadRequest()); } @@ -132,59 +182,97 @@ public class ExchangeControllerTest extends AbstractControllerTest { "shouldFailInsertExchangeDueToMissingParam@ExchangeController.test", UserRoles.STUDENT)); final String token = this.userService.login(user).getToken(); - MockMvc mockMvc = - MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token).contentType(APPLICATION_JSON_UTF8)).andExpect(status().isBadRequest()); } @Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - public void shouldDenyExchangeDueToAssigmentOverlapping(){ + public void shouldDenyExchangeDueToAssigmentOverlapping() throws Exception{ final User user = this.userService.save(new User(null, null, - "shouldDenyExchangeDueToAssigmentOverlapping@ExchangeController.test", UserRoles.STUDENT)); - assertTrue(false); + "shouldDenyExchangeDueToAssigmentOverlapping@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + final String token = this.userService.login(user).getToken(); + final Groups group = this.groupService.save(new Groups(215, "A2-2", null, 520, WeekDay.TUESDAY, null)); + final Commision commision = this.commisionService.save(new Commision(user)); + final Assignment assignment = this.assignmentService.save(new Assignment(group, commision)); + this.exchangeService.save(new Exchange(assignment, group)); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + + token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ user.getId() +", \"group\": "+ group.getId() +" }")).andExpect(status().isBadRequest()); } + @Test public void shouldDenyPostDueToAssignmentNotAccepted() throws Exception{ final User user = this.userService.save(new User(null, null, "shouldDenyPostDueToAssignmentNotAccepted@ExchangeController.test", UserRoles.STUDENT)); final String token = this.userService.login(user).getToken(); final Groups group = this.groupService.save(new Groups(212, "A2-1", null, 420, WeekDay.WEDNESDAY, null)); + final Groups group2 = this.groupService.save(new Groups(213, "A2-2", null, 420, WeekDay.MONDAY, null)); + final Commision commision = this.commisionService.save(new Commision(user)); + final Assignment assignment = this.assignmentService.save(new Assignment(group,commision)); - MockMvc mockMvc = - MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + - token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ user.getId() +", \"group\": "+ group.getId() +" }")).andExpect(status().isBadRequest()); + token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ assignment.getId() +", \"group\": "+ group2.getId() +" }")).andExpect(status().isBadRequest()); } @Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - public void shouldDeleteExchange(){ + public void shouldDeleteExchange() throws Exception{ final User user = this.userService.save(new User(null, null, - "shouldDeleteExchange@ExchangeController.test", UserRoles.STUDENT)); - assertTrue(false); + "shouldDeleteExchange@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + final String token = this.userService.login(user).getToken(); + final Groups group = this.groupService.save(new Groups(215, "A2-2", null, 520, WeekDay.TUESDAY, null)); + final Groups groupDesired = this.groupService.save(new Groups(216, "A2-3", null, 530, WeekDay.MONDAY, null)); + final Commision commision = this.commisionService.save(new Commision(user)); + final Assignment assignment = this.assignmentService.save(new Assignment(group, commision)); + final Exchange exchange = this.exchangeService.save(new Exchange(assignment, groupDesired)); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(delete(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + + token)).andExpect(status().isOk()); } @Test - public void shouldFailDeleteDueToWrongPermissions(){ + public void shouldFailDeleteDueToWrongPermissions() throws Exception{ final User user = this.userService.save(new User(null, null, - "shouldFailDeleteDueToWrongPermissions@ExchangeController.test", UserRoles.STUDENT)); - assertTrue(false); + "shouldFailDeleteDueToWrongPermissions@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + final Groups group = this.groupService.save(new Groups(215, "A2-2", null, 520, WeekDay.TUESDAY, null)); + final Groups groupDesired = this.groupService.save(new Groups(216, "A2-3", null, 530, WeekDay.MONDAY, null)); + final Commision commision = this.commisionService.save(new Commision(user)); + final Assignment assignment = this.assignmentService.save(new Assignment(group, commision)); + final Exchange exchange = this.exchangeService.save(new Exchange(assignment, groupDesired)); + + final User user2 = this.userService.save(new User(null, null, + "shouldFailDeleteDueToWrongPermissions@ExchangeController2.test", "11112", UserRoles.STUDENT, 322)); + final String token2 = this.userService.login(user2).getToken(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(get(EXCHANGE_ENDPOINT + "/" + exchange.getId()).header("Authorization", "Bearer " + + token2)).andExpect(status().isBadRequest()); } @Test - public void shouldFailDeleteDueToMissingParam(){ + public void shouldFailDeleteDueToMissingParam() throws Exception{ final User user = this.userService.save(new User(null, null, - "shouldFailDeleteDueToMissingParam@ExchangeController.test", UserRoles.STUDENT)); - assertTrue(false); + "shouldFailDeleteDueToMissingParam@ExchangeController.test", UserRoles.STUDENT)); + final String token = this.userService.login(user).getToken(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(delete(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + token)).andExpect(status().is4xxClientError()); } @Test - public void shouldFailDeleteDueToExchangeNotFound(){ + public void shouldFailDeleteDueToExchangeNotFound() throws Exception{ final User user = this.userService.save(new User(null, null, "shouldFailDeleteDueToExchangeNotFound@ExchangeController.test", UserRoles.STUDENT)); - assertTrue(false); + final String token = this.userService.login(user).getToken(); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(delete(EXCHANGE_ENDPOINT + "/" + user.getId()).header("Authorization", "Bearer " + + token)).andExpect(status().isBadRequest()); } @Test From 00f639ad557376a55fc22faf2f4260c6596645ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Sun, 10 Jan 2021 14:16:31 +0100 Subject: [PATCH 15/30] Removed unused import MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../main/java/com/plannaplan/controllers/ExchangeController.java | 1 - 1 file changed, 1 deletion(-) diff --git a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java index 7b90d64..9470b3c 100644 --- a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java @@ -3,7 +3,6 @@ package com.plannaplan.controllers; import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.stream.Collector; import java.util.stream.Collectors; import com.plannaplan.App; From 6fe72465b9fd4431b5ed6a3cc63d3842f4d8e519 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Sun, 10 Jan 2021 14:24:39 +0100 Subject: [PATCH 16/30] Checkpoint: All test good :3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../controllers/ExchangeControllerTest.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java index 02b7f7d..1b89a32 100644 --- a/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java @@ -277,9 +277,19 @@ public class ExchangeControllerTest extends AbstractControllerTest { @Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - public void shouldFailPostDueToGroupAlreadyAccepted(){ - final User user = this.userService.save(new User(null, null, - "shouldFailPostDueToGroupAlreadyAccepted@ExchangeController.test", UserRoles.STUDENT)); - assertTrue(false); + public void shouldFailPostDueToGroupAlreadyAccepted() throws Exception{ + final User user = this.userService.save(new User(null, null, "shouldFailPostDueToGroupAlreadyAccepted@ExchangeController.test", "11111", UserRoles.STUDENT, 320)); + final String token = this.userService.login(user).getToken(); + final Groups group = this.groupService.save(new Groups(215, "A2-2", null, 520, WeekDay.TUESDAY, null)); + final Groups group2 = this.groupService.save(new Groups(216, "A2-3", null, 530, WeekDay.MONDAY, null)); + final Commision commision = this.commisionService.save(new Commision(user)); + final Assignment assignment = this.assignmentService.save(new Assignment(group, commision)); + this.assignmentService.save(new Assignment(group2, commision)); + this.assignmentService.callAcceptAlgorythm(); + this.exchangeService.save(new Exchange(assignment, group2)); + + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build(); + mockMvc.perform(post(EXCHANGE_ENDPOINT).header("Authorization", "Bearer " + + token).contentType(APPLICATION_JSON_UTF8).content("{\"assignment\": "+ assignment.getId() +", \"group\": "+ group2.getId() +" }")).andExpect(status().isBadRequest()); } } From 6311ecee9267dc10f80096ed8d94138e4134efd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Sun, 10 Jan 2021 14:37:03 +0100 Subject: [PATCH 17/30] Checkpoint: removed unsed imports MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../java/com/plannaplan/controllers/ExchangeControllerTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java b/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java index 1b89a32..a0736c2 100644 --- a/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java +++ b/restservice/src/test/java/com/plannaplan/controllers/ExchangeControllerTest.java @@ -13,7 +13,6 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*; -import static org.junit.Assert.assertTrue; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; From b18f9cd50e2bae25190e516903fcd0d90e7482dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Sun, 10 Jan 2021 15:59:26 +0100 Subject: [PATCH 18/30] Checkpoint: adding docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../plannaplan/controllers/ExchangeController.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java index 9470b3c..87436d3 100644 --- a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java @@ -30,7 +30,11 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.Api; +import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import io.swagger.annotations.Example; +import io.swagger.annotations.ExampleProperty; @RestController @CrossOrigin @@ -48,8 +52,11 @@ public class ExchangeController extends TokenBasedController{ private ExchangeService exchangeService; @PostMapping("/exchange") - @ApiOperation(value = "Return all courses") - public ResponseEntity createExchange(@RequestBody Map exchangeRequest) + @ApiOperation(value = "Creates exchange offer.") + public ResponseEntity createExchange( + @ApiParam( value = "Json object that contains assignment to trade and desired group") + @RequestBody + Map exchangeRequest) throws UserNotFoundException { final User asker = this.getCurrentUser() From 5e459ac4292795f47aeafd13d197748d08880b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Mon, 11 Jan 2021 16:20:59 +0100 Subject: [PATCH 19/30] Checkpoint: Added test to ExchangeRepo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../repositories/ExchangeRepositoryTest.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 buisnesslogic/src/test/java/com/plannaplan/repositories/ExchangeRepositoryTest.java diff --git a/buisnesslogic/src/test/java/com/plannaplan/repositories/ExchangeRepositoryTest.java b/buisnesslogic/src/test/java/com/plannaplan/repositories/ExchangeRepositoryTest.java new file mode 100644 index 0000000..1690c5b --- /dev/null +++ b/buisnesslogic/src/test/java/com/plannaplan/repositories/ExchangeRepositoryTest.java @@ -0,0 +1,69 @@ +package com.plannaplan.repositories; + +import static org.junit.Assert.assertTrue; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import com.plannaplan.entities.Assignment; +import com.plannaplan.entities.Commision; +import com.plannaplan.entities.Exchange; +import com.plannaplan.entities.Groups; +import com.plannaplan.entities.User; +import com.plannaplan.services.AssignmentService; +import com.plannaplan.services.GroupService; +import com.plannaplan.services.UserService; +import com.plannaplan.types.UserRoles; +import com.plannaplan.types.WeekDay; + +import org.junit.Test; + +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.MethodMode; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +@ContextConfiguration +public class ExchangeRepositoryTest{ + @Autowired + private ExchangeRepository exchangeRepository; + + @Autowired + private AssignmentService assignmentService; + + @Autowired + private GroupService groupService; + + @Autowired + private UserService userService; + + @Autowired + private CommisionRepository commisionRepository; + + @Test + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) + public void shouldReturnMatches() { + final User user1 = this.userService.save(new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT , 451)); + final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); + final Commision commision1 = this.commisionRepository.save(new Commision(user1)); + final User user2 = this.userService.save(new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT , 451)); + final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); + final Commision commision2 = this.commisionRepository.save(new Commision(user2)); + final Assignment assignmentUser1 = this.assignmentService.save(new Assignment(group1, commision1)); + final Assignment assignmentUser2 = this.assignmentService.save(new Assignment(group2, commision2)); + + this.assignmentService.callAcceptAlgorythm(); + + this.exchangeRepository.save(new Exchange(assignmentUser1, group2)); + this.exchangeRepository.save(new Exchange(assignmentUser2, group1)); + + final List exchangeRepoMatches = this.exchangeRepository.getMatches(); + assertTrue(exchangeRepoMatches.size() == 2); + } +} From 76faedc40df2998aee15163838b05c3d52942b78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Mon, 11 Jan 2021 16:21:25 +0100 Subject: [PATCH 20/30] Checkpoint: Added test to ExchangeRepo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../repositories/ExchangeRepository.java | 8 +- .../plannaplan/services/ExchangeService.java | 84 ++++++++++--------- 2 files changed, 49 insertions(+), 43 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/ExchangeRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/ExchangeRepository.java index 0a1a210..d932eb6 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/repositories/ExchangeRepository.java +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/ExchangeRepository.java @@ -16,9 +16,11 @@ import org.springframework.stereotype.Repository; public interface ExchangeRepository extends JpaRepository{ @Query("FROM Exchange WHERE owned_id = ?1 AND desired_id = ?2") - Optional checkForExchange(@Param("owned_id") Assignment assignment, @Param("desired_id") Groups group); + Optional checkForExchange(@Param("owned_id") Assignment assignment, @Param("desired_id") Groups group); @Query("FROM Exchange WHERE ownerId = ?1") - List getByUserId(@Param("id") Long id); - + List getByUserId(@Param("id") Long id); + + @Query("Select e1,e2 FROM Exchange e1, Exchange e2 WHERE e1.ownedAssignment.group.id = e2.desiredAssignment.id") + List getMatches(); } diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java index 143f459..3c423b8 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java @@ -13,47 +13,51 @@ import org.springframework.stereotype.Service; @Service public class ExchangeService { - - @Autowired - private ExchangeRepository repo; - /** - * @param exchange Instance to save in database - * @return Exchange Instance contains database id - */ - public Exchange save(Exchange exchange){ - return this.repo.save(exchange); - } - - /** - * @param id Id of exchange in database - * @return Optional Exchange if found - */ - public Optional getById(Long id){ - return this.repo.findById(id); - } - - /** - * @param id Id of user - * @return List of exchanges that belong to user - */ - public List getByUserId(Long id){ - return this.repo.getByUserId(id); - } + @Autowired + private ExchangeRepository repo; - /** - * @param entity Exchange entity which we would like to delete - */ - public void deleteExchange(Exchange entity){ - this.repo.delete(entity); - } + /** + * @param exchange Instance to save in database + * @return Exchange Instance contains database id + */ + public Exchange save(Exchange exchange) { + return this.repo.save(exchange); + } - /** - * @param assignment Assignment to trade for - * @param group Desired group - * @return Optional with Exchange if exist - */ - public Optional checkForExchange(Assignment assignment, Groups group){ - return this.repo.checkForExchange(assignment, group); - } + /** + * @param id Id of exchange in database + * @return Optional Exchange if found + */ + public Optional getById(Long id) { + return this.repo.findById(id); + } + + /** + * @param id Id of user + * @return List of exchanges that belong to user + */ + public List getByUserId(Long id) { + return this.repo.getByUserId(id); + } + + /** + * @param entity Exchange entity which we would like to delete + */ + public void deleteExchange(Exchange entity) { + this.repo.delete(entity); + } + + /** + * @param assignment Assignment to trade for + * @param group Desired group + * @return Optional with Exchange if exist + */ + public Optional checkForExchange(Assignment assignment, Groups group) { + return this.repo.checkForExchange(assignment, group); + } + + public void performExchange() { + + } } From 3583d30b264130228fc9076340d0066f0965818e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Mon, 11 Jan 2021 16:56:39 +0100 Subject: [PATCH 21/30] Checkpoint: Added test to ExchangeRepo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../java/com/plannaplan/models/MatchData.java | 44 ++++++++++ .../plannaplan/services/ExchangeService.java | 85 ++++++++++--------- 2 files changed, 90 insertions(+), 39 deletions(-) create mode 100644 buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java diff --git a/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java b/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java new file mode 100644 index 0000000..7ecca87 --- /dev/null +++ b/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java @@ -0,0 +1,44 @@ +package com.plannaplan.models; + +import com.plannaplan.entities.Assignment; + +public class MatchData { + private Assignment assignmentOne; + private Assignment assignmentTwo; + + public MatchData(Assignment assignmentOne, Assignment assignmentTwo) { + this.assignmentOne = assignmentOne; + this.assignmentTwo = assignmentTwo; + } + + public Assignment getAssignmentTwo() { + return assignmentTwo; + } + + public Assignment getAssignmentOne() { + return assignmentOne; + } + + @Override + public boolean equals(Object o) { + + // If the object is compared with itself then return true + if (o == this) { + return true; + } + + /* + * Check if o is an instance of Complex or not "null instanceof [type]" also + * returns false + */ + if (!(o instanceof MatchData)) { + return false; + } + + // typecast o to Complex so that we can compare data members + MatchData c = (MatchData) o; + + // Compare the data members and return accordingly + return (this.assignmentOne.equals(c.getAssignmentOne()) && this.assignmentTwo.equals(c.getAssignmentTwo())) || (this.assignmentOne.equals(c.getAssignmentTwo()) && this.assignmentTwo.equals(c.getAssignmentOne())); + } +} diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java index 3c423b8..af43b97 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java @@ -6,6 +6,7 @@ import java.util.Optional; import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Exchange; import com.plannaplan.entities.Groups; +import com.plannaplan.models.MatchData; import com.plannaplan.repositories.ExchangeRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -14,50 +15,56 @@ import org.springframework.stereotype.Service; @Service public class ExchangeService { - @Autowired - private ExchangeRepository repo; + @Autowired + private ExchangeRepository repo; - /** - * @param exchange Instance to save in database - * @return Exchange Instance contains database id - */ - public Exchange save(Exchange exchange) { - return this.repo.save(exchange); - } + /** + * @param exchange Instance to save in database + * @return Exchange Instance contains database id + */ + public Exchange save(Exchange exchange) { + return this.repo.save(exchange); + } - /** - * @param id Id of exchange in database - * @return Optional Exchange if found - */ - public Optional getById(Long id) { - return this.repo.findById(id); - } + /** + * @param id Id of exchange in database + * @return Optional Exchange if found + */ + public Optional getById(Long id) { + return this.repo.findById(id); + } - /** - * @param id Id of user - * @return List of exchanges that belong to user - */ - public List getByUserId(Long id) { - return this.repo.getByUserId(id); - } + /** + * @param id Id of user + * @return List of exchanges that belong to user + */ + public List getByUserId(Long id) { + return this.repo.getByUserId(id); + } - /** - * @param entity Exchange entity which we would like to delete - */ - public void deleteExchange(Exchange entity) { - this.repo.delete(entity); - } + /** + * @param entity Exchange entity which we would like to delete + */ + public void deleteExchange(Exchange entity) { + this.repo.delete(entity); + } - /** - * @param assignment Assignment to trade for - * @param group Desired group - * @return Optional with Exchange if exist - */ - public Optional checkForExchange(Assignment assignment, Groups group) { - return this.repo.checkForExchange(assignment, group); - } + /** + * @param assignment Assignment to trade for + * @param group Desired group + * @return Optional with Exchange if exist + */ + public Optional checkForExchange(Assignment assignment, Groups group) { + return this.repo.checkForExchange(assignment, group); + } - public void performExchange() { + public void performExchange() { - } + } + +// public void getMatches(){ +// final List matches = this.repo.getMatches().stream().map(m -> { +// return (MatchData) m; +// }); +// } } From 1c12a778af030ec7a65632becb77a9dbee8ce14b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Tue, 12 Jan 2021 13:04:57 +0100 Subject: [PATCH 22/30] Checkpoint: Added ExchangeServiceTest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../java/com/plannaplan/models/MatchData.java | 5 ++ .../plannaplan/services/ExchangeService.java | 18 +++-- .../repositories/ExchangeRepositoryTest.java | 2 - .../services/ExchangeServiceTest.java | 66 +++++++++++++++++++ .../controllers/ExchangeController.java | 23 +++---- 5 files changed, 94 insertions(+), 20 deletions(-) create mode 100644 buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java diff --git a/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java b/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java index 7ecca87..77891ce 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java +++ b/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java @@ -19,6 +19,11 @@ public class MatchData { return assignmentOne; } + @Override + public int hashCode() { + return this.assignmentOne.hashCode() + this.assignmentTwo.hashCode(); + } + @Override public boolean equals(Object o) { diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java index af43b97..dea4e63 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java @@ -1,7 +1,10 @@ package com.plannaplan.services; +import java.util.HashSet; import java.util.List; import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Exchange; @@ -62,9 +65,14 @@ public class ExchangeService { } -// public void getMatches(){ -// final List matches = this.repo.getMatches().stream().map(m -> { -// return (MatchData) m; -// }); -// } + public Set getMatches(){ + final List matches = this.repo.getMatches().stream().map(m -> { + final Exchange exchangeOne = (Exchange) m[0]; + final Exchange exchangeTwo = (Exchange) m[1]; + return new MatchData(exchangeOne.getOwnedAssignment(), exchangeTwo.getOwnedAssignment()); + }).collect(Collectors.toList()); + + final Set filterMatches = new HashSet<>(matches); + return filterMatches; + } } diff --git a/buisnesslogic/src/test/java/com/plannaplan/repositories/ExchangeRepositoryTest.java b/buisnesslogic/src/test/java/com/plannaplan/repositories/ExchangeRepositoryTest.java index 1690c5b..1f3640f 100644 --- a/buisnesslogic/src/test/java/com/plannaplan/repositories/ExchangeRepositoryTest.java +++ b/buisnesslogic/src/test/java/com/plannaplan/repositories/ExchangeRepositoryTest.java @@ -2,9 +2,7 @@ package com.plannaplan.repositories; import static org.junit.Assert.assertTrue; -import java.util.HashSet; import java.util.List; -import java.util.Set; import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Commision; diff --git a/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java b/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java new file mode 100644 index 0000000..2ac8b4d --- /dev/null +++ b/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java @@ -0,0 +1,66 @@ +package com.plannaplan.services; + +import static org.junit.Assert.assertTrue; + +import java.util.Set; + +import com.plannaplan.entities.Assignment; +import com.plannaplan.entities.Commision; +import com.plannaplan.entities.Exchange; +import com.plannaplan.entities.Groups; +import com.plannaplan.entities.User; +import com.plannaplan.models.MatchData; +import com.plannaplan.repositories.CommisionRepository; +import com.plannaplan.types.UserRoles; +import com.plannaplan.types.WeekDay; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.MethodMode; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +@ContextConfiguration +public class ExchangeServiceTest { + + @Autowired + private AssignmentService assignmentService; + + @Autowired + private GroupService groupService; + + @Autowired + private UserService userService; + + @Autowired + private CommisionRepository commisionRepository; + + @Autowired + private ExchangeService exchangeService; + + @Test + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) + public void shouldReturnUniqMatches() { + final User user1 = this.userService.save(new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT , 451)); + final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); + final Commision commision1 = this.commisionRepository.save(new Commision(user1)); + final User user2 = this.userService.save(new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT , 451)); + final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); + final Commision commision2 = this.commisionRepository.save(new Commision(user2)); + final Assignment assignmentUser1 = this.assignmentService.save(new Assignment(group1, commision1)); + final Assignment assignmentUser2 = this.assignmentService.save(new Assignment(group2, commision2)); + + this.assignmentService.callAcceptAlgorythm(); + + this.exchangeService.save(new Exchange(assignmentUser1, group2)); + this.exchangeService.save(new Exchange(assignmentUser2, group1)); + + final Set uniqList = this.exchangeService.getMatches(); + assertTrue(uniqList.size() == 1); + } +} diff --git a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java index 87436d3..24c0d89 100644 --- a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java @@ -30,11 +30,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.Api; -import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; -import io.swagger.annotations.Example; -import io.swagger.annotations.ExampleProperty; @RestController @CrossOrigin @@ -47,13 +44,13 @@ public class ExchangeController extends TokenBasedController{ @Autowired private AssignmentService assignmentService; - + @Autowired private ExchangeService exchangeService; @PostMapping("/exchange") @ApiOperation(value = "Creates exchange offer.") - public ResponseEntity createExchange( + public ResponseEntity createExchange( @ApiParam( value = "Json object that contains assignment to trade and desired group") @RequestBody Map exchangeRequest) @@ -63,7 +60,7 @@ public class ExchangeController extends TokenBasedController{ .orElseThrow(() -> new UserNotFoundException("Invalid token")); final Long assignmentId = exchangeRequest.get("assignment"); final Long groupId = exchangeRequest.get("group"); - final List ownedGroups = asker.getStudentRegisteredGrups().stream().map(Groups::getId).collect(Collectors.toList()); + final List ownedGroups = asker.getStudentRegisteredGrups().stream().map(Groups::getId).collect(Collectors.toList()); if(ownedGroups.contains(groupId)){ return new ResponseEntity<>("User has already got this group.", HttpStatus.BAD_REQUEST); @@ -75,7 +72,7 @@ public class ExchangeController extends TokenBasedController{ final Optional assignment = this.assignmentService.getById(assignmentId); final Optional group = this.groupService.getGroupById(groupId); - + if(assignment.isEmpty() || group.isEmpty()){ return new ResponseEntity<>("Some of provided value does not exist.", HttpStatus.BAD_REQUEST); } @@ -101,7 +98,7 @@ public class ExchangeController extends TokenBasedController{ .orElseThrow(() -> new UserNotFoundException("Invalid token")); final Optional exchange = this.exchangeService.getById(offerId); - + if(exchange.isEmpty()){ return new ResponseEntity<>("Given offer does not exist.", HttpStatus.BAD_REQUEST); } @@ -123,10 +120,10 @@ public class ExchangeController extends TokenBasedController{ final User asker = this.getCurrentUser() .orElseThrow(() -> new UserNotFoundException("Invalid token")); - + final List response = exchangeService.getByUserId(asker.getId()); final List listOfResponses = ExchangeResponseMappers.mapToDefaultResponse(response); - + return new ResponseEntity<>(listOfResponses, HttpStatus.OK); } @@ -137,7 +134,7 @@ public class ExchangeController extends TokenBasedController{ final User asker = this.getCurrentUser() .orElseThrow(() -> new UserNotFoundException("Invalid token")); - + final Optional exchange = this.exchangeService.getById(offerId); if(exchange.isEmpty()){ @@ -146,9 +143,9 @@ public class ExchangeController extends TokenBasedController{ final Exchange exchangeInstance = exchange.get(); if(!exchangeInstance.getOwnerId().equals(asker.getId())){ - return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST); + return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST); } - + return new ResponseEntity<>(new ExchangeResponse(exchangeInstance), HttpStatus.OK); } } From bc8f7407fc8ab345fe3769d4a01ed6538c268118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Tue, 12 Jan 2021 14:10:48 +0100 Subject: [PATCH 23/30] Checkpoint: added performExchange --- .../com/plannaplan/entities/Assignment.java | 23 +++++----- .../java/com/plannaplan/entities/User.java | 45 ++++++++++--------- .../java/com/plannaplan/models/MatchData.java | 27 +++++++---- .../plannaplan/services/ExchangeService.java | 44 +++++++++++++++++- 4 files changed, 99 insertions(+), 40 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java index 429d482..5d5550d 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Assignment.java @@ -10,7 +10,7 @@ import javax.persistence.ManyToOne; /** * Entity of Assignment grouping of state associated about group_id and * commision_id - * + * */ @Entity @@ -28,7 +28,7 @@ public class Assignment { /** * Assignment - * + * * @param group group we would like to assign * @param commision commision that assignment belongs to * @param isPastAssignment is assignment past or no @@ -40,14 +40,14 @@ public class Assignment { /** * If it returns trues it mesans u are assigned to group accepted by algorythm - * + * * @return boolean isAccepted */ public boolean isAccepted() { return this.group.getRegisteredStudents().contains(this.commision.getCommisionOwner()); } - /** + /** * Getter of commision * @return Commision Commision of given assignments */ @@ -57,7 +57,7 @@ public class Assignment { /** * Assignment - * + * * @param group group we would like to assign * @param commision commision that assignment belongs to */ @@ -68,9 +68,12 @@ public class Assignment { public Assignment() { } + public void setCommision(Commision commision) { + this.commision = commision; + } /** * Id getter - * + * * @return id id of assignment */ @@ -80,7 +83,7 @@ public class Assignment { /** * getGroup - * + * * @return group */ public Groups getGroup() { @@ -89,7 +92,7 @@ public class Assignment { /** * isPastAssignment getter - * + * * @return isPastAssignment */ public boolean isPastAssignment() { @@ -98,10 +101,10 @@ public class Assignment { /** * setter isPastAssignment - * + * * @param isPastAssignment is assignment past or not */ public void setPastAssignment(boolean isPastAssignment) { this.isPastAssignment = isPastAssignment; } -} \ No newline at end of file +} diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/User.java b/buisnesslogic/src/main/java/com/plannaplan/entities/User.java index 149880d..d14411f 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/User.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/User.java @@ -47,6 +47,11 @@ public class User { return this.studentRegisteredGrups; } + public void removeGroup(Long id) { + final Groups groupToDelete = this.studentRegisteredGrups.stream().filter(e -> e.getId().equals(id)).findFirst().get(); + this.studentRegisteredGrups.remove(groupToDelete); + } + public void claimGroup(Groups group) { if (this.studentRegisteredGrups == null) { this.studentRegisteredGrups = new HashSet<>(); @@ -58,7 +63,7 @@ public class User { } /** - * + * * @param name name given to the user * @param surname surname given to the user * @param mail mail given to the user @@ -72,7 +77,7 @@ public class User { } /** - * + * * @param name name given to the user * @param surname surname given to the user * @param mail mail given to the user @@ -85,7 +90,7 @@ public class User { } /** - * + * * @param name name given to the user * @param surname surname given to the user * @param mail mail given to the user @@ -101,7 +106,7 @@ public class User { /** * usos id getter - * + * * @return usosid */ public String getUsosId() { @@ -110,7 +115,7 @@ public class User { /** * email getter - * + * * @return mailof user */ public String getEmail() { @@ -119,7 +124,7 @@ public class User { /** * email setter - * + * * @param email user email */ public void setEmail(String email) { @@ -128,7 +133,7 @@ public class User { /** * token usage getter - * + * * @return Timestamp when token was used */ public Timestamp getTokenUsageDate() { @@ -137,7 +142,7 @@ public class User { /** * token getter - * + * * @return user token */ public String getToken() { @@ -153,7 +158,7 @@ public class User { /** * token seter. Sets token and automaticly set time when was set - * + * * @param token token to set */ public void setToken(String token) { @@ -164,7 +169,7 @@ public class User { /** * name setter - * + * * @return String user name */ public String getName() { @@ -173,7 +178,7 @@ public class User { /** * user rolse getter - * + * * @return UserRoles of user */ public UserRoles getRole() { @@ -182,7 +187,7 @@ public class User { /** * user role setter - * + * * @param role to be set */ public void setRole(UserRoles role) { @@ -191,7 +196,7 @@ public class User { /** * surname getter - * + * * @return string surname */ public String getSurname() { @@ -200,7 +205,7 @@ public class User { /** * surname setter - * + * * @param surname string to be set as surnames */ public void setSurname(String surname) { @@ -209,7 +214,7 @@ public class User { /** * name stter - * + * * @param name stirng to be set as name */ public void setName(String name) { @@ -218,7 +223,7 @@ public class User { /** * id getter - * + * * @return id in database */ public Long getId() { @@ -227,7 +232,7 @@ public class User { /** * Ranking points are [100;500]. It's calculated by gradesAvg*100*studiesYear - * + * * @return ranking points [100;500] */ public Integer getRanking() { @@ -237,7 +242,7 @@ public class User { /** * Ranking points setter. Ranking points are [100;500]. It's calculated by * gradesAvg*100*studiesYear - * + * * @param ranking ranking points [100;500] */ public void setRanking(Integer ranking) { @@ -246,7 +251,7 @@ public class User { /** * updates user entity with data got by UsosApiService::getUserData - * + * * @param usosData UserApiResponse model with needed data */ public void updateWithUsosData(UserApiResponse usosData) { @@ -257,7 +262,7 @@ public class User { /** * 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() { diff --git a/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java b/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java index 77891ce..320cf46 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java +++ b/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java @@ -1,27 +1,36 @@ package com.plannaplan.models; import com.plannaplan.entities.Assignment; +import com.plannaplan.entities.Exchange; public class MatchData { - private Assignment assignmentOne; - private Assignment assignmentTwo; + private Exchange exchangeOne; + private Exchange exchangeTwo; - public MatchData(Assignment assignmentOne, Assignment assignmentTwo) { - this.assignmentOne = assignmentOne; - this.assignmentTwo = assignmentTwo; + public MatchData(Exchange exchangeOne, Exchange exchangeTwo) { + this.exchangeOne = exchangeOne; + this.exchangeTwo = exchangeTwo; + } + + public Exchange getExchangeOne() { + return this.exchangeOne; + } + + public Exchange getExchangeTwo() { + return this.exchangeTwo; } public Assignment getAssignmentTwo() { - return assignmentTwo; + return this.exchangeTwo.getOwnedAssignment(); } public Assignment getAssignmentOne() { - return assignmentOne; + return this.exchangeOne.getOwnedAssignment(); } @Override public int hashCode() { - return this.assignmentOne.hashCode() + this.assignmentTwo.hashCode(); + return this.getAssignmentOne().hashCode() + this.getAssignmentTwo().hashCode(); } @Override @@ -44,6 +53,6 @@ public class MatchData { MatchData c = (MatchData) o; // Compare the data members and return accordingly - return (this.assignmentOne.equals(c.getAssignmentOne()) && this.assignmentTwo.equals(c.getAssignmentTwo())) || (this.assignmentOne.equals(c.getAssignmentTwo()) && this.assignmentTwo.equals(c.getAssignmentOne())); + return (this.getAssignmentOne().equals(c.getAssignmentOne()) && this.getAssignmentTwo().equals(c.getAssignmentTwo())) || (this.getAssignmentOne().equals(c.getAssignmentTwo()) && this.getAssignmentTwo().equals(c.getAssignmentOne())); } } diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java index dea4e63..1ea00ec 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java @@ -1,5 +1,6 @@ package com.plannaplan.services; +import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Optional; @@ -7,8 +8,10 @@ import java.util.Set; import java.util.stream.Collectors; import com.plannaplan.entities.Assignment; +import com.plannaplan.entities.Commision; import com.plannaplan.entities.Exchange; import com.plannaplan.entities.Groups; +import com.plannaplan.entities.User; import com.plannaplan.models.MatchData; import com.plannaplan.repositories.ExchangeRepository; @@ -21,6 +24,12 @@ public class ExchangeService { @Autowired private ExchangeRepository repo; + @Autowired + private AssignmentService assignmentService; + + @Autowired + private UserService userService; + /** * @param exchange Instance to save in database * @return Exchange Instance contains database id @@ -62,14 +71,47 @@ public class ExchangeService { } public void performExchange() { + final Set matchData = this.getMatches(); + final List performedAssignmentExchanges = new ArrayList<>(); + matchData.forEach( m -> { + final Assignment assignmentOne = m.getAssignmentOne(); + final Assignment assignmentTwo = m.getAssignmentTwo(); + if (!(performedAssignmentExchanges.contains(assignmentOne.getId()) || performedAssignmentExchanges.contains(assignmentTwo.getId()))){ + final Commision commisionOne = assignmentOne.getCommision(); + final User userOne = commisionOne.getCommisionOwner(); + final Commision commisionTwo = assignmentTwo.getCommision(); + final User userTwo = commisionTwo.getCommisionOwner(); + + assignmentOne.setCommision(commisionTwo); + assignmentTwo.setCommision(commisionOne); + + userOne.removeGroup(assignmentOne.getGroup().getId()); + userTwo.removeGroup(assignmentTwo.getGroup().getId()); + + userOne.claimGroup(assignmentTwo.getGroup()); + userTwo.claimGroup(assignmentOne.getGroup()); + + this.assignmentService.save(assignmentOne); + this.assignmentService.save(assignmentTwo); + + this.userService.save(userOne); + this.userService.save(userTwo); + + performedAssignmentExchanges.add(assignmentOne.getId()); + performedAssignmentExchanges.add(assignmentTwo.getId()); + } + + this.deleteExchange(m.getExchangeOne()); + this.deleteExchange(m.getExchangeTwo()); + }); } public Set getMatches(){ final List matches = this.repo.getMatches().stream().map(m -> { final Exchange exchangeOne = (Exchange) m[0]; final Exchange exchangeTwo = (Exchange) m[1]; - return new MatchData(exchangeOne.getOwnedAssignment(), exchangeTwo.getOwnedAssignment()); + return new MatchData(exchangeOne, exchangeTwo); }).collect(Collectors.toList()); final Set filterMatches = new HashSet<>(matches); From 1e1160736dce2014602334f7a6e8194e700d91bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Tue, 12 Jan 2021 14:35:11 +0100 Subject: [PATCH 24/30] Checkpoint: Added ShouldPerformExchange MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../repositories/ExchangeRepositoryTest.java | 6 ++-- .../services/ExchangeServiceTest.java | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/buisnesslogic/src/test/java/com/plannaplan/repositories/ExchangeRepositoryTest.java b/buisnesslogic/src/test/java/com/plannaplan/repositories/ExchangeRepositoryTest.java index 1f3640f..20cc593 100644 --- a/buisnesslogic/src/test/java/com/plannaplan/repositories/ExchangeRepositoryTest.java +++ b/buisnesslogic/src/test/java/com/plannaplan/repositories/ExchangeRepositoryTest.java @@ -47,10 +47,12 @@ public class ExchangeRepositoryTest{ @Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) public void shouldReturnMatches() { - final User user1 = this.userService.save(new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT , 451)); + final User user1 = this.userService.save( + new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT, 451)); final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); final Commision commision1 = this.commisionRepository.save(new Commision(user1)); - final User user2 = this.userService.save(new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT , 451)); + final User user2 = this.userService.save( + new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT, 451)); final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); final Commision commision2 = this.commisionRepository.save(new Commision(user2)); final Assignment assignmentUser1 = this.assignmentService.save(new Assignment(group1, commision1)); diff --git a/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java b/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java index 2ac8b4d..66395a8 100644 --- a/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java +++ b/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java @@ -63,4 +63,39 @@ public class ExchangeServiceTest { final Set uniqList = this.exchangeService.getMatches(); assertTrue(uniqList.size() == 1); } + + @Test + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) + public void shouldPerformExchange() { + final User user1 = this.userService.save(new User(null, null, "1shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT , 451)); + final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); + final Commision commision1 = this.commisionRepository.save(new Commision(user1)); + + final User user2 = this.userService.save(new User(null, null, "2shouldReturnMatches@ExchangeRepository.test", "123455", UserRoles.STUDENT , 452)); + final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); + final Commision commision2 = this.commisionRepository.save(new Commision(user2)); + + final User user3 = this.userService.save(new User(null, null, "3shouldReturnMatches@ExchangeRepository.test", "123456", UserRoles.STUDENT , 453)); + final Groups group3 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.WEDNESDAY, null)); + final Commision commision3 = this.commisionRepository.save(new Commision(user3)); + + final User user4 = this.userService.save(new User(null, null, "2shouldReturnMatches@ExchangeRepository.test", "123457", UserRoles.STUDENT , 455)); + final Groups group4 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.FRIDAY, null)); + final Commision commision4 = this.commisionRepository.save(new Commision(user4)); + + final Assignment assignmentUser1 = this.assignmentService.save(new Assignment(group1, commision1)); + final Assignment assignmentUser2 = this.assignmentService.save(new Assignment(group2, commision2)); + final Assignment assignmentUser3 = this.assignmentService.save(new Assignment(group2, commision3)); + final Assignment assignmentUser4 = this.assignmentService.save(new Assignment(group4, commision4)); + + this.assignmentService.callAcceptAlgorythm(); + + this.exchangeService.save(new Exchange(assignmentUser1, group2)); + this.exchangeService.save(new Exchange(assignmentUser2, group1)); + this.exchangeService.save(new Exchange(assignmentUser3, group1)); + this.exchangeService.save(new Exchange(assignmentUser4, group3)); + + this.exchangeService.performExchange(); + assertTrue(false); + } } From f9a27abb3231a1f6a2a4e5d67e15fff87179db26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Tue, 12 Jan 2021 18:39:22 +0100 Subject: [PATCH 25/30] Checkpoint: Added mew test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../plannaplan/services/ExchangeService.java | 4 + .../services/ExchangeServiceTest.java | 80 +++++++++++++++++-- 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java index 1ea00ec..d142bef 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java @@ -46,6 +46,10 @@ public class ExchangeService { return this.repo.findById(id); } + public List getAllExchanges() { + return this.repo.findAll(); + } + /** * @param id Id of user * @return List of exchanges that belong to user diff --git a/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java b/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java index 66395a8..2669b89 100644 --- a/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java +++ b/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java @@ -2,7 +2,9 @@ package com.plannaplan.services; import static org.junit.Assert.assertTrue; +import java.util.List; import java.util.Set; +import java.util.stream.Collectors; import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Commision; @@ -14,6 +16,7 @@ import com.plannaplan.repositories.CommisionRepository; import com.plannaplan.types.UserRoles; import com.plannaplan.types.WeekDay; +import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -66,20 +69,29 @@ public class ExchangeServiceTest { @Test @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) + @Ignore public void shouldPerformExchange() { - final User user1 = this.userService.save(new User(null, null, "1shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT , 451)); + User user1 = this.userService.save( + new User(null, null, "1shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT, 451)); + final Long user1Id = user1.getId(); final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); final Commision commision1 = this.commisionRepository.save(new Commision(user1)); - final User user2 = this.userService.save(new User(null, null, "2shouldReturnMatches@ExchangeRepository.test", "123455", UserRoles.STUDENT , 452)); + User user2 = this.userService.save( + new User(null, null, "2shouldReturnMatches@ExchangeRepository.test", "123455", UserRoles.STUDENT, 452)); + final Long user2Id = user2.getId(); final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); final Commision commision2 = this.commisionRepository.save(new Commision(user2)); - final User user3 = this.userService.save(new User(null, null, "3shouldReturnMatches@ExchangeRepository.test", "123456", UserRoles.STUDENT , 453)); + User user3 = this.userService.save( + new User(null, null, "3shouldReturnMatches@ExchangeRepository.test", "123456", UserRoles.STUDENT, 453)); + final Long user3Id = user3.getId(); final Groups group3 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.WEDNESDAY, null)); final Commision commision3 = this.commisionRepository.save(new Commision(user3)); - final User user4 = this.userService.save(new User(null, null, "2shouldReturnMatches@ExchangeRepository.test", "123457", UserRoles.STUDENT , 455)); + User user4 = this.userService.save( + new User(null, null, "2shouldReturnMatches@ExchangeRepository.test", "123457", UserRoles.STUDENT, 455)); + final Long user4Id = user4.getId(); final Groups group4 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.FRIDAY, null)); final Commision commision4 = this.commisionRepository.save(new Commision(user4)); @@ -93,9 +105,67 @@ public class ExchangeServiceTest { this.exchangeService.save(new Exchange(assignmentUser1, group2)); this.exchangeService.save(new Exchange(assignmentUser2, group1)); this.exchangeService.save(new Exchange(assignmentUser3, group1)); + this.exchangeService.save(new Exchange(assignmentUser4, group3)); this.exchangeService.performExchange(); - assertTrue(false); + + user1 = this.userService.getById(user1Id).get(); + user2 = this.userService.getById(user2Id).get(); + user3 = this.userService.getById(user3Id).get(); + user4 = this.userService.getById(user4Id).get(); + + final List listGroupsOfUser1 = user1.getStudentRegisteredGrups().stream().map(Groups::getId) + .collect(Collectors.toList()); + final List listGroupsOfUser2 = user2.getStudentRegisteredGrups().stream().map(Groups::getId) + .collect(Collectors.toList()); + final List listGroupsOfUser3 = user3.getStudentRegisteredGrups().stream().map(Groups::getId) + .collect(Collectors.toList()); + final List listGroupsOfUser4 = user4.getStudentRegisteredGrups().stream().map(Groups::getId) + .collect(Collectors.toList()); + + assertTrue(listGroupsOfUser1.contains(group2.getId())); + assertTrue(listGroupsOfUser2.contains(group1.getId())); + assertTrue(listGroupsOfUser3.contains(group2.getId())); + assertTrue(listGroupsOfUser4.contains(group4.getId())); + } + + @Test + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) + public void shouldRemoveOutDatedExchnages() { + User user1 = this.userService.save(new User(null, null, "1shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT , 451)); + final Long user1Id = user1.getId(); + final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); + final Commision commision1 = this.commisionRepository.save(new Commision(user1)); + + User user2 = this.userService.save(new User(null, null, "2shouldReturnMatches@ExchangeRepository.test", "123455", UserRoles.STUDENT, 452)); + final Long user2Id = user2.getId(); + final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); + final Commision commision2 = this.commisionRepository.save(new Commision(user2)); + + final Groups group3 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); + final Groups group4 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); + + final Assignment assignmentUser1 = this.assignmentService.save(new Assignment(group1, commision1)); + final Assignment assignmentUser2 = this.assignmentService.save(new Assignment(group2, commision2)); + + this.assignmentService.callAcceptAlgorythm(); + + this.exchangeService.save(new Exchange(assignmentUser1, group2)); + this.exchangeService.save(new Exchange(assignmentUser2, group1)); + this.exchangeService.save(new Exchange(assignmentUser1, group3)); + this.exchangeService.save(new Exchange(assignmentUser1, group4)); + + this.exchangeService.performExchange(); + + user1 = this.userService.getById(user1Id).get(); + user2 = this.userService.getById(user2Id).get(); + + final List listGroupsOfUser1 = user1.getStudentRegisteredGrups().stream().map(Groups::getId).collect(Collectors.toList()); + final List listGroupsOfUser2 = user2.getStudentRegisteredGrups().stream().map(Groups::getId).collect(Collectors.toList()); + + assertTrue(listGroupsOfUser1.contains(group2.getId())); + assertTrue(listGroupsOfUser2.contains(group1.getId())); + assertTrue(this.exchangeService.getAllExchanges().size() == 0); } } From ed528ad9a080a84e90f3569c4f29267b3f53f5b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Wed, 13 Jan 2021 12:23:00 +0100 Subject: [PATCH 26/30] Checkpoint: ExchangeServiceTest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../com/plannaplan/entities/Exchange.java | 38 ++- .../java/com/plannaplan/models/MatchData.java | 10 + .../plannaplan/services/ExchangeService.java | 7 +- .../services/ExchangeServiceTest.java | 294 ++++++++++-------- 4 files changed, 207 insertions(+), 142 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java index 9ee37b8..44a4d3f 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java @@ -1,5 +1,8 @@ package com.plannaplan.entities; +import java.sql.Date; +import java.sql.Timestamp; + import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; @@ -18,7 +21,7 @@ public class Exchange { private Long id; @OneToOne - @JoinColumn(name = "owned_id", unique = true) + @JoinColumn(name = "owned_id") private Assignment ownedAssignment; @OneToOne @@ -27,67 +30,74 @@ public class Exchange { private Long ownerId; + private Timestamp dateExchange; + public Exchange(){ - + } - /** + /** * @return Long ID of exchange trader */ public Long getOwnerId() { return ownerId; } - - /** + + /** * @param ownerId ID of exchange trader */ public void setOwnerId(Long ownerId) { this.ownerId = ownerId; } + public Timestamp getDataExchange() { + return this.dateExchange; + } + /** - * + * * @param ownedAssignment Assignment which owner would like to trade * @param desiredAssignment Groups instance that trader wants */ public Exchange(Assignment ownedAssignment, Groups desiredAssignment) { this.ownedAssignment = ownedAssignment; this.desiredAssignment = desiredAssignment; - this.ownerId = this.ownedAssignment.getCommision().getCommisionOwner().getId(); + this.ownerId = this.ownedAssignment != null ? this.ownedAssignment.getCommision().getCommisionOwner().getId() : null; + this.dateExchange = new Timestamp(System.currentTimeMillis()); } - /** + /** * @return Long ID in database */ public Long getId(){ return this.id; } - - /** + + /** * @return Groups Target group */ public Groups getDesiredAssignment() { return desiredAssignment; } - /** + /** * @param desiredAssignment Target group */ public void setDesiredAssignment(Groups desiredAssignment) { this.desiredAssignment = desiredAssignment; } - /** + /** * @return Assignment Owned assignment */ public Assignment getOwnedAssignment() { return ownedAssignment; } - /** + /** * @param ownedAssignment Owned assignment */ public void setOwnedAssignment(Assignment ownedAssignment) { this.ownedAssignment = ownedAssignment; } -} \ No newline at end of file +} diff --git a/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java b/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java index 320cf46..93dbec6 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java +++ b/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java @@ -1,5 +1,7 @@ package com.plannaplan.models; +import java.sql.Timestamp; + import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Exchange; @@ -55,4 +57,12 @@ public class MatchData { // Compare the data members and return accordingly return (this.getAssignmentOne().equals(c.getAssignmentOne()) && this.getAssignmentTwo().equals(c.getAssignmentTwo())) || (this.getAssignmentOne().equals(c.getAssignmentTwo()) && this.getAssignmentTwo().equals(c.getAssignmentOne())); } + + public int compare(MatchData m1) { + return Float.compare(m1.getExchangesMsValue(), this.getExchangesMsValue()); + } + + public float getExchangesMsValue(){ + return this.exchangeOne.getDataExchange().getTime() + this.exchangeTwo.getDataExchange().getTime(); + } } diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java index d142bef..c87c0fa 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java @@ -1,10 +1,13 @@ package com.plannaplan.services; +import java.sql.Timestamp; import java.util.ArrayList; +import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.TreeSet; import java.util.stream.Collectors; import com.plannaplan.entities.Assignment; @@ -116,9 +119,11 @@ public class ExchangeService { final Exchange exchangeOne = (Exchange) m[0]; final Exchange exchangeTwo = (Exchange) m[1]; return new MatchData(exchangeOne, exchangeTwo); + }).collect(Collectors.toList()); - final Set filterMatches = new HashSet<>(matches); + Set filterMatches = new TreeSet<>((m1, m2) -> m1.compare(m2)); + filterMatches.addAll(matches); return filterMatches; } } diff --git a/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java b/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java index 2669b89..7336772 100644 --- a/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java +++ b/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java @@ -2,8 +2,11 @@ package com.plannaplan.services; import static org.junit.Assert.assertTrue; +import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import java.util.Set; +import java.util.TreeSet; import java.util.stream.Collectors; import com.plannaplan.entities.Assignment; @@ -16,7 +19,8 @@ import com.plannaplan.repositories.CommisionRepository; import com.plannaplan.types.UserRoles; import com.plannaplan.types.WeekDay; -import org.junit.Ignore; +import org.apache.commons.compress.utils.Lists; +import org.assertj.core.util.Arrays; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -31,141 +35,177 @@ import org.springframework.test.context.junit4.SpringRunner; @ContextConfiguration public class ExchangeServiceTest { - @Autowired - private AssignmentService assignmentService; + @Autowired + private AssignmentService assignmentService; - @Autowired - private GroupService groupService; + @Autowired + private GroupService groupService; - @Autowired - private UserService userService; + @Autowired + private UserService userService; - @Autowired - private CommisionRepository commisionRepository; + @Autowired + private CommisionRepository commisionRepository; - @Autowired - private ExchangeService exchangeService; + @Autowired + private ExchangeService exchangeService; + + @Test + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) + public void shouldReturnUniqMatches() { + final User user1 = this.userService.save( + new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT, 451)); + final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); + final Commision commision1 = this.commisionRepository.save(new Commision(user1)); + final User user2 = this.userService.save( + new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT, 451)); + final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); + final Commision commision2 = this.commisionRepository.save(new Commision(user2)); + final Assignment assignmentUser1 = this.assignmentService.save(new Assignment(group1, commision1)); + final Assignment assignmentUser2 = this.assignmentService.save(new Assignment(group2, commision2)); + + this.assignmentService.callAcceptAlgorythm(); + + this.exchangeService.save(new Exchange(assignmentUser1, group2)); + this.exchangeService.save(new Exchange(assignmentUser2, group1)); + + final Set uniqList = this.exchangeService.getMatches(); + assertTrue(uniqList.size() == 1); + } + + @Test + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) + public void shouldPerformExchange() throws Exception{ + User user1 = this.userService.save( + new User(null, null, "1shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT, 451)); + final Long user1Id = user1.getId(); + final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); + final Commision commision1 = this.commisionRepository.save(new Commision(user1)); + + User user2 = this.userService.save( + new User(null, null, "2shouldReturnMatches@ExchangeRepository.test", "123455", UserRoles.STUDENT, 452)); + final Long user2Id = user2.getId(); + final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); + final Commision commision2 = this.commisionRepository.save(new Commision(user2)); + + User user3 = this.userService.save( + new User(null, null, "3shouldReturnMatches@ExchangeRepository.test", "123456", UserRoles.STUDENT, 453)); + final Long user3Id = user3.getId(); + final Groups group3 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.WEDNESDAY, null)); + final Commision commision3 = this.commisionRepository.save(new Commision(user3)); + + User user4 = this.userService.save( + new User(null, null, "2shouldReturnMatches@ExchangeRepository.test", "123457", UserRoles.STUDENT, 455)); + final Long user4Id = user4.getId(); + final Groups group4 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.FRIDAY, null)); + final Commision commision4 = this.commisionRepository.save(new Commision(user4)); + + final Assignment assignmentUser1 = this.assignmentService.save(new Assignment(group1, commision1)); + final Assignment assignmentUser2 = this.assignmentService.save(new Assignment(group2, commision2)); + final Assignment assignmentUser3 = this.assignmentService.save(new Assignment(group2, commision3)); + final Assignment assignmentUser4 = this.assignmentService.save(new Assignment(group4, commision4)); + + this.assignmentService.callAcceptAlgorythm(); + + this.exchangeService.save(new Exchange(assignmentUser1, group2)); + Thread.sleep(1000); + this.exchangeService.save(new Exchange(assignmentUser2, group1)); + Thread.sleep(1000); + this.exchangeService.save(new Exchange(assignmentUser3, group1)); + Thread.sleep(1000); + this.exchangeService.save(new Exchange(assignmentUser4, group3)); + + this.exchangeService.performExchange(); + + user1 = this.userService.getById(user1Id).get(); + user2 = this.userService.getById(user2Id).get(); + user3 = this.userService.getById(user3Id).get(); + user4 = this.userService.getById(user4Id).get(); + + final List listGroupsOfUser1 = user1.getStudentRegisteredGrups().stream().map(Groups::getId) + .collect(Collectors.toList()); + final List listGroupsOfUser2 = user2.getStudentRegisteredGrups().stream().map(Groups::getId) + .collect(Collectors.toList()); + final List listGroupsOfUser3 = user3.getStudentRegisteredGrups().stream().map(Groups::getId) + .collect(Collectors.toList()); + final List listGroupsOfUser4 = user4.getStudentRegisteredGrups().stream().map(Groups::getId) + .collect(Collectors.toList()); + + assertTrue(listGroupsOfUser1.contains(group2.getId())); + assertTrue(listGroupsOfUser2.contains(group1.getId())); + assertTrue(listGroupsOfUser3.contains(group2.getId())); + assertTrue(listGroupsOfUser4.contains(group4.getId())); + } + + @Test + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) + public void shouldRemoveOutDatedExchnages() throws Exception { + User user1 = this.userService.save( + new User(null, null, "1shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT, 451)); + final Long user1Id = user1.getId(); + final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); + final Commision commision1 = this.commisionRepository.save(new Commision(user1)); + + User user2 = this.userService.save( + new User(null, null, "2shouldReturnMatches@ExchangeRepository.test", "123455", UserRoles.STUDENT, 452)); + final Long user2Id = user2.getId(); + final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); + final Commision commision2 = this.commisionRepository.save(new Commision(user2)); + + final Groups group3 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); + final Groups group4 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); + + final Assignment assignmentUser1 = this.assignmentService.save(new Assignment(group1, commision1)); + final Assignment assignmentUser2 = this.assignmentService.save(new Assignment(group2, commision2)); + + this.assignmentService.callAcceptAlgorythm(); + + this.exchangeService.save(new Exchange(assignmentUser1, group2)); + this.exchangeService.save(new Exchange(assignmentUser2, group1)); + this.exchangeService.save(new Exchange(assignmentUser1, group3)); + this.exchangeService.save(new Exchange(assignmentUser1, group4)); + + this.exchangeService.performExchange(); + + user1 = this.userService.getById(user1Id).get(); + user2 = this.userService.getById(user2Id).get(); + + final List listGroupsOfUser1 = user1.getStudentRegisteredGrups().stream().map(Groups::getId) + .collect(Collectors.toList()); + final List listGroupsOfUser2 = user2.getStudentRegisteredGrups().stream().map(Groups::getId) + .collect(Collectors.toList()); + + assertTrue(listGroupsOfUser1.contains(group2.getId())); + assertTrue(listGroupsOfUser2.contains(group1.getId())); + assertTrue(this.exchangeService.getAllExchanges().size() == 0); + } @Test - @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - public void shouldReturnUniqMatches() { - final User user1 = this.userService.save(new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT , 451)); - final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); - final Commision commision1 = this.commisionRepository.save(new Commision(user1)); - final User user2 = this.userService.save(new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT , 451)); - final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); - final Commision commision2 = this.commisionRepository.save(new Commision(user2)); - final Assignment assignmentUser1 = this.assignmentService.save(new Assignment(group1, commision1)); - final Assignment assignmentUser2 = this.assignmentService.save(new Assignment(group2, commision2)); + public void shouldSortExchanges() throws Exception{ + final List listMatrix = new ArrayList<>(); + final Exchange exchange1 = new Exchange(null, null); + Thread.sleep(1000); + final Exchange exchange2 = new Exchange(null, null); + Thread.sleep(1000); + final Exchange exchange3 = new Exchange(null, null); + Thread.sleep(1000); + final Exchange exchange4 = new Exchange(null, null); + Thread.sleep(1000); - this.assignmentService.callAcceptAlgorythm(); + listMatrix.add(new MatchData(exchange1, exchange3)); + listMatrix.add(new MatchData(exchange2, exchange4)); - this.exchangeService.save(new Exchange(assignmentUser1, group2)); - this.exchangeService.save(new Exchange(assignmentUser2, group1)); + Set filterMatches = new TreeSet<>((m1, m2) -> m1.compare(m2)); + filterMatches.addAll(listMatrix); + + Iterator iterator = filterMatches.iterator(); + + while (iterator.hasNext()) { + MatchData matchData = iterator.next(); + System.out.println(matchData.getExchangesMsValue()); + } final Set uniqList = this.exchangeService.getMatches(); - assertTrue(uniqList.size() == 1); - } - - @Test - @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - @Ignore - public void shouldPerformExchange() { - User user1 = this.userService.save( - new User(null, null, "1shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT, 451)); - final Long user1Id = user1.getId(); - final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); - final Commision commision1 = this.commisionRepository.save(new Commision(user1)); - - User user2 = this.userService.save( - new User(null, null, "2shouldReturnMatches@ExchangeRepository.test", "123455", UserRoles.STUDENT, 452)); - final Long user2Id = user2.getId(); - final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); - final Commision commision2 = this.commisionRepository.save(new Commision(user2)); - - User user3 = this.userService.save( - new User(null, null, "3shouldReturnMatches@ExchangeRepository.test", "123456", UserRoles.STUDENT, 453)); - final Long user3Id = user3.getId(); - final Groups group3 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.WEDNESDAY, null)); - final Commision commision3 = this.commisionRepository.save(new Commision(user3)); - - User user4 = this.userService.save( - new User(null, null, "2shouldReturnMatches@ExchangeRepository.test", "123457", UserRoles.STUDENT, 455)); - final Long user4Id = user4.getId(); - final Groups group4 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.FRIDAY, null)); - final Commision commision4 = this.commisionRepository.save(new Commision(user4)); - - final Assignment assignmentUser1 = this.assignmentService.save(new Assignment(group1, commision1)); - final Assignment assignmentUser2 = this.assignmentService.save(new Assignment(group2, commision2)); - final Assignment assignmentUser3 = this.assignmentService.save(new Assignment(group2, commision3)); - final Assignment assignmentUser4 = this.assignmentService.save(new Assignment(group4, commision4)); - - this.assignmentService.callAcceptAlgorythm(); - - this.exchangeService.save(new Exchange(assignmentUser1, group2)); - this.exchangeService.save(new Exchange(assignmentUser2, group1)); - this.exchangeService.save(new Exchange(assignmentUser3, group1)); - - this.exchangeService.save(new Exchange(assignmentUser4, group3)); - - this.exchangeService.performExchange(); - - user1 = this.userService.getById(user1Id).get(); - user2 = this.userService.getById(user2Id).get(); - user3 = this.userService.getById(user3Id).get(); - user4 = this.userService.getById(user4Id).get(); - - final List listGroupsOfUser1 = user1.getStudentRegisteredGrups().stream().map(Groups::getId) - .collect(Collectors.toList()); - final List listGroupsOfUser2 = user2.getStudentRegisteredGrups().stream().map(Groups::getId) - .collect(Collectors.toList()); - final List listGroupsOfUser3 = user3.getStudentRegisteredGrups().stream().map(Groups::getId) - .collect(Collectors.toList()); - final List listGroupsOfUser4 = user4.getStudentRegisteredGrups().stream().map(Groups::getId) - .collect(Collectors.toList()); - - assertTrue(listGroupsOfUser1.contains(group2.getId())); - assertTrue(listGroupsOfUser2.contains(group1.getId())); - assertTrue(listGroupsOfUser3.contains(group2.getId())); - assertTrue(listGroupsOfUser4.contains(group4.getId())); - } - - @Test - @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) - public void shouldRemoveOutDatedExchnages() { - User user1 = this.userService.save(new User(null, null, "1shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT , 451)); - final Long user1Id = user1.getId(); - final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null)); - final Commision commision1 = this.commisionRepository.save(new Commision(user1)); - - User user2 = this.userService.save(new User(null, null, "2shouldReturnMatches@ExchangeRepository.test", "123455", UserRoles.STUDENT, 452)); - final Long user2Id = user2.getId(); - final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); - final Commision commision2 = this.commisionRepository.save(new Commision(user2)); - - final Groups group3 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); - final Groups group4 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.THURSDAY, null)); - - final Assignment assignmentUser1 = this.assignmentService.save(new Assignment(group1, commision1)); - final Assignment assignmentUser2 = this.assignmentService.save(new Assignment(group2, commision2)); - - this.assignmentService.callAcceptAlgorythm(); - - this.exchangeService.save(new Exchange(assignmentUser1, group2)); - this.exchangeService.save(new Exchange(assignmentUser2, group1)); - this.exchangeService.save(new Exchange(assignmentUser1, group3)); - this.exchangeService.save(new Exchange(assignmentUser1, group4)); - - this.exchangeService.performExchange(); - - user1 = this.userService.getById(user1Id).get(); - user2 = this.userService.getById(user2Id).get(); - - final List listGroupsOfUser1 = user1.getStudentRegisteredGrups().stream().map(Groups::getId).collect(Collectors.toList()); - final List listGroupsOfUser2 = user2.getStudentRegisteredGrups().stream().map(Groups::getId).collect(Collectors.toList()); - - assertTrue(listGroupsOfUser1.contains(group2.getId())); - assertTrue(listGroupsOfUser2.contains(group1.getId())); - assertTrue(this.exchangeService.getAllExchanges().size() == 0); + assertTrue(uniqList.size() == 2); } } From d1641277ac908550185c53b47954a4ccce07e81a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Wed, 13 Jan 2021 13:30:46 +0100 Subject: [PATCH 27/30] Checkpoint: ExchangeServiceTest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../java/com/plannaplan/models/MatchData.java | 4 ++-- .../services/ExchangeServiceTest.java | 24 +++++++------------ 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java b/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java index 93dbec6..c3a8496 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java +++ b/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java @@ -59,10 +59,10 @@ public class MatchData { } public int compare(MatchData m1) { - return Float.compare(m1.getExchangesMsValue(), this.getExchangesMsValue()); + return Long.compare(m1.getExchangesMsValue(), this.getExchangesMsValue()); } - public float getExchangesMsValue(){ + public long getExchangesMsValue(){ return this.exchangeOne.getDataExchange().getTime() + this.exchangeTwo.getDataExchange().getTime(); } } diff --git a/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java b/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java index 7336772..bc68185 100644 --- a/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java +++ b/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java @@ -3,6 +3,7 @@ package com.plannaplan.services; import static org.junit.Assert.assertTrue; import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Set; @@ -19,8 +20,6 @@ import com.plannaplan.repositories.CommisionRepository; import com.plannaplan.types.UserRoles; import com.plannaplan.types.WeekDay; -import org.apache.commons.compress.utils.Lists; -import org.assertj.core.util.Arrays; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -177,10 +176,12 @@ public class ExchangeServiceTest { assertTrue(listGroupsOfUser1.contains(group2.getId())); assertTrue(listGroupsOfUser2.contains(group1.getId())); - assertTrue(this.exchangeService.getAllExchanges().size() == 0); + int size = this.exchangeService.getAllExchanges().size(); + assertTrue(this.exchangeService.getAllExchanges().size() == 1); } @Test + @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD) public void shouldSortExchanges() throws Exception{ final List listMatrix = new ArrayList<>(); final Exchange exchange1 = new Exchange(null, null); @@ -192,20 +193,13 @@ public class ExchangeServiceTest { final Exchange exchange4 = new Exchange(null, null); Thread.sleep(1000); - listMatrix.add(new MatchData(exchange1, exchange3)); listMatrix.add(new MatchData(exchange2, exchange4)); + listMatrix.add(new MatchData(exchange1, exchange3)); - Set filterMatches = new TreeSet<>((m1, m2) -> m1.compare(m2)); - filterMatches.addAll(listMatrix); + final List matchDataListSorted = listMatrix.stream().sorted((m1, m2) -> -1 * m1.compare(m2)).collect(Collectors.toList()); - Iterator iterator = filterMatches.iterator(); - - while (iterator.hasNext()) { - MatchData matchData = iterator.next(); - System.out.println(matchData.getExchangesMsValue()); - } - - final Set uniqList = this.exchangeService.getMatches(); - assertTrue(uniqList.size() == 2); + assertTrue(listMatrix.get(0).equals(matchDataListSorted.get(1))); + assertTrue(listMatrix.get(1).equals(matchDataListSorted.get(0))); + assertTrue(matchDataListSorted.size() == 2); } } From 11469e9314b8b8dd398a0ae5034007fc3ba2e668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Wed, 13 Jan 2021 14:13:26 +0100 Subject: [PATCH 28/30] Checkpoint: ExchangeServiceTest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../com/plannaplan/services/ExchangeService.java | 13 +++++++------ .../plannaplan/services/ExchangeServiceTest.java | 13 ++++++++++--- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java index c87c0fa..d3e9e8d 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java @@ -78,7 +78,7 @@ public class ExchangeService { } public void performExchange() { - final Set matchData = this.getMatches(); + final List matchData = this.getMatches(); final List performedAssignmentExchanges = new ArrayList<>(); matchData.forEach( m -> { final Assignment assignmentOne = m.getAssignmentOne(); @@ -114,16 +114,17 @@ public class ExchangeService { }); } - public Set getMatches(){ + public List getMatches(){ final List matches = this.repo.getMatches().stream().map(m -> { final Exchange exchangeOne = (Exchange) m[0]; final Exchange exchangeTwo = (Exchange) m[1]; return new MatchData(exchangeOne, exchangeTwo); - }).collect(Collectors.toList()); - Set filterMatches = new TreeSet<>((m1, m2) -> m1.compare(m2)); - filterMatches.addAll(matches); - return filterMatches; + final Set uniqData = new HashSet<>(matches); + final List matchDataListSorted = uniqData.stream().sorted((m1, m2) -> -1 * m1.compare(m2)) + .collect(Collectors.toList()); + + return matchDataListSorted; } } diff --git a/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java b/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java index bc68185..d71dba7 100644 --- a/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java +++ b/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java @@ -68,7 +68,7 @@ public class ExchangeServiceTest { this.exchangeService.save(new Exchange(assignmentUser1, group2)); this.exchangeService.save(new Exchange(assignmentUser2, group1)); - final Set uniqList = this.exchangeService.getMatches(); + final List uniqList = this.exchangeService.getMatches(); assertTrue(uniqList.size() == 1); } @@ -117,9 +117,13 @@ public class ExchangeServiceTest { this.exchangeService.performExchange(); user1 = this.userService.getById(user1Id).get(); + Thread.sleep(1000); user2 = this.userService.getById(user2Id).get(); + Thread.sleep(1000); user3 = this.userService.getById(user3Id).get(); + Thread.sleep(1000); user4 = this.userService.getById(user4Id).get(); + Thread.sleep(1000); final List listGroupsOfUser1 = user1.getStudentRegisteredGrups().stream().map(Groups::getId) .collect(Collectors.toList()); @@ -160,9 +164,13 @@ public class ExchangeServiceTest { this.assignmentService.callAcceptAlgorythm(); this.exchangeService.save(new Exchange(assignmentUser1, group2)); + Thread.sleep(1000); this.exchangeService.save(new Exchange(assignmentUser2, group1)); + Thread.sleep(1000); this.exchangeService.save(new Exchange(assignmentUser1, group3)); + Thread.sleep(1000); this.exchangeService.save(new Exchange(assignmentUser1, group4)); + Thread.sleep(1000); this.exchangeService.performExchange(); @@ -176,8 +184,7 @@ public class ExchangeServiceTest { assertTrue(listGroupsOfUser1.contains(group2.getId())); assertTrue(listGroupsOfUser2.contains(group1.getId())); - int size = this.exchangeService.getAllExchanges().size(); - assertTrue(this.exchangeService.getAllExchanges().size() == 1); + assertTrue(this.exchangeService.getAllExchanges().size() == 2); } @Test From 986407b8f8daa88f36db2b3915f36c715d7a7136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Wed, 13 Jan 2021 14:29:01 +0100 Subject: [PATCH 29/30] Checkpoint: Removed unused imports MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../src/main/java/com/plannaplan/entities/Exchange.java | 1 - .../src/main/java/com/plannaplan/models/MatchData.java | 2 -- .../main/java/com/plannaplan/services/ExchangeService.java | 3 --- .../java/com/plannaplan/services/ExchangeServiceTest.java | 4 ---- 4 files changed, 10 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java index 44a4d3f..3b60d8d 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Exchange.java @@ -1,6 +1,5 @@ package com.plannaplan.entities; -import java.sql.Date; import java.sql.Timestamp; import javax.persistence.Entity; diff --git a/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java b/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java index c3a8496..1ee5db5 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java +++ b/buisnesslogic/src/main/java/com/plannaplan/models/MatchData.java @@ -1,7 +1,5 @@ package com.plannaplan.models; -import java.sql.Timestamp; - import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Exchange; diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java index d3e9e8d..6d303ea 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java @@ -1,13 +1,10 @@ package com.plannaplan.services; -import java.sql.Timestamp; import java.util.ArrayList; -import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Optional; import java.util.Set; -import java.util.TreeSet; import java.util.stream.Collectors; import com.plannaplan.entities.Assignment; diff --git a/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java b/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java index d71dba7..b0577c5 100644 --- a/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java +++ b/buisnesslogic/src/test/java/com/plannaplan/services/ExchangeServiceTest.java @@ -3,11 +3,7 @@ package com.plannaplan.services; import static org.junit.Assert.assertTrue; import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; import java.util.List; -import java.util.Set; -import java.util.TreeSet; import java.util.stream.Collectors; import com.plannaplan.entities.Assignment; From 75a3bc46f26fca9c557b4df053cb9dddc2d5f62c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Wo=C5=BAniak?= Date: Wed, 13 Jan 2021 14:42:13 +0100 Subject: [PATCH 30/30] Checkpoint: Added performExchange into cron MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcin Woźniak --- .../java/com/plannaplan/services/EventService.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/EventService.java b/buisnesslogic/src/main/java/com/plannaplan/services/EventService.java index b70af1b..b6fb6c6 100755 --- a/buisnesslogic/src/main/java/com/plannaplan/services/EventService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/EventService.java @@ -6,6 +6,7 @@ import java.util.concurrent.ScheduledFuture; import javax.annotation.PostConstruct; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.support.CronTrigger; @@ -14,6 +15,9 @@ import org.springframework.stereotype.Service; @Service public class EventService { + @Autowired + private ExchangeService exchangeService; + public static final int FIRST_TOUR_SCHEDULE = 0; public static final int SECOND_TOUR_SCHEDULE = 1; @@ -25,9 +29,15 @@ public class EventService { System.out.println("Checking for groups"); } + @Scheduled(cron = "0 0 0 * * *") + public void performExchangeService() { + System.out.println("Performing Exchange"); + this.exchangeService.performExchange(); + } + /** * Schedule provided task to perform - * + * * @param taskId static filed of this class that represents to what event * we want to assign task * @param task runnable class that perform task in implemented run method