You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by da...@apache.org on 2024/02/12 19:07:39 UTC
(causeway) 02/07: CAUSEWAY-3676: wip, adds tests for scenario
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 ca70d48ff49795454a3e826fc877be719491e1b4
Author: danhaywood <da...@haywood-associates.co.uk>
AuthorDate: Mon Feb 12 08:34:45 2024 +0000
CAUSEWAY-3676: wip, adds tests for scenario
---
.../viewer/graphql/model/domain/GqlvScenario.java | 23 ++++++++--
.../graphql/model/domain/GqlvScenarioGiven.java | 1 -
.../graphql/model/domain/GqlvScenarioName.java | 43 +++++++++++++++++++
.../viewer/graphql/model/domain/Scenario.java | 44 ++++++++++++++++++-
.../e2e/scenario/Calculator_IntegTest.concat._.gql | 12 ++++++
.../test/e2e/scenario/Calculator_IntegTest.java | 50 ++++++++++++++++++++++
viewers/graphql/test/src/test/resources/schema.gql | 3 +-
.../graphql/viewer/toplevel/GqlvTopLevelQuery.java | 3 +-
8 files changed, 171 insertions(+), 8 deletions(-)
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 162f30e505..a9eceac23b 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
@@ -18,8 +18,10 @@
*/
package org.apache.causeway.viewer.graphql.model.domain;
+import graphql.Scalars;
import graphql.schema.DataFetcher;
import graphql.schema.FieldCoordinates;
+import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLObjectType;
@@ -29,12 +31,14 @@ import static graphql.schema.GraphQLObjectType.newObject;
import org.apache.causeway.viewer.graphql.model.context.Context;
+import graphql.schema.GraphQLScalarType;
+
import lombok.Getter;
/**
* Exposes a domain service (view model or entity) via the GQL viewer.
*/
-public class GqlvScenario implements GqlvScenarioGiven.Holder {
+public class GqlvScenario implements GqlvScenarioName.Holder, GqlvScenarioGiven.Holder {
private final Holder holder;
private final Context context;
@@ -42,6 +46,7 @@ public class GqlvScenario implements GqlvScenarioGiven.Holder {
private final GraphQLObjectType.Builder gqlObjectTypeBuilder;
private final Scenario scenarioPojo;
+ private final GqlvScenarioName scenarioName;
private final GqlvScenarioGiven scenarioGiven;
@Getter private GraphQLFieldDefinition field;
@@ -56,13 +61,21 @@ public class GqlvScenario implements GqlvScenarioGiven.Holder {
this.scenarioPojo = context.serviceRegistry.lookupService(Scenario.class).orElseThrow();
- this.gqlObjectTypeBuilder = newObject().name("Scenario"); // TODO: probably need to change this type?
+ this.gqlObjectTypeBuilder = newObject().name("Scenario");
+ this.scenarioName = new GqlvScenarioName(this, context);
this.scenarioGiven = new GqlvScenarioGiven(this, context);
this.gqlObjectType = gqlObjectTypeBuilder.build();
- this.field = new GraphQLFieldDefinition.Builder().name("Scenario").type(gqlObjectType).build();
+ this.field = new GraphQLFieldDefinition.Builder()
+ .name("Scenario")
+ .type(gqlObjectType)
+ .argument(new GraphQLArgument.Builder()
+ .name("name")
+ .type(Scalars.GraphQLString)
+ )
+ .build();
holder.addField(field);
}
@@ -83,7 +96,9 @@ public class GqlvScenario implements GqlvScenarioGiven.Holder {
context.codeRegistryBuilder.dataFetcher(
holder.coordinatesFor(getField()),
(DataFetcher<Object>) environment -> scenarioPojo);
- // add
+
+ scenarioName.addDataFetchers();
+ scenarioGiven.addDataFetchers();
}
diff --git a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenarioGiven.java b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenarioGiven.java
index c16b4629e0..4ebfa15b05 100644
--- a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenarioGiven.java
+++ b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenarioGiven.java
@@ -49,7 +49,6 @@ public class GqlvScenarioGiven implements GqlvDomainService.Holder, GqlvDomainOb
}
});
- // add services to top-level query
context.objectSpecifications().forEach(objectSpec -> {
if (Objects.requireNonNull(objectSpec.getBeanSort()) == BeanSort.MANAGED_BEAN_CONTRIBUTING) { // @DomainService
context.serviceRegistry.lookupBeanById(objectSpec.getLogicalTypeName())
diff --git a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenarioName.java b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenarioName.java
new file mode 100644
index 0000000000..d5164f23e2
--- /dev/null
+++ b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/GqlvScenarioName.java
@@ -0,0 +1,43 @@
+package org.apache.causeway.viewer.graphql.model.domain;
+
+import graphql.Scalars;
+import graphql.schema.DataFetcher;
+import graphql.schema.DataFetchingEnvironment;
+import graphql.schema.GraphQLFieldDefinition;
+
+import static graphql.schema.GraphQLObjectType.newObject;
+
+import org.apache.causeway.viewer.graphql.model.context.Context;
+
+public class GqlvScenarioName {
+
+ private final Holder holder;
+ private final Context context;
+ private final GraphQLFieldDefinition field;
+
+ public GqlvScenarioName(
+ final GqlvScenarioName.Holder holder,
+ final Context context) {
+
+ this.holder = holder;
+ this.context = context;
+
+ this.field = GraphQLFieldDefinition.newFieldDefinition().name("Name").type(Scalars.GraphQLString).build();
+ this.holder.addField(field);
+ }
+
+ public void addDataFetchers() {
+ context.codeRegistryBuilder.dataFetcher(
+ holder.coordinatesFor(field),
+ (DataFetcher<Object>) environment -> name(environment));
+ }
+
+ private String name(DataFetchingEnvironment environment) {
+ return context.serviceRegistry.lookupService(Scenario.class).map(Scenario::getName).orElseThrow();
+ }
+
+ public interface Holder
+ extends GqlvHolder {
+ }
+
+}
diff --git a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/Scenario.java b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/Scenario.java
index 0b67dbddcb..1a868f1432 100644
--- a/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/Scenario.java
+++ b/viewers/graphql/model/src/main/java/org/apache/causeway/viewer/graphql/model/domain/Scenario.java
@@ -1,9 +1,51 @@
package org.apache.causeway.viewer.graphql.model.domain;
-import org.apache.causeway.applib.annotation.InteractionScope;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import lombok.Setter;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.inject.Inject;
+
+import org.apache.causeway.applib.annotation.Programmatic;
+import org.apache.causeway.applib.services.bookmark.Bookmark;
+
+import org.apache.causeway.applib.services.bookmark.BookmarkService;
import org.springframework.stereotype.Service;
+/**
+ * Holds the state of an executing scenario.
+ *
+ * <p>
+ * Note that although this is a singleton, it is <i>not</i> thread-safe.
+ * </p>
+ */
@Service
+@RequiredArgsConstructor(onConstructor_ = {@Inject})
public class Scenario {
+
+ private final BookmarkService bookmarkService;
+
+ @Getter @Setter private String name;
+
+ private final Map<String, Bookmark> references = new HashMap<>();
+
+ @Programmatic
+ public void putReference(String reference, final Object pojo) {
+ Bookmark bookmark = bookmarkService.bookmarkFor(pojo).orElseThrow();
+ references.put(reference, bookmark);
+ }
+
+ @Programmatic
+ public Object getReference(String reference) {
+ return bookmarkService.lookup(references.get(reference)).orElseThrow();
+ }
+
+ @Programmatic
+ public void clearReferences() {
+ references.clear();
+ }
}
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
new file mode 100644
index 0000000000..a07ae57dbf
--- /dev/null
+++ b/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/e2e/scenario/Calculator_IntegTest.concat._.gql
@@ -0,0 +1,12 @@
+{
+ Scenario(name: "Fizz buzz"){
+ Name
+ Given {
+ 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/Calculator_IntegTest.java b/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/e2e/scenario/Calculator_IntegTest.java
new file mode 100644
index 0000000000..ab001059ca
--- /dev/null
+++ b/viewers/graphql/test/src/test/java/org/apache/causeway/viewer/graphql/viewer/test/e2e/scenario/Calculator_IntegTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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 org.apache.causeway.viewer.graphql.viewer.test.e2e.Abstract_IntegTest;
+
+import org.approvaltests.Approvals;
+import org.approvaltests.reporters.DiffReporter;
+import org.approvaltests.reporters.UseReporter;
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.Test;
+
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ActiveProfiles;
+
+
+//NOT USING @Transactional since we are running server within same transaction otherwise
+@Order(30)
+@ActiveProfiles("test")
+@DirtiesContext
+public class Calculator_IntegTest extends Abstract_IntegTest {
+
+
+ @Test
+ @UseReporter(DiffReporter.class)
+ void concat() 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 ab93b891dd..c3c1d4042e 100644
--- a/viewers/graphql/test/src/test/resources/schema.gql
+++ b/viewers/graphql/test/src/test/resources/schema.gql
@@ -63,7 +63,7 @@ type Given {
}
type Query {
- Scenario: Scenario
+ 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
@@ -113,6 +113,7 @@ type Query {
type Scenario {
Given: Given
+ Name: String
}
type causeway_applib_DomainObjectList {
diff --git a/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/toplevel/GqlvTopLevelQuery.java b/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/toplevel/GqlvTopLevelQuery.java
index ff5ca87aca..7e7946cf1b 100644
--- a/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/toplevel/GqlvTopLevelQuery.java
+++ b/viewers/graphql/viewer/src/main/java/org/apache/causeway/viewer/graphql/viewer/toplevel/GqlvTopLevelQuery.java
@@ -80,6 +80,7 @@ public class GqlvTopLevelQuery implements GqlvDomainService.Holder, GqlvDomainOb
}
public void addDataFetchers() {
+
domainServices.forEach(domainService -> {
boolean actionsAdded = domainService.hasActions();
if (actionsAdded) {
@@ -90,7 +91,7 @@ public class GqlvTopLevelQuery implements GqlvDomainService.Holder, GqlvDomainOb
domainObjects.forEach(GqlvDomainObject::addDataFetchers);
+ scenario.addDataFetchers();
}
-
}