Skip to content

Commit 71534ed

Browse files
committed
first version
1 parent 072df8b commit 71534ed

9 files changed

Lines changed: 561 additions & 1 deletion

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/evomaster.jar
2+
/.idea/
3+
/target/

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
# rest-api-example
2-
An example of REST API with EvoMaster configured for it
2+
An example of REST API with EvoMaster configured for it.
3+
4+
It requires JDK 8, and Maven.
5+
6+
The API is written with SpringBoot, and uses an H2 embedded database.

pom.xml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>org.evomaster</groupId>
6+
<artifactId>rest-api-example</artifactId>
7+
8+
<version>1.0.0-SNAPSHOT</version>
9+
10+
<properties>
11+
<version.spring>2.3.3.RELEASE</version.spring>
12+
</properties>
13+
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-web</artifactId>
19+
<version>${version.spring}</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-data-jpa</artifactId>
24+
<version>${version.spring}</version>
25+
</dependency>
26+
27+
<!-- Needed to automatically generate the OpenAPI schema-->
28+
<dependency>
29+
<groupId>io.springfox</groupId>
30+
<artifactId>springfox-boot-starter</artifactId>
31+
<version>3.0.0</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.h2database</groupId>
35+
<artifactId>h2</artifactId>
36+
<version>1.4.197</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.junit.jupiter</groupId>
40+
<artifactId>junit-jupiter</artifactId>
41+
<version>5.6.2</version>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>io.rest-assured</groupId>
46+
<artifactId>rest-assured</artifactId>
47+
<version>4.3.0</version>
48+
<scope>test</scope>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>org.evomaster</groupId>
53+
<artifactId>evomaster-client-java-controller</artifactId>
54+
<scope>test</scope>
55+
<version>1.0.1</version>
56+
</dependency>
57+
58+
</dependencies>
59+
60+
61+
62+
63+
<build>
64+
<plugins>
65+
<plugin>
66+
<groupId>org.apache.maven.plugins</groupId>
67+
<artifactId>maven-compiler-plugin</artifactId>
68+
<version>3.8.1</version>
69+
<inherited>true</inherited>
70+
<configuration>
71+
<release>1.8</release>
72+
</configuration>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
77+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.example;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.Bean;
6+
import springfox.documentation.builders.PathSelectors;
7+
import springfox.documentation.spi.DocumentationType;
8+
import springfox.documentation.spring.web.plugins.Docket;
9+
10+
@SpringBootApplication
11+
public class Application {
12+
13+
public static void main(String[] args){
14+
/*
15+
Start application. Schema accessible at:
16+
http://localhost:8080/swagger-ui/index.html
17+
http://localhost:8080/v3/api-docs
18+
*/
19+
SpringApplication.run(Application.class);
20+
}
21+
22+
23+
@Bean
24+
public Docket swaggerApi() {
25+
/*
26+
Needed to activate and configure the OpenAPI schema
27+
*/
28+
return new Docket(DocumentationType.OAS_30)
29+
.select()
30+
.paths(PathSelectors.ant("/api/**"))
31+
.build();
32+
}
33+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.example;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.Id;
6+
7+
@Entity
8+
public class Item {
9+
10+
@Id @GeneratedValue
11+
private Long id;
12+
13+
private String name;
14+
15+
private String description;
16+
17+
private Integer cost;
18+
19+
20+
public Item() {
21+
}
22+
23+
public Long getId() {
24+
return id;
25+
}
26+
27+
public void setId(Long id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
39+
public String getDescription() {
40+
return description;
41+
}
42+
43+
public void setDescription(String description) {
44+
this.description = description;
45+
}
46+
47+
public Integer getCost() {
48+
return cost;
49+
}
50+
51+
public void setCost(Integer cost) {
52+
this.cost = cost;
53+
}
54+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.example;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.data.repository.CrudRepository;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.stereotype.Repository;
7+
import org.springframework.web.bind.annotation.*;
8+
9+
import java.net.URI;
10+
11+
12+
@Repository
13+
interface ItemRepository extends CrudRepository<Item, Long>{}
14+
15+
@RestController
16+
@RequestMapping(path = "/api/items")
17+
public class RestApi {
18+
19+
@Autowired
20+
private ItemRepository repository;
21+
22+
23+
@GetMapping
24+
public Iterable<Item> getAll(){
25+
return repository.findAll();
26+
}
27+
28+
@GetMapping(path = "/{id}")
29+
public ResponseEntity<Item> getById(@PathVariable("id") Long id){
30+
31+
Item item = repository.findById(id).orElse(null);
32+
33+
if(item == null){
34+
return ResponseEntity.notFound().build();
35+
}
36+
37+
return ResponseEntity.ok(item);
38+
}
39+
40+
@PostMapping
41+
public ResponseEntity create(@RequestBody Item item){
42+
item.setId(null);
43+
repository.save(item);
44+
return ResponseEntity.created(URI.create("/api/"+item.getId())).build();
45+
}
46+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;

0 commit comments

Comments
 (0)