[LIB-2] Add snooker-score-api source files
[snooker-score-api.git] / src / test / java / org / hedgecode / snooker / json / AbstractJsonTest.java
diff --git a/src/test/java/org/hedgecode/snooker/json/AbstractJsonTest.java b/src/test/java/org/hedgecode/snooker/json/AbstractJsonTest.java
new file mode 100644 (file)
index 0000000..0ef6fa6
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2017. Developed by Hedgecode.
+ *
+ * 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 org.hedgecode.snooker.json;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+import com.google.gson.Gson;
+
+import org.hedgecode.snooker.gson.SnookerGsonBuilder;
+
+import org.junit.Assert;
+
+/**
+ * Abstract Test class with input JSON from file.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public abstract class AbstractJsonTest extends Assert {
+
+    private static final String JSON_EXT = ".json";
+    private static final String SERIALIZE_EXT = ".ser";
+
+    protected static final Gson GSON = SnookerGsonBuilder.build();
+
+    private final File jsonFile =
+            new File(
+                    getClass().getResource(
+                            getClass().getSimpleName() + JSON_EXT
+                    ).getFile()
+            );
+    private final File serFile =
+            new File(
+                    jsonFile.getParent() + File.separator
+                            + getClass().getSimpleName() + SERIALIZE_EXT
+            );
+
+    public static void assertNull(JsonIdEntity expected, JsonIdEntity[] actuals) {
+        if (actuals.length == 1 && actuals[0] == null) {
+                assertNull(expected);
+        }
+    }
+
+    protected String jsonFromFile() throws IOException {
+        StringBuilder sb = new StringBuilder();
+        try (BufferedReader br =
+                     new BufferedReader(
+                             new FileReader(
+                                     jsonFile.getAbsolutePath()
+                             )
+                     )
+        ) {
+            String line = br.readLine();
+            while (line != null) {
+                sb.append(line);
+                line = br.readLine();
+            }
+        }
+        return sb.toString();
+    }
+
+    protected void serialize(JsonSerializable jsonObject) throws IOException {
+        try (ObjectOutputStream oos =
+                     new ObjectOutputStream(
+                             new FileOutputStream(
+                                     serFile.getAbsolutePath()
+                             )
+                     )
+        ) {
+            oos.writeObject(jsonObject);
+        }
+    }
+
+    protected JsonSerializable deserialize() throws IOException, ClassNotFoundException {
+        JsonSerializable jsonObject;
+        try (ObjectInputStream ois =
+                     new ObjectInputStream(
+                             new FileInputStream(
+                                     serFile.getAbsolutePath()
+                             )
+                     )
+        ) {
+            jsonObject = (JsonSerializable) ois.readObject();
+        }
+        return jsonObject;
+    }
+
+}