Skip to content

Commit e1c62c3

Browse files
authored
chore: refactor test for increment and vup for vulnerability (#64)
* chore: refactor test for increment and vup for vulnerability * docs: update README
1 parent a995b2b commit e1c62c3

3 files changed

Lines changed: 36 additions & 13 deletions

File tree

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ java-cosmos is a client for Azure CosmosDB 's SQL API (also called documentdb fo
2323
<dependency>
2424
<groupId>com.github.thunderz99</groupId>
2525
<artifactId>java-cosmos</artifactId>
26-
<version>0.5.8</version>
26+
<version>0.5.11</version>
2727
</dependency>
2828
```
2929

@@ -233,12 +233,16 @@ db.updatePartial("Collection", user1.id, Map.of("lastName", "Hanks", "status", "
233233
// support incrementing a number field
234234
// see https://docs.microsoft.com/en-us/azure/cosmos-db/partial-document-update-getting-started?tabs=java
235235

236-
// increment age by 1
236+
// increment age by 1. Supports int / long / double.
237237
var result = db.increment("Collection1", "id1", "/age", 1, "Users");
238238

239239
// increment age by -5
240240
var result = db.increment("Collection1", "id1", "/age", -5, "Users");
241241

242+
243+
// 404 Not Found Exception will be throw if the id does not exist
244+
var result = db.increment("Collection1", "not exist id", "/name", 1, "Users");
245+
242246
// 400 Bad Request Exception will be throw if the field is not an integer
243247
var result = db.increment("Collection1", "id1", "/name", 1, "Users");
244248

pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.github.thunderz99</groupId>
55
<artifactId>java-cosmos</artifactId>
66
<packaging>jar</packaging>
7-
<version>0.5.10</version>
7+
<version>0.5.11</version>
88
<name>${project.groupId}:${project.artifactId}$</name>
99
<description>A lightweight Azure CosmosDB client for Java</description>
1010
<url>https://github.com/thunderz99/java-cosmos</url>
@@ -72,14 +72,14 @@
7272
<dependency>
7373
<groupId>com.fasterxml.jackson.core</groupId>
7474
<artifactId>jackson-databind</artifactId>
75-
<version>2.13.1</version>
75+
<version>2.13.3</version>
7676
</dependency>
7777

7878
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
7979
<dependency>
8080
<groupId>com.fasterxml.jackson.core</groupId>
8181
<artifactId>jackson-core</artifactId>
82-
<version>2.13.1</version>
82+
<version>2.13.3</version>
8383
</dependency>
8484

8585
<!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure-documentdb -->
@@ -92,13 +92,13 @@
9292
<dependency>
9393
<groupId>com.azure</groupId>
9494
<artifactId>azure-cosmos</artifactId>
95-
<version>4.29.1</version>
95+
<version>4.30.1</version>
9696
</dependency>
9797

9898
<dependency>
9999
<groupId>io.github.cdimascio</groupId>
100100
<artifactId>java-dotenv</artifactId>
101-
<version>5.2.1</version>
101+
<version>5.2.2</version>
102102
</dependency>
103103

104104
<dependency>

src/test/java/io/github/thunderz99/cosmos/CosmosDatabaseTest.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,30 +1203,39 @@ void increment_should_work() throws Exception {
12031203

12041204

12051205
try {
1206-
var data1 = Map.of("id", id, "name", "John", "contents", Map.of("age", 20), "score", 85.5);
1206+
var data1 = Map.of("id", id, "name", "John", "contents", Map.of("age", 20), "score", 85.5, "number", 3_147_483_647L);
12071207
db.upsert(coll, data1, partition).toMap();
12081208
{
1209-
// increment by 1
1209+
// increment by 1, integer field
12101210
var inc1 = db.increment(coll, id, "/contents/age", 1, partition).toMap();
12111211
assertThat((Map<String, Object>) inc1.get("contents")).containsEntry("age", 21);
12121212

1213-
// increment by -3
1213+
// increment by -3, integer field
12141214
var inc2 = db.increment(coll, id, "/contents/age", -3, partition).toMap();
12151215
assertThat((Map<String, Object>) inc2.get("contents")).containsEntry("age", 18);
1216+
1217+
// increment by 1, long field
1218+
var inc3 = db.increment(coll, id, "/number", 1, partition).toMap();
1219+
assertThat(inc3).containsEntry("number", 3_147_483_648L);
1220+
1221+
// increment by 5, double field
1222+
var inc4 = db.increment(coll, id, "/score", 5, partition).toMap();
1223+
assertThat(inc4).containsEntry("score", 90.5);
1224+
12161225
}
12171226

12181227
{
12191228
// failed when incrementing a string field
12201229
assertThatThrownBy(() -> {
1221-
db.increment(coll, id, "name", 5, partition);
1230+
db.increment(coll, id, "/name", 5, partition);
12221231
}).isInstanceOfSatisfying(CosmosException.class, (e) -> {
12231232
assertThat(e.getStatusCode()).isEqualTo(400);
1224-
assertThat(e.getMessage()).contains("inputs is invalid");
1233+
assertThat(e.getMessage()).contains("is not a number");
12251234
});
12261235
}
12271236

12281237
{
1229-
// failed when incrementing a double field
1238+
// 400 will be thrown when path is not correct
12301239
assertThatThrownBy(() -> {
12311240
db.increment(coll, id, "score", 5, partition);
12321241
}).isInstanceOfSatisfying(CosmosException.class, (e) -> {
@@ -1235,6 +1244,16 @@ void increment_should_work() throws Exception {
12351244
});
12361245
}
12371246

1247+
{
1248+
// 404 will be thrown when incrementing a not existing item
1249+
assertThatThrownBy(() -> {
1250+
db.increment(coll, "not exist", "/number", 1, partition);
1251+
}).isInstanceOfSatisfying(CosmosException.class, (e) -> {
1252+
assertThat(e.getStatusCode()).isEqualTo(404);
1253+
assertThat(e.getMessage()).contains("Not Found");
1254+
});
1255+
}
1256+
12381257
} finally {
12391258
db.delete(coll, id, partition);
12401259
}

0 commit comments

Comments
 (0)