bit.changepurse.wdk.bip.TestGithubUpdates.java Source code

Java tutorial

Introduction

Here is the source code for bit.changepurse.wdk.bip.TestGithubUpdates.java

Source

/*
 * This file is part of ChangePurse.
 *
 * Copyright (c) 2014 Germn Fuentes Capella
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package bit.changepurse.wdk.bip;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import bit.changepurse.wdk.WalletException;
import bit.changepurse.wdk.http.HTTPClient;
import bit.changepurse.wdk.http.HTTPMethod;
import bit.changepurse.wdk.http.HTTPRequest;
import bit.changepurse.wdk.http.HTTPResponse;
import bit.changepurse.wdk.http.HTTPStatusCode;
import bit.changepurse.wdk.util.GithubRef;

@RunWith(value = Parameterized.class)
public class TestGithubUpdates {

    private final Class<? extends Object> aClass;

    public TestGithubUpdates(Class<? extends Object> anotherClass) {
        aClass = anotherClass;
    }

    @Test
    public void testIfThereAreGithubUpdates() {
        GithubRef githubRef = extractGithubRef();
        String treeUrl = getTreeUrl(githubRef);
        HTTPResponse response = doGet(treeUrl);

        JSONObject json = new JSONObject(response.getBody());
        JSONArray treeContent = json.getJSONArray("tree");
        for (int i = 0; i < treeContent.length(); i++) {
            JSONObject file = treeContent.getJSONObject(i);
            if (githubRef.file().equals(file.getString("path"))) {
                assertThat(toString(file), file.getString("sha"), equalTo(githubRef.version()));
                break;
            }
        }
    }

    private String toString(JSONObject file) {
        String header = file.toString();
        HTTPResponse response = doGet(file.getString("url"));
        JSONObject json = new JSONObject(response.getBody());
        String body = json.getString("content");
        if ("base64".equals(json.getString("encoding"))) {
            body = new String(Base64.getDecoder().decode(body.replace("\n", "")));
        }
        return header + "\n" + body;
    }

    private HTTPResponse doGet(String treeUrl) {
        HTTPRequest request = new HTTPRequest().setUrl(treeUrl).setMethod(HTTPMethod.GET);
        try (HTTPClient client = new HTTPClient()) {
            HTTPResponse response = client.send(request);
            assertThat(response.getStatusCode(), equalTo(HTTPStatusCode.SUCCESS));
            return response;
        } catch (IOException e) {
            throw new WalletException(e);
        }
    }

    private GithubRef extractGithubRef() {
        GithubRef[] githubRef = aClass.getAnnotationsByType(GithubRef.class);
        assertThat(githubRef.length, equalTo(1));
        return githubRef[0];
    }

    private String getTreeUrl(GithubRef githubRef) {
        return String.format("https://api.github.com/repos/%s/git/trees/master", githubRef.repository());
    }

    @Parameters
    public static List<Object[]> data() {
        List<Object[]> classesToMonitor = new ArrayList<>();
        classesToMonitor.add(new Object[] { Bip39.class });
        return classesToMonitor;
    }
}