Compare commits

...

9 Commits

Author SHA1 Message Date
aac5318e39 docs: update .all-contributorsrc [skip ci] 2021-10-08 17:35:24 +00:00
b090155cba docs: update README.md [skip ci] 2021-10-08 17:35:23 +00:00
119abf3ee4 Doc: Corrected a few spelling mistakes () 2021-10-08 20:34:47 +03:00
87cc4df14b docs: Add frascu as a Contributor for Code ()
* docs: update README.md [skip ci]

* docs: update .all-contributorsrc [skip ci]

Co-authored-by: Subhrodip Mohanta <hello@subho.xyz>
Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
2021-10-05 10:07:07 +05:30
57f9c2e968 task: Update Lombok to version 1.18.20 ()
Co-authored-by: Subhrodip Mohanta <hello@subho.xyz>
2021-10-05 10:05:20 +05:30
42eb7950ae task: Fix broken links ()
* Fix some broken links

* Remove extra space

* Update filename

* Fix some links in localization folders

* Fix link

Co-authored-by: Subhrodip Mohanta <hello@subho.xyz>
2021-09-29 00:13:31 +05:30
be72a96cd6 docs: add tan31989 as a contributor for code ()
* docs: update README.md [skip ci]

* docs: update .all-contributorsrc [skip ci]

Co-authored-by: Subhrodip Mohanta <hello@subho.xyz>
Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
2021-09-29 00:11:14 +05:30
be25c0b433 bug-fix: Use Junit5 in the serverless module tests ()
* : Fixing the serverless tests to use Junit5 and also modifying other classes to remove the deprecated initMock() method

* : Fixing the sonar code smells

Co-authored-by: Subhrodip Mohanta <hello@subho.xyz>
2021-09-29 00:09:19 +05:30
be59e50205 doc: Fix Typos in French local doc ()
Co-authored-by: Subhrodip Mohanta <hello@subho.xyz>
2021-09-28 21:49:30 +05:30
26 changed files with 94 additions and 71 deletions
.all-contributorsrcREADME.md
active-object
README.md
etc
src
main
java
com
iluwatar
aggregator-microservices/aggregator-service/src/test/java/com/iluwatar/aggregator/microservices
api-gateway/api-gateway-service/src/test/java/com/iluwatar/api/gateway
async-method-invocation/src/test/java/com/iluwatar/async/method/invocation
command
commander
data-bus/src/test/java/com/iluwatar/databus
dependency-injection
event-driven-architecture
half-sync-half-async
localization
fr
ko
strategy
zh
active-object
chain
master-worker-pattern
model-view-viewmodel
pipeline
pom.xml
serverless
subclass-sandbox

@ -1586,6 +1586,33 @@
"contributions": [ "contributions": [
"code" "code"
] ]
},
{
"login": "tan31989",
"name": "Nagaraj Tantri",
"avatar_url": "https://avatars.githubusercontent.com/u/3784194?v=4",
"profile": "https://stackoverflow.com/users/308565/nagaraj-tantri",
"contributions": [
"code"
]
},
{
"login": "frascu",
"name": "Francesco Scuccimarri",
"avatar_url": "https://avatars.githubusercontent.com/u/7107651?v=4",
"profile": "http://scuccimarri.it",
"contributions": [
"code"
]
},
{
"login": "Conhan93",
"name": "Conny Hansson",
"avatar_url": "https://avatars.githubusercontent.com/u/71334757?v=4",
"profile": "https://github.com/Conhan93",
"contributions": [
"doc"
]
} }
], ],
"contributorsPerLine": 4, "contributorsPerLine": 4,

@ -10,12 +10,12 @@
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=iluwatar_java-design-patterns&metric=coverage)](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns) [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=iluwatar_java-design-patterns&metric=coverage)](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
[![Join the chat at https://gitter.im/iluwatar/java-design-patterns](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Join the chat at https://gitter.im/iluwatar/java-design-patterns](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --> <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-174-orange.svg?style=flat-square)](#contributors-) [![All Contributors](https://img.shields.io/badge/all_contributors-177-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END --> <!-- ALL-CONTRIBUTORS-BADGE:END -->
<br/> <br/>
Read in different language : [**zh**](/localization/zh/README.md), [**ko**](/localization/ko/README.md), [**fr**](/localization/fr/README.md), [**tr**](/localization/tr/README.md), [**ar**](/localization/ar/README.md), [**es**](/localization/es/README.md), [**pt**](/localization/pt/README.md) Read in different language : [**zh**](localization/zh/README.md), [**ko**](localization/ko/README.md), [**fr**](localization/fr/README.md), [**tr**](localization/tr/README.md), [**ar**](localization/ar/README.md), [**es**](localization/es/README.md), [**pt**](localization/pt/README.md)
<br/> <br/>
@ -336,6 +336,11 @@ This project is licensed under the terms of the MIT license.
<tr> <tr>
<td align="center"><a href="https://github.com/karthikbhat13"><img src="https://avatars.githubusercontent.com/u/22431014?v=4?s=100" width="100px;" alt=""/><br /><sub><b>karthikbhat13</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=karthikbhat13" title="Code">💻</a></td> <td align="center"><a href="https://github.com/karthikbhat13"><img src="https://avatars.githubusercontent.com/u/22431014?v=4?s=100" width="100px;" alt=""/><br /><sub><b>karthikbhat13</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=karthikbhat13" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/mortezaadi"><img src="https://avatars.githubusercontent.com/u/1329687?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Morteza Adigozalpour</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=mortezaadi" title="Code">💻</a></td> <td align="center"><a href="https://github.com/mortezaadi"><img src="https://avatars.githubusercontent.com/u/1329687?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Morteza Adigozalpour</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=mortezaadi" title="Code">💻</a></td>
<td align="center"><a href="https://stackoverflow.com/users/308565/nagaraj-tantri"><img src="https://avatars.githubusercontent.com/u/3784194?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nagaraj Tantri</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=tan31989" title="Code">💻</a></td>
<td align="center"><a href="http://scuccimarri.it"><img src="https://avatars.githubusercontent.com/u/7107651?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Francesco Scuccimarri</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=frascu" title="Code">💻</a></td>
</tr>
<tr>
<td align="center"><a href="https://github.com/Conhan93"><img src="https://avatars.githubusercontent.com/u/71334757?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Conny Hansson</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=Conhan93" title="Documentation">📖</a></td>
</tr> </tr>
</table> </table>

@ -11,7 +11,7 @@ tags:
## Intent ## Intent
The active object design pattern decouples method execution from method invocation for objects that each reside in their thread of control. The goal is to introduce concurrency, by using asynchronous method invocation and a scheduler for handling requests. The active object design pattern decouples method execution from method invocation for objects that each reside in their thread of control. The goal is to introduce concurrency, by using asynchronous method invocation, and a scheduler for handling requests.
## Explanation ## Explanation
@ -70,7 +70,7 @@ public abstract class ActiveCreature{
requests.put(new Runnable() { requests.put(new Runnable() {
@Override @Override
public void run() { public void run() {
logger.info("{} has started to roam and the wastelands.",name()); logger.info("{} has started to roam the wastelands.",name());
} }
} }
); );
@ -82,7 +82,7 @@ public abstract class ActiveCreature{
} }
``` ```
We can see that any class that will extend the ActiveCreature class will have its own thread of control to execute and invocate methods. We can see that any class that will extend the ActiveCreature class will have its own thread of control to invoke and execute methods.
For example, the Orc class: For example, the Orc class:
@ -96,7 +96,7 @@ public class Orc extends ActiveCreature {
} }
``` ```
Now, we can create multiple creatures such as Orcs, tell them to eat and roam and they will execute it on their own thread of control: Now, we can create multiple creatures such as Orcs, tell them to eat and roam, and they will execute it on their own thread of control:
```java ```java
public static void main(String[] args) { public static void main(String[] args) {
@ -123,4 +123,4 @@ Now, we can create multiple creatures such as Orcs, tell them to eat and roam an
## Class diagram ## Class diagram
![alt text](./etc/active-object.urm.PNG "Active Object class diagram") ![alt text](./etc/active-object.urm.png "Active Object class diagram")

Before

(image error) Size: 19 KiB

After

(image error) Size: 19 KiB

@ -82,7 +82,7 @@ public abstract class ActiveCreature {
} }
/** /**
* Roam in the wastelands. * Roam the wastelands.
* @throws InterruptedException due to firing a new Runnable. * @throws InterruptedException due to firing a new Runnable.
*/ */
public void roam() throws InterruptedException { public void roam() throws InterruptedException {

@ -48,7 +48,7 @@ class AggregatorTest {
@BeforeEach @BeforeEach
public void setup() { public void setup() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.openMocks(this);
} }
/** /**

@ -48,7 +48,7 @@ class ApiGatewayTest {
@BeforeEach @BeforeEach
public void setup() { public void setup() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.openMocks(this);
} }
/** /**

@ -68,7 +68,7 @@ class ThreadAsyncExecutorTest {
@BeforeEach @BeforeEach
void setUp() { void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.openMocks(this);
} }
/** /**

@ -25,4 +25,4 @@ We need a mechanism in place which can handle these kinds of situations. We have
## Credits ## Credits
* [https://www.grahamlea.com/2016/08/distributed-transactions-microservices-icebergs/] * [Distributed Transactions: The Icebergs of Microservices](https://www.grahamlea.com/2016/08/distributed-transactions-microservices-icebergs/)

@ -46,7 +46,7 @@ class DataBusTest {
@BeforeEach @BeforeEach
void setUp() { void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.openMocks(this);
} }
@Test @Test

@ -103,4 +103,4 @@ Use the Dependency Injection pattern when:
* [Dependency Injection Principles, Practices, and Patterns](https://www.amazon.com/gp/product/161729473X/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=161729473X&linkId=57079257a5c7d33755493802f3b884bd) * [Dependency Injection Principles, Practices, and Patterns](https://www.amazon.com/gp/product/161729473X/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=161729473X&linkId=57079257a5c7d33755493802f3b884bd)
* [Clean Code: A Handbook of Agile Software Craftsmanship](https://www.amazon.com/gp/product/0132350882/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0132350882&linkCode=as2&tag=javadesignpat-20&linkId=2c390d89cc9e61c01b9e7005c7842871) * [Clean Code: A Handbook of Agile Software Craftsmanship](https://www.amazon.com/gp/product/0132350882/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0132350882&linkCode=as2&tag=javadesignpat-20&linkId=2c390d89cc9e61c01b9e7005c7842871)
* [Java 9 Dependency Injection: Write loosely coupled code with Spring 5 and Guice](https://www.amazon.com/gp/product/1788296257/ref=as_li_tl?ie=UTF8&tag=javadesignpat-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=1788296257&linkId=4e9137a3bf722a8b5b156cce1eec0fc1) * [Java 9 Dependency Injection: Write loosely coupled code with Spring 5 and Guice](https://www.amazon.com/gp/product/1788296257/ref=as_li_tl?ie=UTF8&tag=javadesignpat-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=1788296257&linkId=4e9137a3bf722a8b5b156cce1eec0fc1)
* [Google Guice Tutorial: Open source Java based dependency injection framework](https://www.amazon.com/gp/product/B083P7DZ8M/ref=as_li_tl?ie=UTF8&tag=javadesignpat-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=B083P7DZ8M&linkId=04f0f902c877921e45215b624a124bfe) * [Google Guice: Agile Lightweight Dependency Injection Framework](https://www.amazon.com/gp/product/1590599977/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1590599977&linkId=3b10c90b7ba480a1b7777ff38000f956)

@ -24,7 +24,6 @@ Use an Event-driven architecture when
## Real world examples ## Real world examples
* SendGrid, an email API, sends events whenever an email is processed, delivered, opened etc... (https://sendgrid.com/docs/API_Reference/Webhooks/event.html)
* Chargify, a billing API, exposes payment activity through various events (https://docs.chargify.com/api-events) * Chargify, a billing API, exposes payment activity through various events (https://docs.chargify.com/api-events)
* Amazon's AWS Lambda, lets you execute code in response to events such as changes to Amazon S3 buckets, updates to an Amazon DynamoDB table, or custom events generated by your applications or devices. (https://aws.amazon.com/lambda) * Amazon's AWS Lambda, lets you execute code in response to events such as changes to Amazon S3 buckets, updates to an Amazon DynamoDB table, or custom events generated by your applications or devices. (https://aws.amazon.com/lambda)
* MySQL runs triggers based on events such as inserts and update events happening on database tables. * MySQL runs triggers based on events such as inserts and update events happening on database tables.
@ -32,6 +31,6 @@ Use an Event-driven architecture when
## Credits ## Credits
* [Event-driven architecture - Wikipedia](https://en.wikipedia.org/wiki/Event-driven_architecture) * [Event-driven architecture - Wikipedia](https://en.wikipedia.org/wiki/Event-driven_architecture)
* [Fundamental Components of an Event-Driven Architecture](http://giocc.com/fundamental-components-of-an-event-driven-architecture.html) * [What is an Event-Driven Architecture](https://aws.amazon.com/event-driven-architecture/)
* [Real World Applications/Event Driven Applications](https://wiki.haskell.org/Real_World_Applications/Event_Driven_Applications) * [Real World Applications/Event Driven Applications](https://wiki.haskell.org/Real_World_Applications/Event_Driven_Applications)
* [Event-driven architecture definition](http://searchsoa.techtarget.com/definition/event-driven-architecture) * [Event-driven architecture definition](http://searchsoa.techtarget.com/definition/event-driven-architecture)

@ -30,7 +30,7 @@ Use Half-Sync/Half-Async pattern when
* [BSD Unix networking subsystem](https://www.dre.vanderbilt.edu/~schmidt/PDF/PLoP-95.pdf) * [BSD Unix networking subsystem](https://www.dre.vanderbilt.edu/~schmidt/PDF/PLoP-95.pdf)
* [Real Time CORBA](http://www.omg.org/news/meetings/workshops/presentations/realtime2001/4-3_Pyarali_thread-pool.pdf) * [Real Time CORBA](http://www.omg.org/news/meetings/workshops/presentations/realtime2001/4-3_Pyarali_thread-pool.pdf)
* [Android AsyncTask framework](http://developer.android.com/reference/android/os/AsyncTask.html) * [Android AsyncTask framework](https://developer.android.com/reference/android/os/AsyncTask)
## Credits ## Credits

@ -48,7 +48,7 @@ que lorsquils sont nécessaires pour une extensibilité pratique.
Une fois que vous êtes familiarisé avec ces concepts, vous pouvez commencer à explorer les Une fois que vous êtes familiarisé avec ces concepts, vous pouvez commencer à explorer les
[modèles de conception disponibles](https://java-design-patterns.com/patterns/) [modèles de conception disponibles](https://java-design-patterns.com/patterns/)
par nimporte laquelle les approches suivantes&nbsp: par nimporte laquelle des approches suivantes&nbsp;:
- Recherchez un modèle spécifique par son nom. - Recherchez un modèle spécifique par son nom.
Vous nen trouvez pas&nbsp;? Veuillez signaler un nouveau modèle [ici](https://github.com/iluwatar/java-design-patterns/issues). Vous nen trouvez pas&nbsp;? Veuillez signaler un nouveau modèle [ici](https://github.com/iluwatar/java-design-patterns/issues).

@ -163,7 +163,7 @@ public class LambdaStrategy {
## 클래스 다이어그램 ## 클래스 다이어그램
![alt text](./etc/strategy_urm.png "Strategy") ![alt text](../../../strategy/etc/strategy_urm.png "Strategy")
## 적용 가능성 ## 적용 가능성

@ -122,4 +122,4 @@ public class Orc extends ActiveCreature {
## 类图 ## 类图
![alt text](../../../active-object/etc/active-object.urm.PNG "Active Object class diagram") ![alt text](../../../active-object/etc/active-object.urm.png "Active Object class diagram")

@ -139,7 +139,7 @@ king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax")); // Orc so
``` ```
## 类图 ## 类图
![alt text](../../../chain/etc/chain.urm.png "Chain of Responsibility class diagram") ![alt text](../../../chain-of-responsibility/etc/chain-of-responsibility.urm.png "Chain of Responsibility class diagram")
## 适用性 ## 适用性
使用责任链模式当 使用责任链模式当

@ -28,5 +28,5 @@ In this pattern, parallel processing is performed using a system consisting of a
## Credits ## Credits
* [https://docs.gigaspaces.com/sbp/master-worker-pattern.html] * [Master-Worker Pattern](https://docs.gigaspaces.com/sbp/master-worker-pattern.html)
* [http://www.cs.sjsu.edu/~pearce/oom/patterns/behavioral/masterslave.htm] * [The Master-Slave Design Pattern](https://www.cs.sjsu.edu/~pearce/oom/patterns/behavioral/masterslave.htm)

@ -118,7 +118,7 @@ To deploy the example, go to model-view-viewmodel folder and run:
* [Zkoss Demo](https://www.zkoss.org/zkdemo/getting_started/mvvm) * [Zkoss Demo](https://www.zkoss.org/zkdemo/getting_started/mvvm)
* [Learn MVVM](https://www.learnmvvm.com/) * [Learn MVVM](https://www.learnmvvm.com/)
* [Android Developer CodeLabs](https://codelabs.developers.google.com/codelabs/android-databinding) * [Data Binding in Android](https://developer.android.com/codelabs/android-databinding#0)
## Typical Use Case ## Typical Use Case

@ -114,7 +114,7 @@ the [Single Responsibility Principle (SRP)](https://java-design-patterns.com/pri
## Related patterns ## Related patterns
* [Chain of Responsibility](https://java-design-patterns.com/patterns/chain/) * [Chain of Responsibility](https://java-design-patterns.com/patterns/chain-of-responsibility/)
## Credits ## Credits

@ -62,7 +62,7 @@
<slf4j.version>1.7.30</slf4j.version> <slf4j.version>1.7.30</slf4j.version>
<logback.version>1.2.3</logback.version> <logback.version>1.2.3</logback.version>
<aws-lambda-core.version>1.1.0</aws-lambda-core.version> <aws-lambda-core.version>1.1.0</aws-lambda-core.version>
<aws-java-sdk-dynamodb.version>1.11.289</aws-java-sdk-dynamodb.version> <aws-java-sdk-dynamodb.version>1.12.13</aws-java-sdk-dynamodb.version>
<aws-lambda-java-events.version>2.0.1</aws-lambda-java-events.version> <aws-lambda-java-events.version>2.0.1</aws-lambda-java-events.version>
<jackson.version>2.12.3</jackson.version> <jackson.version>2.12.3</jackson.version>
<jaxb-api.version>2.3.1</jaxb-api.version> <jaxb-api.version>2.3.1</jaxb-api.version>
@ -71,8 +71,8 @@
<system-lambda.version>1.1.0</system-lambda.version> <system-lambda.version>1.1.0</system-lambda.version>
<urm.version>2.0.0</urm.version> <urm.version>2.0.0</urm.version>
<mockito-junit-jupiter.version>3.5.0</mockito-junit-jupiter.version> <mockito-junit-jupiter.version>3.5.0</mockito-junit-jupiter.version>
<lombok.version>1.18.14</lombok.version> <lombok.version>1.18.20</lombok.version>
<byte-buddy.version>1.10.21</byte-buddy.version> <byte-buddy.version>1.11.5</byte-buddy.version>
<javassist.version>3.27.0-GA</javassist.version> <javassist.version>3.27.0-GA</javassist.version>
<maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version> <maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
<maven-checkstyle-plugin.version>3.1.0</maven-checkstyle-plugin.version> <maven-checkstyle-plugin.version>3.1.0</maven-checkstyle-plugin.version>

@ -44,16 +44,6 @@
<groupId>com.amazonaws</groupId> <groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId> <artifactId>aws-java-sdk-dynamodb</artifactId>
<version>${aws-java-sdk-dynamodb.version}</version> <version>${aws-java-sdk-dynamodb.version}</version>
<exclusions>
<exclusion>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
</exclusion>
<exclusion>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-kms</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.amazonaws</groupId> <groupId>com.amazonaws</groupId>
@ -86,8 +76,8 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>org.hamcrest</groupId>
<artifactId>junit</artifactId> <artifactId>hamcrest-core</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

@ -23,6 +23,9 @@
package com.iluwatar.serverless.baas.api; package com.iluwatar.serverless.baas.api;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@ -32,31 +35,29 @@ import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.iluwatar.serverless.baas.model.Person; import com.iluwatar.serverless.baas.model.Person;
import java.util.Map; import java.util.Map;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/** /**
* Unit tests for FindPersonApiHandler Created by dheeraj.mummar on 3/5/18. * Unit tests for FindPersonApiHandler Created by dheeraj.mummar on 3/5/18.
*/ */
@RunWith(MockitoJUnitRunner.class) class FindPersonApiHandlerTest {
public class FindPersonApiHandlerTest {
private FindPersonApiHandler findPersonApiHandler; private FindPersonApiHandler findPersonApiHandler;
@Mock @Mock
private DynamoDBMapper dynamoDbMapper; private DynamoDBMapper dynamoDbMapper;
@Before @BeforeEach
public void setUp() { public void setUp() {
MockitoAnnotations.openMocks(this);
this.findPersonApiHandler = new FindPersonApiHandler(); this.findPersonApiHandler = new FindPersonApiHandler();
this.findPersonApiHandler.setDynamoDbMapper(dynamoDbMapper); this.findPersonApiHandler.setDynamoDbMapper(dynamoDbMapper);
} }
@Test @Test
public void handleRequest() { void handleRequest() {
findPersonApiHandler.handleRequest(apiGatewayProxyRequestEvent(), mock(Context.class)); findPersonApiHandler.handleRequest(apiGatewayProxyRequestEvent(), mock(Context.class));
verify(dynamoDbMapper, times(1)).load(Person.class, "37e7a1fe-3544-473d-b764-18128f02d72d"); verify(dynamoDbMapper, times(1)).load(Person.class, "37e7a1fe-3544-473d-b764-18128f02d72d");
} }

@ -34,18 +34,18 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.iluwatar.serverless.baas.model.Address; import com.iluwatar.serverless.baas.model.Address;
import com.iluwatar.serverless.baas.model.Person; import com.iluwatar.serverless.baas.model.Person;
import org.junit.Assert; import org.junit.jupiter.api.BeforeEach;
import org.junit.Before; import org.junit.jupiter.api.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.MockitoAnnotations;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/** /**
* Unit tests for SavePersonApiHandler Created by dheeraj.mummar on 3/4/18. * Unit tests for SavePersonApiHandler Created by dheeraj.mummar on 3/4/18.
*/ */
@RunWith(MockitoJUnitRunner.class) class SavePersonApiHandlerTest {
public class SavePersonApiHandlerTest {
private SavePersonApiHandler savePersonApiHandler; private SavePersonApiHandler savePersonApiHandler;
@ -54,31 +54,32 @@ public class SavePersonApiHandlerTest {
private final ObjectMapper objectMapper = new ObjectMapper(); private final ObjectMapper objectMapper = new ObjectMapper();
@Before @BeforeEach
public void setUp() { public void setUp() {
MockitoAnnotations.openMocks(this);
this.savePersonApiHandler = new SavePersonApiHandler(); this.savePersonApiHandler = new SavePersonApiHandler();
this.savePersonApiHandler.setDynamoDbMapper(dynamoDbMapper); this.savePersonApiHandler.setDynamoDbMapper(dynamoDbMapper);
} }
@Test @Test
public void handleRequestSavePersonSuccessful() throws JsonProcessingException { void handleRequestSavePersonSuccessful() throws JsonProcessingException {
var person = newPerson(); var person = newPerson();
var body = objectMapper.writeValueAsString(person); var body = objectMapper.writeValueAsString(person);
var request = apiGatewayProxyRequestEvent(body); var request = apiGatewayProxyRequestEvent(body);
var ctx = mock(Context.class); var ctx = mock(Context.class);
var apiGatewayProxyResponseEvent = this.savePersonApiHandler.handleRequest(request, ctx); var apiGatewayProxyResponseEvent = this.savePersonApiHandler.handleRequest(request, ctx);
verify(dynamoDbMapper, times(1)).save(person); verify(dynamoDbMapper, times(1)).save(person);
Assert.assertNotNull(apiGatewayProxyResponseEvent); assertNotNull(apiGatewayProxyResponseEvent);
Assert.assertEquals(Integer.valueOf(201), apiGatewayProxyResponseEvent.getStatusCode()); assertEquals(Integer.valueOf(201), apiGatewayProxyResponseEvent.getStatusCode());
} }
@Test @Test
public void handleRequestSavePersonException() { void handleRequestSavePersonException() {
var request = apiGatewayProxyRequestEvent("invalid sample request"); var request = apiGatewayProxyRequestEvent("invalid sample request");
var ctx = mock(Context.class); var ctx = mock(Context.class);
var apiGatewayProxyResponseEvent = this.savePersonApiHandler.handleRequest(request, ctx); var apiGatewayProxyResponseEvent = this.savePersonApiHandler.handleRequest(request, ctx);
Assert.assertNotNull(apiGatewayProxyResponseEvent); assertNotNull(apiGatewayProxyResponseEvent);
Assert.assertEquals(Integer.valueOf(400), apiGatewayProxyResponseEvent.getStatusCode()); assertEquals(Integer.valueOf(400), apiGatewayProxyResponseEvent.getStatusCode());
} }
private APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent(String body) { private APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent(String body) {

@ -25,4 +25,4 @@ The Subclass Sandbox pattern is a very simple, common pattern lurking in lots of
## Credits ## Credits
* [Game Programming Patterns - Subclass Sandbox]([http://gameprogrammingpatterns.com/subclass-sandbox.html](http://gameprogrammingpatterns.com/subclass-sandbox.html)) * [Game Programming Patterns - Subclass Sandbox](https://gameprogrammingpatterns.com/subclass-sandbox.html)