Skip to content

Commit

Permalink
added type information to serializable classes (#109)
Browse files Browse the repository at this point in the history
* added type information to serializable classes

* snapshot version bump

Co-authored-by: Paul Latzelsperger <[email protected]>
  • Loading branch information
paullatzelsperger and paullatzelsperger authored Jul 20, 2021
1 parent 2b7b4c3 commit 5e00144
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 24 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ allprojects {
apply(plugin = "maven-publish")
pluginManager.withPlugin("java-library") {
group = "com.microsoft"
version = "0.0.1-SNAPSHOT.1"
version = "0.0.1-SNAPSHOT.2"
dependencies {
api("org.jetbrains:annotations:${jetBrainsAnnotationsVersion}")
api("com.fasterxml.jackson.core:jackson-core:${jacksonVersion}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.microsoft.dagx.schema.azure.AzureBlobStoreSchema;
import com.microsoft.dagx.spi.types.domain.transfer.DataAddress;
import com.microsoft.dagx.spi.types.domain.transfer.ProvisionedDataDestinationResource;
import com.microsoft.dagx.spi.types.domain.transfer.ProvisionedResource;

/**
*
*/
@JsonDeserialize(builder = ObjectContainerProvisionedResource.Builder.class)
@JsonTypeName("dagx:objectcontainerprovisionedresource")
public class ObjectContainerProvisionedResource extends ProvisionedDataDestinationResource {

@JsonProperty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void verifySerialization() {

@Test
void verifyDeserialization() {
final String json = "{\"id\":\"test-id\",\"transferProcessId\":\"test-process-id\",\"resourceDefinitionId\":\"test-resdef-id\",\"error\":false,\"errorMessage\":null,\"accountName\":\"test-account\",\"containerName\":\"test-container\",\"resourceName\":\"test-container\"}";
final String json = "{\"id\":\"test-id\",\"dagxtype\":\"dagx:objectcontainerprovisionedresource\", \"transferProcessId\":\"test-process-id\",\"resourceDefinitionId\":\"test-resdef-id\",\"error\":false,\"errorMessage\":null,\"accountName\":\"test-account\",\"containerName\":\"test-container\",\"resourceName\":\"test-container\"}";
var typeManager = new TypeManager();
typeManager.registerTypes(ObjectContainerProvisionedResource.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@

package com.microsoft.dagx.spi.types.domain.transfer;

import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;

/**
* A provisioned resource that serves as a data destination.
*/
@JsonTypeName("dagx:provisioneddatadestinationresource")
@JsonDeserialize(builder = ProvisionedDataDestinationResource.Builder.class)
public abstract class ProvisionedDataDestinationResource extends ProvisionedResource {

protected ProvisionedDataDestinationResource() {
Expand All @@ -18,6 +24,7 @@ protected ProvisionedDataDestinationResource() {

public abstract String getResourceName();

@JsonPOJOBuilder(withPrefix = "")
protected static class Builder<PR extends ProvisionedResource, B extends ProvisionedResource.Builder<PR, B>> extends ProvisionedResource.Builder<PR, B> {

protected Builder(PR resource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@

package com.microsoft.dagx.spi.types.domain.transfer;

import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.microsoft.dagx.spi.types.domain.Polymorphic;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

/**
* A provisioned resource that supports a data transfer request.
*/
public abstract class ProvisionedResource {
@JsonTypeName("dagx:provisionedresource")
@JsonDeserialize(builder = ProvisionedResource.Builder.class)
public abstract class ProvisionedResource implements Polymorphic {
protected String id;
protected String transferProcessId;
protected String resourceDefinitionId;
Expand Down Expand Up @@ -46,6 +52,7 @@ public String getErrorMessage() {
}


@JsonPOJOBuilder
@SuppressWarnings("unchecked")
public static class Builder<PR extends ProvisionedResource, B extends Builder<PR, B>> {
protected PR provisionedResource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@

package com.microsoft.dagx.spi.types.domain.transfer;

import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.microsoft.dagx.spi.types.domain.Polymorphic;

import java.util.Objects;

/**
* A resource to be provisioned to support a data transfer.
*/
public abstract class ResourceDefinition {
@JsonTypeName("dagx:resourcedefinition")
@JsonDeserialize(builder = ResourceDefinition.Builder.class)
public abstract class ResourceDefinition implements Polymorphic {
protected String id;
protected String transferProcessId;

Expand All @@ -26,6 +33,7 @@ void setTransferProcessId(String transferProcessId) {
this.transferProcessId = transferProcessId;
}

@JsonPOJOBuilder
@SuppressWarnings("unchecked")
public static class Builder<RD extends ResourceDefinition, B extends Builder<RD, B>> {
protected final RD resourceDefinition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.microsoft.dagx.spi.types.domain.transfer;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;

Expand All @@ -18,10 +19,12 @@ public class TransferType {
private String contentType = DEFAULT_CONTENT_TYPE;
private boolean isFinite = true;

@JsonProperty("contentType")
public String getContentType() {
return contentType;
}

@JsonProperty("isFinite")
public boolean isFinite() {
return isFinite;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@

import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
*
*/
class ProvisionedResourceTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@

package com.microsoft.dagx.spi.types.domain.transfer;

/**
*
*/
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;

@JsonDeserialize(builder = TestProvisionedResource.Builder.class)
@JsonTypeName("dagx:testprovisionedresource")
class TestProvisionedResource extends ProvisionedResource {

@JsonPOJOBuilder(withPrefix = "")
public static class Builder extends ProvisionedResource.Builder<TestProvisionedResource, Builder> {
public static Builder newInstance() {
return new Builder();
}

private Builder() {
super(new TestProvisionedResource());
}

@JsonCreator
public static Builder newInstance() {
return new Builder();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@

package com.microsoft.dagx.spi.types.domain.transfer;

/**
*
*/
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;

@JsonTypeName("dagx:testresourcedefinition")
@JsonDeserialize(builder = TestResourceDefinition.Builder.class)
class TestResourceDefinition extends ResourceDefinition {

@JsonPOJOBuilder(withPrefix = "")
public static class Builder extends ResourceDefinition.Builder<TestResourceDefinition, Builder> {
public static Builder newInstance() {
return new Builder();
}

private Builder() {
super(new TestResourceDefinition());
}

@JsonCreator
public static Builder newInstance() {
return new Builder();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) Microsoft Corporation.
* All rights reserved.
*
*/

package com.microsoft.dagx.spi.types.domain.transfer;

import com.microsoft.dagx.spi.types.TypeManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

public class TransferTypeTest {

private TypeManager tm;

@BeforeEach
void setup() {
tm = new TypeManager();
tm.registerTypes(TransferType.class);
}

@Test
void verifySerialization() {
var tt = TransferType.Builder.transferType()
.isFinite(false)
.contentType("someContentType")
.build();

var json = tm.writeValueAsString(tt);

assertThat(json).contains("\"contentType\":\"someContentType\"");
assertThat(json).contains("\"isFinite\":false");
//noinspection unchecked
final Map<String, Object> map = (Map<String, Object>) tm.readValue(json, Map.class);
assertThat(map).hasSize(2);
assertThat(map).containsKey("contentType");
assertThat(map).containsKey("isFinite");
assertThat(map).doesNotContainKey("dagxtype");
}

@Test
void verifyDeserialization() {
var tt = TransferType.Builder.transferType()
.isFinite(false)
.contentType("someContentType")
.build();

var json = tm.writeValueAsString(tt);

var restored = tm.readValue(json, TransferType.class);
assertThat(restored).usingRecursiveComparison().isEqualTo(tt);
assertThat(restored.isFinite()).isFalse();
assertThat(restored.getContentType()).isEqualTo("someContentType");
}

}

0 comments on commit 5e00144

Please sign in to comment.