Provide basic tests for JSON serialization of (maven.data) Dependency
*/
package eu.fasten.core.maven.data;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import java.util.HashMap;
import java.util.HashSet;
+import java.util.Set;
+import org.json.JSONArray;
+import org.json.JSONObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class DependencyTest {
+ private static final Dependency SOME_DEPENDENCY = new Dependency("gid", "aid",
+ Set.of(new VersionConstraint("(1.2.3,2.3.4]")), Set.of(new Exclusion("gid2", "aid2")), "compile", false,
+ "jar", "sources");
+
@Test
public void dependencyTest() {
var expected = new Dependency("junit", "junit", "4.11");
@@ -54,6 +64,54 @@ public class DependencyTest {
});
}
+ @Test
+ public void jsonRoundtripViaObj() {
+ var a = SOME_DEPENDENCY;
+ var jsonObj = a.toJSON();
+ var b = Dependency.fromJSON(jsonObj);
+ assertEquals(a, b);
+ assertEquals(a.hashCode(), b.hashCode());
+ }
+
+ @Test
+ public void jsonRoundtripViaString() {
+ var a = SOME_DEPENDENCY;
+ var json = a.toJSON().toString();
+ var jsonObj = new JSONObject(json);
+ var b = Dependency.fromJSON(jsonObj);
+ assertEquals(a, b);
+ assertEquals(a.hashCode(), b.hashCode());
+ }
+
+ @Test
+ public void jsonHasRightFields() {
+
+ var expected = new HashMap<String, Class<?>>();
+ expected.put("artifactId", String.class);
+ expected.put("groupId", String.class);
+ expected.put("type", String.class);
+ expected.put("versionConstraints", JSONArray.class);
+ expected.put("exclusions", JSONArray.class);
+ expected.put("scope", String.class);
+ expected.put("classifier", String.class);
+ expected.put("optional", Boolean.class);
+
+ var actual = SOME_DEPENDENCY.toJSON();
+ for (var expectedField : expected.keySet()) {
+ var expectedType = expected.get(expectedField);
+
+ if (!actual.has(expectedField)) {
+ fail(String.format("Resulting json object is missing field '%s'", expectedField));
+ }
+ var obj = actual.get(expectedField);
+ assertNotNull(obj);
+ var objType = obj.getClass();
+ if (!expectedType.isAssignableFrom(objType)) {
+ fail(String.format("Expected type %s, but was %s", expectedType, objType));
+ }
+ }
+ }
+
@Test
public void equality() {
var a = someDep();
