Skip to content

Commit 81ba5d3

Browse files
authored
Merge branch 'master' into BAEL-3951
2 parents ec3fd61 + dae02cc commit 81ba5d3

46 files changed

Lines changed: 1540 additions & 227 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

aws-app-sync/pom.xml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.2.6.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
12+
<groupId>com.baeldung</groupId>
13+
<artifactId>aws-app-sync</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<name>aws-app-sync</name>
16+
17+
<description>Spring Boot using AWS App Sync</description>
18+
19+
<properties>
20+
<java.version>1.8</java.version>
21+
</properties>
22+
23+
<dependencies>
24+
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-web</artifactId>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-test</artifactId>
33+
<scope>test</scope>
34+
<exclusions>
35+
<exclusion>
36+
<groupId>org.junit.vintage</groupId>
37+
<artifactId>junit-vintage-engine</artifactId>
38+
</exclusion>
39+
</exclusions>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-webflux</artifactId>
44+
</dependency>
45+
</dependencies>
46+
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-maven-plugin</artifactId>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
56+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.awsappsync;
2+
3+
import org.springframework.http.HttpMethod;
4+
import org.springframework.http.MediaType;
5+
import org.springframework.web.reactive.function.BodyInserters;
6+
import org.springframework.web.reactive.function.client.WebClient;
7+
8+
import java.nio.charset.StandardCharsets;
9+
import java.util.Map;
10+
11+
public class AppSyncClientHelper {
12+
13+
static String apiUrl = "https://m4i3b6icrrb7livfbypfspiifi.appsync-api.us-east-2.amazonaws.com";
14+
static String apiKey = "da2-bm4rpatkkrc5jfyhvvq7itjeke";
15+
static String API_KEY_HEADER = "x-api-key";
16+
17+
public static WebClient.ResponseSpec getResponseBodySpec(Map<String, Object> requestBody) {
18+
return WebClient
19+
.builder()
20+
.baseUrl(apiUrl)
21+
.defaultHeader(API_KEY_HEADER, apiKey)
22+
.defaultHeader("Content-Type", "application/json")
23+
.build()
24+
.method(HttpMethod.POST)
25+
.uri("/graphql")
26+
.body(BodyInserters.fromValue(requestBody))
27+
.accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)
28+
.acceptCharset(StandardCharsets.UTF_8)
29+
.retrieve();
30+
}
31+
32+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.awsappsync;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class AwsAppSyncApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(AwsAppSyncApplication.class, args);
11+
}
12+
13+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.baeldung.awsappsync;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
import org.springframework.web.reactive.function.client.WebClient;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
@SpringBootTest
13+
class AwsAppSyncApplicationTests {
14+
15+
@Test
16+
void givenGraphQuery_whenListEvents_thenReturnAllEvents() {
17+
18+
Map<String, Object> requestBody = new HashMap<>();
19+
requestBody.put("query", "query ListEvents {"
20+
+ " listEvents {"
21+
+ " items {"
22+
+ " id"
23+
+ " name"
24+
+ " where"
25+
+ " when"
26+
+ " description"
27+
+ " }"
28+
+ " }"
29+
+ "}");
30+
requestBody.put("variables", "");
31+
requestBody.put("operationName", "ListEvents");
32+
33+
String bodyString = AppSyncClientHelper.getResponseBodySpec(requestBody)
34+
.bodyToMono(String.class).block();
35+
36+
assertNotNull(bodyString);
37+
assertTrue(bodyString.contains("My First Event"));
38+
assertTrue(bodyString.contains("where"));
39+
assertTrue(bodyString.contains("when"));
40+
}
41+
42+
@Test
43+
void givenGraphAdd_whenMutation_thenReturnIdNameDesc() {
44+
45+
String queryString = "mutation add {"
46+
+ " createEvent("
47+
+ " name:\"My added GraphQL event\""
48+
+ " where:\"Day 2\""
49+
+ " when:\"Saturday night\""
50+
+ " description:\"Studying GraphQL\""
51+
+ " ){"
52+
+ " id"
53+
+ " name"
54+
+ " description"
55+
+ " }"
56+
+ "}";
57+
58+
Map<String, Object> requestBody = new HashMap<>();
59+
requestBody.put("query", queryString);
60+
requestBody.put("variables", "");
61+
requestBody.put("operationName", "add");
62+
63+
WebClient.ResponseSpec response = AppSyncClientHelper.getResponseBodySpec(requestBody);
64+
65+
String bodyString = response.bodyToMono(String.class).block();
66+
67+
assertNotNull(bodyString);
68+
assertTrue(bodyString.contains("My added GraphQL event"));
69+
assertFalse(bodyString.contains("where"));
70+
assertFalse(bodyString.contains("when"));
71+
}
72+
}
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
package com.baeldung.java14.record;
2-
3-
import java.util.Objects;
4-
5-
public record Person (String name, String address) {
6-
7-
public static String UNKNOWN_ADDRESS = "Unknown";
8-
public static String UNNAMED = "Unnamed";
9-
10-
public Person {
11-
Objects.requireNonNull(name);
12-
Objects.requireNonNull(address);
13-
}
14-
15-
public Person(String name) {
16-
this(name, UNKNOWN_ADDRESS);
17-
}
18-
19-
public static Person unnamed(String address) {
20-
return new Person(UNNAMED, address);
21-
}
22-
}
1+
package com.baeldung.java14.record;
2+
3+
import java.util.Objects;
4+
5+
public record Person (String name, String address) {
6+
7+
public static String UNKNOWN_ADDRESS = "Unknown";
8+
public static String UNNAMED = "Unnamed";
9+
10+
public Person {
11+
Objects.requireNonNull(name);
12+
Objects.requireNonNull(address);
13+
}
14+
15+
public Person(String name) {
16+
this(name, UNKNOWN_ADDRESS);
17+
}
18+
19+
public static Person unnamed(String address) {
20+
return new Person(UNNAMED, address);
21+
}
22+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.baeldung.java14.foreign.api;
2+
3+
import jdk.incubator.foreign.*;
4+
import org.junit.Test;
5+
6+
import static org.hamcrest.core.Is.is;
7+
import static org.junit.Assert.assertThat;
8+
9+
import java.lang.invoke.VarHandle;
10+
import java.nio.ByteOrder;
11+
12+
public class ForeignMemoryUnitTest {
13+
14+
@Test
15+
public void whenAValueIsSet_thenAccessTheValue() {
16+
long value = 10;
17+
MemoryAddress memoryAddress =
18+
MemorySegment.allocateNative(8).baseAddress();
19+
VarHandle varHandle = MemoryHandles.varHandle(long.class,
20+
ByteOrder.nativeOrder());
21+
varHandle.set(memoryAddress, value);
22+
assertThat(varHandle.get(memoryAddress), is(value));
23+
}
24+
25+
@Test
26+
public void whenMultipleValuesAreSet_thenAccessAll() {
27+
VarHandle varHandle = MemoryHandles.varHandle(int.class,
28+
ByteOrder.nativeOrder());
29+
30+
try(MemorySegment memorySegment =
31+
MemorySegment.allocateNative(100)) {
32+
MemoryAddress base = memorySegment.baseAddress();
33+
for(int i=0; i<25; i++) {
34+
varHandle.set(base.addOffset((i*4)), i);
35+
}
36+
for(int i=0; i<25; i++) {
37+
assertThat(varHandle.get(base.addOffset((i*4))), is(i));
38+
}
39+
}
40+
}
41+
42+
@Test
43+
public void whenSetValuesWithMemoryLayout_thenTheyCanBeRetrieved() {
44+
SequenceLayout sequenceLayout =
45+
MemoryLayout.ofSequence(25,
46+
MemoryLayout.ofValueBits(64, ByteOrder.nativeOrder()));
47+
VarHandle varHandle =
48+
sequenceLayout.varHandle(long.class,
49+
MemoryLayout.PathElement.sequenceElement());
50+
51+
try(MemorySegment memorySegment =
52+
MemorySegment.allocateNative(sequenceLayout)) {
53+
MemoryAddress base = memorySegment.baseAddress();
54+
for(long i=0; i<sequenceLayout.elementCount().getAsLong(); i++) {
55+
varHandle.set(base, i, i);
56+
}
57+
for(long i=0; i<sequenceLayout.elementCount().getAsLong(); i++) {
58+
assertThat(varHandle.get(base, i), is(i));
59+
}
60+
}
61+
}
62+
63+
@Test
64+
public void whenSlicingMemorySegment_thenTheyCanBeAccessedIndividually() {
65+
MemoryAddress memoryAddress =
66+
MemorySegment.allocateNative(12).baseAddress();
67+
MemoryAddress memoryAddress1 =
68+
memoryAddress.segment().asSlice(0,4).baseAddress();
69+
MemoryAddress memoryAddress2 =
70+
memoryAddress.segment().asSlice(4,4).baseAddress();
71+
MemoryAddress memoryAddress3 =
72+
memoryAddress.segment().asSlice(8,4).baseAddress();
73+
74+
VarHandle intHandle =
75+
MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder());
76+
intHandle.set(memoryAddress1, Integer.MIN_VALUE);
77+
intHandle.set(memoryAddress2, 0);
78+
intHandle.set(memoryAddress3, Integer.MAX_VALUE);
79+
80+
assertThat(intHandle.get(memoryAddress1), is(Integer.MIN_VALUE));
81+
assertThat(intHandle.get(memoryAddress2), is(0));
82+
assertThat(intHandle.get(memoryAddress3), is(Integer.MAX_VALUE));
83+
}
84+
}

0 commit comments

Comments
 (0)