Skip to content
Snippets Groups Projects
Commit 954d739f authored by Ahmad Arslan's avatar Ahmad Arslan
Browse files

Added the suggested changes in the comments of the assignment.

parent 56753560
Branches master
No related tags found
No related merge requests found
......@@ -2,9 +2,11 @@ version: "3"
services:
sonarqube:
image: sonarqube:community
platform: linux/amd64
depends_on:
- db
environment:
SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar
SONAR_JDBC_USERNAME: sonar
SONAR_JDBC_PASSWORD: sonar
......@@ -28,4 +30,4 @@ volumes:
sonarqube_extensions:
sonarqube_logs:
postgresql:
postgresql_data:
\ No newline at end of file
postgresql_data:
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);}
/*
@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);
};
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
*/
}
......@@ -5,148 +5,13 @@ 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 de.rwth.swc.sqa.model.TicketValidationRequest;
import lombok.Data;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import static de.rwth.swc.sqa.model.Ticket.ValidForEnum.*;
@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);
}
//check restirctions given in the pdf file
//1.Check if the bought ticket can be applied for the customer
public static boolean ticketIsValidForTheCustomerAge(Customer customer, Ticket ticket){
boolean isValid=false;
//if customer is a senior
if(DataService.customerIsSenior(customer)){
if(ticket.getValidFor().getValue().equals("1d") || ticket.getValidFor().getValue().equals("30d") || ticket.getValidFor().getValue().equals("1y")){
if(ticket.getStudent()==false){
isValid=true;
}
return isValid;
}
//if customer is a student
//it does not matter if it`s not a student since he is still an adult and can buy an adult ticket
}else if (DataService.customerIsStudent(customer)){
if (ticket.getValidFor().getValue().equals("30d") || ticket.getValidFor().getValue().equals("1y")) {
isValid = true;
return isValid;
}
}else if (DataService.customerIsChild(customer)){
if((ticket.getValidFor().getValue().equals("1h") || ticket.getValidFor().getValue().equals("30d") || ticket.getValidFor().getValue().equals("1y") )){
if(ticket.getStudent()==false){
isValid=true;
}
return isValid;
}
}else if (DataService.customerIsAdult(customer)){
if(ticket.getStudent()==false){
isValid=true;
return isValid;
}
}
return isValid;
}
public static boolean discountCanBeAppliedOnTheTicket(Ticket ticket){
boolean applyDiscount=false;
if(ticket.getValidFor().getValue().equals("30d") || ticket.getValidFor().getValue().equals("1y")){
if(ticket.getDiscountCard()==true){
applyDiscount=true;
}
}
return applyDiscount;
}
//check if the discountCard can be applied for the requested ticket
public static boolean discountCanBeApplied(DiscountCard discountCard, TicketValidationRequest ticketValidationRequest){
boolean applyDiscount=false;
if(discountCard.getId().equals(ticketValidationRequest.getDiscountCardId())){
if(ticketValidationRequest.getDisabled()){
//if(ticketValidationRequest.get.getValue().equals("30d")|| ticketValidationRequest.getZone().getValue().equals("1y")){
//apply type 25 and 50
applyDiscount=true;
}
}
return applyDiscount;
}
//customer is senior
//Senior >60
//born before 1962
public static boolean customerIsSenior(Customer customer){
boolean isSenior= false;
//senior customer
if(Integer.valueOf(customer.getBirthdate().substring(0,4)) <= 1962 ){
isSenior=true;
}
return isSenior;
}
//customer is a child <14
//born before 2008
public static boolean customerIsChild(Customer customer){
boolean isChild= false;
if(Integer.valueOf(customer.getBirthdate().substring(0,4)) >= 2008 ){
isChild=true;
}
return isChild;
}
//Student <28
//born after 1994
public static boolean customerIsStudent(Customer customer){
boolean isStudent= false;
if(Integer.valueOf(customer.getBirthdate().substring(0,4)) >= 1994 && !customerIsChild(customer) ){
isStudent=true;
}
return isStudent;
}
//Adult is not a child
//after 2008
public static boolean customerIsAdult(Customer customer){
boolean isAdult = false;
if(customerIsChild(customer)==false){
isAdult=true;
}
return isAdult;
}
}
......@@ -24,68 +24,60 @@ import io.swagger.annotations.ApiParam;
@Controller
@RequestMapping("/customers")
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("") ) {
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) {
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==false) {
}
boolean exists = false;
for(Customer customer: DataService.customerList) {
if(customer.getId().longValue()==customerId.longValue()) {
exists = true;
}
}
if(!exists) {
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(conflict==true) {
return ResponseEntity.status(409).body(null);
}else {
discountCardList.add(body);
if(card.getValidFrom().equals(body.getValidFrom())) {
conflict = true;
}
}else {
List<DiscountCard> discountCardList = new ArrayList<DiscountCard>();
}
if(conflict) {
return ResponseEntity.status(409).body(null);
}else {
discountCardList.add(body);
DataService.discountCardMap.put(customerId, discountCardList);
}
}
}else {
List<DiscountCard> discountCardList = new ArrayList<DiscountCard>();
discountCardList.add(body);
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 customer id is not valid => customer id is null
if(customerId==null) {
if(customerId.longValue()==0) {
return ResponseEntity.status(400).body(null);
}
boolean exists = false;
......@@ -94,7 +86,7 @@ public class CustomerController implements CustomersApi{
exists = true;
}
}
if(exists==false) {
if(!exists) {
return ResponseEntity.status(404).body(null);
}
return ResponseEntity.ok().body( DataService.discountCardMap.get(customerId));
......
......@@ -5,7 +5,6 @@ import java.util.List;
import javax.validation.Valid;
import de.rwth.swc.sqa.model.Customer;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
......@@ -25,7 +24,6 @@ public class TicketController implements TicketsApi{
@PostMapping("")
public ResponseEntity<Ticket> buyTicket(@ApiParam(value = "TicketRequest object" ,required=true ) @Valid @RequestBody TicketRequest body) {
try {
// create Ticket
Ticket ticket = new Ticket();
ticket.setId(System.currentTimeMillis());
ticket.setBirthdate(body.getBirthdate());
......@@ -36,17 +34,8 @@ public class TicketController implements TicketsApi{
ticket.setValidFor(Ticket.ValidForEnum.fromValue(body.getValidFor().getValue()));
ticket.setValidFrom(body.getValidFrom());
ticket.setZone(Ticket.ZoneEnum.fromValue(body.getZone().getValue()));
Customer customer=new Customer();
customer.setBirthdate(body.getBirthdate());
//check if ticket is valid for the given inputs and customer is allowed to buy it
if(DataService.ticketIsValidForTheCustomerAge(customer,ticket)){
DataService.ticketList.add(ticket);
return ResponseEntity.ok().body(ticket);
}
return ResponseEntity.status(400).body(null);
DataService.ticketList.add(ticket);
return ResponseEntity.ok().body(ticket);
}catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(400).body(null);
......
......@@ -3,7 +3,9 @@ package de.rwth.swc.sqa;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
......@@ -14,12 +16,15 @@ import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import de.rwth.swc.sqa.model.Customer;
import de.rwth.swc.sqa.model.DiscountCard;
import io.restassured.RestAssured;
import io.restassured.response.ValidatableResponse;
@SpringBootTest(webEnvironment = RANDOM_PORT)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class CustomerTest {
private static List<Customer> customerList = new ArrayList<Customer>();
@LocalServerPort
int serverPort;
......@@ -39,27 +44,29 @@ public class CustomerTest {
parms1.put("disabled", true);
RestAssured.given().header("content-Type", "application/json").and()
.body(parms1).when().post("/customers").then().statusCode(400);
//200
//201
Map<String, Object> parms = new HashMap<String, Object>();
parms.put("birthdate", "2000-01-01");
parms.put("disabled", false);
RestAssured.given().header("content-Type", "application/json").and()
ValidatableResponse response = RestAssured.given().header("content-Type", "application/json").and()
.body(parms).when().post("/customers").then().statusCode(201);
Customer customer = response.extract().as(Customer.class);
customerList.add(customer);
Map<String, Object> parms3 = new HashMap<String, Object>();
parms3.put("birthdate", "2001-01-01");
parms3.put("disabled", false);
RestAssured.given().header("content-Type", "application/json").and()
response = RestAssured.given().header("content-Type", "application/json").and()
.body(parms).when().post("/customers").then().statusCode(201);
customer = response.extract().as(Customer.class);
customerList.add(customer);
}
@Test
@Order(2)
public void addDiscountCardToCustomerTest() {
Long customerId =null;
Long customerId =0L;
String path = "/customers/"+customerId+"/discountcards";
Map<String, Object> parms = new HashMap<String, Object>();
//404
......@@ -75,7 +82,7 @@ public class CustomerTest {
.body(parms).when().post(path).then().statusCode(404);
//400
customerId = DataService.customerList.get(0).getId();
customerId = customerList.get(0).getId();
path = "/customers/"+customerId+"/discountcards";
parms.clear();
parms.put("customerId", customerId);
......@@ -87,7 +94,7 @@ public class CustomerTest {
//201
customerId = DataService.customerList.get(0).getId();
customerId = customerList.get(0).getId();
path = "/customers/"+customerId+"/discountcards";
parms.clear();
parms.put("customerId", customerId);
......@@ -99,7 +106,7 @@ public class CustomerTest {
.body(parms).when().post(path).then().statusCode(201);
//409 repeat conflict
customerId = DataService.customerList.get(0).getId();
customerId = customerList.get(0).getId();
path = "/customers/"+customerId+"/discountcards";
parms.clear();
parms.put("customerId", customerId);
......@@ -117,7 +124,7 @@ public class CustomerTest {
@Test
@Order(3)
public void getCustomerDiscountCardTest() {
Long customerId=null;
Long customerId =0L;
String path = "/customers/"+customerId+"/discountcards";
//400
path = "/customers/"+customerId+"/discountcards";
......@@ -128,21 +135,16 @@ public class CustomerTest {
path = "/customers/"+customerId+"/discountcards";
RestAssured.given().when().get(path).then().statusCode(404);
//200
customerId = DataService.customerList.get(0).getId();
//201
customerId = customerList.get(0).getId();
path = "/customers/"+customerId+"/discountcards";
RestAssured.given().when().get(path).then().statusCode(200);
}
}
}
......
......@@ -3,7 +3,9 @@ package de.rwth.swc.sqa;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
......@@ -14,11 +16,14 @@ import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import de.rwth.swc.sqa.model.Ticket;
import io.restassured.RestAssured;
import io.restassured.response.ValidatableResponse;
@SpringBootTest(webEnvironment = RANDOM_PORT)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class TicketTest {
private static List<Ticket> ticketList = new ArrayList<Ticket>();
@LocalServerPort
int serverPort;
......@@ -44,94 +49,33 @@ public class TicketTest {
.body(parms1).when().post("/tickets").then().statusCode(400);
//200
//adult and not student
parms1.clear();
parms1.put("birthdate", "1992-01-01");
parms1.put("validFor", "1d");
parms1.put("zone", "A");
parms1.put("student", false);
parms1.put("discountCard", false);
parms1.put("disabled", true);
parms1.put("validFrom", "2022-05-20 10:00:00");
RestAssured.given().header("content-Type", "application/json").and()
.body(parms1).when().post("/tickets").then().statusCode(200);
//adult and student
//400
parms1.clear();
parms1.put("birthdate", "1992-01-01");
parms1.put("validFor", "30d");
parms1.put("zone", "A");
parms1.put("student", true);
parms1.put("discountCard", true);
parms1.put("disabled", true);
parms1.put("validFrom", "2022-05-20 10:00:00");
RestAssured.given().header("content-Type", "application/json").and()
.body(parms1).when().post("/tickets").then().statusCode(400);
//senior and 24h ticket
//200
parms1.clear();
parms1.put("birthdate", "1962-01-01");
parms1.put("validFor", "30d");
parms1.put("zone", "A");
parms1.put("student", false);
parms1.put("discountCard", true);
parms1.put("disabled", true);
parms1.put("validFrom", "2022-05-20 10:00:00");
RestAssured.given().header("content-Type", "application/json").and()
.body(parms1).when().post("/tickets").then().statusCode(200);
//child and 24h ticket
//400
parms1.clear();
parms1.put("birthdate", "2010-01-01");
parms1.put("validFor", "1d");
parms1.put("zone", "A");
parms1.put("student", false);
parms1.put("discountCard", true);
parms1.put("disabled", true);
parms1.put("validFrom", "2022-05-20 10:00:00");
RestAssured.given().header("content-Type", "application/json").and()
.body(parms1).when().post("/tickets").then().statusCode(400);
//child and 30d ticket
ValidatableResponse response = RestAssured.given().header("content-Type", "application/json").and()
.body(parms1).when().post("/tickets").then().statusCode(200);
Ticket ticket = response.extract().as(Ticket.class);
ticketList.add(ticket);
parms1.clear();
parms1.put("birthdate", "2010-01-01");
parms1.put("birthdate", "1992-01-01");
parms1.put("validFor", "30d");
parms1.put("zone", "A");
parms1.put("student", false);
parms1.put("discountCard", true);
parms1.put("disabled", true);
parms1.put("validFrom", "2022-05-20 10:00:00");
RestAssured.given().header("content-Type", "application/json").and()
.body(parms1).when().post("/tickets").then().statusCode(200);
//student and 1h ticket
parms1.clear();
parms1.put("birthdate", "1997-01-01");
parms1.put("validFor", "1h");
parms1.put("zone", "A");
parms1.put("student", true);
parms1.put("discountCard", true);
parms1.put("disabled", true);
parms1.put("validFrom", "2022-05-20 10:00:00");
RestAssured.given().header("content-Type", "application/json").and()
.body(parms1).when().post("/tickets").then().statusCode(400);
//student and not a student ticket => does not matter since he/she can buy an adult ticket
parms1.clear();
parms1.put("birthdate", "1997-01-01");
parms1.put("validFor", "1h");
parms1.put("zone", "A");
parms1.put("student", true);
parms1.put("discountCard", true);
parms1.put("disabled", true);
parms1.put("validFrom", "2022-05-20 10:00:00");
RestAssured.given().header("content-Type", "application/json").and()
.body(parms1).when().post("/tickets").then().statusCode(400);
response = RestAssured.given().header("content-Type", "application/json").and()
.body(parms1).when().post("/tickets").then().statusCode(200);
ticket = response.extract().as(Ticket.class);
ticketList.add(ticket);
}
......@@ -141,13 +85,13 @@ public class TicketTest {
String path = "/tickets/validate";
Long ticketId =0L;
Map<String, Object> parms = new HashMap<String, Object>();
//400 //格式不对,缺少必要信息
//400 //lacking info
parms.clear();
parms.put("ticketId", ticketId);
RestAssured.given().header("content-Type", "application/json").and()
.body(parms).when().post(path).then().statusCode(400);
//403 //ticketid不对
//403 //ticketid incorrect
parms.clear();
parms.put("ticketId", ticketId);
parms.put("birthdate", "1992-01-01");
......@@ -158,8 +102,8 @@ public class TicketTest {
RestAssured.given().header("content-Type", "application/json").and()
.body(parms).when().post(path).then().statusCode(403);
//403//时间过期
ticketId = DataService.ticketList.get(0).getId();
//403//time incorrect
ticketId = ticketList.get(0).getId();
parms.clear();
parms.put("ticketId", ticketId);
parms.put("birthdate", "1992-01-01");
......@@ -171,13 +115,13 @@ public class TicketTest {
.body(parms).when().post(path).then().statusCode(403);
//200//信息一致,且有效期内
ticketId = DataService.ticketList.get(0).getId();
//200//correct
ticketId = ticketList.get(0).getId();
parms.clear();
parms.put("ticketId", ticketId);
parms.put("birthdate", "1992-01-01");
parms.put("zone", "A");
parms.put("student", false);
parms.put("student", true);
parms.put("disabled", true);
parms.put("date", "2022-05-20 12:00:00");
RestAssured.given().header("content-Type", "application/json").and()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment