Skip to content
Snippets Groups Projects
Commit 9663b357 authored by Sara Prifti's avatar Sara Prifti
Browse files

Fixed getCustomerDiscountCards Test

parent 819d09ba
No related branches found
No related tags found
No related merge requests found
......@@ -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>
......
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);
};
}
}
......@@ -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 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);
}
}
......@@ -29,30 +29,36 @@ public class CustomerController implements CustomersApi{
@PostMapping("")
public ResponseEntity<Customer> addCustomer(@ApiParam(value = "Customer object that should be added",required = true) @Valid @RequestBody Customer body) {
//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) {
//check if discount card id exists
if(body.getId()==null) {
return ResponseEntity.status(400).body(null);
}
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) {
if(exists==false) {
return ResponseEntity.status(404).body(null);
}
boolean conflict=false;
if( DataService.discountCardMap.containsKey(customerId)) {
List<DiscountCard> discountCardList = DataService.discountCardMap.get(customerId);
......@@ -62,12 +68,13 @@ public class CustomerController implements CustomersApi{
conflict = true;
}
}
if(conflict) {
if(conflict==true) {
return ResponseEntity.status(409).body(null);
}else {
discountCardList.add(body);
}
}else {
//if the discount card does not exists for the customer id
List<DiscountCard> discountCardList = new ArrayList<DiscountCard>();
discountCardList.add(body);
DataService.discountCardMap.put(customerId, discountCardList);
......@@ -77,7 +84,8 @@ public class CustomerController implements CustomersApi{
@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));
......
......@@ -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";
......@@ -129,8 +129,7 @@ public class CustomerTest {
RestAssured.given().when().get(path).then().statusCode(404);
//201
//200
customerId = DataService.customerList.get(0).getId();
path = "/customers/"+customerId+"/discountcards";
RestAssured.given().when().get(path).then().statusCode(200);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment