Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • aharslan/sqa-project-part1
  • wildeproust/sqa-project-part1
2 results
Show changes
Commits on Source (19)
......@@ -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>
......
......@@ -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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import de.rwth.swc.sqa.model.Customer;
import de.rwth.swc.sqa.model.DiscountCard;
import de.rwth.swc.sqa.model.Ticket;
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>();
}
package de.rwth.swc.sqa.api;
import de.rwth.swc.sqa.model.DiscountCard;
import io.swagger.annotations.ApiParam;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
//import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;
import javax.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import de.rwth.swc.sqa.DataService;
import de.rwth.swc.sqa.model.Customer;
import de.rwth.swc.sqa.model.DiscountCard;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@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) {
if(body.getId()!=null || body.getBirthdate().equals("") ) {
return ResponseEntity.status(400).body(null);
}else if(body.getDisabled()==null) {
body.setDisabled(false);
}
body.setId(System.currentTimeMillis());
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) {
this.getRequest().ifPresent((request) -> {
Iterator var1 = MediaType.parseMediaTypes(request.getHeader("Accept")).iterator();
while(var1.hasNext()) {
MediaType mediaType = (MediaType)var1.next();
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
String exampleString = "{ \"validFor\" : \"30d\", \"customerId\" : 6, \"id\" : 0, \"validFrom\" : \"1992-01-01\", \"type\" : 1 }";
ApiUtil.setExampleResponse(request, "application/json", exampleString);
break;
}
}
});
return new ResponseEntity(HttpStatus.NOT_IMPLEMENTED);
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) {
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) {
if(card.getValidFrom().equals(body.getValidFrom())) {
conflict = true;
}
}
if(conflict) {
return ResponseEntity.status(409).body(null);
}else {
discountCardList.add(body);
}
}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) {
List<DiscountCard> customerDiscountCards = new ArrayList<DiscountCard>();
DiscountCard testCard = new DiscountCard();
testCard.setCustomerId(new Long(55443327));
testCard.setType(1);
customerDiscountCards.add(testCard);
return ResponseEntity.ok().body(customerDiscountCards);
if(customerId.longValue()==0) {
return ResponseEntity.status(400).body(null);
}
boolean exists = false;
for(Customer customer: DataService.customerList) {
if(customer.getId().longValue()==customerId.longValue() && DataService.discountCardMap.containsKey(customerId)) {
exists = true;
}
}
if(!exists) {
return ResponseEntity.status(404).body(null);
}
return ResponseEntity.ok().body( DataService.discountCardMap.get(customerId));
}
}
package de.rwth.swc.sqa.api;
import de.rwth.swc.sqa.model.Customer;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import de.rwth.swc.sqa.DataService;
import de.rwth.swc.sqa.model.Ticket;
import de.rwth.swc.sqa.model.TicketRequest;
import de.rwth.swc.sqa.model.TicketValidationRequest;
import io.swagger.annotations.ApiParam;
@Controller
@RequestMapping("/tickets")
public class TicketController implements TicketsApi{
Customer customer;
@PostMapping("")
public ResponseEntity<Ticket> buyTicket(@ApiParam(value = "TicketRequest object" ,required=true ) @Valid @RequestBody TicketRequest body) {
try {
Ticket ticket = new Ticket();
ticket.setId(System.currentTimeMillis());
ticket.setBirthdate(body.getBirthdate());
ticket.setDisabled(body.getDisabled()==null?false:body.getDisabled());
ticket.setDiscountCard(body.getDiscountCard()==null?false:body.getDiscountCard());
ticket.setStudent(body.getStudent()==null?false:body.getStudent());
ticket.setValidFor(Ticket.ValidForEnum.fromValue(body.getValidFor().getValue()));
ticket.setValidFrom(body.getValidFrom());
ticket.setZone(Ticket.ZoneEnum.fromValue(body.getZone().getValue()));
DataService.ticketList.add(ticket);
return ResponseEntity.ok().body(ticket);
}catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(400).body(null);
}
}
@PostMapping("/validate")
public ResponseEntity<Void> validateTicket(@ApiParam(value = "TicketValidationRequest object that needs to validated" ,required=true ) @Valid @RequestBody TicketValidationRequest body) {
List<Ticket> ticketList = DataService.ticketList;
boolean valid=false;
try {
for(Ticket ticket:ticketList) {
if(ticket.getId().longValue()==body.getTicketId().longValue()
&& ticket.getDisabled().equals(body.getDisabled())
&& ticket.getStudent().equals(body.getStudent())
&& ticket.getZone().getValue().equals(body.getZone().getValue())) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long startDateTime = dateFormat.parse(ticket.getValidFrom()).getTime();
long endDateTime = dateFormat.parse(body.getDate()).getTime();
long diff =endDateTime - startDateTime;
Ticket.ValidForEnum type= ticket.getValidFor();
long onehour = 1000*60*60;
if( (type.equals(Ticket.ValidForEnum._1H) && diff<onehour)
|| (type.equals(Ticket.ValidForEnum._1D) && diff<onehour*24)
|| (type.equals(Ticket.ValidForEnum._30D) && diff<onehour*24*30)
|| (type.equals(Ticket.ValidForEnum._1Y) && diff<onehour*24*30*365) ) {
valid=true;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
if(valid) {
return ResponseEntity.ok().body(null);
}else {
return ResponseEntity.status(403).body(null);
}
}
}
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;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
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;
@BeforeEach
public void setup() {
RestAssured.port = serverPort;
}
@Test
@Order(1)
public void addCustomerTest() {
//400
Map<String, Object> parms1 = new HashMap<String, Object>();
parms1.put("birthdate", "");
parms1.put("disabled", true);
RestAssured.given().header("content-Type", "application/json").and()
.body(parms1).when().post("/customers").then().statusCode(400);
//201
Map<String, Object> parms = new HashMap<String, Object>();
parms.put("birthdate", "2000-01-01");
parms.put("disabled", false);
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);
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 =0L;
String path = "/customers/"+customerId+"/discountcards";
Map<String, Object> parms = new HashMap<String, Object>();
//404
customerId = 1L;
path = "/customers/"+customerId+"/discountcards";
parms.clear();
parms.put("customerId", customerId);
parms.put("id", 1L);
parms.put("type", 1);
parms.put("validFrom", "2022-01-01");
parms.put("validFor", DiscountCard.ValidForEnum._30D);
RestAssured.given().header("content-Type", "application/json").and()
.body(parms).when().post(path).then().statusCode(404);
//400
customerId = customerList.get(0).getId();
path = "/customers/"+customerId+"/discountcards";
parms.clear();
parms.put("customerId", customerId);
parms.put("type", 1);
parms.put("validFrom", "2022-01-01");
parms.put("validFor", DiscountCard.ValidForEnum._30D);
RestAssured.given().header("content-Type", "application/json").and()
.body(parms).when().post(path).then().statusCode(400);
//201
customerId = customerList.get(0).getId();
path = "/customers/"+customerId+"/discountcards";
parms.clear();
parms.put("customerId", customerId);
parms.put("id", 1L);
parms.put("type", 1);
parms.put("validFrom", "2022-01-01");
parms.put("validFor", DiscountCard.ValidForEnum._30D);
RestAssured.given().header("content-Type", "application/json").and()
.body(parms).when().post(path).then().statusCode(201);
//409 repeat conflict
customerId = customerList.get(0).getId();
path = "/customers/"+customerId+"/discountcards";
parms.clear();
parms.put("customerId", customerId);
parms.put("id", 1L);
parms.put("type", 1);
parms.put("validFrom", "2022-01-01");
parms.put("validFor", DiscountCard.ValidForEnum._30D);
RestAssured.given().header("content-Type", "application/json").and()
.body(parms).when().post(path).then().statusCode(409);
}
@Test
@Order(3)
public void getCustomerDiscountCardTest() {
Long customerId =0L;
String path = "/customers/"+customerId+"/discountcards";
//400
path = "/customers/"+customerId+"/discountcards";
RestAssured.given().when().get(path).then().statusCode(400);
//404
customerId = 11111L;
path = "/customers/"+customerId+"/discountcards";
RestAssured.given().when().get(path).then().statusCode(404);
//201
customerId = customerList.get(0).getId();
path = "/customers/"+customerId+"/discountcards";
RestAssured.given().when().get(path).then().statusCode(200);
}
}
......@@ -20,8 +20,8 @@ public class DemoIntegrationTest {
RestAssured.port = port;
}
@Test
public void whenReadAll_thenStatusIsNotImplemented() {
given().get(PATH).then().statusCode(501);
}
// @Test
// public void whenReadAll_thenStatusIsNotImplemented() {
// given().get(PATH).then().statusCode(501);
// }
}
\ No newline at end of file
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;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
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;
@BeforeEach
public void setup() {
RestAssured.port = serverPort;
}
@Test
@Order(1)
public void buyTicketTest() {
//400//no birthdate
Map<String, Object> parms1 = new HashMap<String, Object>();
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);
//200
parms1.clear();
parms1.put("birthdate", "1992-01-01");
parms1.put("validFor", "1d");
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");
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", "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");
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);
}
@Test
@Order(2)
public void validateTicketTest() {
String path = "/tickets/validate";
Long ticketId =0L;
Map<String, Object> parms = new HashMap<String, Object>();
//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 incorrect
parms.clear();
parms.put("ticketId", ticketId);
parms.put("birthdate", "1992-01-01");
parms.put("zone", "A");
parms.put("student", true);
parms.put("disabled", true);
parms.put("date", "2022-05-22 10:00:00");
RestAssured.given().header("content-Type", "application/json").and()
.body(parms).when().post(path).then().statusCode(403);
//403//time incorrect
ticketId = ticketList.get(0).getId();
parms.clear();
parms.put("ticketId", ticketId);
parms.put("birthdate", "1992-01-01");
parms.put("zone", "A");
parms.put("student", true);
parms.put("disabled", true);
parms.put("date", "2022-05-22 10:00:00");
RestAssured.given().header("content-Type", "application/json").and()
.body(parms).when().post(path).then().statusCode(403);
//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", true);
parms.put("disabled", true);
parms.put("date", "2022-05-20 12:00:00");
RestAssured.given().header("content-Type", "application/json").and()
.body(parms).when().post(path).then().statusCode(200);
}
}