-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] relates to C2-2197: setup the command serialization with tests
- Loading branch information
Ivan Senic
committed
Nov 28, 2022
1 parent
d777930
commit 111027e
Showing
9 changed files
with
165 additions
and
3 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/main/java/io/stargate/sgv3/docsapi/api/configuration/ObjectMapperConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.stargate.sgv3.docsapi.api.configuration; | ||
|
||
import com.fasterxml.jackson.databind.MapperFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
import io.quarkus.jackson.ObjectMapperCustomizer; | ||
import javax.enterprise.inject.Instance; | ||
import javax.enterprise.inject.Produces; | ||
import javax.inject.Singleton; | ||
|
||
/** Configures the {@link ObjectMapper} instance that going to be injectable and used in the app. */ | ||
public class ObjectMapperConfiguration { | ||
|
||
/** Replaces the CDI producer for ObjectMapper built into Quarkus. */ | ||
@Singleton | ||
@Produces | ||
ObjectMapper objectMapper(Instance<ObjectMapperCustomizer> customizers) { | ||
ObjectMapper mapper = createMapper(); | ||
|
||
// apply all ObjectMapperCustomizer beans (incl. Quarkus) | ||
for (ObjectMapperCustomizer customizer : customizers) { | ||
customizer.customize(mapper); | ||
} | ||
|
||
return mapper; | ||
} | ||
|
||
private ObjectMapper createMapper() { | ||
// enabled: | ||
// case insensitive enums, so "before" will match to "BEFORE" in an enum | ||
return JsonMapper.builder().enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS).build(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/io/stargate/sgv3/docsapi/api/model/command/Command.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.stargate.sgv3.docsapi.api.model.command; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import io.stargate.sgv3.docsapi.api.model.command.impl.InsertOneCommand; | ||
|
||
/** | ||
* POJO object (data no behavior) that represents a syntactically and grammatically valid command as | ||
* defined in the API spec. | ||
* | ||
* <p>The behavior about *how* to run a Command is in the {@link CommandResolver}. | ||
* | ||
* <p>Commands <b>should not</b> include JSON other than for documents we want to insert. They | ||
* should represent a translate of the API request into an internal representation. e.g. this | ||
* insulates from tweaking JSON on the wire protocol, we would only need to modify how we create the | ||
* command and nothing else. | ||
* | ||
* <p>These may be created from parsing the incoming message and could also be created | ||
* programmatically for internal and testing purposes. | ||
* | ||
* <p>Each command should validate itself using the javax.validation framework. | ||
*/ | ||
@JsonTypeInfo( | ||
use = JsonTypeInfo.Id.NAME, | ||
include = JsonTypeInfo.As.WRAPPER_OBJECT, | ||
property = "commandName") | ||
@JsonSubTypes({ | ||
@JsonSubTypes.Type(value = InsertOneCommand.class), | ||
}) | ||
@JsonIgnoreProperties({"commandName"}) | ||
public interface Command {} |
4 changes: 4 additions & 0 deletions
4
src/main/java/io/stargate/sgv3/docsapi/api/model/command/ModifyCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package io.stargate.sgv3.docsapi.api.model.command; | ||
|
||
/** Base for any commands that modify, such as insert, delete, update, etc. */ | ||
public interface ModifyCommand extends Command {} |
3 changes: 3 additions & 0 deletions
3
src/main/java/io/stargate/sgv3/docsapi/api/model/command/ReadCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package io.stargate.sgv3.docsapi.api.model.command; | ||
|
||
public interface ReadCommand {} |
19 changes: 19 additions & 0 deletions
19
src/main/java/io/stargate/sgv3/docsapi/api/model/command/impl/InsertOneCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.stargate.sgv3.docsapi.api.model.command.impl; | ||
|
||
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.validation.constraints.NotNull; | ||
import org.eclipse.microprofile.openapi.annotations.media.Schema; | ||
|
||
/** | ||
* Representation of the insertOne API {@link Command}. | ||
* | ||
* @param document The document to insert. | ||
*/ | ||
@Schema(description = "Command that inserts a single JSON document.") | ||
@JsonTypeName("insertOne") | ||
public record InsertOneCommand( | ||
@NotNull @Schema(description = "JSON document to insert.") JsonNode document) | ||
implements ModifyCommand {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/test/java/io/stargate/sgv3/docsapi/api/configuration/ObjectMapperConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.stargate.sgv3.docsapi.api.configuration; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.junit.TestProfile; | ||
import io.stargate.sgv2.common.testprofiles.NoGlobalResourcesTestProfile; | ||
import io.stargate.sgv3.docsapi.api.model.command.Command; | ||
import io.stargate.sgv3.docsapi.api.model.command.impl.InsertOneCommand; | ||
import javax.inject.Inject; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@QuarkusTest | ||
@TestProfile(NoGlobalResourcesTestProfile.Impl.class) | ||
class ObjectMapperConfigurationTest { | ||
|
||
@Inject ObjectMapper objectMapper; | ||
|
||
@Nested | ||
class InsertOne { | ||
|
||
@Test | ||
public void happyPath() throws Exception { | ||
String json = | ||
""" | ||
{ | ||
"insertOne": { | ||
"document": { | ||
"some": { | ||
"data": true | ||
} | ||
} | ||
} | ||
} | ||
"""; | ||
|
||
Command result = objectMapper.readValue(json, Command.class); | ||
|
||
assertThat(result) | ||
.isInstanceOfSatisfying( | ||
InsertOneCommand.class, | ||
insertOne -> { | ||
JsonNode document = insertOne.document(); | ||
assertThat(document).isNotNull(); | ||
assertThat(document.required("some").required("data").asBoolean()).isTrue(); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# by default ignore bridge for data store props in tests | ||
|
||
stargate: | ||
data-store: | ||
ignore-bridge: true |