Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support empty or null options for commands #60

Merged
merged 2 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonTypeName;
import io.stargate.sgv3.docsapi.api.model.command.SchemaChangeCommand;
import javax.annotation.Nullable;
import javax.validation.constraints.NotNull;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
Expand All @@ -14,5 +15,8 @@ public record CreateCollectionCommand(
description = "Name of the collection",
implementation = Object.class,
type = SchemaType.OBJECT)
String name)
implements SchemaChangeCommand {}
String name,
@Nullable Options options)
implements SchemaChangeCommand {
public record Options() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.stargate.sgv3.docsapi.api.model.command.Filterable;
import io.stargate.sgv3.docsapi.api.model.command.ModifyCommand;
import io.stargate.sgv3.docsapi.api.model.command.clause.filter.FilterClause;
import javax.annotation.Nullable;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
Expand All @@ -24,5 +25,8 @@ public record DeleteOneCommand(
implementation = FilterClause.class)
@Valid
@JsonProperty("filter")
FilterClause filterClause)
implements ModifyCommand, Filterable {}
FilterClause filterClause,
@Nullable Options options)
implements ModifyCommand, Filterable {
public record Options() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public record FindCommand(
@Valid @JsonProperty("filter") FilterClause filterClause,
@Valid @JsonProperty("sort") SortClause sortClause,
@Valid @Nullable @JsonProperty("options") Options options)
@Valid @Nullable Options options)
implements ReadCommand, Filterable {

public record Options(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.stargate.sgv3.docsapi.api.model.command.ReadCommand;
import io.stargate.sgv3.docsapi.api.model.command.clause.filter.FilterClause;
import io.stargate.sgv3.docsapi.api.model.command.clause.update.UpdateClause;
import javax.annotation.Nullable;
import javax.validation.Valid;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

Expand All @@ -15,5 +16,8 @@
@JsonTypeName("findOneAndUpdate")
public record FindOneAndUpdateCommand(
@Valid @JsonProperty("filter") FilterClause filterClause,
@Valid @JsonProperty("update") UpdateClause updateClause)
implements ReadCommand, Filterable {}
@Valid @JsonProperty("update") UpdateClause updateClause,
@Nullable Options options)
implements ReadCommand, Filterable {
public record Options() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
import io.stargate.sgv3.docsapi.api.model.command.ReadCommand;
import io.stargate.sgv3.docsapi.api.model.command.clause.filter.FilterClause;
import io.stargate.sgv3.docsapi.api.model.command.clause.sort.SortClause;
import javax.annotation.Nullable;
import javax.validation.Valid;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

@Schema(description = "Command that finds a single JSON document from a collection.")
@JsonTypeName("findOne")
public record FindOneCommand(
@Valid @JsonProperty("filter") FilterClause filterClause,
@Valid @JsonProperty("sort") SortClause sortClause)
implements ReadCommand, Filterable {}
@Valid @JsonProperty("sort") SortClause sortClause,
@Nullable Options options)
implements ReadCommand, Filterable {
public record Options() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.stargate.sgv3.docsapi.api.model.command.Command;
import io.stargate.sgv3.docsapi.api.model.command.ModifyCommand;
import java.util.List;
import javax.annotation.Nullable;
import javax.validation.constraints.NotNull;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
Expand All @@ -22,5 +23,8 @@ public record InsertManyCommand(
description = "JSON document to insert.",
implementation = Object.class,
type = SchemaType.ARRAY)
List<JsonNode> documents)
implements ModifyCommand {}
List<JsonNode> documents,
@Nullable Options options)
implements ModifyCommand {
public record Options() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import io.stargate.sgv3.docsapi.api.model.command.Command;
import io.stargate.sgv3.docsapi.api.model.command.ModifyCommand;
import javax.annotation.Nullable;
import javax.validation.constraints.NotNull;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
Expand All @@ -21,5 +22,8 @@ public record InsertOneCommand(
description = "JSON document to insert.",
implementation = Object.class,
type = SchemaType.OBJECT)
JsonNode document)
implements ModifyCommand {}
JsonNode document,
@Nullable Options options)
implements ModifyCommand {
public record Options() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.stargate.sgv3.docsapi.api.model.command.ReadCommand;
import io.stargate.sgv3.docsapi.api.model.command.clause.filter.FilterClause;
import io.stargate.sgv3.docsapi.api.model.command.clause.update.UpdateClause;
import javax.annotation.Nullable;
import javax.validation.Valid;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

Expand All @@ -15,5 +16,8 @@
@JsonTypeName("updateOne")
public record UpdateOneCommand(
@Valid @JsonProperty("filter") FilterClause filterClause,
@Valid @JsonProperty("update") UpdateClause updateClause)
implements ReadCommand, Filterable {}
@Valid @JsonProperty("update") UpdateClause updateClause,
@Nullable Options options)
implements ReadCommand, Filterable {
public record Options() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
import io.stargate.sgv3.docsapi.api.model.command.clause.filter.ValueComparisonOperator;
import io.stargate.sgv3.docsapi.api.model.command.clause.sort.SortClause;
import io.stargate.sgv3.docsapi.api.model.command.clause.sort.SortExpression;
import io.stargate.sgv3.docsapi.api.model.command.clause.update.UpdateClause;
import io.stargate.sgv3.docsapi.api.model.command.impl.CreateCollectionCommand;
import io.stargate.sgv3.docsapi.api.model.command.impl.FindCommand;
import io.stargate.sgv3.docsapi.api.model.command.impl.FindOneAndUpdateCommand;
import io.stargate.sgv3.docsapi.api.model.command.impl.FindOneCommand;
import io.stargate.sgv3.docsapi.api.model.command.impl.InsertManyCommand;
import io.stargate.sgv3.docsapi.api.model.command.impl.InsertOneCommand;
import io.stargate.sgv3.docsapi.api.model.command.impl.UpdateOneCommand;
import java.util.List;
import javax.inject.Inject;
import org.junit.jupiter.api.Nested;
Expand All @@ -42,7 +48,8 @@ public void happyPath() throws Exception {
"user.name",
"-user.age"
],
"filter": {"username": "aaron"}
"filter": {"username": "aaron"},
"options": {}
}
}
""";
Expand Down Expand Up @@ -116,7 +123,6 @@ public void filterClauseOptional() throws Exception {

@Nested
class InsertOne {

@Test
public void happyPath() throws Exception {
String json =
Expand All @@ -127,7 +133,8 @@ public void happyPath() throws Exception {
"some": {
"data": true
}
}
},
"options" :{}
}
}
""";
Expand All @@ -144,4 +151,165 @@ public void happyPath() throws Exception {
});
}
}

@Nested
class CreateCollection {
@Test
public void happyPath() throws Exception {
String json =
"""
{
"createCollection": {
"name": "some_name",
"options" :{}
}
}
""";

Command result = objectMapper.readValue(json, Command.class);

assertThat(result)
.isInstanceOfSatisfying(
CreateCollectionCommand.class,
createCollection -> {
String name = createCollection.name();
assertThat(name).isNotNull();
});
}
}

@Nested
class Find {
@Test
public void happyPath() throws Exception {
String json =
"""
{
"find": {
"filter" : {"username" : "user1"},
"options" : {
"pageSize" : 1
}
}
}
""";

Command result = objectMapper.readValue(json, Command.class);

assertThat(result)
.isInstanceOfSatisfying(
FindCommand.class,
find -> {
FilterClause filterClause = find.filterClause();
assertThat(filterClause).isNotNull();
final FindCommand.Options options = find.options();
assertThat(options).isNotNull();
assertThat(options.pageSize()).isEqualTo(1);
});
}
}

@Nested
class UpdateOne {
@Test
public void happyPath() throws Exception {
String json =
"""
{
"updateOne": {
"filter" : {"username" : "update_user5"},
"update" : {"$set" : {"new_col": {"sub_doc_col" : "new_val2"}}},
"options" : {}
}
}
""";

Command result = objectMapper.readValue(json, Command.class);

assertThat(result)
.isInstanceOfSatisfying(
UpdateOneCommand.class,
updateOneCommand -> {
FilterClause filterClause = updateOneCommand.filterClause();
assertThat(filterClause).isNotNull();
final UpdateClause updateClause = updateOneCommand.updateClause();
assertThat(updateClause).isNotNull();
assertThat(updateClause.buildOperations()).hasSize(1);
final UpdateOneCommand.Options options = updateOneCommand.options();
assertThat(options).isNotNull();
});
}
}

@Nested
class FindOneAndUpdate {
@Test
public void happyPath() throws Exception {
String json =
"""
{
"findOneAndUpdate": {
"filter" : {"username" : "update_user5"},
"update" : {"$set" : {"new_col": {"sub_doc_col" : "new_val2"}}},
"options" : {}
}
}
""";

Command result = objectMapper.readValue(json, Command.class);

assertThat(result)
.isInstanceOfSatisfying(
FindOneAndUpdateCommand.class,
findOneAndUpdateCommand -> {
FilterClause filterClause = findOneAndUpdateCommand.filterClause();
assertThat(filterClause).isNotNull();
final UpdateClause updateClause = findOneAndUpdateCommand.updateClause();
assertThat(updateClause).isNotNull();
assertThat(updateClause.buildOperations()).hasSize(1);
final FindOneAndUpdateCommand.Options options = findOneAndUpdateCommand.options();
assertThat(options).isNotNull();
});
}
}

@Nested
class InsertMany {
@Test
public void happyPath() throws Exception {
String json =
"""
{
"insertMany": {
"documents": [{
"_id" : "1",
"some": {
"data": true
}
},
{
"_id" : "2",
"some": {
"data": false
}
}],
"options" :{}
}
}
""";

Command result = objectMapper.readValue(json, Command.class);

assertThat(result)
.isInstanceOfSatisfying(
InsertManyCommand.class,
insertManyCommand -> {
final List<JsonNode> documents = insertManyCommand.documents();
assertThat(documents).isNotNull();
assertThat(documents).hasSize(2);
final InsertManyCommand.Options options = insertManyCommand.options();
assertThat(options).isNotNull();
});
}
}
}