Skip to content

Commit a899df5

Browse files
committed
Spring Boot tests
1 parent 1047864 commit a899df5

File tree

15 files changed

+470
-0
lines changed

15 files changed

+470
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FROM maven:3.5-jdk-8
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: '3'
2+
services:
3+
hk-mysql:
4+
container_name: hk-mysql
5+
image: mysql/mysql-server:5.7
6+
environment:
7+
MYSQL_DATABASE: test
8+
MYSQL_ROOT_PASSWORD: hellokoding
9+
MYSQL_ROOT_HOST: '%'
10+
ports:
11+
- "3306:3306"
12+
restart: always
13+
14+
hk-app:
15+
build: .
16+
volumes:
17+
- .:/app
18+
- ~/.m2:/root/.m2
19+
working_dir: /app
20+
ports:
21+
- 8080:8080
22+
command: mvn clean spring-boot:run
23+
depends_on:
24+
- hk-mysql
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.hellokoding.springboot</groupId>
8+
<artifactId>springboot-restapi-testing-all-layers</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>2.1.4.RELEASE</version>
15+
</parent>
16+
17+
<properties>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
<java.version>1.8</java.version>
20+
<org.mapstruct.version>1.2.0.Final</org.mapstruct.version>
21+
<org.projectlombok.version>1.18.2</org.projectlombok.version>
22+
<org.apache.maven.plugins.version>3.6.0</org.apache.maven.plugins.version>
23+
</properties>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-data-jpa</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>mysql</groupId>
36+
<artifactId>mysql-connector-java</artifactId>
37+
<scope>runtime</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.projectlombok</groupId>
41+
<artifactId>lombok</artifactId>
42+
<version>${org.projectlombok.version}</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.mapstruct</groupId>
46+
<artifactId>mapstruct-jdk8</artifactId>
47+
<version>${org.mapstruct.version}</version>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-starter-test</artifactId>
53+
</dependency>
54+
<dependency>
55+
<groupId>org.hsqldb</groupId>
56+
<artifactId>hsqldb</artifactId>
57+
<scope>test</scope>
58+
</dependency>
59+
</dependencies>
60+
61+
<build>
62+
<plugins>
63+
<plugin>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-maven-plugin</artifactId>
66+
</plugin>
67+
68+
<plugin>
69+
<groupId>org.apache.maven.plugins</groupId>
70+
<artifactId>maven-compiler-plugin</artifactId>
71+
<version>${org.apache.maven.plugins.version}</version>
72+
<configuration>
73+
<source>${java.version}</source>
74+
<target>${java.version}</target>
75+
<annotationProcessorPaths>
76+
<path>
77+
<groupId>org.projectlombok</groupId>
78+
<artifactId>lombok</artifactId>
79+
<version>${org.projectlombok.version}</version>
80+
</path>
81+
<path>
82+
<groupId>org.mapstruct</groupId>
83+
<artifactId>mapstruct-processor</artifactId>
84+
<version>${org.mapstruct.version}</version>
85+
</path>
86+
</annotationProcessorPaths>
87+
<compilerArgs>
88+
<arg>-Amapstruct.suppressGeneratorTimestamp=true</arg>
89+
<arg>-Amapstruct.defaultComponentModel=spring</arg>
90+
</compilerArgs>
91+
</configuration>
92+
</plugin>
93+
</plugins>
94+
</build>
95+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.hellokoding.springboot.restful;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
public static void main(String[] args) {
9+
SpringApplication.run(Application.class, args);
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.hellokoding.springboot.restful.product;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
import org.hibernate.annotations.CreationTimestamp;
8+
import org.hibernate.annotations.UpdateTimestamp;
9+
10+
import javax.persistence.Entity;
11+
import javax.persistence.GeneratedValue;
12+
import javax.persistence.GenerationType;
13+
import javax.persistence.Id;
14+
import java.math.BigDecimal;
15+
import java.util.Date;
16+
17+
@Data
18+
@Builder
19+
@NoArgsConstructor
20+
@AllArgsConstructor
21+
22+
@Entity
23+
public class Product {
24+
@Id
25+
@GeneratedValue(strategy = GenerationType.IDENTITY)
26+
private Long id;
27+
28+
private String name;
29+
30+
private String description;
31+
32+
private BigDecimal price;
33+
34+
@CreationTimestamp
35+
private Date createdAt;
36+
37+
@UpdateTimestamp
38+
private Date updatedAt;
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.hellokoding.springboot.restful.product;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.*;
8+
9+
import java.util.List;
10+
import java.util.Optional;
11+
12+
13+
@Slf4j
14+
@RequiredArgsConstructor
15+
16+
@RestController
17+
@RequestMapping("/api/v1/products")
18+
public class ProductAPI {
19+
private final ProductService productService;
20+
private final ProductMapper productMapper;
21+
22+
@GetMapping
23+
public ResponseEntity<List<ProductDTO>> findAll() {
24+
return ResponseEntity.ok(productMapper.toProductDTOs(productService.findAll()));
25+
}
26+
27+
@GetMapping("/{id}")
28+
public ResponseEntity<ProductDTO> findById(@PathVariable Long id) {
29+
Optional<Product> product = productService.findById(id);
30+
31+
return ResponseEntity.ok(productMapper.toProductDTO(product.get()));
32+
}
33+
34+
@PostMapping
35+
public ResponseEntity<ProductDTO> create(@RequestBody ProductDTO productDTO) {
36+
productService.save(productMapper.toProduct(productDTO));
37+
38+
return ResponseEntity.status(HttpStatus.CREATED).body(productDTO);
39+
}
40+
41+
@PutMapping("/{id}")
42+
public ResponseEntity<ProductDTO> update(@PathVariable Long id, @RequestBody ProductDTO productDTO) {
43+
Product product = productMapper.toProduct(productDTO);
44+
product.setId(id);
45+
46+
productService.save(product);
47+
48+
return ResponseEntity.status(HttpStatus.ACCEPTED).body(productDTO);
49+
}
50+
51+
@DeleteMapping("/{id}")
52+
public ResponseEntity delete(@PathVariable Long id) {
53+
productService.deleteById(id);
54+
55+
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.hellokoding.springboot.restful.product;
2+
3+
import lombok.Builder;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
import java.math.BigDecimal;
8+
9+
@Data
10+
@NoArgsConstructor
11+
@Builder
12+
public class ProductDTO {
13+
private String name;
14+
private String description;
15+
private BigDecimal price;
16+
17+
public ProductDTO(String name, String description, BigDecimal price) {
18+
this.name = name;
19+
this.description = description;
20+
this.price = price;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.hellokoding.springboot.restful.product;
2+
3+
import org.mapstruct.Mapper;
4+
5+
import java.util.List;
6+
7+
@Mapper
8+
public interface ProductMapper {
9+
ProductDTO toProductDTO(Product product);
10+
11+
List<ProductDTO> toProductDTOs(List<Product> products);
12+
13+
Product toProduct(ProductDTO productDTO);
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.hellokoding.springboot.restful.product;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
public interface ProductRespository extends JpaRepository<Product, Long> {
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.hellokoding.springboot.restful.product;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.springframework.stereotype.Service;
5+
6+
import java.util.List;
7+
import java.util.Optional;
8+
9+
@RequiredArgsConstructor
10+
11+
@Service
12+
public class ProductService {
13+
private final ProductRespository productRespository;
14+
15+
public List<Product> findAll() {
16+
return productRespository.findAll();
17+
}
18+
19+
public Optional<Product> findById(Long id) {
20+
return productRespository.findById(id);
21+
}
22+
23+
public Product save(Product stock) {
24+
return productRespository.save(stock);
25+
}
26+
27+
public void deleteById(Long id) {
28+
productRespository.deleteById(id);
29+
}
30+
}

0 commit comments

Comments
 (0)