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

Add a root object for serialization in the Microstream rest api #37

Merged
merged 7 commits into from
Apr 29, 2022
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
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ micronaut-http-client = { module = "io.micronaut:micronaut-http-client" }
micronaut-http-server-netty = { module = "io.micronaut:micronaut-http-server-netty" }
micronaut-management = { module = "io.micronaut:micronaut-management" }
micronaut-micrometer-core = { module = 'io.micronaut.micrometer:micronaut-micrometer-core' }
micronaut-serde-api = { module = 'io.micronaut.serde:micronaut-serde-api' }
micronaut-serde-processor = { module = 'io.micronaut.serde:micronaut-serde-processor' }
micronaut-validation = { module = 'io.micronaut:micronaut-validation' }

projectreactor = { module = 'io.projectreactor:reactor-core' }
projectreactor-test = { module = "io.projectreactor:reactor-test", version.ref = 'reactor-test' }
12 changes: 12 additions & 0 deletions microstream-rest/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@ plugins {
}

dependencies {
annotationProcessor(libs.micronaut.serde.processor)

api(libs.micronaut.serde.api)

implementation(libs.managed.microstream.storage.restservice)
implementation(libs.micronaut.validation)

testImplementation(project(":microstream"))
}

configurations.all {
resolutionStrategy.dependencySubstitution {
substitute(module("io.micronaut:micronaut-jackson-databind"))
.using(module("io.micronaut.serde:micronaut-serde-jackson:1.0.1"))
}
}

micronautBuild {
binaryCompatibility {
enabled.set(false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2017-2022 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.microstream.rest;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.serde.annotation.Serdeable;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
* This object represents a root object for the Microstream REST api.
*
* @author Tim Yates
* @since 1.0.0
*/
@Serdeable
public class RootObject {

/**
* root name.
*/
@NonNull
@NotBlank
private final String name;

/**
* the root object id.
*/
@NonNull
@NotNull
private final Long objectId;

/**
* Constructor.
*
* @param name root name
* @param objectId the root object id
*/
public RootObject(@NonNull String name, @NonNull Long objectId) {
this.name = name;
this.objectId = objectId;
}

/**
* @return the name of the root object
*/
@NonNull
public String getName() {
return name;
}

/**
* @return the id of the root object
*/
@NonNull
public Long getObjectId() {
return objectId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package io.micronaut.microstream.rest

import io.micronaut.context.BeanContext
import io.micronaut.core.beans.BeanIntrospection
import io.micronaut.core.type.Argument
import io.micronaut.serde.ObjectMapper
import io.micronaut.serde.SerdeIntrospections
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification

import javax.validation.Validator

@MicronautTest(startApplication = false)
class RootObjectSpec extends Specification {
@Inject
ObjectMapper objectMapper

@Inject
Validator validator

@Inject
BeanContext beanContext

void "RootObject is annotated with @Serdeable.Deserializable"() {
given:
SerdeIntrospections serdeIntrospections = beanContext.getBean(SerdeIntrospections)

when:
serdeIntrospections.getDeserializableIntrospection(Argument.of(RootObject))

then:
noExceptionThrown()
}

void "RootObject is annotated with @Serdeable.Serializable"() {
given:
SerdeIntrospections serdeIntrospections = beanContext.getBean(SerdeIntrospections)

when:
serdeIntrospections.getSerializableIntrospection(Argument.of(RootObject))

then:
noExceptionThrown()
}

void "RootObject is annotated with Introspected"() {
when:
BeanIntrospection.getIntrospection(RootObject)

then:
noExceptionThrown()
}

void "RootObject::toString() does not throw NPE"() {
when:
new RootObject(null, null).toString()

then:
noExceptionThrown()

when:
new RootObject("tim", 1).toString()

then:
noExceptionThrown()
}

void "valid RootObject does not trigger any constraint exception"() {
when:
RootObject el = new RootObject("rooty", 1)

then:
validator.validate(el).isEmpty()
}

void "name is required"() {
when:
RootObject el = new RootObject(null, 1)

then:
!validator.validate(el).isEmpty()
}

void "objectId is required"() {
when:
RootObject el = new RootObject("rooty", null)

then:
!validator.validate(el).isEmpty()
}

void "values are present in json"() {
given:
RootObject el = new RootObject("rooty", 1)

when:
String json = objectMapper.writeValueAsString(el)

then:
json == '{"name":"rooty","objectId":1}'
}

void "round trip works as expected"() {
given:
RootObject el = new RootObject("rooty", 1)

when:
String json = objectMapper.writeValueAsString(el)

and:
RootObject object = objectMapper.readValue(json, RootObject)

then:
object.name == el.name
object.objectId == el.objectId
}
}