package com.mapbox.turf;

import com.google.gson.JsonParser;

import org.hamcrest.Matchers;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class TestUtils {

  public static final double DELTA = 1E-10;
  public static final String ACCESS_TOKEN = "pk.XXX";

  public void compareJson(String expectedJson, String actualJson) {
    JsonParser parser = new JsonParser();
    assertThat(parser.parse(actualJson), Matchers.equalTo(parser.parse(expectedJson)));
  }

  protected String loadJsonFixture(String filename) {
    ClassLoader classLoader = getClass().getClassLoader();
    InputStream inputStream = classLoader.getResourceAsStream(filename);
    Scanner scanner = new Scanner(inputStream, UTF_8.name()).useDelimiter("\\A");
    return scanner.hasNext() ? scanner.next() : "";
  }

  public static <T extends Serializable> byte[] serialize(T obj)
    throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(obj);
    oos.close();
    return baos.toByteArray();
  }

  public static <T extends Serializable> T deserialize(byte[] bytes, Class<T> cl)
    throws IOException, ClassNotFoundException {
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ObjectInputStream ois = new ObjectInputStream(bais);
    Object object = ois.readObject();
    return cl.cast(object);
  }

  /**
   * Comes from Google Utils Test Case
   */
  public static void expectNearNumber(double expected, double actual, double epsilon) {
    assertTrue(String.format("Expected %f to be near %f", actual, expected),
      Math.abs(expected - actual) <= epsilon);
  }

  protected List<String> getResourceFolderFileNames (String folder) {
    ClassLoader loader = getClass().getClassLoader();
    URL url = loader.getResource(folder);
    String path = url.getPath();
    List<String> names = new ArrayList<>();
    for (File file : new File(path).listFiles()) {
      names.add(file.getName());
    }
    return names;
  }
}

@@ -214,36 +214,8 @@
 on;%0A
-import java.io.InputStream;%0A
 impo
@@ -236,32 +236,32 @@
 ectInputStream;%0A
+
 import java.io.O
@@ -329,16 +329,72 @@
 et.URL;%0A
+import java.nio.file.Files;%0Aimport java.nio.file.Paths;%0A
 import j
@@ -439,34 +439,8 @@
 ist;
-%0Aimport java.util.Scanner;
 %0A%0Aim
@@ -577,16 +577,53 @@
 ertTrue;
+%0Aimport static org.junit.Assert.fail;
 %0A%0Apublic
@@ -1006,269 +1006,271 @@
     
-ClassLoader classLoader = getClass().getClassLoader();%0A    InputStream inputStream = classLoader.getResourceAsStream(filename);%0A    Scanner scanner = new Scanner(inputStream, UTF_8.name()).useDelimiter(%22%5C%5CA%22);%0A    return scanner.hasNext() ? scanner.next() : %22%22;
+try %7B%0A      String filepath = %22src/test/resources/%22 + filename;%0A      byte%5B%5D encoded = Files.readAllBytes(Paths.get(filepath));%0A      return new String(encoded, UTF_8);%0A    %7D catch (IOException e) %7B%0A      fail(%22Unable to load %22 + filename);%0A      return %22%22;%0A    %7D
 %0A  %7D

Fix tests running locally