├── .github ├── next-dev.sh ├── update-version.sh └── workflows │ └── build.yml ├── .gitignore ├── LICENSE ├── README.md ├── assembly.xml ├── pom.xml └── src ├── main ├── java │ └── io │ │ └── vlingo │ │ └── xoom │ │ └── hello │ │ ├── Bootstrap.java │ │ ├── infra │ │ ├── DescriptionData.java │ │ ├── GreetingData.java │ │ ├── MessageData.java │ │ ├── persistence │ │ │ ├── CommandModelStoreProvider.java │ │ │ ├── GreetingDataStateAdapter.java │ │ │ ├── GreetingProjectionActor.java │ │ │ ├── GreetingStateAdapter.java │ │ │ ├── ProjectionDispatcherProvider.java │ │ │ ├── Queries.java │ │ │ ├── QueriesActor.java │ │ │ └── QueryModelStoreProvider.java │ │ └── resource │ │ │ ├── GreetingResource.java │ │ │ └── HelloResource.java │ │ └── model │ │ ├── Greeting.java │ │ ├── GreetingEntity.java │ │ └── GreetingState.java └── resources │ ├── logback.xml │ └── xoom-actors.properties └── test └── java └── io └── vlingo └── xoom └── hello └── infra └── resource ├── GreetingResourceTest.java ├── HelloResourceTest.java └── ResourceTestCase.java /.github/next-dev.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | VERSION=$1 3 | 4 | [[ "$VERSION" != "" ]] || (echo "The version has to be passed as the first argument" && exit) 5 | 6 | SCRIPT_PATH=$(cd -- "$(dirname "$0")/" >/dev/null 2>&1 ; pwd -P) 7 | 8 | $SCRIPT_PATH/update-version.sh $VERSION 9 | 10 | git commit -m "Next development version $VERSION" 11 | git push --follow-tags 12 | -------------------------------------------------------------------------------- /.github/update-version.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | VERSION=${1} 4 | 5 | [[ "$VERSION" != "" ]] || (echo "The version has to be passed as the first argument" && exit) 6 | 7 | mvn versions:set -DnewVersion=$VERSION 8 | mvn versions:use-dep-version -Dincludes=io.vlingo.xoom -DdepVersion=$VERSION -DforceVersion=true 9 | 10 | git add pom.xml 11 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | push: 5 | pull_request: 6 | schedule: 7 | - cron: '0 4 * * *' 8 | 9 | jobs: 10 | build: 11 | name: Build & Deploy 12 | runs-on: ubuntu-latest 13 | timeout-minutes: 15 14 | 15 | steps: 16 | - uses: actions/checkout@v4 17 | 18 | - name: Set up JDK 1.8 19 | uses: actions/setup-java@v4 20 | with: 21 | java-version: '8' 22 | distribution: 'zulu' 23 | cache: 'maven' 24 | 25 | - name: Build with Maven 26 | run: mvn --batch-mode --update-snapshots clean install 27 | env: 28 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 29 | 30 | - name: Publish artifacts 31 | uses: actions/upload-artifact@v4 32 | with: 33 | name: JARs 34 | path: target/*.jar 35 | 36 | - name: Notify discord 37 | if: always() && (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/')) && github.repository_owner == 'vlingo' 38 | uses: 'Ilshidur/action-discord@0.3.2' 39 | env: 40 | DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }} 41 | DISCORD_USERNAME: Bob the Builder 42 | DISCORD_EMBEDS: '[{"title":"Build ${{ job.status }}", "description":"[${{ github.repository }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})","author": { "icon_url": "https://avatars.githubusercontent.com/${{ github.actor }}", "name": "${{ github.actor }}", "url": "${{ github.server_url }}/${{ github.actor }}"},"color":"${{ job.status == ''success'' && ''65280'' || ''16711680'' }}"}]' 43 | 44 | - name: Cleanup 45 | run: rm -rf ~/.m2/repository/io/vlingo 46 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # General 2 | bin/ 3 | target/ 4 | .classpath 5 | .project 6 | .settings 7 | .DS_Store 8 | dependency-reduced-pom.xml 9 | *.log 10 | build/ 11 | .factorypath 12 | 13 | # Intellij 14 | .idea/ 15 | *.iml 16 | *.iws 17 | *.ipr 18 | .gradle/ 19 | out/ 20 | 21 | # VSCode 22 | .vscode -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # xoom-helloworld 2 | 3 | [![Build](https://github.com/vlingo/xoom-helloworld/workflows/Build/badge.svg)](https://github.com/vlingo/xoom-helloworld/actions?query=workflow%3ABuild) 4 | 5 | VLINGO XOOM platform SDK "Hello, World!" service that demonstrates the use of the standard components commonly used. 6 | 7 | ## Building hello world 8 | 9 | Open a console/command window so you can build the `xoom-helloworld` and start it by executing the built `jar`: 10 | 11 | ``` 12 | $ mvn clean package 13 | ... 14 | $ java -jar target/xoom-helloworld.jar 15 | ``` 16 | 17 | The above `java` command executes the `jar` on the default port `18080`. If you would like to use 18 | a different port, you must provide it on the command line. This command uses port `8080`. 19 | 20 | ``` 21 | $ java -jar target/xoom-helloworld.jar 8080 22 | ``` 23 | 24 | The following examples assume that have started the service with the default port, `18080`. 25 | 26 | There are two resources, each with multiple endpoints. These are discussed next. 27 | 28 | ## Hello Resource 29 | 30 | The first resource is used to get `"Hello, World!"` and similar messages. 31 | 32 | You may `curl` with `GET` methods on the following resources. 33 | 34 | ``` 35 | $ curl -i -X GET http://localhost:18080/hello 36 | ``` 37 | 38 | The above `curl` responds with `200 OK` and the content body entity `"Hello, World!"` 39 | 40 | You may also provide a path parameter to indicate to whom the service should say `"Hello"`. 41 | The second example responds with `200 OK` and the content body entity `"Hello, Me!"` 42 | 43 | ``` 44 | $ curl -i -X GET http://localhost:18080/hello/Me 45 | ``` 46 | 47 | In this Hello resource example there is only one component involved. See the source code: 48 | 49 | ``` 50 | io.vlingo.xoom.hello.resource.HelloResource 51 | ``` 52 | 53 | ## Greeting Resource 54 | 55 | The second resource is a bit more involved, and is used to maintain any number of `Greeting` messages. These greetings 56 | have the following data associated with them. 57 | 58 | - `id`: a unique identity 59 | - `message`: a text message 60 | - `messageChangedCount`: the number of times the message text has changed since first being defined 61 | - `description`: a text description of the message 62 | - `descriptionChangedCount`: the number of times the description text has changed since first being defined 63 | 64 | The first operation used is to define a new `Greeting`. To do so you `POST` a `JSON` object to the `/greetings` URI. 65 | 66 | ``` 67 | $ curl -i -X POST -H "Content-Type: application/json" -d '{"id":"","message":"Hey","messageChangedCount":"0","description":"Says Hey","descriptionChangedCount":"0" }' http://localhost:18080/greetings 68 | ``` 69 | 70 | Following this you may query the new `Greeting`. See the `Location` header provided by the `POST` response for the URI of the resource to `GET`. 71 | For example: 72 | 73 | ``` 74 | HTTP/1.1 201 Created 75 | Location: /greetings/242 76 | Content-Length: 105 77 | 78 | {"id":"242","message":"Hey","messageChangedCount":0,"description":"Says Hey","descriptionChangedCount":0} 79 | ``` 80 | 81 | The location of the new `Greeting` resource is `/greetings/242`. Let's `GET` that resource. 82 | 83 | ``` 84 | $ curl -i -X GET http://localhost:18080/greetings/242 85 | ``` 86 | 87 | You should see the following: 88 | 89 | ``` 90 | HTTP/1.1 200 OK 91 | Content-Length: 105 92 | 93 | {"id":"242","message":"Hey","messageChangedCount":0,"description":"Says Hey","descriptionChangedCount":0} 94 | ``` 95 | 96 | Next `PATCH` the `Greeting` resource's `message`. 97 | 98 | ``` 99 | $ curl -i -X PATCH -H "Content-Type: application/json" -d '{"value":"Yo"}' http://localhost:18080/greetings/242/message 100 | ``` 101 | 102 | The resource responds with the following. Note that the `message` has changed to `"Yo"`, and the `messageChangedCount` is now `1`. 103 | Also notice that the `description` and the `descriptionChangedCount` remain unchanged. 104 | 105 | ``` 106 | HTTP/1.1 200 OK 107 | Content-Length: 104 108 | 109 | {"id":"242","message":"Yo","messageChangedCount":1,"description":"Says Hey","descriptionChangedCount":0} 110 | ``` 111 | 112 | Next `PATCH` the `Greeting` resource's `description`. 113 | 114 | 115 | ``` 116 | $ curl -i -X PATCH -H "Content-Type: application/json" -d '{"value":"Says Yo"}' http://localhost:18080/greetings/242/description 117 | ``` 118 | 119 | The resource responds with the following. Note that the `description` has changed to `"Says Yo"`, and the `descriptionChangedCount` is now `1`. 120 | 121 | ``` 122 | HTTP/1.1 200 OK 123 | Content-Length: 103 124 | 125 | {"id":"242","message":"Yo","messageChangedCount":1,"description":"Says Yo","descriptionChangedCount":1} 126 | ``` 127 | 128 | Now both the `message` and the `description` and the corresponding counts have all changed. 129 | 130 | In this `Greeting` resource example there are various component sets involved. See the following source code. 131 | 132 | ``` 133 | io.vlingo.xoom.hello.Bootstrap 134 | 135 | io.vlingo.xoom.hello.resource.GreetingResource 136 | 137 | io.vlingo.xoom.hello.model.Greeting 138 | io.vlingo.xoom.hello.model.GreetingEntity 139 | io.vlingo.xoom.hello.model.GreetingState 140 | 141 | io.vlingo.xoom.hello.infra.persistence.GreetingProjectionActor 142 | 143 | io.vlingo.xoom.hello.infra.persistence.Queries 144 | io.vlingo.xoom.hello.infra.persistence.QueriesActor 145 | 146 | io.vlingo.xoom.hello.infra.persistence.* - lower-level persistence setup and storage access 147 | ``` 148 | 149 | ## Documentation 150 | 151 | Documentation for the above components is found in the following links. 152 | 153 | **Suggestion: Right click and open in a new tab.** 154 | 155 | [Starting the World](https://docs.vlingo.io/xoom-actors#starting-and-terminating-the-actor-runtime) 156 | 157 | [CQRS Command and Query Models](https://docs.vlingo.io/xoom-lattice/entity-cqrs) 158 | 159 | [StatefulEntity](https://docs.vlingo.io/xoom-lattice/entity-cqrs#statefulentity-example) 160 | 161 | [StateStore Persistence](https://docs.vlingo.io/xoom-symbio/state-storage) 162 | 163 | [Query Model Projections](https://docs.vlingo.io/xoom-lattice/projections) 164 | 165 | [Reactive Storage](https://docs.vlingo.io/xoom-symbio) 166 | -------------------------------------------------------------------------------- /assembly.xml: -------------------------------------------------------------------------------- 1 | 5 | withdeps 6 | 7 | jar 8 | 9 | false 10 | 11 | 12 | / 13 | true 14 | 15 | ${artifact} 16 | 17 | 18 | 19 | / 20 | true 21 | 22 | ${artifact} 23 | 24 | 25 | 26 | **/logback.xml 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | io.vlingo.xoom 4 | xoom-helloworld 5 | 1.11.2-SNAPSHOT 6 | xoom-helloworld 7 | A "Hello, World!" service that demonstrates the use of the standard components of the VLINGO XOOM Platform. 8 | https://github.com/vlingo/xoom-helloworld 9 | 10 | 11 | Mozilla Public License 2.0 12 | https://mozilla.org/MPL/2.0/ 13 | 14 | 15 | 16 | UTF-8 17 | 1.8 18 | 1.8 19 | io.vlingo.xoom.hello.XoomInitializer 20 | 21 | 22 | 23 | io.vlingo.xoom 24 | xoom-turbo 25 | ${project.version} 26 | 27 | 28 | io.rest-assured 29 | rest-assured 30 | 5.1.1 31 | test 32 | 33 | 34 | ch.qos.logback 35 | logback-classic 36 | 1.3.12 37 | 38 | 39 | junit 40 | junit 41 | 4.13.2 42 | test 43 | 44 | 45 | 46 | 47 | 48 | org.apache.maven.plugins 49 | maven-jar-plugin 50 | 3.2.0 51 | 52 | xoom-helloworld 53 | 54 | 55 | 56 | org.apache.maven.plugins 57 | maven-shade-plugin 58 | 3.1.0 59 | 60 | 61 | package 62 | 63 | shade 64 | 65 | 66 | 67 | 68 | ${exec.mainClass} 69 | 70 | 71 | 72 | .SF 73 | 74 | 75 | .DSA 76 | 77 | 78 | .RSA 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | org.codehaus.mojo 87 | exec-maven-plugin 88 | 1.6.0 89 | 90 | java 91 | 92 | -classpath 93 | 94 | -noverify 95 | -XX:TieredStopAtLevel=1 96 | -Dcom.sun.management.jmxremote 97 | ${exec.mainClass} 98 | 99 | 100 | 101 | 102 | org.apache.maven.plugins 103 | maven-compiler-plugin 104 | 3.7.0 105 | 106 | 107 | -parameters 108 | 109 | 110 | 111 | io.vlingo.xoom 112 | xoom-turbo 113 | ${project.version} 114 | 115 | 116 | 117 | 118 | 119 | compile 120 | compile 121 | 122 | compile 123 | 124 | 125 | 126 | testCompile 127 | test-compile 128 | 129 | testCompile 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | ossrh-snapshots 140 | https://oss.sonatype.org/content/repositories/snapshots/ 141 | false 142 | true 143 | 144 | 145 | 146 | -------------------------------------------------------------------------------- /src/main/java/io/vlingo/xoom/hello/Bootstrap.java: -------------------------------------------------------------------------------- 1 | // Copyright © 2012-2023 VLINGO LABS. All rights reserved. 2 | // 3 | // This Source Code Form is subject to the terms of the 4 | // Mozilla Public License, v. 2.0. If a copy of the MPL 5 | // was not distributed with this file, You can obtain 6 | // one at https://mozilla.org/MPL/2.0/. 7 | 8 | package io.vlingo.xoom.hello; 9 | 10 | import io.vlingo.xoom.hello.infra.persistence.CommandModelStoreProvider; 11 | import io.vlingo.xoom.hello.infra.persistence.ProjectionDispatcherProvider; 12 | import io.vlingo.xoom.hello.infra.persistence.QueryModelStoreProvider; 13 | import io.vlingo.xoom.lattice.grid.Grid; 14 | import io.vlingo.xoom.lattice.model.stateful.StatefulTypeRegistry; 15 | import io.vlingo.xoom.turbo.XoomInitializationAware; 16 | import io.vlingo.xoom.turbo.annotation.initializer.ResourceHandlers; 17 | import io.vlingo.xoom.turbo.annotation.initializer.Xoom; 18 | 19 | /** 20 | * Start the service with a Server. 21 | */ 22 | @Xoom(name = "hello-world") 23 | @ResourceHandlers(packages = "io.vlingo.xoom.hello.infra.resource") 24 | public class Bootstrap implements XoomInitializationAware { 25 | 26 | @Override 27 | public void onInit(final Grid grid) { 28 | final StatefulTypeRegistry registry = new StatefulTypeRegistry(grid.world()); 29 | QueryModelStoreProvider.using(grid.world().stage(), registry); 30 | CommandModelStoreProvider.using(grid.world().stage(), registry, ProjectionDispatcherProvider.using(grid.world().stage()).storeDispatcher); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/io/vlingo/xoom/hello/infra/DescriptionData.java: -------------------------------------------------------------------------------- 1 | // Copyright © 2012-2023 VLINGO LABS. All rights reserved. 2 | // 3 | // This Source Code Form is subject to the terms of the 4 | // Mozilla Public License, v. 2.0. If a copy of the MPL 5 | // was not distributed with this file, You can obtain 6 | // one at https://mozilla.org/MPL/2.0/. 7 | 8 | package io.vlingo.xoom.hello.infra; 9 | 10 | /** 11 | * Greeting data used for HTTP request body, response body, 12 | * as well as resource queries. 13 | */ 14 | public class DescriptionData { 15 | public final String value; 16 | 17 | public static DescriptionData empty() { 18 | return new DescriptionData(""); 19 | } 20 | 21 | public static DescriptionData from(String description) { 22 | return new DescriptionData(description); 23 | } 24 | 25 | public DescriptionData(String description) { 26 | this.value = description; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/io/vlingo/xoom/hello/infra/GreetingData.java: -------------------------------------------------------------------------------- 1 | // Copyright © 2012-2023 VLINGO LABS. All rights reserved. 2 | // 3 | // This Source Code Form is subject to the terms of the 4 | // Mozilla Public License, v. 2.0. If a copy of the MPL 5 | // was not distributed with this file, You can obtain 6 | // one at https://mozilla.org/MPL/2.0/. 7 | 8 | package io.vlingo.xoom.hello.infra; 9 | 10 | import io.vlingo.xoom.hello.model.GreetingState; 11 | 12 | /** 13 | * Greeting data used for HTTP request body, response body, 14 | * as well as resource queries. 15 | */ 16 | public class GreetingData { 17 | private static GreetingData Empty = new GreetingData("", "", 0, "", 0); 18 | 19 | public final String id; 20 | public final String message; 21 | public final int messageChangedCount; 22 | public final String description; 23 | public final int descriptionChangedCount; 24 | 25 | public static GreetingData empty() { 26 | return Empty; 27 | } 28 | 29 | public static GreetingData from(GreetingState state) { 30 | return new GreetingData(state.id, state.message.value, state.message.changeCount, state.description.value, state.description.changeCount); 31 | } 32 | 33 | public static GreetingData from(String id, String message, int messageChangedCount, String description, int descriptionChangedCount) { 34 | return new GreetingData(id, message, messageChangedCount, description, descriptionChangedCount); 35 | } 36 | 37 | public GreetingData(String id, String message, int messageChangedCount, String description, int descriptionChangedCount) { 38 | this.id = id; 39 | this.message = message; 40 | this.messageChangedCount = messageChangedCount; 41 | this.description = description; 42 | this.descriptionChangedCount = descriptionChangedCount; 43 | } 44 | 45 | public GreetingData(String id, String message, String description) { 46 | this(id, message, 0, description, 0); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/io/vlingo/xoom/hello/infra/MessageData.java: -------------------------------------------------------------------------------- 1 | // Copyright © 2012-2023 VLINGO LABS. All rights reserved. 2 | // 3 | // This Source Code Form is subject to the terms of the 4 | // Mozilla Public License, v. 2.0. If a copy of the MPL 5 | // was not distributed with this file, You can obtain 6 | // one at https://mozilla.org/MPL/2.0/. 7 | 8 | package io.vlingo.xoom.hello.infra; 9 | 10 | /** 11 | * Greeting data used for HTTP request body, response body, 12 | * as well as resource queries. 13 | */ 14 | public class MessageData { 15 | public final String value; 16 | 17 | public static MessageData empty() { 18 | return new MessageData(""); 19 | } 20 | 21 | public static MessageData from(String message) { 22 | return new MessageData(message); 23 | } 24 | 25 | public MessageData(String message) { 26 | this.value = message; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/io/vlingo/xoom/hello/infra/persistence/CommandModelStoreProvider.java: -------------------------------------------------------------------------------- 1 | // Copyright © 2012-2023 VLINGO LABS. All rights reserved. 2 | // 3 | // This Source Code Form is subject to the terms of the 4 | // Mozilla Public License, v. 2.0. If a copy of the MPL 5 | // was not distributed with this file, You can obtain 6 | // one at https://mozilla.org/MPL/2.0/. 7 | 8 | package io.vlingo.xoom.hello.infra.persistence; 9 | 10 | import java.util.Arrays; 11 | 12 | import io.vlingo.xoom.actors.Definition; 13 | import io.vlingo.xoom.actors.Protocols; 14 | import io.vlingo.xoom.actors.Stage; 15 | import io.vlingo.xoom.hello.model.GreetingState; 16 | import io.vlingo.xoom.lattice.model.stateful.StatefulTypeRegistry; 17 | import io.vlingo.xoom.lattice.model.stateful.StatefulTypeRegistry.Info; 18 | import io.vlingo.xoom.symbio.EntryAdapterProvider; 19 | import io.vlingo.xoom.symbio.StateAdapterProvider; 20 | import io.vlingo.xoom.symbio.store.dispatch.Dispatcher; 21 | import io.vlingo.xoom.symbio.store.dispatch.DispatcherControl; 22 | import io.vlingo.xoom.symbio.store.state.StateStore; 23 | import io.vlingo.xoom.symbio.store.state.inmemory.InMemoryStateStoreActor; 24 | 25 | public class CommandModelStoreProvider { 26 | private static CommandModelStoreProvider instance; 27 | 28 | public final DispatcherControl dispatcherControl; 29 | public final StateStore store; 30 | 31 | public static CommandModelStoreProvider instance() { 32 | return instance; 33 | } 34 | 35 | @SuppressWarnings("rawtypes") 36 | public static CommandModelStoreProvider using(Stage stage, StatefulTypeRegistry registry, Dispatcher dispatcher) { 37 | if (instance != null) return instance; 38 | 39 | StateAdapterProvider stateAdapterProvider = new StateAdapterProvider(stage.world()); 40 | stateAdapterProvider.registerAdapter(GreetingState.class, new GreetingStateAdapter()); 41 | new EntryAdapterProvider(stage.world()); // future 42 | 43 | Protocols storeProtocols = 44 | stage.actorFor( 45 | new Class[] { StateStore.class, DispatcherControl.class }, 46 | Definition.has(InMemoryStateStoreActor.class, Definition.parameters(Arrays.asList(dispatcher)))); 47 | 48 | Protocols.Two storeWithControl = Protocols.two(storeProtocols); 49 | 50 | instance = new CommandModelStoreProvider(registry, storeWithControl._1, storeWithControl._2); 51 | 52 | return instance; 53 | } 54 | 55 | public static void reset() { 56 | instance = null; 57 | } 58 | 59 | @SuppressWarnings({ "unchecked", "rawtypes" }) 60 | private CommandModelStoreProvider(StatefulTypeRegistry registry, StateStore store, DispatcherControl dispatcherControl) { 61 | this.store = store; 62 | this.dispatcherControl = dispatcherControl; 63 | 64 | registry.register(new Info(store, GreetingState.class, GreetingState.class.getSimpleName())); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/io/vlingo/xoom/hello/infra/persistence/GreetingDataStateAdapter.java: -------------------------------------------------------------------------------- 1 | // Copyright © 2012-2023 VLINGO LABS. All rights reserved. 2 | // 3 | // This Source Code Form is subject to the terms of the 4 | // Mozilla Public License, v. 2.0. If a copy of the MPL 5 | // was not distributed with this file, You can obtain 6 | // one at https://mozilla.org/MPL/2.0/. 7 | 8 | package io.vlingo.xoom.hello.infra.persistence; 9 | 10 | import io.vlingo.xoom.common.serialization.JsonSerialization; 11 | import io.vlingo.xoom.hello.infra.GreetingData; 12 | import io.vlingo.xoom.symbio.Metadata; 13 | import io.vlingo.xoom.symbio.State.TextState; 14 | import io.vlingo.xoom.symbio.StateAdapter; 15 | 16 | public class GreetingDataStateAdapter implements StateAdapter { 17 | 18 | @Override 19 | public int typeVersion() { 20 | return 1; 21 | } 22 | 23 | @Override 24 | public GreetingData fromRawState(final TextState raw) { 25 | return JsonSerialization.deserialized(raw.data, raw.typed()); 26 | } 27 | 28 | @Override 29 | public TextState toRawState(String id, GreetingData state, int stateVersion, Metadata metadata) { 30 | final String serialization = JsonSerialization.serialized(state); 31 | return new TextState(id, GreetingData.class, typeVersion(), serialization, stateVersion, metadata); 32 | } 33 | 34 | @Override 35 | public TextState toRawState(GreetingData state, int stateVersion, Metadata metadata) { 36 | final String serialization = JsonSerialization.serialized(state); 37 | return new TextState(state.id, GreetingData.class, typeVersion(), serialization, stateVersion, metadata); 38 | } 39 | 40 | @Override 41 | public ST fromRawState(TextState raw, Class stateType) { 42 | return JsonSerialization.deserialized(raw.data, stateType); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/io/vlingo/xoom/hello/infra/persistence/GreetingProjectionActor.java: -------------------------------------------------------------------------------- 1 | // Copyright © 2012-2023 VLINGO LABS. All rights reserved. 2 | // 3 | // This Source Code Form is subject to the terms of the 4 | // Mozilla Public License, v. 2.0. If a copy of the MPL 5 | // was not distributed with this file, You can obtain 6 | // one at https://mozilla.org/MPL/2.0/. 7 | 8 | package io.vlingo.xoom.hello.infra.persistence; 9 | 10 | import io.vlingo.xoom.hello.infra.GreetingData; 11 | import io.vlingo.xoom.hello.model.Greeting.Operation; 12 | import io.vlingo.xoom.hello.model.GreetingState; 13 | import io.vlingo.xoom.lattice.model.projection.Projectable; 14 | import io.vlingo.xoom.lattice.model.projection.StateStoreProjectionActor; 15 | 16 | public class GreetingProjectionActor extends StateStoreProjectionActor { 17 | private Operation becauseOf; 18 | 19 | public GreetingProjectionActor() { 20 | super(QueryModelStoreProvider.instance().store); 21 | } 22 | 23 | @Override 24 | protected GreetingData currentDataFor(Projectable projectable) { 25 | becauseOf = Operation.valueOf(projectable.becauseOf()[0]); 26 | 27 | final GreetingState state = projectable.object(); 28 | final GreetingData current = GreetingData.from(state); 29 | 30 | return current; 31 | } 32 | 33 | @Override 34 | protected GreetingData merge(GreetingData previousData, int previousVersion, GreetingData currentData, int currentVersion) { 35 | GreetingData merged; 36 | 37 | switch (becauseOf) { 38 | case GreetingDefined: 39 | merged = currentData; 40 | break; 41 | case GreetingMessageChange: 42 | merged = GreetingData.from(previousData.id, currentData.message, currentData.messageChangedCount, previousData.description, previousData.descriptionChangedCount); 43 | break; 44 | case GreetingDescriptionChanged: 45 | merged = GreetingData.from(previousData.id, previousData.message, previousData.messageChangedCount, currentData.description, currentData.descriptionChangedCount); 46 | break; 47 | default: 48 | merged = currentData; 49 | } 50 | 51 | return merged; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/io/vlingo/xoom/hello/infra/persistence/GreetingStateAdapter.java: -------------------------------------------------------------------------------- 1 | // Copyright © 2012-2023 VLINGO LABS. All rights reserved. 2 | // 3 | // This Source Code Form is subject to the terms of the 4 | // Mozilla Public License, v. 2.0. If a copy of the MPL 5 | // was not distributed with this file, You can obtain 6 | // one at https://mozilla.org/MPL/2.0/. 7 | 8 | package io.vlingo.xoom.hello.infra.persistence; 9 | 10 | import io.vlingo.xoom.common.serialization.JsonSerialization; 11 | import io.vlingo.xoom.hello.model.GreetingState; 12 | import io.vlingo.xoom.symbio.Metadata; 13 | import io.vlingo.xoom.symbio.State.TextState; 14 | import io.vlingo.xoom.symbio.StateAdapter; 15 | 16 | public class GreetingStateAdapter implements StateAdapter { 17 | 18 | @Override 19 | public int typeVersion() { 20 | return 1; 21 | } 22 | 23 | @Override 24 | public GreetingState fromRawState(final TextState raw) { 25 | return JsonSerialization.deserialized(raw.data, raw.typed()); 26 | } 27 | 28 | @Override 29 | public TextState toRawState(String id, GreetingState state, int stateVersion, Metadata metadata) { 30 | final String serialization = JsonSerialization.serialized(state); 31 | return new TextState(id, GreetingState.class, typeVersion(), serialization, stateVersion, metadata); 32 | } 33 | 34 | @Override 35 | public ST fromRawState(TextState raw, Class stateType) { 36 | return JsonSerialization.deserialized(raw.data, stateType); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/io/vlingo/xoom/hello/infra/persistence/ProjectionDispatcherProvider.java: -------------------------------------------------------------------------------- 1 | // Copyright © 2012-2023 VLINGO LABS. All rights reserved. 2 | // 3 | // This Source Code Form is subject to the terms of the 4 | // Mozilla Public License, v. 2.0. If a copy of the MPL 5 | // was not distributed with this file, You can obtain 6 | // one at https://mozilla.org/MPL/2.0/. 7 | 8 | package io.vlingo.xoom.hello.infra.persistence; 9 | 10 | import java.util.Arrays; 11 | import java.util.List; 12 | 13 | import io.vlingo.xoom.actors.Definition; 14 | import io.vlingo.xoom.actors.Protocols; 15 | import io.vlingo.xoom.actors.Stage; 16 | import io.vlingo.xoom.hello.model.Greeting.Operation; 17 | import io.vlingo.xoom.lattice.model.projection.ProjectionDispatcher; 18 | import io.vlingo.xoom.lattice.model.projection.ProjectionDispatcher.ProjectToDescription; 19 | import io.vlingo.xoom.lattice.model.projection.TextProjectionDispatcherActor; 20 | import io.vlingo.xoom.symbio.store.dispatch.Dispatcher; 21 | 22 | @SuppressWarnings("rawtypes") 23 | public class ProjectionDispatcherProvider { 24 | private static ProjectionDispatcherProvider instance; 25 | 26 | public final ProjectionDispatcher projectionDispatcher; 27 | public final Dispatcher storeDispatcher; 28 | 29 | public static ProjectionDispatcherProvider instance() { 30 | return instance; 31 | } 32 | 33 | public static ProjectionDispatcherProvider using(final Stage stage) { 34 | if (instance != null) return instance; 35 | 36 | final List descriptions = 37 | Arrays.asList(new ProjectToDescription( 38 | GreetingProjectionActor.class, 39 | Operation.GreetingDefined.name(), 40 | Operation.GreetingMessageChange.name(), 41 | Operation.GreetingDescriptionChanged.name())); 42 | 43 | final Protocols dispatcherProtocols = 44 | stage.actorFor( 45 | new Class[] { Dispatcher.class, ProjectionDispatcher.class }, 46 | Definition.has(TextProjectionDispatcherActor.class, Definition.parameters(descriptions))); 47 | 48 | final Protocols.Two dispatchers = Protocols.two(dispatcherProtocols); 49 | 50 | instance = new ProjectionDispatcherProvider(dispatchers._1, dispatchers._2); 51 | 52 | return instance; 53 | } 54 | 55 | public static void reset() { 56 | instance = null; 57 | } 58 | 59 | private ProjectionDispatcherProvider(final Dispatcher storeDispatcher, final ProjectionDispatcher projectionDispatcher) { 60 | this.storeDispatcher = storeDispatcher; 61 | this.projectionDispatcher = projectionDispatcher; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/io/vlingo/xoom/hello/infra/persistence/Queries.java: -------------------------------------------------------------------------------- 1 | // Copyright © 2012-2023 VLINGO LABS. All rights reserved. 2 | // 3 | // This Source Code Form is subject to the terms of the 4 | // Mozilla Public License, v. 2.0. If a copy of the MPL 5 | // was not distributed with this file, You can obtain 6 | // one at https://mozilla.org/MPL/2.0/. 7 | 8 | package io.vlingo.xoom.hello.infra.persistence; 9 | 10 | import java.util.Collection; 11 | 12 | import io.vlingo.xoom.common.Completes; 13 | import io.vlingo.xoom.hello.infra.GreetingData; 14 | 15 | /** 16 | * The queries that may be run against the query model. 17 | */ 18 | public interface Queries { 19 | Completes greetingOf(String greetingId); 20 | Completes> greetings(); 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/io/vlingo/xoom/hello/infra/persistence/QueriesActor.java: -------------------------------------------------------------------------------- 1 | // Copyright © 2012-2023 VLINGO LABS. All rights reserved. 2 | // 3 | // This Source Code Form is subject to the terms of the 4 | // Mozilla Public License, v. 2.0. If a copy of the MPL 5 | // was not distributed with this file, You can obtain 6 | // one at https://mozilla.org/MPL/2.0/. 7 | 8 | package io.vlingo.xoom.hello.infra.persistence; 9 | 10 | import java.util.ArrayList; 11 | import java.util.Collection; 12 | 13 | import io.vlingo.xoom.common.Completes; 14 | import io.vlingo.xoom.hello.infra.GreetingData; 15 | import io.vlingo.xoom.lattice.query.StateStoreQueryActor; 16 | import io.vlingo.xoom.symbio.store.state.StateStore; 17 | 18 | /** 19 | * The actor that is responsible for running queries. 20 | */ 21 | public class QueriesActor extends StateStoreQueryActor implements Queries { 22 | public QueriesActor(StateStore store) { 23 | super(store); 24 | } 25 | 26 | @Override 27 | public Completes greetingOf(String greetingId) { 28 | return queryStateFor(greetingId, GreetingData.class, GreetingData.empty()); 29 | } 30 | 31 | @Override 32 | public Completes> greetings() { 33 | return streamAllOf(GreetingData.class, new ArrayList<>()); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/io/vlingo/xoom/hello/infra/persistence/QueryModelStoreProvider.java: -------------------------------------------------------------------------------- 1 | // Copyright © 2012-2023 VLINGO LABS. All rights reserved. 2 | // 3 | // This Source Code Form is subject to the terms of the 4 | // Mozilla Public License, v. 2.0. If a copy of the MPL 5 | // was not distributed with this file, You can obtain 6 | // one at https://mozilla.org/MPL/2.0/. 7 | 8 | package io.vlingo.xoom.hello.infra.persistence; 9 | 10 | import java.util.Arrays; 11 | 12 | import io.vlingo.xoom.actors.Stage; 13 | import io.vlingo.xoom.hello.infra.GreetingData; 14 | import io.vlingo.xoom.lattice.model.stateful.StatefulTypeRegistry; 15 | import io.vlingo.xoom.lattice.model.stateful.StatefulTypeRegistry.Info; 16 | import io.vlingo.xoom.symbio.EntryAdapterProvider; 17 | import io.vlingo.xoom.symbio.StateAdapterProvider; 18 | import io.vlingo.xoom.symbio.store.dispatch.NoOpDispatcher; 19 | import io.vlingo.xoom.symbio.store.state.StateStore; 20 | import io.vlingo.xoom.symbio.store.state.inmemory.InMemoryStateStoreActor; 21 | 22 | public class QueryModelStoreProvider { 23 | private static QueryModelStoreProvider instance; 24 | 25 | public final Queries queries; 26 | public final StateStore store; 27 | 28 | public static QueryModelStoreProvider instance() { 29 | return instance; 30 | } 31 | 32 | public static QueryModelStoreProvider using(final Stage stage, final StatefulTypeRegistry registry) { 33 | if (instance != null) return instance; 34 | 35 | final StateAdapterProvider stateAdapterProvider = new StateAdapterProvider(stage.world()); 36 | stateAdapterProvider.registerAdapter(GreetingData.class, new GreetingDataStateAdapter()); 37 | new EntryAdapterProvider(stage.world()); // future 38 | 39 | final StateStore store = stage.actorFor(StateStore.class, InMemoryStateStoreActor.class, Arrays.asList(new NoOpDispatcher())); 40 | 41 | final Queries queries = stage.actorFor(Queries.class, QueriesActor.class, store); 42 | 43 | instance = new QueryModelStoreProvider(registry, store, queries); 44 | 45 | return instance; 46 | } 47 | 48 | public static void reset() { 49 | instance = null; 50 | } 51 | 52 | @SuppressWarnings({ "unchecked", "rawtypes" }) 53 | private QueryModelStoreProvider(final StatefulTypeRegistry registry, final StateStore store, final Queries queries) { 54 | this.store = store; 55 | this.queries = queries; 56 | 57 | registry.register(new Info(store, GreetingData.class, GreetingData.class.getSimpleName())); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/io/vlingo/xoom/hello/infra/resource/GreetingResource.java: -------------------------------------------------------------------------------- 1 | // Copyright © 2012-2023 VLINGO LABS. All rights reserved. 2 | // 3 | // This Source Code Form is subject to the terms of the 4 | // Mozilla Public License, v. 2.0. If a copy of the MPL 5 | // was not distributed with this file, You can obtain 6 | // one at https://mozilla.org/MPL/2.0/. 7 | 8 | package io.vlingo.xoom.hello.infra.resource; 9 | 10 | import static io.vlingo.xoom.common.serialization.JsonSerialization.serialized; 11 | import static io.vlingo.xoom.http.Response.Status.Created; 12 | import static io.vlingo.xoom.http.Response.Status.NotFound; 13 | import static io.vlingo.xoom.http.Response.Status.Ok; 14 | import static io.vlingo.xoom.http.ResponseHeader.ContentType; 15 | import static io.vlingo.xoom.http.ResponseHeader.Location; 16 | import static io.vlingo.xoom.http.ResponseHeader.headers; 17 | import static io.vlingo.xoom.http.ResponseHeader.of; 18 | import static io.vlingo.xoom.http.resource.ResourceBuilder.get; 19 | import static io.vlingo.xoom.http.resource.ResourceBuilder.patch; 20 | import static io.vlingo.xoom.http.resource.ResourceBuilder.post; 21 | import static io.vlingo.xoom.http.resource.ResourceBuilder.resource; 22 | 23 | import io.vlingo.xoom.actors.Stage; 24 | import io.vlingo.xoom.common.Completes; 25 | import io.vlingo.xoom.hello.infra.DescriptionData; 26 | import io.vlingo.xoom.hello.infra.GreetingData; 27 | import io.vlingo.xoom.hello.infra.MessageData; 28 | import io.vlingo.xoom.hello.infra.persistence.Queries; 29 | import io.vlingo.xoom.hello.infra.persistence.QueryModelStoreProvider; 30 | import io.vlingo.xoom.hello.model.Greeting; 31 | import io.vlingo.xoom.http.Response; 32 | import io.vlingo.xoom.http.resource.DynamicResourceHandler; 33 | import io.vlingo.xoom.http.resource.Resource; 34 | 35 | public class GreetingResource extends DynamicResourceHandler { 36 | 37 | private final Queries queries; 38 | 39 | public GreetingResource(final Stage stage) { 40 | super(stage); 41 | this.queries = QueryModelStoreProvider.instance().queries; 42 | } 43 | 44 | public Completes defineGreeting(GreetingData data) { 45 | return Greeting 46 | .defineWith(stage(), data.message, data.description) 47 | .andThenTo(state -> Completes.withSuccess(Response.of(Created, headers(of(Location, greetingLocation(state.id))).and(of(ContentType, "application/json")), serialized(GreetingData.from(state))))); 48 | } 49 | 50 | public Completes changeGreetingMessage(String greetingId, MessageData message) { 51 | return resolve(greetingId) 52 | .andThenTo(greeting -> greeting.withMessage(message.value)) 53 | .andThenTo(state -> Completes.withSuccess(Response.of(Ok, headers(of(ContentType, "application/json")), serialized(GreetingData.from(state))))) 54 | .otherwise(noGreeting -> Response.of(NotFound)); 55 | } 56 | 57 | public Completes changeGreetingDescription(String greetingId, DescriptionData description) { 58 | return resolve(greetingId) 59 | .andThenTo(greeting -> greeting.withDescription(description.value)) 60 | .andThenTo(state -> Completes.withSuccess(Response.of(Ok, headers(of(ContentType, "application/json")), serialized(GreetingData.from(state))))) 61 | .otherwise(noGreeting -> Response.of(NotFound)); 62 | } 63 | 64 | public Completes queryGreetings() { 65 | return queries.greetings() 66 | .andThenTo(data -> Completes.withSuccess(Response.of(Ok, headers(of(ContentType, "application/json")), serialized(data)))); 67 | } 68 | 69 | public Completes queryGreeting(String greetingId) { 70 | return queries.greetingOf(greetingId) 71 | .andThenTo(GreetingData.empty(), data -> Completes.withSuccess(Response.of(Ok, headers(of(ContentType, "application/json")), serialized(data)))) 72 | .otherwise(noData -> Response.of(NotFound)); 73 | } 74 | 75 | @Override 76 | public Resource routes() { 77 | return resource("Greeting Resource", 78 | post("/greetings") 79 | .body(GreetingData.class) 80 | .handle(this::defineGreeting), 81 | patch("/greetings/{greetingId}/message") 82 | .param(String.class) 83 | .body(MessageData.class) 84 | .handle(this::changeGreetingMessage), 85 | patch("/greetings/{greetingId}/description") 86 | .param(String.class) 87 | .body(DescriptionData.class) 88 | .handle(this::changeGreetingDescription), 89 | get("/greetings") 90 | .handle(this::queryGreetings), 91 | get("/greetings/{greetingId}") 92 | .param(String.class) 93 | .handle(this::queryGreeting)); 94 | } 95 | 96 | private String greetingLocation(final String greetingId) { 97 | return "/greetings/" + greetingId; 98 | } 99 | 100 | private Completes resolve(final String greetingId) { 101 | return stage().actorOf(Greeting.class, stage().addressFactory().from(greetingId)); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /src/main/java/io/vlingo/xoom/hello/infra/resource/HelloResource.java: -------------------------------------------------------------------------------- 1 | // Copyright © 2012-2023 VLINGO LABS. All rights reserved. 2 | // 3 | // This Source Code Form is subject to the terms of the 4 | // Mozilla Public License, v. 2.0. If a copy of the MPL 5 | // was not distributed with this file, You can obtain 6 | // one at https://mozilla.org/MPL/2.0/. 7 | 8 | package io.vlingo.xoom.hello.infra.resource; 9 | 10 | import static io.vlingo.xoom.http.Response.Status.Ok; 11 | import static io.vlingo.xoom.http.resource.ResourceBuilder.get; 12 | import static io.vlingo.xoom.http.resource.ResourceBuilder.resource; 13 | 14 | import io.vlingo.xoom.actors.Stage; 15 | import io.vlingo.xoom.common.Completes; 16 | import io.vlingo.xoom.http.Response; 17 | import io.vlingo.xoom.http.resource.DynamicResourceHandler; 18 | import io.vlingo.xoom.http.resource.Resource; 19 | 20 | public class HelloResource extends DynamicResourceHandler { 21 | private static final String Hello = "Hello, #!"; 22 | private static final String World = "World"; 23 | 24 | public HelloResource(final Stage stage) { super(stage); } 25 | 26 | public Completes hello() { 27 | return helloWhom(World); 28 | } 29 | 30 | public Completes helloWhom(String whom) { 31 | return Completes.withSuccess(Response.of(Ok, Hello.replace("#", whom))); 32 | } 33 | 34 | public Resource routes() { 35 | return resource("Hello Resource", 36 | get("/hello") 37 | .handle(this::hello), 38 | get("/hello/{whom}") 39 | .param(String.class) 40 | .handle(this::helloWhom)); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/io/vlingo/xoom/hello/model/Greeting.java: -------------------------------------------------------------------------------- 1 | // Copyright © 2012-2023 VLINGO LABS. All rights reserved. 2 | // 3 | // This Source Code Form is subject to the terms of the 4 | // Mozilla Public License, v. 2.0. If a copy of the MPL 5 | // was not distributed with this file, You can obtain 6 | // one at https://mozilla.org/MPL/2.0/. 7 | 8 | package io.vlingo.xoom.hello.model; 9 | 10 | import io.vlingo.xoom.actors.Address; 11 | import io.vlingo.xoom.actors.Definition; 12 | import io.vlingo.xoom.actors.Stage; 13 | import io.vlingo.xoom.common.Completes; 14 | 15 | /** 16 | * The Greeting protocol. 17 | */ 18 | public interface Greeting { 19 | static Completes defineWith(Stage stage, String message, String description) { 20 | Address address = stage.addressFactory().uniquePrefixedWith("g-"); 21 | Greeting greeting = stage.actorFor(Greeting.class, Definition.has(GreetingEntity.class, Definition.parameters(address.idString())), address); 22 | return greeting.defineWith(message, description); 23 | } 24 | 25 | /** 26 | * Defines my initial message and description. I may not 27 | * be redefined using this message. 28 | * @param message the String initial message of the Greeting 29 | * @param description the String initial description of the Greeting 30 | * @return {@code Completes} 31 | */ 32 | Completes defineWith(String message, String description); 33 | 34 | /** 35 | * Changes my message. 36 | * @param message the new String message of the Greeting 37 | * @return {@code Completes} 38 | */ 39 | Completes withMessage(String message); 40 | 41 | /** 42 | * Changes my description. 43 | * @param description the new String description of the Greeting 44 | * @return {@code Completes} 45 | */ 46 | Completes withDescription(String description); 47 | 48 | /** 49 | * The operations that may be performed on a Greeting. 50 | */ 51 | static enum Operation { 52 | GreetingDefined, GreetingMessageChange, GreetingDescriptionChanged 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/io/vlingo/xoom/hello/model/GreetingEntity.java: -------------------------------------------------------------------------------- 1 | // Copyright © 2012-2023 VLINGO LABS. All rights reserved. 2 | // 3 | // This Source Code Form is subject to the terms of the 4 | // Mozilla Public License, v. 2.0. If a copy of the MPL 5 | // was not distributed with this file, You can obtain 6 | // one at https://mozilla.org/MPL/2.0/. 7 | 8 | package io.vlingo.xoom.hello.model; 9 | 10 | import io.vlingo.xoom.common.Completes; 11 | import io.vlingo.xoom.lattice.model.stateful.StatefulEntity; 12 | 13 | public class GreetingEntity extends StatefulEntity implements Greeting { 14 | private GreetingState state; 15 | 16 | // when new 17 | public GreetingEntity(String id) { 18 | super(id); 19 | } 20 | 21 | // when existing 22 | public GreetingEntity() { 23 | super(); 24 | } 25 | 26 | @Override 27 | public Completes defineWith(String message, String description) { 28 | if (state == null) { 29 | return apply(GreetingState.has(id, message, description), Operation.GreetingDefined.name(), () -> state); 30 | } else { 31 | return completes().with(state); 32 | } 33 | } 34 | 35 | @Override 36 | public Completes withMessage(String message) { 37 | return apply(state.withMessage(message), Operation.GreetingMessageChange.name(), () -> state); 38 | } 39 | 40 | @Override 41 | public Completes withDescription(String description) { 42 | return apply(state.withDescription(description), Operation.GreetingDescriptionChanged.name(), () -> state); 43 | } 44 | 45 | @Override 46 | protected void state(GreetingState state) { 47 | this.state = state; 48 | } 49 | 50 | @Override 51 | protected Class stateType() { 52 | return GreetingState.class; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/io/vlingo/xoom/hello/model/GreetingState.java: -------------------------------------------------------------------------------- 1 | // Copyright © 2012-2023 VLINGO LABS. All rights reserved. 2 | // 3 | // This Source Code Form is subject to the terms of the 4 | // Mozilla Public License, v. 2.0. If a copy of the MPL 5 | // was not distributed with this file, You can obtain 6 | // one at https://mozilla.org/MPL/2.0/. 7 | 8 | package io.vlingo.xoom.hello.model; 9 | 10 | /** 11 | * State held by the Greeting. 12 | */ 13 | public final class GreetingState { 14 | public final String id; 15 | public final Message message; 16 | public final Description description; 17 | 18 | public static GreetingState has(String id, String message, String description) { 19 | return new GreetingState(id, Message.with(message), Description.with(description)); 20 | } 21 | 22 | public GreetingState withMessage(String message) { 23 | return new GreetingState(id, this.message.changeWith(message), description); 24 | } 25 | 26 | public GreetingState withDescription(String description) { 27 | return new GreetingState(id, message, this.description.changeWith(description)); 28 | } 29 | 30 | private GreetingState(String id, Message message, Description description) { 31 | this.id = id; 32 | this.message = message; 33 | this.description = description; 34 | } 35 | 36 | 37 | /** 38 | * Message of the GreetingState, which tracks change counts. 39 | */ 40 | public static final class Message { 41 | public final String value; 42 | public final int changeCount; 43 | 44 | public static Message with(String message) { 45 | return new Message(message, 0); 46 | } 47 | 48 | public Message changeWith(String message) { 49 | return new Message(message, changeCount + 1); 50 | } 51 | 52 | private Message(String message, int changeCount) { 53 | this.value = message; 54 | this.changeCount = changeCount; 55 | } 56 | } 57 | 58 | /** 59 | * Description of the GreetingState, which tracks change counts. 60 | */ 61 | public static final class Description { 62 | public final String value; 63 | public final int changeCount; 64 | 65 | public static Description with(String description) { 66 | return new Description(description, 0); 67 | } 68 | 69 | public Description changeWith(String description) { 70 | return new Description(description, changeCount + 1); 71 | } 72 | 73 | private Description(String description, int changeCount) { 74 | this.value = description; 75 | this.changeCount = changeCount; 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/resources/xoom-actors.properties: -------------------------------------------------------------------------------- 1 | plugin.name.queueMailbox = true 2 | plugin.queueMailbox.classname = io.vlingo.xoom.actors.plugin.mailbox.concurrentqueue.ConcurrentQueueMailboxPlugin 3 | plugin.queueMailbox.defaultMailbox = true 4 | plugin.queueMailbox.numberOfDispatchersFactor = 1.5 5 | plugin.queueMailbox.numberOfDispatchers = 0 6 | plugin.queueMailbox.dispatcherThrottlingCount = 1 7 | 8 | plugin.name.slf4jLogger = true 9 | plugin.slf4jLogger.classname = io.vlingo.xoom.actors.plugin.logging.slf4j.Slf4jLoggerPlugin 10 | plugin.slf4jLogger.name = XOOM 11 | plugin.slf4jLogger.defaultLogger = true 12 | 13 | plugin.name.pooledCompletes = true 14 | plugin.pooledCompletes.classname = io.vlingo.xoom.actors.plugin.completes.PooledCompletesPlugin 15 | plugin.pooledCompletes.pool = 10 16 | plugin.pooledCompletes.mailbox = queueMailbox 17 | 18 | proxy.generated.classes.main = target/classes/ 19 | proxy.generated.sources.main = target/generated-sources/ 20 | proxy.generated.classes.test = target/test-classes/ 21 | proxy.generated.sources.test = target/generated-test-sources/ 22 | -------------------------------------------------------------------------------- /src/test/java/io/vlingo/xoom/hello/infra/resource/GreetingResourceTest.java: -------------------------------------------------------------------------------- 1 | package io.vlingo.xoom.hello.infra.resource; 2 | 3 | import io.vlingo.xoom.hello.infra.DescriptionData; 4 | import io.vlingo.xoom.hello.infra.GreetingData; 5 | import io.vlingo.xoom.hello.infra.MessageData; 6 | import org.junit.Test; 7 | 8 | import java.util.UUID; 9 | 10 | import static org.hamcrest.CoreMatchers.notNullValue; 11 | import static org.hamcrest.core.IsEqual.equalTo; 12 | import static org.hamcrest.core.StringRegularExpression.matchesRegex; 13 | 14 | public class GreetingResourceTest extends ResourceTestCase { 15 | 16 | @Test 17 | public void itCreatesNewGreeting() { 18 | givenJsonClient() 19 | .body(greetingData("Message", "Description")) 20 | .when() 21 | .post("/greetings") 22 | .then() 23 | .statusCode(201) 24 | .header("Location", matchesRegex("/greetings/([a-f0-9]{8}(-[a-f0-9]{4}){4}[a-f0-9]{8})")) 25 | .body( 26 | "id", notNullValue(), 27 | "message", equalTo("Message"), 28 | "description", equalTo("Description"), 29 | "messageChangedCount", equalTo(0), 30 | "descriptionChangedCount", equalTo(0) 31 | ); 32 | } 33 | 34 | @Test 35 | public void itGetsAnExistingGreeting() { 36 | String location = givenGreetingWasCreated(greetingData("Message", "Description")); 37 | 38 | givenJsonClient() 39 | .when() 40 | .get(location) 41 | .then() 42 | .statusCode(200) 43 | .body( 44 | "id", equalTo(locationToId(location)), 45 | "message", equalTo("Message"), 46 | "description", equalTo("Description"), 47 | "messageChangedCount", equalTo(0), 48 | "descriptionChangedCount", equalTo(0) 49 | ); 50 | } 51 | 52 | @Test 53 | public void itReturns404IfGreetingIsNotFound() { 54 | givenJsonClient() 55 | .when() 56 | .get("/greetings/42424242") 57 | .then() 58 | .statusCode(404); 59 | } 60 | 61 | @Test 62 | public void itGetsExistingGreetings() { 63 | givenGreetingWasCreated(greetingData("Message", "Description")); 64 | 65 | givenJsonClient() 66 | .when() 67 | .get("/greetings") 68 | .then() 69 | .statusCode(200) 70 | .body( 71 | "[0].id", notNullValue(), 72 | "[0].message", equalTo("Message"), 73 | "[0].description", equalTo("Description"), 74 | "[0].messageChangedCount", equalTo(0), 75 | "[0].descriptionChangedCount", equalTo(0) 76 | ); 77 | } 78 | 79 | @Test 80 | public void itChangesTheMessage() { 81 | String id = locationToId( 82 | givenGreetingWasCreated(greetingData("Message", "Description")) 83 | ); 84 | 85 | givenJsonClient() 86 | .body(messageData("New Message")) 87 | .when() 88 | .patch(String.format("/greetings/%s/message", id)) 89 | .then() 90 | .statusCode(200) 91 | .body( 92 | "id", equalTo(id), 93 | "message", equalTo("New Message"), 94 | "description", equalTo("Description"), 95 | "messageChangedCount", equalTo(1), 96 | "descriptionChangedCount", equalTo(0) 97 | ); 98 | } 99 | 100 | @Test 101 | public void itReturns404IfGreetingCannotBeFoundForChangingTheMessage() { 102 | givenJsonClient() 103 | .body(messageData("New Message")) 104 | .when() 105 | .patch(String.format("/greetings/%s/message", UUID.randomUUID())) 106 | .then() 107 | .statusCode(404); 108 | } 109 | 110 | @Test 111 | public void itChangesTheDescription() { 112 | String id = locationToId( 113 | givenGreetingWasCreated(greetingData("Message", "Description")) 114 | ); 115 | 116 | givenJsonClient() 117 | .body(descriptionData("New Description")) 118 | .when() 119 | .patch(String.format("/greetings/%s/description", id)) 120 | .then() 121 | .statusCode(200) 122 | .body( 123 | "id", equalTo(id), 124 | "message", equalTo("Message"), 125 | "description", equalTo("New Description"), 126 | "messageChangedCount", equalTo(0), 127 | "descriptionChangedCount", equalTo(1) 128 | ); 129 | } 130 | 131 | @Test 132 | public void itReturns404IfGreetingCannotBeFoundForChangingTheDescription() { 133 | givenJsonClient() 134 | .body(descriptionData("New Description")) 135 | .when() 136 | .patch(String.format("/greetings/%s/description", UUID.randomUUID())) 137 | .then() 138 | .statusCode(404); 139 | } 140 | 141 | private String givenGreetingWasCreated(GreetingData greetingData) { 142 | return givenJsonClient() 143 | .body(greetingData) 144 | .when() 145 | .post("/greetings") 146 | .then() 147 | .statusCode(201) 148 | .header("Location", matchesRegex("/greetings/([a-f0-9]{8}(-[a-f0-9]{4}){4}[a-f0-9]{8})")) 149 | .extract() 150 | .header("Location"); 151 | } 152 | 153 | private String locationToId(String location) { 154 | return location.replaceFirst("/greetings/", ""); 155 | } 156 | 157 | private GreetingData greetingData(String message, String description) { 158 | return new GreetingData("", message, description); 159 | } 160 | 161 | private MessageData messageData(String newMessage) { 162 | return new MessageData(newMessage); 163 | } 164 | 165 | private DescriptionData descriptionData(String newDescription) { 166 | return new DescriptionData(newDescription); 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /src/test/java/io/vlingo/xoom/hello/infra/resource/HelloResourceTest.java: -------------------------------------------------------------------------------- 1 | package io.vlingo.xoom.hello.infra.resource; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.hamcrest.CoreMatchers.equalTo; 6 | import static org.hamcrest.CoreMatchers.is; 7 | 8 | public class HelloResourceTest extends ResourceTestCase { 9 | 10 | @Test 11 | public void itGreetsTheWorldIfNoGameWasGiven() { 12 | givenJsonClient() 13 | .when() 14 | .get("/hello") 15 | .then() 16 | .statusCode(200) 17 | .body(is(equalTo("Hello, World!"))); 18 | } 19 | 20 | @Test 21 | public void itGreetsUsingTheGivenName() { 22 | givenJsonClient() 23 | .when() 24 | .get("/hello/Kuba") 25 | .then() 26 | .statusCode(200) 27 | .body(is(equalTo("Hello, Kuba!"))); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/io/vlingo/xoom/hello/infra/resource/ResourceTestCase.java: -------------------------------------------------------------------------------- 1 | package io.vlingo.xoom.hello.infra.resource; 2 | 3 | import io.restassured.http.ContentType; 4 | import io.restassured.specification.RequestSpecification; 5 | import io.vlingo.xoom.hello.XoomInitializer; 6 | import io.vlingo.xoom.hello.infra.persistence.CommandModelStoreProvider; 7 | import io.vlingo.xoom.hello.infra.persistence.ProjectionDispatcherProvider; 8 | import io.vlingo.xoom.hello.infra.persistence.QueryModelStoreProvider; 9 | import org.junit.After; 10 | import org.junit.Before; 11 | 12 | import java.util.concurrent.atomic.AtomicInteger; 13 | 14 | import static io.restassured.RestAssured.given; 15 | import static org.hamcrest.CoreMatchers.equalTo; 16 | import static org.hamcrest.CoreMatchers.is; 17 | import static org.hamcrest.MatcherAssert.assertThat; 18 | 19 | abstract class ResourceTestCase { 20 | private XoomInitializer xoom; 21 | 22 | @Before 23 | public void setUp() throws Exception { 24 | XoomInitializer.main(new String[]{}); 25 | xoom = XoomInitializer.instance(); 26 | Boolean startUpSuccess = xoom.server().startUp().await(100); 27 | assertThat(startUpSuccess, is(equalTo(true))); 28 | } 29 | 30 | @After 31 | public void cleanUp() throws Exception { 32 | xoom.stopServer().await(100); 33 | xoom.terminateWorld(); 34 | 35 | QueryModelStoreProvider.reset(); 36 | ProjectionDispatcherProvider.reset(); 37 | CommandModelStoreProvider.reset(); 38 | } 39 | 40 | protected RequestSpecification givenJsonClient() { 41 | return given() 42 | .port(18080) 43 | .accept(ContentType.JSON) 44 | .contentType(ContentType.JSON); 45 | } 46 | } 47 | --------------------------------------------------------------------------------