Search in sources :

Example 1 with User

use of org.hisp.dhis.android.core.user.User in project dhis2-android-sdk by dhis2.

the class MetadataCallShould method setUp.

@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    errorResponse = Response.error(HttpsURLConnection.HTTP_CLIENT_TIMEOUT, ResponseBody.create(MediaType.parse("application/json"), "{}"));
    when(systemInfoService.getSystemInfo(any(Fields.class))).thenReturn(systemInfoCall);
    when(userService.getUser(any(Fields.class))).thenReturn(userCall);
    when(programService.getProgramsForAccess(any(Fields.class), any(Filter.class), anyBoolean())).thenReturn(programWithAccessCall);
    when(programService.getPrograms(any(Fields.class), any(Filter.class), any(Filter.class), anyBoolean())).thenReturn(programCall);
    when(trackedEntityService.trackedEntities(any(Fields.class), any(Filter.class), any(Filter.class), anyBoolean())).thenReturn(trackedEntityCall);
    when(optionSetService.optionSets(anyBoolean(), any(Fields.class), any(Filter.class))).thenReturn(optionSetCall);
    List<UserRole> userRoles = new ArrayList<>();
    userRoles.add(userRole);
    when(systemInfo.serverDate()).thenReturn(serverDateTime);
    when(userCredentials.userRoles()).thenReturn(userRoles);
    when(organisationUnit.uid()).thenReturn("unit");
    when(organisationUnit.path()).thenReturn("path/to/org/unit");
    when(user.userCredentials()).thenReturn(userCredentials);
    when(user.organisationUnits()).thenReturn(Collections.singletonList(organisationUnit));
    when(organisationUnitPayload.items()).thenReturn(Collections.singletonList(organisationUnit));
    when(dataAccess.read()).thenReturn(true);
    when(access.data()).thenReturn(dataAccess);
    when(programWithAccess.access()).thenReturn(access);
    when(program.trackedEntity()).thenReturn(trackedEntity);
    when(program.access()).thenReturn(access);
    when(program.programStages()).thenReturn(Collections.singletonList(programStageWithUid));
    when(programStageWithUid.uid()).thenReturn("program_stage_uid");
    when(programWithAccessPayload.items()).thenReturn(Collections.singletonList(programWithAccess));
    when(programPayload.items()).thenReturn(Collections.singletonList(program));
    when(trackedEntityPayload.items()).thenReturn(Collections.singletonList(trackedEntity));
    when(trackedEntity.uid()).thenReturn("test_tracked_entity_uid");
    when(optionSetPayload.items()).thenReturn(Collections.singletonList(optionSet));
    when(dataElementPayload.items()).thenReturn(Collections.singletonList(dataElement));
    when(resourceStore.getLastUpdated(any(ResourceModel.Type.class))).thenReturn("2017-01-01");
    when(dataSetParentCallFactory.create(any(User.class), any(GenericCallData.class), any(List.class))).thenReturn(dataSetParentCall);
    when(organisationUnitCallFactory.create(any(GenericCallData.class), any(User.class), anySetOf(String.class))).thenReturn(organisationUnitEndpointCall);
    Response<Payload<DataElement>> dataSetParentCallResponse = Response.success(dataElementPayload);
    when(dataSetParentCall.call()).thenReturn(dataSetParentCallResponse);
    when(programStageCallFactory.create(any(GenericCallData.class), any(Set.class))).thenReturn(programStageEndpointCall);
    programStageResponse = Response.success(programStagePayload);
    when(programStageEndpointCall.call()).thenReturn(programStageResponse);
    dhis2MockServer = new Dhis2MockServer(new ResourcesFileReader());
    Retrofit retrofit = new Retrofit.Builder().baseUrl(dhis2MockServer.getBaseEndpoint()).addConverterFactory(JacksonConverterFactory.create(new ObjectMapper())).addConverterFactory(FilterConverterFactory.create()).addConverterFactory(FieldsConverterFactory.create()).build();
    CategoryService categoryService = retrofit.create(CategoryService.class);
    CategoryComboService comboService = retrofit.create(CategoryComboService.class);
    dhis2MockServer.enqueueMockResponse("categories.json");
    dhis2MockServer.enqueueMockResponse("category_combos.json");
    metadataCall = new MetadataCall(databaseAdapter, systemInfoService, userService, programService, trackedEntityService, optionSetService, systemInfoStore, resourceStore, userStore, userCredentialsStore, userRoleStore, programStore, trackedEntityAttributeStore, programTrackedEntityAttributeStore, programRuleVariableStore, programIndicatorStore, programStageSectionProgramIndicatorLinkStore, programRuleActionStore, programRuleStore, relationshipStore, trackedEntityStore, categoryQuery, categoryService, categoryHandler, CategoryComboQuery.defaultQuery(), comboService, mockCategoryComboHandler, optionSetHandler, organisationUnitCallFactory, dataSetParentCallFactory, styleHandler, renderTypeHandler, programStageCallFactory, retrofit);
    when(databaseAdapter.beginNewTransaction()).thenReturn(transaction);
    when(systemInfoCall.execute()).thenReturn(Response.success(systemInfo));
    when(userCall.execute()).thenReturn(Response.success(user));
    when(programCall.execute()).thenReturn(Response.success(programPayload));
    when(programWithAccessCall.execute()).thenReturn(Response.success(programWithAccessPayload));
    when(trackedEntityCall.execute()).thenReturn(Response.success(trackedEntityPayload));
    when(optionSetCall.execute()).thenReturn(Response.success(optionSetPayload));
    when(organisationUnitEndpointCall.call()).thenReturn(Response.success(organisationUnitPayload));
}
Also used : Dhis2MockServer(org.hisp.dhis.android.core.data.server.Dhis2MockServer) User(org.hisp.dhis.android.core.user.User) OptionSet(org.hisp.dhis.android.core.option.OptionSet) Set(java.util.Set) ArrayList(java.util.ArrayList) CategoryComboService(org.hisp.dhis.android.core.category.CategoryComboService) Retrofit(retrofit2.Retrofit) MediaType(okhttp3.MediaType) Fields(org.hisp.dhis.android.core.data.api.Fields) MetadataCall(org.hisp.dhis.android.core.calls.MetadataCall) Filter(org.hisp.dhis.android.core.data.api.Filter) UserRole(org.hisp.dhis.android.core.user.UserRole) ResourcesFileReader(org.hisp.dhis.android.core.data.file.ResourcesFileReader) List(java.util.List) ArrayList(java.util.ArrayList) CategoryService(org.hisp.dhis.android.core.category.CategoryService) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Before(org.junit.Before)

Example 2 with User

use of org.hisp.dhis.android.core.user.User in project dhis2-android-sdk by dhis2.

the class OrganisationUnitCallMockIntegrationShould method setUp.

@Before
@Override
public void setUp() throws IOException {
    super.setUp();
    dhis2MockServer = new Dhis2MockServer(new AssetsFileReader());
    D2 d2 = D2Factory.create(dhis2MockServer.getBaseEndpoint(), databaseAdapter());
    dhis2MockServer.enqueueMockResponse("admin/organisation_units.json");
    List<OrganisationUnit> organisationUnits = Collections.singletonList(OrganisationUnit.create("O6uvpzGd5pu", null, null, null, null, null, null, null, null, null, null, "/ImspTQPwCqd/O6uvpzGd5pu", null, null, null, null, null, null, false));
    UserCredentials userCredentials = UserCredentials.create("credentials_uid", "code", "name", null, null, null, null, null, null);
    // dependencies for the OrganisationUnitCall:
    OrganisationUnitService organisationUnitService = d2.retrofit().create(OrganisationUnitService.class);
    // Create a user with the root as assigned organisation unit (for the test):
    User user = User.create("user_uid", "code", "name", "display_name", new Date(), new Date(), "birthday", "education", "gender", "job_title", "surname", "firstName", "introduction", "employer", "interests", "languages", "email", "phoneNumber", "nationality", userCredentials, organisationUnits, organisationUnits, organisationUnits, false);
    ContentValues userContentValues = new ContentValues();
    userContentValues.put(UserModel.Columns.ID, "user_uid");
    database().insert(UserModel.TABLE, null, userContentValues);
    // inserting programs for creating OrgUnitProgramLinks
    String programUid = "uy2gU8kT1jF";
    String programUid1 = "q04UBOqq3rp";
    String programUid2 = "VBqh0ynB2wv";
    String programUid3 = "eBAyeGv0exc";
    String programUid4 = "kla3mAPgvCH";
    String programUid5 = "lxAQ7Zs9VYR";
    String programUid6 = "IpHINAT79UW";
    String programUid7 = "WSGAb5XwJ3Y";
    String programUid8 = "ur1Edk5Oe2n";
    insertProgramWithUid(programUid);
    insertProgramWithUid(programUid1);
    insertProgramWithUid(programUid2);
    insertProgramWithUid(programUid3);
    insertProgramWithUid(programUid4);
    insertProgramWithUid(programUid5);
    insertProgramWithUid(programUid6);
    insertProgramWithUid(programUid7);
    insertProgramWithUid(programUid8);
    Set<String> programUids = Sets.newHashSet(Lists.newArrayList(programUid, programUid1, programUid2, programUid3, programUid4, programUid5, programUid6, programUid7, programUid8));
    GenericHandler<OrganisationUnit, OrganisationUnitModel> organisationUnitHandler = OrganisationUnitHandler.create(databaseAdapter(), programUids, OrganisationUnitModel.Scope.SCOPE_DATA_CAPTURE, user);
    genericCallData = GenericCallData.create(databaseAdapter(), d2.retrofit());
    organisationUnitCall = new OrganisationUnitCall(user, organisationUnitService, genericCallData, organisationUnitHandler);
}
Also used : Dhis2MockServer(org.hisp.dhis.android.core.data.server.Dhis2MockServer) ContentValues(android.content.ContentValues) User(org.hisp.dhis.android.core.user.User) D2(org.hisp.dhis.android.core.D2) Date(java.util.Date) AssetsFileReader(org.hisp.dhis.android.core.data.file.AssetsFileReader) UserCredentials(org.hisp.dhis.android.core.user.UserCredentials) Before(org.junit.Before)

Example 3 with User

use of org.hisp.dhis.android.core.user.User in project dhis2-android-sdk by dhis2.

the class LogoutCallMockIntegrationShould method givenALoginInDatabase.

private void givenALoginInDatabase() throws Exception {
    dhis2MockServer.enqueueMockResponse("login.json", new Date());
    Response<User> response = d2.logIn("user", "password").call();
    assertThat(response.isSuccessful(), is(true));
}
Also used : User(org.hisp.dhis.android.core.user.User) Date(java.util.Date)

Example 4 with User

use of org.hisp.dhis.android.core.user.User in project dhis2-android-sdk by dhis2.

the class LogoutCallMockIntegrationShould method givenALoginWithSierraLeonaOUInDatabase.

private void givenALoginWithSierraLeonaOUInDatabase() throws Exception {
    dhis2MockServer.enqueueMockResponse("admin/login.json", new Date());
    Response<User> response = d2.logIn("user", "password").call();
    assertThat(response.isSuccessful(), is(true));
}
Also used : User(org.hisp.dhis.android.core.user.User) Date(java.util.Date)

Example 5 with User

use of org.hisp.dhis.android.core.user.User in project dhis2-android-sdk by dhis2.

the class MetadataCall method call.

@SuppressWarnings("PMD.NPathComplexity")
@Override
public Response call() throws Exception {
    synchronized (this) {
        if (isExecuted) {
            throw new IllegalStateException("Already executed");
        }
        isExecuted = true;
    }
    Response response = null;
    Transaction transaction = databaseAdapter.beginNewTransaction();
    try {
        response = new SystemInfoCall(databaseAdapter, systemInfoStore, systemInfoService, resourceStore).call();
        if (!response.isSuccessful()) {
            return response;
        }
        GenericCallData data = GenericCallData.create(databaseAdapter, new ResourceHandler(resourceStore), retrofit);
        Response<User> userResponse = new UserCall(userService, databaseAdapter, userStore, userCredentialsStore, userRoleStore, resourceStore, data.serverDate()).call();
        response = userResponse;
        if (!response.isSuccessful()) {
            return response;
        }
        response = downloadCategories(data.serverDate());
        if (!response.isSuccessful()) {
            return response;
        }
        response = downloadCategoryCombos(data.serverDate());
        if (!response.isSuccessful()) {
            return response;
        }
        Response<Payload<Program>> programAccessResponse = ProgramAccessEndpointCall.FACTORY.create(data, programService).call();
        response = programAccessResponse;
        if (!response.isSuccessful()) {
            return response;
        }
        Set<String> programUids = getProgramUidsWithDataReadAccess(programAccessResponse.body().items());
        response = new ProgramCall(programService, databaseAdapter, resourceStore, programUids, programStore, data.serverDate(), trackedEntityAttributeStore, programTrackedEntityAttributeStore, programRuleVariableStore, programIndicatorStore, programStageSectionProgramIndicatorLinkStore, programRuleActionStore, programRuleStore, relationshipStore, styleHandler, renderTypeHandler).call();
        if (!response.isSuccessful()) {
            return response;
        }
        List<Program> programs = ((Response<Payload<Program>>) response).body().items();
        Set<String> assignedProgramStageUids = getAssignedProgramStageUids(programs);
        Response programStageResponse = programStageCallFactory.create(data, assignedProgramStageUids).call();
        if (!programStageResponse.isSuccessful()) {
            return programStageResponse;
        }
        Set<String> trackedEntityUids = getAssignedTrackedEntityUids(programs);
        response = new TrackedEntityCall(trackedEntityUids, databaseAdapter, trackedEntityStore, resourceStore, trackedEntityService, data.serverDate()).call();
        if (!response.isSuccessful()) {
            return response;
        }
        User user = userResponse.body();
        Response<Payload<OrganisationUnit>> organisationUnitResponse = organisationUnitCallFactory.create(data, user, programUids).call();
        if (!organisationUnitResponse.isSuccessful()) {
            return organisationUnitResponse;
        }
        List<ProgramStage> programStages = ((Response<Payload<ProgramStage>>) programStageResponse).body().items();
        Set<String> optionSetUids = getAssignedOptionSetUids(programs, programStages);
        response = new OptionSetCall(data, optionSetService, optionSetHandler, optionSetUids).call();
        if (!response.isSuccessful()) {
            return response;
        }
        List<OrganisationUnit> organisationUnits = organisationUnitResponse.body().items();
        response = dataSetParentCallFactory.create(user, data, organisationUnits).call();
        if (!response.isSuccessful()) {
            return response;
        }
        transaction.setSuccessful();
        return response;
    } finally {
        transaction.end();
    }
}
Also used : OrganisationUnit(org.hisp.dhis.android.core.organisationunit.OrganisationUnit) User(org.hisp.dhis.android.core.user.User) Program(org.hisp.dhis.android.core.program.Program) TrackedEntityCall(org.hisp.dhis.android.core.trackedentity.TrackedEntityCall) ResourceHandler(org.hisp.dhis.android.core.resource.ResourceHandler) GenericCallData(org.hisp.dhis.android.core.common.GenericCallData) Response(retrofit2.Response) Transaction(org.hisp.dhis.android.core.data.database.Transaction) SystemInfoCall(org.hisp.dhis.android.core.systeminfo.SystemInfoCall) UserCall(org.hisp.dhis.android.core.user.UserCall) Payload(org.hisp.dhis.android.core.common.Payload) ProgramStage(org.hisp.dhis.android.core.program.ProgramStage) OptionSetCall(org.hisp.dhis.android.core.option.OptionSetCall) ProgramCall(org.hisp.dhis.android.core.program.ProgramCall)

Aggregations

User (org.hisp.dhis.android.core.user.User)5 Date (java.util.Date)3 Dhis2MockServer (org.hisp.dhis.android.core.data.server.Dhis2MockServer)2 Before (org.junit.Before)2 ContentValues (android.content.ContentValues)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Set (java.util.Set)1 MediaType (okhttp3.MediaType)1 D2 (org.hisp.dhis.android.core.D2)1 MetadataCall (org.hisp.dhis.android.core.calls.MetadataCall)1 CategoryComboService (org.hisp.dhis.android.core.category.CategoryComboService)1 CategoryService (org.hisp.dhis.android.core.category.CategoryService)1 GenericCallData (org.hisp.dhis.android.core.common.GenericCallData)1 Payload (org.hisp.dhis.android.core.common.Payload)1 Fields (org.hisp.dhis.android.core.data.api.Fields)1 Filter (org.hisp.dhis.android.core.data.api.Filter)1 Transaction (org.hisp.dhis.android.core.data.database.Transaction)1 AssetsFileReader (org.hisp.dhis.android.core.data.file.AssetsFileReader)1