From 6bf0d97d62bdfe986fde782544d47ebba9cb9705 Mon Sep 17 00:00:00 2001 From: Kyle Doyle Date: Fri, 17 Apr 2020 19:50:36 -0400 Subject: [PATCH] Bael-3893 AWS AppSync with Spring Boot --- aws-app-sync/pom.xml | 15 +++- .../src/main/resources/application.properties | 1 - .../AwsAppSyncApplicationTests.java | 90 ++++++++++++++++++- 3 files changed, 102 insertions(+), 4 deletions(-) delete mode 100644 aws-app-sync/src/main/resources/application.properties diff --git a/aws-app-sync/pom.xml b/aws-app-sync/pom.xml index af02307e66..d30cfc96a4 100644 --- a/aws-app-sync/pom.xml +++ b/aws-app-sync/pom.xml @@ -8,17 +8,26 @@ 2.2.6.RELEASE + com.baeldung aws-app-sync 0.0.1-SNAPSHOT aws-app-sync - Demo project for Spring Boot using AWS App Sync + + Spring Boot using AWS App Sync 1.8 + + + com.amazonaws + aws-java-sdk-appsync + 1.11.762 + + org.springframework.boot spring-boot-starter-web @@ -35,6 +44,10 @@ + + org.springframework.boot + spring-boot-starter-webflux + diff --git a/aws-app-sync/src/main/resources/application.properties b/aws-app-sync/src/main/resources/application.properties deleted file mode 100644 index 8b13789179..0000000000 --- a/aws-app-sync/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ - diff --git a/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java b/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java index 7b82393865..15bcc6e9c9 100644 --- a/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java +++ b/aws-app-sync/src/test/java/com/baeldung/awsappsync/AwsAppSyncApplicationTests.java @@ -1,13 +1,99 @@ package com.baeldung.awsappsync; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.client.WebClient; + +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; @SpringBootTest class AwsAppSyncApplicationTests { - @Test - void contextLoads() { + static String apiUrl = "https://m4i3b6icrrb7livfbypfspiifi.appsync-api.us-east-2.amazonaws.com"; + static String apiKey = "da2-es2s6oj4mzhbxk5yu26ss2ruj4"; + static String API_KEY_HEADER = "x-api-key"; + + static WebClient.RequestBodySpec requestBodySpec; + + @BeforeAll + static void setUp() { + requestBodySpec = WebClient + .builder() + .baseUrl(apiUrl) + .defaultHeader(API_KEY_HEADER, apiKey) + .build() + .method(HttpMethod.POST) + .uri("/graphql"); } + @Test + void testGraphQuery() { + + Map requestBody = new HashMap<>(); + requestBody.put("query", "query ListEvents {\n" + + " listEvents {\n" + + " items {\n" + + " id\n" + + " name\n" + + " where\n" + + " when\n" + + " description\n" + + " }\n" + + " }\n" + + "}"); + requestBody.put("variables", ""); + requestBody.put("operationName", "ListEvents"); + + WebClient.ResponseSpec response = requestBodySpec + .body(BodyInserters.fromValue(requestBody)) + .accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML) + .acceptCharset(StandardCharsets.UTF_8) + .retrieve(); + + String bodyString = response.bodyToMono(String.class).block(); + + assertTrue(bodyString != null && bodyString.contains("My First Event")); + } + + @Test + void testGraphAdd() { + + String queryString = "mutation add {\n" + + " createEvent(\n" + + " name:\"My added GraphQL event\"\n" + + " where:\"Day 2\"\n" + + " when:\"Saturday night\"\n" + + " description:\"Studying GraphQL\"\n" + + " ){\n" + + " id\n" + + " name\n" + + " where\n" + + " when\n" + + " description\n" + + " }\n" + + "}"; + + + Map requestBody = new HashMap<>(); + requestBody.put("query", queryString); + requestBody.put("variables", ""); + requestBody.put("operationName", "add"); + + WebClient.ResponseSpec response = requestBodySpec + .body(BodyInserters.fromValue(requestBody)) + .accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML) + .acceptCharset(StandardCharsets.UTF_8) + .retrieve(); + + String bodyString = response.bodyToMono(String.class).block(); + assertTrue(bodyString != null && bodyString.contains("Day 2")); + } }