Skip to content

Commit dc1c024

Browse files
Added new changes
1 parent 41fda16 commit dc1c024

File tree

14 files changed

+135
-128
lines changed

14 files changed

+135
-128
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ jobs:
3535
sleep 2
3636
done
3737
38-
# Можно аналогично добавить wait for kafka/mailhog если нужно
3938
4039
- name: Set up OpenJDK 21
4140
uses: actions/setup-java@v4

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
![CI](https://github.com/irinakomarchenko/user-service-spring/actions/workflows/ci.yml/badge.svg)
22

3-
[![Maintainability](https://qlty.sh/badges/a56c7491-b9be-4239-964a-541250c083e3/maintainability.svg)](https://qlty.sh/gh/irinakomarchenko/projects/user-service-spring)
3+
[![Maintainability](https://qlty.sh/badges/1d96f9b0-00d7-47ce-bd3a-f03a36f33f8c/maintainability.svg)](https://qlty.sh/gh/irinakomarchenko/projects/user-service-spring-Apache-Kafka)
44

5-
# Notification-service
5+
# User Service & Notification Service
6+
7+
**User Service** — микросервис на Spring Boot для управления пользователями (CRUD) и публикации событий о создании/удалении пользователей в Kafka.
8+
**Notification-service** — микросервис, который слушает события из Kafka и отправляет email-уведомления пользователям. Также предоставляет API для отправки email.
69

7-
**Notification-service** — REST API-сервис на Spring Boot для управления пользователями (CRUD).
8-
Сервис отправляет Kafka-события о создании и удалении пользователей. Эти события могут быть обработаны, например, микросервисом notification-service, который отправляет email-уведомления.
910

1011
---
1112

@@ -64,3 +65,4 @@ java -jar target/user-service-spring.jar
6465

6566
### 7. Пример работы приложения
6667
![img_1.png](readme-resources/img_1.png)
68+
![img_2.png](readme-resources/img_2.png)

docker-compose.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ services:
3232
mailhog:
3333
image: mailhog/mailhog
3434
ports:
35-
- '1025:1025' # SMTP
36-
- '8025:8025' # Web UI
37-
35+
- "1025:1025"
36+
- "8025:8025"
3837
volumes:
3938
db:
4039
driver: local

notification-service/pom.xml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,12 @@
2323
<groupId>org.springframework.kafka</groupId>
2424
<artifactId>spring-kafka</artifactId>
2525
</dependency>
26+
2627
<dependency>
2728
<groupId>org.springframework.boot</groupId>
2829
<artifactId>spring-boot-starter-mail</artifactId>
2930
</dependency>
30-
<dependency>
31-
<groupId>com.sun.mail</groupId>
32-
<artifactId>jakarta.mail</artifactId>
33-
<version>2.0.1</version>
34-
</dependency>
31+
3532
<dependency>
3633
<groupId>org.springframework.boot</groupId>
3734
<artifactId>spring-boot-starter-validation</artifactId>
@@ -45,7 +42,11 @@
4542
<artifactId>lombok</artifactId>
4643
<scope>provided</scope>
4744
</dependency>
48-
45+
<dependency>
46+
<groupId>jakarta.persistence</groupId>
47+
<artifactId>jakarta.persistence-api</artifactId>
48+
<version>3.1.0</version>
49+
</dependency>
4950

5051
<dependency>
5152
<groupId>org.springframework.boot</groupId>
Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,47 @@
11
package notificationservice.service;
22

3-
import jakarta.mail.internet.MimeMessage;
4-
import jakarta.mail.internet.InternetAddress;
53
import org.junit.jupiter.api.Test;
6-
import org.springframework.beans.factory.annotation.Autowired;
74
import org.springframework.boot.test.context.SpringBootTest;
8-
import org.springframework.mail.javamail.JavaMailSender;
95
import org.springframework.test.context.ActiveProfiles;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.mail.javamail.JavaMailSender;
8+
import jakarta.mail.internet.MimeMessage;
9+
import jakarta.mail.internet.InternetAddress;
10+
11+
import java.net.HttpURLConnection;
12+
import java.net.URL;
13+
import java.nio.charset.StandardCharsets;
14+
import java.util.Scanner;
1015

1116
import static org.assertj.core.api.Assertions.assertThat;
1217

1318
@SpringBootTest
1419
@ActiveProfiles("test")
1520
public class EmailServiceIntegrationTest {
16-
1721
@Autowired
1822
private JavaMailSender mailSender;
1923

2024
@Test
21-
public void testSendEmail() throws Exception {
25+
public void testSendEmailAndCheckMailhog() throws Exception {
26+
2227
MimeMessage message = mailSender.createMimeMessage();
2328
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("[email protected]"));
2429
message.setFrom(new InternetAddress("[email protected]"));
2530
message.setSubject("Test subject");
2631
message.setText("Hello, this is a test email!");
27-
2832
mailSender.send(message);
2933

30-
assertThat(message.getAllRecipients()).isNotEmpty();
34+
35+
Thread.sleep(500);
36+
37+
URL url = new URL("http://localhost:8025/api/v2/messages");
38+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
39+
connection.setRequestMethod("GET");
40+
41+
try (Scanner scanner = new Scanner(connection.getInputStream(), StandardCharsets.UTF_8)) {
42+
String responseBody = scanner.useDelimiter("\\A").next();
43+
assertThat(responseBody).contains("Hello, this is a test email!");
44+
assertThat(responseBody).contains("[email protected]");
45+
}
3146
}
3247
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
spring.jpa.hibernate.ddl-auto=update
2-
spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver
3-
spring.datasource.url=jdbc:tc:postgresql:15://localhost/test
4-
spring.datasource.username=postgres
5-
spring.datasource.password=postgres
1+
spring.mail.host=localhost
2+
spring.mail.port=1025
3+
spring.mail.username=
4+
spring.mail.password=
5+
spring.mail.protocol=smtp
6+
spring.mail.properties.mail.smtp.auth=false
7+
spring.mail.properties.mail.smtp.starttls.enable=false

pom.xml

Lines changed: 6 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
<dependencyManagement>
2626
<dependencies>
27+
2728
<dependency>
2829
<groupId>org.springframework.boot</groupId>
2930
<artifactId>spring-boot-dependencies</artifactId>
@@ -40,102 +41,20 @@
4041
</dependency>
4142
</dependencies>
4243
</dependencyManagement>
44+
4345
<dependencies>
46+
4447
<dependency>
4548
<groupId>org.projectlombok</groupId>
4649
<artifactId>lombok</artifactId>
50+
<version>${lombok.version}</version>
51+
<scope>provided</scope>
4752
</dependency>
4853
<dependency>
4954
<groupId>org.slf4j</groupId>
5055
<artifactId>slf4j-api</artifactId>
5156
</dependency>
52-
<dependency>
53-
<groupId>org.springframework.kafka</groupId>
54-
<artifactId>spring-kafka</artifactId>
55-
</dependency>
56-
<dependency>
57-
<groupId>org.springframework.boot</groupId>
58-
<artifactId>spring-boot</artifactId>
59-
</dependency>
60-
<dependency>
61-
<groupId>org.springframework.boot</groupId>
62-
<artifactId>spring-boot-autoconfigure</artifactId>
63-
</dependency>
64-
<dependency>
65-
<groupId>org.springframework.data</groupId>
66-
<artifactId>spring-data-jpa</artifactId>
67-
</dependency>
68-
<dependency>
69-
<groupId>jakarta.persistence</groupId>
70-
<artifactId>jakarta.persistence-api</artifactId>
71-
</dependency>
72-
<dependency>
73-
<groupId>org.hibernate.orm</groupId>
74-
<artifactId>hibernate-core</artifactId>
75-
</dependency>
76-
<dependency>
77-
<groupId>org.springframework</groupId>
78-
<artifactId>spring-web</artifactId>
79-
</dependency>
80-
<dependency>
81-
<groupId>org.eclipse.angus</groupId>
82-
<artifactId>jakarta.mail</artifactId>
83-
<scope>test</scope>
84-
</dependency>
85-
<dependency>
86-
<groupId>org.springframework.boot</groupId>
87-
<artifactId>spring-boot-test</artifactId>
88-
<scope>test</scope>
89-
</dependency>
90-
<dependency>
91-
<groupId>org.testng</groupId>
92-
<artifactId>testng</artifactId>
93-
<version>7.1.0</version>
94-
<scope>test</scope>
95-
</dependency>
96-
<dependency>
97-
<groupId>org.springframework</groupId>
98-
<artifactId>spring-context-support</artifactId>
99-
<scope>test</scope>
100-
</dependency>
101-
<dependency>
102-
<groupId>org.assertj</groupId>
103-
<artifactId>assertj-core</artifactId>
104-
<scope>test</scope>
105-
</dependency>
106-
<dependency>
107-
<groupId>org.mockito</groupId>
108-
<artifactId>mockito-core</artifactId>
109-
<scope>test</scope>
110-
</dependency>
111-
<dependency>
112-
<groupId>org.springframework.boot</groupId>
113-
<artifactId>spring-boot-test-autoconfigure</artifactId>
114-
<scope>test</scope>
115-
</dependency>
116-
<dependency>
117-
<groupId>com.fasterxml.jackson.core</groupId>
118-
<artifactId>jackson-databind</artifactId>
119-
<scope>test</scope>
120-
</dependency>
121-
<dependency>
122-
<groupId>junit</groupId>
123-
<artifactId>junit</artifactId>
124-
<scope>test</scope>
125-
</dependency>
126-
<dependency>
127-
<groupId>org.junit.jupiter</groupId>
128-
<artifactId>junit-jupiter-api</artifactId>
129-
<scope>test</scope>
130-
</dependency>
131-
<dependency>
132-
<groupId>jakarta.validation</groupId>
133-
<artifactId>jakarta.validation-api</artifactId>
134-
</dependency>
135-
<dependency>
136-
<groupId>org.springframework</groupId>
137-
<artifactId>spring-context-support</artifactId>
138-
</dependency>
57+
13958
</dependencies>
14059

14160
<build>

readme-resources/img_2.png

176 KB
Loading

user-service/pom.xml

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@
1414
<artifactId>user-service</artifactId>
1515
<packaging>jar</packaging>
1616

17+
<properties>
18+
<java.version>21</java.version>
19+
<spring-boot.version>3.4.6</spring-boot.version>
20+
<testcontainers.version>1.19.7</testcontainers.version>
21+
</properties>
22+
23+
1724
<dependencies>
1825
<dependency>
1926
<groupId>org.springframework.boot</groupId>
2027
<artifactId>spring-boot-starter-web</artifactId>
2128
</dependency>
22-
<dependency>
23-
<groupId>org.springframework.kafka</groupId>
24-
<artifactId>spring-kafka</artifactId>
25-
</dependency>
2629
<dependency>
2730
<groupId>org.springframework.boot</groupId>
2831
<artifactId>spring-boot-starter-data-jpa</artifactId>
@@ -31,6 +34,10 @@
3134
<groupId>org.springframework.boot</groupId>
3235
<artifactId>spring-boot-starter-validation</artifactId>
3336
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.kafka</groupId>
39+
<artifactId>spring-kafka</artifactId>
40+
</dependency>
3441
<dependency>
3542
<groupId>com.fasterxml.jackson.core</groupId>
3643
<artifactId>jackson-databind</artifactId>
@@ -40,7 +47,10 @@
4047
<artifactId>lombok</artifactId>
4148
<scope>provided</scope>
4249
</dependency>
43-
50+
<dependency>
51+
<groupId>org.postgresql</groupId>
52+
<artifactId>postgresql</artifactId>
53+
</dependency>
4454
<dependency>
4555
<groupId>org.springframework.boot</groupId>
4656
<artifactId>spring-boot-starter-test</artifactId>
@@ -56,11 +66,21 @@
5666
<artifactId>junit-jupiter</artifactId>
5767
<scope>test</scope>
5868
</dependency>
69+
<dependency>
70+
<groupId>org.testcontainers</groupId>
71+
<artifactId>postgresql</artifactId>
72+
<scope>test</scope>
73+
</dependency>
5974
<dependency>
6075
<groupId>org.testcontainers</groupId>
6176
<artifactId>kafka</artifactId>
6277
<scope>test</scope>
6378
</dependency>
79+
<dependency>
80+
<groupId>org.testcontainers</groupId>
81+
<artifactId>jdbc</artifactId>
82+
<scope>test</scope>
83+
</dependency>
6484
</dependencies>
6585

6686
<build>
@@ -81,6 +101,17 @@
81101
<target>21</target>
82102
</configuration>
83103
</plugin>
104+
<plugin>
105+
<groupId>org.apache.maven.plugins</groupId>
106+
<artifactId>maven-compiler-plugin</artifactId>
107+
<version>3.11.0</version>
108+
<configuration>
109+
<compilerArgs>
110+
<arg>-parameters</arg>
111+
</compilerArgs>
112+
</configuration>
113+
</plugin>
114+
84115
</plugins>
85116
</build>
86117
</project>

user-service/src/main/java/myuserservice/advice/GlobalExceptionHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ public ResponseEntity<String> handleGeneric(Exception ex) {
3737
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
3838
.body("Unexpected error occurred");
3939
}
40+
41+
@ExceptionHandler(IllegalArgumentException.class)
42+
public ResponseEntity<String> handleIllegalArgument(IllegalArgumentException ex) {
43+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid argument: " + ex.getMessage());
44+
}
4045
}

0 commit comments

Comments
 (0)