Skip to content

Commit 4da7020

Browse files
committed
tests
1 parent b85b066 commit 4da7020

File tree

11 files changed

+113
-32
lines changed

11 files changed

+113
-32
lines changed

build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ dependencies {
5252
testImplementation(libs.junitJupiter)
5353
testRuntimeOnly(libs.junitPlatformLauncher)
5454
runtimeOnly("com.h2database:h2")
55+
runtimeOnly(libs.postgresql)
5556

5657
implementation(libs.jacksonDatabindNullable)
5758
implementation(libs.datafaker)
@@ -60,7 +61,7 @@ dependencies {
6061
tasks.test {
6162
useJUnitPlatform()
6263
testLogging {
63-
exceptionFormat = TestExceptionFormat.FULL
64+
exceptionFormat = TestExceptionFormat.SHORT
6465
events = setOf(
6566
TestLogEvent.FAILED,
6667
TestLogEvent.PASSED,

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ mapstruct = "1.6.3"
5353
h2 = "2.3.232"
5454
junit-bom = "6.0.0"
5555
instancio = "5.5.1"
56+
postgresql = "42.7.5"
5657

5758
[libraries]
5859

@@ -82,3 +83,4 @@ jacksonDatabindNullable = { module = "org.openapitools:jackson-databind-nullable
8283
datafaker = { module = "net.datafaker:datafaker", version.ref = "datafaker" }
8384
instancioCore = { module = "org.instancio:instancio-core", version.ref = "instancio" }
8485
springdocOpenapi = { module = "org.springdoc:springdoc-openapi-starter-webmvc-ui", version.ref = "springdoc-openapi-ui" }
86+
postgresql = { module = "org.postgresql:postgresql", version.ref = "postgresql" }

src/main/java/io/hexlet/project_devops_deploy/component/DataInitializer.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,31 @@
22

33
import io.hexlet.project_devops_deploy.repository.BulletinRepository;
44
import io.hexlet.project_devops_deploy.util.ModelGenerator;
5-
import jakarta.annotation.PostConstruct;
65
import java.util.stream.IntStream;
6+
import lombok.AllArgsConstructor;
7+
import org.springframework.boot.ApplicationArguments;
8+
import org.springframework.boot.ApplicationRunner;
9+
import org.springframework.context.annotation.Profile;
710
import org.springframework.stereotype.Component;
811

912
@Component
10-
public class DataInitializer {
13+
@Profile("!test")
14+
@AllArgsConstructor
15+
public class DataInitializer implements ApplicationRunner {
1116

1217
private static final int BULLETIN_SEED_COUNT = 10;
1318

1419
private final BulletinRepository repository;
1520
private final ModelGenerator modelGenerator;
1621

17-
public DataInitializer(BulletinRepository repository, ModelGenerator modelGenerator) {
18-
this.repository = repository;
19-
this.modelGenerator = modelGenerator;
20-
}
22+
@Override
23+
public void run(ApplicationArguments args) {
24+
if (repository.count() > 0) {
25+
return;
26+
}
2127

22-
@PostConstruct
23-
public void seedData() {
24-
IntStream.range(0, BULLETIN_SEED_COUNT).mapToObj(i -> modelGenerator.generateBulletin())
28+
IntStream.range(0, BULLETIN_SEED_COUNT)
29+
.mapToObj(i -> modelGenerator.generateBulletin())
2530
.forEach(repository::save);
2631
}
2732
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.hexlet.project_devops_deploy.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
5+
6+
@Configuration
7+
@EnableJpaAuditing
8+
public class JpaConfig {
9+
}

src/main/java/io/hexlet/project_devops_deploy/controller/BulletinController.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.hexlet.project_devops_deploy.service.BulletinService;
66
import jakarta.validation.Valid;
77
import java.util.List;
8+
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.http.HttpStatus;
910
import org.springframework.web.bind.annotation.DeleteMapping;
1011
import org.springframework.web.bind.annotation.GetMapping;
@@ -20,24 +21,19 @@
2021
@RequestMapping("/api")
2122
public class BulletinController {
2223

23-
private final BulletinService service;
24+
private BulletinService service;
2425

25-
public BulletinController(BulletinService service) {
26+
@Autowired
27+
public void setService(BulletinService service) {
2628
this.service = service;
2729
}
2830

29-
@PostMapping("/bulletins2")
31+
@PostMapping("/bulletins")
3032
@ResponseStatus(HttpStatus.CREATED)
3133
public BulletinDto create(@Valid @RequestBody BulletinRequest request) {
3234
return service.create(request);
3335
}
3436

35-
@DeleteMapping("/bulletins/{id}")
36-
@ResponseStatus(HttpStatus.NO_CONTENT)
37-
public void delete(@PathVariable Long id) {
38-
service.delete(id);
39-
}
40-
4137
@GetMapping("/bulletins")
4238
public List<BulletinDto> index() {
4339
return service.findAll();
@@ -52,4 +48,11 @@ public BulletinDto show(@PathVariable Long id) {
5248
public BulletinDto update(@PathVariable Long id, @Valid @RequestBody BulletinRequest request) {
5349
return service.update(id, request);
5450
}
51+
52+
53+
@DeleteMapping("/bulletins/{id}")
54+
@ResponseStatus(HttpStatus.NO_CONTENT)
55+
public void delete(@PathVariable Long id) {
56+
service.delete(id);
57+
}
5558
}

src/main/java/io/hexlet/project_devops_deploy/model/Bulletin.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
import jakarta.persistence.Column;
44
import jakarta.persistence.Entity;
5+
import jakarta.persistence.EntityListeners;
56
import jakarta.persistence.EnumType;
67
import jakarta.persistence.Enumerated;
78
import jakarta.persistence.GeneratedValue;
89
import jakarta.persistence.GenerationType;
910
import jakarta.persistence.Id;
1011
import jakarta.persistence.Table;
12+
import java.time.LocalDateTime;
13+
import org.springframework.data.annotation.CreatedDate;
14+
import org.springframework.data.annotation.LastModifiedDate;
15+
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
1116

1217
@Entity
1318
@Table(name = "bulletins")
19+
@EntityListeners(AuditingEntityListener.class)
1420
public class Bulletin {
1521

1622
@Id
@@ -30,6 +36,14 @@ public class Bulletin {
3036
@Column(nullable = false)
3137
private String contact;
3238

39+
@CreatedDate
40+
@Column(name = "created_at", nullable = false, updatable = false)
41+
private LocalDateTime createdAt;
42+
43+
@LastModifiedDate
44+
@Column(name = "updated_at", nullable = false)
45+
private LocalDateTime updatedAt;
46+
3347
public String getContact() {
3448
return contact;
3549
}
@@ -69,4 +83,20 @@ public void setState(BulletinState state) {
6983
public void setTitle(String title) {
7084
this.title = title;
7185
}
86+
87+
public LocalDateTime getCreatedAt() {
88+
return createdAt;
89+
}
90+
91+
public void setCreatedAt(LocalDateTime createdAt) {
92+
this.createdAt = createdAt;
93+
}
94+
95+
public LocalDateTime getUpdatedAt() {
96+
return updatedAt;
97+
}
98+
99+
public void setUpdatedAt(LocalDateTime updatedAt) {
100+
this.updatedAt = updatedAt;
101+
}
72102
}

src/main/java/io/hexlet/project_devops_deploy/util/ModelGenerator.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ public Bulletin generateBulletin() {
2727

2828
@PostConstruct
2929
private void init() {
30-
bulletinModel = Instancio.of(Bulletin.class).ignore(Select.field(Bulletin::getId))
30+
bulletinModel = Instancio.of(Bulletin.class)
31+
.ignore(Select.field(Bulletin::getId))
32+
.ignore(Select.field(Bulletin::getCreatedAt))
33+
.ignore(Select.field(Bulletin::getUpdatedAt))
3134
.supply(Select.field(Bulletin::getTitle), () -> faker.book().title())
3235
.supply(Select.field(Bulletin::getDescription), () -> faker.lorem().paragraph(3))
3336
.supply(Select.field(Bulletin::getState), () -> faker.options().option(BulletinState.values()))
34-
.supply(Select.field(Bulletin::getContact), () -> faker.phoneNumber().phoneNumber()).toModel();
37+
.supply(Select.field(Bulletin::getContact), () -> faker.phoneNumber().phoneNumber())
38+
.toModel();
3539
}
3640
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
spring:
3+
config:
4+
activate:
5+
on-profile: prod
6+
datasource:
7+
url: ${SPRING_DATASOURCE_URL:jdbc:postgresql://localhost:5432/bulletins}
8+
username: ${SPRING_DATASOURCE_USERNAME:postgres}
9+
password: ${SPRING_DATASOURCE_PASSWORD:postgres}
10+
driver-class-name: org.postgresql.Driver
11+
jpa:
12+
hibernate:
13+
ddl-auto: validate
14+
show-sql: false
15+
flyway:
16+
enabled: true
17+
output:
18+
ansi:
19+
enabled: always
20+
h2:
21+
console:
22+
enabled: false

src/main/resources/application.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ logging:
77
io.hexlet.project_devops_deploy: DEBUG
88

99
spring:
10+
profiles:
11+
default: dev
1012
application:
1113
name: bulletins
1214
datasource:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE bulletins ADD COLUMN created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
2+
3+
ALTER TABLE bulletins ADD COLUMN updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;

0 commit comments

Comments
 (0)