diff --git a/Poseiden-skeleton/doc/data.sql b/Poseiden-skeleton/doc/data.sql index 569bdeb432..a7d536ea33 100644 --- a/Poseiden-skeleton/doc/data.sql +++ b/Poseiden-skeleton/doc/data.sql @@ -1,3 +1,6 @@ +DROP database demo; +CREATE database demo; +USE demo; CREATE TABLE BidList ( BidListId tinyint(4) NOT NULL AUTO_INCREMENT, @@ -24,7 +27,7 @@ CREATE TABLE BidList ( side VARCHAR(125), PRIMARY KEY (BidListId) -) +); CREATE TABLE Trade ( TradeId tinyint(4) NOT NULL AUTO_INCREMENT, @@ -50,7 +53,7 @@ CREATE TABLE Trade ( side VARCHAR(125), PRIMARY KEY (TradeId) -) +); CREATE TABLE CurvePoint ( Id tinyint(4) NOT NULL AUTO_INCREMENT, @@ -61,7 +64,7 @@ CREATE TABLE CurvePoint ( creationDate TIMESTAMP , PRIMARY KEY (Id) -) +); CREATE TABLE Rating ( Id tinyint(4) NOT NULL AUTO_INCREMENT, @@ -71,7 +74,7 @@ CREATE TABLE Rating ( orderNumber tinyint, PRIMARY KEY (Id) -) +); CREATE TABLE RuleName ( Id tinyint(4) NOT NULL AUTO_INCREMENT, @@ -83,7 +86,7 @@ CREATE TABLE RuleName ( sqlPart VARCHAR(125), PRIMARY KEY (Id) -) +); CREATE TABLE Users ( Id tinyint(4) NOT NULL AUTO_INCREMENT, @@ -93,7 +96,7 @@ CREATE TABLE Users ( role VARCHAR(125), PRIMARY KEY (Id) -) +); -insert into Users(fullname, username, password, role) values("Administrator", "admin", "$2a$10$pBV8ILO/s/nao4wVnGLrh.sa/rnr5pDpbeC4E.KNzQWoy8obFZdaa", "ADMIN") -insert into Users(fullname, username, password, role) values("User", "user", "$2a$10$pBV8ILO/s/nao4wVnGLrh.sa/rnr5pDpbeC4E.KNzQWoy8obFZdaa", "USER") \ No newline at end of file +insert into Users(fullname, username, password, role) values("Administrator", "admin", "$2a$10$pBV8ILO/s/nao4wVnGLrh.sa/rnr5pDpbeC4E.KNzQWoy8obFZdaa", "ADMIN"); +insert into Users(fullname, username, password, role) values("User", "user", "$2a$10$pBV8ILO/s/nao4wVnGLrh.sa/rnr5pDpbeC4E.KNzQWoy8obFZdaa", "USER"); diff --git a/Poseiden-skeleton/pom.xml b/Poseiden-skeleton/pom.xml index a7dcbc04dc..9d3ec825fe 100644 --- a/Poseiden-skeleton/pom.xml +++ b/Poseiden-skeleton/pom.xml @@ -28,25 +28,27 @@ org.springframework.boot - spring-boot-starter-test - test + spring-boot-starter-oauth2-client + 2.6.4 org.springframework.boot - spring-boot-starter-data-jpa + spring-boot-starter-security + org.springframework.boot - spring-boot-starter-web + spring-boot-starter-data-jpa org.springframework.boot - spring-boot-starter-thymeleaf + spring-boot-starter-web org.springframework.boot - spring-boot-starter-security + spring-boot-starter-thymeleaf + org.springframework.boot spring-boot-devtools @@ -60,6 +62,32 @@ com.h2database h2 + + + javax.xml.bind + jaxb-api + + + org.javassist + javassist + 3.23.1-GA + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.security + spring-security-test + test + @@ -68,6 +96,25 @@ org.springframework.boot spring-boot-maven-plugin + + org.jacoco + jacoco-maven-plugin + 0.8.7 + + + + prepare-agent + + + + jacoco-report + test + + report + + + + diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/configurations/SpringSecurityConfig.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/configurations/SpringSecurityConfig.java new file mode 100644 index 0000000000..e719dd257e --- /dev/null +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/configurations/SpringSecurityConfig.java @@ -0,0 +1,60 @@ +package com.nnk.springboot.configurations; + +import javax.sql.DataSource; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; + +@Configuration +@EnableWebSecurity +public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { + + @Autowired + private DataSource dataSource; + + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } + + @Override + public void configure(HttpSecurity http) throws Exception { + http + .csrf().disable() + .authorizeRequests() + .antMatchers("/", "/login").permitAll() + .antMatchers("/css/**").permitAll() + .antMatchers("/user/**").hasAuthority("ADMIN") + .antMatchers("/app/**").hasAuthority("ADMIN") + .antMatchers("/admin/home").hasAuthority("ADMIN") + .anyRequest().authenticated() + .and() + .exceptionHandling().accessDeniedPage("/app/error") + .and() + .formLogin() + .defaultSuccessUrl("/bidList/list") + .and() + .oauth2Login() + .defaultSuccessUrl("/bidList/list"); + } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + + auth + .jdbcAuthentication() + .dataSource(dataSource) + .passwordEncoder(passwordEncoder()) + .usersByUsernameQuery( + "SELECT username as username, password as password, 1 FROM Users WHERE username =?") + .authoritiesByUsernameQuery( + "SELECT username, role FROM Users WHERE username=?"); + } +} diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/BidListController.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/BidListController.java index a31b9b53ca..6f059473c7 100644 --- a/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/BidListController.java +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/BidListController.java @@ -1,6 +1,11 @@ package com.nnk.springboot.controllers; import com.nnk.springboot.domain.BidList; + + +import com.nnk.springboot.services.BidListService; + +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; @@ -11,45 +16,92 @@ import javax.validation.Valid; - @Controller public class BidListController { - // TODO: Inject Bid service - - @RequestMapping("/bidList/list") - public String home(Model model) - { - // TODO: call service find all bids to show to the view - return "bidList/list"; - } - - @GetMapping("/bidList/add") - public String addBidForm(BidList bid) { - return "bidList/add"; - } - - @PostMapping("/bidList/validate") - public String validate(@Valid BidList bid, BindingResult result, Model model) { - // TODO: check data valid and save to db, after saving return bid list - return "bidList/add"; - } - - @GetMapping("/bidList/update/{id}") - public String showUpdateForm(@PathVariable("id") Integer id, Model model) { - // TODO: get Bid by Id and to model then show to the form - return "bidList/update"; - } - - @PostMapping("/bidList/update/{id}") - public String updateBid(@PathVariable("id") Integer id, @Valid BidList bidList, - BindingResult result, Model model) { - // TODO: check required fields, if valid call service to update Bid and return list Bid - return "redirect:/bidList/list"; - } - - @GetMapping("/bidList/delete/{id}") - public String deleteBid(@PathVariable("id") Integer id, Model model) { - // TODO: Find Bid by Id and delete the bid, return to Bid list - return "redirect:/bidList/list"; - } + + @Autowired + private BidListService bidListService; + + /** + * displays BidList list retrieved from database + */ + @RequestMapping("/bidList/list") + public String home(Model model) { + model.addAttribute("bidLists", bidListService.findAllBidLists()); + return "bidList/list"; + } + + /** + * displays form for new BidList to add + */ + @GetMapping("/bidList/add") + public String addBidForm(BidList bid) { + return "bidList/add"; + } + + /** + * validates new BidList + * + * @param bid: BidList to create and save + * @param result: form result to validate + * @return BidList list displayed if validated, new add form with errors + * displayed else + */ + @PostMapping("/bidList/validate") + public String validate(@Valid BidList bid, BindingResult result, Model model) { + if(!result.hasErrors()) { + bidListService.saveBidList(bid); + model.addAttribute("bidLists", bidListService.findAllBidLists()); + return "redirect:/bidList/list"; + } + return "/bidList/add"; + } + + /** + * displays BidList form to update + * + * @param id : id of the BidList to update + * @return return BidList form to update + */ + @GetMapping("/bidList/update/{id}") + public String showUpdateForm(@PathVariable("id") Integer id, Model model) { + BidList bidList = bidListService.findBidListById(id).orElseThrow(() -> new IllegalArgumentException("Invalid bidList Id:" + id)); + model.addAttribute("bidList", bidList); + return "bidList/update"; + } + + /** + * validates updated BidList + * + * @param id : id of the BidList to update + * @param bidList : BidList to validate + * @param result : form result to validate + * @return BidList list with this BidList updated, BidList to update form + * with errors displayed else + */ + @PostMapping("/bidList/update/{id}") + public String updateBid(@PathVariable("id") Integer id, @Valid BidList bidList, + BindingResult result, Model model) { + if(result.hasErrors()) { + return "/bidList/update"; + } + bidList.setBidListId(id); + bidListService.saveBidList(bidList); + model.addAttribute("bidLists", bidListService.findAllBidLists()); + return "redirect:/bidList/list"; + } + + /** + * deletes selected BidList from database + * + * @param id: id of the BidList to delete + * @return the BidList list after selected BidList deleted + */ + @GetMapping("/bidList/delete/{id}") + public String deleteBid(@PathVariable("id") Integer id, Model model) { + BidList bidList = bidListService.findBidListById(id).orElseThrow(() -> new IllegalArgumentException("Invalid bidList Id:" + id)); + bidListService.deleteBidList(bidList); + model.addAttribute("bidLists", bidListService.findAllBidLists()); + return "redirect:/bidList/list"; + } } diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/CurveController.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/CurveController.java deleted file mode 100644 index db69caf549..0000000000 --- a/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/CurveController.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.nnk.springboot.controllers; - -import com.nnk.springboot.domain.CurvePoint; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.validation.BindingResult; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; - -import javax.validation.Valid; - -@Controller -public class CurveController { - // TODO: Inject Curve Point service - - @RequestMapping("/curvePoint/list") - public String home(Model model) - { - // TODO: find all Curve Point, add to model - return "curvePoint/list"; - } - - @GetMapping("/curvePoint/add") - public String addBidForm(CurvePoint bid) { - return "curvePoint/add"; - } - - @PostMapping("/curvePoint/validate") - public String validate(@Valid CurvePoint curvePoint, BindingResult result, Model model) { - // TODO: check data valid and save to db, after saving return Curve list - return "curvePoint/add"; - } - - @GetMapping("/curvePoint/update/{id}") - public String showUpdateForm(@PathVariable("id") Integer id, Model model) { - // TODO: get CurvePoint by Id and to model then show to the form - return "curvePoint/update"; - } - - @PostMapping("/curvePoint/update/{id}") - public String updateBid(@PathVariable("id") Integer id, @Valid CurvePoint curvePoint, - BindingResult result, Model model) { - // TODO: check required fields, if valid call service to update Curve and return Curve list - return "redirect:/curvePoint/list"; - } - - @GetMapping("/curvePoint/delete/{id}") - public String deleteBid(@PathVariable("id") Integer id, Model model) { - // TODO: Find Curve by Id and delete the Curve, return to Curve list - return "redirect:/curvePoint/list"; - } -} diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/CurvePointController.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/CurvePointController.java new file mode 100644 index 0000000000..ad713dc481 --- /dev/null +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/CurvePointController.java @@ -0,0 +1,107 @@ +package com.nnk.springboot.controllers; + +import com.nnk.springboot.domain.CurvePoint; +import com.nnk.springboot.services.CurvePointService; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +import javax.validation.Valid; + +@Controller +public class CurvePointController { + @Autowired + private CurvePointService curvePointService; + + /** + * + * displays CurvePoint list retrieved from database + * + */ + @RequestMapping("/curvePoint/list") + public String home(Model model) + { + model.addAttribute("curvePoints", curvePointService.findAllCurvePoints()); + return "curvePoint/list"; + } + + /** + * displays CurvePoint to add + */ + @GetMapping("/curvePoint/add") + public String addBidForm(CurvePoint curvePoint) { + return "curvePoint/add"; + } + + /** + * validates new CurvePoint + * + * @param curvePoint: CurvePoint to create and save + * @param result: form result to validate + * @return CurvePoint list displayed if validated, new add form with errors + * displayed else + */ + @PostMapping("/curvePoint/validate") + public String validate(@Valid CurvePoint curvePoint, BindingResult result, Model model) { + if(!result.hasErrors()) { + curvePointService.saveCurvePoint(curvePoint); + model.addAttribute("curvePoints", curvePointService.findAllCurvePoints()); + return "redirect:/curvePoint/list"; + } + return "curvePoint/add"; + } + + /** + * displays CurvePoint form to update + * + * @param id : id of the CurvePoint to update + * @return return CurvePoint form to update + */ + @GetMapping("/curvePoint/update/{id}") + public String showUpdateForm(@PathVariable("id") Integer id, Model model) { + CurvePoint curvePoint = curvePointService.findCurvePointById(id).orElseThrow(() -> new IllegalArgumentException("Invalid curvePoint Id:" + id)); + model.addAttribute("curvePoint", curvePoint); + return "curvePoint/update"; + } + + /** + * validates updated CurvePoint + * + * @param id : id of the CurvePoint to update + * @param curvePoint : CurvePoint to validate + * @param result : form result to validate + * @return CurvePoint list with this CurvePoint updated, CurvePoint to + * update form with errors displayed else + */ + @PostMapping("/curvePoint/update/{id}") + public String updateBid(@PathVariable("id") Integer id, @Valid CurvePoint curvePoint, + BindingResult result, Model model) { + if(result.hasErrors()) { + return "/curvePoint/update"; + } + curvePoint.setId(id); + curvePointService.saveCurvePoint(curvePoint); + model.addAttribute("curvePoints", curvePointService.findAllCurvePoints()); + return "redirect:/curvePoint/list"; + } + + /** + * deletes selected CurvePoint from database + * + * @param id: id of the CurvePoint to delete + * @return the CurvePoint list after selected CurvePoint deleted + */ + @GetMapping("/curvePoint/delete/{id}") + public String deleteBid(@PathVariable("id") Integer id, Model model) { + CurvePoint curvePoint = curvePointService.findCurvePointById(id).orElseThrow(() -> new IllegalArgumentException("Invalid bidList Id:" + id)); + curvePointService.deleteCurvePoint(curvePoint); + model.addAttribute("curvePoints", curvePointService.findAllCurvePoints()); + return "redirect:/curvePoint/list"; + } +} diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/LoginController.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/LoginController.java index ef0c657c72..0eb2181cfa 100644 --- a/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/LoginController.java +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/LoginController.java @@ -1,6 +1,7 @@ package com.nnk.springboot.controllers; -import com.nnk.springboot.repositories.UserRepository; +import com.nnk.springboot.services.UserService; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @@ -12,7 +13,7 @@ public class LoginController { @Autowired - private UserRepository userRepository; + private UserService userService; @GetMapping("login") public ModelAndView login() { @@ -24,7 +25,7 @@ public ModelAndView login() { @GetMapping("secure/article-details") public ModelAndView getAllUserArticles() { ModelAndView mav = new ModelAndView(); - mav.addObject("users", userRepository.findAll()); + mav.addObject("users", userService.findAllUsers()); mav.setViewName("user/list"); return mav; } diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/RatingController.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/RatingController.java index 5e15e68fbc..c721b26c34 100644 --- a/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/RatingController.java +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/RatingController.java @@ -1,6 +1,9 @@ package com.nnk.springboot.controllers; import com.nnk.springboot.domain.Rating; +import com.nnk.springboot.services.RatingService; + +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; @@ -13,42 +16,90 @@ @Controller public class RatingController { - // TODO: Inject Rating service + @Autowired + private RatingService ratingService; + /** + * displays Rating list retrieved from database + */ @RequestMapping("/rating/list") - public String home(Model model) - { - // TODO: find all Rating, add to model + public String home(Model model) { + model.addAttribute("ratings", ratingService.findAllRatings()); return "rating/list"; } + /** + * displays form for new Rating to add + */ @GetMapping("/rating/add") public String addRatingForm(Rating rating) { return "rating/add"; } + /** + * validates new Rating + * + * @param rating: Rating to create and save + * @param result: form result to validate + * @return Rating list displayed if validated, new add form with errors + * displayed else + */ @PostMapping("/rating/validate") public String validate(@Valid Rating rating, BindingResult result, Model model) { - // TODO: check data valid and save to db, after saving return Rating list + if(!result.hasErrors()) { + ratingService.saveRating(rating); + model.addAttribute("ratings", ratingService.findAllRatings()); + return "redirect:/rating/list"; + } return "rating/add"; } + /** + * displays Rating form to update + * + * @param id : id of the Rating to update + * @return return Rating form to update + */ @GetMapping("/rating/update/{id}") public String showUpdateForm(@PathVariable("id") Integer id, Model model) { - // TODO: get Rating by Id and to model then show to the form + Rating rating = ratingService.findRatingById(id).orElseThrow(() -> new IllegalArgumentException("Invalid rating Id:" + id)); + model.addAttribute("rating", rating); return "rating/update"; } + + /** + * validates updated Rating + * + * @param id : id of the Rating to update + * @param rating : Rating to validate + * @param result : form result to validate + * @return Rating list with this Rating updated, Rating to update form + * with errors displayed else + */ @PostMapping("/rating/update/{id}") public String updateRating(@PathVariable("id") Integer id, @Valid Rating rating, BindingResult result, Model model) { - // TODO: check required fields, if valid call service to update Rating and return Rating list + if(result.hasErrors()) { + return "/rating/update"; + } + rating.setId(id); + ratingService.saveRating(rating); + model.addAttribute("ratings", ratingService.findAllRatings()); return "redirect:/rating/list"; } + /** + * deletes selected Rating from database + * + * @param id: id of the Rating to delete + * @return the Rating list after selected Rating deleted + */ @GetMapping("/rating/delete/{id}") public String deleteRating(@PathVariable("id") Integer id, Model model) { - // TODO: Find Rating by Id and delete the Rating, return to Rating list + Rating rating = ratingService.findRatingById(id).orElseThrow(() -> new IllegalArgumentException("Invalid rating Id:" + id)); + ratingService.deleteRating(rating); + model.addAttribute("ratings", ratingService.findAllRatings()); return "redirect:/rating/list"; } } diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/RuleNameController.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/RuleNameController.java index b9e72b1ba6..74ed599014 100644 --- a/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/RuleNameController.java +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/RuleNameController.java @@ -1,6 +1,9 @@ package com.nnk.springboot.controllers; import com.nnk.springboot.domain.RuleName; +import com.nnk.springboot.services.RuleNameService; + +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; @@ -13,42 +16,91 @@ @Controller public class RuleNameController { - // TODO: Inject RuleName service - - @RequestMapping("/ruleName/list") - public String home(Model model) - { - // TODO: find all RuleName, add to model - return "ruleName/list"; - } - - @GetMapping("/ruleName/add") - public String addRuleForm(RuleName bid) { - return "ruleName/add"; - } - - @PostMapping("/ruleName/validate") - public String validate(@Valid RuleName ruleName, BindingResult result, Model model) { - // TODO: check data valid and save to db, after saving return RuleName list - return "ruleName/add"; - } - - @GetMapping("/ruleName/update/{id}") - public String showUpdateForm(@PathVariable("id") Integer id, Model model) { - // TODO: get RuleName by Id and to model then show to the form - return "ruleName/update"; - } - - @PostMapping("/ruleName/update/{id}") - public String updateRuleName(@PathVariable("id") Integer id, @Valid RuleName ruleName, - BindingResult result, Model model) { - // TODO: check required fields, if valid call service to update RuleName and return RuleName list - return "redirect:/ruleName/list"; - } - - @GetMapping("/ruleName/delete/{id}") - public String deleteRuleName(@PathVariable("id") Integer id, Model model) { - // TODO: Find RuleName by Id and delete the RuleName, return to Rule list - return "redirect:/ruleName/list"; - } + @Autowired + private RuleNameService ruleNameService; + + /** + * displays RuleName list retrieved from database + */ + @RequestMapping("/ruleName/list") + public String home(Model model) { + model.addAttribute("ruleNames", ruleNameService.findAllRuleNames()); + return "ruleName/list"; + } + + /** + * displays RuleName to add + */ + @GetMapping("/ruleName/add") + public String addRuleForm(RuleName bid) { + return "ruleName/add"; + } + + /** + * validates new RuleName + * + * @param ruleName: RuleName to create and save + * @param result: form result to validate + * @return RuleName list displayed if validated, new add form with errors + * displayed else + */ + @PostMapping("/ruleName/validate") + public String validate(@Valid RuleName ruleName, BindingResult result, Model model) { + if (!result.hasErrors()) { + ruleNameService.saveRuleName(ruleName); + model.addAttribute("ruleNames", ruleNameService.findAllRuleNames()); + return "redirect:/ruleName/list"; + } + return "ruleName/add"; + } + + /** + * displays RuleName form to update + * + * @param id : id of the RuleName to update + * @return return RuleName form to update + */ + @GetMapping("/ruleName/update/{id}") + public String showUpdateForm(@PathVariable("id") Integer id, Model model) { + RuleName ruleName = ruleNameService.findRuleNameById(id) + .orElseThrow(() -> new IllegalArgumentException("Invalid ruleName Id:" + id)); + model.addAttribute("ruleName", ruleName); + return "ruleName/update"; + } + + /** + * validates updated RuleName + * + * @param id : id of the RuleName to update + * @param rulName : RuleName to validate + * @param result : form result to validate + * @return RuleName list with this RuleName updated, RuleName to update form + * with errors displayed else + */ + @PostMapping("/ruleName/update/{id}") + public String updateRuleName(@PathVariable("id") Integer id, @Valid RuleName ruleName, + BindingResult result, Model model) { + if (result.hasErrors()) { + return "/ruleName/update"; + } + ruleName.setId(id); + ruleNameService.saveRuleName(ruleName); + model.addAttribute("ruleNames", ruleNameService.findAllRuleNames()); + return "redirect:/ruleName/list"; + } + + /** + * deletes selected RuleName from database + * + * @param id: id of the RuleName to delete + * @return the RuleName list after selected RuleName deleted + */ + @GetMapping("/ruleName/delete/{id}") + public String deleteRuleName(@PathVariable("id") Integer id, Model model) { + RuleName ruleName = ruleNameService.findRuleNameById(id) + .orElseThrow(() -> new IllegalArgumentException("Invalid ruleName Id:" + id)); + ruleNameService.deleteRuleName(ruleName); + model.addAttribute("ruleNames", ruleNameService.findAllRuleNames()); + return "redirect:/ruleName/list"; + } } diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/TradeController.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/TradeController.java index 4e667eec22..0d1b82421f 100644 --- a/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/TradeController.java +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/TradeController.java @@ -1,6 +1,9 @@ package com.nnk.springboot.controllers; import com.nnk.springboot.domain.Trade; +import com.nnk.springboot.services.TradeService; + +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; @@ -13,42 +16,89 @@ @Controller public class TradeController { - // TODO: Inject Trade service + @Autowired + private TradeService tradeService; + /** + * displays Trade list retrieved from database + */ @RequestMapping("/trade/list") - public String home(Model model) - { - // TODO: find all Trade, add to model + public String home(Model model) { + model.addAttribute("trades", tradeService.findAllTrades()); return "trade/list"; } + /** + * displays form for Trade to add + */ @GetMapping("/trade/add") public String addUser(Trade bid) { return "trade/add"; } + /** + * validates new Trade + * + * @param trade: Trade to create and save + * @param result: form result to validate + * @return Trade list displayed if validated, new add form with errors + * displayed else + */ @PostMapping("/trade/validate") public String validate(@Valid Trade trade, BindingResult result, Model model) { - // TODO: check data valid and save to db, after saving return Trade list + if(!result.hasErrors()) { + tradeService.saveTrade(trade); + model.addAttribute("trades", tradeService.findAllTrades()); + return "redirect:/trade/list"; + } return "trade/add"; } + /** + * displays Trade form to update + * + * @param id : id of the Trade to update + * @return return Trade form to update + */ @GetMapping("/trade/update/{id}") public String showUpdateForm(@PathVariable("id") Integer id, Model model) { - // TODO: get Trade by Id and to model then show to the form + Trade trade = tradeService.findTradeById(id).orElseThrow(() -> new IllegalArgumentException("Invalid trade Id:" + id)); + model.addAttribute("trade", trade); return "trade/update"; } + /** + * validates updated Trade + * + * @param id : id of the Trade to update + * @param trade : Trade to validate + * @param result : form result to validate + * @return Trade list with this Trade updated, Trade to update form + * with errors displayed else + */ @PostMapping("/trade/update/{id}") public String updateTrade(@PathVariable("id") Integer id, @Valid Trade trade, BindingResult result, Model model) { - // TODO: check required fields, if valid call service to update Trade and return Trade list + if(result.hasErrors()) { + return "trade/update"; + } + trade.setTradeId(id); + tradeService.saveTrade(trade); + model.addAttribute("trades", tradeService.findAllTrades()); return "redirect:/trade/list"; } + /** + * deletes selected Trade from database + * + * @param id: id of the Trade to delete + * @return the Trade list after selected Trade deleted + */ @GetMapping("/trade/delete/{id}") public String deleteTrade(@PathVariable("id") Integer id, Model model) { - // TODO: Find Trade by Id and delete the Trade, return to Trade list + Trade trade = tradeService.findTradeById(id).orElseThrow(() -> new IllegalArgumentException("Invalid trade Id:" + id)); + tradeService.deleteTrade(trade); + model.addAttribute("trades", tradeService.findAllTrades()); return "redirect:/trade/list"; } } diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/UserController.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/UserController.java index 29e30be3d6..a8d76b490c 100644 --- a/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/UserController.java +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/controllers/UserController.java @@ -1,7 +1,9 @@ package com.nnk.springboot.controllers; import com.nnk.springboot.domain.User; -import com.nnk.springboot.repositories.UserRepository; + +import com.nnk.springboot.services.UserService; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Controller; @@ -17,12 +19,13 @@ @Controller public class UserController { @Autowired - private UserRepository userRepository; + private UserService userService; + @RequestMapping("/user/list") public String home(Model model) { - model.addAttribute("users", userRepository.findAll()); + model.addAttribute("users", userService.findAllUsers()); return "user/list"; } @@ -36,8 +39,8 @@ public String validate(@Valid User user, BindingResult result, Model model) { if (!result.hasErrors()) { BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); user.setPassword(encoder.encode(user.getPassword())); - userRepository.save(user); - model.addAttribute("users", userRepository.findAll()); + userService.saveUser(user); + model.addAttribute("users", userService.findAllUsers()); return "redirect:/user/list"; } return "user/add"; @@ -45,7 +48,7 @@ public String validate(@Valid User user, BindingResult result, Model model) { @GetMapping("/user/update/{id}") public String showUpdateForm(@PathVariable("id") Integer id, Model model) { - User user = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id)); + User user = userService.findUserById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id)); user.setPassword(""); model.addAttribute("user", user); return "user/update"; @@ -61,16 +64,16 @@ public String updateUser(@PathVariable("id") Integer id, @Valid User user, BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); user.setPassword(encoder.encode(user.getPassword())); user.setId(id); - userRepository.save(user); - model.addAttribute("users", userRepository.findAll()); + userService.saveUser(user); + model.addAttribute("users", userService.findAllUsers()); return "redirect:/user/list"; } @GetMapping("/user/delete/{id}") public String deleteUser(@PathVariable("id") Integer id, Model model) { - User user = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id)); - userRepository.delete(user); - model.addAttribute("users", userRepository.findAll()); + User user = userService.findUserById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id)); + userService.deleteUser(user); + model.addAttribute("users", userService.findAllUsers()); return "redirect:/user/list"; } } diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/BidList.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/BidList.java index 3a0e27efc8..a77e03f08e 100644 --- a/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/BidList.java +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/BidList.java @@ -1,15 +1,308 @@ package com.nnk.springboot.domain; -import org.springframework.beans.factory.annotation.Required; +import org.hibernate.validator.constraints.Length; import javax.persistence.*; import javax.validation.constraints.Digits; import javax.validation.constraints.NotBlank; -import java.sql.Date; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Past; + import java.sql.Timestamp; @Entity -@Table(name = "bidlist") +@Table(name = "BidList") public class BidList { - // TODO: Map columns in data table BIDLIST with corresponding java fields + // TODO: Map columns in data table BIDLIST with corresponding java fields + @Id + @Digits(integer = 4, fraction = 0) + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "BidListId") + private Integer BidListId; + + @NotBlank(message = "Account is mandatory") + @Length(max = 30, message = "must have 30 characters max") + @Column(name = "account") + private String account; + + @NotBlank(message = "Type is mandatory") + @Length(max = 30, message = "must have 30 characters max") + @Column(name = "type") + private String type; + + @Column(name = "bidQuantity") + @NotNull(message = "must not be null") + @Digits(integer = 4, fraction = 2, message = "must be in the form 0000.00") + private Double bidQuantity; + + @Column(name = "askQuantity") + @Digits(integer = 4, fraction = 2) + private Double askQuantity; + + @Column(name = "bid") + @Digits(integer = 4, fraction = 2) + private Double bid; + + @Column(name = "ask") + @Digits(integer = 4, fraction = 2) + private Double ask; + + @Column(name = "benchmark") + @Length(max = 125) + private String benchmark; + + @Column(name = "bidListDate") + @Past + private Timestamp bidListDate; + + @Column(name = "commentary") + @Length(max = 125) + private String commentary; + + @Column(name = "security") + @Length(max = 125) + private String security; + + @Column(name = "status") + @Length(max = 10) + private String status; + + @Column(name = "trader") + @Length(max = 125) + private String trader; + + @Column(name = "book") + @Length(max = 125) + private String book; + + @Column(name = "creationName") + @Length(max = 125) + private String creationName; + + @Column(name = "creationDate") + @Past + private Timestamp creationDate; + + @Column(name = "revisionName") + @Length(max = 125) + private String revisionName; + + @Column(name = "revisionDate") + @Past + private Timestamp revisionDate; + + @Column(name = "dealName") + @Length(max = 125) + private String dealName; + + @Column(name = "dealType") + @Length(max = 125) + private String dealType; + + @Column(name = "sourceListId") + @Length(max = 125) + private String sourceListId; + + @Column(name = "side") + @Length(max = 125) + private String side; + + public Integer getBidListId() { + return BidListId; + } + + public void setBidListId(Integer bidListId) { + BidListId = bidListId; + } + + public String getAccount() { + return account; + } + + public void setAccount(String account) { + this.account = account; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public Double getBidQuantity() { + return bidQuantity; + } + + public void setBidQuantity(Double bidQuantity) { + if (bidQuantity > 0) { + this.bidQuantity = bidQuantity; + } else { + this.bidQuantity = null; + } + } + + public Double getAskQuantity() { + return askQuantity; + } + + public void setAskQuantity(Double askQuantity) { + this.askQuantity = askQuantity; + } + + public Double getBid() { + return bid; + } + + public void setBid(Double bid) { + this.bid = bid; + } + + public Double getAsk() { + return ask; + } + + public void setAsk(Double ask) { + this.ask = ask; + } + + public String getBenchmark() { + return benchmark; + } + + public void setBenchmark(String benchmark) { + this.benchmark = benchmark; + } + + public Timestamp getBidListDate() { + return bidListDate; + } + + public void setBidListDate(Timestamp bidListDate) { + this.bidListDate = bidListDate; + } + + public String getCommentary() { + return commentary; + } + + public void setCommentary(String commentary) { + this.commentary = commentary; + } + + public String getSecurity() { + return security; + } + + public void setSecurity(String security) { + this.security = security; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getTrader() { + return trader; + } + + public void setTrader(String trader) { + this.trader = trader; + } + + public String getBook() { + return book; + } + + public void setBook(String book) { + this.book = book; + } + + public String getCreationName() { + return creationName; + } + + public void setCreationName(String creationName) { + this.creationName = creationName; + } + + public Timestamp getCreationDate() { + return creationDate; + } + + public void setCreationDate(Timestamp creationDate) { + this.creationDate = creationDate; + } + + public String getRevisionName() { + return revisionName; + } + + public void setRevisionName(String revisionName) { + this.revisionName = revisionName; + } + + public Timestamp getRevisionDate() { + return revisionDate; + } + + public void setRevisionDate(Timestamp revisionDate) { + this.revisionDate = revisionDate; + } + + public String getDealName() { + return dealName; + } + + public void setDealName(String dealName) { + this.dealName = dealName; + } + + public String getDealType() { + return dealType; + } + + public void setDealType(String dealType) { + this.dealType = dealType; + } + + public String getSourceListId() { + return sourceListId; + } + + public void setSourceListId(String sourceListId) { + this.sourceListId = sourceListId; + } + + public String getSide() { + return side; + } + + public void setSide(String side) { + this.side = side; + } + + public BidList(String account, String type, Double bidQuantity) { + super(); + this.account = account; + this.type = type; + this.bidQuantity = bidQuantity; + } + + public BidList() { + super(); + // TODO Auto-generated constructor stub + } + + @Override + public String toString() { + return "BidList [account=" + account + ", type=" + type + ", bidQuantity=" + + bidQuantity + "]"; + } + } diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/CurvePoint.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/CurvePoint.java index 151f80d02f..75fedaf371 100644 --- a/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/CurvePoint.java +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/CurvePoint.java @@ -1,15 +1,123 @@ package com.nnk.springboot.domain; -import org.hibernate.validator.constraints.Length; +import org.hibernate.annotations.CreationTimestamp; import javax.persistence.*; -import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Digits; import javax.validation.constraints.NotNull; -import java.sql.Timestamp; +import javax.validation.constraints.Past; +import java.sql.Timestamp; @Entity -@Table(name = "curvepoint") +@Table(name = "CurvePoint") public class CurvePoint { - // TODO: Map columns in data table CURVEPOINT with corresponding java fields -} + // TODO: Map columns in data table CURVEPOINT with corresponding java fields + @Id + @Digits(integer = 4, fraction = 0) + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "Id") + private Integer id; + + @NotNull(message = "must not be null") + @Digits(integer = 4, fraction = 0, message = "must be an integer number") + @Column(name = "CurveId") + private Integer curveId; + + @Column(name = "asOfDate") + @Past + private Timestamp asOfDate; + + @NotNull(message = "must not be null") + @Column(name = "term") + @Digits(integer = 4, fraction = 2, message = "must be in the form 0000.00") + private Double term; + + @NotNull(message = "must not be null") + @Column(name = "value") + @Digits(integer = 4, fraction = 2, message = "must be in the form 0000.00") + private Double value; + + @CreationTimestamp + @Past + @Column(name = "creationDate") + private Timestamp creationDate; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getCurveId() { + return curveId; + } + + public void setCurveId(Integer curveId) { + if (curveId > 0) { + this.curveId = curveId; + } else { + this.curveId = null; + } + } + + public Timestamp getAsOfDate() { + return asOfDate; + } + + public void setAsOfDate(Timestamp asOfDate) { + this.asOfDate = asOfDate; + } + + public Double getTerm() { + return term; + } + + public void setTerm(Double term) { + if (term > 0) { + this.term = term; + } else { + this.term = null; + } + } + + public Double getValue() { + return value; + } + + public void setValue(Double value) { + if (value > 0) { + this.value = value; + } else { + this.value = null; + } + } + + public Timestamp getCreationDate() { + return creationDate; + } + + public void setCreationDate(Timestamp creationDate) { + this.creationDate = creationDate; + } + + public CurvePoint(Integer curveId, Double term, Double value) { + super(); + this.curveId = curveId; + this.term = term; + this.value = value; + } + + public CurvePoint() { + super(); + // TODO Auto-generated constructor stub + } + + @Override + public String toString() { + return "CurvePoint [curveId=" + curveId + ", term=" + term + ", value=" + value + "]"; + } + +} \ No newline at end of file diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/Rating.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/Rating.java index 12d1be58c0..3f2fdf04f7 100644 --- a/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/Rating.java +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/Rating.java @@ -1,12 +1,97 @@ package com.nnk.springboot.domain; import javax.persistence.*; + +import javax.validation.constraints.Digits; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; -import java.sql.Timestamp; @Entity -@Table(name = "rating") +@Table(name = "Rating") public class Rating { - // TODO: Map columns in data table RATING with corresponding java fields + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Digits(integer = 4, fraction = 0) + @Column(name = "Id") + private Integer id; + + @NotBlank(message = "MoodysRating is mandatory") + @Column(name = "moodysRating") + private String moodysRating; + + @NotBlank(message = "SandRating is mandatory") + @Column(name = "sandPRating") + private String sandPRating; + + @NotBlank(message = "FitchRating is mandatory") + @Column(name = "fitchRating") + private String fitchRating; + + @NotNull(message = "must not be null") + @Column(name = "orderNumber") + private Integer orderNumber; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getMoodysRating() { + return moodysRating; + } + + public void setMoodysRating(String moodysRating) { + this.moodysRating = moodysRating; + } + + public String getSandPRating() { + return sandPRating; + } + + public void setSandPRating(String sandPRating) { + this.sandPRating = sandPRating; + } + + public String getFitchRating() { + return fitchRating; + } + + public void setFitchRating(String fitchRating) { + this.fitchRating = fitchRating; + } + + public Integer getOrderNumber() { + return orderNumber; + } + + public void setOrderNumber(Integer orderNumber) { + if (orderNumber > 0) { + this.orderNumber = orderNumber; + } else { + this.orderNumber = null; + } + } + + public Rating(String moodysRating, String sandPRating, String fitchRating, Integer orderNumber) { + super(); + this.moodysRating = moodysRating; + this.sandPRating = sandPRating; + this.fitchRating = fitchRating; + this.orderNumber = orderNumber; + } + + public Rating() { + super(); + // TODO Auto-generated constructor stub + } + + @Override + public String toString() { + return "Rating [moodysRating=" + moodysRating + ", sandPRating=" + sandPRating + ", fitchRating=" + fitchRating + + ", orderNumber=" + orderNumber + "]"; + } + } diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/RuleName.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/RuleName.java index b8ac970edf..26c63b1fef 100644 --- a/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/RuleName.java +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/RuleName.java @@ -1,11 +1,120 @@ package com.nnk.springboot.domain; import javax.persistence.*; +import javax.validation.constraints.Digits; import javax.validation.constraints.NotBlank; import java.sql.Timestamp; @Entity -@Table(name = "rulename") +@Table(name = "RuleName") public class RuleName { // TODO: Map columns in data table RULENAME with corresponding java fields + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Digits(integer = 4, fraction = 0) + @Column(name = "Id") + private Integer id; + + @NotBlank(message = "Name is mandatory") + @Column(name = "name") + private String name; + + @NotBlank(message = "Description is mandatory") + @Column(name = "description") + private String description; + + @NotBlank(message = "json is mandatory") + @Column(name = "json") + private String json; + + @NotBlank(message = "template is mandatory") + @Column(name = "template") + private String template; + + @NotBlank(message = "sql is mandatory") + @Column(name = "sqlStr") + private String sqlStr; + + @NotBlank(message = "sqlPart is mandatory") + @Column(name = "sqlPart") + private String sqlPart; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getJson() { + return json; + } + + public void setJson(String json) { + this.json = json; + } + + public String getTemplate() { + return template; + } + + public void setTemplate(String template) { + this.template = template; + } + + public String getSqlStr() { + return sqlStr; + } + + public void setSqlStr(String sqlStr) { + this.sqlStr = sqlStr; + } + + public String getSqlPart() { + return sqlPart; + } + + public void setSqlPart(String sqlPart) { + this.sqlPart = sqlPart; + } + + public RuleName(String name, String description, String json, String template, String sqlStr, + String sqlPart) { + super(); + this.name = name; + this.description = description; + this.json = json; + this.template = template; + this.sqlStr = sqlStr; + this.sqlPart = sqlPart; + } + + public RuleName() { + super(); + // TODO Auto-generated constructor stub + } + + @Override + public String toString() { + return "RuleName [name=" + name + ", description=" + description + ", json=" + json + ", template=" + template + + ", sqlStr=" + sqlStr + ", sqlPart=" + sqlPart + "]"; + } + } diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/Trade.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/Trade.java index b6db7c13b7..7699c232b0 100644 --- a/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/Trade.java +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/Trade.java @@ -1,12 +1,290 @@ package com.nnk.springboot.domain; import javax.persistence.*; +import javax.validation.constraints.Digits; import javax.validation.constraints.NotBlank; -import java.sql.Timestamp; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Past; + +import org.hibernate.validator.constraints.Length; +import org.springframework.dao.DataAccessResourceFailureException; +import java.sql.Timestamp; @Entity -@Table(name = "trade") +@Table(name = "Trade") public class Trade { - // TODO: Map columns in data table TRADE with corresponding java fields + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Digits(integer = 4, fraction = 0) + @Column(name = "tradeId") + private Integer tradeId; + + @NotBlank(message = "Account is mandatory") + @Length(max = 30, message = "must have 30 characters max") + @Column(name = "account") + private String account; + + @NotBlank(message = "Type is mandatory") + @Length(max = 30, message = "must have 30 characters max") + @Column(name = "type") + private String type; + + @NotNull(message = "must not be null") + @Digits(integer = 4, fraction = 2, message = "must be in the form 0000.00") + @Column(name = "buyQuantity") + private Double buyQuantity; + + @Column(name = "sellQuantity") + private Double sellQuantity; + + @Column(name = "buyPrice") + private Double buyPrice; + + @Column(name = "sellPrice") + private Double sellPrice; + + @Past + @Column(name = "tradeDate") + private Timestamp tradeDate; + + @Length(max = 125) + @Column(name = "security") + private String security; + + @Length(max = 10) + @Column(name = "status") + private String status; + + @Length(max = 125) + @Column(name = "trader") + private String trader; + + @Length(max = 125) + @Column(name = "benchmark") + private String benchmark; + + @Length(max = 125) + @Column(name = "book") + private String book; + + @Length(max = 125) + @Column(name = "creationName") + private String creationName; + + @Past + @Column(name = "creationDate") + private Timestamp creationDate; + + @Length(max = 125) + @Column(name = "revisionName") + private String revisionName; + + @Past + @Column(name = "revisionDate") + private Timestamp revisionDate; + + @Length(max = 125) + @Column(name = "dealName") + private String dealName; + + @Length(max = 125) + @Column(name = "dealType") + private String dealType; + + @Length(max = 125) + @Column(name = "sourceListId") + private String sourceListId; + + @Length(max = 125) + @Column(name = "side") + private String side; + + public Integer getTradeId() { + return tradeId; + } + + public void setTradeId(Integer tradeId) { + this.tradeId = tradeId; + } + + public String getAccount() { + return account; + } + + public void setAccount(String account) { + this.account = account; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public Double getBuyQuantity() { + return buyQuantity; + } + + public void setBuyQuantity(Double buyQuantity) { + if (buyQuantity > 0) { + this.buyQuantity = buyQuantity; + } + } + + public Double getSellQuantity() { + return sellQuantity; + } + + public void setSellQuantity(Double sellQuantity) { + this.sellQuantity = sellQuantity; + } + + public Double getBuyPrice() { + return buyPrice; + } + + public void setBuyPrice(Double buyPrice) { + this.buyPrice = buyPrice; + } + + public Double getSellPrice() { + return sellPrice; + } + + public void setSellPrice(Double sellPrice) { + this.sellPrice = sellPrice; + } + + public Timestamp getTradeDate() { + return tradeDate; + } + + public void setTradeDate(Timestamp tradeDate) { + this.tradeDate = tradeDate; + } + + public String getSecurity() { + return security; + } + + public void setSecurity(String security) { + this.security = security; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getTrader() { + return trader; + } + + public void setTrader(String trader) { + this.trader = trader; + } + + public String getBenchmark() { + return benchmark; + } + + public void setBenchmark(String benchmark) { + this.benchmark = benchmark; + } + + public String getBook() { + return book; + } + + public void setBook(String book) { + this.book = book; + } + + public String getCreationName() { + return creationName; + } + + public void setCreationName(String creationName) { + this.creationName = creationName; + } + + public Timestamp getCreationDate() { + return creationDate; + } + + public void setCreationDate(Timestamp creationDate) { + this.creationDate = creationDate; + } + + public String getRevisionName() { + return revisionName; + } + + public void setRevisionName(String revisionName) { + this.revisionName = revisionName; + } + + public Timestamp getRevisionDate() { + return revisionDate; + } + + public void setRevisionDate(Timestamp revisionDate) { + this.revisionDate = revisionDate; + } + + public String getDealName() { + return dealName; + } + + public void setDealName(String dealName) { + this.dealName = dealName; + } + + public String getDealType() { + return dealType; + } + + public void setDealType(String dealType) { + this.dealType = dealType; + } + + public String getSourceListId() { + return sourceListId; + } + + public void setSourceListId(String sourceListId) { + this.sourceListId = sourceListId; + } + + public String getSide() { + return side; + } + + public void setSide(String side) { + this.side = side; + } + + public Trade(String account, String type, Double buyQuantity) { + super(); + this.account = account; + this.type = type; + this.buyQuantity = buyQuantity; + } + + public Trade() { + super(); + // TODO Auto-generated constructor stub + } + + @Override + public String toString() { + return "Trade [account=" + account + ", type=" + type + ", buy quantity=" + buyQuantity + "]"; + } + } diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/User.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/User.java index 2be0b8c4ab..5a1a5d5883 100644 --- a/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/User.java +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/domain/User.java @@ -2,20 +2,33 @@ import javax.persistence.*; import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Pattern; + +import org.hibernate.validator.constraints.Length; @Entity -@Table(name = "users") +@Table(name = "Users") public class User { @Id @GeneratedValue(strategy= GenerationType.AUTO) + @Column(name = "Id") private Integer id; @NotBlank(message = "Username is mandatory") + @Length(max = 125, message = "must have 125 caracters max") + @Column(name = "username") private String username; @NotBlank(message = "Password is mandatory") + @Pattern(regexp = "(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$", message = "must contain at least 8 characters with a lowercase, an uppercase, a number and a special character (#?!@$%^&*-)") + @Length(max = 125, message = "must have 125 caracters max") + @Column(name = "password") private String password; @NotBlank(message = "FullName is mandatory") + @Length(max = 125, message = "must have 125 caracters max") + @Column(name = "fullname") private String fullname; @NotBlank(message = "Role is mandatory") + @Length(max = 125, message = "must have 125 caracters max") + @Column(name = "role") private String role; public Integer getId() { @@ -57,4 +70,28 @@ public String getRole() { public void setRole(String role) { this.role = role; } + + public User( + @NotBlank(message = "Username is mandatory") @Length(max = 125, message = "must have 125 caracters max") String username, + @NotBlank(message = "Password is mandatory") @Length(max = 125, message = "must have 125 caracters max") String password, + @NotBlank(message = "FullName is mandatory") @Length(max = 125, message = "must have 125 caracters max") String fullname, + @NotBlank(message = "Role is mandatory") @Length(max = 125, message = "must have 125 caracters max") String role) { + super(); + this.username = username; + this.password = password; + this.fullname = fullname; + this.role = role; + } + + public User() { + super(); + // TODO Auto-generated constructor stub + } + + @Override + public String toString() { + return "User [username=" + username + ", password=" + password + ", fullname=" + fullname + ", role=" + role + + "]"; + } + } diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/BidListRepository.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/BidListRepository.java index f74b94e51d..1fdf3e743b 100644 --- a/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/BidListRepository.java +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/BidListRepository.java @@ -1,9 +1,9 @@ package com.nnk.springboot.repositories; import com.nnk.springboot.domain.BidList; -import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.repository.CrudRepository; -public interface BidListRepository extends JpaRepository { +public interface BidListRepository extends CrudRepository { } diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/CurvePointRepository.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/CurvePointRepository.java index b01751b53e..bac9c0f983 100644 --- a/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/CurvePointRepository.java +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/CurvePointRepository.java @@ -1,9 +1,9 @@ package com.nnk.springboot.repositories; import com.nnk.springboot.domain.CurvePoint; -import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.repository.CrudRepository; -public interface CurvePointRepository extends JpaRepository { +public interface CurvePointRepository extends CrudRepository { } diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/RatingRepository.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/RatingRepository.java index 7ded405e6d..85e14ef4b8 100644 --- a/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/RatingRepository.java +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/RatingRepository.java @@ -1,8 +1,8 @@ package com.nnk.springboot.repositories; import com.nnk.springboot.domain.Rating; -import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.repository.CrudRepository; -public interface RatingRepository extends JpaRepository { +public interface RatingRepository extends CrudRepository { } diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/RuleNameRepository.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/RuleNameRepository.java index 8053d1612e..6aa44db739 100644 --- a/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/RuleNameRepository.java +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/RuleNameRepository.java @@ -1,8 +1,8 @@ package com.nnk.springboot.repositories; import com.nnk.springboot.domain.RuleName; -import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.repository.CrudRepository; -public interface RuleNameRepository extends JpaRepository { +public interface RuleNameRepository extends CrudRepository { } diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/TradeRepository.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/TradeRepository.java index e8da38af02..bc80bc624e 100644 --- a/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/TradeRepository.java +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/TradeRepository.java @@ -1,8 +1,8 @@ package com.nnk.springboot.repositories; import com.nnk.springboot.domain.Trade; -import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.repository.CrudRepository; -public interface TradeRepository extends JpaRepository { +public interface TradeRepository extends CrudRepository { } diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/UserRepository.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/UserRepository.java index b6a3363949..33975a332e 100644 --- a/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/UserRepository.java +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/repositories/UserRepository.java @@ -1,12 +1,10 @@ package com.nnk.springboot.repositories; import com.nnk.springboot.domain.User; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.JpaSpecificationExecutor; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.Param; +import org.springframework.data.repository.CrudRepository; -public interface UserRepository extends JpaRepository, JpaSpecificationExecutor { + +public interface UserRepository extends CrudRepository { } diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/services/BidListService.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/services/BidListService.java new file mode 100644 index 0000000000..1d330659ad --- /dev/null +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/services/BidListService.java @@ -0,0 +1,39 @@ +package com.nnk.springboot.services; + +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.nnk.springboot.domain.BidList; +import com.nnk.springboot.repositories.BidListRepository; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Service +public class BidListService { + + @Autowired + private BidListRepository bidListRepository; + + public Iterable findAllBidLists() { + log.info("All BidLists retrieved from database"); + return bidListRepository.findAll(); + } + + public Optional findBidListById(Integer id) { + log.info("BidList with id: " + id + " retrieved from database"); + return bidListRepository.findById(id); + } + + public BidList saveBidList(BidList bidList) { + log.info("BidList: " + bidList.toString() + " saved in database"); + return bidListRepository.save(bidList); + } + + public void deleteBidList(BidList bidList) { + log.info("BidList: " + bidList.toString() + " deleted in database"); + bidListRepository.delete(bidList); + } +} diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/services/CurvePointService.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/services/CurvePointService.java new file mode 100644 index 0000000000..6a9999ebf9 --- /dev/null +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/services/CurvePointService.java @@ -0,0 +1,39 @@ +package com.nnk.springboot.services; + +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.nnk.springboot.domain.CurvePoint; +import com.nnk.springboot.repositories.CurvePointRepository; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Service +public class CurvePointService { + + @Autowired + private CurvePointRepository curvePointRepository; + + public Iterable findAllCurvePoints() { + log.info("All CurvePoint retrieved from database"); + return curvePointRepository.findAll(); + } + + public Optional findCurvePointById(Integer id) { + log.info("CurvePoint with id: " + id + " retrieved from database"); + return curvePointRepository.findById(id); + } + + public CurvePoint saveCurvePoint(CurvePoint curvePoint) { + log.info("CurvePoint: " + curvePoint.toString() + " saved in database"); + return curvePointRepository.save(curvePoint); + } + + public void deleteCurvePoint(CurvePoint curvePoint) { + log.info("CurvePoint: " + curvePoint.toString() + " deleted in database"); + curvePointRepository.delete(curvePoint); + } +} diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/services/RatingService.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/services/RatingService.java new file mode 100644 index 0000000000..308fa76cef --- /dev/null +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/services/RatingService.java @@ -0,0 +1,39 @@ +package com.nnk.springboot.services; + +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.nnk.springboot.domain.Rating; +import com.nnk.springboot.repositories.RatingRepository; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Service +public class RatingService { + + @Autowired + private RatingRepository ratingRepository; + + public Iterable findAllRatings() { + log.info("All Rating retrieved from database"); + return ratingRepository.findAll(); + } + + public Optional findRatingById(Integer id) { + log.info("Rating with id: " + id + " retrieved from database"); + return ratingRepository.findById(id); + } + + public Rating saveRating(Rating rating) { + log.info("Rating: " + rating.toString() + " saved in database"); + return ratingRepository.save(rating); + } + + public void deleteRating(Rating rating) { + log.info("Rating: " + rating.toString() + " deleted in database"); + ratingRepository.delete(rating); + } +} diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/services/RuleNameService.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/services/RuleNameService.java new file mode 100644 index 0000000000..9b90285366 --- /dev/null +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/services/RuleNameService.java @@ -0,0 +1,40 @@ +package com.nnk.springboot.services; + +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.nnk.springboot.domain.RuleName; +import com.nnk.springboot.repositories.RuleNameRepository; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Service +public class RuleNameService { + + @Autowired + private RuleNameRepository ruleNameRepository; + + public Iterable findAllRuleNames() { + log.info("All RuleName retrieved from database"); + return ruleNameRepository.findAll(); + } + + // a revoir + public Optional findRuleNameById(Integer id) { + log.info("RuleName with id: " + id + " retrieved from database"); + return ruleNameRepository.findById(id); + } + + public RuleName saveRuleName(RuleName ruleName) { + log.info("RuleName: " + ruleName.toString() + " saved in database"); + return ruleNameRepository.save(ruleName); + } + + public void deleteRuleName(RuleName ruleName) { + log.info("RuleName: " + ruleName.toString() + " deleted in database"); + ruleNameRepository.delete(ruleName); + } +} diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/services/TradeService.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/services/TradeService.java new file mode 100644 index 0000000000..9c515bfbc6 --- /dev/null +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/services/TradeService.java @@ -0,0 +1,39 @@ +package com.nnk.springboot.services; + +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.nnk.springboot.domain.Trade; +import com.nnk.springboot.repositories.TradeRepository; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Service +public class TradeService { + + @Autowired + private TradeRepository tradeRepository; + + public Iterable findAllTrades() { + log.info("All Trade retrieved from database"); + return tradeRepository.findAll(); + } + + public Optional findTradeById(Integer id) { + log.info("Trade with id: " + id + " from database"); + return tradeRepository.findById(id); + } + + public Trade saveTrade(Trade trade) { + log.info("Trade: " + trade.toString() + " saved in database"); + return tradeRepository.save(trade); + } + + public void deleteTrade(Trade trade) { + log.info("Trade: " + trade.toString() + " deleted in database"); + tradeRepository.delete(trade); + } +} diff --git a/Poseiden-skeleton/src/main/java/com/nnk/springboot/services/UserService.java b/Poseiden-skeleton/src/main/java/com/nnk/springboot/services/UserService.java new file mode 100644 index 0000000000..d46a9a1cd0 --- /dev/null +++ b/Poseiden-skeleton/src/main/java/com/nnk/springboot/services/UserService.java @@ -0,0 +1,39 @@ +package com.nnk.springboot.services; + +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.nnk.springboot.domain.User; +import com.nnk.springboot.repositories.UserRepository; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Service +public class UserService { + + @Autowired + private UserRepository userRepository; + + public Iterable findAllUsers() { + log.info("All User retrieved from database"); + return userRepository.findAll(); + } + + public Optional findUserById(Integer id) { + log.info("User with id: " + id + " retrieved from database"); + return userRepository.findById(id); + } + + public User saveUser(User user) { + log.info("User: " + user.toString() + " saved in database"); + return userRepository.save(user); + } + + public void deleteUser(User user) { + log.info("User: " + user.toString() + " deleted in database"); + userRepository.delete(user);; + } +} diff --git a/Poseiden-skeleton/src/main/resources/application.properties b/Poseiden-skeleton/src/main/resources/application.properties index 8b194cc76e..4ab59c6298 100644 --- a/Poseiden-skeleton/src/main/resources/application.properties +++ b/Poseiden-skeleton/src/main/resources/application.properties @@ -1,14 +1,23 @@ +######################### Log Configuration ########################## logging.level.org.springframework=INFO +logging.level.root=error +logging.level.com.nnk=info ################### DataSource Configuration ########################## spring.datasource.driver-class-name=com.mysql.jdbc.Driver -spring.datasource.url=jdbc:mysql://localhost:3306/demo +spring.datasource.url=jdbc:mysql://localhost:3306/demo?allowPublicKeyRetrieval=true&useSSL=false spring.datasource.username=root -spring.datasource.password= +spring.datasource.password=rootroot ################### Hibernate Configuration ########################## +spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true +spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl +################ Spring Security oauth2 Configuration ################ + +spring.security.oauth2.client.registration.github.client-id=202d8986dff4305d54d0 +spring.security.oauth2.client.registration.github.client-secret=43c832c0613d3d259572f890cce88e4b1329f852 \ No newline at end of file diff --git a/Poseiden-skeleton/src/main/resources/logback.xml b/Poseiden-skeleton/src/main/resources/logback.xml new file mode 100644 index 0000000000..5f2bead19d --- /dev/null +++ b/Poseiden-skeleton/src/main/resources/logback.xml @@ -0,0 +1,14 @@ + + + + src/main/resources/logsfile.log + true + + %d [%thread] %-5level %logger{35} - %msg%n + + + + + + + \ No newline at end of file diff --git a/Poseiden-skeleton/src/main/resources/templates/403.html b/Poseiden-skeleton/src/main/resources/templates/403.html index c0c3740bd2..4301003e80 100644 --- a/Poseiden-skeleton/src/main/resources/templates/403.html +++ b/Poseiden-skeleton/src/main/resources/templates/403.html @@ -6,8 +6,8 @@

Access Denied Exception

- Logged in user: [[${#httpServletRequest.remoteUser}]] -
+ Logged in user: [[${#httpServletRequest.getUserPrincipal().getName()}]] +
diff --git a/Poseiden-skeleton/src/main/resources/templates/bidList/add.html b/Poseiden-skeleton/src/main/resources/templates/bidList/add.html index c0fb91a99e..4506b8249b 100644 --- a/Poseiden-skeleton/src/main/resources/templates/bidList/add.html +++ b/Poseiden-skeleton/src/main/resources/templates/bidList/add.html @@ -1,26 +1,74 @@ - + - + Home - + -
+
-
-

Add New Bid

-
+
+
+

Add New Bid

+
+
+ +
+
+
+ +
+ + +
+ + +

Account error

+
+
+
+ + +
+ + +

Type error

+
+
+
+ + +
+ -
- - - +

Bid Quantity error

+
+
+
+
+ Cancel + +
+
+ +
+
-
\ No newline at end of file diff --git a/Poseiden-skeleton/src/main/resources/templates/bidList/list.html b/Poseiden-skeleton/src/main/resources/templates/bidList/list.html index 84a1d14a12..b94a16d443 100644 --- a/Poseiden-skeleton/src/main/resources/templates/bidList/list.html +++ b/Poseiden-skeleton/src/main/resources/templates/bidList/list.html @@ -17,8 +17,8 @@ Rule
- Logged in user: [[${#httpServletRequest.remoteUser}]] -
+ Logged in user: [[${#httpServletRequest.getUserPrincipal().getName()}]] +
@@ -37,7 +37,16 @@ - + + Id + + + + + Edit |  + Delete + + diff --git a/Poseiden-skeleton/src/main/resources/templates/bidList/update.html b/Poseiden-skeleton/src/main/resources/templates/bidList/update.html index 7786ebd1ff..f439b83783 100644 --- a/Poseiden-skeleton/src/main/resources/templates/bidList/update.html +++ b/Poseiden-skeleton/src/main/resources/templates/bidList/update.html @@ -8,18 +8,62 @@
+

Update Bid

+
-
- -
+
+
+
+ + +
+ + +

Account error

+
+
+
+ + +
+ + +

Type error

+
+
+
+ + +
+ + +

Bid Quantity error

+
+
+
+
+ Cancel + +
+
+
+
+
- + \ No newline at end of file diff --git a/Poseiden-skeleton/src/main/resources/templates/curvePoint/add.html b/Poseiden-skeleton/src/main/resources/templates/curvePoint/add.html index 28d17cb645..a25b3ed379 100644 --- a/Poseiden-skeleton/src/main/resources/templates/curvePoint/add.html +++ b/Poseiden-skeleton/src/main/resources/templates/curvePoint/add.html @@ -1,27 +1,70 @@ - + - + Home - + -
+
-
-

Add New Curve Point

-
+
+
+

Add New Curve Point

+
+
-
-
- +
+
+ +
+ - -
+
+ + +

Curve Id error

+
+
+
+ + +
+ + +

Term error

+
+
+
+ -
+
+ + +

Value error

+
+
+
+
+ Cancel + +
+
+ + +
+
+ +
\ No newline at end of file diff --git a/Poseiden-skeleton/src/main/resources/templates/curvePoint/list.html b/Poseiden-skeleton/src/main/resources/templates/curvePoint/list.html index 469836149b..2d736a52ae 100644 --- a/Poseiden-skeleton/src/main/resources/templates/curvePoint/list.html +++ b/Poseiden-skeleton/src/main/resources/templates/curvePoint/list.html @@ -17,8 +17,8 @@ Rule
- Logged in user: [[${#httpServletRequest.remoteUser}]] -
+ Logged in user: [[${#httpServletRequest.getUserPrincipal().getName()}]] +
@@ -37,6 +37,17 @@ + + + + + + + Edit |  + Delete + + + diff --git a/Poseiden-skeleton/src/main/resources/templates/curvePoint/update.html b/Poseiden-skeleton/src/main/resources/templates/curvePoint/update.html index e7f5a0d330..21c7520a88 100644 --- a/Poseiden-skeleton/src/main/resources/templates/curvePoint/update.html +++ b/Poseiden-skeleton/src/main/resources/templates/curvePoint/update.html @@ -9,15 +9,55 @@
+

Update CurvePoint

- +
+
+
- +
+ + +
+ + +

Curve Id error

+
+
+
+ + +
+ + +

Term error

+
+
+
+ + +
+ + +

Value error

+
+
+
+
+ Cancel + +
+
+
diff --git a/Poseiden-skeleton/src/main/resources/templates/rating/add.html b/Poseiden-skeleton/src/main/resources/templates/rating/add.html index 8090e5382e..527258fdc4 100644 --- a/Poseiden-skeleton/src/main/resources/templates/rating/add.html +++ b/Poseiden-skeleton/src/main/resources/templates/rating/add.html @@ -1,27 +1,83 @@ - + - + Home - + -
+
-
-

Add New Rating

-
+
+
+

Add New Rating

+
+
+
+
+
+
+ -
- - +
+ - -
+

MoodysRating + error

+
+
+
+ + +
+ + +

SandPRating + error

+
+
+
+ + +
+ -
+

FitchRating + error

+
+
+
+ + +
+ + +

Order + Number error

+
+
+
+
+ Cancel +
+
+ + +
+
+ +
\ No newline at end of file diff --git a/Poseiden-skeleton/src/main/resources/templates/rating/list.html b/Poseiden-skeleton/src/main/resources/templates/rating/list.html index bfbb403b84..49d0457bcd 100644 --- a/Poseiden-skeleton/src/main/resources/templates/rating/list.html +++ b/Poseiden-skeleton/src/main/resources/templates/rating/list.html @@ -17,8 +17,8 @@ Rule
- Logged in user: [[${#httpServletRequest.remoteUser}]] -
+ Logged in user: [[${#httpServletRequest.getUserPrincipal().getName()}]] +
@@ -38,7 +38,17 @@ - + + + + + + + + Edit |  + Delete + + diff --git a/Poseiden-skeleton/src/main/resources/templates/rating/update.html b/Poseiden-skeleton/src/main/resources/templates/rating/update.html index 1e1914100a..a812483595 100644 --- a/Poseiden-skeleton/src/main/resources/templates/rating/update.html +++ b/Poseiden-skeleton/src/main/resources/templates/rating/update.html @@ -1,26 +1,81 @@ + xmlns:th="http://www.thymeleaf.org"> - - Home - + +Home + -
+
+
+
+

Update Rating

+
+
+
+
+
+
+ -
-

Update Rating

-
+
+ -
- - - -
+

MoodysRating error

+
+
+
+ + +
+ + +

SandPRating error

+
+
+
+ -
+
+ + +

FitchRating error

+
+
+
+ + +
+ + +

Order Number error

+
+
+
+
+ Cancel +
+
+ +
+
+
\ No newline at end of file diff --git a/Poseiden-skeleton/src/main/resources/templates/ruleName/add.html b/Poseiden-skeleton/src/main/resources/templates/ruleName/add.html index afb0c2730b..f085c22603 100644 --- a/Poseiden-skeleton/src/main/resources/templates/ruleName/add.html +++ b/Poseiden-skeleton/src/main/resources/templates/ruleName/add.html @@ -1,27 +1,101 @@ - + - + Home - + -
+
-
-

Add New Rule

-
+
+
+

Add New Rule

+
+
+
+
+
+
+ -
- - +
+ - -
+

Name error

+
+
+
+ + +
+ + +

Description error

+
+
+
+ + +
+ + +

json error

+
+
+
+ -
+
+ + +

template error

+
+
+
+ + +
+ + +

sql error

+
+
+
+ + +
+ + +

sqlPart error

+
+
+
+
+ Cancel + +
+
+ +
+
+
\ No newline at end of file diff --git a/Poseiden-skeleton/src/main/resources/templates/ruleName/list.html b/Poseiden-skeleton/src/main/resources/templates/ruleName/list.html index 2ad3868e82..49078f2e60 100644 --- a/Poseiden-skeleton/src/main/resources/templates/ruleName/list.html +++ b/Poseiden-skeleton/src/main/resources/templates/ruleName/list.html @@ -17,8 +17,8 @@ Rule
- Logged in user: [[${#httpServletRequest.remoteUser}]] -
+ Logged in user: [[${#httpServletRequest.getUserPrincipal().getName()}]] +
@@ -40,7 +40,19 @@ - + + + + + + + + + + Edit |  + Delete + + diff --git a/Poseiden-skeleton/src/main/resources/templates/ruleName/update.html b/Poseiden-skeleton/src/main/resources/templates/ruleName/update.html index 34e6c3cc60..f6d1bf1e5f 100644 --- a/Poseiden-skeleton/src/main/resources/templates/ruleName/update.html +++ b/Poseiden-skeleton/src/main/resources/templates/ruleName/update.html @@ -1,27 +1,103 @@ + xmlns:th="http://www.thymeleaf.org"> - - Home - + +Home + -
+
-
-

Update New Rule

-
+
+
+

Update New Rule

+
+
+
+
+
+
+ -
- - +
+ - -
+

Name error

+
+
+
+ + +
+ + +

Description error

+
+
+
+ + +
+ + +

json error

+
+
+
+ + +
+ -
+

template error

+
+
+
+ + +
+ + +

sql error

+
+
+
+ + +
+ + +

sqlPart error

+
+
+
+
+ Cancel + +
+
+ + +
+
+
\ No newline at end of file diff --git a/Poseiden-skeleton/src/main/resources/templates/trade/add.html b/Poseiden-skeleton/src/main/resources/templates/trade/add.html index 6fafa08c01..1b7cc9a99f 100644 --- a/Poseiden-skeleton/src/main/resources/templates/trade/add.html +++ b/Poseiden-skeleton/src/main/resources/templates/trade/add.html @@ -1,27 +1,69 @@ - + - + Home - + -
+
-
-

Add New Trade

-
+
+
+

Add New Trade

+
+
+
+
+
+
+ -
- - +
+ - -
+

Account error

+
+
+
+ + +
+ -
+

Type error

+
+
+
+ + +
+ + +

Bid Quantity error

+
+
+
+
+ Cancel +
+
+ +
+
+
\ No newline at end of file diff --git a/Poseiden-skeleton/src/main/resources/templates/trade/list.html b/Poseiden-skeleton/src/main/resources/templates/trade/list.html index 2a247fa7da..35e1617fca 100644 --- a/Poseiden-skeleton/src/main/resources/templates/trade/list.html +++ b/Poseiden-skeleton/src/main/resources/templates/trade/list.html @@ -1,46 +1,56 @@ - + - + Home - + -
-
-
- Bid List |  - Curve Points |  - Ratings |  - Trade |  - Rule +
+
+
+ Bid List |  Curve Points |  Ratings |  Trade |  + Rule +
+
+ Logged in user: + [[${#httpServletRequest.getUserPrincipal().getName()}]] +
+ +
+
-
- Logged in user: [[${#httpServletRequest.remoteUser}]] -
- -
+
+

Trade List

+
+
+ Add New + + + + + + + + + + + + + + + + + + + +
IdAccountTypeBuy QuantityAction
IdEdit |  + Delete +
-

Trade List

-
- Add New - - - - - - - - - - - - - -
IdAccountTypeBuy QuantityAction
-
-
\ No newline at end of file diff --git a/Poseiden-skeleton/src/main/resources/templates/trade/update.html b/Poseiden-skeleton/src/main/resources/templates/trade/update.html index f4bbc52592..f9957b7728 100644 --- a/Poseiden-skeleton/src/main/resources/templates/trade/update.html +++ b/Poseiden-skeleton/src/main/resources/templates/trade/update.html @@ -1,25 +1,71 @@ - + - + Home - + -
-
-

Update Trade

-
+
+
+
+

Update Trade

+
+
+
+
+
+
+ -
- - - -
+
+ + +

Account error

+
+
+
+ + +
+ -
+

Type error

+
+
+
+ + +
+ + +

Bid Quantity error

+
+
+
+
+ Cancel +
+
+ + +
+
+
\ No newline at end of file diff --git a/Poseiden-skeleton/src/main/resources/templates/user/add.html b/Poseiden-skeleton/src/main/resources/templates/user/add.html index 09246f49b2..1218791adf 100644 --- a/Poseiden-skeleton/src/main/resources/templates/user/add.html +++ b/Poseiden-skeleton/src/main/resources/templates/user/add.html @@ -1,61 +1,75 @@ - + - + Home - + -
+
-
-

Add New User

-
- -
-
-
- -
- -

-
-
-
- -
- -

-
-
-
- -
- -

-
-
-
- -
- - -

-
+
+
+

Add New User

+
+
+
+ +
+ +
+ +

+
+
+
+ +
+ +

+
+
+
+ +
+ +

+
+
+
+ +
+ +

+
+
-
-
- Cancel - -
-
+
+
+ Cancel +
+
- + +
+
- -
\ No newline at end of file diff --git a/Poseiden-skeleton/src/main/resources/templates/user/update.html b/Poseiden-skeleton/src/main/resources/templates/user/update.html index 0322cfdf39..deb12c428b 100644 --- a/Poseiden-skeleton/src/main/resources/templates/user/update.html +++ b/Poseiden-skeleton/src/main/resources/templates/user/update.html @@ -1,61 +1,75 @@ + xmlns:th="http://www.thymeleaf.org"> - - Home - + +Home + -
+
-
-

Update User

-
- -
-
-
- -
- -

-
-
-
- -
- -

-
-
-
- -
- -

-
-
-
- -
- - -
+
+
+

Update User

+
+
+
+ +
+ +
+ +

+
+
+
+ +
+ +

+
+
+
+ +
+ +

+
+
+
+ +
+ +
+
-
-
- - Cancel - -
-
+
+
+ Cancel +
+
- + +
+
- -
\ No newline at end of file diff --git a/Poseiden-skeleton/src/test/java/com/nnk/springboot/BidTests.java b/Poseiden-skeleton/src/test/java/com/nnk/springboot/BidTests.java index f1a4f40316..2150bb2323 100644 --- a/Poseiden-skeleton/src/test/java/com/nnk/springboot/BidTests.java +++ b/Poseiden-skeleton/src/test/java/com/nnk/springboot/BidTests.java @@ -1,7 +1,9 @@ package com.nnk.springboot; import com.nnk.springboot.domain.BidList; -import com.nnk.springboot.repositories.BidListRepository; +import com.nnk.springboot.services.BidListService; + +import org.assertj.core.util.Lists; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -17,30 +19,30 @@ public class BidTests { @Autowired - private BidListRepository bidListRepository; + private BidListService bidListService; @Test public void bidListTest() { BidList bid = new BidList("Account Test", "Type Test", 10d); // Save - bid = bidListRepository.save(bid); + bid = bidListService.saveBidList(bid); Assert.assertNotNull(bid.getBidListId()); Assert.assertEquals(bid.getBidQuantity(), 10d, 10d); // Update bid.setBidQuantity(20d); - bid = bidListRepository.save(bid); + bid = bidListService.saveBidList(bid); Assert.assertEquals(bid.getBidQuantity(), 20d, 20d); // Find - List listResult = bidListRepository.findAll(); + List listResult = Lists.newArrayList(bidListService.findAllBidLists()); Assert.assertTrue(listResult.size() > 0); // Delete Integer id = bid.getBidListId(); - bidListRepository.delete(bid); - Optional bidList = bidListRepository.findById(id); + bidListService.deleteBidList(bid); + Optional bidList = bidListService.findBidListById(id); Assert.assertFalse(bidList.isPresent()); } } diff --git a/Poseiden-skeleton/src/test/java/com/nnk/springboot/CurvePointTests.java b/Poseiden-skeleton/src/test/java/com/nnk/springboot/CurvePointTests.java index 854615c0a0..6bd24803ec 100644 --- a/Poseiden-skeleton/src/test/java/com/nnk/springboot/CurvePointTests.java +++ b/Poseiden-skeleton/src/test/java/com/nnk/springboot/CurvePointTests.java @@ -1,7 +1,9 @@ package com.nnk.springboot; import com.nnk.springboot.domain.CurvePoint; -import com.nnk.springboot.repositories.CurvePointRepository; +import com.nnk.springboot.services.CurvePointService; + +import org.assertj.core.util.Lists; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -17,30 +19,30 @@ public class CurvePointTests { @Autowired - private CurvePointRepository curvePointRepository; + private CurvePointService curvePointService; @Test public void curvePointTest() { CurvePoint curvePoint = new CurvePoint(10, 10d, 30d); // Save - curvePoint = curvePointRepository.save(curvePoint); + curvePoint = curvePointService.saveCurvePoint(curvePoint); Assert.assertNotNull(curvePoint.getId()); Assert.assertTrue(curvePoint.getCurveId() == 10); // Update curvePoint.setCurveId(20); - curvePoint = curvePointRepository.save(curvePoint); + curvePoint = curvePointService.saveCurvePoint(curvePoint); Assert.assertTrue(curvePoint.getCurveId() == 20); // Find - List listResult = curvePointRepository.findAll(); + List listResult = Lists.newArrayList(curvePointService.findAllCurvePoints()); Assert.assertTrue(listResult.size() > 0); // Delete Integer id = curvePoint.getId(); - curvePointRepository.delete(curvePoint); - Optional curvePointList = curvePointRepository.findById(id); + curvePointService.deleteCurvePoint(curvePoint); + Optional curvePointList = curvePointService.findCurvePointById(id); Assert.assertFalse(curvePointList.isPresent()); } diff --git a/Poseiden-skeleton/src/test/java/com/nnk/springboot/RatingTests.java b/Poseiden-skeleton/src/test/java/com/nnk/springboot/RatingTests.java index 6c3ebf0b08..6282a4e26d 100644 --- a/Poseiden-skeleton/src/test/java/com/nnk/springboot/RatingTests.java +++ b/Poseiden-skeleton/src/test/java/com/nnk/springboot/RatingTests.java @@ -1,7 +1,9 @@ package com.nnk.springboot; import com.nnk.springboot.domain.Rating; -import com.nnk.springboot.repositories.RatingRepository; +import com.nnk.springboot.services.RatingService; + +import org.assertj.core.util.Lists; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -17,30 +19,30 @@ public class RatingTests { @Autowired - private RatingRepository ratingRepository; + private RatingService ratingService; @Test public void ratingTest() { Rating rating = new Rating("Moodys Rating", "Sand PRating", "Fitch Rating", 10); // Save - rating = ratingRepository.save(rating); + rating = ratingService.saveRating(rating); Assert.assertNotNull(rating.getId()); Assert.assertTrue(rating.getOrderNumber() == 10); // Update rating.setOrderNumber(20); - rating = ratingRepository.save(rating); + rating = ratingService.saveRating(rating); Assert.assertTrue(rating.getOrderNumber() == 20); // Find - List listResult = ratingRepository.findAll(); + List listResult = Lists.newArrayList(ratingService.findAllRatings()); Assert.assertTrue(listResult.size() > 0); // Delete Integer id = rating.getId(); - ratingRepository.delete(rating); - Optional ratingList = ratingRepository.findById(id); + ratingService.deleteRating(rating); + Optional ratingList = ratingService.findRatingById(id); Assert.assertFalse(ratingList.isPresent()); } } diff --git a/Poseiden-skeleton/src/test/java/com/nnk/springboot/RuleTests.java b/Poseiden-skeleton/src/test/java/com/nnk/springboot/RuleTests.java index 541dab5412..a45565beed 100644 --- a/Poseiden-skeleton/src/test/java/com/nnk/springboot/RuleTests.java +++ b/Poseiden-skeleton/src/test/java/com/nnk/springboot/RuleTests.java @@ -1,7 +1,9 @@ package com.nnk.springboot; import com.nnk.springboot.domain.RuleName; -import com.nnk.springboot.repositories.RuleNameRepository; +import com.nnk.springboot.services.RuleNameService; + +import org.assertj.core.util.Lists; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -17,30 +19,30 @@ public class RuleTests { @Autowired - private RuleNameRepository ruleNameRepository; + private RuleNameService ruleNameService; @Test public void ruleTest() { RuleName rule = new RuleName("Rule Name", "Description", "Json", "Template", "SQL", "SQL Part"); // Save - rule = ruleNameRepository.save(rule); + rule = ruleNameService.saveRuleName(rule); Assert.assertNotNull(rule.getId()); Assert.assertTrue(rule.getName().equals("Rule Name")); // Update rule.setName("Rule Name Update"); - rule = ruleNameRepository.save(rule); + rule = ruleNameService.saveRuleName(rule); Assert.assertTrue(rule.getName().equals("Rule Name Update")); // Find - List listResult = ruleNameRepository.findAll(); + List listResult = Lists.newArrayList(ruleNameService.findAllRuleNames()); Assert.assertTrue(listResult.size() > 0); // Delete Integer id = rule.getId(); - ruleNameRepository.delete(rule); - Optional ruleList = ruleNameRepository.findById(id); + ruleNameService.deleteRuleName(rule); + Optional ruleList = ruleNameService.findRuleNameById(id); Assert.assertFalse(ruleList.isPresent()); } } diff --git a/Poseiden-skeleton/src/test/java/com/nnk/springboot/TradeTests.java b/Poseiden-skeleton/src/test/java/com/nnk/springboot/TradeTests.java index ed50409266..c853170e16 100644 --- a/Poseiden-skeleton/src/test/java/com/nnk/springboot/TradeTests.java +++ b/Poseiden-skeleton/src/test/java/com/nnk/springboot/TradeTests.java @@ -1,7 +1,9 @@ package com.nnk.springboot; import com.nnk.springboot.domain.Trade; -import com.nnk.springboot.repositories.TradeRepository; +import com.nnk.springboot.services.TradeService; + +import org.assertj.core.util.Lists; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -17,30 +19,30 @@ public class TradeTests { @Autowired - private TradeRepository tradeRepository; + private TradeService tradeService; @Test public void tradeTest() { - Trade trade = new Trade("Trade Account", "Type"); + Trade trade = new Trade("Trade Account", "Type", 10D); // Save - trade = tradeRepository.save(trade); + trade = tradeService.saveTrade(trade); Assert.assertNotNull(trade.getTradeId()); Assert.assertTrue(trade.getAccount().equals("Trade Account")); // Update trade.setAccount("Trade Account Update"); - trade = tradeRepository.save(trade); + trade = tradeService.saveTrade(trade); Assert.assertTrue(trade.getAccount().equals("Trade Account Update")); // Find - List listResult = tradeRepository.findAll(); + List listResult = Lists.newArrayList(tradeService.findAllTrades()); Assert.assertTrue(listResult.size() > 0); // Delete Integer id = trade.getTradeId(); - tradeRepository.delete(trade); - Optional tradeList = tradeRepository.findById(id); + tradeService.deleteTrade(trade); + Optional tradeList = tradeService.findTradeById(id); Assert.assertFalse(tradeList.isPresent()); } } diff --git a/Poseiden-skeleton/src/test/java/com/nnk/springboot/UserTests.java b/Poseiden-skeleton/src/test/java/com/nnk/springboot/UserTests.java new file mode 100644 index 0000000000..70944b5a9a --- /dev/null +++ b/Poseiden-skeleton/src/test/java/com/nnk/springboot/UserTests.java @@ -0,0 +1,48 @@ +package com.nnk.springboot; + +import com.nnk.springboot.domain.User; +import com.nnk.springboot.services.UserService; + +import org.assertj.core.util.Lists; +import org.junit.Assert; +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.context.junit4.SpringRunner; + +import java.util.List; +import java.util.Optional; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class UserTests { + + @Autowired + private UserService userService; + + @Test + public void userTest() { + User user = new User("UserName", "Passw0rd!", "UserTestName", "USER"); + + // Save + user = userService.saveUser(user); + Assert.assertNotNull(user.getId()); + Assert.assertTrue(user.getUsername().equals("UserName")); + + // Update + user.setUsername("UserNameUpdate");; + user = userService.saveUser(user); + Assert.assertTrue(user.getUsername().equals("UserNameUpdate")); + + // Find + List listResult = Lists.newArrayList(userService.findAllUsers()); + Assert.assertTrue(listResult.size() > 2); // 2 users already saved in initial database + + // Delete + Integer id = user.getId(); + userService.deleteUser(user); + Optional userList = userService.findUserById(id); + Assert.assertFalse(userList.isPresent()); + } +} diff --git a/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/BidListControllerTest.java b/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/BidListControllerTest.java new file mode 100644 index 0000000000..cd492e9b54 --- /dev/null +++ b/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/BidListControllerTest.java @@ -0,0 +1,227 @@ +package com.nnk.springboot.controllers; + +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.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.web.util.NestedServletException; + +import com.nnk.springboot.domain.BidList; +import com.nnk.springboot.services.BidListService; + + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc +@WebAppConfiguration +public class BidListControllerTest { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private BidListService bidListService; + + @Test + public void test_BidListPages_NeedsAuthentication() throws Exception { + mockMvc.perform(get("/bidList/list")) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("http://localhost/login")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_AdminAuthentication_PermitsBidListListAccess() throws Exception{ + mockMvc.perform(get("/bidList/list")) + .andExpect(status().isOk()) + .andExpect(view().name("bidList/list")); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UserAuthentication_PermitBidListListAccess() throws Exception{ + mockMvc.perform(get("/bidList/list")) + .andExpect(status().isOk()) + .andExpect(view().name("bidList/list")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_AdminAuthentication_PermitBidListAddAccess() throws Exception{ + mockMvc.perform(get("/bidList/add")) + .andExpect(status().isOk()) + .andExpect(view().name("bidList/add")); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UserAuthentication_PermitBidListAddAccess() throws Exception{ + mockMvc.perform(get("/bidList/add")) + .andExpect(status().isOk()) + .andExpect(view().name("bidList/add")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_AdminAuthentication_PermitBidListUpdateAccess() throws Exception{ + // saves a bidList to update in database + BidList bid = new BidList("Account Test", "Type Test", 10d); + bid = bidListService.saveBidList(bid); + + // tries to access to update page + mockMvc.perform(get("/bidList/update/" + bid.getBidListId())) + .andExpect(status().isOk()) + .andExpect(view().name("bidList/update")); + + // deletes the bidList from the database + bidListService.deleteBidList(bid); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UserAuthentication_PermitBidListUpdateAccess() throws Exception{ + // saves a bidList to update in database + BidList bid = new BidList("Account Test", "Type Test", 10d); + bid = bidListService.saveBidList(bid); + + // tries to access to update page + mockMvc.perform(get("/bidList/update/" + bid.getBidListId())) + .andExpect(status().isOk()) + .andExpect(view().name("bidList/update")); + + // deletes the bidList from the database + bidListService.deleteBidList(bid); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_DeleteABidList() throws Exception { + // saves a bidList to delete in database + BidList bid = new BidList("Account Test to delete", "Type Test", 10d); + bid = bidListService.saveBidList(bid); + + // deletes the bidList from database + mockMvc.perform(get("/bidList/delete/{id}", bid.getBidListId())) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/bidList/list")); + } + + @Test(expected = NestedServletException.class) + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotDeleteABidList_withInvalidId() throws Exception { + + // tries to delete a bidList from database with invalid Id + mockMvc.perform(get("/bidList/delete/{id}", 0)) + .andExpect(status().is5xxServerError()); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_AddANewValidBidList() throws Exception { + // creates a bidList to add to database + BidList bid = new BidList("New Account Test", "Type Test", 10D); + + // tries to add the bidList to database + mockMvc + .perform(post("/bidList/validate") + .param("account", bid.getAccount()) + .param("type", bid.getType()) + .param("bidQuantity", bid.getBidQuantity().toString())) + .andExpect(model().hasNoErrors()) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/bidList/list")); + + // retrieves the bidList from database + Iterable bidListsIterable = new ArrayList<>(); + bidListsIterable = bidListService.findAllBidLists(); + List bidLists = new ArrayList<>(); + bidListsIterable.forEach(bidList -> bidLists.add(bidList)); + bid = bidLists.get(bidLists.size() - 1); + + // deletes the bidList from database + bidListService.deleteBidList(bid); + + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotAddANewInvalidBidList() throws Exception { + // creates an invalid bidList to add to database + BidList bid = new BidList("Account Test", "Type Test", 0D); + + // tries to add the bidList to database + mockMvc + .perform(post("/bidList/validate") + .param("account", bid.getAccount()) + .param("type", bid.getType()) + .param("bidQuantity", bid.getBidQuantity().toString())) + .andExpect(model().hasErrors()) + .andExpect(view().name("/bidList/add")); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UpdateValidBidList() throws Exception { + // creates a bidList to update + BidList bid = new BidList("Account test", "Type Test", 10d); + bid = bidListService.saveBidList(bid); + + // tries to update the bidList + mockMvc + .perform(post("/bidList/update/{id}", bid.getBidListId()) + .param("account", "Account updated") + .param("type", bid.getType()) + .param("bidQuantity", bid.getBidQuantity().toString())) + .andExpect(model().hasNoErrors()) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/bidList/list")); + + // deletes the bidList + bidListService.deleteBidList(bid); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotUpdateInvalidBidList() throws Exception { + // creates a bidList to update + BidList bid = new BidList("Account test", "Type Test", 10d); + bid = bidListService.saveBidList(bid); + + // tries to update the bidList with invalid account + mockMvc + .perform(post("/bidList/update/{id}", bid.getBidListId()) + .param("account", "") + .param("type", bid.getType()) + .param("bidQuantity", bid.getBidQuantity().toString())) + .andExpect(model().hasErrors()) + .andExpect(view().name("/bidList/update")); + + // deletes the bidList to update + bidListService.deleteBidList(bid); + } + + @Test(expected = NestedServletException.class) + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotUpdateBidList_withInvalidId() throws Exception { + // tries to update a bidList with invalid Id + mockMvc + .perform(get("/bidList/update/{id}", 0)) + .andExpect(status().is5xxServerError()); + } + +} diff --git a/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/CurvePointControllerTest.java b/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/CurvePointControllerTest.java new file mode 100644 index 0000000000..f659738bc7 --- /dev/null +++ b/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/CurvePointControllerTest.java @@ -0,0 +1,224 @@ +package com.nnk.springboot.controllers; + +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.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.web.util.NestedServletException; + +import com.nnk.springboot.domain.CurvePoint; +import com.nnk.springboot.services.CurvePointService; + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc +@WebAppConfiguration +public class CurvePointControllerTest { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private CurvePointService curvePointService; + + @Test + public void test_CurvePointPages_NeedAuthentication() throws Exception { + mockMvc.perform(get("curvePoint/list")) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("http://localhost/login")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_AdminAuthentication_PermitCurvePointListAccess() throws Exception{ + mockMvc.perform(get("/curvePoint/list")) + .andExpect(status().isOk()) + .andExpect(view().name("curvePoint/list")); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UserAuthentication_PermitCurvePointListAccess() throws Exception{ + mockMvc.perform(get("/curvePoint/list")) + .andExpect(status().isOk()) + .andExpect(view().name("curvePoint/list")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_AdminAuthentication_PermitCurvePointAddAccess() throws Exception{ + mockMvc.perform(get("/curvePoint/add")) + .andExpect(status().isOk()) + .andExpect(view().name("curvePoint/add")); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UserAuthentication_PermitCurvePointAddAccess() throws Exception{ + mockMvc.perform(get("/curvePoint/add")) + .andExpect(status().isOk()) + .andExpect(view().name("curvePoint/add")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_AdminAuthentication_PermitCurvePointUpdateAccess() throws Exception{ + // saves a curvePoint to update in database + CurvePoint curvePoint = new CurvePoint(10, 10d, 30d); + curvePoint = curvePointService.saveCurvePoint(curvePoint); + + // tries to access to update page + mockMvc.perform(get("/curvePoint/update/" + curvePoint.getId())) + .andExpect(status().isOk()) + .andExpect(view().name("curvePoint/update")); + + // deletes the curvePoint from the database + curvePointService.deleteCurvePoint(curvePoint); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UserAuthentication_PermitCurvePointUpdateAccess() throws Exception{ + // saves a curvePoint to update in database + CurvePoint curvePoint = new CurvePoint(10, 10d, 30d); + curvePoint = curvePointService.saveCurvePoint(curvePoint); + + // tries to access to update page + mockMvc.perform(get("/curvePoint/update/" + curvePoint.getId())) + .andExpect(status().isOk()) + .andExpect(view().name("curvePoint/update")); + + // deletes the curvePoint from the database + curvePointService.deleteCurvePoint(curvePoint); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_DeleteACurvePoint() throws Exception { + // saves a curvePoint to delete in database + CurvePoint curvePoint = new CurvePoint(10, 10d, 30d); + curvePoint = curvePointService.saveCurvePoint(curvePoint); + + // deletes the curvePoint from database + mockMvc.perform(get("/curvePoint/delete/{id}", curvePoint.getId())) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/curvePoint/list")); + } + + @Test(expected = NestedServletException.class) + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotDeleteACurvePoint_withInvalidId() throws Exception { + + // tries to delete a curvePoint from database with invalid Id + mockMvc.perform(get("/curvePoint/delete/{id}", 0)) + .andExpect(status().is5xxServerError()); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_AddANewValidCurvePoint() throws Exception { + // creates a curvePoint to add to database + CurvePoint curvePoint = new CurvePoint(10, 10D, 30D); + + // tries to add the curvePoint to database + mockMvc + .perform(post("/curvePoint/validate") + .param("curveId", curvePoint.getCurveId().toString()) + .param("term", curvePoint.getTerm().toString()) + .param("value", curvePoint.getValue().toString())) + .andExpect(model().hasNoErrors()) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/curvePoint/list")); + + // retrieves the curvePoint from database + Iterable curvePointsIterable = new ArrayList<>(); + curvePointsIterable = curvePointService.findAllCurvePoints(); + List curvePoints = new ArrayList<>(); + curvePointsIterable.forEach(curve -> curvePoints.add(curve)); + curvePoint = curvePoints.get(curvePoints.size() - 1); + + // deletes the curvePoint from database + curvePointService.deleteCurvePoint(curvePoint); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotAddANewInvalidCurvePoint() throws Exception { + // creates an invalid curvePoint to add to database + CurvePoint curvePoint = new CurvePoint(0, 10d, 30d); + + // tries to add the curvePoint to database + mockMvc + .perform(post("/curvePoint/validate") + .param("curveId", curvePoint.getCurveId().toString()) + .param("term", curvePoint.getTerm().toString()) + .param("value", curvePoint.getValue().toString())) + .andExpect(model().hasErrors()) + .andExpect(view().name("curvePoint/add")); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UpdateValidCurvePoint() throws Exception { + // creates a curvePoint to update + CurvePoint curvePoint = new CurvePoint(10, 10d, 30d); + curvePoint = curvePointService.saveCurvePoint(curvePoint); + + // tries to update the curvePoint + mockMvc + .perform(post("/curvePoint/update/{id}", curvePoint.getId()) + .param("curveId", curvePoint.getCurveId().toString()) + .param("term", curvePoint.getTerm().toString()) + .param("value", "20D")) + .andExpect(model().hasNoErrors()) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/curvePoint/list")); + + // deletes the curvePoint + curvePointService.deleteCurvePoint(curvePoint); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotUpdateInvalidCurvePoint() throws Exception { + // creates a curvePoint to update + CurvePoint curvePoint = new CurvePoint(10, 10d, 30d); + curvePoint = curvePointService.saveCurvePoint(curvePoint); + + // tries to update the curvePoint with invalid value + mockMvc + .perform(post("/curvePoint/update/{id}", curvePoint.getId()) + .param("curveId", curvePoint.getCurveId().toString()) + .param("term", curvePoint.getTerm().toString()) + .param("value", "")) + .andExpect(model().hasErrors()) + .andExpect(view().name("/curvePoint/update")); + + // deletes the curvePoint to update + curvePointService.deleteCurvePoint(curvePoint); + } + + @Test(expected = NestedServletException.class) + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotUpdateCurvePoint_withInvalidId() throws Exception { + // tries to update a curvePoint with invalid Id + mockMvc + .perform(get("/curvePoint/update/{id}", 0)) + .andExpect(status().is5xxServerError()); + } +} diff --git a/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/LoginControllerTest.java b/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/LoginControllerTest.java new file mode 100644 index 0000000000..5e4f4654d6 --- /dev/null +++ b/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/LoginControllerTest.java @@ -0,0 +1,42 @@ +package com.nnk.springboot.controllers; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc +@WebAppConfiguration +public class LoginControllerTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void test_loginPageDisplaying() throws Exception { + mockMvc.perform(get("/app/login")) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("http://localhost/login")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_allUserArticles() throws Exception { + mockMvc.perform(get("/app/secure/article-details")) + .andExpect(status().isOk()) + .andExpect(view().name("user/list")); + } + +} diff --git a/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/RatingControllerTest.java b/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/RatingControllerTest.java new file mode 100644 index 0000000000..35d250964f --- /dev/null +++ b/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/RatingControllerTest.java @@ -0,0 +1,228 @@ +package com.nnk.springboot.controllers; + +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.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.web.util.NestedServletException; + +import com.nnk.springboot.domain.Rating; +import com.nnk.springboot.services.RatingService; + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc +@WebAppConfiguration +public class RatingControllerTest { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private RatingService ratingService; + + @Test + public void test_RatingPages_NeedAuthentication() throws Exception { + mockMvc.perform(get("curvePoint/list")) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("http://localhost/login")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_AdminAuthentication_PermitRatingListAccess() throws Exception{ + mockMvc.perform(get("/rating/list")) + .andExpect(status().isOk()) + .andExpect(view().name("rating/list")); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UserAuthentication_PermitRatingListAccess() throws Exception{ + mockMvc.perform(get("/rating/list")) + .andExpect(status().isOk()) + .andExpect(view().name("rating/list")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_AdminAuthentication_PermitRatingAddAccess() throws Exception{ + mockMvc.perform(get("/rating/add")) + .andExpect(status().isOk()) + .andExpect(view().name("rating/add")); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UserAuthentication_PermitRatingAddAccess() throws Exception{ + mockMvc.perform(get("/rating/add")) + .andExpect(status().isOk()) + .andExpect(view().name("rating/add")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_AdminAuthentication_PermitRatingUpdateAccess() throws Exception{ + // saves a rating to update in database + Rating rating = new Rating("Moodys Rating", "Sand PRating", "Fitch Rating", 10); + rating = ratingService.saveRating(rating); + + // tries to access to update page + mockMvc.perform(get("/rating/update/" + rating.getId())) + .andExpect(status().isOk()) + .andExpect(view().name("rating/update")); + + // deletes the rating from the database + ratingService.deleteRating(rating); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UserAuthenticationPermit_RatingUpdateAccess() throws Exception{ + // saves a rating to update in database + Rating rating = new Rating("Moodys Rating Test", "Sand PRating Test", "Fitch Rating", 10); + rating = ratingService.saveRating(rating); + + // tries to access to update page + mockMvc.perform(get("/rating/update/" + rating.getId())) + .andExpect(status().isOk()) + .andExpect(view().name("rating/update")); + + // deletes the rating from the database + ratingService.deleteRating(rating); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_DeleteARating() throws Exception { + // saves a rating to delete in database + Rating rating = new Rating("Moodys Rating Test", "Sand PRating Test", "Fitch Rating Test", 10); + rating = ratingService.saveRating(rating); + + // deletes the rating from database + mockMvc.perform(get("/rating/delete/{id}", rating.getId())) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/rating/list")); + } + + @Test(expected = NestedServletException.class) + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotDeleteARating_withInvalidId() throws Exception { + + // tries to delete a rating from database with invalid Id + mockMvc.perform(get("/rating/delete/{id}", 0)) + .andExpect(status().is5xxServerError()); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_AddANewValidRating() throws Exception { + // creates a rating to add to database + Rating rating = new Rating("Moodys Rating Test", "Sand PRating Test", "Fitch Rating Test", 10); + + // tries to add the rating to database + mockMvc + .perform(post("/rating/validate") + .param("moodysRating", rating.getMoodysRating()) + .param("sandPRating", rating.getSandPRating()) + .param("fitchRating", rating.getFitchRating()) + .param("orderNumber", rating.getOrderNumber().toString())) + .andExpect(model().hasNoErrors()) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/rating/list")); + + // retrieves the rating from database + Iterable ratingsIterable = new ArrayList<>(); + ratingsIterable = ratingService.findAllRatings(); + List ratings = new ArrayList<>(); + ratingsIterable.forEach(r -> ratings.add(r)); + rating = ratings.get(ratings.size() - 1); + + // deletes the rating from database + ratingService.deleteRating(rating); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotAddANewInvalidRating() throws Exception { + // creates an invalid rating to add to database + Rating rating = new Rating("Moodys Rating Test", "Sand PRating Test", "Fitch Rating Test", 10); + + // tries to add the rating to database + mockMvc + .perform(post("/rating/validate") + .param("moodysRating", "") + .param("sandPRating", rating.getSandPRating()) + .param("fitchRating", rating.getFitchRating()) + .param("orderNumber", rating.getOrderNumber().toString())) + .andExpect(model().hasErrors()) + .andExpect(view().name("rating/add")); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UpdateValidRating() throws Exception { + // creates a rating to update + Rating rating = new Rating("Moodys Rating Test", "Sand PRating Test", "Fitch Rating Test", 10); + rating = ratingService.saveRating(rating); + + // tries to update the rating + mockMvc + .perform(post("/rating/update/{id}", rating.getId()) + .param("moodysRating", rating.getMoodysRating()) + .param("sandPRating", rating.getSandPRating()) + .param("fitchRating", rating.getFitchRating()) + .param("orderNumber", "20")) + .andExpect(model().hasNoErrors()) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/rating/list")); + + // deletes the rating + ratingService.deleteRating(rating); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotUpdateInvalidRating() throws Exception { + // creates a rating to update + Rating rating = new Rating("Moodys Rating Test", "Sand PRating Test", "Fitch Rating Test", 10); + rating = ratingService.saveRating(rating); + + // tries to update the rating with invalid order number + mockMvc + .perform(post("/rating/update/{id}", rating.getId()) + .param("moodysRating", rating.getMoodysRating()) + .param("sandPRating", rating.getSandPRating()) + .param("fitchRating", rating.getFitchRating()) + .param("orderNumber", "0")) + .andExpect(model().hasErrors()) + .andExpect(view().name("/rating/update")); + + // deletes the rating to update + ratingService.deleteRating(rating); + } + + @Test(expected = NestedServletException.class) + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotUpdateRating_withInvalidId() throws Exception { + // tries to update a rating with invalid Id + mockMvc + .perform(get("/rating/update/{id}", 0)) + .andExpect(status().is5xxServerError()); + } +} diff --git a/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/RuleNameControllerTest.java b/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/RuleNameControllerTest.java new file mode 100644 index 0000000000..5cdb76615a --- /dev/null +++ b/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/RuleNameControllerTest.java @@ -0,0 +1,237 @@ +package com.nnk.springboot.controllers; + +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.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.web.util.NestedServletException; + +import com.nnk.springboot.domain.RuleName; +import com.nnk.springboot.services.RuleNameService; + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc +@WebAppConfiguration + +public class RuleNameControllerTest { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private RuleNameService ruleNameService; + + @Test + public void test_RuleNamePages_NeedAuthentication() throws Exception { + mockMvc.perform(get("ruleName/list")) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("http://localhost/login")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_AdminAuthentication_PermitRuleNameListAccess() throws Exception{ + mockMvc.perform(get("/ruleName/list")) + .andExpect(status().isOk()) + .andExpect(view().name("ruleName/list")); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UserAuthentication_PermitRuleNameListAccess() throws Exception{ + mockMvc.perform(get("/ruleName/list")) + .andExpect(status().isOk()) + .andExpect(view().name("ruleName/list")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_AdminAuthentication_PermitRuleNameAddAccess() throws Exception{ + mockMvc.perform(get("/ruleName/add")) + .andExpect(status().isOk()) + .andExpect(view().name("ruleName/add")); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UserAuthentication_PermitRuleNameAddAccess() throws Exception{ + mockMvc.perform(get("/ruleName/add")) + .andExpect(status().isOk()) + .andExpect(view().name("ruleName/add")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_AdminAuthentication_PermitRuleNameUpdateAccess() throws Exception{ + // saves a rule to update in database + RuleName ruleName = new RuleName("Rule Name", "Description", "Json", "Template", "SQL", "SQL Part"); + ruleName = ruleNameService.saveRuleName(ruleName); + + // tries to access to update page + mockMvc.perform(get("/ruleName/update/" + ruleName.getId())) + .andExpect(status().isOk()) + .andExpect(view().name("ruleName/update")); + + // deletes the rule from the database + ruleNameService.deleteRuleName(ruleName); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UserAuthentication_PermitRuleNameUpdateAccess() throws Exception{ + // saves a rule to update in database + RuleName ruleName = new RuleName("Rule Name", "Description", "Json", "Template", "SQL", "SQL Part"); + ruleName = ruleNameService.saveRuleName(ruleName); + + // tries to access to update page + mockMvc.perform(get("/ruleName/update/" + ruleName.getId())) + .andExpect(status().isOk()) + .andExpect(view().name("ruleName/update")); + + // deletes the rule from the database + ruleNameService.deleteRuleName(ruleName); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_DeleteARuleName() throws Exception { + // saves a rule to delete in database + RuleName ruleName = new RuleName("Rule Name", "Description", "Json", "Template", "SQL", "SQL Part"); + ruleName = ruleNameService.saveRuleName(ruleName); + + // deletes the rule from database + mockMvc.perform(get("/ruleName/delete/{id}", ruleName.getId())) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/ruleName/list")); + } + + @Test(expected = NestedServletException.class) + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotDeleteARuleName_withInvalidId() throws Exception { + + // tries to delete a rule from database with invalid Id + mockMvc.perform(get("/ruleName/delete/{id}", 0)) + .andExpect(status().is5xxServerError()); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_AddANewValidRuleName() throws Exception { + // creates a rule to add to database + RuleName ruleName = new RuleName("Rule Name", "Description", "Json", "Template", "SQL", "SQL Part"); + + // tries to add the rule to database + mockMvc + .perform(post("/ruleName/validate") + .param("name", ruleName.getName()) + .param("description", ruleName.getDescription()) + .param("json", ruleName.getJson()) + .param("template", ruleName.getTemplate()) + .param("sqlStr", ruleName.getSqlStr()) + .param("sqlPart", ruleName.getSqlPart())) + .andExpect(model().hasNoErrors()) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/ruleName/list")); + + // retrieves the rule from database + Iterable ruleNamesIterable = new ArrayList<>(); + ruleNamesIterable = ruleNameService.findAllRuleNames(); + List ruleNames = new ArrayList<>(); + ruleNamesIterable.forEach(bidList -> ruleNames.add(bidList)); + ruleName = ruleNames.get(ruleNames.size() - 1); + + // deletes the rule from database + ruleNameService.deleteRuleName(ruleName); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotAddANewInvalidRuleName() throws Exception { + // creates an invalid rule to add to database + RuleName ruleName = new RuleName("", "Description", "Json", "Template", "SQL", "SQL Part"); + + // tries to add the rule to database + mockMvc + .perform(post("/ruleName/validate") + .param("name", ruleName.getName()) + .param("description", ruleName.getDescription()) + .param("json", ruleName.getJson()) + .param("template", ruleName.getTemplate()) + .param("sqlStr", ruleName.getSqlStr()) + .param("sqlPart", ruleName.getSqlPart())) + .andExpect(model().hasErrors()) + .andExpect(view().name("ruleName/add")); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UpdateValidRuleName() throws Exception { + // creates a rule to update + RuleName ruleName = new RuleName("Rule Name", "Description", "Json", "Template", "SQL", "SQL Part"); + ruleName = ruleNameService.saveRuleName(ruleName); + + // tries to update the rule + mockMvc + .perform(post("/ruleName/update/{id}", ruleName.getId()) + .param("name", "Rule Name updated") + .param("description", ruleName.getDescription()) + .param("json", ruleName.getJson()) + .param("template", ruleName.getTemplate()) + .param("sqlStr", ruleName.getSqlStr()) + .param("sqlPart", ruleName.getSqlPart())) + .andExpect(model().hasNoErrors()) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/ruleName/list")); + + // deletes the rule + ruleNameService.deleteRuleName(ruleName); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotUpdateInvalidRuleName() throws Exception { + // creates a rule to update + RuleName ruleName = new RuleName("Rule Name", "Description", "Json", "Template", "SQL", "SQL Part"); + ruleName = ruleNameService.saveRuleName(ruleName); + + // tries to update the rule with invalid name + mockMvc + .perform(post("/ruleName/update/{id}", ruleName.getId()) + .param("name", "") + .param("description", ruleName.getDescription()) + .param("json", ruleName.getJson()) + .param("template", ruleName.getTemplate()) + .param("sqlStr", ruleName.getSqlStr()) + .param("sqlPart", ruleName.getSqlPart())) + .andExpect(model().hasErrors()) + .andExpect(view().name("/ruleName/update")); + + // deletes the rule to update + ruleNameService.deleteRuleName(ruleName); + } + + @Test(expected = NestedServletException.class) + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotUpdateRuleName_withInvalidId() throws Exception { + // tries to update a rule with invalid Id + mockMvc + .perform(get("/ruleName/update/{id}", 0)) + .andExpect(status().is5xxServerError()); + } +} diff --git a/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/TradeControllerTest.java b/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/TradeControllerTest.java new file mode 100644 index 0000000000..0da3319f29 --- /dev/null +++ b/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/TradeControllerTest.java @@ -0,0 +1,226 @@ +package com.nnk.springboot.controllers; + +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.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.web.util.NestedServletException; + +import com.nnk.springboot.domain.Trade; +import com.nnk.springboot.services.TradeService; + + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc +@WebAppConfiguration + +public class TradeControllerTest { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private TradeService tradeService; + + @Test + public void test_TradePages_NeedAuthentication() throws Exception { + mockMvc.perform(get("trade/list")) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("http://localhost/login")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_AdminAuthentication_PermitTradeListAccess() throws Exception{ + mockMvc.perform(get("/trade/list")) + .andExpect(status().isOk()) + .andExpect(view().name("trade/list")); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UserAuthentication_PermitTradeListAccess() throws Exception{ + mockMvc.perform(get("/trade/list")) + .andExpect(status().isOk()) + .andExpect(view().name("trade/list")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_AdminAuthentication_PermitTradeAddAccess() throws Exception{ + mockMvc.perform(get("/trade/add")) + .andExpect(status().isOk()) + .andExpect(view().name("trade/add")); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UserAuthentication_PermitTradeAddAccess() throws Exception{ + mockMvc.perform(get("/trade/add")) + .andExpect(status().isOk()) + .andExpect(view().name("trade/add")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_AdminAuthentication_PermitTradeUpdateAccess() throws Exception{ + // saves a trade to update in database + Trade trade = new Trade("Trade Account", "Type", 10D); + trade = tradeService.saveTrade(trade); + + // tries to access to update page + mockMvc.perform(get("/trade/update/" + trade.getTradeId())) + .andExpect(status().isOk()) + .andExpect(view().name("trade/update")); + + // deletes the trade from the database + tradeService.deleteTrade(trade); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UserAuthentication_PermitTradeUpdateAccess() throws Exception{ + // saves a trade to update in database + Trade trade = new Trade("Trade Account", "Type", 10D); + trade = tradeService.saveTrade(trade); + + // tries to access to update page + mockMvc.perform(get("/trade/update/" + trade.getTradeId())) + .andExpect(status().isOk()) + .andExpect(view().name("trade/update")); + + // deletes the trade from the database + tradeService.deleteTrade(trade); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_DeleteATrade() throws Exception { + // saves a trade to update in database + Trade trade = new Trade("Trade Account", "Type", 10D); + trade = tradeService.saveTrade(trade); + + // deletes the trade from database + mockMvc.perform(get("/trade/delete/{id}", trade.getTradeId())) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/trade/list")); + } + + @Test(expected = NestedServletException.class) + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotDeleteATrade_withInvalidId() throws Exception { + + // tries to delete a trade from database with invalid Id + mockMvc.perform(get("/trade/delete/{id}", 0)) + .andExpect(status().is5xxServerError()); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_AddANewValidTrade() throws Exception { + // creates a trade to add to database + Trade trade = new Trade("Trade Account", "Type", 10D); + + // tries to add the trade to database + mockMvc + .perform(post("/trade/validate") + .param("account", trade.getAccount()) + .param("type", trade.getType()) + .param("buyQuantity", trade.getBuyQuantity().toString())) + .andExpect(model().hasNoErrors()) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/trade/list")); + + // retrieves the trade from database + Iterable tradesIterable = new ArrayList<>(); + tradesIterable = tradeService.findAllTrades(); + List trades = new ArrayList<>(); + tradesIterable.forEach(t -> trades.add(t)); + trade = trades.get(trades.size() - 1); + + // deletes the trade from database + tradeService.deleteTrade(trade); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotAddANewInvalidTrade() throws Exception { + // creates an invalid trade to add to database + Trade trade = new Trade("Account", "Type", 0D); + + // tries to add the trade to database + mockMvc + .perform(post("/trade/validate") + .param("account", trade.getAccount()) + .param("type", trade.getType()) + .param("buyQuantity", trade.getBuyQuantity().toString())) + .andExpect(model().hasErrors()) + .andExpect(view().name("trade/add")); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UpdateValidTrade() throws Exception { + // creates a trade to update + Trade trade = new Trade("Trade Account", "Type", 10D); + trade = tradeService.saveTrade(trade); + + // tries to update the trade + mockMvc + .perform(post("/trade/update/{id}", trade.getTradeId()) + .param("account", "Trade Account updated") + .param("type", trade.getType()) + .param("buyQuantity", trade.getBuyQuantity().toString())) + .andExpect(model().hasNoErrors()) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/trade/list")); + + // deletes the trade + tradeService.deleteTrade(trade); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotUpdateInvalidTrade() throws Exception { + // creates a trade to update + Trade trade = new Trade("Trade Account", "Type", 10D); + trade = tradeService.saveTrade(trade); + + // tries to update the trade with invalid account + mockMvc + .perform(post("/trade/update/{id}", trade.getTradeId()) + .param("account", "") + .param("type", trade.getType()) + .param("buyQuantity", trade.getBuyQuantity().toString())) + .andExpect(model().hasErrors()) + .andExpect(view().name("trade/update")); + + // deletes the trade to update + tradeService.deleteTrade(trade); + } + + @Test(expected = NestedServletException.class) + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_NotUpdateTrade_withInvalidId() throws Exception { + // tries to update a bidList with invalid Id + mockMvc + .perform(get("/trade/update/{id}", 0)) + .andExpect(status().is5xxServerError()); + } +} diff --git a/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/UserControllerTest.java b/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/UserControllerTest.java new file mode 100644 index 0000000000..afd3b7507e --- /dev/null +++ b/Poseiden-skeleton/src/test/java/com/nnk/springboot/controllers/UserControllerTest.java @@ -0,0 +1,248 @@ +package com.nnk.springboot.controllers; + +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.result.MockMvcResultMatchers.forwardedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.web.util.NestedServletException; + +import com.nnk.springboot.domain.User; +import com.nnk.springboot.services.UserService; + + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc +@WebAppConfiguration +public class UserControllerTest { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private UserService userService; + + @Test + public void test_UserPages_NeedAuthentication() throws Exception { + mockMvc.perform(get("user/list")) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("http://localhost/login")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_AdminAuthentication_PermitUserListAccess() throws Exception{ + mockMvc.perform(get("/user/list")) + .andExpect(status().isOk()) + .andExpect(view().name("user/list")); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UserAuthentication_NotPermitUserListAccess() throws Exception{ + mockMvc.perform(get("/user/list")) + .andExpect(status().is4xxClientError()) + .andExpect(forwardedUrl("/app/error")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_AdminAuthentication_PermitUserAddAccess() throws Exception{ + mockMvc.perform(get("/user/add")) + .andExpect(status().isOk()) + .andExpect(view().name("user/add")); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UserAuthentication_NotPermitUserAddAccess() throws Exception{ + mockMvc.perform(get("/user/add")) + .andExpect(status().is4xxClientError()) + .andExpect(forwardedUrl("/app/error")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_AdminAuthentication_PermitUserUpdateAccess() throws Exception{ + // saves a user to update in database + User user = new User("UserName", "Passw0rd!", "UserTestName", "USER"); + user = userService.saveUser(user); + + // tries to access to update page + mockMvc.perform(get("/user/update/" + user.getId())) + .andExpect(status().isOk()) + .andExpect(view().name("user/update")); + + // deletes the user from the database + userService.deleteUser(user); + } + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "USER") + public void test_UserAuthentication_NotPermitUserUpdateAccess() throws Exception{ + // saves a user to update in database + User user = new User("UserName", "Passw0rd!", "UserTestName", "USER"); + user = userService.saveUser(user); + + // tries to access to update page + mockMvc.perform(get("/user/update/" + user.getId())) + .andExpect(status().is4xxClientError()) + .andExpect(forwardedUrl("/app/error")); + + // deletes the user from the database + userService.deleteUser(user); + } + + + @Test + @WithMockUser(username = "userTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_DeleteAnUser() throws Exception { + // saves a user to delete in database + User user = new User("UserName", "Passw0rd!", "UserTestName", "USER"); + user = userService.saveUser(user); + + // deletes the user from database + mockMvc.perform(get("/user/delete/{id}", user.getId())) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/user/list")); + } + + @Test(expected = NestedServletException.class) + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_NotDeleteAnUser_withInvalidId() throws Exception { + + // tries to delete a user from database with invalid Id + mockMvc.perform(get("/user/delete/{id}", 0)) + .andExpect(status().is5xxServerError()); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_AddANewValidUser() throws Exception { + // creates a user to add to database + User user = new User("UserName", "Passw0rd!", "UserTestName", "USER"); + + // tries to add the user to database + mockMvc + .perform(post("/user/validate") + .param("username", user.getUsername()) + .param("password", user.getPassword()) + .param("fullname", user.getFullname()) + .param("role", user.getRole())) + .andExpect(model().hasNoErrors()) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/user/list")); + + // retrieves the user from database + Iterable usersIterable = new ArrayList<>(); + usersIterable = userService.findAllUsers(); + List users = new ArrayList<>(); + usersIterable.forEach(u -> users.add(u)); + user = users.get(users.size() - 1); + + // deletes the user from database + userService.deleteUser(user); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_NotAddANewInvalidUser() throws Exception { + // creates an invalid user to add to database + User user = new User("", "Passw0rd!", "UserTestName", "USER"); + + // tries to add the user to database + mockMvc + .perform(post("/user/validate") + .param("username", user.getUsername()) + .param("password", user.getPassword()) + .param("fullname", user.getFullname()) + .param("role", user.getRole())) + .andExpect(model().hasErrors()) + .andExpect(view().name("user/add")); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_UpdateValidUser() throws Exception { + // creates a user to update + User user = new User("UserName", "Passw0rd!", "UserTestName", "USER"); + user = userService.saveUser(user); + + // tries to update the user + mockMvc + .perform(post("/user/update/{id}", user.getId()) + .param("username", "UserName updated") + .param("password", user.getPassword()) + .param("fullname", user.getFullname()) + .param("role", user.getRole())) + .andExpect(model().hasNoErrors()) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl("/user/list")); + + // deletes the user + userService.deleteUser(user); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_NotUpdateInvalidUser() throws Exception { + // creates a user to update + User user = new User("UserName", "Passw0rd!", "UserTestName", "USER"); + user = userService.saveUser(user); + + // tries to update the user with invalid username + mockMvc + .perform(post("/user/update/{id}", user.getId()) + .param("username", "") + .param("password", user.getPassword()) + .param("fullname", user.getFullname()) + .param("role", user.getRole())) + .andExpect(model().hasErrors()) + .andExpect(view().name("user/update")); + + // deletes the user to update + userService.deleteUser(user); + } + + @Test + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_NotAddUser_withInvalidPassword() throws Exception { + // creates an invalid user to add to database + User user = new User("UserName", "password", "UserTestName", "USER"); + + // tries to add the user to database + mockMvc + .perform(post("/user/validate") + .param("username", user.getUsername()) + .param("password", user.getPassword()) + .param("fullname", user.getFullname()) + .param("role", user.getRole())) + .andExpect(model().hasErrors()) + .andExpect(view().name("user/add")); + } + + @Test(expected = NestedServletException.class) + @WithMockUser(username = "adminTest", password = "Passw0rd!", authorities = "ADMIN") + public void test_NotUpdateUser_withInvalidId() throws Exception { + // tries to update a user with invalid Id + mockMvc + .perform(get("/user/update/{id}", 0)) + .andExpect(status().is5xxServerError()); + } +}