Skip to content

Commit

Permalink
fix build and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dariuszkuc committed Oct 28, 2022
1 parent 2a0952b commit 440b8a3
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 6 deletions.
9 changes: 5 additions & 4 deletions products-subgraph/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ repositories {
mavenCentral()
}

val federation_jvm_version: String = project.property("federation-jvm.version").toString()
dependencies {
implementation("com.apollographql.federation:federation-graphql-java-support:2.1.0")
implementation("com.apollographql.federation:federation-graphql-java-support:$federation_jvm_version")
implementation("org.springframework.boot:spring-boot-starter-graphql")
implementation("org.springframework.boot:spring-boot-starter-web")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework:spring-webflux")
testImplementation("org.springframework.graphql:spring-graphql-test")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.boot:spring-boot-starter-webflux")
testImplementation("org.springframework.graphql:spring-graphql-test")
}

java {
Expand Down
2 changes: 2 additions & 0 deletions products-subgraph/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
federation-jvm.version = 2.1.0
graphql-java.version = 19.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.products;

import com.example.products.model.Product;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.graphql.test.tester.HttpGraphQlTester;
import org.springframework.test.web.reactive.server.WebTestClient;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ProductsApplicationTest {

@LocalServerPort
int serverPort;

@Test
public void verifiesProductQuery() {
WebTestClient testClient = WebTestClient.bindToServer()
.baseUrl("http://localhost:" + serverPort + "/graphql")
.build();
HttpGraphQlTester tester = HttpGraphQlTester.create(testClient);

String query = """
query ProductById($productId: ID!) {
product(id: $productId) {
id
name
description
}
}
""";

// new Product("1","Saturn V", "The Original Super Heavy-Lift Rocket!"),
tester.document(query)
.variable("productId", "1")
.execute()
.path("product")
.entity(Product.class)
.isEqualTo(new Product("1","Saturn V", "The Original Super Heavy-Lift Rocket!"));
}
}
5 changes: 3 additions & 2 deletions reviews-subgraph/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ repositories {
mavenCentral()
}

val federation_jvm_version: String = project.property("federation-jvm.version").toString()
dependencies {
implementation("com.apollographql.federation:federation-graphql-java-support:2.1.0")
implementation("com.apollographql.federation:federation-graphql-java-support:$federation_jvm_version")
implementation("org.springframework.boot:spring-boot-starter-graphql")
implementation("org.springframework.boot:spring-boot-starter-web")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework:spring-webflux")
testImplementation("org.springframework.boot:spring-boot-starter-webflux")
testImplementation("org.springframework.graphql:spring-graphql-test")
}

Expand Down
2 changes: 2 additions & 0 deletions reviews-subgraph/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
federation-jvm.version = 2.1.0
graphql-java.version = 19.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.example.reviews;

import com.example.reviews.model.Review;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.graphql.test.tester.HttpGraphQlTester;
import org.springframework.test.web.reactive.server.WebTestClient;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ReviewsApplicationTest {

@LocalServerPort
int serverPort;

@Test
public void verifiesProductQuery() {
WebTestClient testClient = WebTestClient.bindToServer()
.baseUrl("http://localhost:" + serverPort + "/graphql")
.build();
HttpGraphQlTester tester = HttpGraphQlTester.create(testClient);

String query = """
query Entities($representations: [_Any!]!) {
_entities(representations: $representations) {
...on Product {
id
reviews {
id
text
starRating
}
}
}
}
""";

tester.document(query)
.variable("representations", List.of(Map.of("__typename", "Product", "id", "5")))
.execute()
.path("_entities[0].id").entity(String.class).isEqualTo("5")
.path("_entities[0].reviews[0]")
.entity(Review.class)
.isEqualTo(new Review("1050", "Amazing! Would Fly Again!", 5));
}
}

0 comments on commit 440b8a3

Please sign in to comment.