aboutsummaryrefslogtreecommitdiffstats
path: root/utils-test/src/test/java/org/onap/policy/common/utils/io/SerializerTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'utils-test/src/test/java/org/onap/policy/common/utils/io/SerializerTest.java')
-rw-r--r--utils-test/src/test/java/org/onap/policy/common/utils/io/SerializerTest.java46
1 files changed, 26 insertions, 20 deletions
diff --git a/utils-test/src/test/java/org/onap/policy/common/utils/io/SerializerTest.java b/utils-test/src/test/java/org/onap/policy/common/utils/io/SerializerTest.java
index 95abd4db..203c78ec 100644
--- a/utils-test/src/test/java/org/onap/policy/common/utils/io/SerializerTest.java
+++ b/utils-test/src/test/java/org/onap/policy/common/utils/io/SerializerTest.java
@@ -2,7 +2,8 @@
* ============LICENSE_START=======================================================
* ONAP Policy Engine - Common Modules
* ================================================================================
- * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
+ * Modificaitons Copyright (C) 2023 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -41,7 +42,7 @@ import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.common.utils.io.Serializer.Factory;
-import org.powermock.reflect.Whitebox;
+import org.springframework.test.util.ReflectionTestUtils;
public class SerializerTest {
private static final String FACTORY = "factory";
@@ -53,12 +54,12 @@ public class SerializerTest {
@BeforeClass
public static void setUpBeforeClass() {
- saveFactory = Whitebox.getInternalState(Serializer.class, FACTORY);
+ saveFactory = (Factory) ReflectionTestUtils.getField(Serializer.class, FACTORY);
}
@AfterClass
public static void tearDownAfterClass() {
- Whitebox.setInternalState(Serializer.class, FACTORY, saveFactory);
+ ReflectionTestUtils.setField(Serializer.class, FACTORY, saveFactory);
}
@Before
@@ -80,7 +81,7 @@ public class SerializerTest {
byte[] data2 = Serializer.serialize(obj1);
assertEquals(Arrays.toString(data), Arrays.toString(data2));
- MyObject obj2 = Serializer.deserialize(MyObject.class, data);
+ MyObject obj2 = Serializer.roundTrip(obj1);
assertEquals(obj1.value, obj2.value);
}
@@ -210,16 +211,14 @@ public class SerializerTest {
}
});
- assertThatThrownBy(() -> Serializer.serialize(new MyObject(130))).isEqualTo(ex2);
+ assertThatThrownBy(() -> Serializer.roundTrip(new MyObject(130))).isEqualTo(ex2);
}
@Test
public void testDeserialize() throws Exception {
MyObject obj1 = new MyObject(3);
- byte[] data = Serializer.serialize(obj1);
-
- MyObject obj2 = Serializer.deserialize(MyObject.class, data);
+ MyObject obj2 = Serializer.roundTrip(obj1);
assertEquals(obj1.value, obj2.value);
}
@@ -249,8 +248,7 @@ public class SerializerTest {
}
});
- byte[] data = Serializer.serialize(new MyObject(300));
- assertThatThrownBy(() -> Serializer.deserialize(MyObject.class, data)).isEqualTo(ex);
+ assertThatThrownBy(() -> Serializer.roundTrip(new MyObject(300))).isEqualTo(ex);
}
@Test
@@ -267,8 +265,7 @@ public class SerializerTest {
}
});
- byte[] data = Serializer.serialize(new MyObject(310));
- assertThatThrownBy(() -> Serializer.deserialize(MyObject.class, data)).isEqualTo(ex);
+ assertThatThrownBy(() -> Serializer.roundTrip(new MyObject(310))).isEqualTo(ex);
}
@Test
@@ -287,9 +284,20 @@ public class SerializerTest {
*/
text = text.replace("MyObject", "AnObject");
- byte[] data = text.getBytes(binary);
+ byte[] data2 = text.getBytes(binary);
+
+ /*
+ * Use a factory that returns a byte array for "data2" instead of the real "data".
+ */
+ setFactory(new Factory() {
+ @Override
+ public ByteArrayInputStream makeByteArrayInputStream(byte[] data) {
+ // read from "data2" instead of "data"
+ return super.makeByteArrayInputStream(data2);
+ }
+ });
- assertThatThrownBy(() -> Serializer.deserialize(MyObject.class, data)).isInstanceOf(IOException.class)
+ assertThatThrownBy(() -> Serializer.roundTrip(obj1)).isInstanceOf(IOException.class)
.hasCauseInstanceOf(ClassNotFoundException.class);
}
@@ -313,8 +321,7 @@ public class SerializerTest {
}
});
- byte[] data = Serializer.serialize(new MyObject(320));
- assertThatThrownBy(() -> Serializer.deserialize(MyObject.class, data)).isEqualTo(ex);
+ assertThatThrownBy(() -> Serializer.roundTrip(new MyObject(320))).isEqualTo(ex);
}
@Test
@@ -348,8 +355,7 @@ public class SerializerTest {
}
});
- byte[] data = Serializer.serialize(new MyObject(330));
- assertThatThrownBy(() -> Serializer.deserialize(MyObject.class, data)).isEqualTo(ex2);
+ assertThatThrownBy(() -> Serializer.roundTrip(new MyObject(330))).isEqualTo(ex2);
}
@Test
@@ -371,7 +377,7 @@ public class SerializerTest {
* @param factory new factory to be set
*/
private void setFactory(Factory factory) {
- Whitebox.setInternalState(Serializer.class, FACTORY, factory);
+ ReflectionTestUtils.setField(Serializer.class, FACTORY, factory);
}
/**