Search in sources :

Example 1 with JSONCompareResult

use of org.skyscreamer.jsonassert.JSONCompareResult in project jsonschema2pojo by joelittlejohn.

the class JsonAssert method assertEqualsJson.

public static void assertEqualsJson(String expectedJson, String actualJson, JSONCompareMode compareMode) {
    try {
        JSONCompareResult result = compareJSON(expectedJson, actualJson, compareMode);
        if (result.failed()) {
            String failureMessage = result.getMessage();
            if (failureMessage != null) {
                failureMessage = failureMessage.replaceAll(" ; ", "\n");
            }
            failureMessage = "\n================ Expected JSON ================" + new JSONObject(expectedJson).toString(4) + "\n================= Actual JSON =================" + new JSONObject(actualJson).toString(4) + "\n================= Error List ==================\n" + failureMessage + "\n\n";
            fail(failureMessage);
        }
    } catch (JSONException e) {
        throw new RuntimeException("JSON completely failed to parse json", e);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONCompareResult(org.skyscreamer.jsonassert.JSONCompareResult) JSONException(org.json.JSONException)

Example 2 with JSONCompareResult

use of org.skyscreamer.jsonassert.JSONCompareResult in project spring-boot by spring-projects.

the class JsonContentAssert method compareForNull.

private JSONCompareResult compareForNull(CharSequence expectedJson) {
    JSONCompareResult result = new JSONCompareResult();
    result.passed();
    if (expectedJson != null) {
        result.fail("Expected null JSON");
    }
    return result;
}
Also used : JSONCompareResult(org.skyscreamer.jsonassert.JSONCompareResult)

Example 3 with JSONCompareResult

use of org.skyscreamer.jsonassert.JSONCompareResult in project clutchandroid by clutchio.

the class ClutchSync method sync.

public static void sync(ClutchStats clutchStats) {
    if (thisIsHappening) {
        return;
    }
    thisIsHappening = true;
    if (pendingReload) {
        pendingReload = false;
        for (ClutchView clutchView : clutchViews) {
            clutchView.contentChanged();
        }
    }
    ClutchAPIClient.callMethod("sync", null, new ClutchAPIResponseHandler() {

        @Override
        public void onSuccess(JSONObject response) {
            final AssetManager mgr = context.getAssets();
            File parentCacheDir = context.getCacheDir();
            final File tempDir;
            try {
                tempDir = File.createTempFile("clutchtemp", Long.toString(System.nanoTime()), parentCacheDir);
                if (!tempDir.delete()) {
                    Log.e(TAG, "Could not delete temp file: " + tempDir.getAbsolutePath());
                    return;
                }
                if (!tempDir.mkdir()) {
                    Log.e(TAG, "Could not create temp directory: " + tempDir.getAbsolutePath());
                    return;
                }
            } catch (IOException e) {
                Log.e(TAG, "Could not create temp file");
                return;
            }
            File cacheDir = getCacheDir();
            if (cacheDir == null) {
                try {
                    if (!copyAssetDir(mgr, tempDir)) {
                        return;
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Couldn't copy the asset dir files to the temp dir: " + e);
                    return;
                }
            } else {
                try {
                    if (!copyDir(cacheDir, tempDir)) {
                        return;
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Couldn't copy the cache dir files to the temp dir: " + e);
                    return;
                }
            }
            conf = response.optJSONObject("conf");
            String version = "" + conf.optInt("_version");
            newFilesDownloaded = false;
            try {
                JSONCompareResult confCompare = JSONCompare.compareJSON(ClutchConf.getConf(), conf, JSONCompareMode.NON_EXTENSIBLE);
                if (confCompare.failed()) {
                    newFilesDownloaded = true;
                // This is where in the ObjC version we write out the conf, but I don't think we need to anymore
                }
            } catch (JSONException e1) {
                Log.i(TAG, "Couldn't compare the conf file with the cached conf file: " + e1);
            }
            File cachedFiles = new File(tempDir, "__files.json");
            JSONObject cached = null;
            if (cachedFiles.exists()) {
                StringBuffer strContent = new StringBuffer("");
                try {
                    FileInputStream in = new FileInputStream(cachedFiles);
                    int ch;
                    while ((ch = in.read()) != -1) {
                        strContent.append((char) ch);
                    }
                    in.close();
                    cached = new JSONObject(new JSONTokener(strContent.toString()));
                } catch (IOException e) {
                    Log.e(TAG, "Could not read __files.json from cache file: " + e);
                } catch (JSONException e) {
                    Log.e(TAG, "Could not parse __files.json from cache file: " + e);
                }
            }
            if (cached == null) {
                cached = new JSONObject();
            }
            final JSONObject files = response.optJSONObject("files");
            try {
                JSONCompareResult filesCompare = JSONCompare.compareJSON(cached, files, JSONCompareMode.NON_EXTENSIBLE);
                if (filesCompare.passed()) {
                    complete(tempDir, files);
                    return;
                }
            } catch (JSONException e1) {
                Log.i(TAG, "Couldn't compare the file hash list with the cached file hash list: " + e1);
            }
            try {
                BufferedWriter bw = new BufferedWriter(new FileWriter(cachedFiles));
                bw.write(files.toString());
                bw.flush();
                bw.close();
            } catch (FileNotFoundException e) {
            } catch (IOException e) {
            }
            currentFile = 0;
            final int numFiles = files.length();
            Iterator<?> it = files.keys();
            while (it.hasNext()) {
                final String fileName = (String) it.next();
                final String hash = files.optString(fileName);
                final String prevHash = cached.optString(fileName);
                // If they equal, then just continue
                if (hash.equals(prevHash)) {
                    if (++currentFile == numFiles) {
                        complete(tempDir, files);
                        return;
                    }
                    continue;
                }
                // Looks like we've seen a new file, so we should reload when this is all done
                newFilesDownloaded = true;
                // Otherwise we need to download the new file
                ClutchAPIClient.downloadFile(fileName, version, new ClutchAPIDownloadResponseHandler() {

                    @Override
                    public void onSuccess(String response) {
                        try {
                            File fullFile = new File(tempDir, fileName);
                            fullFile.getParentFile().mkdirs();
                            fullFile.createNewFile();
                            BufferedWriter bw = new BufferedWriter(new FileWriter(fullFile));
                            bw.write(response);
                            bw.flush();
                            bw.close();
                        } catch (IOException e) {
                            final Writer result = new StringWriter();
                            final PrintWriter printWriter = new PrintWriter(result);
                            e.printStackTrace(printWriter);
                            Log.e(TAG, "Tried, but could not write file: " + fileName + " : " + result);
                        }
                        if (++currentFile == numFiles) {
                            complete(tempDir, files);
                            return;
                        }
                    }

                    @Override
                    public void onFailure(Throwable e, String content) {
                        final Writer result = new StringWriter();
                        final PrintWriter printWriter = new PrintWriter(result);
                        e.printStackTrace(printWriter);
                        Log.e(TAG, "Error downloading file from server: " + fileName + " " + result + " " + content);
                        if (++currentFile == numFiles) {
                            complete(tempDir, files);
                            return;
                        }
                    }
                });
            }
        }

        @Override
        public void onFailure(Throwable e, JSONObject errorResponse) {
            Log.e(TAG, "Failed to sync with the Clutch server: " + errorResponse);
        }
    });
    background(clutchStats);
}
Also used : AssetManager(android.content.res.AssetManager) JSONCompareResult(org.skyscreamer.jsonassert.JSONCompareResult) FileWriter(java.io.FileWriter) FileNotFoundException(java.io.FileNotFoundException) JSONException(org.json.JSONException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BufferedWriter(java.io.BufferedWriter) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) StringWriter(java.io.StringWriter) Iterator(java.util.Iterator) File(java.io.File) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) StringWriter(java.io.StringWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Aggregations

JSONCompareResult (org.skyscreamer.jsonassert.JSONCompareResult)3 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 AssetManager (android.content.res.AssetManager)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 Iterator (java.util.Iterator)1 JSONTokener (org.json.JSONTokener)1