From 9663b3571b34644fcc200d94a74dd5ee39f28c61 Mon Sep 17 00:00:00 2001 From: Sara Prifti <72574638+sarapr97@users.noreply.github.com> Date: Thu, 26 May 2022 19:46:59 +0200 Subject: [PATCH] Fixed getCustomerDiscountCards Test --- pom.xml | 4 ++ .../java/de/rwth/swc/sqa/Application.java | 29 +++++++- .../java/de/rwth/swc/sqa/DataService.java | 18 ++++- .../rwth/swc/sqa/api/CustomerController.java | 66 +++++++++++-------- .../java/de/rwth/swc/sqa/CustomerTest.java | 9 ++- 5 files changed, 88 insertions(+), 38 deletions(-) diff --git a/pom.xml b/pom.xml index afe2aab..bbc8115 100644 --- a/pom.xml +++ b/pom.xml @@ -69,6 +69,10 @@ <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + </dependency> </dependencies> <build> diff --git a/src/main/java/de/rwth/swc/sqa/Application.java b/src/main/java/de/rwth/swc/sqa/Application.java index a4d9ff0..a3bd13e 100644 --- a/src/main/java/de/rwth/swc/sqa/Application.java +++ b/src/main/java/de/rwth/swc/sqa/Application.java @@ -1,11 +1,36 @@ package de.rwth.swc.sqa; +import de.rwth.swc.sqa.model.Customer; +import de.rwth.swc.sqa.model.DiscountCard; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import java.util.ArrayList; +import java.util.List; + +@Slf4j @SpringBootApplication public class Application { - public static void main(String[] args) { - SpringApplication.run(Application.class, args); + + public static void main(String[] args) {SpringApplication.run(Application.class, args);} + + @Bean + CommandLineRunner run(DataService dataService) { + return args -> { + Customer customer=new Customer(); + customer.setId((long) Math.random()); + customer.setBirthdate(String.valueOf(Math.random())); + dataService.saveCustomer(customer); + List<DiscountCard> discountCards= new ArrayList<>(); + DiscountCard discountCard= new DiscountCard(); + discountCard.setId((long) Math.random()); + discountCard.setCustomerId(customer.getId()); + discountCards.add(discountCard); + dataService.saveDiscountCardToCustomer(customer.getId(), discountCards); + + }; } } diff --git a/src/main/java/de/rwth/swc/sqa/DataService.java b/src/main/java/de/rwth/swc/sqa/DataService.java index 7de0f65..e1f5cdc 100644 --- a/src/main/java/de/rwth/swc/sqa/DataService.java +++ b/src/main/java/de/rwth/swc/sqa/DataService.java @@ -5,13 +5,27 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import com.fasterxml.jackson.annotation.JsonSetter; import de.rwth.swc.sqa.model.Customer; import de.rwth.swc.sqa.model.DiscountCard; import de.rwth.swc.sqa.model.Ticket; +import lombok.Data; +import org.springframework.boot.CommandLineRunner; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Service; +@Service public class DataService { - public static List<Customer> customerList = new ArrayList<Customer>(); + public static List<Customer> customerList = new ArrayList<Customer>(); public static Map<Long, List<DiscountCard>> discountCardMap = new HashMap<Long, List<DiscountCard>>(); + public static List<Ticket> ticketList = new ArrayList<Ticket>(); - + + public void saveCustomer(Customer customer){ + customerList.add(customer); + } + + public void saveDiscountCardToCustomer(Long customer_id, List<DiscountCard> discountCard){ + discountCardMap.put(customer_id,discountCard); + } } diff --git a/src/main/java/de/rwth/swc/sqa/api/CustomerController.java b/src/main/java/de/rwth/swc/sqa/api/CustomerController.java index 2f683ef..773dc10 100644 --- a/src/main/java/de/rwth/swc/sqa/api/CustomerController.java +++ b/src/main/java/de/rwth/swc/sqa/api/CustomerController.java @@ -29,55 +29,63 @@ public class CustomerController implements CustomersApi{ @PostMapping("") public ResponseEntity<Customer> addCustomer(@ApiParam(value = "Customer object that should be added",required = true) @Valid @RequestBody Customer body) { - if(body.getId()!=null || body.getBirthdate().equals("") ) { + //id and birthday should not be empty + if(body.getId()!=null || body.getBirthdate().equals("") ) { return ResponseEntity.status(400).body(null); }else if(body.getDisabled()==null) { body.setDisabled(false); } + //set a random Id for the new customer to be added body.setId(System.currentTimeMillis()); + //save customer to the list of existing customers DataService.customerList.add(body); return ResponseEntity.status(201).body(body); } @PostMapping("/{customerId}/discountcards") public ResponseEntity<DiscountCard> addDiscountCardToCustomer(@ApiParam(value = "ID of customer",required = true) @PathVariable("customerId") Long customerId, @ApiParam(value = "DiscountCard object that needs to be added to the customer",required = true) @RequestBody @Valid DiscountCard body) { - if(body.getId()==null) { + //check if discount card id exists + if(body.getId()==null) { return ResponseEntity.status(400).body(null); - } - boolean exists = false; - for(Customer customer: DataService.customerList) { - if(customer.getId().longValue()==customerId.longValue()) { - exists = true; - } - } - if(!exists) { + } + boolean exists = false; + //check if customer is already found in the customer list based on its id + for(Customer customer: DataService.customerList) { + if(customer.getId().longValue()==customerId.longValue()) { + exists = true; + } + } + if(exists==false) { return ResponseEntity.status(404).body(null); - } - boolean conflict=false; - if( DataService.discountCardMap.containsKey(customerId)) { - List<DiscountCard> discountCardList = DataService.discountCardMap.get(customerId); - for(DiscountCard card:discountCardList) { + } + + boolean conflict=false; + if( DataService.discountCardMap.containsKey(customerId)) { + List<DiscountCard> discountCardList = DataService.discountCardMap.get(customerId); + for(DiscountCard card:discountCardList) { - if(card.getValidFrom().equals(body.getValidFrom())) { - conflict = true; + if(card.getValidFrom().equals(body.getValidFrom())) { + conflict = true; + } + } + if(conflict==true) { + return ResponseEntity.status(409).body(null); + }else { + discountCardList.add(body); } - } - if(conflict) { - return ResponseEntity.status(409).body(null); - }else { + }else { + //if the discount card does not exists for the customer id + List<DiscountCard> discountCardList = new ArrayList<DiscountCard>(); discountCardList.add(body); - } - }else { - List<DiscountCard> discountCardList = new ArrayList<DiscountCard>(); - discountCardList.add(body); - DataService.discountCardMap.put(customerId, discountCardList); - } + DataService.discountCardMap.put(customerId, discountCardList); + } return ResponseEntity.status(201).body(body); } @GetMapping("/{customerId}/discountcards") public ResponseEntity<List<DiscountCard>> getCustomerDiscountCards(@ApiParam(value = "ID of customer to search for discount cards",required = true) @PathVariable("customerId") Long customerId) { - if(customerId.longValue()==0) { + //if customer id is not valid => customer id is null + if(customerId==null) { return ResponseEntity.status(400).body(null); } boolean exists = false; @@ -86,7 +94,7 @@ public class CustomerController implements CustomersApi{ exists = true; } } - if(!exists) { + if(exists==false) { return ResponseEntity.status(404).body(null); } return ResponseEntity.ok().body( DataService.discountCardMap.get(customerId)); diff --git a/src/test/java/de/rwth/swc/sqa/CustomerTest.java b/src/test/java/de/rwth/swc/sqa/CustomerTest.java index 8bbaf72..7769b3a 100644 --- a/src/test/java/de/rwth/swc/sqa/CustomerTest.java +++ b/src/test/java/de/rwth/swc/sqa/CustomerTest.java @@ -117,7 +117,7 @@ public class CustomerTest { @Test @Order(3) public void getCustomerDiscountCardTest() { - Long customerId =0L; + Long customerId=null; String path = "/customers/"+customerId+"/discountcards"; //400 path = "/customers/"+customerId+"/discountcards"; @@ -128,10 +128,9 @@ public class CustomerTest { path = "/customers/"+customerId+"/discountcards"; RestAssured.given().when().get(path).then().statusCode(404); - - - //201 - customerId = DataService.customerList.get(0).getId(); + + //200 + customerId = DataService.customerList.get(0).getId(); path = "/customers/"+customerId+"/discountcards"; RestAssured.given().when().get(path).then().statusCode(200); -- GitLab