未验证 提交 4d3fd5f8 编写于 作者: I Ilkka Seppälä 提交者: GitHub

Merge branch 'master' into all-contributors/add-Kevinyl3

......@@ -1740,6 +1740,52 @@
"review",
"code"
]
},
{
"login": "Shrirang97",
"name": "Shrirang",
"avatar_url": "https://avatars.githubusercontent.com/u/28738668?v=4",
"profile": "https://github.com/Shrirang97",
"contributions": [
"review",
"code"
]
},
{
"login": "interactwithankush",
"name": "interactwithankush",
"avatar_url": "https://avatars.githubusercontent.com/u/18613127?v=4",
"profile": "https://github.com/interactwithankush",
"contributions": [
"code"
]
},
{
"login": "yuhangbin",
"name": "CharlieYu",
"avatar_url": "https://avatars.githubusercontent.com/u/17566866?v=4",
"profile": "https://github.com/yuhangbin",
"contributions": [
"code"
]
},
{
"login": "Leisterbecker",
"name": "Leisterbecker",
"avatar_url": "https://avatars.githubusercontent.com/u/20650323?v=4",
"profile": "https://github.com/Leisterbecker",
"contributions": [
"code"
]
},
{
"login": "castleKing1997",
"name": "DragonDreamer",
"avatar_url": "https://avatars.githubusercontent.com/u/35420129?v=4",
"profile": "http://rosaecrucis.cn",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,
......
......@@ -10,7 +10,7 @@
[![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)
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-191-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-196-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->
<br/>
......@@ -321,6 +321,14 @@ This project is licensed under the terms of the MIT license.
<tr>
<td align="center"><a href="https://www.linkedin.com/in/abhinav-vashisth-06613b208/"><img src="https://avatars.githubusercontent.com/u/89785800?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Abhinav Vashisth</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=vashisthabhinav" title="Documentation">📖</a></td>
<td align="center"><a href="http://no website"><img src="https://avatars.githubusercontent.com/u/47126749?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Kevin</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/pulls?q=is%3Apr+reviewed-by%3AKevinyl3" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/iluwatar/java-design-patterns/commits?author=Kevinyl3" title="Code">💻</a></td>
<td align="center"><a href="http://no website"><img src="https://avatars.githubusercontent.com/u/47126749?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Kevin</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/pulls?q=is%3Apr+reviewed-by%3AKevinyl3" title="Reviewed Pull Requests">👀</a></td>
<td align="center"><a href="https://github.com/Shrirang97"><img src="https://avatars.githubusercontent.com/u/28738668?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Shrirang</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/pulls?q=is%3Apr+reviewed-by%3AShrirang97" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/iluwatar/java-design-patterns/commits?author=Shrirang97" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/interactwithankush"><img src="https://avatars.githubusercontent.com/u/18613127?v=4?s=100" width="100px;" alt=""/><br /><sub><b>interactwithankush</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=interactwithankush" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/yuhangbin"><img src="https://avatars.githubusercontent.com/u/17566866?v=4?s=100" width="100px;" alt=""/><br /><sub><b>CharlieYu</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=yuhangbin" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/Leisterbecker"><img src="https://avatars.githubusercontent.com/u/20650323?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Leisterbecker</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=Leisterbecker" title="Code">💻</a></td>
</tr>
<tr>
<td align="center"><a href="http://rosaecrucis.cn"><img src="https://avatars.githubusercontent.com/u/35420129?v=4?s=100" width="100px;" alt=""/><br /><sub><b>DragonDreamer</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=castleKing1997" title="Code">💻</a></td>
</tr>
</table>
......
......@@ -18,10 +18,10 @@ couldn't otherwise because of incompatible interfaces.
## Explanation
Real world example
Real-world example
> Consider that you have some pictures in your memory card and you need to transfer them to your computer. In order to transfer them you need some kind of adapter that is compatible with your computer ports so that you can attach memory card to your computer. In this case card reader is an adapter.
> Another example would be the famous power adapter; a three legged plug can't be connected to a two pronged outlet, it needs to use a power adapter that makes it compatible with the two pronged outlet.
> Consider that you have some pictures on your memory card and you need to transfer them to your computer. To transfer them, you need some kind of adapter that is compatible with your computer ports so that you can attach a memory card to your computer. In this case card reader is an adapter.
> Another example would be the famous power adapter; a three-legged plug can't be connected to a two-pronged outlet, it needs to use a power adapter that makes it compatible with the two-pronged outlets.
> Yet another example would be a translator translating words spoken by one person to another
In plain words
......@@ -36,7 +36,7 @@ Wikipedia says
Consider a captain that can only use rowing boats and cannot sail at all.
First we have interfaces `RowingBoat` and `FishingBoat`
First, we have interfaces `RowingBoat` and `FishingBoat`
```java
public interface RowingBoat {
......@@ -68,7 +68,7 @@ public class Captain {
}
```
Now let's say the pirates are coming and our captain needs to escape but there is only fishing boat available. We need to create an adapter that allows the captain to operate the fishing boat with his rowing boat skills.
Now let's say the pirates are coming and our captain needs to escape but there is only a fishing boat available. We need to create an adapter that allows the captain to operate the fishing boat with his rowing boat skills.
```java
@Slf4j
......@@ -100,10 +100,10 @@ captain.row();
## Applicability
Use the Adapter pattern when
* you want to use an existing class, and its interface does not match the one you need
* you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces
* you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.
* most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code.
* You want to use an existing class, and its interface does not match the one you need
* You want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces
* You need to use several existing subclasses, but it's impractical to adapt their interface by subclassing everyone. An object adapter can adapt the interface of its parent class.
* Most of the applications using third-party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code.
## Tutorials
......@@ -114,17 +114,17 @@ Use the Adapter pattern when
## Consequences
Class and object adapters have different trade-offs. A class adapter
* adapts Adaptee to Target by committing to a concrete Adaptee class. As a consequence, a class adapter won’t work when we want to adapt a class and all its subclasses.
* let’s Adapter override some of Adaptee’s behavior, since Adapter is a subclass of Adaptee.
* introduces only one object, and no additional pointer indirection is needed to get to the adaptee.
* Adapts Adaptee to Target by committing to a concrete Adaptee class. As a consequence, a class adapter won’t work when we want to adapt a class and all its subclasses.
* Let’s Adapter override some of Adaptee’s behavior since Adapter is a subclass of Adaptee.
* Introduces only one object, and no additional pointer indirection is needed to get to the adaptee.
An object adapter
* let’s a single Adapter work with many Adaptees—that is, the Adaptee itself and all of its subclasses (if any). The Adapter can also add functionality to all Adaptees at once.
* makes it harder to override Adaptee behavior. It will require subclassing Adaptee and making Adapter refer to the subclass rather than the Adaptee itself.
* Lets a single Adapter work with many Adaptees—that is, the Adaptee itself and all of its subclasses (if any). The Adapter can also add functionality to all Adaptees at once.
* Makes it harder to override Adaptee behavior. It will require subclassing Adaptee and making the Adapter refer to the subclass rather than the Adaptee itself.
## Known uses
## Real-world examples
* [java.util.Arrays#asList()](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList%28T...%29)
* [java.util.Collections#list()](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#list-java.util.Enumeration-)
......
......@@ -33,7 +33,7 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
/**
* Test class
* Tests for the adapter pattern.
*/
class AdapterPatternTest {
......
......@@ -33,9 +33,7 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
class AppTest {
/**
* Issue: Add at least one assertion to this test case.
*
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
* Check whether the execution of the main method in {@link App}
* throws an exception.
*/
......
# Build output
target/
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
# IDE
.idea/
*.iml
.settings/
.project
.classpath
# macOS
.DS_Store
# Azure Functions
local.settings.json
bin/
obj/
---
layout: pattern
title: Claim Check Pattern
folder: cloud-claim-check-pattern
permalink: /patterns/cloud-claim-check-pattern/
categories: Cloud
language: en
tags:
- Cloud distributed
- Microservices
---
## Name
[Claim Check Pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/claim-check)
## Also known as
[Reference-Based Messaging](https://www.enterpriseintegrationpatterns.com/patterns/messaging/StoreInLibrary.html)
## Intent
- Reduce the load of data transfer through the Internet. Instead of sending actual data directly, just send the message reference.
- Improve data security. As only message reference is shared, no data is exposed to the Internet.
## Explanation
Real-World Example
> Suppose if you want to build a photo processing system. A photo processing system takes an image as input, processes it, and outputs a different set of images. Consider system has one persistent storage, one input component, ten processing components, messaging platform. Once a photo is given to the input component, it stores that image on persistent storage. It then creates ten different messages/events with the same image location and publishes them to the messaging platform. The messaging platform triggers ten different processing components. The ten processing components extract information about image location from the received event and then read an image from persistent storage. They generate ten different images from the original image and drop these images again to persistent storage.
In Plain words
> Split a large message into a claim check and a payload. Send the claim check to the messaging platform and store the payload to an external service. This pattern allows large messages to be processed while protecting the message bus and the client from being overwhelmed or slowed down. This pattern also helps to reduce costs, as storage is usually cheaper than resource units used by the messaging platform.([ref](https://docs.microsoft.com/en-us/azure/architecture/patterns/claim-check))
## Architecture Diagram
![alt text](./etc/Claim-Check-Pattern.png "Claim Check Pattern")
## Applicability
Use the Claim Check Pattern when
- Huge processing data causes a lot of bandwidth consumption to transfer data through the Internet.
- To secure your data transfer by storing in common persistent storage.
- Using a cloud platform - Azure Functions or AWS Lambda, Azure EventGrid or AWS Event Bridge, Azure Blob Storage or AWS S3 Bucket.
- Each service must be independent and idempotent. Output data is dropped to persistent storage by the service.
- Publish-subscribe messaging pattern needs to be used.
## Consequences
- This pattern is stateless. Any compute API will not store any data.
- You must have persistent storage and a reliable messaging platform.
## Tutorials
### Workflow
Suppose a telecom company wants to build call cost calculator system which generate the call cost daily. At the end of each day, details of the calls made by the consumers are stored somewhere. The call calculator system will read this data and generate call cost data for each user. Consumers will be billed using this generated data in case of postpaid service.
Producer class( `UsageDetailPublisherFunction` Azure Function) will generate call usage details (here we are generating call data in producer class itself. In real world scenario, it will read from storage). `UsageDetailPublisherFunction` creates a message. Message consists of message header and message body. Message header is basically an event grid event or claim or message reference. Message body contains actual data. `UsageDetailPublisherFunction` sends a message header to Event Grid topic `usage-detail` and drops an entire message to the blob storage. Event Grid then sent this message header to the `UsageCostProcessorFunction` Azure function. It will read the entire message from blob storage with the help of the header, will calculate call cost and drop the result to the blob storage.
### Class Diagrams
![alt text](./etc/class-diagram.png "Claim-Check-Class-Diagram")
### Setup
- Any operating system can be used macOS, Windows, Linux as everything is deployed on Azure.
- Install Java JDK 11 and set up Java environmental variables.
- Install Git.
- Install Visual Studio Code.
- Install [ Azure Functions extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions) to be able to deploy using Visual studio.
### Storage Data
The data is stored in the Azure blob storage in the container `callusageapp`. For every trigger, one GUID is created. Under the `GUID folder`, 2 files will be created `input.json` and `output.json`.
`Input.json` is dropped `producer` azure function which contains call usage details.` Output.json` contains call cost details which are dropped by the `consumer` azure function.
## Credits
- [Messaging Pattern - Claim Check](https://www.enterpriseintegrationpatterns.com/patterns/messaging/StoreInLibrary.html)
- [Azure Architecture Pattern - Claim Check Pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/claim-check)
# Build output
target/
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
# IDE
.idea/
*.iml
.settings/
.project
.classpath
# macOS
.DS_Store
# Azure Functions
local.settings.json
bin/
obj/
@startuml
package com.iluwatar.claimcheckpattern.producer.calldetails.functions {
class UsageDetailPublisherFunction {
- eventHandlerUtility : EventHandlerUtility<MessageHeader>
- messageHandlerUtility : MessageHandlerUtility<UsageDetail>
+ UsageDetailPublisherFunction()
+ UsageDetailPublisherFunction(messageHandlerUtility : MessageHandlerUtility<UsageDetail>, eventHandlerUtility : EventHandlerUtility<MessageHeader>)
+ run(request : HttpRequestMessage<Optional<String>>, context : ExecutionContext) : HttpResponseMessage
}
}
package com.iluwatar.claimcheckpattern.domain {
class Message<T> {
- messageBody : MessageBody<T>
- messageHeader : MessageHeader
+ Message<T>()
+ getMessageBody() : MessageBody<T>
+ getMessageHeader() : MessageHeader
+ setMessageBody(messageBody : MessageBody<T>)
+ setMessageHeader(messageHeader : MessageHeader)
}
class MessageBody<T> {
- data : List<T>
+ MessageBody<T>()
+ getData() : List<T>
+ setData(data : List<T>)
}
class MessageHeader {
- data : Object
- dataVersion : String
- eventTime : String
- eventType : String
- id : String
- subject : String
- topic : String
+ MessageHeader()
+ getData() : Object
+ getDataVersion() : String
+ getEventTime() : String
+ getEventType() : String
+ getId() : String
+ getSubject() : String
+ getTopic() : String
+ setData(data : Object)
+ setDataVersion(dataVersion : String)
+ setEventTime(eventTime : String)
+ setEventType(eventType : String)
+ setId(id : String)
+ setSubject(subject : String)
+ setTopic(topic : String)
}
class MessageReference {
- dataFileName : String
- dataLocation : String
+ MessageReference()
+ MessageReference(dataLocation : String, dataFileName : String)
+ getDataFileName() : String
+ getDataLocation() : String
+ setDataFileName(dataFileName : String)
+ setDataLocation(dataLocation : String)
}
class UsageCostDetail {
- callCost : double
- dataCost : double
- userId : String
+ UsageCostDetail()
+ getCallCost() : double
+ getDataCost() : double
+ getUserId() : String
+ setCallCost(callCost : double)
+ setDataCost(dataCost : double)
+ setUserId(userId : String)
}
class UsageDetail {
- data : int
- duration : int
- userId : String
+ UsageDetail()
+ getData() : int
+ getDuration() : int
+ getUserId() : String
+ setData(data : int)
+ setDuration(duration : int)
+ setUserId(userId : String)
}
}
package com.iluwatar.claimcheckpattern.utility {
class EventHandlerUtility<T> {
- customEventClient : EventGridPublisherClient<BinaryData>
+ EventHandlerUtility<T>()
+ EventHandlerUtility<T>(customEventClient : EventGridPublisherClient<BinaryData>)
+ publishEvent(customEvent : T, logger : Logger)
}
class MessageHandlerUtility<T> {
- blobServiceClient : BlobServiceClient
+ MessageHandlerUtility<T>()
+ MessageHandlerUtility<T>(blobServiceClient : BlobServiceClient)
+ dropToPersistantStorage(message : Message<T>, logger : Logger)
+ readFromPersistantStorage(messageReference : MessageReference, logger : Logger) : Message<T>
}
}
package com.iluwatar.claimcheckpattern.consumer.callcostprocessor.functions {
class UsageCostProcessorFunction {
- messageHandlerUtilityForUsageCostDetail : MessageHandlerUtility<UsageCostDetail>
- messageHandlerUtilityForUsageDetail : MessageHandlerUtility<UsageDetail>
+ UsageCostProcessorFunction()
+ UsageCostProcessorFunction(messageHandlerUtilityForUsageDetail : MessageHandlerUtility<UsageDetail>, messageHandlerUtilityForUsageCostDetail : MessageHandlerUtility<UsageCostDetail>)
- calculateUsageCostDetails(usageDetailsList : List<UsageDetail>) : List<UsageCostDetail>
+ run(request : HttpRequestMessage<Optional<String>>, context : ExecutionContext) : HttpResponseMessage
}
}
UsageCostProcessorFunction --> "-messageHandlerUtilityForUsageDetail" MessageHandlerUtility
Message --> "-messageBody" MessageBody
UsageDetailPublisherFunction --> "-eventHandlerUtility" EventHandlerUtility
Builder ..+ HttpResponseMessage
UsageDetailPublisherFunction --> "-messageHandlerUtility" MessageHandlerUtility
Message --> "-messageHeader" MessageHeader
@enduml
\ No newline at end of file
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8" ?>
<!--
The MIT License
Copyright © 2014-2021 Ilkka Seppälä
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>claim-check-pattern</artifactId>
<version>1.25.0-SNAPSHOT</version>
</parent>
<artifactId>call-usage-app</artifactId>
<name>call-usage-app</name>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<azure.functions.maven.plugin.version>1.14.0</azure.functions.maven.plugin.version>
<azure.functions.java.library.version>1.4.2</azure.functions.java.library.version>
<functionAppName>CallUsageApp</functionAppName>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-sdk-bom</artifactId>
<version>1.0.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.microsoft.azure.functions</groupId>
<artifactId>azure-functions-java-library</artifactId>
<version>${azure.functions.java.library.version}</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-messaging-eventgrid</artifactId>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
<version>12.13.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-maven-plugin</artifactId>
<version>${azure.functions.maven.plugin.version}</version>
<configuration>
<!-- function app name -->
<appName>${functionAppName}</appName>
<!-- function app resource group -->
<resourceGroup>java-functions-group</resourceGroup>
<!-- function app service plan name -->
<appServicePlanName>java-functions-app-service-plan</appServicePlanName>
<!-- function app region-->
<!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details#supported-regions for all valid values -->
<region>westus</region>
<!-- function pricingTier, default to be consumption if not specified -->
<!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details#supported-pricing-tiers for all valid values -->
<!-- <pricingTier></pricingTier> -->
<!-- Whether to disable application insights, default is false -->
<!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details for all valid configurations for application insights-->
<!-- <disableAppInsights></disableAppInsights> -->
<runtime>
<!-- runtime os, could be windows, linux or docker-->
<os>windows</os>
<javaVersion>11</javaVersion>
</runtime>
<appSettings>
<property>
<name>FUNCTIONS_EXTENSION_VERSION</name>
<value>~3</value>
</property>
</appSettings>
</configuration>
<executions>
<execution>
<id>package-functions</id>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
<!--Remove obj folder generated by .NET SDK in maven clean-->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<filesets>
<fileset>
<directory>obj</directory>
</fileset>
</filesets>
</configuration>
</plugin>
</plugins>
</build>
</project>
/*
* The MIT License
* Copyright © 2014-2021 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.claimcheckpattern.consumer.callcostprocessor.functions;
import com.azure.core.util.BinaryData;
import com.azure.core.util.serializer.TypeReference;
import com.azure.messaging.eventgrid.EventGridEvent;
import com.azure.messaging.eventgrid.systemevents.SubscriptionValidationEventData;
import com.azure.messaging.eventgrid.systemevents.SubscriptionValidationResponse;
import com.iluwatar.claimcheckpattern.domain.Message;
import com.iluwatar.claimcheckpattern.domain.MessageBody;
import com.iluwatar.claimcheckpattern.domain.MessageHeader;
import com.iluwatar.claimcheckpattern.domain.MessageReference;
import com.iluwatar.claimcheckpattern.domain.UsageCostDetail;
import com.iluwatar.claimcheckpattern.domain.UsageDetail;
import com.iluwatar.claimcheckpattern.utility.MessageHandlerUtility;
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
* Azure Functions with HTTP Trigger.
* This is Consumer class.
*/
public class UsageCostProcessorFunction {
private MessageHandlerUtility<UsageDetail> messageHandlerUtilityForUsageDetail;
private MessageHandlerUtility<UsageCostDetail> messageHandlerUtilityForUsageCostDetail;
public UsageCostProcessorFunction() {
this.messageHandlerUtilityForUsageDetail = new MessageHandlerUtility<>();
this.messageHandlerUtilityForUsageCostDetail = new MessageHandlerUtility<>();
}
public UsageCostProcessorFunction(
MessageHandlerUtility<UsageDetail> messageHandlerUtilityForUsageDetail,
MessageHandlerUtility<UsageCostDetail> messageHandlerUtilityForUsageCostDetail) {
this.messageHandlerUtilityForUsageDetail = messageHandlerUtilityForUsageDetail;
this.messageHandlerUtilityForUsageCostDetail = messageHandlerUtilityForUsageCostDetail;
}
/**
* Azure function which gets triggered when event grid event send event to it.
* After receiving event, it read input file from blob storage, calculate call cost details.
* It creates new message with cost details and drop message to blob storage.
* @param request represents HttpRequestMessage
* @param context represents ExecutionContext
* @return HttpResponseMessage
*/
@FunctionName("UsageCostProcessorFunction")
public HttpResponseMessage run(@HttpTrigger(name = "req", methods = { HttpMethod.GET,
HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
try {
var eventGridEvents = EventGridEvent.fromString(request.getBody().get());
for (var eventGridEvent : eventGridEvents) {
// Handle system events
if (eventGridEvent.getEventType()
.equals("Microsoft.EventGrid.SubscriptionValidationEvent")) {
SubscriptionValidationEventData subscriptionValidationEventData = eventGridEvent.getData()
.toObject(SubscriptionValidationEventData.class);
// Handle the subscription validation event
var responseData = new SubscriptionValidationResponse();
responseData.setValidationResponse(subscriptionValidationEventData.getValidationCode());
return request.createResponseBuilder(HttpStatus.OK).body(responseData).build();
} else if (eventGridEvent.getEventType().equals("UsageDetail")) {
// Get message header and reference
var messageReference = eventGridEvent.getData()
.toObject(MessageReference.class);
// Read message from persistent storage
var message = this.messageHandlerUtilityForUsageDetail
.readFromPersistantStorage(messageReference, context.getLogger());
// Get Data and generate cost details
List<UsageDetail> usageDetailsList = BinaryData.fromObject(
message.getMessageBody().getData())
.toObject(new TypeReference<>() {
});
var usageCostDetailsList = calculateUsageCostDetails(usageDetailsList);
// Create message body
var newMessageBody = new MessageBody<UsageCostDetail>();
newMessageBody.setData(usageCostDetailsList);
// Create message header
var newMessageReference = new MessageReference("callusageapp",
eventGridEvent.getId() + "/output.json");
var newMessageHeader = new MessageHeader();
newMessageHeader.setId(eventGridEvent.getId());
newMessageHeader.setSubject("UsageCostProcessor");
newMessageHeader.setTopic("");
newMessageHeader.setEventType("UsageCostDetail");
newMessageHeader.setEventTime(OffsetDateTime.now().toString());
newMessageHeader.setData(newMessageReference);
newMessageHeader.setDataVersion("v1.0");
// Create entire message
var newMessage = new Message<UsageCostDetail>();
newMessage.setMessageHeader(newMessageHeader);
newMessage.setMessageBody(newMessageBody);
// Drop data to persistent storage
this.messageHandlerUtilityForUsageCostDetail.dropToPersistantStorage(newMessage,
context.getLogger());
context.getLogger().info("Message is dropped successfully");
return request.createResponseBuilder(HttpStatus.OK)
.body("Message is dropped successfully").build();
}
}
} catch (Exception e) {
context.getLogger().warning(e.getMessage());
}
return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body(null).build();
}
private List<UsageCostDetail> calculateUsageCostDetails(List<UsageDetail> usageDetailsList) {
if (usageDetailsList == null) {
return null;
}
var usageCostDetailsList = new ArrayList<UsageCostDetail>();
usageDetailsList.forEach(usageDetail -> {
var usageCostDetail = new UsageCostDetail();
usageCostDetail.setUserId(usageDetail.getUserId());
usageCostDetail.setCallCost(usageDetail.getDuration() * 0.30); // 0.30₹ per minute
usageCostDetail.setDataCost(usageDetail.getData() * 0.20); // 0.20₹ per MB
usageCostDetailsList.add(usageCostDetail);
});
return usageCostDetailsList;
}
}
/*
* The MIT License
* Copyright © 2014-2021 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.claimcheckpattern.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* It is the message which gets dropped or read by Producer or Consumer Azure functions.
* It is stored in the json format.
* @param <T> represents UsageDetail or UsageCostDetail
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Message<T> {
private MessageHeader messageHeader;
private MessageBody<T> messageBody;
}
\ No newline at end of file
/*
* The MIT License
* Copyright © 2014-2021 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.claimcheckpattern.domain;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* It is message body of the message.
* It stores actual data in our case UsageCostDetail or UsageDetail.
* @param <T> represents UsageDetail or UsageCostDetail
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MessageBody<T> {
private List<T> data;
}
/*
* The MIT License
* Copyright © 2014-2021 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.claimcheckpattern.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* This is message header or event which is sent to Event Grid.
* Its structure is same as Azure Event Grid Event Class.
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MessageHeader {
private String id;
private String subject;
private String topic;
private String eventType;
private String eventTime;
private Object data;
private String dataVersion;
}
\ No newline at end of file
/*
* The MIT License
* Copyright © 2014-2021 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.claimcheckpattern.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* This is claim/message reference class.
* It contains the information about data where it is stored in persistent storage
* and file name.
* dataLocation is blob storage container name.
* dataFileName is file name in above container.
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MessageReference {
private String dataLocation;
private String dataFileName;
}
/*
* The MIT License
* Copyright © 2014-2021 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.claimcheckpattern.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* This is call cost details class.
* It stores userId of the caller, call duration cost and data cost.
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UsageCostDetail {
private String userId;
private double callCost;
private double dataCost;
}
/*
* The MIT License
* Copyright © 2014-2021 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.claimcheckpattern.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* This is call usage detail calls.
* It stores userId of the caller, call duration and data used.
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UsageDetail {
private String userId;
private int duration;
private int data;
}
/*
* The MIT License
* Copyright © 2014-2021 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.claimcheckpattern.producer.calldetails.functions;
import com.azure.messaging.eventgrid.EventGridEvent;
import com.azure.messaging.eventgrid.systemevents.SubscriptionValidationEventData;
import com.azure.messaging.eventgrid.systemevents.SubscriptionValidationResponse;
import com.iluwatar.claimcheckpattern.domain.Message;
import com.iluwatar.claimcheckpattern.domain.MessageBody;
import com.iluwatar.claimcheckpattern.domain.MessageHeader;
import com.iluwatar.claimcheckpattern.domain.MessageReference;
import com.iluwatar.claimcheckpattern.domain.UsageDetail;
import com.iluwatar.claimcheckpattern.utility.EventHandlerUtility;
import com.iluwatar.claimcheckpattern.utility.MessageHandlerUtility;
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.UUID;
/**
* Azure Functions with HTTP Trigger.
* This is Producer class.
*/
public class UsageDetailPublisherFunction {
private MessageHandlerUtility<UsageDetail> messageHandlerUtility;
private EventHandlerUtility<MessageHeader> eventHandlerUtility;
public UsageDetailPublisherFunction() {
this.messageHandlerUtility = new MessageHandlerUtility<>();
this.eventHandlerUtility = new EventHandlerUtility<>();
}
public UsageDetailPublisherFunction(MessageHandlerUtility<UsageDetail> messageHandlerUtility,
EventHandlerUtility<MessageHeader> eventHandlerUtility) {
this.messageHandlerUtility = messageHandlerUtility;
this.eventHandlerUtility = eventHandlerUtility;
}
/**
* Azure function which create message, drop it in persistent storage
* and publish the event to Event Grid topic.
* @param request represents HttpRequestMessage
* @param context represents ExecutionContext
* @return HttpResponseMessage
*/
@FunctionName("UsageDetailPublisherFunction")
public HttpResponseMessage run(@HttpTrigger(name = "req", methods = {
HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
try {
var eventGridEvents = EventGridEvent.fromString(request.getBody().get());
for (EventGridEvent eventGridEvent : eventGridEvents) {
// Handle system events
if (eventGridEvent.getEventType()
.equals("Microsoft.EventGrid.SubscriptionValidationEvent")) {
SubscriptionValidationEventData subscriptionValidationEventData = eventGridEvent.getData()
.toObject(SubscriptionValidationEventData.class);
// Handle the subscription validation event
var responseData = new SubscriptionValidationResponse();
responseData.setValidationResponse(subscriptionValidationEventData.getValidationCode());
return request.createResponseBuilder(HttpStatus.OK).body(responseData).build();
} else if (eventGridEvent.getEventType().equals("UsageDetail")) {
// Create message body
var messageBody = new MessageBody<UsageDetail>();
var usageDetailsList = new ArrayList<UsageDetail>();
var random = new Random();
for (int i = 0; i < 51; i++) {
var usageDetail = new UsageDetail();
usageDetail.setUserId("userId" + i);
usageDetail.setData(random.nextInt(500));
usageDetail.setDuration(random.nextInt(500));
usageDetailsList.add(usageDetail);
}
messageBody.setData(usageDetailsList);
// Create message header
var messageHeader = new MessageHeader();
messageHeader.setId(UUID.randomUUID().toString());
messageHeader.setSubject("UsageDetailPublisher");
messageHeader.setTopic("usagecostprocessorfunction-topic");
messageHeader.setEventType("UsageDetail");
messageHeader.setEventTime(OffsetDateTime.now().toString());
var messageReference = new MessageReference("callusageapp",
messageHeader.getId() + "/input.json");
messageHeader.setData(messageReference);
messageHeader.setDataVersion("v1.0");
// Create entire message
var message = new Message<UsageDetail>();
message.setMessageHeader(messageHeader);
message.setMessageBody(messageBody);
// Drop data to persistent storage
this.messageHandlerUtility.dropToPersistantStorage(message, context.getLogger());
// Publish event to event grid topic
eventHandlerUtility.publishEvent(messageHeader, context.getLogger());
context.getLogger().info("Message is dropped and event is published successfully");
return request.createResponseBuilder(HttpStatus.OK).body(message).build();
}
}
} catch (Exception e) {
context.getLogger().warning(e.getMessage());
}
return request.createResponseBuilder(HttpStatus.OK).body(null).build();
}
}
/*
* The MIT License
* Copyright © 2014-2021 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.claimcheckpattern.utility;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.util.BinaryData;
import com.azure.messaging.eventgrid.EventGridPublisherClient;
import com.azure.messaging.eventgrid.EventGridPublisherClientBuilder;
import java.util.logging.Logger;
/**
* This class is event publisher utility which published message header to Event Grid topic.
* @param <T> represents UsageDetail or UsageCostDetail
*/
public class EventHandlerUtility<T> {
private EventGridPublisherClient<BinaryData> customEventClient;
/** Default constructor.
*/
public EventHandlerUtility() {
this.customEventClient = new EventGridPublisherClientBuilder()
.endpoint(System.getenv("EventGridURL"))
.credential(new AzureKeyCredential(System.getenv("EventGridKey")))
.buildCustomEventPublisherClient();
}
/**
Parameterized constructor.
*/
public EventHandlerUtility(EventGridPublisherClient<BinaryData> customEventClient) {
this.customEventClient = customEventClient;
}
/**
Method for publishing event to Event Grid Topic.
*/
public void publishEvent(T customEvent, Logger logger) {
try {
customEventClient.sendEvent(BinaryData.fromObject(customEvent));
} catch (Exception e) {
logger.info(e.getMessage());
}
}
}
/*
* The MIT License
* Copyright © 2014-2021 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.claimcheckpattern.utility;
import com.azure.core.util.BinaryData;
import com.azure.core.util.serializer.TypeReference;
import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.iluwatar.claimcheckpattern.domain.Message;
import com.iluwatar.claimcheckpattern.domain.MessageReference;
import java.util.logging.Logger;
/**
* This class read and drop message from Azure blob storage.
* @param <T> represents UsageDetail or UsageCostDetail
*/
public class MessageHandlerUtility<T> {
private BlobServiceClient blobServiceClient;
/**
* Parameterized constructor.
* @param blobServiceClient represents BlobServiceClient
*/
public MessageHandlerUtility(BlobServiceClient blobServiceClient) {
this.blobServiceClient = blobServiceClient;
}
/**
* Default constructor.
*/
public MessageHandlerUtility() {
// Create a BlobServiceClient object which will be used to create a container
// client
this.blobServiceClient = new BlobServiceClientBuilder()
.connectionString(System.getenv("BlobStorageConnectionString")).buildClient();
}
/**
* Read message from blob storage.
* @param messageReference represents MessageReference
* @param logger represents Logger
* @return Message
*/
public Message<T> readFromPersistantStorage(MessageReference messageReference, Logger logger) {
Message<T> message = null;
try {
// Get container name from message reference
String containerName = messageReference.getDataLocation();
// Get blob name from message reference
String blobName = messageReference.getDataFileName();
// Get container client
BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
// Get a reference to a blob
BlobClient blobClient = containerClient.getBlobClient(blobName);
// download the blob
message = blobClient.downloadContent().toObject(new TypeReference<Message<T>>() {
});
} catch (Exception e) {
logger.info(e.getMessage());
}
return message;
}
/**
* Drop message to blob storage.
* @param message represents Message
* @param logger represents Logger
*/
public void dropToPersistantStorage(Message<T> message, Logger logger) {
try {
// Get message reference
MessageReference messageReference = (MessageReference) message.getMessageHeader().getData();
// Create a unique name for the container
String containerName = messageReference.getDataLocation();
// Create the container and return a container client object
BlobContainerClient containerClient = this.blobServiceClient
.getBlobContainerClient(containerName);
if (!containerClient.exists()) {
containerClient.create();
}
// Get a reference to a blob
BlobClient blobClient = containerClient.getBlobClient(messageReference.getDataFileName());
// Upload the blob
blobClient.upload(BinaryData.fromObject(message));
} catch (Exception e) {
logger.info(e.getMessage());
}
}
}
/*
* The MIT License
* Copyright © 2014-2021 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.claimcheckpattern;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.HttpStatusType;
import java.util.HashMap;
import java.util.Map;
/**
* The mock for HttpResponseMessage, can be used in unit tests to verify if the
* returned response by HTTP trigger function is correct or not.
*/
public class HttpResponseMessageMock implements HttpResponseMessage {
private int httpStatusCode;
private HttpStatusType httpStatus;
private Object body;
private Map<String, String> headers;
public HttpResponseMessageMock(HttpStatusType status, Map<String, String> headers, Object body) {
this.httpStatus = status;
this.httpStatusCode = status.value();
this.headers = headers;
this.body = body;
}
@Override
public HttpStatusType getStatus() {
return this.httpStatus;
}
@Override
public int getStatusCode() {
return httpStatusCode;
}
@Override
public String getHeader(String key) {
return this.headers.get(key);
}
@Override
public Object getBody() {
return this.body;
}
public static class HttpResponseMessageBuilderMock implements HttpResponseMessage.Builder {
private Object body;
private int httpStatusCode;
private Map<String, String> headers = new HashMap<>();
private HttpStatusType httpStatus;
public Builder status(HttpStatus status) {
this.httpStatusCode = status.value();
this.httpStatus = status;
return this;
}
@Override
public Builder status(HttpStatusType httpStatusType) {
this.httpStatusCode = httpStatusType.value();
this.httpStatus = httpStatusType;
return this;
}
@Override
public HttpResponseMessage.Builder header(String key, String value) {
this.headers.put(key, value);
return this;
}
@Override
public HttpResponseMessage.Builder body(Object body) {
this.body = body;
return this;
}
@Override
public HttpResponseMessage build() {
return new HttpResponseMessageMock(this.httpStatus, this.headers, this.body);
}
}
}
/*
* The MIT License
* Copyright © 2014-2021 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.claimcheckpattern.consumer.callcostprocessor.functions;
import com.iluwatar.claimcheckpattern.HttpResponseMessageMock;
import com.iluwatar.claimcheckpattern.domain.Message;
import com.iluwatar.claimcheckpattern.domain.MessageBody;
import com.iluwatar.claimcheckpattern.domain.MessageHeader;
import com.iluwatar.claimcheckpattern.domain.MessageReference;
import com.iluwatar.claimcheckpattern.domain.UsageCostDetail;
import com.iluwatar.claimcheckpattern.domain.UsageDetail;
import com.iluwatar.claimcheckpattern.utility.MessageHandlerUtility;
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.OffsetDateTime;
import java.util.*;
import java.util.logging.Logger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
/**
* Unit test for Function class.
*/
@ExtendWith(MockitoExtension.class)
public class UsageCostProcessorFunctionTest {
@Mock
MessageHandlerUtility<UsageDetail> mockMessageHandlerUtilityForUsageADetail;
@Mock
MessageHandlerUtility<UsageCostDetail> mockMessageHandlerUtilityForUsageCostDetail;
@Mock
ExecutionContext context;
Message<UsageCostDetail> messageToDrop;
Message<UsageDetail> messageToRead;
MessageReference messageReference;
@InjectMocks
UsageCostProcessorFunction usageCostProcessorFunction;
@BeforeEach
public void setUp() {
var messageBodyUsageDetail = new MessageBody<UsageDetail>();
var usageDetailsList = new ArrayList<UsageDetail>();
var messageBodyUsageCostDetail = new MessageBody<UsageCostDetail>();
var usageCostDetailsList = new ArrayList<UsageCostDetail>();
for (int i = 0; i < 2; i++) {
var usageDetail = new UsageDetail();
usageDetail.setUserId("userId" + i);
usageDetail.setData(i + 1);
usageDetail.setDuration(i + 1);
usageDetailsList.add(usageDetail);
var usageCostDetail = new UsageCostDetail();
usageCostDetail.setUserId(usageDetail.getUserId());
usageCostDetail.setDataCost(usageDetail.getData() * 0.20);
usageCostDetail.setCallCost(usageDetail.getDuration() * 0.30);
usageCostDetailsList.add(usageCostDetail);
}
messageBodyUsageDetail.setData(usageDetailsList);
messageBodyUsageCostDetail.setData(usageCostDetailsList);
// Create message header
var messageHeader = new MessageHeader();
messageHeader.setId(UUID.randomUUID().toString());
messageHeader.setSubject("UsageDetailPublisher");
messageHeader.setTopic("usagecostprocessorfunction-topic");
messageHeader.setEventType("UsageDetail");
messageHeader.setEventTime(OffsetDateTime.now().toString());
this.messageReference = new MessageReference("callusageapp", "d8284456-dfff-4bd4-9cef-ea99f70f4835/input.json");
messageHeader.setData(messageReference);
messageHeader.setDataVersion("v1.0");
// Create entire message
messageToRead = new Message<>();
messageToRead.setMessageHeader(messageHeader);
messageToRead.setMessageBody(messageBodyUsageDetail);
messageToDrop = new Message<>();
messageToDrop.setMessageHeader(messageHeader);
messageToDrop.setMessageBody(messageBodyUsageCostDetail);
}
/**
* Unit test for HttpTriggerJava method.
*/
@Test
public void shouldTriggerHttpAzureFunctionJavaWithSubscriptionValidationEventType() throws Exception {
// Setup
@SuppressWarnings("unchecked")
final HttpRequestMessage<Optional<String>> req = mock(HttpRequestMessage.class);
String fileAbsolutePath = getClass().getResource("/subscriptionValidationEvent.json").getPath()
.replaceAll("%20", " "), jsonBody = Files.readString(Paths.get(fileAbsolutePath)).replaceAll("\n", " ");
doReturn(Optional.of(jsonBody)).when(req).getBody();
doAnswer(new Answer<HttpResponseMessage.Builder>() {
@Override
public HttpResponseMessage.Builder answer(InvocationOnMock invocation) {
HttpStatus status = (HttpStatus) invocation.getArguments()[0];
return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status);
}
}).when(req).createResponseBuilder(any(HttpStatus.class));
final ExecutionContext context = mock(ExecutionContext.class);
// Invoke
final HttpResponseMessage ret = this.usageCostProcessorFunction.run(req, context);
// Verify
assertEquals(ret.getStatus(), HttpStatus.OK);
}
@Test
public void shouldTriggerHttpAzureFunctionJavaWithUsageDetailEventType() throws Exception {
// Setup
@SuppressWarnings("unchecked")
final HttpRequestMessage<Optional<String>> req = mock(HttpRequestMessage.class);
String fileAbsolutePath = getClass().getResource("/usageDetailEvent.json").getPath().replaceAll("%20", " "),
jsonBody = Files.readString(Paths.get(fileAbsolutePath)).replaceAll("\n", " ");
doReturn(Optional.of(jsonBody)).when(req).getBody();
doReturn(Logger.getGlobal()).when(context).getLogger();
when(this.mockMessageHandlerUtilityForUsageADetail.readFromPersistantStorage(any(MessageReference.class),
eq(Logger.getGlobal()))).thenReturn(messageToRead);
doAnswer(new Answer<HttpResponseMessage.Builder>() {
@Override
public HttpResponseMessage.Builder answer(InvocationOnMock invocation) {
HttpStatus status = (HttpStatus) invocation.getArguments()[0];
return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status);
}
}).when(req).createResponseBuilder(any(HttpStatus.class));
assertNotNull(this.mockMessageHandlerUtilityForUsageADetail);
assertEquals(this.messageToRead, this.mockMessageHandlerUtilityForUsageADetail
.readFromPersistantStorage(this.messageReference, Logger.getGlobal()));
// Invoke
final HttpResponseMessage ret = this.usageCostProcessorFunction.run(req, context);
// Verify
assertEquals(HttpStatus.OK, ret.getStatus());
assertEquals("Message is dropped successfully", ret.getBody());
}
}
/*
* The MIT License
* Copyright © 2014-2021 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.claimcheckpattern.producer.calldetails.functions;
import com.iluwatar.claimcheckpattern.HttpResponseMessageMock;
import com.iluwatar.claimcheckpattern.domain.MessageHeader;
import com.iluwatar.claimcheckpattern.domain.UsageDetail;
import com.iluwatar.claimcheckpattern.utility.EventHandlerUtility;
import com.iluwatar.claimcheckpattern.utility.MessageHandlerUtility;
import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.logging.Logger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
/**
* Unit test for Function class.
*/
@ExtendWith(MockitoExtension.class)
public class UsageDetailPublisherFunctionTest {
@Mock
MessageHandlerUtility<UsageDetail> mockMessageHandlerUtility;
@Mock
EventHandlerUtility<MessageHeader> mockEventHandlerUtility;
@InjectMocks
UsageDetailPublisherFunction usageDetailPublisherFunction;
/**
* Unit test for HttpTriggerJava method.
*/
@Test
public void shouldTriggerHttpAzureFunctionJavaWithSubscriptionValidationEventType() throws Exception {
// Setup
@SuppressWarnings("unchecked")
final HttpRequestMessage<Optional<String>> req = mock(HttpRequestMessage.class);
String fileAbsolutePath = getClass().getResource("/subscriptionValidationEvent.json").getPath()
.replaceAll("%20", " "), jsonBody = Files.readString(Paths.get(fileAbsolutePath)).replaceAll("\n", " ");
doReturn(Optional.of(jsonBody)).when(req).getBody();
doAnswer(new Answer<HttpResponseMessage.Builder>() {
@Override
public HttpResponseMessage.Builder answer(InvocationOnMock invocation) {
HttpStatus status = (HttpStatus) invocation.getArguments()[0];
return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status);
}
}).when(req).createResponseBuilder(any(HttpStatus.class));
final ExecutionContext context = mock(ExecutionContext.class);
// Invoke
final HttpResponseMessage ret = this.usageDetailPublisherFunction.run(req, context);
// Verify
assertEquals(ret.getStatus(), HttpStatus.OK);
}
@Test
public void shouldTriggerHttpAzureFunctionJavaWithUsageDetailEventType() throws Exception {
// Setup
@SuppressWarnings("unchecked")
final HttpRequestMessage<Optional<String>> req = mock(HttpRequestMessage.class);
String fileAbsolutePath = getClass().getResource("/usageDetailEvent.json").getPath().replaceAll("%20", " "),
jsonBody = Files.readString(Paths.get(fileAbsolutePath)).replaceAll("\n", " ");
doReturn(Optional.of(jsonBody)).when(req).getBody();
doAnswer(new Answer<HttpResponseMessage.Builder>() {
@Override
public HttpResponseMessage.Builder answer(InvocationOnMock invocation) {
HttpStatus status = (HttpStatus) invocation.getArguments()[0];
return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status);
}
}).when(req).createResponseBuilder(any(HttpStatus.class));
final ExecutionContext context = mock(ExecutionContext.class);
doReturn(Logger.getGlobal()).when(context).getLogger();
// Invoke
final HttpResponseMessage ret = this.usageDetailPublisherFunction.run(req, context);
// Verify
assertEquals(ret.getStatus(), HttpStatus.OK);
}
}
/*
* The MIT License
* Copyright © 2014-2021 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.claimcheckpattern.utility;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import com.azure.core.util.BinaryData;
import com.azure.messaging.eventgrid.EventGridPublisherClient;
import com.iluwatar.claimcheckpattern.domain.Message;
import com.iluwatar.claimcheckpattern.domain.UsageDetail;
import java.util.logging.Logger;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
public class EventHandlerUtilityTest {
@Mock
EventGridPublisherClient<BinaryData> mockCustomEventClient;
@InjectMocks
EventHandlerUtility<Message<UsageDetail>> eventHandlerUtility;
@BeforeEach
public void setUp() {
System.setProperty("EventGridURL", "https://www.dummyEndpoint.com/api/events");
System.setProperty("EventGridKey", "EventGridURL");
}
@Test
void shouldPublishEvent() {
doNothing().when(mockCustomEventClient).sendEvent(any(BinaryData.class));
eventHandlerUtility.publishEvent(null, Logger.getLogger("logger"));
verify(mockCustomEventClient, times(1)).sendEvent(any(BinaryData.class));
}
@Test
void shouldPublishEventWithNullLogger() {
eventHandlerUtility.publishEvent(null, null);
verify(mockCustomEventClient, times(1)).sendEvent(any(BinaryData.class));
}
}
/*
* The MIT License
* Copyright © 2014-2021 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.claimcheckpattern.utility;
import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.iluwatar.claimcheckpattern.domain.*;
import com.iluwatar.claimcheckpattern.utility.MessageHandlerUtility;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.logging.Logger;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
public class MessageHandlerUtilityTest {
@Mock
private BlobClient mockBlobClient;
@Mock
private BlobContainerClient mockContainerClient;
@Mock
private BlobServiceClient mockBlobServiceClient;
@InjectMocks
private MessageHandlerUtility<UsageDetail> messageHandlerUtility;
private Message<UsageDetail> messageToPublish;
private MessageReference messageReference;
@BeforeEach
public void setUp() {
System.setProperty("BlobStorageConnectionString", "https://www.dummyEndpoint.com/api/blobs");
var messageBody = new MessageBody<UsageDetail>();
var usageDetailsList = new ArrayList<UsageDetail>();
var random = new Random();
for (int i = 0; i < 51; i++) {
var usageDetail = new UsageDetail();
usageDetail.setUserId("userId" + i);
usageDetail.setData(random.nextInt(500));
usageDetail.setDuration(random.nextInt(500));
usageDetailsList.add(usageDetail);
}
messageBody.setData(usageDetailsList);
// Create message header
var messageHeader = new MessageHeader();
messageHeader.setId(UUID.randomUUID().toString());
messageHeader.setSubject("UsageDetailPublisher");
messageHeader.setTopic("usagecostprocessorfunction-topic");
messageHeader.setEventType("UsageDetail");
messageHeader.setEventTime(OffsetDateTime.now().toString());
this.messageReference = new MessageReference("callusageapp", messageHeader.getId() + "/input.json");
messageHeader.setData(messageReference);
messageHeader.setDataVersion("v1.0");
// Create entire message
this.messageToPublish = new Message<>();
this.messageToPublish.setMessageHeader(messageHeader);
this.messageToPublish.setMessageBody(messageBody);
when(mockContainerClient.getBlobClient(anyString())).thenReturn(mockBlobClient);
when(mockBlobServiceClient.getBlobContainerClient(anyString())).thenReturn(mockContainerClient);
}
@Test
void shouldDropMessageToPersistantStorage() {
messageHandlerUtility.dropToPersistantStorage(messageToPublish, Logger.getLogger("logger"));
verify(mockBlobServiceClient, times(1)).getBlobContainerClient(anyString());
// verify(mockContainerClient, times(0)).exists();
}
@Test
void shouldReadMessageFromPersistantStorage() {
messageHandlerUtility.readFromPersistantStorage(messageReference, Logger.getLogger("logger"));
verify(mockBlobServiceClient, times(1)).getBlobContainerClient(anyString());
}
}
[
{
"data": {
"validationCode": "C12F266E-79D9-4C0A-9922-5EF6201A34C2",
"validationUrl": "https://rp-centralindia.eventgrid.azure.net:553/eventsubscriptions/usagedetailpublisherfunction-subscription/validate?idu003dC12F266E-79D9-4C0A-9922-5EF6201A34C2u0026tu003d2021-10-26T08:10:52.4999377Zu0026apiVersionu003d2020-10-15-previewu0026tokenu003d30kEVoL8rAOWzQv0buurhrKnbP%2bGMtHObbA%2bax6wb4Y%3d"
},
"dataVersion": "2",
"eventTime": "2021-10-26T08:10:52.4999377Z",
"eventType": "Microsoft.EventGrid.SubscriptionValidationEvent",
"id": "e2a8466b-3dc0-46b7-bb7d-b999e51a2848",
"metadataVersion": "1",
"subject": "",
"topic": "/subscriptions/0fef643d-a6b1-48f9-a256-53fbd0d22f48/resourceGroups/resource-group-ccp/providers/Microsoft.EventGrid/domains/event-grid-domains-ccp/topics/usagedetailpublisherfunction-topic"
}
]
[
{
"data": {
"dataFileName": "d8284456-dfff-4bd4-9cef-ea99f70f4835/input.json",
"dataLocation": "callusageapp"
},
"dataVersion": "v1.0",
"eventTime": "2021-10-25T19:17:15.7468501Z",
"eventType": "UsageDetail",
"id": "d8284456-dfff-4bd4-9cef-ea99f70f4835",
"metadataVersion": "1",
"subject": "UsageDetailPublisher",
"topic": "/subscriptions/0fef643d-a6b1-48f9-a256-ea99f70f4835/resourceGroups/resource-group-ccp/providers/Microsoft.EventGrid/domains/event-grid-domains-ccp/topics/usagecostprocessorfunction-topic"
}
]
@startuml
class UsageDetailPublisherFunction [[java:com.iluwatar.producer.calldetails.functions]] {
-messageHandlerUtility: MessageHandlerUtility<UsageDetail>
-eventHandlerUtility: EventHandlerUtility<MessageHeader>
+run(): HttpResponseMessage
}
class UsageCostProcessorFunction [[java:com.iluwatar.consumer.callcostprocessor.functions]] {
-messageHandlerUtilityForUsageDetail: MessageHandlerUtility<UsageDetail>
-messageHandlerUtilityForUsageCostDetail: MessageHandlerUtility<UsageCostDetail>
+run(): HttpResponseMessage
}
class "MessageHandlerUtility<T>" as MessageHandlerUtility_T [[java:com.iluwatar.claimcheckpattern.utility]] {
+readFromPersistantStorage(messageReference: MessageReference, logger: Logger): Message
+dropToPersistantStorage(message: Message, logger: Logger): void
}
class "EventHandlerUtility<T>" as EventHandlerUtility_T [[java:com.callusage.utility.PersistentLocalStorageUtility]] {
+publishEvent(customEvent: T, logger: Logger): void
}
class "Message<T>" as Message_T [[java:com.iluwatar.claimcheckpattern.domain]] {
-messageHeader: MessageHeader
-messageData: MessageData<T>
+Message(messageHeader: MessageHeader, messageData: MessageData<T>)
+getMessageData(): MessageData<T>
+getMessageHeader(): MessageHeader
}
class MessageHeader [[java:com.iluwatar.claimcheckpattern.domain]] {
-id: String
-subject: String
-topic: String
-eventType: String
-eventTime: String
-data: Object
-dataVersion: String
+getId(): String
+setId(id: String): void
+getSubject(): String
+setSubject(subject: String): void
+getTopic(): String
+setTopic(topic: String): void
+getEventType(): String
+setEventType(eventType: String): void
+getEventTime(): String
+setEventTime(eventTime: String): void
+getData(): Object
+setData(data: Object): void
+getDataVersion(): String
+setDataVersion(dataVersion:String): void
}
class "MessageBody<T>" as MessageBody_T [[java:com.iluwatar.claimcheckpattern.domain]] {
-data: List[] T
+getData(): List[] T
+setData(data:List[] T): void
}
class MessageReference [[java:com.iluwatar.claimcheckpattern.domain]] {
-dataLocation: String
-dataFileName: String
+getDataLocation(): String
+setDataLocation(dataLocation:String): void
+getDataFileName(): String
+setDataFileName(dataFileName:String): void
}
class UsageDetail [[java:com.iluwatar.claimcheckpattern.domain]] {
-userId: String
-duration: int
-data: int
+getUserId(): String
+setUserId(userId: String): void
+getDuration(): long
+setDuration(duration: long): void
+getData(): long
+setData(data: long): void
}
class UsageCostDetail [[java:com.iluwatar.claimcheckpattern.domain]] {
-userId: String
-callCost: double
-dataCost: double
+getUserId(): String
+setUserId(userId: String): void
+getCallCost(): double
+setCallCost(callCost:double): void
+getDataCost(): double
+setDataCost(dataCost:double) : void
}
Message_T "1" *-- "1" MessageHeader : has
Message_T "1" *-- "1" MessageBody_T : has
MessageHeader "1" *-- "1" MessageReference : has as data object
MessageBody_T "1" *-- "1" UsageDetail: has
MessageBody_T "1" *-- "1" UsageCostDetail: has
EventHandlerUtility_T "1" *-- "1" MessageHeader: has
MessageHandlerUtility_T "1" *-- "1" Message_T: has
UsageDetailPublisherFunction "1" *-- "1" MessageHandlerUtility_T : has
UsageDetailPublisherFunction "1" *-- "1" EventHandlerUtility_T : has
UsageCostProcessorFunction "1" *-- "1" MessageHandlerUtility_T : has
UsageCostProcessorFunction "1" *-- "1" MessageHandlerUtility_T : has
@enduml
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright © 2014-2021 Ilkka Seppälä
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.25.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>claim-check-pattern</artifactId>
<packaging>pom</packaging>
<modules>
<module>call-usage-app</module>
</modules>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -36,9 +36,6 @@ public class Wizard {
private final Deque<Runnable> undoStack = new LinkedList<>();
private final Deque<Runnable> redoStack = new LinkedList<>();
public Wizard() {
}
/**
* Cast spell.
*/
......
---
layout: pattern
title: Composite View
folder: composite-view
permalink: /patterns/composite-view/
categories: Structural
language: en
tags:
- Enterprise Integration Pattern
- Presentation
---
## Name
**Composite View**
## Intent
The purpose of the Composite View Pattern is to increase re-usability and flexibility when creating views for websites/webapps.
This pattern seeks to decouple the content of the page from its layout, allowing changes to be made to either the content
or layout of the page without impacting the other. This pattern also allows content to be easily reused across different views easily.
## Explanation
Real World Example
> A news site wants to display the current date and news to different users
> based on that user's preferences. The news site will substitute in different news feed
> components depending on the user's interest, defaulting to local news.
In Plain Words
> Composite View Pattern is having a main view being composed of smaller subviews.
> The layout of this composite view is based on a template. A View-manager then decides which
> subviews to include in this template.
Wikipedia Says
> Composite views that are composed of multiple atomic subviews. Each component of
> the template may be included dynamically into the whole and the layout of the page may be managed independently of the content.
> This solution provides for the creation of a composite view based on the inclusion and substitution of
> modular dynamic and static template fragments.
> It promotes the reuse of atomic portions of the view by encouraging modular design.
**Programmatic Example**
Since this is a web development pattern, a server is required to demonstrate it.
This example uses Tomcat 10.0.13 to run the servlet, and this programmatic example will only work with Tomcat 10+.
Firstly there is `AppServlet` which is an `HttpServlet` that runs on Tomcat 10+.
```java
public class AppServlet extends HttpServlet {
private String msgPartOne = "<h1>This Server Doesn't Support";
private String msgPartTwo = "Requests</h1>\n"
+ "<h2>Use a GET request with boolean values for the following parameters<h2>\n"
+ "<h3>'name'</h3>\n<h3>'bus'</h3>\n<h3>'sports'</h3>\n<h3>'sci'</h3>\n<h3>'world'</h3>";
private String destination = "newsDisplay.jsp";
public AppServlet() {
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
RequestDispatcher requestDispatcher = req.getRequestDispatcher(destination);
ClientPropertiesBean reqParams = new ClientPropertiesBean(req);
req.setAttribute("properties", reqParams);
requestDispatcher.forward(req, resp);
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println(msgPartOne + " Post " + msgPartTwo);
}
@Override
public void doDelete(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println(msgPartOne + " Delete " + msgPartTwo);
}
@Override
public void doPut(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println(msgPartOne + " Put " + msgPartTwo);
}
}
```
This servlet is not part of the pattern, and simply forwards GET requests to the correct JSP.
PUT, POST, and DELETE requests are not supported and will simply show an error message.
The view management in this example is done via a javabean class: `ClientPropertiesBean`, which stores user preferences.
```java
public class ClientPropertiesBean implements Serializable {
private static final String WORLD_PARAM = "world";
private static final String SCIENCE_PARAM = "sci";
private static final String SPORTS_PARAM = "sport";
private static final String BUSINESS_PARAM = "bus";
private static final String NAME_PARAM = "name";
private static final String DEFAULT_NAME = "DEFAULT_NAME";
private boolean worldNewsInterest;
private boolean sportsInterest;
private boolean businessInterest;
private boolean scienceNewsInterest;
private String name;
public ClientPropertiesBean() {
worldNewsInterest = true;
sportsInterest = true;
businessInterest = true;
scienceNewsInterest = true;
name = DEFAULT_NAME;
}
public ClientPropertiesBean(HttpServletRequest req) {
worldNewsInterest = Boolean.parseBoolean(req.getParameter(WORLD_PARAM));
sportsInterest = Boolean.parseBoolean(req.getParameter(SPORTS_PARAM));
businessInterest = Boolean.parseBoolean(req.getParameter(BUSINESS_PARAM));
scienceNewsInterest = Boolean.parseBoolean(req.getParameter(SCIENCE_PARAM));
String tempName = req.getParameter(NAME_PARAM);
if (tempName == null || tempName == "") {
tempName = DEFAULT_NAME;
}
name = tempName;
}
// getters and setters generated by Lombok
}
```
This javabean has a default constructor, and another that takes an `HttpServletRequest`.
This second constructor takes the request object, parses out the request parameters which contain the
user preferences for different types of news.
The template for the news page is in `newsDisplay.jsp`
```html
<html>
<head>
<style>
h1 { text-align: center;}
h2 { text-align: center;}
h3 { text-align: center;}
.centerTable {
margin-left: auto;
margin-right: auto;
}
table {border: 1px solid black;}
tr {text-align: center;}
td {text-align: center;}
</style>
</head>
<body>
<%ClientPropertiesBean propertiesBean = (ClientPropertiesBean) request.getAttribute("properties");%>
<h1>Welcome <%= propertiesBean.getName()%></h1>
<jsp:include page="header.jsp"></jsp:include>
<table class="centerTable">
<tr>
<td></td>
<% if(propertiesBean.isWorldNewsInterest()) { %>
<td><%@include file="worldNews.jsp"%></td>
<% } else { %>
<td><%@include file="localNews.jsp"%></td>
<% } %>
<td></td>
</tr>
<tr>
<% if(propertiesBean.isBusinessInterest()) { %>
<td><%@include file="businessNews.jsp"%></td>
<% } else { %>
<td><%@include file="localNews.jsp"%></td>
<% } %>
<td></td>
<% if(propertiesBean.isSportsInterest()) { %>
<td><%@include file="sportsNews.jsp"%></td>
<% } else { %>
<td><%@include file="localNews.jsp"%></td>
<% } %>
</tr>
<tr>
<td></td>
<% if(propertiesBean.isScienceNewsInterest()) { %>
<td><%@include file="scienceNews.jsp"%></td>
<% } else { %>
<td><%@include file="localNews.jsp"%></td>
<% } %>
<td></td>
</tr>
</table>
</body>
</html>
```
This JSP page is the template. It declares a table with three rows, with one component in the first row,
two components in the second row, and one component in the third row.
The scriplets in the file are part of the
view management strategy that include different atomic subviews based on the user preferences in the Javabean.
Here are two examples of the mock atomic subviews used in the composite:
`businessNews.jsp`
```html
<html>
<head>
<style>
h2 { text-align: center;}
table {border: 1px solid black;}
tr {text-align: center;}
td {text-align: center;}
</style>
</head>
<body>
<h2>
Generic Business News
</h2>
<table style="margin-right: auto; margin-left: auto">
<tr>
<td>Stock prices up across the world</td>
<td>New tech companies to invest in</td>
</tr>
<tr>
<td>Industry leaders unveil new project</td>
<td>Price fluctuations and what they mean</td>
</tr>
</table>
</body>
</html>
```
`localNews.jsp`
```html
<html>
<body>
<div style="text-align: center">
<h3>
Generic Local News
</h3>
<ul style="list-style-type: none">
<li>
Mayoral elections coming up in 2 weeks
</li>
<li>
New parking meter rates downtown coming tomorrow
</li>
<li>
Park renovations to finish by the next year
</li>
<li>
Annual marathon sign ups available online
</li>
</ul>
</div>
</body>
</html>
```
The results are as such:
1) The user has put their name as `Tammy` in the request parameters and no preferences:
![alt text](etc/images/noparam.PNG)
2) The user has put their name as `Johnny` in the request parameters and has a preference for world, business, and science news:
![alt text](etc/images/threeparams.PNG)
The different subviews such as `worldNews.jsp`, `businessNews.jsp`, etc. are included conditionally
based on the request parameters.
**How To Use**
To try this example, make sure you have Tomcat 10+ installed.
Set up your IDE to build a WAR file from the module and deploy that file to the server
IntelliJ:
Under `Run` and `edit configurations` Make sure Tomcat server is one of the run configurations.
Go to the deployment tab, and make sure there is one artifact being built called `composite-view:war exploded`.
If not present, add one.
Ensure that the artifact is being built from the content of the `web` directory and the compilation results of the module.
Point the output of the artifact to a convenient place. Run the configuration and view the landing page,
follow instructions on that page to continue.
## Class diagram
![alt text](./etc/composite_view.png)
The class diagram here displays the Javabean which is the view manager.
The views are JSP's held inside the web directory.
## Applicability
This pattern is applicable to most websites that require content to be displayed dynamically/conditionally.
If there are components that need to be re-used for multiple views, or if the project requires reusing a template,
or if it needs to include content depending on certain conditions, then this pattern is a good choice.
## Known uses
Most modern websites use composite views in some shape or form, as they have templates for views and small atomic components
that are included in the page dynamically. Most modern Javascript libraries, like React, support this design pattern
with components.
## Consequences
**Pros**
* Easy to re-use components
* Change layout/content without affecting the other
* Reduce code duplication
* Code is more maintainable and modular
**Cons**
* Overhead cost at runtime
* Slower response compared to directly embedding elements
* Increases potential for display errors
## Related patterns
* [Composite (GoF)](https://java-design-patterns.com/patterns/composite/)
* [View Helper](https://www.oracle.com/java/technologies/viewhelper.html)
## Credits
* [Core J2EE Patterns - Composite View](https://www.oracle.com/java/technologies/composite-view.html)
* [Composite View Design Pattern – Core J2EE Patterns](https://www.dineshonjava.com/composite-view-design-pattern/)
@startuml
package com.iluwatar.compositeview {
class ClientPropertiesBean {
- BUSINESS_PARAM : String {static}
- DEFAULT_NAME : String {static}
- NAME_PARAM : String {static}
- SCIENCE_PARAM : String {static}
- SPORTS_PARAM : String {static}
- WORLD_PARAM : String {static}
- businessInterest : boolean
- name : String
- scienceNewsInterest : boolean
- sportsInterest : boolean
- worldNewsInterest : boolean
+ ClientPropertiesBean()
+ ClientPropertiesBean(req : HttpServletRequest)
+ getName() : String
+ isBusinessInterest() : boolean
+ isScienceNewsInterest() : boolean
+ isSportsInterest() : boolean
+ isWorldNewsInterest() : boolean
+ setBusinessInterest(businessInterest : boolean)
+ setName(name : String)
+ setScienceNewsInterest(scienceNewsInterest : boolean)
+ setSportsInterest(sportsInterest : boolean)
+ setWorldNewsInterest(worldNewsInterest : boolean)
}
}
@enduml
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright © 2014-2021 Ilkka Seppälä
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.26.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>composite-view</artifactId>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<configuration>
<archive>
<manifest>
<mainClass>com.iluwatar.compositeview.App</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package com.iluwatar.compositeview;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* A servlet object that extends HttpServlet.
* Runs on Tomcat 10 and handles Http requests
*/
public final class AppServlet extends HttpServlet {
private String msgPartOne = "<h1>This Server Doesn't Support";
private String msgPartTwo = "Requests</h1>\n"
+ "<h2>Use a GET request with boolean values for the following parameters<h2>\n"
+ "<h3>'name'</h3>\n<h3>'bus'</h3>\n<h3>'sports'</h3>\n<h3>'sci'</h3>\n<h3>'world'</h3>";
private String destination = "newsDisplay.jsp";
public AppServlet() {
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
RequestDispatcher requestDispatcher = req.getRequestDispatcher(destination);
ClientPropertiesBean reqParams = new ClientPropertiesBean(req);
req.setAttribute("properties", reqParams);
requestDispatcher.forward(req, resp);
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
try (PrintWriter out = resp.getWriter()) {
out.println(msgPartOne + " Post " + msgPartTwo);
}
}
@Override
public void doDelete(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
try (PrintWriter out = resp.getWriter()) {
out.println(msgPartOne + " Delete " + msgPartTwo);
}
}
@Override
public void doPut(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
try (PrintWriter out = resp.getWriter()) {
out.println(msgPartOne + " Put " + msgPartTwo);
}
}
}
package com.iluwatar.compositeview;
import jakarta.servlet.http.HttpServletRequest;
import java.io.Serializable;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
/**
* A Java beans class that parses a http request and stores parameters.
* Java beans used in JSP's to dynamically include elements in view.
* DEFAULT_NAME = a constant, default name to be used for the default constructor
* worldNewsInterest = whether current request has world news interest
* sportsInterest = whether current request has a sportsInterest
* businessInterest = whether current request has a businessInterest
* scienceNewsInterest = whether current request has a scienceNewsInterest
*/
@Getter
@Setter
@NoArgsConstructor
public class ClientPropertiesBean implements Serializable {
private static final String WORLD_PARAM = "world";
private static final String SCIENCE_PARAM = "sci";
private static final String SPORTS_PARAM = "sport";
private static final String BUSINESS_PARAM = "bus";
private static final String NAME_PARAM = "name";
private static final String DEFAULT_NAME = "DEFAULT_NAME";
private boolean worldNewsInterest = true;
private boolean sportsInterest = true;
private boolean businessInterest = true;
private boolean scienceNewsInterest = true;
private String name = DEFAULT_NAME;
/**
* Constructor that parses an HttpServletRequest and stores all the request parameters.
*
* @param req the HttpServletRequest object that is passed in
*/
public ClientPropertiesBean(HttpServletRequest req) {
worldNewsInterest = Boolean.parseBoolean(req.getParameter(WORLD_PARAM));
sportsInterest = Boolean.parseBoolean(req.getParameter(SPORTS_PARAM));
businessInterest = Boolean.parseBoolean(req.getParameter(BUSINESS_PARAM));
scienceNewsInterest = Boolean.parseBoolean(req.getParameter(SCIENCE_PARAM));
String tempName = req.getParameter(NAME_PARAM);
if (tempName == null || tempName.equals("")) {
tempName = DEFAULT_NAME;
}
name = tempName;
}
}
package com.iluwatar.compositeview;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.io.PrintWriter;
import java.io.StringWriter;
import static org.junit.Assert.*;
/* Written with reference from https://stackoverflow.com/questions/5434419/how-to-test-my-servlet-using-junit
and https://stackoverflow.com/questions/50211433/servlets-unit-testing
*/
public class AppServletTest extends Mockito{
private String msgPartOne = "<h1>This Server Doesn't Support";
private String msgPartTwo = "Requests</h1>\n"
+ "<h2>Use a GET request with boolean values for the following parameters<h2>\n"
+ "<h3>'name'</h3>\n<h3>'bus'</h3>\n<h3>'sports'</h3>\n<h3>'sci'</h3>\n<h3>'world'</h3>";
private String destination = "newsDisplay.jsp";
@Test
public void testDoGet() throws Exception {
HttpServletRequest mockReq = Mockito.mock(HttpServletRequest.class);
HttpServletResponse mockResp = Mockito.mock(HttpServletResponse.class);
RequestDispatcher mockDispatcher = Mockito.mock(RequestDispatcher.class);
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
when(mockResp.getWriter()).thenReturn(printWriter);
when(mockReq.getRequestDispatcher(destination)).thenReturn(mockDispatcher);
AppServlet curServlet = new AppServlet();
curServlet.doGet(mockReq, mockResp);
verify(mockReq, times(1)).getRequestDispatcher(destination);
verify(mockDispatcher).forward(mockReq, mockResp);
}
@Test
public void testDoPost() throws Exception {
HttpServletRequest mockReq = Mockito.mock(HttpServletRequest.class);
HttpServletResponse mockResp = Mockito.mock(HttpServletResponse.class);
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
when(mockResp.getWriter()).thenReturn(printWriter);
AppServlet curServlet = new AppServlet();
curServlet.doPost(mockReq, mockResp);
printWriter.flush();
assertTrue(stringWriter.toString().contains(msgPartOne + " Post " + msgPartTwo));
}
@Test
public void testDoPut() throws Exception {
HttpServletRequest mockReq = Mockito.mock(HttpServletRequest.class);
HttpServletResponse mockResp = Mockito.mock(HttpServletResponse.class);
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
when(mockResp.getWriter()).thenReturn(printWriter);
AppServlet curServlet = new AppServlet();
curServlet.doPut(mockReq, mockResp);
printWriter.flush();
assertTrue(stringWriter.toString().contains(msgPartOne + " Put " + msgPartTwo));
}
@Test
public void testDoDelete() throws Exception {
HttpServletRequest mockReq = Mockito.mock(HttpServletRequest.class);
HttpServletResponse mockResp = Mockito.mock(HttpServletResponse.class);
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
when(mockResp.getWriter()).thenReturn(printWriter);
AppServlet curServlet = new AppServlet();
curServlet.doDelete(mockReq, mockResp);
printWriter.flush();
assertTrue(stringWriter.toString().contains(msgPartOne + " Delete " + msgPartTwo));
}
}
package com.iluwatar.compositeview;
import jakarta.servlet.http.HttpServletRequest;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import static org.junit.Assert.*;
public class JavaBeansTest {
@Test
public void testDefaultConstructor() {
ClientPropertiesBean newBean = new ClientPropertiesBean();
assertEquals("DEFAULT_NAME", newBean.getName());
assertTrue(newBean.isBusinessInterest());
assertTrue(newBean.isScienceNewsInterest());
assertTrue(newBean.isSportsInterest());
assertTrue(newBean.isWorldNewsInterest());
}
@Test
public void testNameGetterSetter() {
ClientPropertiesBean newBean = new ClientPropertiesBean();
assertEquals("DEFAULT_NAME", newBean.getName());
newBean.setName("TEST_NAME_ONE");
assertEquals("TEST_NAME_ONE", newBean.getName());
}
@Test
public void testBusinessSetterGetter() {
ClientPropertiesBean newBean = new ClientPropertiesBean();
assertTrue(newBean.isBusinessInterest());
newBean.setBusinessInterest(false);
assertFalse(newBean.isBusinessInterest());
}
@Test
public void testScienceSetterGetter() {
ClientPropertiesBean newBean = new ClientPropertiesBean();
assertTrue(newBean.isScienceNewsInterest());
newBean.setScienceNewsInterest(false);
assertFalse(newBean.isScienceNewsInterest());
}
@Test
public void testSportsSetterGetter() {
ClientPropertiesBean newBean = new ClientPropertiesBean();
assertTrue(newBean.isSportsInterest());
newBean.setSportsInterest(false);
assertFalse(newBean.isSportsInterest());
}
@Test
public void testWorldSetterGetter() {
ClientPropertiesBean newBean = new ClientPropertiesBean();
assertTrue(newBean.isWorldNewsInterest());
newBean.setWorldNewsInterest(false);
assertFalse(newBean.isWorldNewsInterest());
}
@Test
public void testRequestConstructor(){
HttpServletRequest mockReq = Mockito.mock(HttpServletRequest.class);
ClientPropertiesBean newBean = new ClientPropertiesBean((mockReq));
assertEquals("DEFAULT_NAME", newBean.getName());
assertFalse(newBean.isWorldNewsInterest());
assertFalse(newBean.isBusinessInterest());
assertFalse(newBean.isScienceNewsInterest());
assertFalse(newBean.isSportsInterest());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>com.iluwatar.compositeview.AppServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/news</url-pattern>
</servlet-mapping>
</web-app>
\ No newline at end of file
<%--
Created by IntelliJ IDEA.
User: Kevin
Date: 11/29/2021
Time: 2:51 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<style>
h2 { text-align: center;}
table {border: 1px solid black;}
tr {text-align: center;}
td {text-align: center;}
</style>
</head>
<body>
<h2>
Generic Business News
</h2>
<table style="margin-right: auto; margin-left: auto">
<tr>
<td>Stock prices up across the world</td>
<td>New tech companies to invest in</td>
</tr>
<tr>
<td>Industry leaders unveil new project</td>
<td>Price fluctuations and what they mean</td>
</tr>
</table>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: Kevin
Date: 11/29/2021
Time: 1:28 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.Date"%>
<html>
<head>
<style>
h1 { text-align: center;}
h2 { text-align: center;}
h3 { text-align: center;}
</style>
</head>
<body>
<% String todayDateStr = (new Date().toString()); %>
<h1>Today's Personalized Frontpage</h1>
<h2><%=todayDateStr%></h2>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<style>
h1 { text-align: center;}
h2 { text-align: center;}
h3 { text-align: center;}
</style>
</head>
<body>
<h1>Welcome To The Composite Patterns Mock News Site</h1>
<h2>Send a GET request to the "/news" path to see the composite view with mock news</h2>
<h2>Use the following parameters:</h2>
<h3>name: string name to be dynamically displayed</h3>
<h3>bus: boolean for whether you want to see the mock business news</h3>
<h3>world: boolean for whether you want to see the mock world news</h3>
<h3>sci: boolean for whether you want to see the mock world news</h3>
<h3>sport: boolean for whether you want to see the mock world news</h3>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<div style="text-align: center">
<h3>
Generic Local News
</h3>
<ul style="list-style-type: none">
<li>
Mayoral elections coming up in 2 weeks
</li>
<li>
New parking meter rates downtown coming tomorrow
</li>
<li>
Park renovations to finish by the next year
</li>
<li>
Annual marathon sign ups available online
</li>
</ul>
</div>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.iluwatar.compositeview.ClientPropertiesBean"%>
<html>
<head>
<style>
h1 { text-align: center;}
h2 { text-align: center;}
h3 { text-align: center;}
.centerTable {
margin-left: auto;
margin-right: auto;
}
table {border: 1px solid black;}
tr {text-align: center;}
td {text-align: center;}
</style>
</head>
<body>
<%ClientPropertiesBean propertiesBean = (ClientPropertiesBean) request.getAttribute("properties");%>
<h1>Welcome <%= propertiesBean.getName()%></h1>
<jsp:include page="header.jsp"></jsp:include>
<table class="centerTable">
<tr>
<td></td>
<% if(propertiesBean.isWorldNewsInterest()) { %>
<td><%@include file="worldNews.jsp"%></td>
<% } else { %>
<td><%@include file="localNews.jsp"%></td>
<% } %>
<td></td>
</tr>
<tr>
<% if(propertiesBean.isBusinessInterest()) { %>
<td><%@include file="businessNews.jsp"%></td>
<% } else { %>
<td><%@include file="localNews.jsp"%></td>
<% } %>
<td></td>
<% if(propertiesBean.isSportsInterest()) { %>
<td><%@include file="sportsNews.jsp"%></td>
<% } else { %>
<td><%@include file="localNews.jsp"%></td>
<% } %>
</tr>
<tr>
<td></td>
<% if(propertiesBean.isScienceNewsInterest()) { %>
<td><%@include file="scienceNews.jsp"%></td>
<% } else { %>
<td><%@include file="localNews.jsp"%></td>
<% } %>
<td></td>
</tr>
</table>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: Kevin
Date: 11/29/2021
Time: 4:18 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<div style="text-align: center">
<h3>
Generic Science News
</h3>
<ul>
<li>
New model of gravity proposed for dark matter
</li>
<li>
Genetic modifying technique proved on bacteria
</li>
<li>
Neurology study maps brain with new precision
</li>
<li>
Survey of rainforest discovers 15 new species
</li>
<li>
New signalling pathway for immune system discovered
</li>
</ul>
</div>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: Kevin
Date: 11/29/2021
Time: 3:53 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<style>
h2 { text-align: center;}
table {border: 1px solid black;}
tr {text-align: center;}
td {text-align: center;}
</style>
</head>
<body>
<h2>
Generic Sports News
</h2>
<div style="margin-left: auto; margin-right: auto; padding: 20px">
International football match delayed due to weather, will be held next week
</div>
<div style="margin-left: auto; margin-right: auto; padding: 20px">
New rising stars in winter sports, ten new athletes that will shake up the scene
</div>
<div style="margin-left: auto; margin-right: auto; padding: 20px">
Biggest upset in basketball history, upstart team sweeps competition
</div>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: Kevin
Date: 11/29/2021
Time: 2:51 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<style>
h2 { text-align: center;}
table {border: 1px solid black;}
tr {text-align: center;}
td {text-align: center;}
</style>
</head>
<body>
<h2>
Generic World News
</h2>
<table style="margin-right: auto; margin-left: auto">
<tr>
<td>New trade talks happening at UN on Thursday</td>
</tr>
<tr>
<td>European Union to announce new resolution next week</td>
</tr>
<tr>
<td>UN delivers report on world economic status</td>
</tr>
</table>
</body>
</html>
......@@ -9,6 +9,10 @@ tags:
- Reactive
---
## Name
Event Aggregator
## Intent
A system with lots of objects can lead to complexities when a
client wants to subscribe to events. The client has to find and register for
......@@ -17,6 +21,136 @@ requires a separate subscription. An Event Aggregator acts as a single source
of events for many objects. It registers for all the events of the many objects
allowing clients to register with just the aggregator.
## Explanation
Real-world example
> King Joffrey sits on the iron throne and rules the seven kingdoms of Westeros. He receives most
> of his critical information from King's Hand, the second in command. King's hand has many
> close advisors himself, feeding him with relevant information about events occurring in the
> kingdom.
In Plain Words
> Event Aggregator is an event mediator that collects events from multiple sources and delivers
> them to registered observers.
**Programmatic Example**
In our programmatic example, we demonstrate the implementation of an event aggregator pattern. Some of
the objects are event listeners, some are event emitters, and the event aggregator does both.
```java
public interface EventObserver {
void onEvent(Event e);
}
public abstract class EventEmitter {
private final Map<Event, List<EventObserver>> observerLists;
public EventEmitter() {
observerLists = new HashMap<>();
}
public final void registerObserver(EventObserver obs, Event e) {
...
}
protected void notifyObservers(Event e) {
...
}
}
```
`KingJoffrey` is listening to events from `KingsHand`.
```java
@Slf4j
public class KingJoffrey implements EventObserver {
@Override
public void onEvent(Event e) {
LOGGER.info("Received event from the King's Hand: {}", e.toString());
}
}
```
`KingsHand` is listening to events from his subordinates `LordBaelish`, `LordVarys`, and `Scout`.
Whatever he hears from them, he delivers to `KingJoffrey`.
```java
public class KingsHand extends EventEmitter implements EventObserver {
public KingsHand() {
}
public KingsHand(EventObserver obs, Event e) {
super(obs, e);
}
@Override
public void onEvent(Event e) {
notifyObservers(e);
}
}
```
For example, `LordVarys` finds a traitor every Sunday and notifies the `KingsHand`.
```java
@Slf4j
public class LordVarys extends EventEmitter implements EventObserver {
@Override
public void timePasses(Weekday day) {
if (day == Weekday.SATURDAY) {
notifyObservers(Event.TRAITOR_DETECTED);
}
}
}
```
The following snippet demonstrates how the objects are constructed and wired together.
```java
var kingJoffrey = new KingJoffrey();
var kingsHand = new KingsHand();
kingsHand.registerObserver(kingJoffrey, Event.TRAITOR_DETECTED);
kingsHand.registerObserver(kingJoffrey, Event.STARK_SIGHTED);
kingsHand.registerObserver(kingJoffrey, Event.WARSHIPS_APPROACHING);
kingsHand.registerObserver(kingJoffrey, Event.WHITE_WALKERS_SIGHTED);
var varys = new LordVarys();
varys.registerObserver(kingsHand, Event.TRAITOR_DETECTED);
varys.registerObserver(kingsHand, Event.WHITE_WALKERS_SIGHTED);
var scout = new Scout();
scout.registerObserver(kingsHand, Event.WARSHIPS_APPROACHING);
scout.registerObserver(varys, Event.WHITE_WALKERS_SIGHTED);
var baelish = new LordBaelish(kingsHand, Event.STARK_SIGHTED);
var emitters = List.of(
kingsHand,
baelish,
varys,
scout
);
Arrays.stream(Weekday.values())
.<Consumer<? super EventEmitter>>map(day -> emitter -> emitter.timePasses(day))
.forEachOrdered(emitters::forEach);
```
The console output after running the example.
```
18:21:52.955 [main] INFO com.iluwatar.event.aggregator.KingJoffrey - Received event from the King's Hand: Warships approaching
18:21:52.960 [main] INFO com.iluwatar.event.aggregator.KingJoffrey - Received event from the King's Hand: White walkers sighted
18:21:52.960 [main] INFO com.iluwatar.event.aggregator.KingJoffrey - Received event from the King's Hand: Stark sighted
18:21:52.960 [main] INFO com.iluwatar.event.aggregator.KingJoffrey - Received event from the King's Hand: Traitor detected
```
## Class diagram
![alt text](./etc/classes.png "Event Aggregator")
......@@ -26,9 +160,13 @@ Use the Event Aggregator pattern when
* Event Aggregator is a good choice when you have lots of objects that are
potential event sources. Rather than have the observer deal with registering
with them all, you can centralize the registration logic to the Event
Aggregator. As well as simplifying registration, a Event Aggregator also
Aggregator. As well as simplifying registration, an Event Aggregator also
simplifies the memory management issues in using observers.
## Related patterns
* [Observer](https://java-design-patterns.com/patterns/observer/)
## Credits
* [Martin Fowler - Event Aggregator](http://martinfowler.com/eaaDev/EventAggregator.html)
......@@ -49,13 +49,28 @@ public class App {
public static void main(String[] args) {
var kingJoffrey = new KingJoffrey();
var kingsHand = new KingsHand(kingJoffrey);
var kingsHand = new KingsHand();
kingsHand.registerObserver(kingJoffrey, Event.TRAITOR_DETECTED);
kingsHand.registerObserver(kingJoffrey, Event.STARK_SIGHTED);
kingsHand.registerObserver(kingJoffrey, Event.WARSHIPS_APPROACHING);
kingsHand.registerObserver(kingJoffrey, Event.WHITE_WALKERS_SIGHTED);
var varys = new LordVarys();
varys.registerObserver(kingsHand, Event.TRAITOR_DETECTED);
varys.registerObserver(kingsHand, Event.WHITE_WALKERS_SIGHTED);
var scout = new Scout();
scout.registerObserver(kingsHand, Event.WARSHIPS_APPROACHING);
scout.registerObserver(varys, Event.WHITE_WALKERS_SIGHTED);
var baelish = new LordBaelish(kingsHand, Event.STARK_SIGHTED);
var emitters = List.of(
kingsHand,
new LordBaelish(kingsHand),
new LordVarys(kingsHand),
new Scout(kingsHand)
baelish,
varys,
scout
);
Arrays.stream(Weekday.values())
......
......@@ -31,6 +31,7 @@ import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public enum Event {
WHITE_WALKERS_SIGHTED("White walkers sighted"),
STARK_SIGHTED("Stark sighted"),
WARSHIPS_APPROACHING("Warships approaching"),
TRAITOR_DETECTED("Traitor detected");
......
......@@ -23,31 +23,48 @@
package com.iluwatar.event.aggregator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* EventEmitter is the base class for event producers that can be observed.
*/
public abstract class EventEmitter {
private final List<EventObserver> observers;
private final Map<Event, List<EventObserver>> observerLists;
public EventEmitter() {
observers = new LinkedList<>();
observerLists = new HashMap<>();
}
public EventEmitter(EventObserver obs) {
public EventEmitter(EventObserver obs, Event e) {
this();
registerObserver(obs);
registerObserver(obs, e);
}
public final void registerObserver(EventObserver obs) {
observers.add(obs);
/**
* Registers observer for specific event in the related list.
*
* @param obs the observer that observers this emitter
* @param e the specific event for that observation occurs
* */
public final void registerObserver(EventObserver obs, Event e) {
if (!observerLists.containsKey(e)) {
observerLists.put(e, new LinkedList<>());
}
if (!observerLists.get(e).contains(obs)) {
observerLists.get(e).add(obs);
}
}
protected void notifyObservers(Event e) {
observers.forEach(obs -> obs.onEvent(e));
if (observerLists.containsKey(e)) {
observerLists
.get(e)
.forEach(observer -> observer.onEvent(e));
}
}
public abstract void timePasses(Weekday day);
......
......@@ -31,8 +31,8 @@ public class KingsHand extends EventEmitter implements EventObserver {
public KingsHand() {
}
public KingsHand(EventObserver obs) {
super(obs);
public KingsHand(EventObserver obs, Event e) {
super(obs, e);
}
@Override
......@@ -42,6 +42,5 @@ public class KingsHand extends EventEmitter implements EventObserver {
@Override
public void timePasses(Weekday day) {
// NOP
}
}
......@@ -31,8 +31,8 @@ public class LordBaelish extends EventEmitter {
public LordBaelish() {
}
public LordBaelish(EventObserver obs) {
super(obs);
public LordBaelish(EventObserver obs, Event e) {
super(obs, e);
}
@Override
......
......@@ -23,16 +23,19 @@
package com.iluwatar.event.aggregator;
import lombok.extern.slf4j.Slf4j;
/**
* LordVarys produces events.
*/
public class LordVarys extends EventEmitter {
@Slf4j
public class LordVarys extends EventEmitter implements EventObserver {
public LordVarys() {
}
public LordVarys(EventObserver obs) {
super(obs);
public LordVarys(EventObserver obs, Event e) {
super(obs, e);
}
@Override
......@@ -41,4 +44,10 @@ public class LordVarys extends EventEmitter {
notifyObservers(Event.TRAITOR_DETECTED);
}
}
@Override
public void onEvent(Event e) {
notifyObservers(e);
}
}
......@@ -31,8 +31,8 @@ public class Scout extends EventEmitter {
public Scout() {
}
public Scout(EventObserver obs) {
super(obs);
public Scout(EventObserver obs, Event e) {
super(obs, e);
}
@Override
......@@ -40,5 +40,8 @@ public class Scout extends EventEmitter {
if (day == Weekday.TUESDAY) {
notifyObservers(Event.WARSHIPS_APPROACHING);
}
if (day == Weekday.WEDNESDAY) {
notifyObservers(Event.WHITE_WALKERS_SIGHTED);
}
}
}
......@@ -31,6 +31,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import org.junit.jupiter.api.Test;
......@@ -46,7 +47,7 @@ abstract class EventEmitterTest<E extends EventEmitter> {
/**
* Factory used to create a new instance of the test object with a default observer
*/
private final Function<EventObserver, E> factoryWithDefaultObserver;
private final BiFunction<EventObserver, Event, E> factoryWithDefaultObserver;
/**
* Factory used to create a new instance of the test object without passing a default observer
......@@ -67,7 +68,7 @@ abstract class EventEmitterTest<E extends EventEmitter> {
* Create a new event emitter test, using the given test object factories, special day and event
*/
EventEmitterTest(final Weekday specialDay, final Event event,
final Function<EventObserver, E> factoryWithDefaultObserver,
final BiFunction<EventObserver, Event, E> factoryWithDefaultObserver,
final Supplier<E> factoryWithoutDefaultObserver) {
this.specialDay = specialDay;
......@@ -129,8 +130,8 @@ abstract class EventEmitterTest<E extends EventEmitter> {
final var observer2 = mock(EventObserver.class);
final var emitter = this.factoryWithoutDefaultObserver.get();
emitter.registerObserver(observer1);
emitter.registerObserver(observer2);
emitter.registerObserver(observer1, event);
emitter.registerObserver(observer2, event);
testAllDays(specialDay, event, emitter, observer1, observer2);
}
......@@ -146,9 +147,9 @@ abstract class EventEmitterTest<E extends EventEmitter> {
final var observer1 = mock(EventObserver.class);
final var observer2 = mock(EventObserver.class);
final var emitter = this.factoryWithDefaultObserver.apply(defaultObserver);
emitter.registerObserver(observer1);
emitter.registerObserver(observer2);
final var emitter = this.factoryWithDefaultObserver.apply(defaultObserver, event);
emitter.registerObserver(observer1, event);
emitter.registerObserver(observer2, event);
testAllDays(specialDay, event, emitter, defaultObserver, observer1, observer2);
}
......
......@@ -55,7 +55,11 @@ class KingsHandTest extends EventEmitterTest<KingsHand> {
@Test
void testPassThrough() throws Exception {
final var observer = mock(EventObserver.class);
final var kingsHand = new KingsHand(observer);
final var kingsHand = new KingsHand();
kingsHand.registerObserver(observer, Event.STARK_SIGHTED);
kingsHand.registerObserver(observer, Event.WARSHIPS_APPROACHING);
kingsHand.registerObserver(observer, Event.TRAITOR_DETECTED);
kingsHand.registerObserver(observer, Event.WHITE_WALKERS_SIGHTED);
// The kings hand should not pass any events before he received one
verifyZeroInteractions(observer);
......
......@@ -34,7 +34,9 @@ class ScoutTest extends EventEmitterTest<Scout> {
* Create a new test instance, using the correct object factory
*/
public ScoutTest() {
super(Weekday.TUESDAY, Event.WARSHIPS_APPROACHING, Scout::new, Scout::new);
super(Weekday.TUESDAY, Event.WARSHIPS_APPROACHING, Scout::new, Scout::new);
}
}
\ No newline at end of file
......@@ -17,10 +17,10 @@ the user to specify only what to do with the resource.
## Explanation
Real world example
Real-world example
> We need to provide a class that can be used to write text strings to files. To make it easy for
> the user we let our service class open and close the file automatically, the user only has to
> A class needs to be provided for writing text strings to files. To make it easy for
> the user, the service class opens and closes the file automatically. The user only has to
> specify what is written into which file.
In plain words
......@@ -35,35 +35,50 @@ In plain words
**Programmatic Example**
Let's introduce our file writer class.
`SimpleFileWriter` class implements the Execute Around idiom. It takes `FileWriterAction` as a
constructor argument allowing the user to specify what gets written into the file.
```java
@FunctionalInterface
public interface FileWriterAction {
void writeFile(FileWriter writer) throws IOException;
}
@Slf4j
public class SimpleFileWriter {
public SimpleFileWriter(String filename, FileWriterAction action) throws IOException {
try (var writer = new FileWriter(filename)) {
action.writeFile(writer);
public SimpleFileWriter(String filename, FileWriterAction action) throws IOException {
LOGGER.info("Opening the file");
try (var writer = new FileWriter(filename)) {
LOGGER.info("Executing the action");
action.writeFile(writer);
LOGGER.info("Closing the file");
}
}
}
}
```
To utilize the file writer the following code is needed.
The following code demonstrates how `SimpleFileWriter` is used. `Scanner` is used to print the file
contents after the writing finishes.
```java
FileWriterAction writeHello = writer -> {
writer.write("Hello");
writer.append(" ");
writer.append("there!");
};
new SimpleFileWriter("testfile.txt", writeHello);
FileWriterAction writeHello = writer -> {
writer.write("Gandalf was here");
};
new SimpleFileWriter("testfile.txt", writeHello);
var scanner = new Scanner(new File("testfile.txt"));
while (scanner.hasNextLine()) {
LOGGER.info(scanner.nextLine());
}
```
Here's the console output.
```
21:18:07.185 [main] INFO com.iluwatar.execute.around.SimpleFileWriter - Opening the file
21:18:07.188 [main] INFO com.iluwatar.execute.around.SimpleFileWriter - Executing the action
21:18:07.189 [main] INFO com.iluwatar.execute.around.SimpleFileWriter - Closing the file
21:18:07.199 [main] INFO com.iluwatar.execute.around.App - Gandalf was here
```
## Class diagram
......@@ -74,8 +89,7 @@ To utilize the file writer the following code is needed.
Use the Execute Around idiom when
* You use an API that requires methods to be called in pairs such as open/close or
allocate/deallocate.
* An API requires methods to be called in pairs such as open/close or allocate/deallocate.
## Credits
......
......@@ -23,10 +23,14 @@
package com.iluwatar.execute.around;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import lombok.extern.slf4j.Slf4j;
/**
* The Execute Around idiom specifies some code to be executed before and after a method. Typically
* The Execute Around idiom specifies executable code before and after a method. Typically
* the idiom is used when the API has methods to be executed in pairs, such as resource
* allocation/deallocation or lock acquisition/release.
*
......@@ -34,6 +38,7 @@ import java.io.IOException;
* the user. The user specifies only what to do with the file by providing the {@link
* FileWriterAction} implementation.
*/
@Slf4j
public class App {
/**
......@@ -41,11 +46,16 @@ public class App {
*/
public static void main(String[] args) throws IOException {
// create the file writer and execute the custom action
FileWriterAction writeHello = writer -> {
writer.write("Hello");
writer.append(" ");
writer.append("there!");
writer.write("Gandalf was here");
};
new SimpleFileWriter("testfile.txt", writeHello);
// print the file contents
var scanner = new Scanner(new File("testfile.txt"));
while (scanner.hasNextLine()) {
LOGGER.info(scanner.nextLine());
}
}
}
......@@ -26,18 +26,24 @@ package com.iluwatar.execute.around;
import java.io.FileWriter;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
/**
* SimpleFileWriter handles opening and closing file for the user. The user only has to specify what
* to do with the file resource through {@link FileWriterAction} parameter.
*/
@Slf4j
public class SimpleFileWriter {
/**
* Constructor.
*/
public SimpleFileWriter(String filename, FileWriterAction action) throws IOException {
LOGGER.info("Opening the file");
try (var writer = new FileWriter(filename)) {
LOGGER.info("Executing the action");
action.writeFile(writer);
LOGGER.info("Closing the file");
}
}
}
......@@ -10,19 +10,115 @@ tags:
---
## Intent
Define a factory of immutable content with separated builder and factory interfaces.
## Explanation
Real-world example
> Imagine a magical weapon factory that can create any type of weapon wished for. When the factory
> is unboxed, the master recites the weapon types needed to prepare it. After that, any of those
> weapon types can be summoned in an instant.
In plain words
> Factory kit is a configurable object builder.
**Programmatic Example**
Let's first define the simple `Weapon` hierarchy.
```java
public interface Weapon {
}
public enum WeaponType {
SWORD,
AXE,
BOW,
SPEAR
}
public class Sword implements Weapon {
@Override
public String toString() {
return "Sword";
}
}
// Axe, Bow, and Spear are defined similarly
```
Next, we define a functional interface that allows adding a builder with a name to the factory.
```java
public interface Builder {
void add(WeaponType name, Supplier<Weapon> supplier);
}
```
The meat of the example is the `WeaponFactory` interface that effectively implements the factory
kit pattern. The method `#factory` is used to configure the factory with the classes it needs to
be able to construct. The method `#create` is then used to create object instances.
```java
public interface WeaponFactory {
static WeaponFactory factory(Consumer<Builder> consumer) {
var map = new HashMap<WeaponType, Supplier<Weapon>>();
consumer.accept(map::put);
return name -> map.get(name).get();
}
Weapon create(WeaponType name);
}
```
Now, we can show how `WeaponFactory` can be used.
```java
var factory = WeaponFactory.factory(builder -> {
builder.add(WeaponType.SWORD, Sword::new);
builder.add(WeaponType.AXE, Axe::new);
builder.add(WeaponType.SPEAR, Spear::new);
builder.add(WeaponType.BOW, Bow::new);
});
var list = new ArrayList<Weapon>();
list.add(factory.create(WeaponType.AXE));
list.add(factory.create(WeaponType.SPEAR));
list.add(factory.create(WeaponType.SWORD));
list.add(factory.create(WeaponType.BOW));
list.stream().forEach(weapon -> LOGGER.info("{}", weapon.toString()));
```
Here is the console output when the example is run.
```
21:15:49.709 [main] INFO com.iluwatar.factorykit.App - Axe
21:15:49.713 [main] INFO com.iluwatar.factorykit.App - Spear
21:15:49.713 [main] INFO com.iluwatar.factorykit.App - Sword
21:15:49.713 [main] INFO com.iluwatar.factorykit.App - Bow
```
## Class diagram
![alt text](./etc/factory-kit.png "Factory Kit")
## Applicability
Use the Factory Kit pattern when
* a class can't anticipate the class of objects it must create
* you just want a new instance of a custom builder instead of the global one
* you explicitly want to define types of objects, that factory can build
* you want a separated builder and creator interface
* The factory class can't anticipate the types of objects it must create
* A new instance of a custom builder is needed instead of a global one
* The types of objects that the factory can build need to be defined outside the class
* The builder and creator interfaces need to be separated
## Related patterns
* [Builder](https://java-design-patterns.com/patterns/builder/)
* [Factory](https://java-design-patterns.com/patterns/factory/)
## Credits
* [Design Pattern Reloaded by Remi Forax: ](https://www.youtube.com/watch?v=-k2X7guaArU)
* [Design Pattern Reloaded by Remi Forax](https://www.youtube.com/watch?v=-k2X7guaArU)
......@@ -23,14 +23,16 @@
package com.iluwatar.factorykit;
import java.util.ArrayList;
import lombok.extern.slf4j.Slf4j;
/**
* Factory-kit is a creational pattern which defines a factory of immutable content with separated
* Factory kit is a creational pattern that defines a factory of immutable content with separated
* builder and factory interfaces to deal with the problem of creating one of the objects specified
* directly in the factory-kit instance.
* directly in the factory kit instance.
*
* <p>In the given example {@link WeaponFactory} represents the factory-kit, that contains four
* <p>In the given example {@link WeaponFactory} represents the factory kit, that contains four
* {@link Builder}s for creating new objects of the classes implementing {@link Weapon} interface.
*
* <p>Each of them can be called with {@link WeaponFactory#create(WeaponType)} method, with
......@@ -52,7 +54,11 @@ public class App {
builder.add(WeaponType.SPEAR, Spear::new);
builder.add(WeaponType.BOW, Bow::new);
});
var axe = factory.create(WeaponType.AXE);
LOGGER.info(axe.toString());
var list = new ArrayList<Weapon>();
list.add(factory.create(WeaponType.AXE));
list.add(factory.create(WeaponType.SPEAR));
list.add(factory.create(WeaponType.SWORD));
list.add(factory.create(WeaponType.BOW));
list.stream().forEach(weapon -> LOGGER.info("{}", weapon.toString()));
}
}
---
layout: pattern
title: Metadata Mapping
folder: metadata-mapping
permalink: /patterns/metadata-mapping/
categories: Architectural
language: en
tags:
- Data access
---
## Intent
Holds details of object-relational mapping in the metadata.
## Explanation
Real world example
> Hibernate ORM Tool uses Metadata Mapping Pattern to specify the mapping between classes and tables either using XML or annotations in code.
In plain words
> Metadata Mapping specifies the mapping between classes and tables so that we could treat a table of any database like a Java class.
Wikipedia says
> Create a "virtual [object database](https://en.wikipedia.org/wiki/Object_database)" that can be used from within the programming language.
**Programmatic Example**
We give an example about visiting the information of `USER` table in `h2` database. Firstly, we create `USER` table with `h2`:
```java
@Slf4j
public class DatabaseUtil {
private static final String DB_URL = "jdbc:h2:mem:metamapping";
private static final String CREATE_SCHEMA_SQL = "DROP TABLE IF EXISTS `user`;"
+ "CREATE TABLE `user` (\n"
+ " `id` int(11) NOT NULL AUTO_INCREMENT,\n"
+ " `username` varchar(255) NOT NULL,\n"
+ " `password` varchar(255) NOT NULL,\n"
+ " PRIMARY KEY (`id`)\n"
+ ");";
/**
* Create database.
*/
static {
LOGGER.info("create h2 database");
var source = new JdbcDataSource();
source.setURL(DB_URL);
try (var statement = source.getConnection().createStatement()) {
statement.execute(CREATE_SCHEMA_SQL);
} catch (SQLException e) {
LOGGER.error("unable to create h2 data source", e);
}
}
}
```
Correspondingly, here's the basic `User` entity.
```java
@Setter
@Getter
@ToString
public class User {
private Integer id;
private String username;
private String password;
/**
* Get a user.
* @param username user name
* @param password user password
*/
public User(String username, String password) {
this.username = username;
this.password = password;
}
}
```
Then we write a `xml` file to show the mapping between the table and the object:
```xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.iluwatar.metamapping.model.User" table="user">
<id name="id" type="java.lang.Integer" column="id">
<generator class="native"/>
</id>
<property name="username" column="username" type="java.lang.String"/>
<property name="password" column="password" type="java.lang.String"/>
</class>
</hibernate-mapping>
```
We use `Hibernate` to resolve the mapping and connect to our database, here's its configuration:
```xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- JDBC Database connection settings -->
<property name="connection.url">jdbc:h2:mem:metamapping</property>
<property name="connection.driver_class">org.h2.Driver</property>
<!-- JDBC connection pool settings ... using built-in test pool -->
<property name="connection.pool_size">1</property>
<!-- Select our SQL dialect -->
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<!-- Echo the SQL to stdout -->
<property name="show_sql">false</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create-drop</property>
<mapping resource="com/iluwatar/metamapping/model/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
```
Then we can get access to the table just like an object with `Hibernate`, here's some CRUDs:
```java
@Slf4j
public class UserService {
private static final SessionFactory factory = HibernateUtil.getSessionFactory();
/**
* List all users.
* @return list of users
*/
public List<User> listUser() {
LOGGER.info("list all users.");
List<User> users = new ArrayList<>();
try (var session = factory.openSession()) {
var tx = session.beginTransaction();
List<User> userIter = session.createQuery("FROM User").list();
for (var iterator = userIter.iterator(); iterator.hasNext();) {
users.add(iterator.next());
}
tx.commit();
} catch (HibernateException e) {
LOGGER.debug("fail to get users", e);
}
return users;
}
// other CRUDs ->
...
public void close() {
HibernateUtil.shutdown();
}
}
```
## Class diagram
![metamapping](etc/metamapping.png)
## Applicability
Use the Metadata Mapping when:
- you want reduce the amount of work needed to handle database mapping.
## Known uses
[Hibernate](https://hibernate.org/), [EclipseLink](https://www.eclipse.org/eclipselink/), [MyBatis](https://blog.mybatis.org/)......
## Credits
- [J2EE Design Patterns](https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=48d37c67fb3d845b802fa9b619ad8f31)
@startuml
interface com.iluwatar.metamapping.service.UserService {
+ List<User> listUser()
+ int createUser(User)
+ void updateUser(Integer,User)
+ void deleteUser(Integer)
+ User getUser(Integer)
+ void close()
}
class com.iluwatar.metamapping.utils.DatabaseUtil {
+ {static} void createDataSource()
}
class com.iluwatar.metamapping.model.User {
- Integer id
- String username
- String password
+ User(String username, String password)
}
class com.iluwatar.metamapping.utils.HibernateUtil {
+ {static} SessionFactory getSessionFactory()
+ {static} void shutdown()
}
class com.iluwatar.metamapping.App {
+ {static} void main(String[])
+ {static} List<User> generateSampleUsers()
}
com.iluwatar.metamapping.service.UserService <.. com.iluwatar.metamapping.App
com.iluwatar.metamapping.model.User <.. com.iluwatar.metamapping.service.UserService
com.iluwatar.metamapping.utils.HibernateUtil <.. com.iluwatar.metamapping.service.UserService
com.iluwatar.metamapping.utils.DatabaseUtil <-- com.iluwatar.metamapping.utils.HibernateUtil
@enduml
\ No newline at end of file
此差异已折叠。
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.iluwatar.metamapping.model.User" table="user">
<id name="id" type="java.lang.Integer" column="id">
<generator class="native"/>
</id>
<property name="username" column="username" type="java.lang.String"/>
<property name="password" column="password" type="java.lang.String"/>
</class>
</hibernate-mapping>
\ No newline at end of file
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册