forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix fabric8io#5009: ensuring underlying serialization is used
- Loading branch information
Showing
5 changed files
with
131 additions
and
12 deletions.
There are no files selected for viewing
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
119 changes: 119 additions & 0 deletions
119
...src/test/java/io/fabric8/kubernetes/client/utils/SerializationWrappedPolymorphicTest.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,119 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* 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 | ||
* | ||
* http://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.fabric8.kubernetes.client.utils; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAnyGetter; | ||
import com.fasterxml.jackson.annotation.JsonAnySetter; | ||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import com.fasterxml.jackson.annotation.JsonTypeName; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import io.fabric8.kubernetes.api.model.KubernetesResource; | ||
import io.fabric8.kubernetes.api.model.Namespaced; | ||
import io.fabric8.kubernetes.client.CustomResource; | ||
import io.fabric8.kubernetes.model.annotation.Group; | ||
import io.fabric8.kubernetes.model.annotation.Version; | ||
import lombok.Data; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class SerializationWrappedPolymorphicTest { | ||
|
||
@Group("example.com") | ||
@Version("v1alpha1") | ||
static class TestCR extends CustomResource<TestCR.TestCRSpec, Void> implements Namespaced { | ||
|
||
@JsonDeserialize(using = JsonDeserializer.None.class) | ||
static class TestCRSpec implements KubernetesResource { | ||
|
||
@JsonIgnore | ||
private Map<String, Object> additionalProperties = new HashMap<String, Object>(); | ||
|
||
private List<Child> children = new ArrayList<>(); | ||
|
||
public List<Child> getChildren() { | ||
return children; | ||
} | ||
|
||
public void setChildren(List<Child> children) { | ||
this.children = children; | ||
} | ||
|
||
@JsonAnyGetter | ||
public Map<String, Object> getAdditionalProperties() { | ||
return this.additionalProperties; | ||
} | ||
|
||
@JsonAnySetter | ||
public void setAdditionalProperty(String name, Object value) { | ||
this.additionalProperties.put(name, value); | ||
} | ||
} | ||
|
||
@Override | ||
protected TestCRSpec initSpec() { | ||
return new TestCRSpec(); | ||
} | ||
} | ||
|
||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT) | ||
@JsonSubTypes({ // | ||
@JsonSubTypes.Type(FooChild.class), // | ||
@JsonSubTypes.Type(BarChild.class), // | ||
}) | ||
@JsonDeserialize(using = JsonDeserializer.None.class) | ||
interface Child extends KubernetesResource { | ||
} | ||
|
||
@JsonTypeName("foo") | ||
@Data | ||
static class FooChild implements Child { | ||
private String name; | ||
} | ||
|
||
@JsonTypeName("bar") | ||
@Data | ||
static class BarChild implements Child { | ||
private String file; | ||
} | ||
|
||
@Test | ||
void testPolyRoundTrip() { | ||
TestCR original = new TestCR(); | ||
FooChild foo = new FooChild(); | ||
foo.setName("alice"); | ||
BarChild bar = new BarChild(); | ||
bar.setFile("bob"); | ||
original.getSpec().setChildren(Arrays.asList(foo, bar)); | ||
String json = Serialization.asJson(original); | ||
assertThat(json).isEqualTo( | ||
"{\"apiVersion\":\"example.com/v1alpha1\",\"kind\":\"TestCR\",\"metadata\":{},\"spec\":{\"children\":[{\"foo\":{\"name\":\"alice\"}},{\"bar\":{\"file\":\"bob\"}}]}}"); | ||
TestCR deserialized = Serialization.unmarshal(json, TestCR.class); | ||
assertEquals(deserialized.getSpec().getChildren(), original.getSpec().getChildren()); | ||
} | ||
|
||
} |
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
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