This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch CAUSEWAY-3676
in repository https://gitbox.apache.org/repos/asf/causeway.git

commit 14e3a77893190a4b3da517f53829858b04171bc4
Author: danhaywood <[email protected]>
AuthorDate: Mon Feb 12 23:38:38 2024 +0000

    CAUSEWAY-3676: adds given/when/then
---
 .../viewer/graphql/model/domain/GqlvAbstract.java  | 24 +++++++++
 .../graphql/model/domain/GqlvAbstractCustom.java   | 16 +++++-
 .../viewer/graphql/model/domain/GqlvScenario.java  | 25 +++++++--
 .../graphql/model/domain/GqlvScenarioStep.java     |  9 ++--
 .../viewer/graphql/model/domain/Parent.java        |  2 +
 .../e2e/scenario/Calculator_IntegTest.concat._.gql |  2 +-
 ...IntegTest.find_department_and_change_name._.gql | 42 +++++++++++++++
 ...t.find_department_and_change_name.approved.json | 44 +++++++++++++++
 .../test/e2e/scenario/Department_IntegTest.java    | 62 ++++++++++++++++++++++
 viewers/graphql/test/src/test/resources/schema.gql | 18 ++++---
 10 files changed, 223 insertions(+), 21 deletions(-)

diff --git 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAbstract.java
 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAbstract.java
index 51b98f99a1..66662c2cd7 100644
--- 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAbstract.java
+++ 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAbstract.java
@@ -31,6 +31,14 @@ public abstract class GqlvAbstract {
 
     protected final Context context;
 
+    /**
+     * Usually populated, being the field that will be added to the parent's 
type
+     *
+     * <p>
+     *     However, {@link GqlvScenarioStep} is an exception; it doesn't 
populate this field - instead
+     *     {@link GqlvAbstractCustom#newField(String)} is used to create 
multiple fields for the type.
+     * </p>
+     */
     @Getter
     private GraphQLFieldDefinition field;
 
@@ -58,6 +66,22 @@ public abstract class GqlvAbstract {
         addDataFetchersForChildren();
     }
 
+
+    /**
+     * Use the provided fieldName rather than that of {@link #getField()}.
+     *
+     * <p>
+     *     Used to allow multiple fields of the same type, eg {@link 
GqlvScenarioStep}.
+     * </p>
+     */
+    public final void addDataFetcher(Parent parent, String fieldName) {
+        context.codeRegistryBuilder.dataFetcher(
+                parent.coordinatesFor(fieldName),
+                this::fetchData);
+
+        addDataFetchersForChildren();
+    }
+
     protected void addDataFetchersForChildren() {
     }
 
diff --git 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAbstractCustom.java
 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAbstractCustom.java
index 6153389225..9847453041 100644
--- 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAbstractCustom.java
+++ 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvAbstractCustom.java
@@ -74,10 +74,14 @@ public abstract class GqlvAbstractCustom extends 
GqlvAbstract implements Parent
             buildObjectType();
         }
 
-        setField(newFieldDefinition()
+        setField(newField(fieldName));
+    }
+
+    public GraphQLFieldDefinition newField(String fieldName) {
+        return newFieldDefinition()
                 .name(fieldName)
                 .type(getGqlObjectType())
-                .build());
+                .build();
     }
 
     protected final GraphQLObjectType buildObjectType() {
@@ -96,4 +100,12 @@ public abstract class GqlvAbstractCustom extends 
GqlvAbstract implements Parent
         return FieldCoordinates.coordinates(gqlObjectType, field);
     }
 
+    public final FieldCoordinates coordinatesFor(final String fieldName) {
+        if (gqlObjectType == null) {
+            throw new IllegalStateException(
+                    String.format("GQL Object Type for '%s' not yet built", 
typeName));
+        }
+        return FieldCoordinates.coordinates(gqlObjectType, fieldName);
+    }
+
 }
diff --git 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenario.java
 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenario.java
index a71154394d..3ab8fa478c 100644
--- 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenario.java
+++ 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenario.java
@@ -34,7 +34,7 @@ public class GqlvScenario
     public static final String KEY_SCENARIO_NAME = String.format("%s#%s", 
GqlvScenario.class.getName(), "name");
 
     private final GqlvScenarioName scenarioName;
-    private final GqlvScenarioStep scenarioGiven;
+    private final GqlvScenarioStep scenarioStep;
 
     public GqlvScenario(
             final Context context) {
@@ -42,8 +42,17 @@ public class GqlvScenario
 
         this.scenarioName = new GqlvScenarioName(context);
         addChildField(scenarioName.getField());
-        this.scenarioGiven = new GqlvScenarioStep("Given", "Given", context);
-        addChildField(scenarioGiven.getField());
+
+        this.scenarioStep = new GqlvScenarioStep(context);
+        addChildField(scenarioStep.newField("Given"));
+//        addChildField(scenarioStep.newField("GivenAlso"));
+//        addChildField(scenarioStep.newField("GivenAlsoAlso"));
+//        addChildField(scenarioStep.newField("GivenAlsoAlsoAlso"));
+        addChildField(scenarioStep.newField("When"));
+        addChildField(scenarioStep.newField("Then"));
+//        addChildField(scenarioStep.newField("ThenAlso"));
+//        addChildField(scenarioStep.newField("ThenAlsoAlso"));
+//        addChildField(scenarioStep.newField("ThenAlsoAlsoAlso"));
 
         buildObjectType();
 
@@ -62,7 +71,15 @@ public class GqlvScenario
     @Override
     protected void addDataFetchersForChildren() {
         scenarioName.addDataFetcher(this);
-        scenarioGiven.addDataFetcher(this);
+        scenarioStep.addDataFetcher(this, "Given");
+//        scenarioStep.addDataFetcher(this, "GivenAlso");
+//        scenarioStep.addDataFetcher(this, "GivenAlsoAlso");
+//        scenarioStep.addDataFetcher(this, "GivenAlsoAlsoAlso");
+        scenarioStep.addDataFetcher(this, "When");
+        scenarioStep.addDataFetcher(this, "Then");
+//        scenarioStep.addDataFetcher(this, "ThenAlso");
+//        scenarioStep.addDataFetcher(this, "ThenAlsoAlso");
+//        scenarioStep.addDataFetcher(this, "ThenAlsoAlsoAlso");
     }
 
     @Override
diff --git 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenarioStep.java
 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenarioStep.java
index df12d0366e..a852daaa71 100644
--- 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenarioStep.java
+++ 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenarioStep.java
@@ -16,11 +16,8 @@ public class GqlvScenarioStep
     private final List<GqlvDomainService> domainServices = new ArrayList<>();
     private final List<GqlvDomainObject> domainObjects = new ArrayList<>();
 
-    public GqlvScenarioStep(
-            final String typeName,
-            final String fieldName,
-            final Context context) {
-        super(typeName, context);
+    public GqlvScenarioStep(final Context context) {
+        super("ScenarioStep", context);
 
         context.objectSpecifications().forEach(objectSpec -> {
             switch (objectSpec.getBeanSort()) {
@@ -51,7 +48,7 @@ public class GqlvScenarioStep
             addChildField(domainObject.getField());
         }
 
-        buildObjectTypeAndField(fieldName);
+        buildObjectType();
     }
 
 
diff --git 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/Parent.java
 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/Parent.java
index a03de13e56..2bf6990a5d 100644
--- 
a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/Parent.java
+++ 
b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/Parent.java
@@ -27,4 +27,6 @@ public interface Parent {
      * Called while registering the fetchers.
      */
     FieldCoordinates coordinatesFor(GraphQLFieldDefinition fieldDefinition);
+
+    FieldCoordinates coordinatesFor(String fieldName);
 }
diff --git 
a/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/e2e/scenario/Calculator_IntegTest.concat._.gql
 
b/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/e2e/scenario/Calculator_IntegTest.concat._.gql
index 88cf551654..4bf72496ad 100644
--- 
a/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/e2e/scenario/Calculator_IntegTest.concat._.gql
+++ 
b/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/e2e/scenario/Calculator_IntegTest.concat._.gql
@@ -1,7 +1,7 @@
 {
   Scenario(name: "Fizz and buzz are concatenated together"){
     Name
-    Given {
+    When {
       university_calc_Calculator {
         concat {
           invoke(prefix: "Fizz", suffix: "Buzz")
diff --git 
a/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/e2e/scenario/Department_IntegTest.find_department_and_change_name._.gql
 
b/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/e2e/scenario/Department_IntegTest.find_department_and_change_name._.gql
new file mode 100644
index 0000000000..e143491e46
--- /dev/null
+++ 
b/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/e2e/scenario/Department_IntegTest.find_department_and_change_name._.gql
@@ -0,0 +1,42 @@
+{
+  Scenario(name: "Find department and change its name"){
+    Name
+    Given {
+      university_dept_Departments {
+        findDepartmentByName {
+          invoke(name: "Classics") {
+            name {
+              get
+            }
+          }
+        }
+      }
+    }
+    When {
+      university_dept_Departments {
+        findDepartmentByName {
+          invoke(name: "Classics") {
+            changeName {
+              invokeIdempotent(newName: "Ancient History") {
+                name {
+                  get
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+    Then {
+      university_dept_Departments {
+        findDepartmentByName {
+          invoke(name: "Ancient History") {
+            name {
+              get
+            }
+          }
+        }
+      }
+    }
+  }
+}
diff --git 
a/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/e2e/scenario/Department_IntegTest.find_department_and_change_name.approved.json
 
b/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/e2e/scenario/Department_IntegTest.find_department_and_change_name.approved.json
new file mode 100644
index 0000000000..fbc3a7ca8f
--- /dev/null
+++ 
b/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/e2e/scenario/Department_IntegTest.find_department_and_change_name.approved.json
@@ -0,0 +1,44 @@
+{
+  "data" : {
+    "Scenario" : {
+      "Name" : "Find department and change its name",
+      "Given" : {
+        "university_dept_Departments" : {
+          "findDepartmentByName" : {
+            "invoke" : {
+              "name" : {
+                "get" : "Classics"
+              }
+            }
+          }
+        }
+      },
+      "When" : {
+        "university_dept_Departments" : {
+          "findDepartmentByName" : {
+            "invoke" : {
+              "changeName" : {
+                "invokeIdempotent" : {
+                  "name" : {
+                    "get" : "Ancient History"
+                  }
+                }
+              }
+            }
+          }
+        }
+      },
+      "Then" : {
+        "university_dept_Departments" : {
+          "findDepartmentByName" : {
+            "invoke" : {
+              "name" : {
+                "get" : "Ancient History"
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+}
\ No newline at end of file
diff --git 
a/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/e2e/scenario/Department_IntegTest.java
 
b/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/e2e/scenario/Department_IntegTest.java
new file mode 100644
index 0000000000..ae9c052f89
--- /dev/null
+++ 
b/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/e2e/scenario/Department_IntegTest.java
@@ -0,0 +1,62 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.causeway.viewer.graphql.viewer.test.e2e.scenario;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import org.approvaltests.Approvals;
+import org.approvaltests.reporters.DiffReporter;
+import org.approvaltests.reporters.UseReporter;
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
+
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.transaction.annotation.Propagation;
+
+import org.apache.causeway.commons.internal.base._Strings;
+import org.apache.causeway.commons.internal.collections._Maps;
+import org.apache.causeway.viewer.graphql.viewer.test.domain.dept.Department;
+import org.apache.causeway.viewer.graphql.viewer.test.domain.dept.DeptHead;
+import org.apache.causeway.viewer.graphql.viewer.test.e2e.Abstract_IntegTest;
+
+import lombok.val;
+
+
+//NOT USING @Transactional since we are running server within same transaction 
otherwise
+@Order(40)
+@ActiveProfiles("test")
+public class Department_IntegTest extends Abstract_IntegTest {
+
+    @Test
+    @UseReporter(DiffReporter.class)
+    void find_department_and_change_name() throws Exception {
+
+        // when, then
+        Approvals.verify(submit(), jsonOptions());
+    }
+
+
+}
diff --git a/viewers/graphql/test/src/test/resources/schema.gql 
b/viewers/graphql/test/src/test/resources/schema.gql
index d0e4b40449..5bef2dd3be 100644
--- a/viewers/graphql/test/src/test/resources/schema.gql
+++ b/viewers/graphql/test/src/test/resources/schema.gql
@@ -22,7 +22,8 @@ directive @specifiedBy(
     url: String!
   ) on SCALAR
 
-type Given {
+type Query {
+  Scenario(name: String): Scenario
   _gqlv_lookup__causeway_applib_DomainObjectList(object: 
causeway_applib_DomainObjectList__gqlv_input): causeway_applib_DomainObjectList
   _gqlv_lookup__causeway_applib_FacetGroupNode(object: 
causeway_applib_FacetGroupNode__gqlv_input): causeway_applib_FacetGroupNode
   _gqlv_lookup__causeway_applib_ParameterNode(object: 
causeway_applib_ParameterNode__gqlv_input): causeway_applib_ParameterNode
@@ -70,8 +71,14 @@ type Given {
   university_dept_Staff: university_dept_Staff
 }
 
-type Query {
-  Scenario(name: String): Scenario
+type Scenario {
+  Given: ScenarioStep
+  Name: String
+  Then: ScenarioStep
+  When: ScenarioStep
+}
+
+type ScenarioStep {
   _gqlv_lookup__causeway_applib_DomainObjectList(object: 
causeway_applib_DomainObjectList__gqlv_input): causeway_applib_DomainObjectList
   _gqlv_lookup__causeway_applib_FacetGroupNode(object: 
causeway_applib_FacetGroupNode__gqlv_input): causeway_applib_FacetGroupNode
   _gqlv_lookup__causeway_applib_ParameterNode(object: 
causeway_applib_ParameterNode__gqlv_input): causeway_applib_ParameterNode
@@ -119,11 +126,6 @@ type Query {
   university_dept_Staff: university_dept_Staff
 }
 
-type Scenario {
-  Given: Given
-  Name: String
-}
-
 type causeway_applib_DomainObjectList {
   _gqlv_meta: causeway_applib_DomainObjectList__gqlv_meta
   actionArguments: 
causeway_applib_DomainObjectList__actionArguments__gqlv_property

Reply via email to