Skip to content

Commit

Permalink
add execution test
Browse files Browse the repository at this point in the history
  • Loading branch information
raminqaf committed Aug 23, 2022
1 parent d5de0ec commit c27f78e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import java.util.List;
import java.util.Map;
import lombok.Builder;
import lombok.Data;
import lombok.Value;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

Expand Down Expand Up @@ -115,6 +115,43 @@ void shouldExecuteQueryWithSingleField(final TestInfo testInfo) throws IOExcepti
.containsEntry("productId", "product");
}

@Test
void shouldExecuteRange(final TestInfo testInfo) throws IOException {
final String name = testInfo.getTestMethod().orElseThrow().getName();
final Path schemaPath = workingDirectory.resolve(name + ".graphql");
final Path queryPath = workingDirectory.resolve(name + "Query.graphql");

final GraphQLSchema schema = this.generator.create(Files.readString(schemaPath));
final GraphQL graphQL = GraphQL.newGraphQL(schema).build();

final DataFetcherClient<?> dataFetcherClient = this.supplier.getClients().get("user-request-range");

final List<?> userRequests = List.of(
UserRequest.builder().userId(1).timestamp(1).requests(5).build(),
UserRequest.builder().userId(1).timestamp(2).requests(10).build(),
UserRequest.builder().userId(1).timestamp(3).requests(8).build()
);

when(dataFetcherClient.fetchRange("1", "1", "3")).thenAnswer(invocation -> userRequests);

final ExecutionResult executionResult = graphQL.execute(Files.readString(queryPath));

assertThat(executionResult.getErrors()).isEmpty();

final Map<String, List<Map<String, Object>>> data = executionResult.getData();
assertThat(data.get("userRequests"))
.isNotNull()
.hasSize(3)
.satisfies(userRequest -> {
assertThat(userRequest.get(0).get("requests")).isEqualTo(5);

}).satisfies(userRequest -> {
assertThat(userRequest.get(1).get("requests")).isEqualTo(10);
}).satisfies(userRequest -> {
assertThat(userRequest.get(2).get("requests")).isEqualTo(8);
});
}

@Test
void shouldExecuteListQueryWithSingleFieldAndModification(final TestInfo testInfo) throws IOException {
final String name = testInfo.getTestMethod().orElseThrow().getName();
Expand Down Expand Up @@ -259,7 +296,8 @@ void shouldThrowErrorForNonNullableField() throws IOException {
.hasSize(1)
.first()
.satisfies(error -> {
assertThat(error.getMessage()).startsWith("The field at path '/findPurchase/productId' was declared as a non null type");
assertThat(error.getMessage()).startsWith(
"The field at path '/findPurchase/productId' was declared as a non null type");
assertThat(error.getPath()).containsExactly("findPurchase", "productId");
});
}
Expand Down Expand Up @@ -290,29 +328,43 @@ private void registerTopics() {
"url-topic",
new TopicData("url-topic", TopicWriteType.MUTABLE, QuickTopicType.STRING, QuickTopicType.AVRO, "")
).blockingAwait();

this.registryClient.register(
"user-request-range",
new TopicData("user-request-range", TopicWriteType.MUTABLE, QuickTopicType.INTEGER, QuickTopicType.AVRO,
"")
).blockingAwait();
}

@Data
@Value
@Builder
private static class Purchase {
private String purchaseId;
private String productId;
private int amount;
String purchaseId;
String productId;
int amount;
}

@Data
@Value
@Builder
private static class Product {
private String productId;
private String name;
private String description;
private Price price;
String productId;
String name;
String description;
Price price;
}

@Data
@Value
@Builder
private static class Price {
private double total;
private String currency;
double total;
String currency;
}

@Value
@Builder
private static class UserRequest {
int userId;
int timestamp;
int requests;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type Query {
userRequests(
userId: Int
timestampFrom: Int
timestampTo: Int
): [UserRequests] @topic(name: "user-request-range",
keyArgument: "userId",
rangeFrom: "timestampFrom",
rangeTo: "timestampTo")
}

type UserRequests {
userId: Int
timestamp: Int
requests: Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
userRequests(userId: 1, timestampFrom: 1, timestampTo: 3) {
requests
}
}

0 comments on commit c27f78e

Please sign in to comment.