Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import static com.mongodb.hibernate.jdbc.MongoStatementIntegrationTests.doAndTerminateTransaction;
import static com.mongodb.hibernate.jdbc.MongoStatementIntegrationTests.doWithSpecifiedAutoCommit;
import static com.mongodb.hibernate.jdbc.MongoStatementIntegrationTests.insertTestData;
import static java.lang.String.format;
import static java.sql.Statement.SUCCESS_NO_INFO;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
Expand Down Expand Up @@ -308,14 +307,13 @@ void testNoCollectionNameProvidedExecuteBatch() {
}

@Test
void testAbsentRequiredAggregateCommandField() {
void testMissingRequiredAggregateCommandField() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced "absent" with "missing", as that's what the rest of the codebase uses:

  • $project stage is missing
  • Command name is missing
  • Collection name is missing
  • StructAggregateEmbeddableIntegrationTests/EmbeddableIntegrationTests.testReadNestedValuesMissingFields

doWorkAwareOfAutoCommit(connection -> {
String mql =
"""
var mql = """
{
aggregate: "books"
}""";
try (PreparedStatement pstm = connection.prepareStatement(mql)) {
try (var pstm = connection.prepareStatement(mql)) {
assertThatThrownBy(pstm::executeQuery)
.isInstanceOf(SQLSyntaxErrorException.class)
.hasMessage("Invalid MQL: [%s]".formatted(toExtendedJson(mql)))
Expand All @@ -327,15 +325,15 @@ void testAbsentRequiredAggregateCommandField() {
}

@Test
void testAbsentRequiredProjectAggregationPipelineStage() {
void testMissingRequiredProjectAggregationPipelineStage() {
doWorkAwareOfAutoCommit(connection -> {
String mql =
var mql =
"""
{
aggregate: "books",
"pipeline": []
}""";
try (PreparedStatement pstm = connection.prepareStatement(mql)) {
try (var pstm = connection.prepareStatement(mql)) {
assertThatThrownBy(pstm::executeQuery)
.isInstanceOf(SQLSyntaxErrorException.class)
.hasMessage("Invalid MQL. $project stage is missing [%s]".formatted(toExtendedJson(mql)));
Expand Down Expand Up @@ -393,8 +391,7 @@ void testQueriesReturningResult() {
void testEmptyBatch() {
doWorkAwareOfAutoCommit(connection -> {
try (var pstmt = connection.prepareStatement(INSERT_MQL)) {
var updateCounts = pstmt.executeBatch();
assertEquals(0, updateCounts.length);
assertExecuteBatch(pstmt, 0);
}
});

Expand All @@ -403,7 +400,7 @@ void testEmptyBatch() {

@Test
@DisplayName("Test statement’s batch of commands is reset once executeBatch returns")
void testBatchQueueIsResetAfterExecute() {
void testBatchOfCommandsIsResetAfterExecute() {
doWorkAwareOfAutoCommit(connection -> {
try (var pstmt = connection.prepareStatement(
"""
Expand Down Expand Up @@ -493,7 +490,7 @@ void testBatchInsert() {
}]
}""")) {

for (int i = 1; i <= batchSize; i++) {
for (var i = 1; i <= batchSize; i++) {
pstmt.setInt(1, i);
pstmt.setString(2, "Book " + i);
pstmt.addBatch();
Expand All @@ -503,14 +500,14 @@ void testBatchInsert() {
});

var expectedDocs = new ArrayList<BsonDocument>();
for (int i = 1; i <= batchSize; i++) {
expectedDocs.add(BsonDocument.parse(format(
for (var i = 1; i <= batchSize; i++) {
expectedDocs.add(BsonDocument.parse(
"""
{
"_id": %d,
"title": "Book %d"
}""",
i, i)));
}"""
.formatted(i, i)));
}
assertThat(mongoCollection.find()).containsExactlyElementsOf(expectedDocs);
}
Expand All @@ -530,7 +527,7 @@ void testBatchUpdate() {
multi: true
}]
}""")) {
for (int i = 1; i <= batchSize; i++) {
for (var i = 1; i <= batchSize; i++) {
pstmt.setInt(1, i);
pstmt.setString(2, "Book " + i);
pstmt.addBatch();
Expand All @@ -540,14 +537,14 @@ void testBatchUpdate() {
});

var expectedDocs = new ArrayList<BsonDocument>();
for (int i = 1; i <= batchSize; i++) {
expectedDocs.add(BsonDocument.parse(format(
for (var i = 1; i <= batchSize; i++) {
expectedDocs.add(BsonDocument.parse(
"""
{
"_id": %d,
"title": "Book %d"
}""",
i, i)));
}"""
.formatted(i, i)));
}
assertThat(mongoCollection.find()).containsExactlyElementsOf(expectedDocs);
}
Expand All @@ -566,7 +563,7 @@ void testBatchDelete() {
limit: 0
}]
}""")) {
for (int i = 1; i <= batchSize; i++) {
for (var i = 1; i <= batchSize; i++) {
pstmt.setInt(1, i);
pstmt.addBatch();
}
Expand All @@ -579,9 +576,9 @@ void testBatchDelete() {

private static void assertExecuteBatch(PreparedStatement pstmt, int expectedUpdateCountsSize)
throws SQLException {
int[] updateCounts = pstmt.executeBatch();
var updateCounts = pstmt.executeBatch();
assertEquals(expectedUpdateCountsSize, updateCounts.length);
for (int updateCount : updateCounts) {
for (var updateCount : updateCounts) {
assertEquals(SUCCESS_NO_INFO, updateCount);
}
}
Expand Down Expand Up @@ -761,11 +758,11 @@ void testDelete() {
}""")));
}

@ParameterizedTest(name = "test not supported commands. Parameters: {0}")
@ParameterizedTest(name = "test not supported command {0}")
@ValueSource(strings = {"findAndModify", "aggregate", "bulkWrite"})
void testNotSupportedCommands(String commandName) {
doWorkAwareOfAutoCommit(connection -> {
try (PreparedStatement pstm = connection.prepareStatement(
try (var pstm = connection.prepareStatement(
"""
{
%s: "books"
Expand All @@ -778,7 +775,7 @@ void testNotSupportedCommands(String commandName) {
});
}

@ParameterizedTest(name = "test not supported update command field. Parameters: option={0}")
@ParameterizedTest(name = "test not supported update command field {0}")
@ValueSource(
strings = {
"maxTimeMS: 1",
Expand All @@ -790,7 +787,7 @@ void testNotSupportedCommands(String commandName) {
})
void testNotSupportedUpdateCommandField(String unsupportedField) {
doWorkAwareOfAutoCommit(connection -> {
try (PreparedStatement pstm = connection.prepareStatement(
try (var pstm = connection.prepareStatement(
"""
{
update: "books",
Expand All @@ -813,14 +810,14 @@ void testNotSupportedUpdateCommandField(String unsupportedField) {
}

@Test
void testAbsentRequiredUpdateCommandField() {
void testMissingRequiredUpdateCommandField() {
doWorkAwareOfAutoCommit(connection -> {
String mql =
var mql =
"""
{
update: "books"
}""";
try (PreparedStatement pstm = connection.prepareStatement(mql)) {
try (var pstm = connection.prepareStatement(mql)) {
assertThatThrownBy(pstm::executeUpdate)
.isInstanceOf(SQLSyntaxErrorException.class)
.hasMessage("Invalid MQL: [%s]".formatted(toExtendedJson(mql)))
Expand All @@ -830,11 +827,11 @@ void testAbsentRequiredUpdateCommandField() {
});
}

@ParameterizedTest(name = "test not supported delete command field. Parameters: option={0}")
@ParameterizedTest(name = "test not supported delete command field {0}")
@ValueSource(strings = {"maxTimeMS: 1", "writeConcern: {}", "comment: {}", "ordered: true", "let: {}"})
void testNotSupportedDeleteCommandField(String unsupportedField) {
doWorkAwareOfAutoCommit(connection -> {
try (PreparedStatement pstm = connection.prepareStatement(
try (var pstm = connection.prepareStatement(
"""
{
delete: "books",
Expand All @@ -856,14 +853,14 @@ void testNotSupportedDeleteCommandField(String unsupportedField) {
}

@Test
void testAbsentRequiredDeleteCommandField() {
void testMissingRequiredDeleteCommandField() {
doWorkAwareOfAutoCommit(connection -> {
String mql =
var mql =
"""
{
delete: "books"
}""";
try (PreparedStatement pstm = connection.prepareStatement(mql)) {
try (var pstm = connection.prepareStatement(mql)) {
assertThatThrownBy(pstm::executeUpdate)
.isInstanceOf(SQLSyntaxErrorException.class)
.hasMessage("Invalid MQL: [%s]".formatted(toExtendedJson(mql)))
Expand All @@ -873,7 +870,7 @@ void testAbsentRequiredDeleteCommandField() {
});
}

@ParameterizedTest(name = "test not supported insert command field. Parameters: option={0}")
@ParameterizedTest(name = "test not supported insert command field {0}")
@ValueSource(
strings = {
"maxTimeMS: 1",
Expand All @@ -885,7 +882,7 @@ void testAbsentRequiredDeleteCommandField() {
})
void testNotSupportedInsertCommandField(String unsupportedField) {
doWorkAwareOfAutoCommit(connection -> {
try (PreparedStatement pstm = connection.prepareStatement(
try (var pstm = connection.prepareStatement(
"""
{
insert: "books",
Expand All @@ -906,14 +903,14 @@ void testNotSupportedInsertCommandField(String unsupportedField) {
}

@Test
void testAbsentRequiredInsertCommandField() {
void testMissingRequiredInsertCommandField() {
doWorkAwareOfAutoCommit(connection -> {
String mql =
var mql =
"""
{
insert: "books"
}""";
try (PreparedStatement pstm = connection.prepareStatement(mql)) {
try (var pstm = connection.prepareStatement(mql)) {
assertThatThrownBy(pstm::executeUpdate)
.isInstanceOf(SQLSyntaxErrorException.class)
.hasMessage("Invalid MQL: [%s]".formatted(toExtendedJson(mql)))
Expand All @@ -923,7 +920,7 @@ void testAbsentRequiredInsertCommandField() {
});
}

private static Stream<Arguments> unsupportedUpdateStatementFields() {
private static Stream<Arguments> testNotSupportedUpdateStatemenField() {
return Stream.of(
of("hint: {}", "Unsupported field in [update] statement: [hint]"),
of("hint: \"a\"", "Unsupported field in [update] statement: [hint]"),
Expand All @@ -935,11 +932,11 @@ private static Stream<Arguments> unsupportedUpdateStatementFields() {
of("c: {}", "Unsupported field in [update] statement: [c]"));
}

@ParameterizedTest(name = "test not supported update statement field. Parameters: option={0}")
@MethodSource("unsupportedUpdateStatementFields")
@ParameterizedTest(name = "test not supported update statement field {0}")
@MethodSource
void testNotSupportedUpdateStatemenField(String unsupportedField, String expectedMessage) {
doWorkAwareOfAutoCommit(connection -> {
try (PreparedStatement pstm = connection.prepareStatement(
try (var pstm = connection.prepareStatement(
"""
{
update: "books",
Expand All @@ -960,11 +957,11 @@ void testNotSupportedUpdateStatemenField(String unsupportedField, String expecte
});
}

@ParameterizedTest(name = "test absent required update statement field. Parameters: fieldToRemove={0}")
@ValueSource(strings = {"q: {}", "u: {}"})
void testAbsentRequiredUpdateStatementField(String fieldToRemove) {
@ParameterizedTest(name = "test missing required update statement field {0}")
@ValueSource(strings = {"q", "u"})
void testMissingRequiredUpdateStatementField(String missingFieldName) {
doWorkAwareOfAutoCommit(connection -> {
String mql =
var mqlDocument = BsonDocument.parse(
"""
{
update: "books",
Expand All @@ -974,23 +971,24 @@ void testAbsentRequiredUpdateStatementField(String fieldToRemove) {
u: {},
}
]
}"""
.replace(fieldToRemove + ",", "");
try (PreparedStatement pstm = connection.prepareStatement(mql)) {
}""");
mqlDocument.getArray("updates").get(0).asDocument().remove(missingFieldName);
var mql = mqlDocument.toJson();
try (var pstm = connection.prepareStatement(mql)) {
assertThatThrownBy(pstm::executeUpdate)
.isInstanceOf(SQLSyntaxErrorException.class)
.hasMessage("Invalid MQL: [%s]".formatted(toExtendedJson(mql)))
.cause()
.hasMessage("Document does not contain key %s".formatted(getFieldName(fieldToRemove)));
.hasMessage("Document does not contain key %s".formatted(missingFieldName));
}
});
}

@ParameterizedTest(name = "test not supported delete statement field. Parameters: option={0}")
@ParameterizedTest(name = "test not supported delete statement field {0}")
@ValueSource(strings = {"hint: {}", "hint: \"a\"", "collation: {}"})
void testNotSupportedDeleteStatementField(String unsupportedField) {
doWorkAwareOfAutoCommit(connection -> {
try (PreparedStatement pstm = connection.prepareStatement(
try (var pstm = connection.prepareStatement(
"""
{
delete: "books",
Expand All @@ -1011,28 +1009,29 @@ void testNotSupportedDeleteStatementField(String unsupportedField) {
});
}

@ParameterizedTest(name = "test absent required update statement field. Parameters: fieldToRemove={0}")
@ValueSource(strings = {"q: {}", "limit: 0"})
void testAbsentRequiredDeleteStatementField(String fieldToRemove) {
@ParameterizedTest(name = "test missing required update statement field {0}")
@ValueSource(strings = {"q", "limit"})
void testMissingRequiredDeleteStatementField(String missingFieldName) {
doWorkAwareOfAutoCommit(connection -> {
String mql =
var mqlDocument = BsonDocument.parse(
"""
{
delete: "books",
deletes: [
{
q: {},
limit: 0,
}
]
}"""
.replace(fieldToRemove + ",", "");
try (PreparedStatement pstm = connection.prepareStatement(mql)) {
{
delete: "books",
deletes: [
{
q: {},
limit: 0,
}
]
}""");
mqlDocument.getArray("deletes").get(0).asDocument().remove(missingFieldName);
var mql = mqlDocument.toJson();
try (var pstm = connection.prepareStatement(mql)) {
assertThatThrownBy(pstm::executeUpdate)
.isInstanceOf(SQLSyntaxErrorException.class)
.hasMessage("Invalid MQL: [%s]".formatted(toExtendedJson(mql)))
.cause()
.hasMessage("Document does not contain key %s".formatted(getFieldName(fieldToRemove)));
.hasMessage("Document does not contain key %s".formatted(missingFieldName));
}
});
}
Expand All @@ -1054,7 +1053,7 @@ private void assertExecuteUpdate(
private void assertInvalidMql(
String mql, SqlConsumer<PreparedStatement> executor, String expectedExceptionMessage) {
doWorkAwareOfAutoCommit(connection -> {
try (PreparedStatement pstm = connection.prepareStatement(mql)) {
try (var pstm = connection.prepareStatement(mql)) {
assertThatThrownBy(() -> executor.accept(pstm))
.isInstanceOf(SQLSyntaxErrorException.class)
.hasMessage(expectedExceptionMessage);
Expand All @@ -1070,11 +1069,11 @@ void doAwareOfAutoCommit(Connection connection, SqlExecutable work) throws SQLEx
doWithSpecifiedAutoCommit(false, connection, () -> doAndTerminateTransaction(connection, work));
}

private static String getFieldName(String unsupportedField) {
return BsonDocument.parse("{" + unsupportedField + "}").getFirstKey();
private static String getFieldName(String field) {
Copy link
Member Author

@stIncMale stIncMale Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method does not care whether the argument passed is a supported field or not.

return BsonDocument.parse("{" + field + "}").getFirstKey();
}

private String toExtendedJson(String mql) {
private static String toExtendedJson(String mql) {
return BsonDocument.parse(mql).toJson(EXTENDED_JSON_WRITER_SETTINGS);
}

Expand Down
Loading