Search in sources :

Example 11 with AppData

use of com.jakdor.labday.common.model.AppData in project LabDayApp by jakdor.

the class RetrofitBuilderTest method createServiceWithInterceptor.

/**
 * test RetrofitBuilder in isolation from AuthenticationInterceptor
 */
@Test
public void createServiceWithInterceptor() throws Exception {
    Request request = new Request.Builder().header("Authorization", dummyToken).get().url(dummyApiUrl).build();
    ResponseBody dummyResponse = new ResponseBody() {

        @Override
        public MediaType contentType() {
            return null;
        }

        @Override
        public long contentLength() {
            return 0;
        }

        @Override
        public BufferedSource source() {
            return null;
        }
    };
    when(authenticationInterceptor.intercept(any())).thenReturn(new Response.Builder().request(request).protocol(Protocol.HTTP_2).code(200).body(dummyResponse).message(message).build());
    LabService labService = retrofitBuilder.createService(dummyApiUrl, LabService.class, dummyToken);
    TestObserver<AppData> testObserver = labService.getAppData().test();
    testObserver.assertSubscribed();
    testObserver.onComplete();
    InOrder order = inOrder(authenticationInterceptor);
    order.verify(authenticationInterceptor, calls(1)).setAuthToken(dummyToken);
    order.verify(authenticationInterceptor, calls(1)).intercept(any());
}
Also used : Response(okhttp3.Response) LabService(com.jakdor.labday.common.network.LabService) InOrder(org.mockito.InOrder) AppData(com.jakdor.labday.common.model.AppData) RetrofitBuilder(com.jakdor.labday.common.network.RetrofitBuilder) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 12 with AppData

use of com.jakdor.labday.common.model.AppData in project LabDayApp by jakdor.

the class MainFragmentTest method initMockData.

private void initMockData() {
    AppData data = new AppData();
    ArrayList<Path> paths = new ArrayList<>();
    paths.add(new Path(1, "dummyNameWrong", "PathInfoWrong", false));
    paths.add(new Path(2, "dummyName", "PathInfo", true));
    data.setPaths(paths);
    appData.setValue(RxResponse.success(data));
    loadingStatus.setValue(false);
}
Also used : Path(com.jakdor.labday.common.model.Path) AppData(com.jakdor.labday.common.model.AppData) ArrayList(java.util.ArrayList)

Example 13 with AppData

use of com.jakdor.labday.common.model.AppData in project LabDayApp by jakdor.

the class LocalDbIntegrationTests method integrationTestSaveAndLoad.

/**
 * Save and load {@link com.jakdor.labday.common.model.AppData} to local db
 */
@Test
public void integrationTestSaveAndLoad() throws Exception {
    Gson gson = new Gson();
    AppData appData = gson.fromJson(readFile(appDataJsonPath), AppData.class);
    localDbHandler.pushAppDataToDb(appData);
    TestObserver<RxResponse<AppData>> testObserver = localDbHandler.getAppDataFromDb().test();
    testObserver.assertSubscribed();
    testObserver.awaitCount(1);
    testObserver.assertNoErrors();
    testObserver.assertValue(appDataRxResponse -> {
        Assert.assertEquals(RxStatus.SUCCESS_DB, appDataRxResponse.status);
        Assert.assertNotNull(appDataRxResponse.data);
        Assert.assertNull(appDataRxResponse.error);
        Assert.assertEquals(appData, appDataRxResponse.data);
        Assert.assertEquals(appData.hashCode(), appDataRxResponse.data.hashCode());
        Assert.assertNotNull(appDataRxResponse.data.getEvents());
        Assert.assertNotNull(appDataRxResponse.data.getMapOthers());
        Assert.assertNotNull(appDataRxResponse.data.getPaths());
        Assert.assertNotNull(appDataRxResponse.data.getSpeakers());
        Assert.assertNotNull(appDataRxResponse.data.getTimetables());
        return true;
    });
    testObserver.onComplete();
}
Also used : AppData(com.jakdor.labday.common.model.AppData) Gson(com.google.gson.Gson) RxResponse(com.jakdor.labday.rx.RxResponse) Test(org.junit.Test)

Example 14 with AppData

use of com.jakdor.labday.common.model.AppData in project LabDayApp by jakdor.

the class ProjectRepositoryIntegrationTest method integrationUpdateTestScenario1.

/**
 * {@link ProjectRepository} getUpdate() integration test scenario 1
 * - local last update id doesn't match API id
 * - get API last update id
 * - get appData API response (successful)
 * - check ProjectRepository after successful call
 */
@Test
public void integrationUpdateTestScenario1() throws Exception {
    SharedPreferences sharedPreferences = targetContext.getSharedPreferences(targetContext.getString(R.string.pref_file_name), Context.MODE_PRIVATE);
    sharedPreferences.edit().putString(targetContext.getString(R.string.pref_api_last_update_id), "0").commit();
    Assert.assertEquals(projectRepository.getRepositoryState(), ProjectRepository.repositoryStates.INIT);
    Assert.assertNull(projectRepository.getData());
    projectRepository.setAccessToken(dummyToken);
    Gson gson = new Gson();
    AppData appData = gson.fromJson(readAssetFile(testContext, "api/app_data.json"), AppData.class);
    TestObserver<RxResponse<AppData>> testObserver = projectRepository.getUpdate(dummyApiUrl, targetContext).subscribeOn(Schedulers.io()).doOnError(throwable -> Assert.fail()).test();
    testObserver.assertSubscribed();
    testObserver.awaitCount(1);
    testObserver.assertNoErrors();
    testObserver.assertValue(appDataRxResponse -> {
        Assert.assertNotNull(appDataRxResponse);
        Assert.assertNotNull(appDataRxResponse.data);
        Assert.assertNull(appDataRxResponse.error);
        Assert.assertEquals(RxStatus.SUCCESS, appDataRxResponse.status);
        Assert.assertEquals(appData, appDataRxResponse.data);
        Assert.assertEquals(appData.hashCode(), appDataRxResponse.data.hashCode());
        Assert.assertEquals(projectRepository.getRepositoryState(), ProjectRepository.repositoryStates.READY);
        Assert.assertNotNull(projectRepository.getData());
        Assert.assertEquals(projectRepository.getData().data, appData);
        LastUpdate expectedLastUpdate = gson.fromJson(readAssetFile(testContext, "api/last_update.json"), LastUpdate.class);
        Assert.assertNotNull(expectedLastUpdate.getUpdatedAt());
        Assert.assertEquals(new String(expectedLastUpdate.getUpdatedAt().toCharArray()), sharedPreferences.getString(targetContext.getString(R.string.pref_api_last_update_id), null));
        return true;
    });
    testObserver.onComplete();
}
Also used : Context(android.content.Context) TestUtils.readAssetFile(com.jakdor.labday.TestUtils.readAssetFile) R(com.jakdor.labday.R) Instrumentation(android.app.Instrumentation) LabService(com.jakdor.labday.common.network.LabService) ApplicationProvider(androidx.test.core.app.ApplicationProvider) ProjectRepository(com.jakdor.labday.common.repository.ProjectRepository) LocalDbHandler(com.jakdor.labday.common.localdb.LocalDbHandler) Charset(java.nio.charset.Charset) Gson(com.google.gson.Gson) RxSchedulersFacade(com.jakdor.labday.rx.RxSchedulersFacade) After(org.junit.After) Schedulers(io.reactivex.schedulers.Schedulers) ExpectedException(org.junit.rules.ExpectedException) RxStatus(com.jakdor.labday.rx.RxStatus) Before(org.junit.Before) NetworkManager(com.jakdor.labday.common.repository.NetworkManager) AppData(com.jakdor.labday.common.model.AppData) RetrofitBuilder(com.jakdor.labday.common.network.RetrofitBuilder) InstrumentationRegistry(androidx.test.platform.app.InstrumentationRegistry) RxResponse(com.jakdor.labday.rx.RxResponse) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) TestObserver(io.reactivex.observers.TestObserver) LastUpdate(com.jakdor.labday.common.model.LastUpdate) Rule(org.junit.Rule) SharedPreferences(android.content.SharedPreferences) SoLoader(com.facebook.soloader.SoLoader) Assert(org.junit.Assert) TestApp(com.jakdor.labday.TestApp) SharedPreferences(android.content.SharedPreferences) AppData(com.jakdor.labday.common.model.AppData) LastUpdate(com.jakdor.labday.common.model.LastUpdate) Gson(com.google.gson.Gson) RxResponse(com.jakdor.labday.rx.RxResponse) Test(org.junit.Test)

Example 15 with AppData

use of com.jakdor.labday.common.model.AppData in project LabDayApp by jakdor.

the class RetrofitAPICallsIntegrationTest method appDataTokenTest.

/**
 * Tests response from AppData API call with token authorization
 */
@Test
public void appDataTokenTest() throws Exception {
    Gson gson = new Gson();
    AppData appData = gson.fromJson(readFile(appDataJsonPath), AppData.class);
    LabService labService = retrofitBuilder.createService(dummyApiUrl, LabService.class, dummyToken);
    TestObserver<AppData> testObserver = labService.getAppData().test();
    testObserver.assertSubscribed();
    testObserver.awaitCount(1);
    testObserver.assertNoErrors();
    testObserver.assertValue(appData1 -> {
        assertEquals(appData, appData1);
        assertEquals(appData.hashCode(), appData1.hashCode());
        return true;
    });
    testObserver.onComplete();
}
Also used : LabService(com.jakdor.labday.common.network.LabService) AppData(com.jakdor.labday.common.model.AppData) Gson(com.google.gson.Gson) Test(org.junit.Test)

Aggregations

AppData (com.jakdor.labday.common.model.AppData)15 Test (org.junit.Test)12 Gson (com.google.gson.Gson)10 RxResponse (com.jakdor.labday.rx.RxResponse)9 LabService (com.jakdor.labday.common.network.LabService)7 Context (android.content.Context)6 SharedPreferences (android.content.SharedPreferences)6 R (com.jakdor.labday.R)6 LocalDbHandler (com.jakdor.labday.common.localdb.LocalDbHandler)6 LastUpdate (com.jakdor.labday.common.model.LastUpdate)6 RetrofitBuilder (com.jakdor.labday.common.network.RetrofitBuilder)6 RxSchedulersFacade (com.jakdor.labday.rx.RxSchedulersFacade)6 RxStatus (com.jakdor.labday.rx.RxStatus)6 FileOutputStream (java.io.FileOutputStream)6 Charset (java.nio.charset.Charset)6 Instrumentation (android.app.Instrumentation)5 ApplicationProvider (androidx.test.core.app.ApplicationProvider)5 InstrumentationRegistry (androidx.test.platform.app.InstrumentationRegistry)5 SoLoader (com.facebook.soloader.SoLoader)5 TestApp (com.jakdor.labday.TestApp)5