The expiration time for new job artifacts in CI/CD pipelines is now 30 days (GitLab default). Previously generated artifacts in already completed jobs will not be affected by the change. The latest artifacts for all jobs in the latest successful pipelines will be kept. More information: https://gitlab.lrz.de/help/user/admin_area/settings/continuous_integration.html#default-artifacts-expiration

Commit 21cecb1b authored by Jakob Schöttl's avatar Jakob Schöttl
Browse files

Add test for JSON deserialization with Jackson

parent 175d8210
package org.vadere.state.attributes;
import static org.junit.Assert.*;
import org.junit.Test;
import org.vadere.state.util.StateJsonConverter;
import com.fasterxml.jackson.databind.JsonMappingException;
public class TestPojoJsonDeserialization {
private static final String incompleteJson = "{\"b\":5}";
private static final String completeJson = "{\"a\":4,\"b\":5}";
@Test
public void testIncompleteDeserialization() {
// Important: After deserialization with incomplete JSON,
// the init values (for a and b) in POJOs take effect!
assertTestPojoEquals(new TestPojoWithDefaultConstructor(1, 5), incompleteJson, TestPojoWithDefaultConstructor.class);
}
@Test
public void testCompleteDeserialization() {
assertTestPojoEquals(new TestPojoWithDefaultConstructor(4, 5), completeJson, TestPojoWithDefaultConstructor.class);
}
@Test
public void testIncompleteDeserializationWithoutDefaultCtor() {
try {
assertTestPojoEquals(new TestPojoWithoutDefaultConstructor(0, 5), incompleteJson, TestPojoWithoutDefaultConstructor.class);
} catch (RuntimeException e) {
assertExceptionCorrect(e);
}
}
@Test
public void testCompleteDeserializationWithoutDefaultCtor() {
try {
assertTestPojoEquals(new TestPojoWithoutDefaultConstructor(0, 5), completeJson, TestPojoWithoutDefaultConstructor.class);
} catch (RuntimeException e) {
assertExceptionCorrect(e);
}
}
private void assertExceptionCorrect(RuntimeException e) {
assertTrue(e.getCause() instanceof JsonMappingException);
}
private void assertTestPojoEquals(Object pojo, String json, Class<?> clazz) {
assertEquals(pojo, StateJsonConverter.deserializeObjectFromJson(json, clazz));
}
public static class TestPojoWithDefaultConstructor {
int a = 1;
int b = 2;
@SuppressWarnings("unused")
private TestPojoWithDefaultConstructor() { }
public TestPojoWithDefaultConstructor(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public boolean equals(Object obj) {
// auto-generated
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TestPojoWithDefaultConstructor other = (TestPojoWithDefaultConstructor) obj;
if (a != other.a)
return false;
if (b != other.b)
return false;
return true;
}
}
public static class TestPojoWithoutDefaultConstructor {
int a = 1;
int b = 2;
public TestPojoWithoutDefaultConstructor(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public boolean equals(Object obj) {
// auto-generated
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TestPojoWithoutDefaultConstructor other = (TestPojoWithoutDefaultConstructor) obj;
if (a != other.a)
return false;
if (b != other.b)
return false;
return true;
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment