Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ea23d31

Browse files
committedNov 2, 2023
iluwatar#2681 : add refactoring on PR#2712
-- refactoring source, test -- clean up in pom file -- updated comments in source -- refactoring the readme wiki
1 parent ca934b4 commit ea23d31

File tree

21 files changed

+554
-55
lines changed

21 files changed

+554
-55
lines changed
 

‎component/src/main/java/com/iluwatar/component/component/physiccomponent/PhysicComponent.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
125
package com.iluwatar.component.component.physiccomponent;
226

327
import com.iluwatar.component.GameObject;

‎messaging/.mvn/wrapper/maven-wrapper.properties

Lines changed: 0 additions & 2 deletions
This file was deleted.

‎messaging/README.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,61 @@
11
## MESSAGING DESIGN PATTERN
22

3+
4+
### Also Known As :
35
Event Driven / Message Stream Based MicroService
46

7+
### Intent:
8+
Sample implementation for Kafka Bases messaging service
9+
and events will be published based on transactions
10+
11+
### Explanation
12+
13+
Use asynchronous messaging for inter-service communication. Services communicating by exchanging messages over messaging channels.
14+
15+
There are several different styles of asynchronous communication:
16+
17+
Request/response - a service sends a request message to a recipient and expects to receive a reply message promptly
18+
Notifications - a sender sends a message a recipient but does not expect a reply. Nor is one sent.
19+
Request/asynchronous response - a service sends a request message to a recipient and expects to receive a reply message eventually
20+
Publish/subscribe - a service publishes a message to zero or more recipients
21+
Publish/asynchronous response - a service publishes a request to one or recipients, some of whom send back a reply
22+
23+
#### This pattern has the following benefits:
24+
25+
Loose runtime coupling since it decouples the message sender from the consumer
26+
* Improved availability since the message broker buffers messages until the consumer is able to process them
27+
* Supports a variety of communication patterns including request/reply, notifications, request/async response, publish/subscribe, publish/async response etc
28+
* This pattern has the following drawbacks:
29+
30+
### Class diagram:
31+
![img.png](etc/messaging-urm.png)
32+
33+
#### High Level Design:
34+
![img.png](etc/high-level-design.png)
35+
36+
37+
### Applicability:
38+
* Its more appropriate where the system expecting Pub/Sub based communication
39+
* If immediate notification is required between two microservices, then this pattern will help on the same
40+
41+
### Tutorials :
42+
1. https://medium.com/design-microservices-architecture-with-patterns/microservices-asynchronous-message-based-communication-6643bee06123
43+
2. https://microservices.io/patterns/communication-style/messaging.html
44+
45+
### Known uses:
46+
* Event Driven MicroService - Communication between more than one microservice using messaging channel
47+
* Distributed Transaction Service - Auditing and Notifying system from one to another
48+
49+
### Consequences:
50+
Additional complexity of message broker, which must be highly available
51+
This pattern has the following issues:
52+
53+
- Request/reply-style communication is more complex
54+
55+
### Related patterns :
56+
* The Saga pattern and CQRS pattern use messaging : https://microservices.io/patterns/data/saga.html , https://microservices.io/patterns/data/cqrs.html
57+
* The Transactional Outbox pattern enables messages to be sent as part of a database transaction : https://microservices.io/patterns/data/transactional-outbox.html
558

6-
![img.png](img.png)
59+
### Credits:
60+
``` Chris Richardson ```
61+
Experienced software architect, author of POJOs in Action, the creator of the original CloudFoundry.com
File renamed without changes.

‎messaging/etc/messaging-urm.png

53.4 KB
Loading

‎messaging/pom.xml

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,44 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
5+
6+
The MIT License
7+
Copyright © 2014-2022 Ilkka Seppälä
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
27+
-->
228
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
329
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
430
<modelVersion>4.0.0</modelVersion>
531
<parent>
6-
<groupId>org.springframework.boot</groupId>
7-
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>3.1.4</version>
9-
<relativePath/> <!-- lookup parent from repository -->
32+
<groupId>com.iluwatar</groupId>
33+
<artifactId>java-design-patterns</artifactId>
34+
<version>1.26.0-SNAPSHOT</version>
35+
<relativePath/>
1036
</parent>
1137
<groupId>com.iluwatar</groupId>
1238
<artifactId>messaging</artifactId>
1339
<version>0.0.1-SNAPSHOT</version>
1440
<name>messaging</name>
1541
<description>messaging</description>
16-
<properties>
17-
<java.version>11</java.version>
18-
</properties>
1942
<dependencies>
2043
<dependency>
2144
<groupId>org.springframework.boot</groupId>
@@ -37,33 +60,27 @@
3760
<dependency>
3861
<groupId>org.springframework.kafka</groupId>
3962
<artifactId>spring-kafka</artifactId>
40-
<version>3.0.11</version>
4163
</dependency>
4264

4365
<dependency>
4466
<groupId>org.springframework.kafka</groupId>
4567
<artifactId>spring-kafka-test</artifactId>
46-
<version>3.0.11</version>
47-
<scope>test</scope>
48-
</dependency>
49-
50-
<dependency>
51-
<groupId>org.testcontainers</groupId>
52-
<artifactId>kafka</artifactId>
53-
<version>1.19.1</version>
5468
<scope>test</scope>
5569
</dependency>
5670

5771
<dependency>
5872
<groupId>com.fasterxml.jackson.core</groupId>
5973
<artifactId>jackson-databind</artifactId>
60-
<version>2.15.3</version>
6174
</dependency>
6275

6376
<dependency>
6477
<groupId>org.springframework.boot</groupId>
6578
<artifactId>spring-boot-starter-web</artifactId>
66-
<version>3.1.4</version>
79+
</dependency>
80+
81+
<dependency>
82+
<groupId>org.projectlombok</groupId>
83+
<artifactId>lombok</artifactId>
6784
</dependency>
6885

6986

@@ -75,7 +92,32 @@
7592
<groupId>org.springframework.boot</groupId>
7693
<artifactId>spring-boot-maven-plugin</artifactId>
7794
</plugin>
95+
96+
97+
<plugin>
98+
<groupId>com.iluwatar.urm</groupId>
99+
<artifactId>urm-maven-plugin</artifactId>
100+
<version>2.1.1</version>
101+
<configuration>
102+
<!-- if outputDirectory is not set explicitly it will default to your build dir -->
103+
<outputDirectory>${project.basedir}/etc</outputDirectory>
104+
<packages>
105+
<param>com.iluwatar.messaging</param>
106+
</packages>
107+
<includeMainDirectory>true</includeMainDirectory>
108+
<includeTestDirectory>false</includeTestDirectory>
109+
<presenter>graphviz</presenter>
110+
</configuration>
111+
<executions>
112+
<execution>
113+
<phase>process-classes</phase>
114+
<goals>
115+
<goal>map</goal>
116+
</goals>
117+
</execution>
118+
</executions>
119+
</plugin>
78120
</plugins>
79121
</build>
80-
s
122+
81123
</project>

‎messaging/src/main/java/com/iluwatar/messaging/MessagingApplication.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,40 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
125
package com.iluwatar.messaging;
226

327
import org.springframework.boot.SpringApplication;
428
import org.springframework.boot.autoconfigure.SpringBootApplication;
529

30+
31+
/***
32+
* Design pattern : Event Driven / Message Stream Based MicroService
33+
* An Implementation for Kafka Based messaging service and events will be published based on transactions, and
34+
* it has implementation of asynchronous messaging for inter-service communication. Services communicating by
35+
* exchanging messages over messaging channels.
36+
*/
37+
638
@SpringBootApplication
739
public class MessagingApplication {
840

‎messaging/src/main/java/com/iluwatar/messaging/controller/OrderController.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
125
package com.iluwatar.messaging.controller;
226

327
import com.iluwatar.messaging.exception.ConsumerNotFoundException;

‎messaging/src/main/java/com/iluwatar/messaging/exception/ConsumerNotFoundException.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
125
package com.iluwatar.messaging.exception;
226

327
public class ConsumerNotFoundException extends Exception{

‎messaging/src/main/java/com/iluwatar/messaging/exception/OrderWithZeroItemException.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
125
package com.iluwatar.messaging.exception;
226

327
public class OrderWithZeroItemException extends Exception {

‎messaging/src/main/java/com/iluwatar/messaging/exception/RestaurantNotFoundException.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
125
package com.iluwatar.messaging.exception;
226

327
public class RestaurantNotFoundException extends Exception{

‎messaging/src/main/java/com/iluwatar/messaging/kafka/KafkaConsumer.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
125
package com.iluwatar.messaging.kafka;
226

327
import org.apache.kafka.clients.consumer.ConsumerRecord;

‎messaging/src/main/java/com/iluwatar/messaging/kafka/KafkaProducer.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
125
package com.iluwatar.messaging.kafka;
226

327
import org.slf4j.Logger;
Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
125
package com.iluwatar.messaging.model;
226

27+
import lombok.Getter;
28+
import lombok.Setter;
29+
30+
@Getter
31+
@Setter
332
public class MenuItemIdAndQuantity {
433

534
private String item;
@@ -15,21 +44,6 @@ public MenuItemIdAndQuantity(String item, int quantity) {
1544
this.quantity = quantity;
1645
}
1746

18-
public String getItem() {
19-
return item;
20-
}
21-
22-
public void setItem(String item) {
23-
this.item = item;
24-
}
25-
26-
public int getQuantity() {
27-
return quantity;
28-
}
29-
30-
public void setQuantity(int quantity) {
31-
this.quantity = quantity;
32-
}
3347
}
3448

3549

‎messaging/src/main/java/com/iluwatar/messaging/model/Order.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
125
package com.iluwatar.messaging.model;
226

327
import java.util.List;

‎messaging/src/main/java/com/iluwatar/messaging/model/OrderRequest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
125
package com.iluwatar.messaging.model;
226

327
import java.util.List;
@@ -10,6 +34,16 @@ public class OrderRequest {
1034

1135
private List<MenuItemIdAndQuantity> menuItemIdAndQuantityList;
1236

37+
public OrderRequest() {
38+
super();
39+
}
40+
41+
public OrderRequest(long consumerId, long restaurantId, List<MenuItemIdAndQuantity> menuItemIdAndQuantityList) {
42+
this.consumerId = consumerId;
43+
this.restaurantId = restaurantId;
44+
this.menuItemIdAndQuantityList = menuItemIdAndQuantityList;
45+
}
46+
1347
public long getConsumerId() {
1448
return consumerId;
1549
}

‎messaging/src/main/java/com/iluwatar/messaging/service/OrderService.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
125
package com.iluwatar.messaging.service;
226

327
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -8,13 +32,13 @@
832
import com.iluwatar.messaging.kafka.KafkaProducer;
933
import com.iluwatar.messaging.model.MenuItemIdAndQuantity;
1034
import com.iluwatar.messaging.model.Order;
11-
import jakarta.annotation.PostConstruct;
1235
import org.apache.commons.collections4.CollectionUtils;
1336
import org.slf4j.Logger;
1437
import org.slf4j.LoggerFactory;
1538
import org.springframework.beans.factory.annotation.Value;
1639
import org.springframework.stereotype.Service;
1740

41+
import javax.annotation.PostConstruct;
1842
import java.util.HashMap;
1943
import java.util.HashSet;
2044
import java.util.List;

‎messaging/src/main/resources/application.properties

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
#
2+
# This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
#
4+
# The MIT License
5+
# Copyright © 2014-2022 Ilkka Seppälä
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in
15+
# all copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
# THE SOFTWARE.
24+
#
25+
126

227
spring.kafka.consumer.auto-offset-reset=earliest
328
spring.kafka.consumer.group-id=messaging
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,49 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
125
package com.iluwatar.messaging;
226

27+
import com.iluwatar.messaging.kafka.KafkaConsumer;
28+
import com.iluwatar.messaging.kafka.KafkaProducer;
29+
import com.iluwatar.messaging.service.OrderService;
30+
import org.junit.jupiter.api.Assertions;
331
import org.junit.jupiter.api.Test;
32+
import org.springframework.beans.factory.annotation.Autowired;
433
import org.springframework.boot.test.context.SpringBootTest;
534

35+
636
@SpringBootTest
737
class MessagingApplicationTests {
838

39+
@Autowired OrderService orderService;
40+
41+
/***
42+
* Simple context test which will load all the IOC beans and properties to validate the spring boot application
43+
*/
944
@Test
1045
void contextLoads() {
46+
Assertions.assertNotNull(orderService);
1147
}
1248

1349
}

‎messaging/src/test/java/com/iluwatar/messaging/controller/OrderControllerIntegrationTest.java

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
125
package com.iluwatar.messaging.controller;
226

327
import com.fasterxml.jackson.core.JsonProcessingException;
428
import com.fasterxml.jackson.databind.ObjectMapper;
529
import com.iluwatar.messaging.model.MenuItemIdAndQuantity;
630
import com.iluwatar.messaging.model.OrderRequest;
7-
import jakarta.servlet.ServletContext;
8-
import org.jetbrains.annotations.NotNull;
31+
import lombok.SneakyThrows;
32+
import org.junit.jupiter.api.Assertions;
933
import org.junit.jupiter.api.Test;
1034
import org.junit.jupiter.api.extension.ExtendWith;
1135
import org.springframework.beans.factory.annotation.Autowired;
@@ -19,6 +43,7 @@
1943
import org.springframework.test.web.servlet.RequestBuilder;
2044
import org.springframework.test.web.servlet.ResultActions;
2145

46+
import javax.servlet.ServletContext;
2247
import java.nio.charset.StandardCharsets;
2348
import java.util.List;
2449

@@ -29,7 +54,11 @@
2954
@SpringBootTest
3055
@AutoConfigureMockMvc
3156
@DirtiesContext
32-
@EmbeddedKafka(partitions = 1, brokerProperties = {"listeners=PLAINTEXT://localhost:9092", "port=9092"})
57+
@EmbeddedKafka(
58+
topics = {"order-topic"},
59+
partitions = 2, brokerProperties = {
60+
"listeners=PLAINTEXT://localhost:9092",
61+
"auto.create.topics.enable=true"})
3362
public class OrderControllerIntegrationTest {
3463

3564
ObjectMapper objectMapper = new ObjectMapper();
@@ -39,10 +68,7 @@ public class OrderControllerIntegrationTest {
3968
@Test
4069
void shouldPlaceTheOrderAndReturn_200WhenOrderIsOk() throws Exception {
4170

42-
OrderRequest orderRequest = new OrderRequest();
43-
orderRequest.setConsumerId(1001L);
44-
orderRequest.setRestaurantId(2001L);
45-
orderRequest.setMenuItemIdAndQuantityList(testMenuItems());
71+
OrderRequest orderRequest = new OrderRequest(1001L, 2001L, testMenuItems());
4672

4773
RequestBuilder requestBuilder = new RequestBuilder() {
4874
@Override public MockHttpServletRequest buildRequest(ServletContext servletContext) {
@@ -60,6 +86,7 @@ void shouldPlaceTheOrderAndReturn_200WhenOrderIsOk() throws Exception {
6086
};
6187

6288
ResultActions resultActions = this.mockMvc.perform(requestBuilder).andExpect(status().isCreated());
89+
Assertions.assertNotNull(resultActions);
6390
}
6491

6592
@Test
@@ -69,21 +96,18 @@ void shouldPlaceTheOrderAndReturn_400WhenOrderIsInvalid() throws Exception {
6996
orderRequest.setMenuItemIdAndQuantityList(testMenuItems());
7097

7198
RequestBuilder requestBuilder = new RequestBuilder() {
72-
@Override public MockHttpServletRequest buildRequest(ServletContext servletContext) {
99+
@SneakyThrows @Override public MockHttpServletRequest buildRequest(ServletContext servletContext) {
73100
MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
74101
mockHttpServletRequest.setRequestURI("/order/place");
75102
mockHttpServletRequest.setMethod("POST");
76-
try {
77-
mockHttpServletRequest.setContent(objectMapper.writeValueAsString(orderRequest).getBytes(StandardCharsets.UTF_8));
78-
} catch (JsonProcessingException e) {
79-
throw new RuntimeException(e);
80-
}
103+
mockHttpServletRequest.setContent(objectMapper.writeValueAsString(orderRequest).getBytes(StandardCharsets.UTF_8));
81104
mockHttpServletRequest.setContentType(APPLICATION_JSON_VALUE);
82105
return mockHttpServletRequest;
83106
}
84107
};
85108

86109
ResultActions resultActions = this.mockMvc.perform(requestBuilder).andExpect(status().isBadRequest());
110+
Assertions.assertNotNull(resultActions);
87111
}
88112

89113
@Test
@@ -109,9 +133,10 @@ void shouldPlaceTheOrderAndReturn_500WhenOrderDoesNotHaveItems() throws Exceptio
109133
};
110134

111135
ResultActions resultActions = this.mockMvc.perform(requestBuilder).andExpect(status().isInternalServerError());
136+
Assertions.assertNotNull(resultActions);
112137
}
113138

114-
@NotNull private List<MenuItemIdAndQuantity> testMenuItems() {
139+
private List<MenuItemIdAndQuantity> testMenuItems() {
115140
return List.of(new MenuItemIdAndQuantity("CAKE", 1));
116141
}
117142
}

‎messaging/src/test/java/com/iluwatar/messaging/service/OrderServiceIntegrationTest.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
125
package com.iluwatar.messaging.service;
226

327
import com.iluwatar.messaging.exception.ConsumerNotFoundException;
@@ -6,7 +30,6 @@
630
import com.iluwatar.messaging.kafka.KafkaConsumer;
731
import com.iluwatar.messaging.model.MenuItemIdAndQuantity;
832
import com.iluwatar.messaging.model.Order;
9-
import org.jetbrains.annotations.NotNull;
1033
import org.junit.jupiter.api.Assertions;
1134
import org.junit.jupiter.api.Test;
1235
import org.springframework.beans.factory.annotation.Autowired;
@@ -56,8 +79,7 @@ void shouldThrowRestaurantNotFoundException() {
5679
void shouldThrowOrderWithZeroItemException() {
5780
Assertions.assertThrows(OrderWithZeroItemException.class, ()->orderService.createOrder(1001L, 2001, null));
5881
}
59-
60-
@NotNull private List<MenuItemIdAndQuantity> testMenuItems() {
82+
private List<MenuItemIdAndQuantity> testMenuItems() {
6183
return List.of(new MenuItemIdAndQuantity("CAKE", 1));
6284
}
6385

0 commit comments

Comments
 (0)
Please sign in to comment.