diff --git a/app/src/main/java/org/openobservatory/ooniprobe/common/Application.java b/app/src/main/java/org/openobservatory/ooniprobe/common/Application.java index a0cc430ee..2b1ce3c06 100644 --- a/app/src/main/java/org/openobservatory/ooniprobe/common/Application.java +++ b/app/src/main/java/org/openobservatory/ooniprobe/common/Application.java @@ -100,10 +100,6 @@ public OONICheckInConfig getOONICheckInConfig() { public ActivityComponent getActivityComponent() { return component.activityComponent(); } - public OkHttpClient getOkHttpClient() { - return _okHttpClient; - } - public OONIAPIClient getApiClient() { return _apiClient; } diff --git a/app/src/test/java/org/openobservatory/ooniprobe/client/OONIAPIClientTest.java b/app/src/test/java/org/openobservatory/ooniprobe/client/OONIAPIClientTest.java deleted file mode 100644 index 9bdb9cdc5..000000000 --- a/app/src/test/java/org/openobservatory/ooniprobe/client/OONIAPIClientTest.java +++ /dev/null @@ -1,156 +0,0 @@ -package org.openobservatory.ooniprobe.client; - -import com.google.gson.GsonBuilder; -import com.google.gson.JsonParser; -import com.raizlabs.android.dbflow.sql.language.Delete; - -import org.apache.commons.io.FileUtils; -import org.jetbrains.annotations.NotNull; -import org.junit.Assert; -import org.junit.Test; -import org.openobservatory.ooniprobe.RobolectricAbstractTest; -import org.openobservatory.ooniprobe.client.callback.CheckReportIdCallback; -import org.openobservatory.ooniprobe.domain.callback.GetMeasurementsCallback; -import org.openobservatory.ooniprobe.model.database.Measurement; - -import java.io.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -import okhttp3.Call; -import okhttp3.Callback; -import okhttp3.Request; -import okhttp3.Response; - -public class OONIAPIClientTest extends RobolectricAbstractTest { - - private static final String EXISTING_REPORT_ID = "20190113T202156Z_AS327931_CgoC3KbgM6zKajvIIt1AxxybJ1HbjwwWJjsJnlxy9rpcGY54VH"; - private static final String NONEXISTING_REPORT_ID = "EMPTY"; - private static final String CLIENT_URL = "https://ams-pg.ooni.org"; - private static final String NON_EXISTING_MEASUREMENT_URL = CLIENT_URL + "/api/v1/measurement/nonexistent-measurement-url"; - - @Test - public void getMeasurementSuccess() { - final CountDownLatch signal = new CountDownLatch(1); - a.getApiClient().getMeasurement(EXISTING_REPORT_ID, null).enqueue(new GetMeasurementsCallback() { - @Override - public void onSuccess(String result) { - Assert.assertNotNull(result); - } - - @Override - public void onError(String msg) { - Assert.fail(); - signal.countDown(); - } - }); - try { - signal.await(1, TimeUnit.MINUTES); - } catch (InterruptedException e) { - e.printStackTrace(); - Assert.fail(); - } - } - - @Test - public void getMeasurementError() { - final CountDownLatch signal = new CountDownLatch(1); - a.getApiClient().getMeasurement(NONEXISTING_REPORT_ID, null).enqueue(new GetMeasurementsCallback() { - @Override - public void onSuccess(String result) { - Assert.fail(); - signal.countDown(); - } - - @Override - public void onError(String msg) { - signal.countDown(); - } - }); - try { - signal.await(1, TimeUnit.MINUTES); - } catch (InterruptedException e) { - e.printStackTrace(); - Assert.fail(); - } - } - - @Test - public void testSelectMeasurementsWithJson() throws IOException { - final CountDownLatch signal = new CountDownLatch(1); - Delete.table(Measurement.class); - addMeasurement(EXISTING_REPORT_ID, false); - a.getApiClient().checkReportId(EXISTING_REPORT_ID).enqueue(new CheckReportIdCallback() { - @Override - public void onSuccess(Boolean found) { - Assert.assertTrue(found); - signal.countDown(); - } - - @Override - public void onError(String msg) { - signal.countDown(); - } - }); - try { - signal.await(1, TimeUnit.MINUTES); - } catch (InterruptedException e) { - e.printStackTrace(); - Assert.fail(); - } - } - - @Test - public void getMeasurementJsonError() { - final CountDownLatch signal = new CountDownLatch(1); - a.getOkHttpClient().newCall(new Request.Builder().url(NON_EXISTING_MEASUREMENT_URL).build()).enqueue(new Callback() { - @Override - public void onResponse(@NotNull Call call, @NotNull Response response) { - try { - new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create() - .toJson(JsonParser.parseString(response.body().string())); - Assert.fail(); - } catch (Exception ignored) { } - signal.countDown(); - } - - @Override - public void onFailure(@NotNull Call call, @NotNull IOException e) { - signal.countDown(); - } - }); - try { - signal.await(1, TimeUnit.MINUTES); - } catch (InterruptedException e) { - e.printStackTrace(); - Assert.fail(); - } - } - - private Measurement addMeasurement(String report_id, Boolean write_file) throws IOException { - //Simulating measurement done and uploaded - Measurement measurement = new Measurement(); - measurement.report_id = report_id; - measurement.is_done = true; - measurement.is_uploaded = true; - measurement.save(); - if (write_file) { - File entryFile = Measurement.getReportFile(a, measurement.id, measurement.test_name); - File parentFile = entryFile.getParentFile(); - - if (parentFile == null) { - return measurement; - } - - parentFile.mkdirs(); - FileUtils.writeStringToFile( - entryFile, - "", - StandardCharsets.UTF_8 - ); - } - return measurement; - } -} diff --git a/app/src/test/java/org/openobservatory/ooniprobe/client/OONIAPIClientTest.kt b/app/src/test/java/org/openobservatory/ooniprobe/client/OONIAPIClientTest.kt new file mode 100644 index 000000000..670054ec8 --- /dev/null +++ b/app/src/test/java/org/openobservatory/ooniprobe/client/OONIAPIClientTest.kt @@ -0,0 +1,47 @@ +package org.openobservatory.ooniprobe.client + +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertTrue +import org.junit.Test +import org.openobservatory.ooniprobe.RobolectricAbstractTest + +class OONIAPIClientTest : RobolectricAbstractTest() { + + @Test + fun measurementSuccess() { + val response = apiClient.getMeasurement(EXISTING_REPORT_ID, null).execute() + + assertTrue(response.isSuccessful) + response.body().use { body -> + assertNotNull(body) + assertNotNull(body!!.string()) + } + } + + @Test + fun measurementError() { + val response = apiClient.getMeasurement(NON_EXISTING_REPORT_ID, null).execute() + + assertFalse(response.isSuccessful) + } + + @Test + fun testSelectMeasurementsWithJson() { + val response = apiClient.checkReportId(EXISTING_REPORT_ID).execute() + + assertTrue(response.isSuccessful) + with(response.body()) { + assertNotNull(this) + assertTrue(this!!.found) + } + } + + private val apiClient get() = a.apiClient + + companion object { + private const val EXISTING_REPORT_ID = + "20190113T202156Z_AS327931_CgoC3KbgM6zKajvIIt1AxxybJ1HbjwwWJjsJnlxy9rpcGY54VH" + private const val NON_EXISTING_REPORT_ID = "EMPTY" + } +} diff --git a/shared-test/src/main/java/org/openobservatory/ooniprobe/di/TestAppModule.java b/shared-test/src/main/java/org/openobservatory/ooniprobe/di/TestAppModule.java index 97ee7ef76..f7b53e959 100644 --- a/shared-test/src/main/java/org/openobservatory/ooniprobe/di/TestAppModule.java +++ b/shared-test/src/main/java/org/openobservatory/ooniprobe/di/TestAppModule.java @@ -8,7 +8,7 @@ @Module public class TestAppModule extends ApplicationModule { - private static final String CLIENT_URL = "https://ams-pg.ooni.org"; + private static final String CLIENT_URL = "https://backend-hel.ooni.org"; public TestAppModule(Application application) { super(application);