|
| 1 | +// Copyright © 2012-2021 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 | +package io.vlingo.xoom.designer.codegen.e2e.java.bookstoreservice; |
| 8 | + |
| 9 | +import io.restassured.path.json.JsonPath; |
| 10 | +import io.restassured.response.Response; |
| 11 | +import io.vlingo.xoom.designer.codegen.e2e.Project; |
| 12 | +import io.vlingo.xoom.designer.codegen.e2e.RequestAttempt; |
| 13 | +import io.vlingo.xoom.designer.codegen.e2e.java.JavaBasedProjectGenerationTest; |
| 14 | +import org.junit.jupiter.api.AfterEach; |
| 15 | +import org.junit.jupiter.api.Assertions; |
| 16 | +import org.junit.jupiter.api.BeforeAll; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import java.util.UUID; |
| 20 | +import java.util.function.Predicate; |
| 21 | + |
| 22 | +import static io.vlingo.xoom.http.Response.Status; |
| 23 | + |
| 24 | +/** |
| 25 | + * See: https://docs.vlingo.io/xoom-designer/development-guide/e2e-tests |
| 26 | + */ |
| 27 | +public class BookStoreWithCompositeIdServiceGenerationTest extends JavaBasedProjectGenerationTest { |
| 28 | + |
| 29 | + @BeforeAll |
| 30 | + public static void setUp() { |
| 31 | + init(); |
| 32 | + } |
| 33 | + /** |
| 34 | + * Test that the service is generated and working with: |
| 35 | + * - CompositeId |
| 36 | + * - Sourced Entities containing only scalar-typed fields |
| 37 | + * - Event-based projection |
| 38 | + * - Xoom Annotations |
| 39 | + * - ReactJS app gen |
| 40 | + */ |
| 41 | + @Test |
| 42 | + public void testThatServiceIsWorking() { |
| 43 | + final BookData newBook = |
| 44 | + BookData.sampleOfInitialData(); |
| 45 | + |
| 46 | + final BookData bookWithNewPrice = |
| 47 | + BookData.sampleOfChangedPrice(); |
| 48 | + |
| 49 | + final Project projectCompositeId = |
| 50 | + Project.from("book-store-context", "book-store-with-composite-id"); |
| 51 | + |
| 52 | + generateAndRun(projectCompositeId); |
| 53 | + |
| 54 | + final Predicate<JsonPath> validResponsePreConditionOnNewBook = |
| 55 | + res -> res.get("title") != null && !res.get("title").toString().isEmpty(); |
| 56 | + |
| 57 | + final Predicate<JsonPath> validResponsePreConditionOnChangedPrice = |
| 58 | + res -> res.get("price") != null && !res.get("price").toString().equals(String.valueOf(newBook.price)); |
| 59 | + |
| 60 | + assertThatBookIsCreated(projectCompositeId, newBook); |
| 61 | + assertThatBookIsRetrievedById(projectCompositeId, newBook, validResponsePreConditionOnNewBook); |
| 62 | + assertThatBooksAreRetrieved(projectCompositeId, newBook); |
| 63 | + |
| 64 | + bookWithNewPrice.id = newBook.id; |
| 65 | + |
| 66 | + assertThatPriceIsChanged(projectCompositeId, bookWithNewPrice); |
| 67 | + assertThatBookIsRetrievedById(projectCompositeId, bookWithNewPrice, validResponsePreConditionOnChangedPrice); |
| 68 | + assertThatBooksAreRetrieved(projectCompositeId, bookWithNewPrice); |
| 69 | + } |
| 70 | + |
| 71 | + private void assertThatBookIsCreated(final Project bookStoreProject, final BookData newBook) { |
| 72 | + final Response response = |
| 73 | + apiOf(bookStoreProject).body(newBook).post("/authors/"+ UUID.randomUUID() +"/books"); |
| 74 | + |
| 75 | + final BookData responseBody = |
| 76 | + response.then().extract().body().as(BookData.class); |
| 77 | + |
| 78 | + newBook.id = responseBody.id; |
| 79 | + |
| 80 | + Assertions.assertEquals(Status.Created.code, response.statusCode(), "Wrong http status while creating book " + bookStoreProject); |
| 81 | + Assertions.assertEquals(newBook, responseBody, "Wrong response while creating book " + bookStoreProject); |
| 82 | + } |
| 83 | + |
| 84 | + private void assertThatBookIsRetrievedById(final Project bookStoreProject, final BookData book, final Predicate<JsonPath> responsePreCondition) { |
| 85 | + final Response response = |
| 86 | + RequestAttempt.of("Book retrieval by id").acceptResponseWhen(responsePreCondition) |
| 87 | + .perform(() -> apiOf(bookStoreProject).get("/authors/"+ UUID.randomUUID() +"/books/" + book.id)); |
| 88 | + |
| 89 | + final BookData responseBody = |
| 90 | + response.then().extract().body().as(BookData.class); |
| 91 | + |
| 92 | + Assertions.assertEquals(Status.Ok.code, response.statusCode(), "Wrong http status while retrieving book by id " + bookStoreProject); |
| 93 | + Assertions.assertEquals(book, responseBody, "Wrong response while retrieving book by id " + bookStoreProject); |
| 94 | + } |
| 95 | + |
| 96 | + private void assertThatBooksAreRetrieved(final Project bookStoreProject, final BookData newBook) { |
| 97 | + final Response response = |
| 98 | + apiOf(bookStoreProject).get("/authors/"+ UUID.randomUUID() +"/books"); |
| 99 | + |
| 100 | + final BookData[] responseBody = |
| 101 | + response.then().extract().body().as(BookData[].class); |
| 102 | + |
| 103 | + Assertions.assertEquals(Status.Ok.code, response.statusCode(), "Wrong http status while retrieving all " + bookStoreProject); |
| 104 | + Assertions.assertEquals(newBook, responseBody[0], "Wrong response while retrieving all " + bookStoreProject); |
| 105 | + } |
| 106 | + |
| 107 | + private void assertThatPriceIsChanged(final Project bookStoreProject, final BookData bookWithNewPrice) { |
| 108 | + final Response response = |
| 109 | + apiOf(bookStoreProject).body(bookWithNewPrice).patch("/authors/"+ UUID.randomUUID() +"/books/" + bookWithNewPrice.id + "/price"); |
| 110 | + |
| 111 | + final BookData responseBody = |
| 112 | + response.then().extract().body().as(BookData.class); |
| 113 | + |
| 114 | + bookWithNewPrice.id = responseBody.id; |
| 115 | + |
| 116 | + Assertions.assertEquals(Status.Ok.code, response.statusCode(), "Wrong http status while changing price " + bookStoreProject); |
| 117 | + Assertions.assertEquals(bookWithNewPrice, responseBody, "Wrong response while changing price " + bookStoreProject); |
| 118 | + } |
| 119 | + |
| 120 | + @AfterEach |
| 121 | + public void stopProject() { |
| 122 | + stopServices(); |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments