Skip to content

Commit 7efa796

Browse files
authored
test: add Spanner JSON test suite (#1376)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/java-spanner/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes #<issue_number_goes_here> ☕️
1 parent c320f47 commit 7efa796

File tree

284 files changed

+438
-0
lines changed

Some content is hidden

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

284 files changed

+438
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.spanner.it;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertThrows;
21+
import static org.junit.Assume.assumeFalse;
22+
23+
import com.google.cloud.spanner.Database;
24+
import com.google.cloud.spanner.DatabaseClient;
25+
import com.google.cloud.spanner.IntegrationTestEnv;
26+
import com.google.cloud.spanner.Mutation;
27+
import com.google.cloud.spanner.ParallelIntegrationTest;
28+
import com.google.cloud.spanner.ResultSet;
29+
import com.google.cloud.spanner.SpannerException;
30+
import com.google.cloud.spanner.Statement;
31+
import com.google.cloud.spanner.Value;
32+
import com.google.cloud.spanner.testing.EmulatorSpannerHelper;
33+
import com.google.cloud.spanner.testing.RemoteSpannerHelper;
34+
import com.google.common.io.Resources;
35+
import java.io.File;
36+
import java.io.IOException;
37+
import java.nio.charset.StandardCharsets;
38+
import java.nio.file.Files;
39+
import java.nio.file.Path;
40+
import java.nio.file.Paths;
41+
import java.util.ArrayList;
42+
import java.util.Collections;
43+
import java.util.List;
44+
import java.util.concurrent.atomic.AtomicLong;
45+
import java.util.stream.Collectors;
46+
import org.junit.BeforeClass;
47+
import org.junit.ClassRule;
48+
import org.junit.Test;
49+
import org.junit.experimental.categories.Category;
50+
import org.junit.runner.RunWith;
51+
import org.junit.runners.JUnit4;
52+
53+
@Category(ParallelIntegrationTest.class)
54+
@RunWith(JUnit4.class)
55+
public class ITJsonWriteReadTest {
56+
57+
private static final String RESOURCES_DIR = "com/google/cloud/spanner/it/";
58+
private static final String VALID_JSON_DIR = "valid";
59+
private static final String INVALID_JSON_DIR = "invalid";
60+
61+
private static final String TABLE_NAME = "TestTable";
62+
63+
@ClassRule public static IntegrationTestEnv env = new IntegrationTestEnv();
64+
65+
private static DatabaseClient databaseClient;
66+
67+
@BeforeClass
68+
public static void beforeClass() {
69+
final RemoteSpannerHelper testHelper = env.getTestHelper();
70+
if (!EmulatorSpannerHelper.isUsingEmulator()) {
71+
final Database database =
72+
testHelper.createTestDatabase(
73+
"CREATE TABLE "
74+
+ TABLE_NAME
75+
+ "("
76+
+ "Id INT64 NOT NULL,"
77+
+ "json JSON"
78+
+ ") PRIMARY KEY (Id)");
79+
databaseClient = testHelper.getDatabaseClient(database);
80+
}
81+
}
82+
83+
@Test
84+
public void testWriteValidJsonValues() throws IOException {
85+
assumeFalse("Emulator does not yet support JSON", EmulatorSpannerHelper.isUsingEmulator());
86+
87+
List<String> resources = getJsonFilePaths(RESOURCES_DIR + File.separator + VALID_JSON_DIR);
88+
89+
long id = 0L;
90+
List<Mutation> mutations = new ArrayList<>();
91+
for (String resource : resources) {
92+
String jsonStr =
93+
Resources.toString(
94+
Resources.getResource(this.getClass(), VALID_JSON_DIR + File.separator + resource),
95+
StandardCharsets.UTF_8);
96+
Mutation mutation =
97+
Mutation.newInsertBuilder(TABLE_NAME)
98+
.set("Id")
99+
.to(id++)
100+
.set("json")
101+
.to(Value.json(jsonStr))
102+
.build();
103+
mutations.add(mutation);
104+
}
105+
databaseClient.write(mutations);
106+
107+
try (ResultSet resultSet =
108+
databaseClient
109+
.singleUse()
110+
.executeQuery(Statement.of("SELECT COUNT(*) FROM " + TABLE_NAME))) {
111+
resultSet.next();
112+
assertEquals(resultSet.getLong(0), resources.size());
113+
}
114+
}
115+
116+
@Test
117+
public void testWriteAndReadInvalidJsonValues() throws IOException {
118+
assumeFalse("Emulator does not yet support JSON", EmulatorSpannerHelper.isUsingEmulator());
119+
120+
List<String> resources = getJsonFilePaths(RESOURCES_DIR + File.separator + INVALID_JSON_DIR);
121+
122+
AtomicLong id = new AtomicLong(100);
123+
for (String resource : resources) {
124+
String jsonStr =
125+
Resources.toString(
126+
Resources.getResource(this.getClass(), INVALID_JSON_DIR + File.separator + resource),
127+
StandardCharsets.UTF_8);
128+
assertThrows(
129+
SpannerException.class,
130+
() ->
131+
databaseClient.write(
132+
Collections.singletonList(
133+
Mutation.newInsertBuilder(TABLE_NAME)
134+
.set("Id")
135+
.to(id.getAndIncrement())
136+
.set("json")
137+
.to(Value.json(jsonStr))
138+
.build())));
139+
}
140+
}
141+
142+
private List<String> getJsonFilePaths(String folder) throws IOException {
143+
String fixturesRoot = Resources.getResource(folder).getPath();
144+
final Path fixturesRootPath = Paths.get(fixturesRoot);
145+
return Files.walk(fixturesRootPath)
146+
.filter(Files::isRegularFile)
147+
.map(path -> fixturesRootPath.relativize(path).toString())
148+
.filter(path -> path.endsWith(".json"))
149+
.collect(Collectors.toList());
150+
}
151+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[1 true]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[a�]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["": 1]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[""],
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[,1]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[1,,2]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["x",,]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["x"]]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["",]

0 commit comments

Comments
 (0)