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
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
@@ -1,7 +1,9 @@
package io.stargate.sgv3.docsapi.api.model.command.impl;

import com.fasterxml.jackson.annotation.JsonProperty;
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;
@@ -14,5 +16,8 @@ public record CreateCollectionCommand(
description = "Name of the collection",
implementation = Object.class,
type = SchemaType.OBJECT)
String name)
implements SchemaChangeCommand {}
String name,
@Nullable @JsonProperty("options") Options options)
maheshrajamani marked this conversation as resolved.
Show resolved Hide resolved
implements SchemaChangeCommand {
public record Options() {}
}
Original file line number Diff line number Diff line change
@@ -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;
@@ -24,5 +25,8 @@ public record DeleteOneCommand(
implementation = FilterClause.class)
@Valid
@JsonProperty("filter")
FilterClause filterClause)
implements ModifyCommand, Filterable {}
FilterClause filterClause,
@Nullable @JsonProperty("options") Options options)
implements ModifyCommand, Filterable {
public record Options() {}
}
Original file line number Diff line number Diff line change
@@ -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;

@@ -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 @JsonProperty("options") Options options)
implements ReadCommand, Filterable {
public record Options() {}
}
Original file line number Diff line number Diff line change
@@ -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 @JsonProperty("options") Options options)
implements ReadCommand, Filterable {
public record Options() {}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.stargate.sgv3.docsapi.api.model.command.impl;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
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 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;
@@ -22,5 +24,8 @@ public record InsertManyCommand(
description = "JSON document to insert.",
implementation = Object.class,
type = SchemaType.ARRAY)
List<JsonNode> documents)
implements ModifyCommand {}
List<JsonNode> documents,
@Nullable @JsonProperty("options") Options options)
implements ModifyCommand {
public record Options() {}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.stargate.sgv3.docsapi.api.model.command.impl;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
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;
@@ -21,5 +23,8 @@ public record InsertOneCommand(
description = "JSON document to insert.",
implementation = Object.class,
type = SchemaType.OBJECT)
JsonNode document)
implements ModifyCommand {}
JsonNode document,
@Nullable @JsonProperty("options") Options options)
implements ModifyCommand {
public record Options() {}
}
Original file line number Diff line number Diff line change
@@ -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;

@@ -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 @JsonProperty("options") Options options)
implements ReadCommand, Filterable {
public record Options() {}
}
Original file line number Diff line number Diff line change
@@ -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;
@@ -42,7 +48,8 @@ public void happyPath() throws Exception {
"user.name",
"-user.age"
],
"filter": {"username": "aaron"}
"filter": {"username": "aaron"},
"options": {}
}
}
""";
@@ -116,7 +123,6 @@ public void filterClauseOptional() throws Exception {

@Nested
class InsertOne {

@Test
public void happyPath() throws Exception {
String json =
@@ -127,7 +133,8 @@ public void happyPath() throws Exception {
"some": {
"data": true
}
}
},
"options" :{}
}
}
""";
@@ -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();
});
}
}
}